]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - fs/nfsd/nfs4state.c
cifs: fix DFS failover
[mirror_ubuntu-hirsute-kernel.git] / fs / nfsd / nfs4state.c
CommitLineData
1da177e4 1/*
1da177e4
LT
2* Copyright (c) 2001 The Regents of the University of Michigan.
3* All rights reserved.
4*
5* Kendrick Smith <kmsmith@umich.edu>
6* Andy Adamson <kandros@umich.edu>
7*
8* Redistribution and use in source and binary forms, with or without
9* modification, are permitted provided that the following conditions
10* are met:
11*
12* 1. Redistributions of source code must retain the above copyright
13* notice, this list of conditions and the following disclaimer.
14* 2. Redistributions in binary form must reproduce the above copyright
15* notice, this list of conditions and the following disclaimer in the
16* documentation and/or other materials provided with the distribution.
17* 3. Neither the name of the University nor the names of its
18* contributors may be used to endorse or promote products derived
19* from this software without specific prior written permission.
20*
21* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
22* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*
33*/
34
aceaf78d 35#include <linux/file.h>
b89f4321 36#include <linux/fs.h>
5a0e3ad6 37#include <linux/slab.h>
0964a3d3 38#include <linux/namei.h>
c2f1a551 39#include <linux/swap.h>
17456804 40#include <linux/pagemap.h>
7df302f7 41#include <linux/ratelimit.h>
68e76ad0 42#include <linux/sunrpc/svcauth_gss.h>
5976687a 43#include <linux/sunrpc/addr.h>
87545899 44#include <linux/jhash.h>
169319f1 45#include <linux/string_helpers.h>
9a74af21 46#include "xdr4.h"
06b332a5 47#include "xdr4cb.h"
0a3adade 48#include "vfs.h"
bfa4b365 49#include "current_stateid.h"
1da177e4 50
5e1533c7 51#include "netns.h"
9cf514cc 52#include "pnfs.h"
fd4f83fd 53#include "filecache.h"
dd5e3fbc 54#include "trace.h"
5e1533c7 55
1da177e4
LT
56#define NFSDDBG_FACILITY NFSDDBG_PROC
57
f32f3c2d
BF
58#define all_ones {{~0,~0},~0}
59static const stateid_t one_stateid = {
60 .si_generation = ~0,
61 .si_opaque = all_ones,
62};
63static const stateid_t zero_stateid = {
64 /* all fields zero */
65};
19ff0f28
TM
66static const stateid_t currentstateid = {
67 .si_generation = 1,
68};
fb500a7c
TM
69static const stateid_t close_stateid = {
70 .si_generation = 0xffffffffU,
71};
f32f3c2d 72
ec6b5d7b 73static u64 current_sessionid = 1;
fd39ca9a 74
f32f3c2d
BF
75#define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
76#define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
19ff0f28 77#define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
ae254dac 78#define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
1da177e4 79
1da177e4 80/* forward declarations */
f9c00c3a 81static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
6011695d 82static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
362063a5 83void nfsd4_end_grace(struct nfsd_net *nn);
624322f1 84static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps);
1da177e4 85
8b671b80
BF
86/* Locking: */
87
8b671b80
BF
88/*
89 * Currently used for the del_recall_lru and file hash table. In an
90 * effort to decrease the scope of the client_mutex, this spinlock may
91 * eventually cover more:
92 */
cdc97505 93static DEFINE_SPINLOCK(state_lock);
8b671b80 94
4f34bd05
AE
95enum nfsd4_st_mutex_lock_subclass {
96 OPEN_STATEID_MUTEX = 0,
97 LOCK_STATEID_MUTEX = 1,
98};
99
b401be22
JL
100/*
101 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
102 * the refcount on the open stateid to drop.
103 */
104static DECLARE_WAIT_QUEUE_HEAD(close_wq);
105
89c905be
BF
106/*
107 * A waitqueue where a writer to clients/#/ctl destroying a client can
108 * wait for cl_rpc_users to drop to 0 and then for the client to be
109 * unhashed.
110 */
111static DECLARE_WAIT_QUEUE_HEAD(expiry_wq);
112
9258a2d5 113static struct kmem_cache *client_slab;
abf1135b
CH
114static struct kmem_cache *openowner_slab;
115static struct kmem_cache *lockowner_slab;
116static struct kmem_cache *file_slab;
117static struct kmem_cache *stateid_slab;
118static struct kmem_cache *deleg_slab;
8287f009 119static struct kmem_cache *odstate_slab;
e60d4398 120
66b2b9b2 121static void free_session(struct nfsd4_session *);
508dc6e1 122
c4cb8974 123static const struct nfsd4_callback_ops nfsd4_cb_recall_ops;
76d348fa 124static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops;
0162ac2b 125
f0f51f5c 126static bool is_session_dead(struct nfsd4_session *ses)
66b2b9b2 127{
f0f51f5c 128 return ses->se_flags & NFS4_SESSION_DEAD;
66b2b9b2
BF
129}
130
f0f51f5c 131static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
508dc6e1 132{
f0f51f5c 133 if (atomic_read(&ses->se_ref) > ref_held_by_me)
66b2b9b2
BF
134 return nfserr_jukebox;
135 ses->se_flags |= NFS4_SESSION_DEAD;
136 return nfs_ok;
508dc6e1
BH
137}
138
221a6876
BF
139static bool is_client_expired(struct nfs4_client *clp)
140{
141 return clp->cl_time == 0;
142}
143
221a6876
BF
144static __be32 get_client_locked(struct nfs4_client *clp)
145{
0a880a28
TM
146 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
147
148 lockdep_assert_held(&nn->client_lock);
149
221a6876
BF
150 if (is_client_expired(clp))
151 return nfserr_expired;
14ed14cc 152 atomic_inc(&clp->cl_rpc_users);
221a6876
BF
153 return nfs_ok;
154}
155
156/* must be called under the client_lock */
157static inline void
158renew_client_locked(struct nfs4_client *clp)
159{
160 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
161
162 if (is_client_expired(clp)) {
163 WARN_ON(1);
164 printk("%s: client (clientid %08x/%08x) already expired\n",
165 __func__,
166 clp->cl_clientid.cl_boot,
167 clp->cl_clientid.cl_id);
168 return;
169 }
170
221a6876 171 list_move_tail(&clp->cl_lru, &nn->client_lru);
20b7d86f 172 clp->cl_time = ktime_get_boottime_seconds();
221a6876
BF
173}
174
ba138435 175static void put_client_renew_locked(struct nfs4_client *clp)
221a6876 176{
0a880a28
TM
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
178
179 lockdep_assert_held(&nn->client_lock);
180
14ed14cc 181 if (!atomic_dec_and_test(&clp->cl_rpc_users))
221a6876
BF
182 return;
183 if (!is_client_expired(clp))
184 renew_client_locked(clp);
89c905be
BF
185 else
186 wake_up_all(&expiry_wq);
221a6876
BF
187}
188
4b24ca7d
JL
189static void put_client_renew(struct nfs4_client *clp)
190{
191 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
192
14ed14cc 193 if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock))
d6c249b4
JL
194 return;
195 if (!is_client_expired(clp))
196 renew_client_locked(clp);
89c905be
BF
197 else
198 wake_up_all(&expiry_wq);
4b24ca7d
JL
199 spin_unlock(&nn->client_lock);
200}
201
d4e19e70
TM
202static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
203{
204 __be32 status;
205
206 if (is_session_dead(ses))
207 return nfserr_badsession;
208 status = get_client_locked(ses->se_client);
209 if (status)
210 return status;
211 atomic_inc(&ses->se_ref);
212 return nfs_ok;
213}
214
215static void nfsd4_put_session_locked(struct nfsd4_session *ses)
216{
217 struct nfs4_client *clp = ses->se_client;
0a880a28
TM
218 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
219
220 lockdep_assert_held(&nn->client_lock);
d4e19e70
TM
221
222 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
223 free_session(ses);
224 put_client_renew_locked(clp);
225}
226
227static void nfsd4_put_session(struct nfsd4_session *ses)
228{
229 struct nfs4_client *clp = ses->se_client;
230 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
231
232 spin_lock(&nn->client_lock);
233 nfsd4_put_session_locked(ses);
234 spin_unlock(&nn->client_lock);
235}
236
76d348fa
JL
237static struct nfsd4_blocked_lock *
238find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
239 struct nfsd_net *nn)
240{
241 struct nfsd4_blocked_lock *cur, *found = NULL;
242
0cc11a61 243 spin_lock(&nn->blocked_locks_lock);
76d348fa
JL
244 list_for_each_entry(cur, &lo->lo_blocked, nbl_list) {
245 if (fh_match(fh, &cur->nbl_fh)) {
246 list_del_init(&cur->nbl_list);
7919d0a2 247 list_del_init(&cur->nbl_lru);
76d348fa
JL
248 found = cur;
249 break;
250 }
251 }
0cc11a61 252 spin_unlock(&nn->blocked_locks_lock);
76d348fa 253 if (found)
cb03f94f 254 locks_delete_block(&found->nbl_lock);
76d348fa
JL
255 return found;
256}
257
258static struct nfsd4_blocked_lock *
259find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh,
260 struct nfsd_net *nn)
261{
262 struct nfsd4_blocked_lock *nbl;
263
264 nbl = find_blocked_lock(lo, fh, nn);
265 if (!nbl) {
266 nbl= kmalloc(sizeof(*nbl), GFP_KERNEL);
267 if (nbl) {
e1e8399e
VA
268 INIT_LIST_HEAD(&nbl->nbl_list);
269 INIT_LIST_HEAD(&nbl->nbl_lru);
76d348fa
JL
270 fh_copy_shallow(&nbl->nbl_fh, fh);
271 locks_init_lock(&nbl->nbl_lock);
272 nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client,
273 &nfsd4_cb_notify_lock_ops,
274 NFSPROC4_CLNT_CB_NOTIFY_LOCK);
275 }
276 }
277 return nbl;
278}
279
280static void
281free_blocked_lock(struct nfsd4_blocked_lock *nbl)
282{
6aaafc43 283 locks_delete_block(&nbl->nbl_lock);
76d348fa
JL
284 locks_release_private(&nbl->nbl_lock);
285 kfree(nbl);
286}
287
68ef3bc3
JL
288static void
289remove_blocked_locks(struct nfs4_lockowner *lo)
290{
291 struct nfs4_client *clp = lo->lo_owner.so_client;
292 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
293 struct nfsd4_blocked_lock *nbl;
294 LIST_HEAD(reaplist);
295
296 /* Dequeue all blocked locks */
297 spin_lock(&nn->blocked_locks_lock);
298 while (!list_empty(&lo->lo_blocked)) {
299 nbl = list_first_entry(&lo->lo_blocked,
300 struct nfsd4_blocked_lock,
301 nbl_list);
302 list_del_init(&nbl->nbl_list);
303 list_move(&nbl->nbl_lru, &reaplist);
304 }
305 spin_unlock(&nn->blocked_locks_lock);
306
307 /* Now free them */
308 while (!list_empty(&reaplist)) {
309 nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock,
310 nbl_lru);
311 list_del_init(&nbl->nbl_lru);
68ef3bc3
JL
312 free_blocked_lock(nbl);
313 }
314}
315
f456458e
JL
316static void
317nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb)
318{
319 struct nfsd4_blocked_lock *nbl = container_of(cb,
320 struct nfsd4_blocked_lock, nbl_cb);
321 locks_delete_block(&nbl->nbl_lock);
322}
323
76d348fa
JL
324static int
325nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task)
326{
327 /*
328 * Since this is just an optimization, we don't try very hard if it
329 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
330 * just quit trying on anything else.
331 */
332 switch (task->tk_status) {
333 case -NFS4ERR_DELAY:
334 rpc_delay(task, 1 * HZ);
335 return 0;
336 default:
337 return 1;
338 }
339}
340
341static void
342nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb)
343{
344 struct nfsd4_blocked_lock *nbl = container_of(cb,
345 struct nfsd4_blocked_lock, nbl_cb);
346
347 free_blocked_lock(nbl);
348}
349
350static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = {
f456458e 351 .prepare = nfsd4_cb_notify_lock_prepare,
76d348fa
JL
352 .done = nfsd4_cb_notify_lock_done,
353 .release = nfsd4_cb_notify_lock_release,
354};
355
b5971afa
KM
356static inline struct nfs4_stateowner *
357nfs4_get_stateowner(struct nfs4_stateowner *sop)
358{
359 atomic_inc(&sop->so_count);
360 return sop;
361}
362
7ffb5880 363static int
d4f0489f 364same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
7ffb5880
TM
365{
366 return (sop->so_owner.len == owner->len) &&
d4f0489f 367 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
7ffb5880
TM
368}
369
370static struct nfs4_openowner *
371find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
d4f0489f 372 struct nfs4_client *clp)
7ffb5880
TM
373{
374 struct nfs4_stateowner *so;
7ffb5880 375
d4f0489f 376 lockdep_assert_held(&clp->cl_lock);
7ffb5880 377
d4f0489f
TM
378 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
379 so_strhash) {
7ffb5880
TM
380 if (!so->so_is_open_owner)
381 continue;
b5971afa
KM
382 if (same_owner_str(so, &open->op_owner))
383 return openowner(nfs4_get_stateowner(so));
7ffb5880
TM
384 }
385 return NULL;
386}
387
388static struct nfs4_openowner *
389find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
d4f0489f 390 struct nfs4_client *clp)
7ffb5880
TM
391{
392 struct nfs4_openowner *oo;
393
d4f0489f
TM
394 spin_lock(&clp->cl_lock);
395 oo = find_openstateowner_str_locked(hashval, open, clp);
396 spin_unlock(&clp->cl_lock);
7ffb5880
TM
397 return oo;
398}
399
1da177e4
LT
400static inline u32
401opaque_hashval(const void *ptr, int nbytes)
402{
403 unsigned char *cptr = (unsigned char *) ptr;
404
405 u32 x = 0;
406 while (nbytes--) {
407 x *= 37;
408 x += *cptr++;
409 }
410 return x;
411}
412
5b095e99 413static void nfsd4_free_file_rcu(struct rcu_head *rcu)
32513b40 414{
5b095e99
JL
415 struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu);
416
417 kmem_cache_free(file_slab, fp);
32513b40
BF
418}
419
e6ba76e1 420void
13cd2184
N
421put_nfs4_file(struct nfs4_file *fi)
422{
02e1215f
JL
423 might_lock(&state_lock);
424
818a34eb 425 if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) {
5b095e99 426 hlist_del_rcu(&fi->fi_hash);
cdc97505 427 spin_unlock(&state_lock);
8287f009 428 WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate));
5b095e99
JL
429 WARN_ON_ONCE(!list_empty(&fi->fi_delegations));
430 call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu);
8b671b80 431 }
13cd2184
N
432}
433
eb82dd39 434static struct nfsd_file *
de18643d
TM
435__nfs4_get_fd(struct nfs4_file *f, int oflag)
436{
437 if (f->fi_fds[oflag])
eb82dd39 438 return nfsd_file_get(f->fi_fds[oflag]);
de18643d
TM
439 return NULL;
440}
441
eb82dd39 442static struct nfsd_file *
de18643d
TM
443find_writeable_file_locked(struct nfs4_file *f)
444{
eb82dd39 445 struct nfsd_file *ret;
de18643d
TM
446
447 lockdep_assert_held(&f->fi_lock);
448
449 ret = __nfs4_get_fd(f, O_WRONLY);
450 if (!ret)
451 ret = __nfs4_get_fd(f, O_RDWR);
452 return ret;
453}
454
eb82dd39 455static struct nfsd_file *
de18643d
TM
456find_writeable_file(struct nfs4_file *f)
457{
eb82dd39 458 struct nfsd_file *ret;
de18643d
TM
459
460 spin_lock(&f->fi_lock);
461 ret = find_writeable_file_locked(f);
462 spin_unlock(&f->fi_lock);
463
464 return ret;
465}
466
eb82dd39
JL
467static struct nfsd_file *
468find_readable_file_locked(struct nfs4_file *f)
de18643d 469{
eb82dd39 470 struct nfsd_file *ret;
de18643d
TM
471
472 lockdep_assert_held(&f->fi_lock);
473
474 ret = __nfs4_get_fd(f, O_RDONLY);
475 if (!ret)
476 ret = __nfs4_get_fd(f, O_RDWR);
477 return ret;
478}
479
eb82dd39 480static struct nfsd_file *
de18643d
TM
481find_readable_file(struct nfs4_file *f)
482{
eb82dd39 483 struct nfsd_file *ret;
de18643d
TM
484
485 spin_lock(&f->fi_lock);
486 ret = find_readable_file_locked(f);
487 spin_unlock(&f->fi_lock);
488
489 return ret;
490}
491
eb82dd39 492struct nfsd_file *
de18643d
TM
493find_any_file(struct nfs4_file *f)
494{
eb82dd39 495 struct nfsd_file *ret;
de18643d 496
a451b123
TM
497 if (!f)
498 return NULL;
de18643d
TM
499 spin_lock(&f->fi_lock);
500 ret = __nfs4_get_fd(f, O_RDWR);
501 if (!ret) {
502 ret = __nfs4_get_fd(f, O_WRONLY);
503 if (!ret)
504 ret = __nfs4_get_fd(f, O_RDONLY);
505 }
506 spin_unlock(&f->fi_lock);
507 return ret;
508}
509
9affa435
BF
510static struct nfsd_file *find_deleg_file(struct nfs4_file *f)
511{
512 struct nfsd_file *ret = NULL;
513
514 spin_lock(&f->fi_lock);
515 if (f->fi_deleg_file)
516 ret = nfsd_file_get(f->fi_deleg_file);
517 spin_unlock(&f->fi_lock);
518 return ret;
519}
520
02a3508d 521static atomic_long_t num_delegations;
697ce9be 522unsigned long max_delegations;
ef0f3390
N
523
524/*
525 * Open owner state (share locks)
526 */
527
16bfdaaf
BF
528/* hash tables for lock and open owners */
529#define OWNER_HASH_BITS 8
530#define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
531#define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
ef0f3390 532
d4f0489f 533static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
ddc04c41
BF
534{
535 unsigned int ret;
536
537 ret = opaque_hashval(ownername->data, ownername->len);
16bfdaaf 538 return ret & OWNER_HASH_MASK;
ddc04c41 539}
ef0f3390 540
ef0f3390
N
541/* hash table for nfs4_file */
542#define FILE_HASH_BITS 8
543#define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
35079582 544
ca943217 545static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
ddc04c41 546{
ca943217
TM
547 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
548}
549
550static unsigned int file_hashval(struct knfsd_fh *fh)
551{
552 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
553}
554
89876f8c 555static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
ef0f3390 556
12659651
JL
557static void
558__nfs4_file_get_access(struct nfs4_file *fp, u32 access)
3477565e 559{
7214e860
JL
560 lockdep_assert_held(&fp->fi_lock);
561
12659651
JL
562 if (access & NFS4_SHARE_ACCESS_WRITE)
563 atomic_inc(&fp->fi_access[O_WRONLY]);
564 if (access & NFS4_SHARE_ACCESS_READ)
565 atomic_inc(&fp->fi_access[O_RDONLY]);
3477565e
BF
566}
567
12659651
JL
568static __be32
569nfs4_file_get_access(struct nfs4_file *fp, u32 access)
998db52c 570{
7214e860
JL
571 lockdep_assert_held(&fp->fi_lock);
572
12659651
JL
573 /* Does this access mode make sense? */
574 if (access & ~NFS4_SHARE_ACCESS_BOTH)
575 return nfserr_inval;
576
baeb4ff0
JL
577 /* Does it conflict with a deny mode already set? */
578 if ((access & fp->fi_share_deny) != 0)
579 return nfserr_share_denied;
580
12659651
JL
581 __nfs4_file_get_access(fp, access);
582 return nfs_ok;
998db52c
BF
583}
584
baeb4ff0
JL
585static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
586{
587 /* Common case is that there is no deny mode. */
588 if (deny) {
589 /* Does this deny mode make sense? */
590 if (deny & ~NFS4_SHARE_DENY_BOTH)
591 return nfserr_inval;
592
593 if ((deny & NFS4_SHARE_DENY_READ) &&
594 atomic_read(&fp->fi_access[O_RDONLY]))
595 return nfserr_share_denied;
596
597 if ((deny & NFS4_SHARE_DENY_WRITE) &&
598 atomic_read(&fp->fi_access[O_WRONLY]))
599 return nfserr_share_denied;
600 }
601 return nfs_ok;
602}
603
998db52c 604static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
f9d7562f 605{
de18643d
TM
606 might_lock(&fp->fi_lock);
607
608 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
fd4f83fd
JL
609 struct nfsd_file *f1 = NULL;
610 struct nfsd_file *f2 = NULL;
de18643d 611
6d338b51 612 swap(f1, fp->fi_fds[oflag]);
0c7c3e67 613 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
6d338b51 614 swap(f2, fp->fi_fds[O_RDWR]);
de18643d
TM
615 spin_unlock(&fp->fi_lock);
616 if (f1)
fd4f83fd 617 nfsd_file_put(f1);
de18643d 618 if (f2)
fd4f83fd 619 nfsd_file_put(f2);
f9d7562f
BF
620 }
621}
622
12659651 623static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
998db52c 624{
12659651
JL
625 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
626
627 if (access & NFS4_SHARE_ACCESS_WRITE)
998db52c 628 __nfs4_file_put_access(fp, O_WRONLY);
12659651
JL
629 if (access & NFS4_SHARE_ACCESS_READ)
630 __nfs4_file_put_access(fp, O_RDONLY);
998db52c
BF
631}
632
8287f009
SB
633/*
634 * Allocate a new open/delegation state counter. This is needed for
635 * pNFS for proper return on close semantics.
636 *
637 * Note that we only allocate it for pNFS-enabled exports, otherwise
638 * all pointers to struct nfs4_clnt_odstate are always NULL.
639 */
640static struct nfs4_clnt_odstate *
641alloc_clnt_odstate(struct nfs4_client *clp)
642{
643 struct nfs4_clnt_odstate *co;
644
645 co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL);
646 if (co) {
647 co->co_client = clp;
cff7cb2e 648 refcount_set(&co->co_odcount, 1);
8287f009
SB
649 }
650 return co;
651}
652
653static void
654hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co)
655{
656 struct nfs4_file *fp = co->co_file;
657
658 lockdep_assert_held(&fp->fi_lock);
659 list_add(&co->co_perfile, &fp->fi_clnt_odstate);
660}
661
662static inline void
663get_clnt_odstate(struct nfs4_clnt_odstate *co)
664{
665 if (co)
cff7cb2e 666 refcount_inc(&co->co_odcount);
8287f009
SB
667}
668
669static void
670put_clnt_odstate(struct nfs4_clnt_odstate *co)
671{
672 struct nfs4_file *fp;
673
674 if (!co)
675 return;
676
677 fp = co->co_file;
cff7cb2e 678 if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) {
8287f009
SB
679 list_del(&co->co_perfile);
680 spin_unlock(&fp->fi_lock);
681
682 nfsd4_return_all_file_layouts(co->co_client, fp);
683 kmem_cache_free(odstate_slab, co);
684 }
685}
686
687static struct nfs4_clnt_odstate *
688find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new)
689{
690 struct nfs4_clnt_odstate *co;
691 struct nfs4_client *cl;
692
693 if (!new)
694 return NULL;
695
696 cl = new->co_client;
697
698 spin_lock(&fp->fi_lock);
699 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
700 if (co->co_client == cl) {
701 get_clnt_odstate(co);
702 goto out;
703 }
704 }
705 co = new;
706 co->co_file = fp;
707 hash_clnt_odstate_locked(new);
708out:
709 spin_unlock(&fp->fi_lock);
710 return co;
711}
712
d19fb70d
KM
713struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab,
714 void (*sc_free)(struct nfs4_stid *))
2a74aba7 715{
3abdb607 716 struct nfs4_stid *stid;
6136d2b4 717 int new_id;
2a74aba7 718
f8338834 719 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
3abdb607
BF
720 if (!stid)
721 return NULL;
722
4770d722
JL
723 idr_preload(GFP_KERNEL);
724 spin_lock(&cl->cl_lock);
78599c42
BF
725 /* Reserving 0 for start of file in nfsdfs "states" file: */
726 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT);
4770d722
JL
727 spin_unlock(&cl->cl_lock);
728 idr_preload_end();
ebd6c707 729 if (new_id < 0)
3abdb607 730 goto out_free;
d19fb70d
KM
731
732 stid->sc_free = sc_free;
2a74aba7 733 stid->sc_client = cl;
3abdb607
BF
734 stid->sc_stateid.si_opaque.so_id = new_id;
735 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
2a74aba7 736 /* Will be incremented before return to client: */
a15dfcd5 737 refcount_set(&stid->sc_count, 1);
9767feb2 738 spin_lock_init(&stid->sc_lock);
624322f1 739 INIT_LIST_HEAD(&stid->sc_cp_list);
996e0938 740
996e0938 741 /*
3abdb607
BF
742 * It shouldn't be a problem to reuse an opaque stateid value.
743 * I don't think it is for 4.1. But with 4.0 I worry that, for
744 * example, a stray write retransmission could be accepted by
745 * the server when it should have been rejected. Therefore,
746 * adopt a trick from the sctp code to attempt to maximize the
747 * amount of time until an id is reused, by ensuring they always
748 * "increase" (mod INT_MAX):
996e0938 749 */
3abdb607
BF
750 return stid;
751out_free:
2c44a234 752 kmem_cache_free(slab, stid);
3abdb607 753 return NULL;
2a74aba7
BF
754}
755
e0639dc5
OK
756/*
757 * Create a unique stateid_t to represent each COPY.
758 */
624322f1
OK
759static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid,
760 unsigned char sc_type)
e0639dc5
OK
761{
762 int new_id;
763
9cc76801 764 stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time;
624322f1
OK
765 stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id;
766 stid->sc_type = sc_type;
767
e0639dc5
OK
768 idr_preload(GFP_KERNEL);
769 spin_lock(&nn->s2s_cp_lock);
624322f1
OK
770 new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT);
771 stid->stid.si_opaque.so_id = new_id;
ca9364dd 772 stid->stid.si_generation = 1;
e0639dc5
OK
773 spin_unlock(&nn->s2s_cp_lock);
774 idr_preload_end();
775 if (new_id < 0)
776 return 0;
e0639dc5
OK
777 return 1;
778}
779
624322f1
OK
780int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy)
781{
782 return nfs4_init_cp_state(nn, &copy->cp_stateid, NFS4_COPY_STID);
783}
784
785struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn,
786 struct nfs4_stid *p_stid)
787{
788 struct nfs4_cpntf_state *cps;
789
790 cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL);
791 if (!cps)
792 return NULL;
20b7d86f 793 cps->cpntf_time = ktime_get_boottime_seconds();
624322f1
OK
794 refcount_set(&cps->cp_stateid.sc_count, 1);
795 if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID))
796 goto out_free;
797 spin_lock(&nn->s2s_cp_lock);
798 list_add(&cps->cp_list, &p_stid->sc_cp_list);
799 spin_unlock(&nn->s2s_cp_lock);
800 return cps;
801out_free:
802 kfree(cps);
803 return NULL;
804}
805
806void nfs4_free_copy_state(struct nfsd4_copy *copy)
e0639dc5
OK
807{
808 struct nfsd_net *nn;
809
624322f1 810 WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID);
e0639dc5
OK
811 nn = net_generic(copy->cp_clp->net, nfsd_net_id);
812 spin_lock(&nn->s2s_cp_lock);
624322f1
OK
813 idr_remove(&nn->s2s_cp_stateids,
814 copy->cp_stateid.stid.si_opaque.so_id);
815 spin_unlock(&nn->s2s_cp_lock);
816}
817
818static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid)
819{
820 struct nfs4_cpntf_state *cps;
821 struct nfsd_net *nn;
822
823 nn = net_generic(net, nfsd_net_id);
824 spin_lock(&nn->s2s_cp_lock);
825 while (!list_empty(&stid->sc_cp_list)) {
826 cps = list_first_entry(&stid->sc_cp_list,
827 struct nfs4_cpntf_state, cp_list);
828 _free_cpntf_state_locked(nn, cps);
829 }
e0639dc5
OK
830 spin_unlock(&nn->s2s_cp_lock);
831}
832
b49e084d 833static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
4cdc951b 834{
6011695d 835 struct nfs4_stid *stid;
6011695d 836
d19fb70d 837 stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid);
6011695d
TM
838 if (!stid)
839 return NULL;
840
d19fb70d 841 return openlockstateid(stid);
6011695d
TM
842}
843
844static void nfs4_free_deleg(struct nfs4_stid *stid)
845{
6011695d
TM
846 kmem_cache_free(deleg_slab, stid);
847 atomic_long_dec(&num_delegations);
4cdc951b
BF
848}
849
6282cd56
N
850/*
851 * When we recall a delegation, we should be careful not to hand it
852 * out again straight away.
853 * To ensure this we keep a pair of bloom filters ('new' and 'old')
854 * in which the filehandles of recalled delegations are "stored".
855 * If a filehandle appear in either filter, a delegation is blocked.
856 * When a delegation is recalled, the filehandle is stored in the "new"
857 * filter.
858 * Every 30 seconds we swap the filters and clear the "new" one,
859 * unless both are empty of course.
860 *
861 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
862 * low 3 bytes as hash-table indices.
863 *
f54fe962 864 * 'blocked_delegations_lock', which is always taken in block_delegations(),
6282cd56
N
865 * is used to manage concurrent access. Testing does not need the lock
866 * except when swapping the two filters.
867 */
f54fe962 868static DEFINE_SPINLOCK(blocked_delegations_lock);
6282cd56
N
869static struct bloom_pair {
870 int entries, old_entries;
b3f255ef 871 time64_t swap_time;
6282cd56
N
872 int new; /* index into 'set' */
873 DECLARE_BITMAP(set[2], 256);
874} blocked_delegations;
875
876static int delegation_blocked(struct knfsd_fh *fh)
877{
878 u32 hash;
879 struct bloom_pair *bd = &blocked_delegations;
880
881 if (bd->entries == 0)
882 return 0;
b3f255ef 883 if (ktime_get_seconds() - bd->swap_time > 30) {
f54fe962 884 spin_lock(&blocked_delegations_lock);
b3f255ef 885 if (ktime_get_seconds() - bd->swap_time > 30) {
6282cd56
N
886 bd->entries -= bd->old_entries;
887 bd->old_entries = bd->entries;
888 memset(bd->set[bd->new], 0,
889 sizeof(bd->set[0]));
890 bd->new = 1-bd->new;
b3f255ef 891 bd->swap_time = ktime_get_seconds();
6282cd56 892 }
f54fe962 893 spin_unlock(&blocked_delegations_lock);
6282cd56 894 }
87545899 895 hash = jhash(&fh->fh_base, fh->fh_size, 0);
6282cd56
N
896 if (test_bit(hash&255, bd->set[0]) &&
897 test_bit((hash>>8)&255, bd->set[0]) &&
898 test_bit((hash>>16)&255, bd->set[0]))
899 return 1;
900
901 if (test_bit(hash&255, bd->set[1]) &&
902 test_bit((hash>>8)&255, bd->set[1]) &&
903 test_bit((hash>>16)&255, bd->set[1]))
904 return 1;
905
906 return 0;
907}
908
909static void block_delegations(struct knfsd_fh *fh)
910{
911 u32 hash;
912 struct bloom_pair *bd = &blocked_delegations;
913
87545899 914 hash = jhash(&fh->fh_base, fh->fh_size, 0);
6282cd56 915
f54fe962 916 spin_lock(&blocked_delegations_lock);
6282cd56
N
917 __set_bit(hash&255, bd->set[bd->new]);
918 __set_bit((hash>>8)&255, bd->set[bd->new]);
919 __set_bit((hash>>16)&255, bd->set[bd->new]);
920 if (bd->entries == 0)
b3f255ef 921 bd->swap_time = ktime_get_seconds();
6282cd56 922 bd->entries += 1;
f54fe962 923 spin_unlock(&blocked_delegations_lock);
6282cd56
N
924}
925
1da177e4 926static struct nfs4_delegation *
86d29b10
BF
927alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp,
928 struct svc_fh *current_fh,
8287f009 929 struct nfs4_clnt_odstate *odstate)
1da177e4
LT
930{
931 struct nfs4_delegation *dp;
02a3508d 932 long n;
1da177e4
LT
933
934 dprintk("NFSD alloc_init_deleg\n");
02a3508d
TM
935 n = atomic_long_inc_return(&num_delegations);
936 if (n < 0 || n > max_delegations)
937 goto out_dec;
6282cd56 938 if (delegation_blocked(&current_fh->fh_handle))
02a3508d 939 goto out_dec;
d19fb70d 940 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg));
5b2d21c1 941 if (dp == NULL)
02a3508d 942 goto out_dec;
6011695d 943
2a74aba7
BF
944 /*
945 * delegation seqid's are never incremented. The 4.1 special
6136d2b4
BF
946 * meaning of seqid 0 isn't meaningful, really, but let's avoid
947 * 0 anyway just for consistency and use 1:
2a74aba7
BF
948 */
949 dp->dl_stid.sc_stateid.si_generation = 1;
ea1da636
N
950 INIT_LIST_HEAD(&dp->dl_perfile);
951 INIT_LIST_HEAD(&dp->dl_perclnt);
1da177e4 952 INIT_LIST_HEAD(&dp->dl_recall_lru);
8287f009
SB
953 dp->dl_clnt_odstate = odstate;
954 get_clnt_odstate(odstate);
99c41515 955 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
f0b5de1b
CH
956 dp->dl_retries = 1;
957 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
0162ac2b 958 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
86d29b10
BF
959 get_nfs4_file(fp);
960 dp->dl_stid.sc_file = fp;
1da177e4 961 return dp;
02a3508d
TM
962out_dec:
963 atomic_long_dec(&num_delegations);
964 return NULL;
1da177e4
LT
965}
966
967void
6011695d 968nfs4_put_stid(struct nfs4_stid *s)
1da177e4 969{
11b9164a 970 struct nfs4_file *fp = s->sc_file;
6011695d
TM
971 struct nfs4_client *clp = s->sc_client;
972
4770d722
JL
973 might_lock(&clp->cl_lock);
974
a15dfcd5 975 if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
b401be22 976 wake_up_all(&close_wq);
6011695d 977 return;
b401be22 978 }
6011695d 979 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
624322f1 980 nfs4_free_cpntf_statelist(clp->net, s);
4770d722 981 spin_unlock(&clp->cl_lock);
6011695d 982 s->sc_free(s);
11b9164a
TM
983 if (fp)
984 put_nfs4_file(fp);
1da177e4
LT
985}
986
9767feb2
JL
987void
988nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid)
989{
990 stateid_t *src = &stid->sc_stateid;
991
992 spin_lock(&stid->sc_lock);
993 if (unlikely(++src->si_generation == 0))
994 src->si_generation = 1;
995 memcpy(dst, src, sizeof(*dst));
996 spin_unlock(&stid->sc_lock);
997}
998
353601e7 999static void put_deleg_file(struct nfs4_file *fp)
1da177e4 1000{
eb82dd39 1001 struct nfsd_file *nf = NULL;
b8232d33 1002
6bcc034e 1003 spin_lock(&fp->fi_lock);
353601e7 1004 if (--fp->fi_delegees == 0)
eb82dd39 1005 swap(nf, fp->fi_deleg_file);
6bcc034e
JL
1006 spin_unlock(&fp->fi_lock);
1007
eb82dd39
JL
1008 if (nf)
1009 nfsd_file_put(nf);
353601e7
BF
1010}
1011
1012static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp)
1013{
1014 struct nfs4_file *fp = dp->dl_stid.sc_file;
eb82dd39 1015 struct nfsd_file *nf = fp->fi_deleg_file;
353601e7
BF
1016
1017 WARN_ON_ONCE(!fp->fi_delegees);
1018
eb82dd39 1019 vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp);
353601e7 1020 put_deleg_file(fp);
1da177e4
LT
1021}
1022
0af6e690
BF
1023static void destroy_unhashed_deleg(struct nfs4_delegation *dp)
1024{
1025 put_clnt_odstate(dp->dl_clnt_odstate);
353601e7 1026 nfs4_unlock_deleg_lease(dp);
0af6e690
BF
1027 nfs4_put_stid(&dp->dl_stid);
1028}
1029
cd61c522 1030void nfs4_unhash_stid(struct nfs4_stid *s)
6136d2b4 1031{
3abdb607 1032 s->sc_type = 0;
6136d2b4
BF
1033}
1034
34ed9872 1035/**
68b18f52 1036 * nfs4_delegation_exists - Discover if this delegation already exists
34ed9872
AE
1037 * @clp: a pointer to the nfs4_client we're granting a delegation to
1038 * @fp: a pointer to the nfs4_file we're granting a delegation on
1039 *
1040 * Return:
68b18f52 1041 * On success: true iff an existing delegation is found
34ed9872
AE
1042 */
1043
68b18f52
BF
1044static bool
1045nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp)
34ed9872
AE
1046{
1047 struct nfs4_delegation *searchdp = NULL;
1048 struct nfs4_client *searchclp = NULL;
1049
1050 lockdep_assert_held(&state_lock);
1051 lockdep_assert_held(&fp->fi_lock);
1052
1053 list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) {
1054 searchclp = searchdp->dl_stid.sc_client;
1055 if (clp == searchclp) {
51d87bc2 1056 return true;
34ed9872
AE
1057 }
1058 }
51d87bc2 1059 return false;
34ed9872
AE
1060}
1061
1062/**
1063 * hash_delegation_locked - Add a delegation to the appropriate lists
1064 * @dp: a pointer to the nfs4_delegation we are adding.
1065 * @fp: a pointer to the nfs4_file we're granting a delegation on
1066 *
1067 * Return:
1068 * On success: NULL if the delegation was successfully hashed.
1069 *
1070 * On error: -EAGAIN if one was previously granted to this
1071 * nfs4_client for this nfs4_file. Delegation is not hashed.
1072 *
1073 */
1074
1075static int
931ee56c
BH
1076hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
1077{
34ed9872
AE
1078 struct nfs4_client *clp = dp->dl_stid.sc_client;
1079
cdc97505 1080 lockdep_assert_held(&state_lock);
417c6629 1081 lockdep_assert_held(&fp->fi_lock);
931ee56c 1082
68b18f52
BF
1083 if (nfs4_delegation_exists(clp, fp))
1084 return -EAGAIN;
a15dfcd5 1085 refcount_inc(&dp->dl_stid.sc_count);
3fb87d13 1086 dp->dl_stid.sc_type = NFS4_DELEG_STID;
931ee56c 1087 list_add(&dp->dl_perfile, &fp->fi_delegations);
34ed9872
AE
1088 list_add(&dp->dl_perclnt, &clp->cl_delegations);
1089 return 0;
931ee56c
BH
1090}
1091
3fcbbd24 1092static bool
42690676 1093unhash_delegation_locked(struct nfs4_delegation *dp)
1da177e4 1094{
11b9164a 1095 struct nfs4_file *fp = dp->dl_stid.sc_file;
02e1215f 1096
42690676
JL
1097 lockdep_assert_held(&state_lock);
1098
3fcbbd24
JL
1099 if (list_empty(&dp->dl_perfile))
1100 return false;
1101
b0fc29d6 1102 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
d55a166c
JL
1103 /* Ensure that deleg break won't try to requeue it */
1104 ++dp->dl_time;
417c6629 1105 spin_lock(&fp->fi_lock);
931ee56c 1106 list_del_init(&dp->dl_perclnt);
1da177e4 1107 list_del_init(&dp->dl_recall_lru);
02e1215f
JL
1108 list_del_init(&dp->dl_perfile);
1109 spin_unlock(&fp->fi_lock);
3fcbbd24 1110 return true;
3bd64a5b
BF
1111}
1112
3bd64a5b
BF
1113static void destroy_delegation(struct nfs4_delegation *dp)
1114{
3fcbbd24
JL
1115 bool unhashed;
1116
42690676 1117 spin_lock(&state_lock);
3fcbbd24 1118 unhashed = unhash_delegation_locked(dp);
42690676 1119 spin_unlock(&state_lock);
0af6e690
BF
1120 if (unhashed)
1121 destroy_unhashed_deleg(dp);
3bd64a5b
BF
1122}
1123
1124static void revoke_delegation(struct nfs4_delegation *dp)
1125{
1126 struct nfs4_client *clp = dp->dl_stid.sc_client;
1127
2d4a532d
JL
1128 WARN_ON(!list_empty(&dp->dl_recall_lru));
1129
0af6e690 1130 if (clp->cl_minorversion) {
3bd64a5b 1131 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
0af6e690 1132 refcount_inc(&dp->dl_stid.sc_count);
2d4a532d
JL
1133 spin_lock(&clp->cl_lock);
1134 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
1135 spin_unlock(&clp->cl_lock);
3bd64a5b 1136 }
0af6e690 1137 destroy_unhashed_deleg(dp);
3bd64a5b
BF
1138}
1139
1da177e4
LT
1140/*
1141 * SETCLIENTID state
1142 */
1143
ddc04c41
BF
1144static unsigned int clientid_hashval(u32 id)
1145{
1146 return id & CLIENT_HASH_MASK;
1147}
1148
6b189105 1149static unsigned int clientstr_hashval(struct xdr_netobj name)
ddc04c41 1150{
6b189105 1151 return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK;
ddc04c41
BF
1152}
1153
f9d7562f
BF
1154/*
1155 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1156 * st_{access,deny}_bmap field of the stateid, in order to track not
1157 * only what share bits are currently in force, but also what
1158 * combinations of share bits previous opens have used. This allows us
1159 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1160 * return an error if the client attempt to downgrade to a combination
1161 * of share bits not explicable by closing some of its previous opens.
1162 *
1163 * XXX: This enforcement is actually incomplete, since we don't keep
1164 * track of access/deny bit combinations; so, e.g., we allow:
1165 *
1166 * OPEN allow read, deny write
1167 * OPEN allow both, deny none
1168 * DOWNGRADE allow read, deny none
1169 *
1170 * which we should reject.
1171 */
5ae037e5
JL
1172static unsigned int
1173bmap_to_share_mode(unsigned long bmap) {
f9d7562f 1174 int i;
5ae037e5 1175 unsigned int access = 0;
f9d7562f 1176
f9d7562f
BF
1177 for (i = 1; i < 4; i++) {
1178 if (test_bit(i, &bmap))
5ae037e5 1179 access |= i;
f9d7562f 1180 }
5ae037e5 1181 return access;
f9d7562f
BF
1182}
1183
82c5ff1b
JL
1184/* set share access for a given stateid */
1185static inline void
1186set_access(u32 access, struct nfs4_ol_stateid *stp)
1187{
c11c591f
JL
1188 unsigned char mask = 1 << access;
1189
1190 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1191 stp->st_access_bmap |= mask;
82c5ff1b
JL
1192}
1193
1194/* clear share access for a given stateid */
1195static inline void
1196clear_access(u32 access, struct nfs4_ol_stateid *stp)
1197{
c11c591f
JL
1198 unsigned char mask = 1 << access;
1199
1200 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
1201 stp->st_access_bmap &= ~mask;
82c5ff1b
JL
1202}
1203
1204/* test whether a given stateid has access */
1205static inline bool
1206test_access(u32 access, struct nfs4_ol_stateid *stp)
1207{
c11c591f
JL
1208 unsigned char mask = 1 << access;
1209
1210 return (bool)(stp->st_access_bmap & mask);
82c5ff1b
JL
1211}
1212
ce0fc43c
JL
1213/* set share deny for a given stateid */
1214static inline void
c11c591f 1215set_deny(u32 deny, struct nfs4_ol_stateid *stp)
ce0fc43c 1216{
c11c591f
JL
1217 unsigned char mask = 1 << deny;
1218
1219 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1220 stp->st_deny_bmap |= mask;
ce0fc43c
JL
1221}
1222
1223/* clear share deny for a given stateid */
1224static inline void
c11c591f 1225clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
ce0fc43c 1226{
c11c591f
JL
1227 unsigned char mask = 1 << deny;
1228
1229 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
1230 stp->st_deny_bmap &= ~mask;
ce0fc43c
JL
1231}
1232
1233/* test whether a given stateid is denying specific access */
1234static inline bool
c11c591f 1235test_deny(u32 deny, struct nfs4_ol_stateid *stp)
ce0fc43c 1236{
c11c591f
JL
1237 unsigned char mask = 1 << deny;
1238
1239 return (bool)(stp->st_deny_bmap & mask);
f9d7562f
BF
1240}
1241
1242static int nfs4_access_to_omode(u32 access)
1243{
8f34a430 1244 switch (access & NFS4_SHARE_ACCESS_BOTH) {
f9d7562f
BF
1245 case NFS4_SHARE_ACCESS_READ:
1246 return O_RDONLY;
1247 case NFS4_SHARE_ACCESS_WRITE:
1248 return O_WRONLY;
1249 case NFS4_SHARE_ACCESS_BOTH:
1250 return O_RDWR;
1251 }
063b0fb9
BF
1252 WARN_ON_ONCE(1);
1253 return O_RDONLY;
f9d7562f
BF
1254}
1255
baeb4ff0
JL
1256/*
1257 * A stateid that had a deny mode associated with it is being released
1258 * or downgraded. Recalculate the deny mode on the file.
1259 */
1260static void
1261recalculate_deny_mode(struct nfs4_file *fp)
1262{
1263 struct nfs4_ol_stateid *stp;
1264
1265 spin_lock(&fp->fi_lock);
1266 fp->fi_share_deny = 0;
1267 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
1268 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
1269 spin_unlock(&fp->fi_lock);
1270}
1271
1272static void
1273reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
1274{
1275 int i;
1276 bool change = false;
1277
1278 for (i = 1; i < 4; i++) {
1279 if ((i & deny) != i) {
1280 change = true;
1281 clear_deny(i, stp);
1282 }
1283 }
1284
1285 /* Recalculate per-file deny mode if there was a change */
1286 if (change)
11b9164a 1287 recalculate_deny_mode(stp->st_stid.sc_file);
baeb4ff0
JL
1288}
1289
82c5ff1b
JL
1290/* release all access and file references for a given stateid */
1291static void
1292release_all_access(struct nfs4_ol_stateid *stp)
1293{
1294 int i;
11b9164a 1295 struct nfs4_file *fp = stp->st_stid.sc_file;
baeb4ff0
JL
1296
1297 if (fp && stp->st_deny_bmap != 0)
1298 recalculate_deny_mode(fp);
82c5ff1b
JL
1299
1300 for (i = 1; i < 4; i++) {
1301 if (test_access(i, stp))
11b9164a 1302 nfs4_file_put_access(stp->st_stid.sc_file, i);
82c5ff1b
JL
1303 clear_access(i, stp);
1304 }
1305}
1306
d50ffded
KM
1307static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop)
1308{
1309 kfree(sop->so_owner.data);
1310 sop->so_ops->so_free(sop);
1311}
1312
6b180f0b
JL
1313static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
1314{
a819ecc1
JL
1315 struct nfs4_client *clp = sop->so_client;
1316
1317 might_lock(&clp->cl_lock);
1318
1319 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
6b180f0b 1320 return;
8f4b54c5 1321 sop->so_ops->so_unhash(sop);
a819ecc1 1322 spin_unlock(&clp->cl_lock);
d50ffded 1323 nfs4_free_stateowner(sop);
6b180f0b
JL
1324}
1325
a451b123
TM
1326static bool
1327nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp)
1328{
1329 return list_empty(&stp->st_perfile);
1330}
1331
e8568739 1332static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp)
529d7b2a 1333{
11b9164a 1334 struct nfs4_file *fp = stp->st_stid.sc_file;
1d31a253 1335
1c755dc1
JL
1336 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
1337
e8568739
JL
1338 if (list_empty(&stp->st_perfile))
1339 return false;
1340
1d31a253 1341 spin_lock(&fp->fi_lock);
e8568739 1342 list_del_init(&stp->st_perfile);
1d31a253 1343 spin_unlock(&fp->fi_lock);
529d7b2a 1344 list_del(&stp->st_perstateowner);
e8568739 1345 return true;
529d7b2a
BF
1346}
1347
6011695d 1348static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
529d7b2a 1349{
6011695d 1350 struct nfs4_ol_stateid *stp = openlockstateid(stid);
4665e2ba 1351
8287f009 1352 put_clnt_odstate(stp->st_clnt_odstate);
6011695d 1353 release_all_access(stp);
d3134b10
JL
1354 if (stp->st_stateowner)
1355 nfs4_put_stateowner(stp->st_stateowner);
6011695d 1356 kmem_cache_free(stateid_slab, stid);
529d7b2a
BF
1357}
1358
b49e084d 1359static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
529d7b2a 1360{
b49e084d
JL
1361 struct nfs4_ol_stateid *stp = openlockstateid(stid);
1362 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
eb82dd39 1363 struct nfsd_file *nf;
529d7b2a 1364
eb82dd39
JL
1365 nf = find_any_file(stp->st_stid.sc_file);
1366 if (nf) {
1367 get_file(nf->nf_file);
1368 filp_close(nf->nf_file, (fl_owner_t)lo);
1369 nfsd_file_put(nf);
1370 }
b49e084d
JL
1371 nfs4_free_ol_stateid(stid);
1372}
1373
2c41beb0
JL
1374/*
1375 * Put the persistent reference to an already unhashed generic stateid, while
1376 * holding the cl_lock. If it's the last reference, then put it onto the
1377 * reaplist for later destruction.
1378 */
1379static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
1380 struct list_head *reaplist)
1381{
1382 struct nfs4_stid *s = &stp->st_stid;
1383 struct nfs4_client *clp = s->sc_client;
1384
1385 lockdep_assert_held(&clp->cl_lock);
1386
1387 WARN_ON_ONCE(!list_empty(&stp->st_locks));
1388
a15dfcd5 1389 if (!refcount_dec_and_test(&s->sc_count)) {
2c41beb0
JL
1390 wake_up_all(&close_wq);
1391 return;
1392 }
1393
1394 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
1395 list_add(&stp->st_locks, reaplist);
1396}
1397
e8568739 1398static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp)
b49e084d 1399{
f46c445b 1400 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
3c1c995c 1401
a451b123
TM
1402 if (!unhash_ol_stateid(stp))
1403 return false;
3c1c995c 1404 list_del_init(&stp->st_locks);
cd61c522 1405 nfs4_unhash_stid(&stp->st_stid);
a451b123 1406 return true;
3c1c995c
JL
1407}
1408
1409static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1410{
f46c445b 1411 struct nfs4_client *clp = stp->st_stid.sc_client;
e8568739 1412 bool unhashed;
3c1c995c 1413
f46c445b 1414 spin_lock(&clp->cl_lock);
e8568739 1415 unhashed = unhash_lock_stateid(stp);
f46c445b 1416 spin_unlock(&clp->cl_lock);
e8568739
JL
1417 if (unhashed)
1418 nfs4_put_stid(&stp->st_stid);
529d7b2a
BF
1419}
1420
c58c6610 1421static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
8f4b54c5 1422{
d4f0489f 1423 struct nfs4_client *clp = lo->lo_owner.so_client;
c58c6610 1424
d4f0489f 1425 lockdep_assert_held(&clp->cl_lock);
c58c6610 1426
8f4b54c5
JL
1427 list_del_init(&lo->lo_owner.so_strhash);
1428}
1429
2c41beb0
JL
1430/*
1431 * Free a list of generic stateids that were collected earlier after being
1432 * fully unhashed.
1433 */
1434static void
1435free_ol_stateid_reaplist(struct list_head *reaplist)
1436{
1437 struct nfs4_ol_stateid *stp;
fb94d766 1438 struct nfs4_file *fp;
2c41beb0
JL
1439
1440 might_sleep();
1441
1442 while (!list_empty(reaplist)) {
1443 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1444 st_locks);
1445 list_del(&stp->st_locks);
fb94d766 1446 fp = stp->st_stid.sc_file;
2c41beb0 1447 stp->st_stid.sc_free(&stp->st_stid);
fb94d766
KM
1448 if (fp)
1449 put_nfs4_file(fp);
2c41beb0
JL
1450 }
1451}
1452
d83017f9
JL
1453static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1454 struct list_head *reaplist)
3c87b9b7
TM
1455{
1456 struct nfs4_ol_stateid *stp;
1457
e8568739
JL
1458 lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock);
1459
3c87b9b7
TM
1460 while (!list_empty(&open_stp->st_locks)) {
1461 stp = list_entry(open_stp->st_locks.next,
1462 struct nfs4_ol_stateid, st_locks);
e8568739 1463 WARN_ON(!unhash_lock_stateid(stp));
d83017f9 1464 put_ol_stateid_locked(stp, reaplist);
529d7b2a
BF
1465 }
1466}
1467
e8568739 1468static bool unhash_open_stateid(struct nfs4_ol_stateid *stp,
d83017f9 1469 struct list_head *reaplist)
2283963f 1470{
2c41beb0
JL
1471 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1472
a451b123
TM
1473 if (!unhash_ol_stateid(stp))
1474 return false;
d83017f9 1475 release_open_stateid_locks(stp, reaplist);
a451b123 1476 return true;
38c387b5
BF
1477}
1478
1479static void release_open_stateid(struct nfs4_ol_stateid *stp)
1480{
2c41beb0
JL
1481 LIST_HEAD(reaplist);
1482
1483 spin_lock(&stp->st_stid.sc_client->cl_lock);
e8568739
JL
1484 if (unhash_open_stateid(stp, &reaplist))
1485 put_ol_stateid_locked(stp, &reaplist);
2c41beb0
JL
1486 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1487 free_ol_stateid_reaplist(&reaplist);
2283963f
BF
1488}
1489
7ffb5880 1490static void unhash_openowner_locked(struct nfs4_openowner *oo)
f1d110ca 1491{
d4f0489f 1492 struct nfs4_client *clp = oo->oo_owner.so_client;
7ffb5880 1493
d4f0489f 1494 lockdep_assert_held(&clp->cl_lock);
7ffb5880 1495
8f4b54c5
JL
1496 list_del_init(&oo->oo_owner.so_strhash);
1497 list_del_init(&oo->oo_perclient);
f1d110ca
BF
1498}
1499
f7a4d872
BF
1500static void release_last_closed_stateid(struct nfs4_openowner *oo)
1501{
217526e7
JL
1502 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1503 nfsd_net_id);
1504 struct nfs4_ol_stateid *s;
f7a4d872 1505
217526e7
JL
1506 spin_lock(&nn->client_lock);
1507 s = oo->oo_last_closed_stid;
f7a4d872 1508 if (s) {
d3134b10 1509 list_del_init(&oo->oo_close_lru);
f7a4d872
BF
1510 oo->oo_last_closed_stid = NULL;
1511 }
217526e7
JL
1512 spin_unlock(&nn->client_lock);
1513 if (s)
1514 nfs4_put_stid(&s->st_stid);
f7a4d872
BF
1515}
1516
2c41beb0 1517static void release_openowner(struct nfs4_openowner *oo)
8f4b54c5
JL
1518{
1519 struct nfs4_ol_stateid *stp;
d4f0489f 1520 struct nfs4_client *clp = oo->oo_owner.so_client;
2c41beb0 1521 struct list_head reaplist;
7ffb5880 1522
2c41beb0 1523 INIT_LIST_HEAD(&reaplist);
8f4b54c5 1524
2c41beb0
JL
1525 spin_lock(&clp->cl_lock);
1526 unhash_openowner_locked(oo);
8f4b54c5
JL
1527 while (!list_empty(&oo->oo_owner.so_stateids)) {
1528 stp = list_first_entry(&oo->oo_owner.so_stateids,
1529 struct nfs4_ol_stateid, st_perstateowner);
e8568739
JL
1530 if (unhash_open_stateid(stp, &reaplist))
1531 put_ol_stateid_locked(stp, &reaplist);
8f4b54c5 1532 }
d4f0489f 1533 spin_unlock(&clp->cl_lock);
2c41beb0 1534 free_ol_stateid_reaplist(&reaplist);
f7a4d872 1535 release_last_closed_stateid(oo);
6b180f0b 1536 nfs4_put_stateowner(&oo->oo_owner);
f1d110ca
BF
1537}
1538
5282fd72
ME
1539static inline int
1540hash_sessionid(struct nfs4_sessionid *sessionid)
1541{
1542 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1543
1544 return sid->sequence % SESSION_HASH_SIZE;
1545}
1546
135dd002 1547#ifdef CONFIG_SUNRPC_DEBUG
5282fd72
ME
1548static inline void
1549dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1550{
1551 u32 *ptr = (u32 *)(&sessionid->data[0]);
1552 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1553}
8f199b82
TM
1554#else
1555static inline void
1556dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1557{
1558}
1559#endif
1560
9411b1d4
BF
1561/*
1562 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1563 * won't be used for replay.
1564 */
1565void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1566{
1567 struct nfs4_stateowner *so = cstate->replay_owner;
1568
1569 if (nfserr == nfserr_replay_me)
1570 return;
1571
1572 if (!seqid_mutating_err(ntohl(nfserr))) {
58fb12e6 1573 nfsd4_cstate_clear_replay(cstate);
9411b1d4
BF
1574 return;
1575 }
1576 if (!so)
1577 return;
1578 if (so->so_is_open_owner)
1579 release_last_closed_stateid(openowner(so));
1580 so->so_seqid++;
1581 return;
1582}
5282fd72 1583
ec6b5d7b
AA
1584static void
1585gen_sessionid(struct nfsd4_session *ses)
1586{
1587 struct nfs4_client *clp = ses->se_client;
1588 struct nfsd4_sessionid *sid;
1589
1590 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1591 sid->clientid = clp->cl_clientid;
1592 sid->sequence = current_sessionid++;
1593 sid->reserved = 0;
1594}
1595
1596/*
a649637c
AA
1597 * The protocol defines ca_maxresponssize_cached to include the size of
1598 * the rpc header, but all we need to cache is the data starting after
1599 * the end of the initial SEQUENCE operation--the rest we regenerate
1600 * each time. Therefore we can advertise a ca_maxresponssize_cached
1601 * value that is the number of bytes in our cache plus a few additional
1602 * bytes. In order to stay on the safe side, and not promise more than
1603 * we can cache, those additional bytes must be the minimum possible: 24
1604 * bytes of rpc header (xid through accept state, with AUTH_NULL
1605 * verifier), 12 for the compound header (with zero-length tag), and 44
1606 * for the SEQUENCE op response:
1607 */
1608#define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1609
557ce264
AA
1610static void
1611free_session_slots(struct nfsd4_session *ses)
1612{
1613 int i;
1614
53da6a53
BF
1615 for (i = 0; i < ses->se_fchannel.maxreqs; i++) {
1616 free_svc_cred(&ses->se_slots[i]->sl_cred);
557ce264 1617 kfree(ses->se_slots[i]);
53da6a53 1618 }
557ce264
AA
1619}
1620
a649637c 1621/*
efe0cb6d
BF
1622 * We don't actually need to cache the rpc and session headers, so we
1623 * can allocate a little less for each slot:
1624 */
55c760cf 1625static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
efe0cb6d 1626{
55c760cf 1627 u32 size;
efe0cb6d 1628
55c760cf
BF
1629 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1630 size = 0;
1631 else
1632 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1633 return size + sizeof(struct nfsd4_slot);
5b6feee9 1634}
ec6b5d7b 1635
5b6feee9
BF
1636/*
1637 * XXX: If we run out of reserved DRC memory we could (up to a point)
a649637c 1638 * re-negotiate active sessions and reduce their slot usage to make
42b2aa86 1639 * room for new connections. For now we just fail the create session.
ec6b5d7b 1640 */
2030ca56 1641static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
ec6b5d7b 1642{
55c760cf
BF
1643 u32 slotsize = slot_bytes(ca);
1644 u32 num = ca->maxreqs;
c54f24e3 1645 unsigned long avail, total_avail;
2030ca56 1646 unsigned int scale_factor;
ec6b5d7b 1647
5b6feee9 1648 spin_lock(&nfsd_drc_lock);
7f49fd5d
N
1649 if (nfsd_drc_max_mem > nfsd_drc_mem_used)
1650 total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used;
1651 else
1652 /* We have handed out more space than we chose in
1653 * set_max_drc() to allow. That isn't really a
1654 * problem as long as that doesn't make us think we
1655 * have lots more due to integer overflow.
1656 */
1657 total_avail = 0;
c54f24e3 1658 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail);
de766e57 1659 /*
2030ca56 1660 * Never use more than a fraction of the remaining memory,
7f49fd5d 1661 * unless it's the only way to give this client a slot.
2030ca56
N
1662 * The chosen fraction is either 1/8 or 1/number of threads,
1663 * whichever is smaller. This ensures there are adequate
1664 * slots to support multiple clients per thread.
7f49fd5d
N
1665 * Give the client one slot even if that would require
1666 * over-allocation--it is better than failure.
de766e57 1667 */
2030ca56
N
1668 scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads);
1669
1670 avail = clamp_t(unsigned long, avail, slotsize,
1671 total_avail/scale_factor);
5b6feee9 1672 num = min_t(int, num, avail / slotsize);
7f49fd5d 1673 num = max_t(int, num, 1);
5b6feee9
BF
1674 nfsd_drc_mem_used += num * slotsize;
1675 spin_unlock(&nfsd_drc_lock);
ec6b5d7b 1676
5b6feee9
BF
1677 return num;
1678}
ec6b5d7b 1679
55c760cf 1680static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
5b6feee9 1681{
55c760cf
BF
1682 int slotsize = slot_bytes(ca);
1683
4bd9b0f4 1684 spin_lock(&nfsd_drc_lock);
55c760cf 1685 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
4bd9b0f4 1686 spin_unlock(&nfsd_drc_lock);
5b6feee9 1687}
ec6b5d7b 1688
60810e54
KM
1689static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1690 struct nfsd4_channel_attrs *battrs)
5b6feee9 1691{
60810e54
KM
1692 int numslots = fattrs->maxreqs;
1693 int slotsize = slot_bytes(fattrs);
5b6feee9
BF
1694 struct nfsd4_session *new;
1695 int mem, i;
a649637c 1696
5b6feee9
BF
1697 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1698 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1699 mem = numslots * sizeof(struct nfsd4_slot *);
ec6b5d7b 1700
5b6feee9
BF
1701 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1702 if (!new)
1703 return NULL;
557ce264 1704 /* allocate each struct nfsd4_slot and data cache in one piece */
5b6feee9 1705 for (i = 0; i < numslots; i++) {
55c760cf 1706 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
5b6feee9 1707 if (!new->se_slots[i])
557ce264 1708 goto out_free;
557ce264 1709 }
60810e54
KM
1710
1711 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1712 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1713
5b6feee9
BF
1714 return new;
1715out_free:
1716 while (i--)
1717 kfree(new->se_slots[i]);
1718 kfree(new);
1719 return NULL;
ec6b5d7b
AA
1720}
1721
19cf5c02
BF
1722static void free_conn(struct nfsd4_conn *c)
1723{
1724 svc_xprt_put(c->cn_xprt);
1725 kfree(c);
1726}
ec6b5d7b 1727
19cf5c02
BF
1728static void nfsd4_conn_lost(struct svc_xpt_user *u)
1729{
1730 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1731 struct nfs4_client *clp = c->cn_session->se_client;
ec6b5d7b 1732
19cf5c02
BF
1733 spin_lock(&clp->cl_lock);
1734 if (!list_empty(&c->cn_persession)) {
1735 list_del(&c->cn_persession);
1736 free_conn(c);
1737 }
eea49806 1738 nfsd4_probe_callback(clp);
2e4b7239 1739 spin_unlock(&clp->cl_lock);
19cf5c02 1740}
ec6b5d7b 1741
d29c374c 1742static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
c7662518 1743{
c7662518 1744 struct nfsd4_conn *conn;
ec6b5d7b 1745
c7662518
BF
1746 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1747 if (!conn)
db90681d 1748 return NULL;
c7662518
BF
1749 svc_xprt_get(rqstp->rq_xprt);
1750 conn->cn_xprt = rqstp->rq_xprt;
d29c374c 1751 conn->cn_flags = flags;
db90681d
BF
1752 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1753 return conn;
1754}
a649637c 1755
328ead28
BF
1756static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1757{
1758 conn->cn_session = ses;
1759 list_add(&conn->cn_persession, &ses->se_conns);
ec6b5d7b
AA
1760}
1761
db90681d 1762static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
557ce264 1763{
db90681d 1764 struct nfs4_client *clp = ses->se_client;
557ce264 1765
c7662518 1766 spin_lock(&clp->cl_lock);
328ead28 1767 __nfsd4_hash_conn(conn, ses);
c7662518 1768 spin_unlock(&clp->cl_lock);
557ce264
AA
1769}
1770
21b75b01 1771static int nfsd4_register_conn(struct nfsd4_conn *conn)
efe0cb6d 1772{
19cf5c02 1773 conn->cn_xpt_user.callback = nfsd4_conn_lost;
21b75b01 1774 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
efe0cb6d
BF
1775}
1776
e1ff371f 1777static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
ec6b5d7b 1778{
21b75b01 1779 int ret;
ec6b5d7b 1780
db90681d 1781 nfsd4_hash_conn(conn, ses);
21b75b01
BF
1782 ret = nfsd4_register_conn(conn);
1783 if (ret)
1784 /* oops; xprt is already down: */
1785 nfsd4_conn_lost(&conn->cn_xpt_user);
57a37144
BF
1786 /* We may have gained or lost a callback channel: */
1787 nfsd4_probe_callback_sync(ses->se_client);
c7662518 1788}
ec6b5d7b 1789
e1ff371f 1790static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1d1bc8f2
BF
1791{
1792 u32 dir = NFS4_CDFC4_FORE;
1793
e1ff371f 1794 if (cses->flags & SESSION4_BACK_CHAN)
1d1bc8f2 1795 dir |= NFS4_CDFC4_BACK;
e1ff371f 1796 return alloc_conn(rqstp, dir);
1d1bc8f2
BF
1797}
1798
1799/* must be called under client_lock */
19cf5c02 1800static void nfsd4_del_conns(struct nfsd4_session *s)
c7662518 1801{
19cf5c02
BF
1802 struct nfs4_client *clp = s->se_client;
1803 struct nfsd4_conn *c;
ec6b5d7b 1804
19cf5c02
BF
1805 spin_lock(&clp->cl_lock);
1806 while (!list_empty(&s->se_conns)) {
1807 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1808 list_del_init(&c->cn_persession);
1809 spin_unlock(&clp->cl_lock);
557ce264 1810
19cf5c02
BF
1811 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1812 free_conn(c);
ec6b5d7b 1813
19cf5c02
BF
1814 spin_lock(&clp->cl_lock);
1815 }
1816 spin_unlock(&clp->cl_lock);
c7662518 1817}
ec6b5d7b 1818
1377b69e
BF
1819static void __free_session(struct nfsd4_session *ses)
1820{
1377b69e
BF
1821 free_session_slots(ses);
1822 kfree(ses);
1823}
1824
66b2b9b2 1825static void free_session(struct nfsd4_session *ses)
c7662518 1826{
19cf5c02 1827 nfsd4_del_conns(ses);
55c760cf 1828 nfsd4_put_drc_mem(&ses->se_fchannel);
1377b69e 1829 __free_session(ses);
c7662518
BF
1830}
1831
135ae827 1832static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
a827bcb2 1833{
a827bcb2 1834 int idx;
1872de0e 1835 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
a827bcb2 1836
ec6b5d7b
AA
1837 new->se_client = clp;
1838 gen_sessionid(new);
ec6b5d7b 1839
c7662518
BF
1840 INIT_LIST_HEAD(&new->se_conns);
1841
ac7c46f2 1842 new->se_cb_seq_nr = 1;
ec6b5d7b 1843 new->se_flags = cses->flags;
8b5ce5cd 1844 new->se_cb_prog = cses->callback_prog;
c6bb3ca2 1845 new->se_cb_sec = cses->cb_sec;
66b2b9b2 1846 atomic_set(&new->se_ref, 0);
5b6feee9 1847 idx = hash_sessionid(&new->se_sessionid);
1872de0e 1848 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
4c649378 1849 spin_lock(&clp->cl_lock);
ec6b5d7b 1850 list_add(&new->se_perclnt, &clp->cl_sessions);
4c649378 1851 spin_unlock(&clp->cl_lock);
60810e54 1852
b0d2e42c 1853 {
edd76786 1854 struct sockaddr *sa = svc_addr(rqstp);
dcbeaa68
BF
1855 /*
1856 * This is a little silly; with sessions there's no real
1857 * use for the callback address. Use the peer address
1858 * as a reasonable default for now, but consider fixing
1859 * the rpc client not to require an address in the
1860 * future:
1861 */
edd76786
BF
1862 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1863 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
edd76786 1864 }
ec6b5d7b
AA
1865}
1866
9089f1b4 1867/* caller must hold client_lock */
5282fd72 1868static struct nfsd4_session *
d4e19e70 1869__find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
5282fd72
ME
1870{
1871 struct nfsd4_session *elem;
1872 int idx;
1872de0e 1873 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5282fd72 1874
0a880a28
TM
1875 lockdep_assert_held(&nn->client_lock);
1876
5282fd72
ME
1877 dump_sessionid(__func__, sessionid);
1878 idx = hash_sessionid(sessionid);
5282fd72 1879 /* Search in the appropriate list */
1872de0e 1880 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
5282fd72
ME
1881 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1882 NFS4_MAX_SESSIONID_LEN)) {
1883 return elem;
1884 }
1885 }
1886
1887 dprintk("%s: session not found\n", __func__);
1888 return NULL;
1889}
1890
d4e19e70
TM
1891static struct nfsd4_session *
1892find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1893 __be32 *ret)
1894{
1895 struct nfsd4_session *session;
1896 __be32 status = nfserr_badsession;
1897
1898 session = __find_in_sessionid_hashtbl(sessionid, net);
1899 if (!session)
1900 goto out;
1901 status = nfsd4_get_session_locked(session);
1902 if (status)
1903 session = NULL;
1904out:
1905 *ret = status;
1906 return session;
1907}
1908
9089f1b4 1909/* caller must hold client_lock */
7116ed6b 1910static void
5282fd72 1911unhash_session(struct nfsd4_session *ses)
7116ed6b 1912{
0a880a28
TM
1913 struct nfs4_client *clp = ses->se_client;
1914 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1915
1916 lockdep_assert_held(&nn->client_lock);
1917
7116ed6b 1918 list_del(&ses->se_hash);
4c649378 1919 spin_lock(&ses->se_client->cl_lock);
7116ed6b 1920 list_del(&ses->se_perclnt);
4c649378 1921 spin_unlock(&ses->se_client->cl_lock);
5282fd72
ME
1922}
1923
1da177e4
LT
1924/* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1925static int
2c142baa 1926STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1da177e4 1927{
bbc7f33a
BF
1928 /*
1929 * We're assuming the clid was not given out from a boot
1930 * precisely 2^32 (about 136 years) before this one. That seems
1931 * a safe assumption:
1932 */
1933 if (clid->cl_boot == (u32)nn->boot_time)
1da177e4 1934 return 0;
dd5e3fbc 1935 trace_nfsd_clid_stale(clid);
1da177e4
LT
1936 return 1;
1937}
1938
1939/*
1940 * XXX Should we use a slab cache ?
1941 * This type of memory management is somewhat inefficient, but we use it
1942 * anyway since SETCLIENTID is not a common operation.
1943 */
35bba9a3 1944static struct nfs4_client *alloc_client(struct xdr_netobj name)
1da177e4
LT
1945{
1946 struct nfs4_client *clp;
d4f0489f 1947 int i;
1da177e4 1948
9258a2d5 1949 clp = kmem_cache_zalloc(client_slab, GFP_KERNEL);
35bba9a3
BF
1950 if (clp == NULL)
1951 return NULL;
6f4859b8 1952 xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL);
d4f0489f
TM
1953 if (clp->cl_name.data == NULL)
1954 goto err_no_name;
6da2ec56
KC
1955 clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE,
1956 sizeof(struct list_head),
1957 GFP_KERNEL);
d4f0489f
TM
1958 if (!clp->cl_ownerstr_hashtbl)
1959 goto err_no_hashtbl;
1960 for (i = 0; i < OWNER_HASH_SIZE; i++)
1961 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
5694c93e
TM
1962 INIT_LIST_HEAD(&clp->cl_sessions);
1963 idr_init(&clp->cl_stateids);
14ed14cc 1964 atomic_set(&clp->cl_rpc_users, 0);
5694c93e
TM
1965 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1966 INIT_LIST_HEAD(&clp->cl_idhash);
1967 INIT_LIST_HEAD(&clp->cl_openowners);
1968 INIT_LIST_HEAD(&clp->cl_delegations);
1969 INIT_LIST_HEAD(&clp->cl_lru);
5694c93e 1970 INIT_LIST_HEAD(&clp->cl_revoked);
9cf514cc
CH
1971#ifdef CONFIG_NFSD_PNFS
1972 INIT_LIST_HEAD(&clp->cl_lo_states);
1973#endif
e0639dc5
OK
1974 INIT_LIST_HEAD(&clp->async_copies);
1975 spin_lock_init(&clp->async_lock);
5694c93e
TM
1976 spin_lock_init(&clp->cl_lock);
1977 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1da177e4 1978 return clp;
d4f0489f
TM
1979err_no_hashtbl:
1980 kfree(clp->cl_name.data);
1981err_no_name:
9258a2d5 1982 kmem_cache_free(client_slab, clp);
d4f0489f 1983 return NULL;
1da177e4
LT
1984}
1985
59f8e91b
BF
1986static void __free_client(struct kref *k)
1987{
e8a79fb1
BF
1988 struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref);
1989 struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs);
59f8e91b
BF
1990
1991 free_svc_cred(&clp->cl_cred);
1992 kfree(clp->cl_ownerstr_hashtbl);
1993 kfree(clp->cl_name.data);
79123444
BF
1994 kfree(clp->cl_nii_domain.data);
1995 kfree(clp->cl_nii_name.data);
59f8e91b 1996 idr_destroy(&clp->cl_stateids);
59f8e91b
BF
1997 kmem_cache_free(client_slab, clp);
1998}
1999
297e57a2 2000static void drop_client(struct nfs4_client *clp)
59f8e91b 2001{
e8a79fb1 2002 kref_put(&clp->cl_nfsdfs.cl_ref, __free_client);
59f8e91b
BF
2003}
2004
4dd86e15 2005static void
1da177e4
LT
2006free_client(struct nfs4_client *clp)
2007{
792c95dd
BF
2008 while (!list_empty(&clp->cl_sessions)) {
2009 struct nfsd4_session *ses;
2010 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
2011 se_perclnt);
2012 list_del(&ses->se_perclnt);
66b2b9b2
BF
2013 WARN_ON_ONCE(atomic_read(&ses->se_ref));
2014 free_session(ses);
792c95dd 2015 }
4cb57e30 2016 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
89c905be 2017 if (clp->cl_nfsd_dentry) {
e8a79fb1 2018 nfsd_client_rmdir(clp->cl_nfsd_dentry);
89c905be
BF
2019 clp->cl_nfsd_dentry = NULL;
2020 wake_up_all(&expiry_wq);
2021 }
59f8e91b 2022 drop_client(clp);
1da177e4
LT
2023}
2024
84d38ac9 2025/* must be called under the client_lock */
4beb345b 2026static void
84d38ac9
BH
2027unhash_client_locked(struct nfs4_client *clp)
2028{
4beb345b 2029 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
792c95dd
BF
2030 struct nfsd4_session *ses;
2031
0a880a28
TM
2032 lockdep_assert_held(&nn->client_lock);
2033
4beb345b
TM
2034 /* Mark the client as expired! */
2035 clp->cl_time = 0;
2036 /* Make it invisible */
2037 if (!list_empty(&clp->cl_idhash)) {
2038 list_del_init(&clp->cl_idhash);
2039 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
2040 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
2041 else
2042 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
2043 }
2044 list_del_init(&clp->cl_lru);
4c649378 2045 spin_lock(&clp->cl_lock);
792c95dd
BF
2046 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
2047 list_del_init(&ses->se_hash);
4c649378 2048 spin_unlock(&clp->cl_lock);
84d38ac9
BH
2049}
2050
1da177e4 2051static void
4beb345b
TM
2052unhash_client(struct nfs4_client *clp)
2053{
2054 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2055
2056 spin_lock(&nn->client_lock);
2057 unhash_client_locked(clp);
2058 spin_unlock(&nn->client_lock);
2059}
2060
97403d95
JL
2061static __be32 mark_client_expired_locked(struct nfs4_client *clp)
2062{
14ed14cc 2063 if (atomic_read(&clp->cl_rpc_users))
97403d95
JL
2064 return nfserr_jukebox;
2065 unhash_client_locked(clp);
2066 return nfs_ok;
2067}
2068
4beb345b
TM
2069static void
2070__destroy_client(struct nfs4_client *clp)
1da177e4 2071{
68ef3bc3 2072 int i;
fe0750e5 2073 struct nfs4_openowner *oo;
1da177e4 2074 struct nfs4_delegation *dp;
1da177e4
LT
2075 struct list_head reaplist;
2076
1da177e4 2077 INIT_LIST_HEAD(&reaplist);
cdc97505 2078 spin_lock(&state_lock);
ea1da636
N
2079 while (!list_empty(&clp->cl_delegations)) {
2080 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
3fcbbd24 2081 WARN_ON(!unhash_delegation_locked(dp));
42690676 2082 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 2083 }
cdc97505 2084 spin_unlock(&state_lock);
1da177e4
LT
2085 while (!list_empty(&reaplist)) {
2086 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
42690676 2087 list_del_init(&dp->dl_recall_lru);
0af6e690 2088 destroy_unhashed_deleg(dp);
1da177e4 2089 }
2d4a532d 2090 while (!list_empty(&clp->cl_revoked)) {
c876486b 2091 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
2d4a532d 2092 list_del_init(&dp->dl_recall_lru);
6011695d 2093 nfs4_put_stid(&dp->dl_stid);
956c4fee 2094 }
ea1da636 2095 while (!list_empty(&clp->cl_openowners)) {
fe0750e5 2096 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
b5971afa 2097 nfs4_get_stateowner(&oo->oo_owner);
fe0750e5 2098 release_openowner(oo);
1da177e4 2099 }
68ef3bc3
JL
2100 for (i = 0; i < OWNER_HASH_SIZE; i++) {
2101 struct nfs4_stateowner *so, *tmp;
2102
2103 list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i],
2104 so_strhash) {
2105 /* Should be no openowners at this point */
2106 WARN_ON_ONCE(so->so_is_open_owner);
2107 remove_blocked_locks(lockowner(so));
2108 }
2109 }
9cf514cc 2110 nfsd4_return_all_client_layouts(clp);
e0639dc5 2111 nfsd4_shutdown_copy(clp);
6ff8da08 2112 nfsd4_shutdown_callback(clp);
84d38ac9
BH
2113 if (clp->cl_cb_conn.cb_xprt)
2114 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
221a6876 2115 free_client(clp);
89c905be 2116 wake_up_all(&expiry_wq);
1da177e4
LT
2117}
2118
4beb345b
TM
2119static void
2120destroy_client(struct nfs4_client *clp)
2121{
2122 unhash_client(clp);
2123 __destroy_client(clp);
2124}
2125
362063a5
SM
2126static void inc_reclaim_complete(struct nfs4_client *clp)
2127{
2128 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2129
2130 if (!nn->track_reclaim_completes)
2131 return;
2132 if (!nfsd4_find_reclaim_client(clp->cl_name, nn))
2133 return;
2134 if (atomic_inc_return(&nn->nr_reclaim_complete) ==
2135 nn->reclaim_str_hashtbl_size) {
2136 printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2137 clp->net->ns.inum);
2138 nfsd4_end_grace(nn);
2139 }
2140}
2141
0d22f68f
BF
2142static void expire_client(struct nfs4_client *clp)
2143{
4beb345b 2144 unhash_client(clp);
0d22f68f 2145 nfsd4_client_record_remove(clp);
4beb345b 2146 __destroy_client(clp);
0d22f68f
BF
2147}
2148
35bba9a3
BF
2149static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
2150{
2151 memcpy(target->cl_verifier.data, source->data,
2152 sizeof(target->cl_verifier.data));
1da177e4
LT
2153}
2154
35bba9a3
BF
2155static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
2156{
1da177e4
LT
2157 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
2158 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
2159}
2160
50043859
BF
2161static int copy_cred(struct svc_cred *target, struct svc_cred *source)
2162{
2f10fdcb
N
2163 target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL);
2164 target->cr_raw_principal = kstrdup(source->cr_raw_principal,
2165 GFP_KERNEL);
9abdda5d
CL
2166 target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL);
2167 if ((source->cr_principal && !target->cr_principal) ||
2168 (source->cr_raw_principal && !target->cr_raw_principal) ||
2169 (source->cr_targ_princ && !target->cr_targ_princ))
2f10fdcb 2170 return -ENOMEM;
50043859 2171
d5497fc6 2172 target->cr_flavor = source->cr_flavor;
1da177e4
LT
2173 target->cr_uid = source->cr_uid;
2174 target->cr_gid = source->cr_gid;
2175 target->cr_group_info = source->cr_group_info;
2176 get_group_info(target->cr_group_info);
0dc1531a
BF
2177 target->cr_gss_mech = source->cr_gss_mech;
2178 if (source->cr_gss_mech)
2179 gss_mech_get(source->cr_gss_mech);
03a4e1f6 2180 return 0;
1da177e4
LT
2181}
2182
ef17af2a 2183static int
ac55fdc4
JL
2184compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
2185{
ef17af2a
RV
2186 if (o1->len < o2->len)
2187 return -1;
2188 if (o1->len > o2->len)
2189 return 1;
2190 return memcmp(o1->data, o2->data, o1->len);
ac55fdc4
JL
2191}
2192
1da177e4 2193static int
599e0a22
BF
2194same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
2195{
2196 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1da177e4
LT
2197}
2198
2199static int
599e0a22
BF
2200same_clid(clientid_t *cl1, clientid_t *cl2)
2201{
2202 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1da177e4
LT
2203}
2204
8fbba96e
BF
2205static bool groups_equal(struct group_info *g1, struct group_info *g2)
2206{
2207 int i;
2208
2209 if (g1->ngroups != g2->ngroups)
2210 return false;
2211 for (i=0; i<g1->ngroups; i++)
81243eac 2212 if (!gid_eq(g1->gid[i], g2->gid[i]))
8fbba96e
BF
2213 return false;
2214 return true;
2215}
2216
68eb3508
BF
2217/*
2218 * RFC 3530 language requires clid_inuse be returned when the
2219 * "principal" associated with a requests differs from that previously
2220 * used. We use uid, gid's, and gss principal string as our best
2221 * approximation. We also don't want to allow non-gss use of a client
2222 * established using gss: in theory cr_principal should catch that
2223 * change, but in practice cr_principal can be null even in the gss case
2224 * since gssd doesn't always pass down a principal string.
2225 */
2226static bool is_gss_cred(struct svc_cred *cr)
2227{
2228 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2229 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
2230}
2231
2232
5559b50a 2233static bool
599e0a22
BF
2234same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
2235{
68eb3508 2236 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
6fab8779
EB
2237 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
2238 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
8fbba96e
BF
2239 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
2240 return false;
9abdda5d 2241 /* XXX: check that cr_targ_princ fields match ? */
8fbba96e
BF
2242 if (cr1->cr_principal == cr2->cr_principal)
2243 return true;
2244 if (!cr1->cr_principal || !cr2->cr_principal)
2245 return false;
5559b50a 2246 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1da177e4
LT
2247}
2248
57266a6e
BF
2249static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
2250{
2251 struct svc_cred *cr = &rqstp->rq_cred;
2252 u32 service;
2253
c4720591
BF
2254 if (!cr->cr_gss_mech)
2255 return false;
57266a6e
BF
2256 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
2257 return service == RPC_GSS_SVC_INTEGRITY ||
2258 service == RPC_GSS_SVC_PRIVACY;
2259}
2260
dedeb13f 2261bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
57266a6e
BF
2262{
2263 struct svc_cred *cr = &rqstp->rq_cred;
2264
2265 if (!cl->cl_mach_cred)
2266 return true;
2267 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
2268 return false;
2269 if (!svc_rqst_integrity_protected(rqstp))
2270 return false;
414ca017
BF
2271 if (cl->cl_cred.cr_raw_principal)
2272 return 0 == strcmp(cl->cl_cred.cr_raw_principal,
2273 cr->cr_raw_principal);
57266a6e
BF
2274 if (!cr->cr_principal)
2275 return false;
2276 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
2277}
2278
294ac32e 2279static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
deda2faa 2280{
ab4684d1 2281 __be32 verf[2];
1da177e4 2282
f419992c
JL
2283 /*
2284 * This is opaque to client, so no need to byte-swap. Use
2285 * __force to keep sparse happy
2286 */
9104ae49 2287 verf[0] = (__force __be32)(u32)ktime_get_real_seconds();
19311aa8 2288 verf[1] = (__force __be32)nn->clverifier_counter++;
ab4684d1 2289 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1da177e4
LT
2290}
2291
294ac32e
JL
2292static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
2293{
9cc76801 2294 clp->cl_clientid.cl_boot = (u32)nn->boot_time;
294ac32e
JL
2295 clp->cl_clientid.cl_id = nn->clientid_counter++;
2296 gen_confirm(clp, nn);
2297}
2298
4770d722
JL
2299static struct nfs4_stid *
2300find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
4581d140 2301{
3abdb607
BF
2302 struct nfs4_stid *ret;
2303
2304 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
2305 if (!ret || !ret->sc_type)
2306 return NULL;
2307 return ret;
4d71ab87
BF
2308}
2309
4770d722
JL
2310static struct nfs4_stid *
2311find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
f459e453
BF
2312{
2313 struct nfs4_stid *s;
4d71ab87 2314
4770d722
JL
2315 spin_lock(&cl->cl_lock);
2316 s = find_stateid_locked(cl, t);
2d3f9668
TM
2317 if (s != NULL) {
2318 if (typemask & s->sc_type)
a15dfcd5 2319 refcount_inc(&s->sc_count);
2d3f9668
TM
2320 else
2321 s = NULL;
2322 }
4770d722
JL
2323 spin_unlock(&cl->cl_lock);
2324 return s;
4581d140
BF
2325}
2326
a204f25e
BF
2327static struct nfs4_client *get_nfsdfs_clp(struct inode *inode)
2328{
2329 struct nfsdfs_client *nc;
2330 nc = get_nfsdfs_client(inode);
2331 if (!nc)
2332 return NULL;
2333 return container_of(nc, struct nfs4_client, cl_nfsdfs);
2334}
2335
169319f1
BF
2336static void seq_quote_mem(struct seq_file *m, char *data, int len)
2337{
2338 seq_printf(m, "\"");
2339 seq_escape_mem_ascii(m, data, len);
2340 seq_printf(m, "\"");
2341}
2342
97ad4031
BF
2343static int client_info_show(struct seq_file *m, void *v)
2344{
2345 struct inode *inode = m->private;
97ad4031
BF
2346 struct nfs4_client *clp;
2347 u64 clid;
2348
a204f25e
BF
2349 clp = get_nfsdfs_clp(inode);
2350 if (!clp)
97ad4031 2351 return -ENXIO;
97ad4031
BF
2352 memcpy(&clid, &clp->cl_clientid, sizeof(clid));
2353 seq_printf(m, "clientid: 0x%llx\n", clid);
169319f1
BF
2354 seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr);
2355 seq_printf(m, "name: ");
2356 seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len);
2357 seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion);
79123444
BF
2358 if (clp->cl_nii_domain.data) {
2359 seq_printf(m, "Implementation domain: ");
2360 seq_quote_mem(m, clp->cl_nii_domain.data,
2361 clp->cl_nii_domain.len);
2362 seq_printf(m, "\nImplementation name: ");
2363 seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len);
e29f4703 2364 seq_printf(m, "\nImplementation time: [%lld, %ld]\n",
79123444
BF
2365 clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec);
2366 }
97ad4031
BF
2367 drop_client(clp);
2368
2369 return 0;
2370}
2371
2372static int client_info_open(struct inode *inode, struct file *file)
2373{
2374 return single_open(file, client_info_show, inode);
2375}
2376
2377static const struct file_operations client_info_fops = {
2378 .open = client_info_open,
2379 .read = seq_read,
2380 .llseek = seq_lseek,
2381 .release = single_release,
2382};
2383
78599c42
BF
2384static void *states_start(struct seq_file *s, loff_t *pos)
2385 __acquires(&clp->cl_lock)
2386{
2387 struct nfs4_client *clp = s->private;
2388 unsigned long id = *pos;
2389 void *ret;
2390
2391 spin_lock(&clp->cl_lock);
2392 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2393 *pos = id;
2394 return ret;
2395}
2396
2397static void *states_next(struct seq_file *s, void *v, loff_t *pos)
2398{
2399 struct nfs4_client *clp = s->private;
2400 unsigned long id = *pos;
2401 void *ret;
2402
2403 id = *pos;
2404 id++;
2405 ret = idr_get_next_ul(&clp->cl_stateids, &id);
2406 *pos = id;
2407 return ret;
2408}
2409
2410static void states_stop(struct seq_file *s, void *v)
2411 __releases(&clp->cl_lock)
2412{
2413 struct nfs4_client *clp = s->private;
2414
2415 spin_unlock(&clp->cl_lock);
2416}
2417
580da465
AG
2418static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f)
2419{
2420 seq_printf(s, "filename: \"%pD2\"", f->nf_file);
2421}
2422
fd4f83fd 2423static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f)
78599c42 2424{
fd4f83fd 2425 struct inode *inode = f->nf_inode;
78599c42
BF
2426
2427 seq_printf(s, "superblock: \"%02x:%02x:%ld\"",
2428 MAJOR(inode->i_sb->s_dev),
2429 MINOR(inode->i_sb->s_dev),
2430 inode->i_ino);
2431}
2432
2433static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo)
2434{
2435 seq_printf(s, "owner: ");
2436 seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len);
2437}
2438
ace7ade4
BF
2439static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid)
2440{
ee590d25
BF
2441 seq_printf(s, "0x%.8x", stid->si_generation);
2442 seq_printf(s, "%12phN", &stid->si_opaque);
ace7ade4
BF
2443}
2444
78599c42
BF
2445static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st)
2446{
2447 struct nfs4_ol_stateid *ols;
2448 struct nfs4_file *nf;
fd4f83fd 2449 struct nfsd_file *file;
78599c42
BF
2450 struct nfs4_stateowner *oo;
2451 unsigned int access, deny;
2452
2453 if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID)
2454 return 0; /* XXX: or SEQ_SKIP? */
2455 ols = openlockstateid(st);
2456 oo = ols->st_stateowner;
2457 nf = st->sc_file;
2458 file = find_any_file(nf);
9affa435
BF
2459 if (!file)
2460 return 0;
78599c42 2461
ace7ade4
BF
2462 seq_printf(s, "- ");
2463 nfs4_show_stateid(s, &st->sc_stateid);
2464 seq_printf(s, ": { type: open, ");
78599c42
BF
2465
2466 access = bmap_to_share_mode(ols->st_access_bmap);
2467 deny = bmap_to_share_mode(ols->st_deny_bmap);
2468
c4b77edb 2469 seq_printf(s, "access: %s%s, ",
78599c42
BF
2470 access & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2471 access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
c4b77edb 2472 seq_printf(s, "deny: %s%s, ",
78599c42
BF
2473 deny & NFS4_SHARE_ACCESS_READ ? "r" : "-",
2474 deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-");
2475
2476 nfs4_show_superblock(s, file);
2477 seq_printf(s, ", ");
580da465
AG
2478 nfs4_show_fname(s, file);
2479 seq_printf(s, ", ");
78599c42
BF
2480 nfs4_show_owner(s, oo);
2481 seq_printf(s, " }\n");
fd4f83fd 2482 nfsd_file_put(file);
16d36e09
BF
2483
2484 return 0;
2485}
2486
2487static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st)
2488{
2489 struct nfs4_ol_stateid *ols;
2490 struct nfs4_file *nf;
fd4f83fd 2491 struct nfsd_file *file;
16d36e09
BF
2492 struct nfs4_stateowner *oo;
2493
2494 ols = openlockstateid(st);
2495 oo = ols->st_stateowner;
2496 nf = st->sc_file;
2497 file = find_any_file(nf);
9affa435
BF
2498 if (!file)
2499 return 0;
16d36e09 2500
ace7ade4
BF
2501 seq_printf(s, "- ");
2502 nfs4_show_stateid(s, &st->sc_stateid);
2503 seq_printf(s, ": { type: lock, ");
16d36e09
BF
2504
2505 /*
2506 * Note: a lock stateid isn't really the same thing as a lock,
2507 * it's the locking state held by one owner on a file, and there
2508 * may be multiple (or no) lock ranges associated with it.
2509 * (Same for the matter is true of open stateids.)
2510 */
78599c42 2511
16d36e09
BF
2512 nfs4_show_superblock(s, file);
2513 /* XXX: open stateid? */
2514 seq_printf(s, ", ");
580da465
AG
2515 nfs4_show_fname(s, file);
2516 seq_printf(s, ", ");
16d36e09
BF
2517 nfs4_show_owner(s, oo);
2518 seq_printf(s, " }\n");
fd4f83fd 2519 nfsd_file_put(file);
78599c42
BF
2520
2521 return 0;
2522}
2523
16d36e09
BF
2524static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st)
2525{
2526 struct nfs4_delegation *ds;
2527 struct nfs4_file *nf;
eb82dd39 2528 struct nfsd_file *file;
16d36e09
BF
2529
2530 ds = delegstateid(st);
2531 nf = st->sc_file;
9affa435
BF
2532 file = find_deleg_file(nf);
2533 if (!file)
2534 return 0;
16d36e09 2535
ace7ade4
BF
2536 seq_printf(s, "- ");
2537 nfs4_show_stateid(s, &st->sc_stateid);
2538 seq_printf(s, ": { type: deleg, ");
16d36e09
BF
2539
2540 /* Kinda dead code as long as we only support read delegs: */
2541 seq_printf(s, "access: %s, ",
2542 ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w");
2543
2544 /* XXX: lease time, whether it's being recalled. */
2545
2546 nfs4_show_superblock(s, file);
580da465
AG
2547 seq_printf(s, ", ");
2548 nfs4_show_fname(s, file);
16d36e09 2549 seq_printf(s, " }\n");
9affa435 2550 nfsd_file_put(file);
16d36e09
BF
2551
2552 return 0;
2553}
2554
0c4b62b0
BF
2555static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st)
2556{
2557 struct nfs4_layout_stateid *ls;
eb82dd39 2558 struct nfsd_file *file;
0c4b62b0
BF
2559
2560 ls = container_of(st, struct nfs4_layout_stateid, ls_stid);
2561 file = ls->ls_file;
2562
ace7ade4
BF
2563 seq_printf(s, "- ");
2564 nfs4_show_stateid(s, &st->sc_stateid);
2565 seq_printf(s, ": { type: layout, ");
0c4b62b0
BF
2566
2567 /* XXX: What else would be useful? */
2568
2569 nfs4_show_superblock(s, file);
580da465
AG
2570 seq_printf(s, ", ");
2571 nfs4_show_fname(s, file);
0c4b62b0
BF
2572 seq_printf(s, " }\n");
2573
2574 return 0;
2575}
2576
78599c42
BF
2577static int states_show(struct seq_file *s, void *v)
2578{
2579 struct nfs4_stid *st = v;
2580
2581 switch (st->sc_type) {
2582 case NFS4_OPEN_STID:
2583 return nfs4_show_open(s, st);
16d36e09
BF
2584 case NFS4_LOCK_STID:
2585 return nfs4_show_lock(s, st);
2586 case NFS4_DELEG_STID:
2587 return nfs4_show_deleg(s, st);
0c4b62b0
BF
2588 case NFS4_LAYOUT_STID:
2589 return nfs4_show_layout(s, st);
78599c42
BF
2590 default:
2591 return 0; /* XXX: or SEQ_SKIP? */
2592 }
16d36e09 2593 /* XXX: copy stateids? */
78599c42
BF
2594}
2595
2596static struct seq_operations states_seq_ops = {
2597 .start = states_start,
2598 .next = states_next,
2599 .stop = states_stop,
2600 .show = states_show
2601};
2602
2603static int client_states_open(struct inode *inode, struct file *file)
2604{
78599c42
BF
2605 struct seq_file *s;
2606 struct nfs4_client *clp;
2607 int ret;
2608
a204f25e
BF
2609 clp = get_nfsdfs_clp(inode);
2610 if (!clp)
78599c42 2611 return -ENXIO;
78599c42
BF
2612
2613 ret = seq_open(file, &states_seq_ops);
2614 if (ret)
2615 return ret;
2616 s = file->private_data;
2617 s->private = clp;
2618 return 0;
2619}
2620
2621static int client_opens_release(struct inode *inode, struct file *file)
2622{
2623 struct seq_file *m = file->private_data;
2624 struct nfs4_client *clp = m->private;
2625
2626 /* XXX: alternatively, we could get/drop in seq start/stop */
2627 drop_client(clp);
2628 return 0;
2629}
2630
2631static const struct file_operations client_states_fops = {
2632 .open = client_states_open,
2633 .read = seq_read,
2634 .llseek = seq_lseek,
2635 .release = client_opens_release,
2636};
2637
89c905be
BF
2638/*
2639 * Normally we refuse to destroy clients that are in use, but here the
2640 * administrator is telling us to just do it. We also want to wait
2641 * so the caller has a guarantee that the client's locks are gone by
2642 * the time the write returns:
2643 */
297e57a2 2644static void force_expire_client(struct nfs4_client *clp)
89c905be
BF
2645{
2646 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
2647 bool already_expired;
2648
2649 spin_lock(&clp->cl_lock);
2650 clp->cl_time = 0;
2651 spin_unlock(&clp->cl_lock);
2652
2653 wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0);
2654 spin_lock(&nn->client_lock);
2655 already_expired = list_empty(&clp->cl_lru);
2656 if (!already_expired)
2657 unhash_client_locked(clp);
2658 spin_unlock(&nn->client_lock);
2659
2660 if (!already_expired)
2661 expire_client(clp);
2662 else
2663 wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL);
2664}
2665
2666static ssize_t client_ctl_write(struct file *file, const char __user *buf,
2667 size_t size, loff_t *pos)
2668{
2669 char *data;
2670 struct nfs4_client *clp;
2671
2672 data = simple_transaction_get(file, buf, size);
2673 if (IS_ERR(data))
2674 return PTR_ERR(data);
2675 if (size != 7 || 0 != memcmp(data, "expire\n", 7))
2676 return -EINVAL;
2677 clp = get_nfsdfs_clp(file_inode(file));
2678 if (!clp)
2679 return -ENXIO;
2680 force_expire_client(clp);
2681 drop_client(clp);
2682 return 7;
2683}
2684
2685static const struct file_operations client_ctl_fops = {
2686 .write = client_ctl_write,
2687 .release = simple_transaction_release,
2688};
2689
97ad4031
BF
2690static const struct tree_descr client_files[] = {
2691 [0] = {"info", &client_info_fops, S_IRUSR},
78599c42 2692 [1] = {"states", &client_states_fops, S_IRUSR},
6cbfad5f 2693 [2] = {"ctl", &client_ctl_fops, S_IWUSR},
78599c42 2694 [3] = {""},
97ad4031
BF
2695};
2696
2216d449 2697static struct nfs4_client *create_client(struct xdr_netobj name,
b09333c4
RL
2698 struct svc_rqst *rqstp, nfs4_verifier *verf)
2699{
2700 struct nfs4_client *clp;
2701 struct sockaddr *sa = svc_addr(rqstp);
03a4e1f6 2702 int ret;
c212cecf 2703 struct net *net = SVC_NET(rqstp);
e8a79fb1 2704 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b09333c4
RL
2705
2706 clp = alloc_client(name);
2707 if (clp == NULL)
2708 return NULL;
2709
03a4e1f6
BF
2710 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
2711 if (ret) {
03a4e1f6 2712 free_client(clp);
03a4e1f6 2713 return NULL;
b09333c4 2714 }
e8a79fb1
BF
2715 gen_clid(clp, nn);
2716 kref_init(&clp->cl_nfsdfs.cl_ref);
0162ac2b 2717 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
20b7d86f 2718 clp->cl_time = ktime_get_boottime_seconds();
b09333c4 2719 clear_bit(0, &clp->cl_cb_slot_busy);
b09333c4 2720 copy_verf(clp, verf);
3bade247 2721 memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage));
edd76786 2722 clp->cl_cb_session = NULL;
c212cecf 2723 clp->net = net;
e8a79fb1 2724 clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs,
97ad4031
BF
2725 clp->cl_clientid.cl_id - nn->clientid_base,
2726 client_files);
e8a79fb1
BF
2727 if (!clp->cl_nfsd_dentry) {
2728 free_client(clp);
2729 return NULL;
2730 }
b09333c4
RL
2731 return clp;
2732}
2733
fd39ca9a 2734static void
ac55fdc4
JL
2735add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
2736{
2737 struct rb_node **new = &(root->rb_node), *parent = NULL;
2738 struct nfs4_client *clp;
2739
2740 while (*new) {
2741 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
2742 parent = *new;
2743
2744 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
2745 new = &((*new)->rb_left);
2746 else
2747 new = &((*new)->rb_right);
2748 }
2749
2750 rb_link_node(&new_clp->cl_namenode, parent, new);
2751 rb_insert_color(&new_clp->cl_namenode, root);
2752}
2753
2754static struct nfs4_client *
2755find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
2756{
ef17af2a 2757 int cmp;
ac55fdc4
JL
2758 struct rb_node *node = root->rb_node;
2759 struct nfs4_client *clp;
2760
2761 while (node) {
2762 clp = rb_entry(node, struct nfs4_client, cl_namenode);
2763 cmp = compare_blob(&clp->cl_name, name);
2764 if (cmp > 0)
2765 node = node->rb_left;
2766 else if (cmp < 0)
2767 node = node->rb_right;
2768 else
2769 return clp;
2770 }
2771 return NULL;
2772}
2773
2774static void
2775add_to_unconfirmed(struct nfs4_client *clp)
1da177e4
LT
2776{
2777 unsigned int idhashval;
0a7ec377 2778 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 2779
0a880a28
TM
2780 lockdep_assert_held(&nn->client_lock);
2781
ac55fdc4 2782 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
a99454aa 2783 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1da177e4 2784 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
0a7ec377 2785 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
3dbacee6 2786 renew_client_locked(clp);
1da177e4
LT
2787}
2788
fd39ca9a 2789static void
1da177e4
LT
2790move_to_confirmed(struct nfs4_client *clp)
2791{
2792 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
8daae4dc 2793 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1da177e4 2794
0a880a28
TM
2795 lockdep_assert_held(&nn->client_lock);
2796
1da177e4 2797 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
8daae4dc 2798 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
a99454aa 2799 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
382a62e7 2800 add_clp_to_name_tree(clp, &nn->conf_name_tree);
ac55fdc4 2801 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
3dbacee6 2802 renew_client_locked(clp);
1da177e4
LT
2803}
2804
2805static struct nfs4_client *
bfa85e83 2806find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1da177e4
LT
2807{
2808 struct nfs4_client *clp;
2809 unsigned int idhashval = clientid_hashval(clid->cl_id);
2810
bfa85e83 2811 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
a50d2ad1 2812 if (same_clid(&clp->cl_clientid, clid)) {
d15c077e
BF
2813 if ((bool)clp->cl_minorversion != sessions)
2814 return NULL;
3dbacee6 2815 renew_client_locked(clp);
1da177e4 2816 return clp;
a50d2ad1 2817 }
1da177e4
LT
2818 }
2819 return NULL;
2820}
2821
bfa85e83
BF
2822static struct nfs4_client *
2823find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
2824{
2825 struct list_head *tbl = nn->conf_id_hashtbl;
2826
0a880a28 2827 lockdep_assert_held(&nn->client_lock);
bfa85e83
BF
2828 return find_client_in_id_table(tbl, clid, sessions);
2829}
2830
1da177e4 2831static struct nfs4_client *
0a7ec377 2832find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1da177e4 2833{
bfa85e83 2834 struct list_head *tbl = nn->unconf_id_hashtbl;
1da177e4 2835
0a880a28 2836 lockdep_assert_held(&nn->client_lock);
bfa85e83 2837 return find_client_in_id_table(tbl, clid, sessions);
1da177e4
LT
2838}
2839
6e5f15c9 2840static bool clp_used_exchangeid(struct nfs4_client *clp)
a1bcecd2 2841{
6e5f15c9 2842 return clp->cl_exchange_flags != 0;
e203d506 2843}
a1bcecd2 2844
28ce6054 2845static struct nfs4_client *
382a62e7 2846find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 2847{
0a880a28 2848 lockdep_assert_held(&nn->client_lock);
382a62e7 2849 return find_clp_in_name_tree(name, &nn->conf_name_tree);
28ce6054
N
2850}
2851
2852static struct nfs4_client *
a99454aa 2853find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
28ce6054 2854{
0a880a28 2855 lockdep_assert_held(&nn->client_lock);
a99454aa 2856 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
28ce6054
N
2857}
2858
fd39ca9a 2859static void
6f3d772f 2860gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
1da177e4 2861{
07263f1e 2862 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
6f3d772f
TU
2863 struct sockaddr *sa = svc_addr(rqstp);
2864 u32 scopeid = rpc_get_scope_id(sa);
7077ecba
JL
2865 unsigned short expected_family;
2866
2867 /* Currently, we only support tcp and tcp6 for the callback channel */
2868 if (se->se_callback_netid_len == 3 &&
2869 !memcmp(se->se_callback_netid_val, "tcp", 3))
2870 expected_family = AF_INET;
2871 else if (se->se_callback_netid_len == 4 &&
2872 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2873 expected_family = AF_INET6;
2874 else
1da177e4
LT
2875 goto out_err;
2876
c212cecf 2877 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
aa9a4ec7 2878 se->se_callback_addr_len,
07263f1e
BF
2879 (struct sockaddr *)&conn->cb_addr,
2880 sizeof(conn->cb_addr));
aa9a4ec7 2881
07263f1e 2882 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
1da177e4 2883 goto out_err;
aa9a4ec7 2884
07263f1e
BF
2885 if (conn->cb_addr.ss_family == AF_INET6)
2886 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
fbf4665f 2887
07263f1e
BF
2888 conn->cb_prog = se->se_callback_prog;
2889 conn->cb_ident = se->se_callback_ident;
849a1cf1 2890 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
1eace0d1 2891 trace_nfsd_cb_args(clp, conn);
1da177e4
LT
2892 return;
2893out_err:
07263f1e
BF
2894 conn->cb_addr.ss_family = AF_UNSPEC;
2895 conn->cb_addrlen = 0;
1eace0d1 2896 trace_nfsd_cb_nodelegs(clp);
1da177e4
LT
2897 return;
2898}
2899
074fe897 2900/*
067e1ace 2901 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
074fe897 2902 */
b607664e 2903static void
074fe897 2904nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
074fe897 2905{
f5236013 2906 struct xdr_buf *buf = resp->xdr.buf;
557ce264
AA
2907 struct nfsd4_slot *slot = resp->cstate.slot;
2908 unsigned int base;
074fe897 2909
557ce264 2910 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 2911
085def3a 2912 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
557ce264
AA
2913 slot->sl_opcnt = resp->opcnt;
2914 slot->sl_status = resp->cstate.status;
53da6a53
BF
2915 free_svc_cred(&slot->sl_cred);
2916 copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred);
074fe897 2917
085def3a
BF
2918 if (!nfsd4_cache_this(resp)) {
2919 slot->sl_flags &= ~NFSD4_SLOT_CACHED;
bf864a31 2920 return;
074fe897 2921 }
085def3a
BF
2922 slot->sl_flags |= NFSD4_SLOT_CACHED;
2923
f5236013
BF
2924 base = resp->cstate.data_offset;
2925 slot->sl_datalen = buf->len - base;
2926 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
d3f03403
DC
2927 WARN(1, "%s: sessions DRC could not cache compound\n",
2928 __func__);
557ce264 2929 return;
074fe897
AA
2930}
2931
2932/*
abfabf8c
AA
2933 * Encode the replay sequence operation from the slot values.
2934 * If cachethis is FALSE encode the uncached rep error on the next
2935 * operation which sets resp->p and increments resp->opcnt for
2936 * nfs4svc_encode_compoundres.
074fe897 2937 *
074fe897 2938 */
abfabf8c
AA
2939static __be32
2940nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2941 struct nfsd4_compoundres *resp)
074fe897 2942{
abfabf8c
AA
2943 struct nfsd4_op *op;
2944 struct nfsd4_slot *slot = resp->cstate.slot;
bf864a31 2945
abfabf8c
AA
2946 /* Encode the replayed sequence operation */
2947 op = &args->ops[resp->opcnt - 1];
2948 nfsd4_encode_operation(resp, op);
bf864a31 2949
085def3a
BF
2950 if (slot->sl_flags & NFSD4_SLOT_CACHED)
2951 return op->status;
2952 if (args->opcnt == 1) {
2953 /*
2954 * The original operation wasn't a solo sequence--we
2955 * always cache those--so this retry must not match the
2956 * original:
2957 */
2958 op->status = nfserr_seq_false_retry;
2959 } else {
abfabf8c
AA
2960 op = &args->ops[resp->opcnt++];
2961 op->status = nfserr_retry_uncached_rep;
2962 nfsd4_encode_operation(resp, op);
074fe897 2963 }
abfabf8c 2964 return op->status;
074fe897
AA
2965}
2966
2967/*
557ce264
AA
2968 * The sequence operation is not cached because we can use the slot and
2969 * session values.
074fe897 2970 */
3ca2eb98 2971static __be32
bf864a31
AA
2972nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2973 struct nfsd4_sequence *seq)
074fe897 2974{
557ce264 2975 struct nfsd4_slot *slot = resp->cstate.slot;
f5236013
BF
2976 struct xdr_stream *xdr = &resp->xdr;
2977 __be32 *p;
074fe897
AA
2978 __be32 status;
2979
557ce264 2980 dprintk("--> %s slot %p\n", __func__, slot);
074fe897 2981
abfabf8c 2982 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
0da7b19c 2983 if (status)
abfabf8c 2984 return status;
074fe897 2985
f5236013
BF
2986 p = xdr_reserve_space(xdr, slot->sl_datalen);
2987 if (!p) {
2988 WARN_ON_ONCE(1);
2989 return nfserr_serverfault;
2990 }
2991 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2992 xdr_commit_encode(xdr);
074fe897 2993
557ce264 2994 resp->opcnt = slot->sl_opcnt;
f5236013 2995 return slot->sl_status;
074fe897
AA
2996}
2997
0733d213
AA
2998/*
2999 * Set the exchange_id flags returned by the server.
3000 */
3001static void
3002nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
3003{
9cf514cc
CH
3004#ifdef CONFIG_NFSD_PNFS
3005 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS;
3006#else
0733d213 3007 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
9cf514cc 3008#endif
0733d213
AA
3009
3010 /* Referrals are supported, Migration is not. */
3011 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
3012
3013 /* set the wire flags to return to client. */
3014 clid->flags = new->cl_exchange_flags;
3015}
3016
4eaea134
BF
3017static bool client_has_openowners(struct nfs4_client *clp)
3018{
3019 struct nfs4_openowner *oo;
3020
3021 list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) {
3022 if (!list_empty(&oo->oo_owner.so_stateids))
3023 return true;
3024 }
3025 return false;
3026}
3027
631fc9ea
BF
3028static bool client_has_state(struct nfs4_client *clp)
3029{
4eaea134 3030 return client_has_openowners(clp)
47e970be
KM
3031#ifdef CONFIG_NFSD_PNFS
3032 || !list_empty(&clp->cl_lo_states)
3033#endif
6eccece9 3034 || !list_empty(&clp->cl_delegations)
e0639dc5
OK
3035 || !list_empty(&clp->cl_sessions)
3036 || !list_empty(&clp->async_copies);
631fc9ea
BF
3037}
3038
79123444
BF
3039static __be32 copy_impl_id(struct nfs4_client *clp,
3040 struct nfsd4_exchange_id *exid)
3041{
3042 if (!exid->nii_domain.data)
3043 return 0;
3044 xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL);
3045 if (!clp->cl_nii_domain.data)
3046 return nfserr_jukebox;
3047 xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL);
3048 if (!clp->cl_nii_name.data)
3049 return nfserr_jukebox;
e29f4703 3050 clp->cl_nii_time = exid->nii_time;
79123444
BF
3051 return 0;
3052}
3053
069b6ad4 3054__be32
eb69853d
CH
3055nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3056 union nfsd4_op_u *u)
069b6ad4 3057{
eb69853d 3058 struct nfsd4_exchange_id *exid = &u->exchange_id;
3dbacee6
TM
3059 struct nfs4_client *conf, *new;
3060 struct nfs4_client *unconf = NULL;
57b7b43b 3061 __be32 status;
363168b4 3062 char addr_str[INET6_ADDRSTRLEN];
0733d213 3063 nfs4_verifier verf = exid->verifier;
363168b4 3064 struct sockaddr *sa = svc_addr(rqstp);
83e08fd4 3065 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
c212cecf 3066 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
0733d213 3067
363168b4 3068 rpc_ntop(sa, addr_str, sizeof(addr_str));
0733d213 3069 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
523ec6ed 3070 "ip_addr=%s flags %x, spa_how %u\n",
0733d213 3071 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
363168b4 3072 addr_str, exid->flags, exid->spa_how);
0733d213 3073
a084daf5 3074 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
0733d213
AA
3075 return nfserr_inval;
3076
50c7b948
BF
3077 new = create_client(exid->clname, rqstp, &verf);
3078 if (new == NULL)
3079 return nfserr_jukebox;
79123444
BF
3080 status = copy_impl_id(new, exid);
3081 if (status)
3082 goto out_nolock;
50c7b948 3083
0733d213 3084 switch (exid->spa_how) {
57266a6e 3085 case SP4_MACH_CRED:
ed941643
AE
3086 exid->spo_must_enforce[0] = 0;
3087 exid->spo_must_enforce[1] = (
3088 1 << (OP_BIND_CONN_TO_SESSION - 32) |
3089 1 << (OP_EXCHANGE_ID - 32) |
3090 1 << (OP_CREATE_SESSION - 32) |
3091 1 << (OP_DESTROY_SESSION - 32) |
3092 1 << (OP_DESTROY_CLIENTID - 32));
3093
3094 exid->spo_must_allow[0] &= (1 << (OP_CLOSE) |
3095 1 << (OP_OPEN_DOWNGRADE) |
3096 1 << (OP_LOCKU) |
3097 1 << (OP_DELEGRETURN));
3098
3099 exid->spo_must_allow[1] &= (
3100 1 << (OP_TEST_STATEID - 32) |
3101 1 << (OP_FREE_STATEID - 32));
50c7b948
BF
3102 if (!svc_rqst_integrity_protected(rqstp)) {
3103 status = nfserr_inval;
3104 goto out_nolock;
920dd9bb
BF
3105 }
3106 /*
3107 * Sometimes userspace doesn't give us a principal.
3108 * Which is a bug, really. Anyway, we can't enforce
3109 * MACH_CRED in that case, better to give up now:
3110 */
414ca017
BF
3111 if (!new->cl_cred.cr_principal &&
3112 !new->cl_cred.cr_raw_principal) {
920dd9bb
BF
3113 status = nfserr_serverfault;
3114 goto out_nolock;
50c7b948
BF
3115 }
3116 new->cl_mach_cred = true;
0733d213
AA
3117 case SP4_NONE:
3118 break;
063b0fb9
BF
3119 default: /* checked by xdr code */
3120 WARN_ON_ONCE(1);
df561f66 3121 fallthrough;
0733d213 3122 case SP4_SSV:
8edf4b02
KM
3123 status = nfserr_encr_alg_unsupp;
3124 goto out_nolock;
0733d213
AA
3125 }
3126
2dbb269d 3127 /* Cases below refer to rfc 5661 section 18.35.4: */
3dbacee6 3128 spin_lock(&nn->client_lock);
382a62e7 3129 conf = find_confirmed_client_by_name(&exid->clname, nn);
0733d213 3130 if (conf) {
83e08fd4
BF
3131 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
3132 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
3133
136e658d
BF
3134 if (update) {
3135 if (!clp_used_exchangeid(conf)) { /* buggy client */
2dbb269d 3136 status = nfserr_inval;
1a308118
BF
3137 goto out;
3138 }
dedeb13f 3139 if (!nfsd4_mach_creds_match(conf, rqstp)) {
57266a6e
BF
3140 status = nfserr_wrong_cred;
3141 goto out;
3142 }
136e658d 3143 if (!creds_match) { /* case 9 */
ea236d07 3144 status = nfserr_perm;
136e658d
BF
3145 goto out;
3146 }
3147 if (!verfs_match) { /* case 8 */
0733d213
AA
3148 status = nfserr_not_same;
3149 goto out;
3150 }
136e658d
BF
3151 /* case 6 */
3152 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
136e658d 3153 goto out_copy;
0733d213 3154 }
136e658d 3155 if (!creds_match) { /* case 3 */
631fc9ea
BF
3156 if (client_has_state(conf)) {
3157 status = nfserr_clid_inuse;
0733d213
AA
3158 goto out;
3159 }
0733d213
AA
3160 goto out_new;
3161 }
136e658d 3162 if (verfs_match) { /* case 2 */
0f1ba0ef 3163 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
136e658d
BF
3164 goto out_copy;
3165 }
3166 /* case 5, client reboot */
3dbacee6 3167 conf = NULL;
136e658d 3168 goto out_new;
6ddbbbfe
MS
3169 }
3170
2dbb269d 3171 if (update) { /* case 7 */
6ddbbbfe
MS
3172 status = nfserr_noent;
3173 goto out;
0733d213
AA
3174 }
3175
a99454aa 3176 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2dbb269d 3177 if (unconf) /* case 4, possible retry or client restart */
3dbacee6 3178 unhash_client_locked(unconf);
0733d213 3179
2dbb269d 3180 /* case 1 (normal case) */
0733d213 3181out_new:
fd699b8a
JL
3182 if (conf) {
3183 status = mark_client_expired_locked(conf);
3184 if (status)
3185 goto out;
3186 }
4f540e29 3187 new->cl_minorversion = cstate->minorversion;
ed941643
AE
3188 new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0];
3189 new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1];
0733d213 3190
ac55fdc4 3191 add_to_unconfirmed(new);
3dbacee6 3192 swap(new, conf);
0733d213 3193out_copy:
5cc40fd7
TM
3194 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
3195 exid->clientid.cl_id = conf->cl_clientid.cl_id;
0733d213 3196
5cc40fd7
TM
3197 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
3198 nfsd4_set_ex_flags(conf, exid);
0733d213
AA
3199
3200 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
5cc40fd7 3201 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
0733d213
AA
3202 status = nfs_ok;
3203
3204out:
3dbacee6 3205 spin_unlock(&nn->client_lock);
50c7b948 3206out_nolock:
5cc40fd7 3207 if (new)
3dbacee6
TM
3208 expire_client(new);
3209 if (unconf)
3210 expire_client(unconf);
0733d213 3211 return status;
069b6ad4
AA
3212}
3213
57b7b43b 3214static __be32
88e588d5 3215check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
b85d4c01 3216{
88e588d5
AA
3217 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
3218 slot_seqid);
b85d4c01
BH
3219
3220 /* The slot is in use, and no response has been sent. */
88e588d5
AA
3221 if (slot_inuse) {
3222 if (seqid == slot_seqid)
b85d4c01
BH
3223 return nfserr_jukebox;
3224 else
3225 return nfserr_seq_misordered;
3226 }
f6d82485 3227 /* Note unsigned 32-bit arithmetic handles wraparound: */
88e588d5 3228 if (likely(seqid == slot_seqid + 1))
b85d4c01 3229 return nfs_ok;
88e588d5 3230 if (seqid == slot_seqid)
b85d4c01 3231 return nfserr_replay_cache;
b85d4c01
BH
3232 return nfserr_seq_misordered;
3233}
3234
49557cc7
AA
3235/*
3236 * Cache the create session result into the create session single DRC
3237 * slot cache by saving the xdr structure. sl_seqid has been set.
3238 * Do this for solo or embedded create session operations.
3239 */
3240static void
3241nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
57b7b43b 3242 struct nfsd4_clid_slot *slot, __be32 nfserr)
49557cc7
AA
3243{
3244 slot->sl_status = nfserr;
3245 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
3246}
3247
3248static __be32
3249nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
3250 struct nfsd4_clid_slot *slot)
3251{
3252 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
3253 return slot->sl_status;
3254}
3255
1b74c25b
MJ
3256#define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3257 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3258 1 + /* MIN tag is length with zero, only length */ \
3259 3 + /* version, opcount, opcode */ \
3260 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3261 /* seqid, slotID, slotID, cache */ \
3262 4 ) * sizeof(__be32))
3263
3264#define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3265 2 + /* verifier: AUTH_NULL, length 0 */\
3266 1 + /* status */ \
3267 1 + /* MIN tag is length with zero, only length */ \
3268 3 + /* opcount, opcode, opstatus*/ \
3269 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3270 /* seqid, slotID, slotID, slotID, status */ \
3271 5 ) * sizeof(__be32))
3272
55c760cf 3273static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
1b74c25b 3274{
55c760cf
BF
3275 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
3276
373cd409
BF
3277 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
3278 return nfserr_toosmall;
3279 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
3280 return nfserr_toosmall;
55c760cf
BF
3281 ca->headerpadsz = 0;
3282 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
3283 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
3284 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
3285 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
3286 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
3287 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
3288 /*
3289 * Note decreasing slot size below client's request may make it
3290 * difficult for client to function correctly, whereas
3291 * decreasing the number of slots will (just?) affect
3292 * performance. When short on memory we therefore prefer to
3293 * decrease number of slots instead of their size. Clients that
3294 * request larger slots than they need will get poor results:
7f49fd5d
N
3295 * Note that we always allow at least one slot, because our
3296 * accounting is soft and provides no guarantees either way.
55c760cf 3297 */
2030ca56 3298 ca->maxreqs = nfsd4_get_drc_mem(ca, nn);
55c760cf 3299
373cd409 3300 return nfs_ok;
1b74c25b
MJ
3301}
3302
4500632f
CL
3303/*
3304 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3305 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3306 */
3307#define RPC_MAX_HEADER_WITH_AUTH_SYS \
3308 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3309
3310#define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3311 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3312
8a891633 3313#define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
4500632f 3314 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
8a891633 3315#define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
4500632f
CL
3316 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3317 sizeof(__be32))
8a891633 3318
06b332a5 3319static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
1b74c25b 3320{
06b332a5
BF
3321 ca->headerpadsz = 0;
3322
8a891633 3323 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
06b332a5 3324 return nfserr_toosmall;
8a891633 3325 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
06b332a5
BF
3326 return nfserr_toosmall;
3327 ca->maxresp_cached = 0;
3328 if (ca->maxops < 2)
3329 return nfserr_toosmall;
3330
3331 return nfs_ok;
1b74c25b
MJ
3332}
3333
b78724b7
BF
3334static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
3335{
3336 switch (cbs->flavor) {
3337 case RPC_AUTH_NULL:
3338 case RPC_AUTH_UNIX:
3339 return nfs_ok;
3340 default:
3341 /*
3342 * GSS case: the spec doesn't allow us to return this
3343 * error. But it also doesn't allow us not to support
3344 * GSS.
3345 * I'd rather this fail hard than return some error the
3346 * client might think it can already handle:
3347 */
3348 return nfserr_encr_alg_unsupp;
3349 }
3350}
3351
069b6ad4
AA
3352__be32
3353nfsd4_create_session(struct svc_rqst *rqstp,
eb69853d 3354 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
069b6ad4 3355{
eb69853d 3356 struct nfsd4_create_session *cr_ses = &u->create_session;
363168b4 3357 struct sockaddr *sa = svc_addr(rqstp);
ec6b5d7b 3358 struct nfs4_client *conf, *unconf;
d20c11d8 3359 struct nfs4_client *old = NULL;
ac7c46f2 3360 struct nfsd4_session *new;
81f0b2a4 3361 struct nfsd4_conn *conn;
49557cc7 3362 struct nfsd4_clid_slot *cs_slot = NULL;
57b7b43b 3363 __be32 status = 0;
8daae4dc 3364 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
ec6b5d7b 3365
a62573dc
MJ
3366 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
3367 return nfserr_inval;
b78724b7
BF
3368 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
3369 if (status)
3370 return status;
55c760cf 3371 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
06b332a5
BF
3372 if (status)
3373 return status;
3374 status = check_backchannel_attrs(&cr_ses->back_channel);
373cd409 3375 if (status)
f403e450 3376 goto out_release_drc_mem;
81f0b2a4 3377 status = nfserr_jukebox;
60810e54 3378 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
55c760cf
BF
3379 if (!new)
3380 goto out_release_drc_mem;
81f0b2a4
BF
3381 conn = alloc_conn_from_crses(rqstp, cr_ses);
3382 if (!conn)
3383 goto out_free_session;
a62573dc 3384
d20c11d8 3385 spin_lock(&nn->client_lock);
0a7ec377 3386 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
8daae4dc 3387 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
78389046 3388 WARN_ON_ONCE(conf && unconf);
ec6b5d7b
AA
3389
3390 if (conf) {
57266a6e 3391 status = nfserr_wrong_cred;
dedeb13f 3392 if (!nfsd4_mach_creds_match(conf, rqstp))
57266a6e 3393 goto out_free_conn;
49557cc7
AA
3394 cs_slot = &conf->cl_cs_slot;
3395 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
f5e22bb6
KM
3396 if (status) {
3397 if (status == nfserr_replay_cache)
3398 status = nfsd4_replay_create_session(cr_ses, cs_slot);
81f0b2a4 3399 goto out_free_conn;
ec6b5d7b 3400 }
ec6b5d7b
AA
3401 } else if (unconf) {
3402 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
363168b4 3403 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
ec6b5d7b 3404 status = nfserr_clid_inuse;
81f0b2a4 3405 goto out_free_conn;
ec6b5d7b 3406 }
57266a6e 3407 status = nfserr_wrong_cred;
dedeb13f 3408 if (!nfsd4_mach_creds_match(unconf, rqstp))
57266a6e 3409 goto out_free_conn;
49557cc7
AA
3410 cs_slot = &unconf->cl_cs_slot;
3411 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
38eb76a5
AA
3412 if (status) {
3413 /* an unconfirmed replay returns misordered */
ec6b5d7b 3414 status = nfserr_seq_misordered;
81f0b2a4 3415 goto out_free_conn;
ec6b5d7b 3416 }
382a62e7 3417 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
221a6876 3418 if (old) {
d20c11d8 3419 status = mark_client_expired_locked(old);
7abea1e8
JL
3420 if (status) {
3421 old = NULL;
221a6876 3422 goto out_free_conn;
7abea1e8 3423 }
221a6876 3424 }
8f9d3d3b 3425 move_to_confirmed(unconf);
ec6b5d7b
AA
3426 conf = unconf;
3427 } else {
3428 status = nfserr_stale_clientid;
81f0b2a4 3429 goto out_free_conn;
ec6b5d7b 3430 }
81f0b2a4 3431 status = nfs_ok;
4ce85c8c 3432 /* Persistent sessions are not supported */
408b79bc 3433 cr_ses->flags &= ~SESSION4_PERSIST;
4ce85c8c 3434 /* Upshifting from TCP to RDMA is not supported */
408b79bc
BF
3435 cr_ses->flags &= ~SESSION4_RDMA;
3436
81f0b2a4 3437 init_session(rqstp, new, conf, cr_ses);
d20c11d8 3438 nfsd4_get_session_locked(new);
81f0b2a4 3439
ac7c46f2 3440 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
ec6b5d7b 3441 NFS4_MAX_SESSIONID_LEN);
86c3e16c 3442 cs_slot->sl_seqid++;
49557cc7 3443 cr_ses->seqid = cs_slot->sl_seqid;
ec6b5d7b 3444
d20c11d8 3445 /* cache solo and embedded create sessions under the client_lock */
49557cc7 3446 nfsd4_cache_create_session(cr_ses, cs_slot, status);
d20c11d8
JL
3447 spin_unlock(&nn->client_lock);
3448 /* init connection and backchannel */
3449 nfsd4_init_conn(rqstp, conn, new);
3450 nfsd4_put_session(new);
d20c11d8
JL
3451 if (old)
3452 expire_client(old);
ec6b5d7b 3453 return status;
81f0b2a4 3454out_free_conn:
d20c11d8 3455 spin_unlock(&nn->client_lock);
81f0b2a4 3456 free_conn(conn);
d20c11d8
JL
3457 if (old)
3458 expire_client(old);
81f0b2a4
BF
3459out_free_session:
3460 __free_session(new);
55c760cf
BF
3461out_release_drc_mem:
3462 nfsd4_put_drc_mem(&cr_ses->fore_channel);
1ca50792 3463 return status;
069b6ad4
AA
3464}
3465
1d1bc8f2
BF
3466static __be32 nfsd4_map_bcts_dir(u32 *dir)
3467{
3468 switch (*dir) {
3469 case NFS4_CDFC4_FORE:
3470 case NFS4_CDFC4_BACK:
3471 return nfs_ok;
3472 case NFS4_CDFC4_FORE_OR_BOTH:
3473 case NFS4_CDFC4_BACK_OR_BOTH:
3474 *dir = NFS4_CDFC4_BOTH;
3475 return nfs_ok;
fc5fc5d7 3476 }
1d1bc8f2
BF
3477 return nfserr_inval;
3478}
3479
eb69853d
CH
3480__be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp,
3481 struct nfsd4_compound_state *cstate,
3482 union nfsd4_op_u *u)
cb73a9f4 3483{
eb69853d 3484 struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl;
cb73a9f4 3485 struct nfsd4_session *session = cstate->session;
c9a49628 3486 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
b78724b7 3487 __be32 status;
cb73a9f4 3488
b78724b7
BF
3489 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
3490 if (status)
3491 return status;
c9a49628 3492 spin_lock(&nn->client_lock);
cb73a9f4
BF
3493 session->se_cb_prog = bc->bc_cb_program;
3494 session->se_cb_sec = bc->bc_cb_sec;
c9a49628 3495 spin_unlock(&nn->client_lock);
cb73a9f4
BF
3496
3497 nfsd4_probe_callback(session->se_client);
3498
3499 return nfs_ok;
3500}
3501
c2d715a1
BF
3502static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
3503{
3504 struct nfsd4_conn *c;
3505
3506 list_for_each_entry(c, &s->se_conns, cn_persession) {
3507 if (c->cn_xprt == xpt) {
3508 return c;
3509 }
3510 }
3511 return NULL;
3512}
3513
3514static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst,
3515 struct nfsd4_session *session, u32 req)
3516{
3517 struct nfs4_client *clp = session->se_client;
3518 struct svc_xprt *xpt = rqst->rq_xprt;
3519 struct nfsd4_conn *c;
3520 __be32 status;
3521
3522 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3523 spin_lock(&clp->cl_lock);
3524 c = __nfsd4_find_conn(xpt, session);
3525 if (!c)
3526 status = nfserr_noent;
3527 else if (req == c->cn_flags)
3528 status = nfs_ok;
3529 else if (req == NFS4_CDFC4_FORE_OR_BOTH &&
3530 c->cn_flags != NFS4_CDFC4_BACK)
3531 status = nfs_ok;
3532 else if (req == NFS4_CDFC4_BACK_OR_BOTH &&
3533 c->cn_flags != NFS4_CDFC4_FORE)
3534 status = nfs_ok;
3535 else
3536 status = nfserr_inval;
3537 spin_unlock(&clp->cl_lock);
3538 return status;
3539}
3540
1d1bc8f2
BF
3541__be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
3542 struct nfsd4_compound_state *cstate,
eb69853d 3543 union nfsd4_op_u *u)
1d1bc8f2 3544{
eb69853d 3545 struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session;
1d1bc8f2 3546 __be32 status;
3ba63671 3547 struct nfsd4_conn *conn;
4f6e6c17 3548 struct nfsd4_session *session;
d4e19e70
TM
3549 struct net *net = SVC_NET(rqstp);
3550 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1d1bc8f2
BF
3551
3552 if (!nfsd4_last_compound_op(rqstp))
3553 return nfserr_not_only_op;
c9a49628 3554 spin_lock(&nn->client_lock);
d4e19e70 3555 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
c9a49628 3556 spin_unlock(&nn->client_lock);
4f6e6c17 3557 if (!session)
d4e19e70 3558 goto out_no_session;
57266a6e 3559 status = nfserr_wrong_cred;
dedeb13f 3560 if (!nfsd4_mach_creds_match(session->se_client, rqstp))
57266a6e 3561 goto out;
c2d715a1
BF
3562 status = nfsd4_match_existing_connection(rqstp, session, bcts->dir);
3563 if (status == nfs_ok || status == nfserr_inval)
3564 goto out;
1d1bc8f2 3565 status = nfsd4_map_bcts_dir(&bcts->dir);
3ba63671 3566 if (status)
4f6e6c17 3567 goto out;
3ba63671 3568 conn = alloc_conn(rqstp, bcts->dir);
4f6e6c17 3569 status = nfserr_jukebox;
3ba63671 3570 if (!conn)
4f6e6c17
BF
3571 goto out;
3572 nfsd4_init_conn(rqstp, conn, session);
3573 status = nfs_ok;
3574out:
d4e19e70
TM
3575 nfsd4_put_session(session);
3576out_no_session:
4f6e6c17 3577 return status;
1d1bc8f2
BF
3578}
3579
665d5072 3580static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid)
5d4cec2f 3581{
665d5072 3582 if (!cstate->session)
51d87bc2 3583 return false;
665d5072 3584 return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid));
5d4cec2f
BF
3585}
3586
069b6ad4 3587__be32
eb69853d
CH
3588nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate,
3589 union nfsd4_op_u *u)
069b6ad4 3590{
ca0552f4 3591 struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid;
e10e0cfc 3592 struct nfsd4_session *ses;
abcdff09 3593 __be32 status;
f0f51f5c 3594 int ref_held_by_me = 0;
d4e19e70
TM
3595 struct net *net = SVC_NET(r);
3596 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
e10e0cfc 3597
abcdff09 3598 status = nfserr_not_only_op;
ca0552f4 3599 if (nfsd4_compound_in_session(cstate, sessionid)) {
57716355 3600 if (!nfsd4_last_compound_op(r))
abcdff09 3601 goto out;
f0f51f5c 3602 ref_held_by_me++;
57716355 3603 }
ca0552f4 3604 dump_sessionid(__func__, sessionid);
c9a49628 3605 spin_lock(&nn->client_lock);
ca0552f4 3606 ses = find_in_sessionid_hashtbl(sessionid, net, &status);
abcdff09
BF
3607 if (!ses)
3608 goto out_client_lock;
57266a6e 3609 status = nfserr_wrong_cred;
dedeb13f 3610 if (!nfsd4_mach_creds_match(ses->se_client, r))
d4e19e70 3611 goto out_put_session;
f0f51f5c 3612 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
66b2b9b2 3613 if (status)
f0f51f5c 3614 goto out_put_session;
e10e0cfc 3615 unhash_session(ses);
c9a49628 3616 spin_unlock(&nn->client_lock);
e10e0cfc 3617
84f5f7cc 3618 nfsd4_probe_callback_sync(ses->se_client);
19cf5c02 3619
c9a49628 3620 spin_lock(&nn->client_lock);
e10e0cfc 3621 status = nfs_ok;
f0f51f5c 3622out_put_session:
d4e19e70 3623 nfsd4_put_session_locked(ses);
abcdff09
BF
3624out_client_lock:
3625 spin_unlock(&nn->client_lock);
e10e0cfc 3626out:
e10e0cfc 3627 return status;
069b6ad4
AA
3628}
3629
57266a6e 3630static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
328ead28
BF
3631{
3632 struct nfs4_client *clp = ses->se_client;
a663bdd8 3633 struct nfsd4_conn *c;
57266a6e 3634 __be32 status = nfs_ok;
21b75b01 3635 int ret;
328ead28
BF
3636
3637 spin_lock(&clp->cl_lock);
a663bdd8 3638 c = __nfsd4_find_conn(new->cn_xprt, ses);
57266a6e
BF
3639 if (c)
3640 goto out_free;
3641 status = nfserr_conn_not_bound_to_session;
3642 if (clp->cl_mach_cred)
3643 goto out_free;
328ead28
BF
3644 __nfsd4_hash_conn(new, ses);
3645 spin_unlock(&clp->cl_lock);
21b75b01
BF
3646 ret = nfsd4_register_conn(new);
3647 if (ret)
3648 /* oops; xprt is already down: */
3649 nfsd4_conn_lost(&new->cn_xpt_user);
57266a6e
BF
3650 return nfs_ok;
3651out_free:
3652 spin_unlock(&clp->cl_lock);
3653 free_conn(new);
3654 return status;
328ead28
BF
3655}
3656
868b89c3
MJ
3657static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
3658{
3659 struct nfsd4_compoundargs *args = rqstp->rq_argp;
3660
3661 return args->opcnt > session->se_fchannel.maxops;
3662}
3663
ae82a8d0
MJ
3664static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
3665 struct nfsd4_session *session)
3666{
3667 struct xdr_buf *xb = &rqstp->rq_arg;
3668
3669 return xb->len > session->se_fchannel.maxreq_sz;
3670}
3671
53da6a53
BF
3672static bool replay_matches_cache(struct svc_rqst *rqstp,
3673 struct nfsd4_sequence *seq, struct nfsd4_slot *slot)
3674{
3675 struct nfsd4_compoundargs *argp = rqstp->rq_argp;
3676
3677 if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) !=
3678 (bool)seq->cachethis)
3679 return false;
3680 /*
6e73e92b
SM
3681 * If there's an error then the reply can have fewer ops than
3682 * the call.
53da6a53 3683 */
6e73e92b
SM
3684 if (slot->sl_opcnt < argp->opcnt && !slot->sl_status)
3685 return false;
3686 /*
3687 * But if we cached a reply with *more* ops than the call you're
3688 * sending us now, then this new call is clearly not really a
3689 * replay of the old one:
3690 */
3691 if (slot->sl_opcnt > argp->opcnt)
53da6a53
BF
3692 return false;
3693 /* This is the only check explicitly called by spec: */
3694 if (!same_creds(&rqstp->rq_cred, &slot->sl_cred))
3695 return false;
3696 /*
3697 * There may be more comparisons we could actually do, but the
3698 * spec doesn't require us to catch every case where the calls
3699 * don't match (that would require caching the call as well as
3700 * the reply), so we don't bother.
3701 */
3702 return true;
3703}
3704
069b6ad4 3705__be32
eb69853d
CH
3706nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
3707 union nfsd4_op_u *u)
069b6ad4 3708{
eb69853d 3709 struct nfsd4_sequence *seq = &u->sequence;
f9bb94c4 3710 struct nfsd4_compoundres *resp = rqstp->rq_resp;
47ee5298 3711 struct xdr_stream *xdr = &resp->xdr;
b85d4c01 3712 struct nfsd4_session *session;
221a6876 3713 struct nfs4_client *clp;
b85d4c01 3714 struct nfsd4_slot *slot;
a663bdd8 3715 struct nfsd4_conn *conn;
57b7b43b 3716 __be32 status;
47ee5298 3717 int buflen;
d4e19e70
TM
3718 struct net *net = SVC_NET(rqstp);
3719 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b85d4c01 3720
f9bb94c4
AA
3721 if (resp->opcnt != 1)
3722 return nfserr_sequence_pos;
3723
a663bdd8
BF
3724 /*
3725 * Will be either used or freed by nfsd4_sequence_check_conn
3726 * below.
3727 */
3728 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
3729 if (!conn)
3730 return nfserr_jukebox;
3731
c9a49628 3732 spin_lock(&nn->client_lock);
d4e19e70 3733 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
b85d4c01 3734 if (!session)
221a6876
BF
3735 goto out_no_session;
3736 clp = session->se_client;
b85d4c01 3737
868b89c3
MJ
3738 status = nfserr_too_many_ops;
3739 if (nfsd4_session_too_many_ops(rqstp, session))
66b2b9b2 3740 goto out_put_session;
868b89c3 3741
ae82a8d0
MJ
3742 status = nfserr_req_too_big;
3743 if (nfsd4_request_too_big(rqstp, session))
66b2b9b2 3744 goto out_put_session;
ae82a8d0 3745
b85d4c01 3746 status = nfserr_badslot;
6c18ba9f 3747 if (seq->slotid >= session->se_fchannel.maxreqs)
66b2b9b2 3748 goto out_put_session;
b85d4c01 3749
557ce264 3750 slot = session->se_slots[seq->slotid];
b85d4c01
BH
3751 dprintk("%s: slotid %d\n", __func__, seq->slotid);
3752
a8dfdaeb
AA
3753 /* We do not negotiate the number of slots yet, so set the
3754 * maxslots to the session maxreqs which is used to encode
3755 * sr_highest_slotid and the sr_target_slot id to maxslots */
3756 seq->maxslots = session->se_fchannel.maxreqs;
3757
73e79482
BF
3758 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
3759 slot->sl_flags & NFSD4_SLOT_INUSE);
b85d4c01 3760 if (status == nfserr_replay_cache) {
bf5c43c8
BF
3761 status = nfserr_seq_misordered;
3762 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
66b2b9b2 3763 goto out_put_session;
53da6a53
BF
3764 status = nfserr_seq_false_retry;
3765 if (!replay_matches_cache(rqstp, seq, slot))
3766 goto out_put_session;
b85d4c01
BH
3767 cstate->slot = slot;
3768 cstate->session = session;
4b24ca7d 3769 cstate->clp = clp;
da3846a2 3770 /* Return the cached reply status and set cstate->status
557ce264 3771 * for nfsd4_proc_compound processing */
bf864a31 3772 status = nfsd4_replay_cache_entry(resp, seq);
da3846a2 3773 cstate->status = nfserr_replay_cache;
aaf84eb9 3774 goto out;
b85d4c01
BH
3775 }
3776 if (status)
66b2b9b2 3777 goto out_put_session;
b85d4c01 3778
57266a6e 3779 status = nfsd4_sequence_check_conn(conn, session);
a663bdd8 3780 conn = NULL;
57266a6e
BF
3781 if (status)
3782 goto out_put_session;
328ead28 3783
47ee5298
BF
3784 buflen = (seq->cachethis) ?
3785 session->se_fchannel.maxresp_cached :
3786 session->se_fchannel.maxresp_sz;
3787 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
3788 nfserr_rep_too_big;
a5cddc88 3789 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
47ee5298 3790 goto out_put_session;
32aaa62e 3791 svc_reserve(rqstp, buflen);
47ee5298
BF
3792
3793 status = nfs_ok;
b85d4c01 3794 /* Success! bump slot seqid */
b85d4c01 3795 slot->sl_seqid = seq->seqid;
bf5c43c8 3796 slot->sl_flags |= NFSD4_SLOT_INUSE;
73e79482
BF
3797 if (seq->cachethis)
3798 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
bf5c43c8
BF
3799 else
3800 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
b85d4c01
BH
3801
3802 cstate->slot = slot;
3803 cstate->session = session;
4b24ca7d 3804 cstate->clp = clp;
b85d4c01 3805
b85d4c01 3806out:
221a6876
BF
3807 switch (clp->cl_cb_state) {
3808 case NFSD4_CB_DOWN:
3809 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
3810 break;
3811 case NFSD4_CB_FAULT:
3812 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
3813 break;
3814 default:
3815 seq->status_flags = 0;
aaf84eb9 3816 }
3bd64a5b
BF
3817 if (!list_empty(&clp->cl_revoked))
3818 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
221a6876 3819out_no_session:
3f42d2c4
KM
3820 if (conn)
3821 free_conn(conn);
c9a49628 3822 spin_unlock(&nn->client_lock);
b85d4c01 3823 return status;
66b2b9b2 3824out_put_session:
d4e19e70 3825 nfsd4_put_session_locked(session);
221a6876 3826 goto out_no_session;
069b6ad4
AA
3827}
3828
b607664e
TM
3829void
3830nfsd4_sequence_done(struct nfsd4_compoundres *resp)
3831{
3832 struct nfsd4_compound_state *cs = &resp->cstate;
3833
3834 if (nfsd4_has_session(cs)) {
b607664e
TM
3835 if (cs->status != nfserr_replay_cache) {
3836 nfsd4_store_cache_entry(resp);
3837 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
3838 }
d4e19e70 3839 /* Drop session reference that was taken in nfsd4_sequence() */
b607664e 3840 nfsd4_put_session(cs->session);
4b24ca7d
JL
3841 } else if (cs->clp)
3842 put_client_renew(cs->clp);
b607664e
TM
3843}
3844
345c2842 3845__be32
eb69853d
CH
3846nfsd4_destroy_clientid(struct svc_rqst *rqstp,
3847 struct nfsd4_compound_state *cstate,
3848 union nfsd4_op_u *u)
345c2842 3849{
eb69853d 3850 struct nfsd4_destroy_clientid *dc = &u->destroy_clientid;
6b10ad19
TM
3851 struct nfs4_client *conf, *unconf;
3852 struct nfs4_client *clp = NULL;
57b7b43b 3853 __be32 status = 0;
8daae4dc 3854 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
345c2842 3855
6b10ad19 3856 spin_lock(&nn->client_lock);
0a7ec377 3857 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
8daae4dc 3858 conf = find_confirmed_client(&dc->clientid, true, nn);
78389046 3859 WARN_ON_ONCE(conf && unconf);
345c2842
MJ
3860
3861 if (conf) {
c0293b01 3862 if (client_has_state(conf)) {
345c2842
MJ
3863 status = nfserr_clientid_busy;
3864 goto out;
3865 }
fd699b8a
JL
3866 status = mark_client_expired_locked(conf);
3867 if (status)
3868 goto out;
6b10ad19 3869 clp = conf;
345c2842
MJ
3870 } else if (unconf)
3871 clp = unconf;
3872 else {
3873 status = nfserr_stale_clientid;
3874 goto out;
3875 }
dedeb13f 3876 if (!nfsd4_mach_creds_match(clp, rqstp)) {
6b10ad19 3877 clp = NULL;
57266a6e
BF
3878 status = nfserr_wrong_cred;
3879 goto out;
3880 }
6b10ad19 3881 unhash_client_locked(clp);
345c2842 3882out:
6b10ad19 3883 spin_unlock(&nn->client_lock);
6b10ad19
TM
3884 if (clp)
3885 expire_client(clp);
345c2842
MJ
3886 return status;
3887}
3888
4dc6ec00 3889__be32
eb69853d
CH
3890nfsd4_reclaim_complete(struct svc_rqst *rqstp,
3891 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
4dc6ec00 3892{
eb69853d 3893 struct nfsd4_reclaim_complete *rc = &u->reclaim_complete;
57b7b43b 3894 __be32 status = 0;
bcecf1cc 3895
4dc6ec00
BF
3896 if (rc->rca_one_fs) {
3897 if (!cstate->current_fh.fh_dentry)
3898 return nfserr_nofilehandle;
3899 /*
3900 * We don't take advantage of the rca_one_fs case.
3901 * That's OK, it's optional, we can safely ignore it.
3902 */
d28c442f 3903 return nfs_ok;
4dc6ec00 3904 }
bcecf1cc 3905
bcecf1cc 3906 status = nfserr_complete_already;
a52d726b
JL
3907 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
3908 &cstate->session->se_client->cl_flags))
bcecf1cc
MJ
3909 goto out;
3910
3911 status = nfserr_stale_clientid;
3912 if (is_client_expired(cstate->session->se_client))
4dc6ec00
BF
3913 /*
3914 * The following error isn't really legal.
3915 * But we only get here if the client just explicitly
3916 * destroyed the client. Surely it no longer cares what
3917 * error it gets back on an operation for the dead
3918 * client.
3919 */
bcecf1cc
MJ
3920 goto out;
3921
3922 status = nfs_ok;
2a4317c5 3923 nfsd4_client_record_create(cstate->session->se_client);
362063a5 3924 inc_reclaim_complete(cstate->session->se_client);
bcecf1cc 3925out:
bcecf1cc 3926 return status;
4dc6ec00
BF
3927}
3928
b37ad28b 3929__be32
b591480b 3930nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 3931 union nfsd4_op_u *u)
1da177e4 3932{
eb69853d 3933 struct nfsd4_setclientid *setclid = &u->setclientid;
a084daf5 3934 struct xdr_netobj clname = setclid->se_name;
1da177e4 3935 nfs4_verifier clverifier = setclid->se_verf;
3dbacee6
TM
3936 struct nfs4_client *conf, *new;
3937 struct nfs4_client *unconf = NULL;
b37ad28b 3938 __be32 status;
c212cecf
SK
3939 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3940
5cc40fd7
TM
3941 new = create_client(clname, rqstp, &clverifier);
3942 if (new == NULL)
3943 return nfserr_jukebox;
63db4632 3944 /* Cases below refer to rfc 3530 section 14.2.33: */
3dbacee6 3945 spin_lock(&nn->client_lock);
382a62e7 3946 conf = find_confirmed_client_by_name(&clname, nn);
2b634821 3947 if (conf && client_has_state(conf)) {
63db4632 3948 /* case 0: */
1da177e4 3949 status = nfserr_clid_inuse;
e203d506
BF
3950 if (clp_used_exchangeid(conf))
3951 goto out;
026722c2 3952 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
dd5e3fbc 3953 trace_nfsd_clid_inuse_err(conf);
1da177e4
LT
3954 goto out;
3955 }
1da177e4 3956 }
a99454aa 3957 unconf = find_unconfirmed_client_by_name(&clname, nn);
8f930711 3958 if (unconf)
3dbacee6 3959 unhash_client_locked(unconf);
45f56da8 3960 /* We need to handle only case 1: probable callback update */
41eb1670 3961 if (conf && same_verf(&conf->cl_verifier, &clverifier)) {
1da177e4 3962 copy_clid(new, conf);
41eb1670 3963 gen_confirm(new, nn);
45f56da8 3964 }
8323c3b2 3965 new->cl_minorversion = 0;
6f3d772f 3966 gen_callback(new, setclid, rqstp);
ac55fdc4 3967 add_to_unconfirmed(new);
1da177e4
LT
3968 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
3969 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
3970 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
5cc40fd7 3971 new = NULL;
1da177e4
LT
3972 status = nfs_ok;
3973out:
3dbacee6 3974 spin_unlock(&nn->client_lock);
5cc40fd7
TM
3975 if (new)
3976 free_client(new);
3dbacee6
TM
3977 if (unconf)
3978 expire_client(unconf);
1da177e4
LT
3979 return status;
3980}
3981
3982
b37ad28b 3983__be32
b591480b 3984nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
eb69853d
CH
3985 struct nfsd4_compound_state *cstate,
3986 union nfsd4_op_u *u)
1da177e4 3987{
eb69853d
CH
3988 struct nfsd4_setclientid_confirm *setclientid_confirm =
3989 &u->setclientid_confirm;
21ab45a4 3990 struct nfs4_client *conf, *unconf;
d20c11d8 3991 struct nfs4_client *old = NULL;
1da177e4
LT
3992 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
3993 clientid_t * clid = &setclientid_confirm->sc_clientid;
b37ad28b 3994 __be32 status;
7f2210fa 3995 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 3996
2c142baa 3997 if (STALE_CLIENTID(clid, nn))
1da177e4 3998 return nfserr_stale_clientid;
21ab45a4 3999
d20c11d8 4000 spin_lock(&nn->client_lock);
8daae4dc 4001 conf = find_confirmed_client(clid, false, nn);
0a7ec377 4002 unconf = find_unconfirmed_client(clid, false, nn);
a186e767 4003 /*
8695b90a
BF
4004 * We try hard to give out unique clientid's, so if we get an
4005 * attempt to confirm the same clientid with a different cred,
f984a7ce
BF
4006 * the client may be buggy; this should never happen.
4007 *
4008 * Nevertheless, RFC 7530 recommends INUSE for this case:
a186e767 4009 */
f984a7ce 4010 status = nfserr_clid_inuse;
8695b90a
BF
4011 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
4012 goto out;
4013 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
4014 goto out;
63db4632 4015 /* cases below refer to rfc 3530 section 14.2.34: */
90d700b7 4016 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
7d22fc11
BF
4017 if (conf && same_verf(&confirm, &conf->cl_confirm)) {
4018 /* case 2: probable retransmit */
1da177e4 4019 status = nfs_ok;
7d22fc11 4020 } else /* case 4: client hasn't noticed we rebooted yet? */
90d700b7
BF
4021 status = nfserr_stale_clientid;
4022 goto out;
4023 }
4024 status = nfs_ok;
4025 if (conf) { /* case 1: callback update */
d20c11d8
JL
4026 old = unconf;
4027 unhash_client_locked(old);
8695b90a 4028 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
90d700b7 4029 } else { /* case 3: normal case; new or rebooted client */
d20c11d8
JL
4030 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
4031 if (old) {
2b634821
BF
4032 status = nfserr_clid_inuse;
4033 if (client_has_state(old)
4034 && !same_creds(&unconf->cl_cred,
4035 &old->cl_cred))
4036 goto out;
d20c11d8 4037 status = mark_client_expired_locked(old);
7abea1e8
JL
4038 if (status) {
4039 old = NULL;
221a6876 4040 goto out;
7abea1e8 4041 }
221a6876 4042 }
8695b90a 4043 move_to_confirmed(unconf);
d20c11d8 4044 conf = unconf;
08e8987c 4045 }
d20c11d8
JL
4046 get_client_locked(conf);
4047 spin_unlock(&nn->client_lock);
4048 nfsd4_probe_callback(conf);
4049 spin_lock(&nn->client_lock);
4050 put_client_renew_locked(conf);
1da177e4 4051out:
d20c11d8
JL
4052 spin_unlock(&nn->client_lock);
4053 if (old)
4054 expire_client(old);
1da177e4
LT
4055 return status;
4056}
4057
32513b40
BF
4058static struct nfs4_file *nfsd4_alloc_file(void)
4059{
4060 return kmem_cache_alloc(file_slab, GFP_KERNEL);
4061}
4062
1da177e4 4063/* OPEN Share state helper functions */
5b095e99
JL
4064static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval,
4065 struct nfs4_file *fp)
1da177e4 4066{
950e0118
TM
4067 lockdep_assert_held(&state_lock);
4068
818a34eb 4069 refcount_set(&fp->fi_ref, 1);
1d31a253 4070 spin_lock_init(&fp->fi_lock);
32513b40
BF
4071 INIT_LIST_HEAD(&fp->fi_stateids);
4072 INIT_LIST_HEAD(&fp->fi_delegations);
8287f009 4073 INIT_LIST_HEAD(&fp->fi_clnt_odstate);
e2cf80d7 4074 fh_copy_shallow(&fp->fi_fhandle, fh);
0c637be8 4075 fp->fi_deleg_file = NULL;
32513b40 4076 fp->fi_had_conflict = false;
baeb4ff0 4077 fp->fi_share_deny = 0;
32513b40
BF
4078 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
4079 memset(fp->fi_access, 0, sizeof(fp->fi_access));
9cf514cc
CH
4080#ifdef CONFIG_NFSD_PNFS
4081 INIT_LIST_HEAD(&fp->fi_lo_states);
c5c707f9 4082 atomic_set(&fp->fi_lo_recalls, 0);
9cf514cc 4083#endif
5b095e99 4084 hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]);
1da177e4
LT
4085}
4086
e8ff2a84 4087void
1da177e4
LT
4088nfsd4_free_slabs(void)
4089{
9258a2d5 4090 kmem_cache_destroy(client_slab);
abf1135b
CH
4091 kmem_cache_destroy(openowner_slab);
4092 kmem_cache_destroy(lockowner_slab);
4093 kmem_cache_destroy(file_slab);
4094 kmem_cache_destroy(stateid_slab);
4095 kmem_cache_destroy(deleg_slab);
9258a2d5 4096 kmem_cache_destroy(odstate_slab);
e60d4398 4097}
1da177e4 4098
72083396 4099int
e60d4398
N
4100nfsd4_init_slabs(void)
4101{
9258a2d5
JL
4102 client_slab = kmem_cache_create("nfsd4_clients",
4103 sizeof(struct nfs4_client), 0, 0, NULL);
4104 if (client_slab == NULL)
4105 goto out;
fe0750e5
BF
4106 openowner_slab = kmem_cache_create("nfsd4_openowners",
4107 sizeof(struct nfs4_openowner), 0, 0, NULL);
4108 if (openowner_slab == NULL)
9258a2d5 4109 goto out_free_client_slab;
fe0750e5 4110 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3c40794b 4111 sizeof(struct nfs4_lockowner), 0, 0, NULL);
fe0750e5 4112 if (lockowner_slab == NULL)
abf1135b 4113 goto out_free_openowner_slab;
e60d4398 4114 file_slab = kmem_cache_create("nfsd4_files",
20c2df83 4115 sizeof(struct nfs4_file), 0, 0, NULL);
e60d4398 4116 if (file_slab == NULL)
abf1135b 4117 goto out_free_lockowner_slab;
5ac049ac 4118 stateid_slab = kmem_cache_create("nfsd4_stateids",
dcef0413 4119 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
5ac049ac 4120 if (stateid_slab == NULL)
abf1135b 4121 goto out_free_file_slab;
5b2d21c1 4122 deleg_slab = kmem_cache_create("nfsd4_delegations",
20c2df83 4123 sizeof(struct nfs4_delegation), 0, 0, NULL);
5b2d21c1 4124 if (deleg_slab == NULL)
abf1135b 4125 goto out_free_stateid_slab;
8287f009
SB
4126 odstate_slab = kmem_cache_create("nfsd4_odstate",
4127 sizeof(struct nfs4_clnt_odstate), 0, 0, NULL);
4128 if (odstate_slab == NULL)
4129 goto out_free_deleg_slab;
e60d4398 4130 return 0;
abf1135b 4131
8287f009
SB
4132out_free_deleg_slab:
4133 kmem_cache_destroy(deleg_slab);
abf1135b
CH
4134out_free_stateid_slab:
4135 kmem_cache_destroy(stateid_slab);
4136out_free_file_slab:
4137 kmem_cache_destroy(file_slab);
4138out_free_lockowner_slab:
4139 kmem_cache_destroy(lockowner_slab);
4140out_free_openowner_slab:
4141 kmem_cache_destroy(openowner_slab);
9258a2d5
JL
4142out_free_client_slab:
4143 kmem_cache_destroy(client_slab);
abf1135b 4144out:
e60d4398 4145 return -ENOMEM;
1da177e4
LT
4146}
4147
ff194bd9 4148static void init_nfs4_replay(struct nfs4_replay *rp)
1da177e4 4149{
ff194bd9
BF
4150 rp->rp_status = nfserr_serverfault;
4151 rp->rp_buflen = 0;
4152 rp->rp_buf = rp->rp_ibuf;
58fb12e6
JL
4153 mutex_init(&rp->rp_mutex);
4154}
4155
4156static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
4157 struct nfs4_stateowner *so)
4158{
4159 if (!nfsd4_has_session(cstate)) {
4160 mutex_lock(&so->so_replay.rp_mutex);
b5971afa 4161 cstate->replay_owner = nfs4_get_stateowner(so);
58fb12e6
JL
4162 }
4163}
4164
4165void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
4166{
4167 struct nfs4_stateowner *so = cstate->replay_owner;
4168
4169 if (so != NULL) {
4170 cstate->replay_owner = NULL;
4171 mutex_unlock(&so->so_replay.rp_mutex);
4172 nfs4_put_stateowner(so);
4173 }
1da177e4
LT
4174}
4175
fe0750e5 4176static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
ff194bd9 4177{
1da177e4 4178 struct nfs4_stateowner *sop;
1da177e4 4179
fe0750e5 4180 sop = kmem_cache_alloc(slab, GFP_KERNEL);
ff194bd9
BF
4181 if (!sop)
4182 return NULL;
4183
6f4859b8 4184 xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL);
ff194bd9 4185 if (!sop->so_owner.data) {
fe0750e5 4186 kmem_cache_free(slab, sop);
1da177e4 4187 return NULL;
ff194bd9 4188 }
ff194bd9 4189
ea1da636 4190 INIT_LIST_HEAD(&sop->so_stateids);
ff194bd9
BF
4191 sop->so_client = clp;
4192 init_nfs4_replay(&sop->so_replay);
6b180f0b 4193 atomic_set(&sop->so_count, 1);
ff194bd9
BF
4194 return sop;
4195}
4196
fe0750e5 4197static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
ff194bd9 4198{
d4f0489f 4199 lockdep_assert_held(&clp->cl_lock);
9b531137 4200
d4f0489f
TM
4201 list_add(&oo->oo_owner.so_strhash,
4202 &clp->cl_ownerstr_hashtbl[strhashval]);
fe0750e5 4203 list_add(&oo->oo_perclient, &clp->cl_openowners);
ff194bd9
BF
4204}
4205
8f4b54c5
JL
4206static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
4207{
d4f0489f 4208 unhash_openowner_locked(openowner(so));
8f4b54c5
JL
4209}
4210
6b180f0b
JL
4211static void nfs4_free_openowner(struct nfs4_stateowner *so)
4212{
4213 struct nfs4_openowner *oo = openowner(so);
4214
4215 kmem_cache_free(openowner_slab, oo);
4216}
4217
4218static const struct nfs4_stateowner_operations openowner_ops = {
8f4b54c5
JL
4219 .so_unhash = nfs4_unhash_openowner,
4220 .so_free = nfs4_free_openowner,
6b180f0b
JL
4221};
4222
7fc0564e
AE
4223static struct nfs4_ol_stateid *
4224nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4225{
4226 struct nfs4_ol_stateid *local, *ret = NULL;
4227 struct nfs4_openowner *oo = open->op_openowner;
4228
4229 lockdep_assert_held(&fp->fi_lock);
4230
4231 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
4232 /* ignore lock owners */
4233 if (local->st_stateowner->so_is_open_owner == 0)
4234 continue;
15ca08d3
TM
4235 if (local->st_stateowner != &oo->oo_owner)
4236 continue;
4237 if (local->st_stid.sc_type == NFS4_OPEN_STID) {
7fc0564e 4238 ret = local;
a15dfcd5 4239 refcount_inc(&ret->st_stid.sc_count);
7fc0564e
AE
4240 break;
4241 }
4242 }
4243 return ret;
4244}
4245
15ca08d3
TM
4246static __be32
4247nfsd4_verify_open_stid(struct nfs4_stid *s)
4248{
4249 __be32 ret = nfs_ok;
4250
4251 switch (s->sc_type) {
4252 default:
4253 break;
4f176417 4254 case 0:
15ca08d3
TM
4255 case NFS4_CLOSED_STID:
4256 case NFS4_CLOSED_DELEG_STID:
4257 ret = nfserr_bad_stateid;
4258 break;
4259 case NFS4_REVOKED_DELEG_STID:
4260 ret = nfserr_deleg_revoked;
4261 }
4262 return ret;
4263}
4264
4265/* Lock the stateid st_mutex, and deal with races with CLOSE */
4266static __be32
4267nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp)
4268{
4269 __be32 ret;
4270
4f34bd05 4271 mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX);
15ca08d3
TM
4272 ret = nfsd4_verify_open_stid(&stp->st_stid);
4273 if (ret != nfs_ok)
4274 mutex_unlock(&stp->st_mutex);
4275 return ret;
4276}
4277
4278static struct nfs4_ol_stateid *
4279nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
4280{
4281 struct nfs4_ol_stateid *stp;
4282 for (;;) {
4283 spin_lock(&fp->fi_lock);
4284 stp = nfsd4_find_existing_open(fp, open);
4285 spin_unlock(&fp->fi_lock);
4286 if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok)
4287 break;
4288 nfs4_put_stid(&stp->st_stid);
4289 }
4290 return stp;
4291}
4292
fe0750e5 4293static struct nfs4_openowner *
13d6f66b 4294alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
db24b3b4
JL
4295 struct nfsd4_compound_state *cstate)
4296{
13d6f66b 4297 struct nfs4_client *clp = cstate->clp;
7ffb5880 4298 struct nfs4_openowner *oo, *ret;
ff194bd9 4299
fe0750e5
BF
4300 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
4301 if (!oo)
ff194bd9 4302 return NULL;
6b180f0b 4303 oo->oo_owner.so_ops = &openowner_ops;
fe0750e5
BF
4304 oo->oo_owner.so_is_open_owner = 1;
4305 oo->oo_owner.so_seqid = open->op_seqid;
d3134b10 4306 oo->oo_flags = 0;
db24b3b4
JL
4307 if (nfsd4_has_session(cstate))
4308 oo->oo_flags |= NFS4_OO_CONFIRMED;
fe0750e5 4309 oo->oo_time = 0;
38c387b5 4310 oo->oo_last_closed_stid = NULL;
fe0750e5 4311 INIT_LIST_HEAD(&oo->oo_close_lru);
d4f0489f
TM
4312 spin_lock(&clp->cl_lock);
4313 ret = find_openstateowner_str_locked(strhashval, open, clp);
7ffb5880
TM
4314 if (ret == NULL) {
4315 hash_openowner(oo, clp, strhashval);
4316 ret = oo;
4317 } else
d50ffded
KM
4318 nfs4_free_stateowner(&oo->oo_owner);
4319
d4f0489f 4320 spin_unlock(&clp->cl_lock);
c5952338 4321 return ret;
1da177e4
LT
4322}
4323
7fc0564e 4324static struct nfs4_ol_stateid *
8c7245ab 4325init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open)
7fc0564e
AE
4326{
4327
fe0750e5 4328 struct nfs4_openowner *oo = open->op_openowner;
7fc0564e 4329 struct nfs4_ol_stateid *retstp = NULL;
8c7245ab 4330 struct nfs4_ol_stateid *stp;
1da177e4 4331
8c7245ab 4332 stp = open->op_stp;
5cc1fb2a
OD
4333 /* We are moving these outside of the spinlocks to avoid the warnings */
4334 mutex_init(&stp->st_mutex);
4f34bd05 4335 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
5cc1fb2a 4336
15ca08d3 4337retry:
7fc0564e
AE
4338 spin_lock(&oo->oo_owner.so_client->cl_lock);
4339 spin_lock(&fp->fi_lock);
4340
4341 retstp = nfsd4_find_existing_open(fp, open);
4342 if (retstp)
4343 goto out_unlock;
8c7245ab
OD
4344
4345 open->op_stp = NULL;
a15dfcd5 4346 refcount_inc(&stp->st_stid.sc_count);
3abdb607 4347 stp->st_stid.sc_type = NFS4_OPEN_STID;
3c87b9b7 4348 INIT_LIST_HEAD(&stp->st_locks);
b5971afa 4349 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
13cd2184 4350 get_nfs4_file(fp);
11b9164a 4351 stp->st_stid.sc_file = fp;
1da177e4
LT
4352 stp->st_access_bmap = 0;
4353 stp->st_deny_bmap = 0;
4c4cd222 4354 stp->st_openstp = NULL;
1c755dc1 4355 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
1d31a253 4356 list_add(&stp->st_perfile, &fp->fi_stateids);
7fc0564e
AE
4357
4358out_unlock:
1d31a253 4359 spin_unlock(&fp->fi_lock);
1c755dc1 4360 spin_unlock(&oo->oo_owner.so_client->cl_lock);
5cc1fb2a 4361 if (retstp) {
15ca08d3
TM
4362 /* Handle races with CLOSE */
4363 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
4364 nfs4_put_stid(&retstp->st_stid);
4365 goto retry;
4366 }
8c7245ab 4367 /* To keep mutex tracking happy */
5cc1fb2a 4368 mutex_unlock(&stp->st_mutex);
8c7245ab 4369 stp = retstp;
5cc1fb2a 4370 }
8c7245ab 4371 return stp;
1da177e4
LT
4372}
4373
d3134b10
JL
4374/*
4375 * In the 4.0 case we need to keep the owners around a little while to handle
4376 * CLOSE replay. We still do need to release any file access that is held by
4377 * them before returning however.
4378 */
fd39ca9a 4379static void
d3134b10 4380move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
1da177e4 4381{
217526e7 4382 struct nfs4_ol_stateid *last;
d3134b10
JL
4383 struct nfs4_openowner *oo = openowner(s->st_stateowner);
4384 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
4385 nfsd_net_id);
73758fed 4386
fe0750e5 4387 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
1da177e4 4388
b401be22
JL
4389 /*
4390 * We know that we hold one reference via nfsd4_close, and another
4391 * "persistent" reference for the client. If the refcount is higher
4392 * than 2, then there are still calls in progress that are using this
4393 * stateid. We can't put the sc_file reference until they are finished.
4394 * Wait for the refcount to drop to 2. Since it has been unhashed,
4395 * there should be no danger of the refcount going back up again at
4396 * this point.
4397 */
a15dfcd5 4398 wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2);
b401be22 4399
d3134b10
JL
4400 release_all_access(s);
4401 if (s->st_stid.sc_file) {
4402 put_nfs4_file(s->st_stid.sc_file);
4403 s->st_stid.sc_file = NULL;
4404 }
217526e7
JL
4405
4406 spin_lock(&nn->client_lock);
4407 last = oo->oo_last_closed_stid;
d3134b10 4408 oo->oo_last_closed_stid = s;
73758fed 4409 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
20b7d86f 4410 oo->oo_time = ktime_get_boottime_seconds();
217526e7
JL
4411 spin_unlock(&nn->client_lock);
4412 if (last)
4413 nfs4_put_stid(&last->st_stid);
1da177e4
LT
4414}
4415
1da177e4
LT
4416/* search file_hashtbl[] for file */
4417static struct nfs4_file *
5b095e99 4418find_file_locked(struct knfsd_fh *fh, unsigned int hashval)
1da177e4 4419{
1da177e4
LT
4420 struct nfs4_file *fp;
4421
36a80491
MB
4422 hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash,
4423 lockdep_is_held(&state_lock)) {
4d94c2ef 4424 if (fh_match(&fp->fi_fhandle, fh)) {
818a34eb 4425 if (refcount_inc_not_zero(&fp->fi_ref))
5b095e99 4426 return fp;
13cd2184 4427 }
1da177e4
LT
4428 }
4429 return NULL;
4430}
4431
e6ba76e1 4432struct nfs4_file *
ca943217 4433find_file(struct knfsd_fh *fh)
950e0118
TM
4434{
4435 struct nfs4_file *fp;
5b095e99 4436 unsigned int hashval = file_hashval(fh);
950e0118 4437
5b095e99
JL
4438 rcu_read_lock();
4439 fp = find_file_locked(fh, hashval);
4440 rcu_read_unlock();
950e0118
TM
4441 return fp;
4442}
4443
4444static struct nfs4_file *
f9c00c3a 4445find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
950e0118
TM
4446{
4447 struct nfs4_file *fp;
5b095e99
JL
4448 unsigned int hashval = file_hashval(fh);
4449
4450 rcu_read_lock();
4451 fp = find_file_locked(fh, hashval);
4452 rcu_read_unlock();
4453 if (fp)
4454 return fp;
950e0118
TM
4455
4456 spin_lock(&state_lock);
5b095e99
JL
4457 fp = find_file_locked(fh, hashval);
4458 if (likely(fp == NULL)) {
4459 nfsd4_init_file(fh, hashval, new);
950e0118
TM
4460 fp = new;
4461 }
4462 spin_unlock(&state_lock);
4463
4464 return fp;
4465}
4466
1da177e4
LT
4467/*
4468 * Called to check deny when READ with all zero stateid or
4469 * WRITE with all zero or all one stateid
4470 */
b37ad28b 4471static __be32
1da177e4
LT
4472nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
4473{
1da177e4 4474 struct nfs4_file *fp;
baeb4ff0 4475 __be32 ret = nfs_ok;
1da177e4 4476
ca943217 4477 fp = find_file(&current_fh->fh_handle);
13cd2184 4478 if (!fp)
baeb4ff0
JL
4479 return ret;
4480 /* Check for conflicting share reservations */
1d31a253 4481 spin_lock(&fp->fi_lock);
baeb4ff0
JL
4482 if (fp->fi_share_deny & deny_type)
4483 ret = nfserr_locked;
1d31a253 4484 spin_unlock(&fp->fi_lock);
13cd2184
N
4485 put_nfs4_file(fp);
4486 return ret;
1da177e4
LT
4487}
4488
0162ac2b 4489static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
1da177e4 4490{
0162ac2b 4491 struct nfs4_delegation *dp = cb_to_delegation(cb);
11b9164a
TM
4492 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
4493 nfsd_net_id);
e8c69d17 4494
11b9164a 4495 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
f54fe962 4496
dff1399f 4497 /*
f54fe962
JL
4498 * We can't do this in nfsd_break_deleg_cb because it is
4499 * already holding inode->i_lock.
4500 *
dff1399f
JL
4501 * If the dl_time != 0, then we know that it has already been
4502 * queued for a lease break. Don't queue it again.
4503 */
f54fe962 4504 spin_lock(&state_lock);
dff1399f 4505 if (dp->dl_time == 0) {
20b7d86f 4506 dp->dl_time = ktime_get_boottime_seconds();
02e1215f 4507 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
dff1399f 4508 }
02e1215f
JL
4509 spin_unlock(&state_lock);
4510}
1da177e4 4511
0162ac2b
CH
4512static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
4513 struct rpc_task *task)
4514{
4515 struct nfs4_delegation *dp = cb_to_delegation(cb);
4516
12ed22f3
BF
4517 if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID ||
4518 dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID)
a457974f
AE
4519 return 1;
4520
0162ac2b
CH
4521 switch (task->tk_status) {
4522 case 0:
4523 return 1;
1c73b9d2
SM
4524 case -NFS4ERR_DELAY:
4525 rpc_delay(task, 2 * HZ);
4526 return 0;
0162ac2b
CH
4527 case -EBADHANDLE:
4528 case -NFS4ERR_BAD_STATEID:
4529 /*
4530 * Race: client probably got cb_recall before open reply
4531 * granting delegation.
4532 */
4533 if (dp->dl_retries--) {
4534 rpc_delay(task, 2 * HZ);
4535 return 0;
4536 }
df561f66 4537 fallthrough;
0162ac2b 4538 default:
1c73b9d2 4539 return 1;
0162ac2b
CH
4540 }
4541}
4542
4543static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
4544{
4545 struct nfs4_delegation *dp = cb_to_delegation(cb);
4546
4547 nfs4_put_stid(&dp->dl_stid);
4548}
4549
c4cb8974 4550static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
0162ac2b
CH
4551 .prepare = nfsd4_cb_recall_prepare,
4552 .done = nfsd4_cb_recall_done,
4553 .release = nfsd4_cb_recall_release,
4554};
4555
02e1215f
JL
4556static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
4557{
4558 /*
4559 * We're assuming the state code never drops its reference
4560 * without first removing the lease. Since we're in this lease
4a269efb
BF
4561 * callback (and since the lease code is serialized by the
4562 * i_lock) we know the server hasn't removed the lease yet, and
4563 * we know it's safe to take a reference.
02e1215f 4564 */
a15dfcd5 4565 refcount_inc(&dp->dl_stid.sc_count);
f0b5de1b 4566 nfsd4_run_cb(&dp->dl_recall);
6b57d9c8
BF
4567}
4568
1c8c601a 4569/* Called from break_lease() with i_lock held. */
4d01b7f5
JL
4570static bool
4571nfsd_break_deleg_cb(struct file_lock *fl)
6b57d9c8 4572{
4d01b7f5 4573 bool ret = false;
653e514e
BF
4574 struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner;
4575 struct nfs4_file *fp = dp->dl_stid.sc_file;
6b57d9c8 4576
dd5e3fbc
CL
4577 trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid);
4578
0272e1fd
BF
4579 /*
4580 * We don't want the locks code to timeout the lease for us;
acfdf5c3 4581 * we'll remove it ourself if a delegation isn't returned
6b57d9c8 4582 * in time:
0272e1fd
BF
4583 */
4584 fl->fl_break_time = 0;
1da177e4 4585
02e1215f 4586 spin_lock(&fp->fi_lock);
417c6629 4587 fp->fi_had_conflict = true;
353601e7 4588 nfsd_break_one_deleg(dp);
02e1215f 4589 spin_unlock(&fp->fi_lock);
4d01b7f5 4590 return ret;
1da177e4
LT
4591}
4592
28df3d15
BF
4593static bool nfsd_breaker_owns_lease(struct file_lock *fl)
4594{
4595 struct nfs4_delegation *dl = fl->fl_owner;
4596 struct svc_rqst *rqst;
4597 struct nfs4_client *clp;
4598
4599 if (!i_am_nfsd())
4600 return NULL;
4601 rqst = kthread_data(current);
13956160
BF
4602 /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */
4603 if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4)
34b09af4 4604 return NULL;
28df3d15
BF
4605 clp = *(rqst->rq_lease_breaker);
4606 return dl->dl_stid.sc_client == clp;
4607}
4608
c45198ed 4609static int
7448cc37
JL
4610nfsd_change_deleg_cb(struct file_lock *onlist, int arg,
4611 struct list_head *dispose)
1da177e4
LT
4612{
4613 if (arg & F_UNLCK)
c45198ed 4614 return lease_modify(onlist, arg, dispose);
1da177e4
LT
4615 else
4616 return -EAGAIN;
4617}
4618
7b021967 4619static const struct lock_manager_operations nfsd_lease_mng_ops = {
28df3d15 4620 .lm_breaker_owns_lease = nfsd_breaker_owns_lease,
8fb47a4f
BF
4621 .lm_break = nfsd_break_deleg_cb,
4622 .lm_change = nfsd_change_deleg_cb,
1da177e4
LT
4623};
4624
7a8711c9
BF
4625static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
4626{
4627 if (nfsd4_has_session(cstate))
4628 return nfs_ok;
4629 if (seqid == so->so_seqid - 1)
4630 return nfserr_replay_me;
4631 if (seqid == so->so_seqid)
4632 return nfs_ok;
4633 return nfserr_bad_seqid;
4634}
1da177e4 4635
4b24ca7d
JL
4636static __be32 lookup_clientid(clientid_t *clid,
4637 struct nfsd4_compound_state *cstate,
b7342204
OK
4638 struct nfsd_net *nn,
4639 bool sessions)
4b24ca7d
JL
4640{
4641 struct nfs4_client *found;
4642
4643 if (cstate->clp) {
4644 found = cstate->clp;
4645 if (!same_clid(&found->cl_clientid, clid))
4646 return nfserr_stale_clientid;
4647 return nfs_ok;
4648 }
4649
4650 if (STALE_CLIENTID(clid, nn))
4651 return nfserr_stale_clientid;
4652
4653 /*
4654 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4655 * cached already then we know this is for is for v4.0 and "sessions"
4656 * will be false.
4657 */
4658 WARN_ON_ONCE(cstate->session);
3e339f96 4659 spin_lock(&nn->client_lock);
b7342204 4660 found = find_confirmed_client(clid, sessions, nn);
3e339f96
TM
4661 if (!found) {
4662 spin_unlock(&nn->client_lock);
4b24ca7d 4663 return nfserr_expired;
3e339f96 4664 }
14ed14cc 4665 atomic_inc(&found->cl_rpc_users);
3e339f96 4666 spin_unlock(&nn->client_lock);
4b24ca7d
JL
4667
4668 /* Cache the nfs4_client in cstate! */
4669 cstate->clp = found;
4b24ca7d
JL
4670 return nfs_ok;
4671}
4672
b37ad28b 4673__be32
6668958f 4674nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3320fef1 4675 struct nfsd4_open *open, struct nfsd_net *nn)
1da177e4 4676{
1da177e4
LT
4677 clientid_t *clientid = &open->op_clientid;
4678 struct nfs4_client *clp = NULL;
4679 unsigned int strhashval;
fe0750e5 4680 struct nfs4_openowner *oo = NULL;
4cdc951b 4681 __be32 status;
1da177e4 4682
2c142baa 4683 if (STALE_CLIENTID(&open->op_clientid, nn))
1da177e4 4684 return nfserr_stale_clientid;
32513b40
BF
4685 /*
4686 * In case we need it later, after we've already created the
4687 * file and don't want to risk a further failure:
4688 */
4689 open->op_file = nfsd4_alloc_file();
4690 if (open->op_file == NULL)
4691 return nfserr_jukebox;
1da177e4 4692
b7342204 4693 status = lookup_clientid(clientid, cstate, nn, false);
2d91e895
TM
4694 if (status)
4695 return status;
4696 clp = cstate->clp;
4697
d4f0489f
TM
4698 strhashval = ownerstr_hashval(&open->op_owner);
4699 oo = find_openstateowner_str(strhashval, open, clp);
fe0750e5
BF
4700 open->op_openowner = oo;
4701 if (!oo) {
bcf130f9 4702 goto new_owner;
1da177e4 4703 }
dad1c067 4704 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
0f442aa2 4705 /* Replace unconfirmed owners without checking for replay. */
fe0750e5
BF
4706 release_openowner(oo);
4707 open->op_openowner = NULL;
bcf130f9 4708 goto new_owner;
0f442aa2 4709 }
4cdc951b
BF
4710 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
4711 if (status)
4712 return status;
4cdc951b 4713 goto alloc_stateid;
bcf130f9 4714new_owner:
13d6f66b 4715 oo = alloc_init_open_stateowner(strhashval, open, cstate);
bcf130f9
BF
4716 if (oo == NULL)
4717 return nfserr_jukebox;
4718 open->op_openowner = oo;
4cdc951b 4719alloc_stateid:
b49e084d 4720 open->op_stp = nfs4_alloc_open_stateid(clp);
4cdc951b
BF
4721 if (!open->op_stp)
4722 return nfserr_jukebox;
8287f009
SB
4723
4724 if (nfsd4_has_session(cstate) &&
4725 (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) {
4726 open->op_odstate = alloc_clnt_odstate(clp);
4727 if (!open->op_odstate)
4728 return nfserr_jukebox;
4729 }
4730
0f442aa2 4731 return nfs_ok;
1da177e4
LT
4732}
4733
b37ad28b 4734static inline __be32
4a6e43e6
N
4735nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
4736{
4737 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
4738 return nfserr_openmode;
4739 else
4740 return nfs_ok;
4741}
4742
f459e453 4743static int share_access_to_flags(u32 share_access)
52f4fb43 4744{
f459e453 4745 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
52f4fb43
N
4746}
4747
38c2f4b1 4748static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
24a0111e 4749{
f459e453 4750 struct nfs4_stid *ret;
24a0111e 4751
95da1b3a
AE
4752 ret = find_stateid_by_type(cl, s,
4753 NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID);
f459e453
BF
4754 if (!ret)
4755 return NULL;
4756 return delegstateid(ret);
24a0111e
BF
4757}
4758
8b289b2c
BF
4759static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
4760{
4761 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
4762 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
4763}
4764
b37ad28b 4765static __be32
41d22663 4766nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
567d9829
N
4767 struct nfs4_delegation **dp)
4768{
4769 int flags;
b37ad28b 4770 __be32 status = nfserr_bad_stateid;
dcd94cc2 4771 struct nfs4_delegation *deleg;
567d9829 4772
dcd94cc2
TM
4773 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
4774 if (deleg == NULL)
c44c5eeb 4775 goto out;
95da1b3a
AE
4776 if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) {
4777 nfs4_put_stid(&deleg->dl_stid);
4778 if (cl->cl_minorversion)
4779 status = nfserr_deleg_revoked;
4780 goto out;
4781 }
24a0111e 4782 flags = share_access_to_flags(open->op_share_access);
dcd94cc2
TM
4783 status = nfs4_check_delegmode(deleg, flags);
4784 if (status) {
4785 nfs4_put_stid(&deleg->dl_stid);
4786 goto out;
4787 }
4788 *dp = deleg;
c44c5eeb 4789out:
8b289b2c 4790 if (!nfsd4_is_deleg_cur(open))
c44c5eeb
N
4791 return nfs_ok;
4792 if (status)
4793 return status;
dad1c067 4794 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
c44c5eeb 4795 return nfs_ok;
567d9829
N
4796}
4797
21fb4016
BF
4798static inline int nfs4_access_to_access(u32 nfs4_access)
4799{
4800 int flags = 0;
4801
4802 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
4803 flags |= NFSD_MAY_READ;
4804 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
4805 flags |= NFSD_MAY_WRITE;
4806 return flags;
4807}
4808
7e6a72e5
CH
4809static inline __be32
4810nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
4811 struct nfsd4_open *open)
4812{
4813 struct iattr iattr = {
4814 .ia_valid = ATTR_SIZE,
4815 .ia_size = 0,
4816 };
4817 if (!open->op_truncate)
4818 return 0;
4819 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
4820 return nfserr_inval;
2a1aa489 4821 return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0);
7e6a72e5
CH
4822}
4823
0c12eaff 4824static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
6eb3a1d0
JL
4825 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
4826 struct nfsd4_open *open)
f9d7562f 4827{
fd4f83fd 4828 struct nfsd_file *nf = NULL;
f9d7562f 4829 __be32 status;
0c12eaff
CB
4830 int oflag = nfs4_access_to_omode(open->op_share_access);
4831 int access = nfs4_access_to_access(open->op_share_access);
baeb4ff0 4832 unsigned char old_access_bmap, old_deny_bmap;
0c12eaff 4833
de18643d 4834 spin_lock(&fp->fi_lock);
baeb4ff0
JL
4835
4836 /*
4837 * Are we trying to set a deny mode that would conflict with
4838 * current access?
4839 */
4840 status = nfs4_file_check_deny(fp, open->op_share_deny);
4841 if (status != nfs_ok) {
4842 spin_unlock(&fp->fi_lock);
4843 goto out;
4844 }
4845
4846 /* set access to the file */
4847 status = nfs4_file_get_access(fp, open->op_share_access);
4848 if (status != nfs_ok) {
4849 spin_unlock(&fp->fi_lock);
4850 goto out;
4851 }
4852
4853 /* Set access bits in stateid */
4854 old_access_bmap = stp->st_access_bmap;
4855 set_access(open->op_share_access, stp);
4856
4857 /* Set new deny mask */
4858 old_deny_bmap = stp->st_deny_bmap;
4859 set_deny(open->op_share_deny, stp);
4860 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4861
f9d7562f 4862 if (!fp->fi_fds[oflag]) {
de18643d 4863 spin_unlock(&fp->fi_lock);
fd4f83fd 4864 status = nfsd_file_acquire(rqstp, cur_fh, access, &nf);
f9d7562f 4865 if (status)
baeb4ff0 4866 goto out_put_access;
de18643d
TM
4867 spin_lock(&fp->fi_lock);
4868 if (!fp->fi_fds[oflag]) {
fd4f83fd
JL
4869 fp->fi_fds[oflag] = nf;
4870 nf = NULL;
de18643d 4871 }
f9d7562f 4872 }
de18643d 4873 spin_unlock(&fp->fi_lock);
fd4f83fd
JL
4874 if (nf)
4875 nfsd_file_put(nf);
f9d7562f 4876
7e6a72e5
CH
4877 status = nfsd4_truncate(rqstp, cur_fh, open);
4878 if (status)
4879 goto out_put_access;
7e6a72e5
CH
4880out:
4881 return status;
baeb4ff0
JL
4882out_put_access:
4883 stp->st_access_bmap = old_access_bmap;
4884 nfs4_file_put_access(fp, open->op_share_access);
4885 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
4886 goto out;
1da177e4
LT
4887}
4888
b37ad28b 4889static __be32
dcef0413 4890nfs4_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 4891{
b37ad28b 4892 __be32 status;
6ac75368 4893 unsigned char old_deny_bmap = stp->st_deny_bmap;
1da177e4 4894
6eb3a1d0 4895 if (!test_access(open->op_share_access, stp))
baeb4ff0 4896 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
7e6a72e5 4897
baeb4ff0
JL
4898 /* test and set deny mode */
4899 spin_lock(&fp->fi_lock);
4900 status = nfs4_file_check_deny(fp, open->op_share_deny);
4901 if (status == nfs_ok) {
baeb4ff0
JL
4902 set_deny(open->op_share_deny, stp);
4903 fp->fi_share_deny |=
4904 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
4905 }
4906 spin_unlock(&fp->fi_lock);
4907
4908 if (status != nfs_ok)
1da177e4 4909 return status;
1da177e4 4910
baeb4ff0
JL
4911 status = nfsd4_truncate(rqstp, cur_fh, open);
4912 if (status != nfs_ok)
4913 reset_union_bmap_deny(old_deny_bmap, stp);
4914 return status;
4915}
1da177e4 4916
14a24e99
BF
4917/* Should we give out recallable state?: */
4918static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
4919{
4920 if (clp->cl_cb_state == NFSD4_CB_UP)
4921 return true;
4922 /*
4923 * In the sessions case, since we don't have to establish a
4924 * separate connection for callbacks, we assume it's OK
4925 * until we hear otherwise:
4926 */
4927 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
4928}
4929
653e514e
BF
4930static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp,
4931 int flag)
22d38c4c
BF
4932{
4933 struct file_lock *fl;
4934
4935 fl = locks_alloc_lock();
4936 if (!fl)
4937 return NULL;
22d38c4c 4938 fl->fl_lmops = &nfsd_lease_mng_ops;
617588d5 4939 fl->fl_flags = FL_DELEG;
22d38c4c
BF
4940 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
4941 fl->fl_end = OFFSET_MAX;
653e514e 4942 fl->fl_owner = (fl_owner_t)dp;
22d38c4c 4943 fl->fl_pid = current->tgid;
eb82dd39 4944 fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file;
22d38c4c
BF
4945 return fl;
4946}
4947
94415b06
BF
4948static int nfsd4_check_conflicting_opens(struct nfs4_client *clp,
4949 struct nfs4_file *fp)
4950{
4951 struct nfs4_clnt_odstate *co;
4952 struct file *f = fp->fi_deleg_file->nf_file;
4953 struct inode *ino = locks_inode(f);
4954 int writes = atomic_read(&ino->i_writecount);
4955
4956 if (fp->fi_fds[O_WRONLY])
4957 writes--;
4958 if (fp->fi_fds[O_RDWR])
4959 writes--;
94415b06
BF
4960 if (writes > 0)
4961 return -EAGAIN;
4962 spin_lock(&fp->fi_lock);
4963 list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) {
4964 if (co->co_client != clp) {
4965 spin_unlock(&fp->fi_lock);
4966 return -EAGAIN;
4967 }
4968 }
4969 spin_unlock(&fp->fi_lock);
4970 return 0;
4971}
4972
0b26693c
JL
4973static struct nfs4_delegation *
4974nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
8287f009 4975 struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate)
acfdf5c3 4976{
68b18f52 4977 int status = 0;
0b26693c 4978 struct nfs4_delegation *dp;
eb82dd39 4979 struct nfsd_file *nf;
353601e7 4980 struct file_lock *fl;
417c6629 4981
353601e7
BF
4982 /*
4983 * The fi_had_conflict and nfs_get_existing_delegation checks
4984 * here are just optimizations; we'll need to recheck them at
4985 * the end:
4986 */
bf7bd3e9 4987 if (fp->fi_had_conflict)
0b26693c
JL
4988 return ERR_PTR(-EAGAIN);
4989
eb82dd39
JL
4990 nf = find_readable_file(fp);
4991 if (!nf) {
94415b06
BF
4992 /*
4993 * We probably could attempt another open and get a read
4994 * delegation, but for now, don't bother until the
4995 * client actually sends us one.
4996 */
4997 return ERR_PTR(-EAGAIN);
353601e7 4998 }
34ed9872
AE
4999 spin_lock(&state_lock);
5000 spin_lock(&fp->fi_lock);
68b18f52
BF
5001 if (nfs4_delegation_exists(clp, fp))
5002 status = -EAGAIN;
353601e7 5003 else if (!fp->fi_deleg_file) {
eb82dd39 5004 fp->fi_deleg_file = nf;
353601e7
BF
5005 /* increment early to prevent fi_deleg_file from being
5006 * cleared */
5007 fp->fi_delegees = 1;
eb82dd39 5008 nf = NULL;
353601e7
BF
5009 } else
5010 fp->fi_delegees++;
34ed9872
AE
5011 spin_unlock(&fp->fi_lock);
5012 spin_unlock(&state_lock);
eb82dd39
JL
5013 if (nf)
5014 nfsd_file_put(nf);
34ed9872
AE
5015 if (status)
5016 return ERR_PTR(status);
5017
353601e7 5018 status = -ENOMEM;
86d29b10 5019 dp = alloc_init_deleg(clp, fp, fh, odstate);
0b26693c 5020 if (!dp)
353601e7
BF
5021 goto out_delegees;
5022
5023 fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ);
5024 if (!fl)
bd8d7250 5025 goto out_clnt_odstate;
353601e7 5026
94415b06
BF
5027 status = nfsd4_check_conflicting_opens(clp, fp);
5028 if (status) {
5029 locks_free_lock(fl);
5030 goto out_clnt_odstate;
5031 }
eb82dd39 5032 status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL);
353601e7
BF
5033 if (fl)
5034 locks_free_lock(fl);
5035 if (status)
5036 goto out_clnt_odstate;
94415b06
BF
5037 status = nfsd4_check_conflicting_opens(clp, fp);
5038 if (status)
5039 goto out_clnt_odstate;
0b26693c 5040
417c6629
JL
5041 spin_lock(&state_lock);
5042 spin_lock(&fp->fi_lock);
353601e7 5043 if (fp->fi_had_conflict)
417c6629 5044 status = -EAGAIN;
353601e7
BF
5045 else
5046 status = hash_delegation_locked(dp, fp);
417c6629 5047 spin_unlock(&fp->fi_lock);
cdc97505 5048 spin_unlock(&state_lock);
353601e7
BF
5049
5050 if (status)
692ad280
AE
5051 goto out_unlock;
5052
0b26693c 5053 return dp;
692ad280 5054out_unlock:
eb82dd39 5055 vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp);
353601e7
BF
5056out_clnt_odstate:
5057 put_clnt_odstate(dp->dl_clnt_odstate);
353601e7
BF
5058 nfs4_put_stid(&dp->dl_stid);
5059out_delegees:
5060 put_deleg_file(fp);
5061 return ERR_PTR(status);
edab9782
BF
5062}
5063
4aa8913c
BH
5064static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
5065{
5066 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5067 if (status == -EAGAIN)
5068 open->op_why_no_deleg = WND4_CONTENTION;
5069 else {
5070 open->op_why_no_deleg = WND4_RESOURCE;
5071 switch (open->op_deleg_want) {
5072 case NFS4_SHARE_WANT_READ_DELEG:
5073 case NFS4_SHARE_WANT_WRITE_DELEG:
5074 case NFS4_SHARE_WANT_ANY_DELEG:
5075 break;
5076 case NFS4_SHARE_WANT_CANCEL:
5077 open->op_why_no_deleg = WND4_CANCELLED;
5078 break;
5079 case NFS4_SHARE_WANT_NO_DELEG:
063b0fb9 5080 WARN_ON_ONCE(1);
4aa8913c
BH
5081 }
5082 }
5083}
5084
1da177e4
LT
5085/*
5086 * Attempt to hand out a delegation.
99c41515
BF
5087 *
5088 * Note we don't support write delegations, and won't until the vfs has
5089 * proper support for them.
1da177e4
LT
5090 */
5091static void
4cf59221
JL
5092nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
5093 struct nfs4_ol_stateid *stp)
1da177e4
LT
5094{
5095 struct nfs4_delegation *dp;
4cf59221
JL
5096 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
5097 struct nfs4_client *clp = stp->st_stid.sc_client;
14a24e99 5098 int cb_up;
99c41515 5099 int status = 0;
1da177e4 5100
fe0750e5 5101 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
7b190fec
N
5102 open->op_recall = 0;
5103 switch (open->op_claim_type) {
5104 case NFS4_OPEN_CLAIM_PREVIOUS:
2bf23875 5105 if (!cb_up)
7b190fec 5106 open->op_recall = 1;
99c41515
BF
5107 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
5108 goto out_no_deleg;
7b190fec
N
5109 break;
5110 case NFS4_OPEN_CLAIM_NULL:
ed47b062 5111 case NFS4_OPEN_CLAIM_FH:
99c41515
BF
5112 /*
5113 * Let's not give out any delegations till everyone's
c87fb4a3
BF
5114 * had the chance to reclaim theirs, *and* until
5115 * NLM locks have all been reclaimed:
99c41515 5116 */
4cf59221 5117 if (locks_in_grace(clp->net))
99c41515 5118 goto out_no_deleg;
dad1c067 5119 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
99c41515 5120 goto out_no_deleg;
7b190fec
N
5121 break;
5122 default:
99c41515 5123 goto out_no_deleg;
7b190fec 5124 }
8287f009 5125 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate);
0b26693c 5126 if (IS_ERR(dp))
dd239cc0 5127 goto out_no_deleg;
1da177e4 5128
d5477a8d 5129 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
1da177e4 5130
3caf9175 5131 trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid);
99c41515 5132 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
67cb1279 5133 nfs4_put_stid(&dp->dl_stid);
dd239cc0 5134 return;
dd239cc0 5135out_no_deleg:
99c41515
BF
5136 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
5137 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
d08d32e6 5138 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
99c41515 5139 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
d08d32e6
BF
5140 open->op_recall = 1;
5141 }
99c41515
BF
5142
5143 /* 4.1 client asking for a delegation? */
5144 if (open->op_deleg_want)
5145 nfsd4_open_deleg_none_ext(open, status);
5146 return;
1da177e4
LT
5147}
5148
e27f49c3
BH
5149static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
5150 struct nfs4_delegation *dp)
5151{
5152 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
5153 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5154 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5155 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
5156 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
5157 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
5158 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5159 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
5160 }
5161 /* Otherwise the client must be confused wanting a delegation
5162 * it already has, therefore we don't return
5163 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5164 */
5165}
5166
b37ad28b 5167__be32
1da177e4
LT
5168nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
5169{
6668958f 5170 struct nfsd4_compoundres *resp = rqstp->rq_resp;
38c2f4b1 5171 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
1da177e4 5172 struct nfs4_file *fp = NULL;
dcef0413 5173 struct nfs4_ol_stateid *stp = NULL;
567d9829 5174 struct nfs4_delegation *dp = NULL;
b37ad28b 5175 __be32 status;
d8a1a000 5176 bool new_stp = false;
1da177e4 5177
1da177e4
LT
5178 /*
5179 * Lookup file; if found, lookup stateid and check open request,
5180 * and check for delegations in the process of being recalled.
5181 * If not found, create the nfs4_file struct
5182 */
f9c00c3a 5183 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
950e0118 5184 if (fp != open->op_file) {
41d22663 5185 status = nfs4_check_deleg(cl, open, &dp);
c44c5eeb
N
5186 if (status)
5187 goto out;
15ca08d3 5188 stp = nfsd4_find_and_lock_existing_open(fp, open);
1da177e4 5189 } else {
950e0118 5190 open->op_file = NULL;
c44c5eeb 5191 status = nfserr_bad_stateid;
8b289b2c 5192 if (nfsd4_is_deleg_cur(open))
c44c5eeb 5193 goto out;
1da177e4
LT
5194 }
5195
d8a1a000
TM
5196 if (!stp) {
5197 stp = init_open_stateid(fp, open);
5198 if (!open->op_stp)
5199 new_stp = true;
5200 }
5201
1da177e4
LT
5202 /*
5203 * OPEN the file, or upgrade an existing OPEN.
5204 * If truncate fails, the OPEN fails.
d8a1a000
TM
5205 *
5206 * stp is already locked.
1da177e4 5207 */
d8a1a000 5208 if (!new_stp) {
1da177e4 5209 /* Stateid was found, this is an OPEN upgrade */
f9d7562f 5210 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
35a92fe8 5211 if (status) {
feb9dad5 5212 mutex_unlock(&stp->st_mutex);
1da177e4 5213 goto out;
35a92fe8 5214 }
1da177e4 5215 } else {
6eb3a1d0
JL
5216 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
5217 if (status) {
d8a1a000 5218 stp->st_stid.sc_type = NFS4_CLOSED_STID;
6eb3a1d0 5219 release_open_stateid(stp);
d8a1a000 5220 mutex_unlock(&stp->st_mutex);
6eb3a1d0
JL
5221 goto out;
5222 }
8287f009
SB
5223
5224 stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp,
5225 open->op_odstate);
5226 if (stp->st_clnt_odstate == open->op_odstate)
5227 open->op_odstate = NULL;
1da177e4 5228 }
d8a1a000 5229
9767feb2 5230 nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid);
feb9dad5 5231 mutex_unlock(&stp->st_mutex);
1da177e4 5232
d24433cd 5233 if (nfsd4_has_session(&resp->cstate)) {
d24433cd
BH
5234 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
5235 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
5236 open->op_why_no_deleg = WND4_NOT_WANTED;
5237 goto nodeleg;
5238 }
5239 }
5240
1da177e4
LT
5241 /*
5242 * Attempt to hand out a delegation. No error return, because the
5243 * OPEN succeeds even if we fail.
5244 */
4cf59221 5245 nfs4_open_delegation(current_fh, open, stp);
d24433cd 5246nodeleg:
1da177e4 5247 status = nfs_ok;
3caf9175 5248 trace_nfsd_open(&stp->st_stid.sc_stateid);
1da177e4 5249out:
d24433cd
BH
5250 /* 4.1 client trying to upgrade/downgrade delegation? */
5251 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
e27f49c3
BH
5252 open->op_deleg_want)
5253 nfsd4_deleg_xgrade_none_ext(open, dp);
d24433cd 5254
13cd2184
N
5255 if (fp)
5256 put_nfs4_file(fp);
37515177 5257 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
87186022 5258 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
1da177e4
LT
5259 /*
5260 * To finish the open response, we just need to set the rflags.
5261 */
5262 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
19e4c347
JL
5263 if (nfsd4_has_session(&resp->cstate))
5264 open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK;
5265 else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED))
1da177e4 5266 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
19e4c347 5267
dcd94cc2
TM
5268 if (dp)
5269 nfs4_put_stid(&dp->dl_stid);
d6f2bc5d
TM
5270 if (stp)
5271 nfs4_put_stid(&stp->st_stid);
1da177e4
LT
5272
5273 return status;
5274}
5275
58fb12e6 5276void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
42297899 5277 struct nfsd4_open *open)
d29b20cd
BF
5278{
5279 if (open->op_openowner) {
d3134b10
JL
5280 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
5281
5282 nfsd4_cstate_assign_replay(cstate, so);
5283 nfs4_put_stateowner(so);
d29b20cd 5284 }
32513b40 5285 if (open->op_file)
5b095e99 5286 kmem_cache_free(file_slab, open->op_file);
4cdc951b 5287 if (open->op_stp)
6011695d 5288 nfs4_put_stid(&open->op_stp->st_stid);
8287f009
SB
5289 if (open->op_odstate)
5290 kmem_cache_free(odstate_slab, open->op_odstate);
d29b20cd
BF
5291}
5292
b37ad28b 5293__be32
b591480b 5294nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 5295 union nfsd4_op_u *u)
1da177e4 5296{
eb69853d 5297 clientid_t *clid = &u->renew;
1da177e4 5298 struct nfs4_client *clp;
b37ad28b 5299 __be32 status;
7f2210fa 5300 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 5301
dd5e3fbc 5302 trace_nfsd_clid_renew(clid);
b7342204 5303 status = lookup_clientid(clid, cstate, nn, false);
9b2ef62b 5304 if (status)
1da177e4 5305 goto out;
4b24ca7d 5306 clp = cstate->clp;
1da177e4 5307 status = nfserr_cb_path_down;
ea1da636 5308 if (!list_empty(&clp->cl_delegations)
77a3569d 5309 && clp->cl_cb_state != NFSD4_CB_UP)
1da177e4
LT
5310 goto out;
5311 status = nfs_ok;
5312out:
1da177e4
LT
5313 return status;
5314}
5315
7f5ef2e9 5316void
12760c66 5317nfsd4_end_grace(struct nfsd_net *nn)
a76b4319 5318{
33dcc481 5319 /* do nothing if grace period already ended */
a51c84ed 5320 if (nn->grace_ended)
33dcc481
JL
5321 return;
5322
dd5e3fbc 5323 trace_nfsd_grace_complete(nn);
a51c84ed 5324 nn->grace_ended = true;
70b28235
BF
5325 /*
5326 * If the server goes down again right now, an NFSv4
5327 * client will still be allowed to reclaim after it comes back up,
5328 * even if it hasn't yet had a chance to reclaim state this time.
5329 *
5330 */
919b8049 5331 nfsd4_record_grace_done(nn);
70b28235
BF
5332 /*
5333 * At this point, NFSv4 clients can still reclaim. But if the
5334 * server crashes, any that have not yet reclaimed will be out
5335 * of luck on the next boot.
5336 *
5337 * (NFSv4.1+ clients are considered to have reclaimed once they
5338 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5339 * have reclaimed after their first OPEN.)
5340 */
5e1533c7 5341 locks_end_grace(&nn->nfsd4_manager);
70b28235
BF
5342 /*
5343 * At this point, and once lockd and/or any other containers
5344 * exit their grace period, further reclaims will fail and
5345 * regular locking can resume.
5346 */
a76b4319
N
5347}
5348
03f318ca
BF
5349/*
5350 * If we've waited a lease period but there are still clients trying to
5351 * reclaim, wait a little longer to give them a chance to finish.
5352 */
5353static bool clients_still_reclaiming(struct nfsd_net *nn)
5354{
20b7d86f
AB
5355 time64_t double_grace_period_end = nn->boot_time +
5356 2 * nn->nfsd4_lease;
03f318ca 5357
362063a5
SM
5358 if (nn->track_reclaim_completes &&
5359 atomic_read(&nn->nr_reclaim_complete) ==
5360 nn->reclaim_str_hashtbl_size)
5361 return false;
03f318ca
BF
5362 if (!nn->somebody_reclaimed)
5363 return false;
5364 nn->somebody_reclaimed = false;
5365 /*
5366 * If we've given them *two* lease times to reclaim, and they're
5367 * still not done, give up:
5368 */
20b7d86f 5369 if (ktime_get_boottime_seconds() > double_grace_period_end)
03f318ca
BF
5370 return false;
5371 return true;
5372}
5373
20b7d86f 5374static time64_t
09121281 5375nfs4_laundromat(struct nfsd_net *nn)
1da177e4
LT
5376{
5377 struct nfs4_client *clp;
fe0750e5 5378 struct nfs4_openowner *oo;
1da177e4 5379 struct nfs4_delegation *dp;
217526e7 5380 struct nfs4_ol_stateid *stp;
7919d0a2 5381 struct nfsd4_blocked_lock *nbl;
1da177e4 5382 struct list_head *pos, *next, reaplist;
20b7d86f
AB
5383 time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease;
5384 time64_t t, new_timeo = nn->nfsd4_lease;
624322f1
OK
5385 struct nfs4_cpntf_state *cps;
5386 copy_stateid_t *cps_t;
5387 int i;
1da177e4 5388
03f318ca
BF
5389 if (clients_still_reclaiming(nn)) {
5390 new_timeo = 0;
5391 goto out;
5392 }
12760c66 5393 nfsd4_end_grace(nn);
36acb66b 5394 INIT_LIST_HEAD(&reaplist);
624322f1
OK
5395
5396 spin_lock(&nn->s2s_cp_lock);
5397 idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) {
5398 cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid);
5399 if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID &&
20b7d86f 5400 cps->cpntf_time > cutoff)
624322f1
OK
5401 _free_cpntf_state_locked(nn, cps);
5402 }
5403 spin_unlock(&nn->s2s_cp_lock);
5404
c9a49628 5405 spin_lock(&nn->client_lock);
5ed58bb2 5406 list_for_each_safe(pos, next, &nn->client_lru) {
1da177e4 5407 clp = list_entry(pos, struct nfs4_client, cl_lru);
20b7d86f 5408 if (clp->cl_time > cutoff) {
1da177e4 5409 t = clp->cl_time - cutoff;
a832e7ae 5410 new_timeo = min(new_timeo, t);
1da177e4
LT
5411 break;
5412 }
221a6876 5413 if (mark_client_expired_locked(clp)) {
dd5e3fbc 5414 trace_nfsd_clid_expired(&clp->cl_clientid);
d7682988
BH
5415 continue;
5416 }
4864af97 5417 list_add(&clp->cl_lru, &reaplist);
36acb66b 5418 }
c9a49628 5419 spin_unlock(&nn->client_lock);
36acb66b
BH
5420 list_for_each_safe(pos, next, &reaplist) {
5421 clp = list_entry(pos, struct nfs4_client, cl_lru);
dd5e3fbc 5422 trace_nfsd_clid_purged(&clp->cl_clientid);
4864af97 5423 list_del_init(&clp->cl_lru);
1da177e4
LT
5424 expire_client(clp);
5425 }
cdc97505 5426 spin_lock(&state_lock);
e8c69d17 5427 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 5428 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
20b7d86f 5429 if (dp->dl_time > cutoff) {
a832e7ae
JL
5430 t = dp->dl_time - cutoff;
5431 new_timeo = min(new_timeo, t);
1da177e4
LT
5432 break;
5433 }
3fcbbd24 5434 WARN_ON(!unhash_delegation_locked(dp));
42690676 5435 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 5436 }
cdc97505 5437 spin_unlock(&state_lock);
2d4a532d
JL
5438 while (!list_empty(&reaplist)) {
5439 dp = list_first_entry(&reaplist, struct nfs4_delegation,
5440 dl_recall_lru);
5441 list_del_init(&dp->dl_recall_lru);
3bd64a5b 5442 revoke_delegation(dp);
1da177e4 5443 }
217526e7
JL
5444
5445 spin_lock(&nn->client_lock);
5446 while (!list_empty(&nn->close_lru)) {
5447 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
5448 oo_close_lru);
20b7d86f 5449 if (oo->oo_time > cutoff) {
a832e7ae
JL
5450 t = oo->oo_time - cutoff;
5451 new_timeo = min(new_timeo, t);
1da177e4
LT
5452 break;
5453 }
217526e7
JL
5454 list_del_init(&oo->oo_close_lru);
5455 stp = oo->oo_last_closed_stid;
5456 oo->oo_last_closed_stid = NULL;
5457 spin_unlock(&nn->client_lock);
5458 nfs4_put_stid(&stp->st_stid);
5459 spin_lock(&nn->client_lock);
1da177e4 5460 }
217526e7
JL
5461 spin_unlock(&nn->client_lock);
5462
7919d0a2
JL
5463 /*
5464 * It's possible for a client to try and acquire an already held lock
5465 * that is being held for a long time, and then lose interest in it.
5466 * So, we clean out any un-revisited request after a lease period
5467 * under the assumption that the client is no longer interested.
5468 *
5469 * RFC5661, sec. 9.6 states that the client must not rely on getting
5470 * notifications and must continue to poll for locks, even when the
5471 * server supports them. Thus this shouldn't lead to clients blocking
5472 * indefinitely once the lock does become free.
5473 */
5474 BUG_ON(!list_empty(&reaplist));
0cc11a61 5475 spin_lock(&nn->blocked_locks_lock);
7919d0a2
JL
5476 while (!list_empty(&nn->blocked_locks_lru)) {
5477 nbl = list_first_entry(&nn->blocked_locks_lru,
5478 struct nfsd4_blocked_lock, nbl_lru);
20b7d86f 5479 if (nbl->nbl_time > cutoff) {
7919d0a2
JL
5480 t = nbl->nbl_time - cutoff;
5481 new_timeo = min(new_timeo, t);
5482 break;
5483 }
5484 list_move(&nbl->nbl_lru, &reaplist);
5485 list_del_init(&nbl->nbl_list);
5486 }
0cc11a61 5487 spin_unlock(&nn->blocked_locks_lock);
7919d0a2
JL
5488
5489 while (!list_empty(&reaplist)) {
64ebe124 5490 nbl = list_first_entry(&reaplist,
7919d0a2
JL
5491 struct nfsd4_blocked_lock, nbl_lru);
5492 list_del_init(&nbl->nbl_lru);
7919d0a2
JL
5493 free_blocked_lock(nbl);
5494 }
03f318ca 5495out:
20b7d86f 5496 new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
a832e7ae 5497 return new_timeo;
1da177e4
LT
5498}
5499
a254b246
HH
5500static struct workqueue_struct *laundry_wq;
5501static void laundromat_main(struct work_struct *);
a254b246
HH
5502
5503static void
09121281 5504laundromat_main(struct work_struct *laundry)
1da177e4 5505{
20b7d86f 5506 time64_t t;
2e55f3ab 5507 struct delayed_work *dwork = to_delayed_work(laundry);
09121281
SK
5508 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
5509 laundromat_work);
1da177e4 5510
09121281 5511 t = nfs4_laundromat(nn);
09121281 5512 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
1da177e4
LT
5513}
5514
8fcd461d 5515static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
1da177e4 5516{
8fcd461d 5517 if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
f7a4d872
BF
5518 return nfserr_bad_stateid;
5519 return nfs_ok;
1da177e4
LT
5520}
5521
1da177e4 5522static inline int
82c5ff1b 5523access_permit_read(struct nfs4_ol_stateid *stp)
1da177e4 5524{
82c5ff1b
JL
5525 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
5526 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
5527 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
1da177e4
LT
5528}
5529
5530static inline int
82c5ff1b 5531access_permit_write(struct nfs4_ol_stateid *stp)
1da177e4 5532{
82c5ff1b
JL
5533 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
5534 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
1da177e4
LT
5535}
5536
5537static
dcef0413 5538__be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
1da177e4 5539{
b37ad28b 5540 __be32 status = nfserr_openmode;
1da177e4 5541
02921914
BF
5542 /* For lock stateid's, we test the parent open, not the lock: */
5543 if (stp->st_openstp)
5544 stp = stp->st_openstp;
82c5ff1b 5545 if ((flags & WR_STATE) && !access_permit_write(stp))
1da177e4 5546 goto out;
82c5ff1b 5547 if ((flags & RD_STATE) && !access_permit_read(stp))
1da177e4
LT
5548 goto out;
5549 status = nfs_ok;
5550out:
5551 return status;
5552}
5553
b37ad28b 5554static inline __be32
5ccb0066 5555check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
1da177e4 5556{
203a8c8e 5557 if (ONE_STATEID(stateid) && (flags & RD_STATE))
1da177e4 5558 return nfs_ok;
c87fb4a3 5559 else if (opens_in_grace(net)) {
25985edc 5560 /* Answer in remaining cases depends on existence of
1da177e4
LT
5561 * conflicting state; so we must wait out the grace period. */
5562 return nfserr_grace;
5563 } else if (flags & WR_STATE)
5564 return nfs4_share_conflict(current_fh,
5565 NFS4_SHARE_DENY_WRITE);
5566 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5567 return nfs4_share_conflict(current_fh,
5568 NFS4_SHARE_DENY_READ);
5569}
5570
5571/*
5572 * Allow READ/WRITE during grace period on recovered state only for files
5573 * that are not able to provide mandatory locking.
5574 */
5575static inline int
5ccb0066 5576grace_disallows_io(struct net *net, struct inode *inode)
1da177e4 5577{
c87fb4a3 5578 return opens_in_grace(net) && mandatory_lock(inode);
1da177e4
LT
5579}
5580
57b7b43b 5581static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
0836f587 5582{
6668958f
AA
5583 /*
5584 * When sessions are used the stateid generation number is ignored
5585 * when it is zero.
5586 */
28dde241 5587 if (has_session && in->si_generation == 0)
81b82965
BF
5588 return nfs_ok;
5589
5590 if (in->si_generation == ref->si_generation)
5591 return nfs_ok;
6668958f 5592
0836f587 5593 /* If the client sends us a stateid from the future, it's buggy: */
14b7f4a1 5594 if (nfsd4_stateid_generation_after(in, ref))
0836f587
BF
5595 return nfserr_bad_stateid;
5596 /*
81b82965
BF
5597 * However, we could see a stateid from the past, even from a
5598 * non-buggy client. For example, if the client sends a lock
5599 * while some IO is outstanding, the lock may bump si_generation
5600 * while the IO is still in flight. The client could avoid that
5601 * situation by waiting for responses on all the IO requests,
5602 * but better performance may result in retrying IO that
5603 * receives an old_stateid error if requests are rarely
5604 * reordered in flight:
0836f587 5605 */
81b82965 5606 return nfserr_old_stateid;
0836f587
BF
5607}
5608
03da3169
TM
5609static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session)
5610{
5611 __be32 ret;
5612
5613 spin_lock(&s->sc_lock);
5614 ret = nfsd4_verify_open_stid(s);
5615 if (ret == nfs_ok)
5616 ret = check_stateid_generation(in, &s->sc_stateid, has_session);
5617 spin_unlock(&s->sc_lock);
5618 return ret;
5619}
5620
ebe9cb3b
CH
5621static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
5622{
5623 if (ols->st_stateowner->so_is_open_owner &&
5624 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
5625 return nfserr_bad_stateid;
5626 return nfs_ok;
5627}
5628
7df302f7 5629static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
17456804 5630{
97b7e3b6 5631 struct nfs4_stid *s;
1af71cc8 5632 __be32 status = nfserr_bad_stateid;
17456804 5633
ae254dac
AE
5634 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5635 CLOSE_STATEID(stateid))
1af71cc8 5636 return status;
663e36f0 5637 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid))
1af71cc8 5638 return status;
1af71cc8
JL
5639 spin_lock(&cl->cl_lock);
5640 s = find_stateid_locked(cl, stateid);
97b7e3b6 5641 if (!s)
1af71cc8 5642 goto out_unlock;
03da3169 5643 status = nfsd4_stid_check_stateid_generation(stateid, s, 1);
17456804 5644 if (status)
1af71cc8 5645 goto out_unlock;
23340032
BF
5646 switch (s->sc_type) {
5647 case NFS4_DELEG_STID:
1af71cc8
JL
5648 status = nfs_ok;
5649 break;
3bd64a5b 5650 case NFS4_REVOKED_DELEG_STID:
1af71cc8
JL
5651 status = nfserr_deleg_revoked;
5652 break;
23340032
BF
5653 case NFS4_OPEN_STID:
5654 case NFS4_LOCK_STID:
ebe9cb3b 5655 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
1af71cc8 5656 break;
23340032
BF
5657 default:
5658 printk("unknown stateid type %x\n", s->sc_type);
df561f66 5659 fallthrough;
23340032 5660 case NFS4_CLOSED_STID:
b0fc29d6 5661 case NFS4_CLOSED_DELEG_STID:
1af71cc8 5662 status = nfserr_bad_stateid;
23340032 5663 }
1af71cc8
JL
5664out_unlock:
5665 spin_unlock(&cl->cl_lock);
5666 return status;
17456804
BS
5667}
5668
cd61c522 5669__be32
2dd6e458
TM
5670nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
5671 stateid_t *stateid, unsigned char typemask,
5672 struct nfs4_stid **s, struct nfsd_net *nn)
38c2f4b1 5673{
0eb6f20a 5674 __be32 status;
95da1b3a
AE
5675 bool return_revoked = false;
5676
5677 /*
5678 * only return revoked delegations if explicitly asked.
5679 * otherwise we report revoked or bad_stateid status.
5680 */
5681 if (typemask & NFS4_REVOKED_DELEG_STID)
5682 return_revoked = true;
5683 else if (typemask & NFS4_DELEG_STID)
5684 typemask |= NFS4_REVOKED_DELEG_STID;
38c2f4b1 5685
ae254dac
AE
5686 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) ||
5687 CLOSE_STATEID(stateid))
38c2f4b1 5688 return nfserr_bad_stateid;
b7342204
OK
5689 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn,
5690 false);
a8a7c677 5691 if (status == nfserr_stale_clientid) {
4b24ca7d 5692 if (cstate->session)
a8a7c677 5693 return nfserr_bad_stateid;
38c2f4b1 5694 return nfserr_stale_stateid;
a8a7c677 5695 }
0eb6f20a
BF
5696 if (status)
5697 return status;
4b24ca7d 5698 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
38c2f4b1
BF
5699 if (!*s)
5700 return nfserr_bad_stateid;
95da1b3a
AE
5701 if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) {
5702 nfs4_put_stid(*s);
5703 if (cstate->minorversion)
5704 return nfserr_deleg_revoked;
5705 return nfserr_bad_stateid;
5706 }
38c2f4b1 5707 return nfs_ok;
38c2f4b1
BF
5708}
5709
eb82dd39 5710static struct nfsd_file *
a0649b2d
CH
5711nfs4_find_file(struct nfs4_stid *s, int flags)
5712{
af90f707
CH
5713 if (!s)
5714 return NULL;
5715
a0649b2d
CH
5716 switch (s->sc_type) {
5717 case NFS4_DELEG_STID:
5718 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
5719 return NULL;
eb82dd39 5720 return nfsd_file_get(s->sc_file->fi_deleg_file);
a0649b2d
CH
5721 case NFS4_OPEN_STID:
5722 case NFS4_LOCK_STID:
5723 if (flags & RD_STATE)
5724 return find_readable_file(s->sc_file);
5725 else
5726 return find_writeable_file(s->sc_file);
a0649b2d
CH
5727 }
5728
5729 return NULL;
5730}
5731
5732static __be32
d8836f77 5733nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags)
a0649b2d
CH
5734{
5735 __be32 status;
5736
a0649b2d
CH
5737 status = nfsd4_check_openowner_confirmed(ols);
5738 if (status)
5739 return status;
5740 return nfs4_check_openmode(ols, flags);
5741}
5742
af90f707
CH
5743static __be32
5744nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s,
5c4583b2 5745 struct nfsd_file **nfp, int flags)
af90f707
CH
5746{
5747 int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE;
eb82dd39 5748 struct nfsd_file *nf;
af90f707
CH
5749 __be32 status;
5750
eb82dd39
JL
5751 nf = nfs4_find_file(s, flags);
5752 if (nf) {
af90f707
CH
5753 status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry,
5754 acc | NFSD_MAY_OWNER_OVERRIDE);
5c4583b2
JL
5755 if (status) {
5756 nfsd_file_put(nf);
eb82dd39 5757 goto out;
5c4583b2 5758 }
af90f707 5759 } else {
eb82dd39 5760 status = nfsd_file_acquire(rqstp, fhp, acc, &nf);
af90f707
CH
5761 if (status)
5762 return status;
af90f707 5763 }
5c4583b2 5764 *nfp = nf;
eb82dd39 5765out:
eb82dd39 5766 return status;
af90f707 5767}
624322f1
OK
5768static void
5769_free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5770{
5771 WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID);
5772 if (!refcount_dec_and_test(&cps->cp_stateid.sc_count))
5773 return;
5774 list_del(&cps->cp_list);
5775 idr_remove(&nn->s2s_cp_stateids,
5776 cps->cp_stateid.stid.si_opaque.so_id);
5777 kfree(cps);
5778}
b7342204
OK
5779/*
5780 * A READ from an inter server to server COPY will have a
5781 * copy stateid. Look up the copy notify stateid from the
5782 * idr structure and take a reference on it.
5783 */
ce0887ac
OK
5784__be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5785 struct nfs4_client *clp,
5786 struct nfs4_cpntf_state **cps)
b7342204
OK
5787{
5788 copy_stateid_t *cps_t;
5789 struct nfs4_cpntf_state *state = NULL;
5790
5791 if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id)
5792 return nfserr_bad_stateid;
5793 spin_lock(&nn->s2s_cp_lock);
5794 cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id);
5795 if (cps_t) {
5796 state = container_of(cps_t, struct nfs4_cpntf_state,
5797 cp_stateid);
5277a79e
DC
5798 if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) {
5799 state = NULL;
5800 goto unlock;
5801 }
ce0887ac
OK
5802 if (!clp)
5803 refcount_inc(&state->cp_stateid.sc_count);
5804 else
5805 _free_cpntf_state_locked(nn, state);
b7342204 5806 }
5277a79e 5807unlock:
b7342204
OK
5808 spin_unlock(&nn->s2s_cp_lock);
5809 if (!state)
5810 return nfserr_bad_stateid;
ce0887ac
OK
5811 if (!clp && state)
5812 *cps = state;
b7342204
OK
5813 return 0;
5814}
5815
5816static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st,
5817 struct nfs4_stid **stid)
5818{
5819 __be32 status;
5820 struct nfs4_cpntf_state *cps = NULL;
5821 struct nfsd4_compound_state cstate;
5822
ce0887ac 5823 status = manage_cpntf_state(nn, st, NULL, &cps);
b7342204
OK
5824 if (status)
5825 return status;
5826
20b7d86f 5827 cps->cpntf_time = ktime_get_boottime_seconds();
b7342204
OK
5828 memset(&cstate, 0, sizeof(cstate));
5829 status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true);
5830 if (status)
5831 goto out;
5832 status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid,
5833 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
5834 stid, nn);
5835 put_client_renew(cstate.clp);
5836out:
5837 nfs4_put_cpntf_state(nn, cps);
5838 return status;
5839}
624322f1
OK
5840
5841void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps)
5842{
5843 spin_lock(&nn->s2s_cp_lock);
5844 _free_cpntf_state_locked(nn, cps);
5845 spin_unlock(&nn->s2s_cp_lock);
5846}
af90f707 5847
1da177e4 5848/*
a0649b2d
CH
5849 * Checks for stateid operations
5850 */
b37ad28b 5851__be32
af90f707 5852nfs4_preprocess_stateid_op(struct svc_rqst *rqstp,
aa0d6aed 5853 struct nfsd4_compound_state *cstate, struct svc_fh *fhp,
624322f1
OK
5854 stateid_t *stateid, int flags, struct nfsd_file **nfp,
5855 struct nfs4_stid **cstid)
1da177e4 5856{
a0649b2d 5857 struct inode *ino = d_inode(fhp->fh_dentry);
af90f707 5858 struct net *net = SVC_NET(rqstp);
3320fef1 5859 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
af90f707 5860 struct nfs4_stid *s = NULL;
b37ad28b 5861 __be32 status;
1da177e4 5862
5c4583b2
JL
5863 if (nfp)
5864 *nfp = NULL;
1da177e4 5865
5ccb0066 5866 if (grace_disallows_io(net, ino))
1da177e4
LT
5867 return nfserr_grace;
5868
af90f707
CH
5869 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) {
5870 status = check_special_stateids(net, fhp, stateid, flags);
5871 goto done;
5872 }
1da177e4 5873
2dd6e458 5874 status = nfsd4_lookup_stateid(cstate, stateid,
db24b3b4 5875 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
2dd6e458 5876 &s, nn);
b7342204
OK
5877 if (status == nfserr_bad_stateid)
5878 status = find_cpntf_state(nn, stateid, &s);
38c2f4b1 5879 if (status)
c2d1d6a8 5880 return status;
03da3169 5881 status = nfsd4_stid_check_stateid_generation(stateid, s,
a0649b2d 5882 nfsd4_has_session(cstate));
69064a27
BF
5883 if (status)
5884 goto out;
a0649b2d 5885
f7a4d872
BF
5886 switch (s->sc_type) {
5887 case NFS4_DELEG_STID:
a0649b2d 5888 status = nfs4_check_delegmode(delegstateid(s), flags);
f7a4d872
BF
5889 break;
5890 case NFS4_OPEN_STID:
5891 case NFS4_LOCK_STID:
d8836f77 5892 status = nfs4_check_olstateid(openlockstateid(s), flags);
f7a4d872
BF
5893 break;
5894 default:
14bcab1a 5895 status = nfserr_bad_stateid;
a0649b2d
CH
5896 break;
5897 }
8fcd461d
JL
5898 if (status)
5899 goto out;
5900 status = nfs4_check_fh(fhp, s);
a0649b2d 5901
af90f707 5902done:
5c4583b2
JL
5903 if (status == nfs_ok && nfp)
5904 status = nfs4_check_file(rqstp, fhp, s, nfp, flags);
1da177e4 5905out:
624322f1
OK
5906 if (s) {
5907 if (!status && cstid)
5908 *cstid = s;
5909 else
5910 nfs4_put_stid(s);
5911 }
1da177e4
LT
5912 return status;
5913}
5914
17456804
BS
5915/*
5916 * Test if the stateid is valid
5917 */
5918__be32
5919nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 5920 union nfsd4_op_u *u)
17456804 5921{
eb69853d 5922 struct nfsd4_test_stateid *test_stateid = &u->test_stateid;
03cfb420
BS
5923 struct nfsd4_test_stateid_id *stateid;
5924 struct nfs4_client *cl = cstate->session->se_client;
5925
03cfb420 5926 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
7df302f7
CL
5927 stateid->ts_id_status =
5928 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
03cfb420 5929
17456804
BS
5930 return nfs_ok;
5931}
5932
42691398
CL
5933static __be32
5934nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s)
5935{
5936 struct nfs4_ol_stateid *stp = openlockstateid(s);
5937 __be32 ret;
5938
659aefb6
TM
5939 ret = nfsd4_lock_ol_stateid(stp);
5940 if (ret)
5941 goto out_put_stid;
42691398
CL
5942
5943 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5944 if (ret)
5945 goto out;
5946
5947 ret = nfserr_locks_held;
5948 if (check_for_locks(stp->st_stid.sc_file,
5949 lockowner(stp->st_stateowner)))
5950 goto out;
5951
5952 release_lock_stateid(stp);
5953 ret = nfs_ok;
5954
5955out:
5956 mutex_unlock(&stp->st_mutex);
659aefb6 5957out_put_stid:
42691398
CL
5958 nfs4_put_stid(s);
5959 return ret;
5960}
5961
e1ca12df
BS
5962__be32
5963nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 5964 union nfsd4_op_u *u)
e1ca12df 5965{
eb69853d 5966 struct nfsd4_free_stateid *free_stateid = &u->free_stateid;
e1ca12df 5967 stateid_t *stateid = &free_stateid->fr_stateid;
2da1cec7 5968 struct nfs4_stid *s;
3bd64a5b 5969 struct nfs4_delegation *dp;
38c2f4b1 5970 struct nfs4_client *cl = cstate->session->se_client;
2da1cec7 5971 __be32 ret = nfserr_bad_stateid;
e1ca12df 5972
1af71cc8
JL
5973 spin_lock(&cl->cl_lock);
5974 s = find_stateid_locked(cl, stateid);
2da1cec7 5975 if (!s)
1af71cc8 5976 goto out_unlock;
03da3169 5977 spin_lock(&s->sc_lock);
2da1cec7
BF
5978 switch (s->sc_type) {
5979 case NFS4_DELEG_STID:
e1ca12df 5980 ret = nfserr_locks_held;
1af71cc8 5981 break;
2da1cec7 5982 case NFS4_OPEN_STID:
2da1cec7
BF
5983 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
5984 if (ret)
1af71cc8
JL
5985 break;
5986 ret = nfserr_locks_held;
f7a4d872 5987 break;
1af71cc8 5988 case NFS4_LOCK_STID:
03da3169 5989 spin_unlock(&s->sc_lock);
a15dfcd5 5990 refcount_inc(&s->sc_count);
1af71cc8 5991 spin_unlock(&cl->cl_lock);
42691398 5992 ret = nfsd4_free_lock_stateid(stateid, s);
1af71cc8 5993 goto out;
3bd64a5b 5994 case NFS4_REVOKED_DELEG_STID:
03da3169 5995 spin_unlock(&s->sc_lock);
3bd64a5b 5996 dp = delegstateid(s);
2d4a532d
JL
5997 list_del_init(&dp->dl_recall_lru);
5998 spin_unlock(&cl->cl_lock);
6011695d 5999 nfs4_put_stid(s);
3bd64a5b 6000 ret = nfs_ok;
1af71cc8
JL
6001 goto out;
6002 /* Default falls through and returns nfserr_bad_stateid */
e1ca12df 6003 }
03da3169 6004 spin_unlock(&s->sc_lock);
1af71cc8
JL
6005out_unlock:
6006 spin_unlock(&cl->cl_lock);
e1ca12df 6007out:
e1ca12df
BS
6008 return ret;
6009}
6010
4c4cd222
N
6011static inline int
6012setlkflg (int type)
6013{
6014 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
6015 RD_STATE : WR_STATE;
6016}
1da177e4 6017
dcef0413 6018static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
c0a5d93e
BF
6019{
6020 struct svc_fh *current_fh = &cstate->current_fh;
6021 struct nfs4_stateowner *sop = stp->st_stateowner;
6022 __be32 status;
6023
c0a5d93e
BF
6024 status = nfsd4_check_seqid(cstate, sop, seqid);
6025 if (status)
6026 return status;
9271d7e5
TM
6027 status = nfsd4_lock_ol_stateid(stp);
6028 if (status != nfs_ok)
6029 return status;
f7a4d872 6030 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
35a92fe8
JL
6031 if (status == nfs_ok)
6032 status = nfs4_check_fh(current_fh, &stp->st_stid);
6033 if (status != nfs_ok)
feb9dad5 6034 mutex_unlock(&stp->st_mutex);
35a92fe8 6035 return status;
c0a5d93e
BF
6036}
6037
1da177e4
LT
6038/*
6039 * Checks for sequence id mutating operations.
6040 */
b37ad28b 6041static __be32
dd453dfd 6042nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
2288d0e3 6043 stateid_t *stateid, char typemask,
3320fef1
SK
6044 struct nfs4_ol_stateid **stpp,
6045 struct nfsd_net *nn)
1da177e4 6046{
0836f587 6047 __be32 status;
38c2f4b1 6048 struct nfs4_stid *s;
e17f99b7 6049 struct nfs4_ol_stateid *stp = NULL;
1da177e4 6050
dd5e3fbc 6051 trace_nfsd_preprocess(seqid, stateid);
3a4f98bb 6052
1da177e4 6053 *stpp = NULL;
2dd6e458 6054 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
c0a5d93e
BF
6055 if (status)
6056 return status;
e17f99b7 6057 stp = openlockstateid(s);
58fb12e6 6058 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
1da177e4 6059
e17f99b7 6060 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
fd911011 6061 if (!status)
e17f99b7 6062 *stpp = stp;
fd911011
TM
6063 else
6064 nfs4_put_stid(&stp->st_stid);
e17f99b7 6065 return status;
c0a5d93e 6066}
39325bd0 6067
3320fef1
SK
6068static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
6069 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
c0a5d93e
BF
6070{
6071 __be32 status;
6072 struct nfs4_openowner *oo;
4cbfc9f7 6073 struct nfs4_ol_stateid *stp;
1da177e4 6074
c0a5d93e 6075 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4cbfc9f7 6076 NFS4_OPEN_STID, &stp, nn);
7a8711c9
BF
6077 if (status)
6078 return status;
4cbfc9f7
TM
6079 oo = openowner(stp->st_stateowner);
6080 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
feb9dad5 6081 mutex_unlock(&stp->st_mutex);
4cbfc9f7 6082 nfs4_put_stid(&stp->st_stid);
3a4f98bb 6083 return nfserr_bad_stateid;
4cbfc9f7
TM
6084 }
6085 *stpp = stp;
3a4f98bb 6086 return nfs_ok;
1da177e4
LT
6087}
6088
b37ad28b 6089__be32
ca364317 6090nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 6091 union nfsd4_op_u *u)
1da177e4 6092{
eb69853d 6093 struct nfsd4_open_confirm *oc = &u->open_confirm;
b37ad28b 6094 __be32 status;
fe0750e5 6095 struct nfs4_openowner *oo;
dcef0413 6096 struct nfs4_ol_stateid *stp;
3320fef1 6097 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 6098
a6a9f18f
AV
6099 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6100 cstate->current_fh.fh_dentry);
1da177e4 6101
ca364317 6102 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
a8cddc5d
BF
6103 if (status)
6104 return status;
1da177e4 6105
9072d5c6 6106 status = nfs4_preprocess_seqid_op(cstate,
ca364317 6107 oc->oc_seqid, &oc->oc_req_stateid,
3320fef1 6108 NFS4_OPEN_STID, &stp, nn);
9072d5c6 6109 if (status)
68b66e82 6110 goto out;
fe0750e5 6111 oo = openowner(stp->st_stateowner);
68b66e82 6112 status = nfserr_bad_stateid;
35a92fe8 6113 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
feb9dad5 6114 mutex_unlock(&stp->st_mutex);
2585fc79 6115 goto put_stateid;
35a92fe8 6116 }
dad1c067 6117 oo->oo_flags |= NFS4_OO_CONFIRMED;
9767feb2 6118 nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid);
feb9dad5 6119 mutex_unlock(&stp->st_mutex);
dd5e3fbc 6120 trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid);
2a4317c5 6121 nfsd4_client_record_create(oo->oo_owner.so_client);
68b66e82 6122 status = nfs_ok;
2585fc79
TM
6123put_stateid:
6124 nfs4_put_stid(&stp->st_stid);
1da177e4 6125out:
9411b1d4 6126 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
6127 return status;
6128}
6129
6409a5a6 6130static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
1da177e4 6131{
82c5ff1b 6132 if (!test_access(access, stp))
6409a5a6 6133 return;
11b9164a 6134 nfs4_file_put_access(stp->st_stid.sc_file, access);
82c5ff1b 6135 clear_access(access, stp);
6409a5a6 6136}
f197c271 6137
6409a5a6
BF
6138static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
6139{
6140 switch (to_access) {
6141 case NFS4_SHARE_ACCESS_READ:
6142 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
6143 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6144 break;
6145 case NFS4_SHARE_ACCESS_WRITE:
6146 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
6147 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
6148 break;
6149 case NFS4_SHARE_ACCESS_BOTH:
6150 break;
6151 default:
063b0fb9 6152 WARN_ON_ONCE(1);
1da177e4
LT
6153 }
6154}
6155
b37ad28b 6156__be32
ca364317 6157nfsd4_open_downgrade(struct svc_rqst *rqstp,
eb69853d 6158 struct nfsd4_compound_state *cstate, union nfsd4_op_u *u)
1da177e4 6159{
eb69853d 6160 struct nfsd4_open_downgrade *od = &u->open_downgrade;
b37ad28b 6161 __be32 status;
dcef0413 6162 struct nfs4_ol_stateid *stp;
3320fef1 6163 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 6164
a6a9f18f
AV
6165 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6166 cstate->current_fh.fh_dentry);
1da177e4 6167
c30e92df 6168 /* We don't yet support WANT bits: */
2c8bd7e0
BH
6169 if (od->od_deleg_want)
6170 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
6171 od->od_deleg_want);
1da177e4 6172
c0a5d93e 6173 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
3320fef1 6174 &od->od_stateid, &stp, nn);
9072d5c6 6175 if (status)
1da177e4 6176 goto out;
1da177e4 6177 status = nfserr_inval;
82c5ff1b 6178 if (!test_access(od->od_share_access, stp)) {
c11c591f 6179 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
1da177e4 6180 stp->st_access_bmap, od->od_share_access);
0667b1e9 6181 goto put_stateid;
1da177e4 6182 }
ce0fc43c 6183 if (!test_deny(od->od_share_deny, stp)) {
c11c591f 6184 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
1da177e4 6185 stp->st_deny_bmap, od->od_share_deny);
0667b1e9 6186 goto put_stateid;
1da177e4 6187 }
6409a5a6 6188 nfs4_stateid_downgrade(stp, od->od_share_access);
ce0fc43c 6189 reset_union_bmap_deny(od->od_share_deny, stp);
9767feb2 6190 nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid);
1da177e4 6191 status = nfs_ok;
0667b1e9 6192put_stateid:
feb9dad5 6193 mutex_unlock(&stp->st_mutex);
0667b1e9 6194 nfs4_put_stid(&stp->st_stid);
1da177e4 6195out:
9411b1d4 6196 nfsd4_bump_seqid(cstate, status);
1da177e4
LT
6197 return status;
6198}
6199
f7a4d872
BF
6200static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
6201{
acf9295b 6202 struct nfs4_client *clp = s->st_stid.sc_client;
e8568739 6203 bool unhashed;
d83017f9 6204 LIST_HEAD(reaplist);
acf9295b 6205
2c41beb0 6206 spin_lock(&clp->cl_lock);
e8568739 6207 unhashed = unhash_open_stateid(s, &reaplist);
acf9295b 6208
d83017f9 6209 if (clp->cl_minorversion) {
e8568739
JL
6210 if (unhashed)
6211 put_ol_stateid_locked(s, &reaplist);
d83017f9
JL
6212 spin_unlock(&clp->cl_lock);
6213 free_ol_stateid_reaplist(&reaplist);
6214 } else {
6215 spin_unlock(&clp->cl_lock);
6216 free_ol_stateid_reaplist(&reaplist);
e8568739
JL
6217 if (unhashed)
6218 move_to_close_lru(s, clp->net);
d83017f9 6219 }
38c387b5
BF
6220}
6221
1da177e4
LT
6222/*
6223 * nfs4_unlock_state() called after encode
6224 */
b37ad28b 6225__be32
ca364317 6226nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 6227 union nfsd4_op_u *u)
1da177e4 6228{
eb69853d 6229 struct nfsd4_close *close = &u->close;
b37ad28b 6230 __be32 status;
dcef0413 6231 struct nfs4_ol_stateid *stp;
3320fef1
SK
6232 struct net *net = SVC_NET(rqstp);
6233 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 6234
a6a9f18f
AV
6235 dprintk("NFSD: nfsd4_close on file %pd\n",
6236 cstate->current_fh.fh_dentry);
1da177e4 6237
f7a4d872
BF
6238 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
6239 &close->cl_stateid,
6240 NFS4_OPEN_STID|NFS4_CLOSED_STID,
3320fef1 6241 &stp, nn);
9411b1d4 6242 nfsd4_bump_seqid(cstate, status);
9072d5c6 6243 if (status)
1da177e4 6244 goto out;
15ca08d3
TM
6245
6246 stp->st_stid.sc_type = NFS4_CLOSED_STID;
bd2decac
JL
6247
6248 /*
6249 * Technically we don't _really_ have to increment or copy it, since
6250 * it should just be gone after this operation and we clobber the
6251 * copied value below, but we continue to do so here just to ensure
6252 * that racing ops see that there was a state change.
6253 */
9767feb2 6254 nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid);
1da177e4 6255
f7a4d872 6256 nfsd4_close_open_stateid(stp);
15ca08d3 6257 mutex_unlock(&stp->st_mutex);
8a0b589d 6258
bd2decac
JL
6259 /* v4.1+ suggests that we send a special stateid in here, since the
6260 * clients should just ignore this anyway. Since this is not useful
6261 * for v4.0 clients either, we set it to the special close_stateid
6262 * universally.
6263 *
6264 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6265 */
6266 memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid));
fb500a7c 6267
8a0b589d
TM
6268 /* put reference from nfs4_preprocess_seqid_op */
6269 nfs4_put_stid(&stp->st_stid);
1da177e4 6270out:
1da177e4
LT
6271 return status;
6272}
6273
b37ad28b 6274__be32
ca364317 6275nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 6276 union nfsd4_op_u *u)
1da177e4 6277{
eb69853d 6278 struct nfsd4_delegreturn *dr = &u->delegreturn;
203a8c8e
BF
6279 struct nfs4_delegation *dp;
6280 stateid_t *stateid = &dr->dr_stateid;
38c2f4b1 6281 struct nfs4_stid *s;
b37ad28b 6282 __be32 status;
3320fef1 6283 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 6284
ca364317 6285 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
203a8c8e 6286 return status;
1da177e4 6287
2dd6e458 6288 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
38c2f4b1 6289 if (status)
203a8c8e 6290 goto out;
38c2f4b1 6291 dp = delegstateid(s);
03da3169 6292 status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate));
203a8c8e 6293 if (status)
fd911011 6294 goto put_stateid;
203a8c8e 6295
3bd64a5b 6296 destroy_delegation(dp);
fd911011
TM
6297put_stateid:
6298 nfs4_put_stid(&dp->dl_stid);
1da177e4
LT
6299out:
6300 return status;
6301}
6302
87df4de8
BH
6303static inline u64
6304end_offset(u64 start, u64 len)
6305{
6306 u64 end;
6307
6308 end = start + len;
6309 return end >= start ? end: NFS4_MAX_UINT64;
6310}
6311
6312/* last octet in a range */
6313static inline u64
6314last_byte_offset(u64 start, u64 len)
6315{
6316 u64 end;
6317
063b0fb9 6318 WARN_ON_ONCE(!len);
87df4de8
BH
6319 end = start + len;
6320 return end > start ? end - 1: NFS4_MAX_UINT64;
6321}
6322
1da177e4
LT
6323/*
6324 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6325 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6326 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6327 * locking, this prevents us from being completely protocol-compliant. The
6328 * real solution to this problem is to start using unsigned file offsets in
6329 * the VFS, but this is a very deep change!
6330 */
6331static inline void
6332nfs4_transform_lock_offset(struct file_lock *lock)
6333{
6334 if (lock->fl_start < 0)
6335 lock->fl_start = OFFSET_MAX;
6336 if (lock->fl_end < 0)
6337 lock->fl_end = OFFSET_MAX;
6338}
6339
cae80b30
JL
6340static fl_owner_t
6341nfsd4_fl_get_owner(fl_owner_t owner)
aef9583b 6342{
cae80b30
JL
6343 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
6344
6345 nfs4_get_stateowner(&lo->lo_owner);
6346 return owner;
aef9583b
KM
6347}
6348
cae80b30
JL
6349static void
6350nfsd4_fl_put_owner(fl_owner_t owner)
aef9583b 6351{
cae80b30 6352 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner;
aef9583b 6353
cae80b30 6354 if (lo)
aef9583b 6355 nfs4_put_stateowner(&lo->lo_owner);
aef9583b
KM
6356}
6357
76d348fa
JL
6358static void
6359nfsd4_lm_notify(struct file_lock *fl)
6360{
6361 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
6362 struct net *net = lo->lo_owner.so_client->net;
6363 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6364 struct nfsd4_blocked_lock *nbl = container_of(fl,
6365 struct nfsd4_blocked_lock, nbl_lock);
6366 bool queue = false;
6367
7919d0a2 6368 /* An empty list means that something else is going to be using it */
0cc11a61 6369 spin_lock(&nn->blocked_locks_lock);
76d348fa
JL
6370 if (!list_empty(&nbl->nbl_list)) {
6371 list_del_init(&nbl->nbl_list);
7919d0a2 6372 list_del_init(&nbl->nbl_lru);
76d348fa
JL
6373 queue = true;
6374 }
0cc11a61 6375 spin_unlock(&nn->blocked_locks_lock);
76d348fa
JL
6376
6377 if (queue)
6378 nfsd4_run_cb(&nbl->nbl_cb);
6379}
6380
7b021967 6381static const struct lock_manager_operations nfsd_posix_mng_ops = {
76d348fa 6382 .lm_notify = nfsd4_lm_notify,
aef9583b
KM
6383 .lm_get_owner = nfsd4_fl_get_owner,
6384 .lm_put_owner = nfsd4_fl_put_owner,
d5b9026a 6385};
1da177e4
LT
6386
6387static inline void
6388nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
6389{
fe0750e5 6390 struct nfs4_lockowner *lo;
1da177e4 6391
d5b9026a 6392 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
fe0750e5 6393 lo = (struct nfs4_lockowner *) fl->fl_owner;
6f4859b8
BF
6394 xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner,
6395 GFP_KERNEL);
7c13f344
BF
6396 if (!deny->ld_owner.data)
6397 /* We just don't care that much */
6398 goto nevermind;
fe0750e5 6399 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
d5b9026a 6400 } else {
7c13f344
BF
6401nevermind:
6402 deny->ld_owner.len = 0;
6403 deny->ld_owner.data = NULL;
d5b9026a
N
6404 deny->ld_clientid.cl_boot = 0;
6405 deny->ld_clientid.cl_id = 0;
1da177e4
LT
6406 }
6407 deny->ld_start = fl->fl_start;
87df4de8
BH
6408 deny->ld_length = NFS4_MAX_UINT64;
6409 if (fl->fl_end != NFS4_MAX_UINT64)
1da177e4
LT
6410 deny->ld_length = fl->fl_end - fl->fl_start + 1;
6411 deny->ld_type = NFS4_READ_LT;
6412 if (fl->fl_type != F_RDLCK)
6413 deny->ld_type = NFS4_WRITE_LT;
6414}
6415
fe0750e5 6416static struct nfs4_lockowner *
c8623999 6417find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner)
1da177e4 6418{
d4f0489f 6419 unsigned int strhashval = ownerstr_hashval(owner);
b3c32bcd 6420 struct nfs4_stateowner *so;
1da177e4 6421
0a880a28
TM
6422 lockdep_assert_held(&clp->cl_lock);
6423
d4f0489f
TM
6424 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
6425 so_strhash) {
b3c32bcd
TM
6426 if (so->so_is_open_owner)
6427 continue;
b5971afa
KM
6428 if (same_owner_str(so, owner))
6429 return lockowner(nfs4_get_stateowner(so));
1da177e4
LT
6430 }
6431 return NULL;
6432}
6433
c58c6610 6434static struct nfs4_lockowner *
c8623999 6435find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner)
c58c6610
TM
6436{
6437 struct nfs4_lockowner *lo;
6438
d4f0489f 6439 spin_lock(&clp->cl_lock);
c8623999 6440 lo = find_lockowner_str_locked(clp, owner);
d4f0489f 6441 spin_unlock(&clp->cl_lock);
c58c6610
TM
6442 return lo;
6443}
6444
8f4b54c5
JL
6445static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
6446{
c58c6610 6447 unhash_lockowner_locked(lockowner(sop));
8f4b54c5
JL
6448}
6449
6b180f0b
JL
6450static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
6451{
6452 struct nfs4_lockowner *lo = lockowner(sop);
6453
6454 kmem_cache_free(lockowner_slab, lo);
6455}
6456
6457static const struct nfs4_stateowner_operations lockowner_ops = {
8f4b54c5
JL
6458 .so_unhash = nfs4_unhash_lockowner,
6459 .so_free = nfs4_free_lockowner,
6b180f0b
JL
6460};
6461
1da177e4
LT
6462/*
6463 * Alloc a lock owner structure.
6464 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
25985edc 6465 * occurred.
1da177e4 6466 *
16bfdaaf 6467 * strhashval = ownerstr_hashval
1da177e4 6468 */
fe0750e5 6469static struct nfs4_lockowner *
c58c6610
TM
6470alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
6471 struct nfs4_ol_stateid *open_stp,
6472 struct nfsd4_lock *lock)
6473{
c58c6610 6474 struct nfs4_lockowner *lo, *ret;
1da177e4 6475
fe0750e5
BF
6476 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
6477 if (!lo)
1da177e4 6478 return NULL;
76d348fa 6479 INIT_LIST_HEAD(&lo->lo_blocked);
fe0750e5
BF
6480 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
6481 lo->lo_owner.so_is_open_owner = 0;
5db1c03f 6482 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
6b180f0b 6483 lo->lo_owner.so_ops = &lockowner_ops;
d4f0489f 6484 spin_lock(&clp->cl_lock);
c8623999 6485 ret = find_lockowner_str_locked(clp, &lock->lk_new_owner);
c58c6610
TM
6486 if (ret == NULL) {
6487 list_add(&lo->lo_owner.so_strhash,
d4f0489f 6488 &clp->cl_ownerstr_hashtbl[strhashval]);
c58c6610
TM
6489 ret = lo;
6490 } else
d50ffded
KM
6491 nfs4_free_stateowner(&lo->lo_owner);
6492
d4f0489f 6493 spin_unlock(&clp->cl_lock);
340f0ba1 6494 return ret;
1da177e4
LT
6495}
6496
fd1fd685 6497static struct nfs4_ol_stateid *
a451b123
TM
6498find_lock_stateid(const struct nfs4_lockowner *lo,
6499 const struct nfs4_ol_stateid *ost)
fd1fd685
TM
6500{
6501 struct nfs4_ol_stateid *lst;
fd1fd685 6502
a451b123 6503 lockdep_assert_held(&ost->st_stid.sc_client->cl_lock);
fd1fd685 6504
a451b123
TM
6505 /* If ost is not hashed, ost->st_locks will not be valid */
6506 if (!nfs4_ol_stateid_unhashed(ost))
6507 list_for_each_entry(lst, &ost->st_locks, st_locks) {
6508 if (lst->st_stateowner == &lo->lo_owner) {
6509 refcount_inc(&lst->st_stid.sc_count);
6510 return lst;
6511 }
fd1fd685 6512 }
fd1fd685
TM
6513 return NULL;
6514}
6515
beeca19c 6516static struct nfs4_ol_stateid *
356a95ec
JL
6517init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
6518 struct nfs4_file *fp, struct inode *inode,
6519 struct nfs4_ol_stateid *open_stp)
1da177e4 6520{
d3b313a4 6521 struct nfs4_client *clp = lo->lo_owner.so_client;
beeca19c 6522 struct nfs4_ol_stateid *retstp;
1da177e4 6523
beeca19c 6524 mutex_init(&stp->st_mutex);
4f34bd05 6525 mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX);
beeca19c
TM
6526retry:
6527 spin_lock(&clp->cl_lock);
a451b123
TM
6528 if (nfs4_ol_stateid_unhashed(open_stp))
6529 goto out_close;
6530 retstp = find_lock_stateid(lo, open_stp);
beeca19c 6531 if (retstp)
a451b123 6532 goto out_found;
a15dfcd5 6533 refcount_inc(&stp->st_stid.sc_count);
3abdb607 6534 stp->st_stid.sc_type = NFS4_LOCK_STID;
b5971afa 6535 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
13cd2184 6536 get_nfs4_file(fp);
11b9164a 6537 stp->st_stid.sc_file = fp;
0997b173 6538 stp->st_access_bmap = 0;
1da177e4 6539 stp->st_deny_bmap = open_stp->st_deny_bmap;
4c4cd222 6540 stp->st_openstp = open_stp;
a451b123 6541 spin_lock(&fp->fi_lock);
3c87b9b7 6542 list_add(&stp->st_locks, &open_stp->st_locks);
1c755dc1 6543 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
1d31a253
TM
6544 list_add(&stp->st_perfile, &fp->fi_stateids);
6545 spin_unlock(&fp->fi_lock);
beeca19c 6546 spin_unlock(&clp->cl_lock);
beeca19c 6547 return stp;
a451b123
TM
6548out_found:
6549 spin_unlock(&clp->cl_lock);
6550 if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) {
6551 nfs4_put_stid(&retstp->st_stid);
6552 goto retry;
6553 }
6554 /* To keep mutex tracking happy */
6555 mutex_unlock(&stp->st_mutex);
6556 return retstp;
6557out_close:
6558 spin_unlock(&clp->cl_lock);
6559 mutex_unlock(&stp->st_mutex);
6560 return NULL;
1da177e4
LT
6561}
6562
356a95ec
JL
6563static struct nfs4_ol_stateid *
6564find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
6565 struct inode *inode, struct nfs4_ol_stateid *ost,
6566 bool *new)
6567{
6568 struct nfs4_stid *ns = NULL;
6569 struct nfs4_ol_stateid *lst;
6570 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6571 struct nfs4_client *clp = oo->oo_owner.so_client;
6572
beeca19c 6573 *new = false;
356a95ec 6574 spin_lock(&clp->cl_lock);
a451b123 6575 lst = find_lock_stateid(lo, ost);
356a95ec 6576 spin_unlock(&clp->cl_lock);
beeca19c
TM
6577 if (lst != NULL) {
6578 if (nfsd4_lock_ol_stateid(lst) == nfs_ok)
6579 goto out;
6580 nfs4_put_stid(&lst->st_stid);
6581 }
6582 ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid);
6583 if (ns == NULL)
6584 return NULL;
6585
6586 lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost);
6587 if (lst == openlockstateid(ns))
6588 *new = true;
6589 else
356a95ec 6590 nfs4_put_stid(ns);
beeca19c 6591out:
356a95ec
JL
6592 return lst;
6593}
c53530da 6594
fd39ca9a 6595static int
1da177e4
LT
6596check_lock_length(u64 offset, u64 length)
6597{
e7969315
KM
6598 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
6599 (length > ~offset)));
1da177e4
LT
6600}
6601
dcef0413 6602static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
0997b173 6603{
11b9164a 6604 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
0997b173 6605
7214e860
JL
6606 lockdep_assert_held(&fp->fi_lock);
6607
82c5ff1b 6608 if (test_access(access, lock_stp))
0997b173 6609 return;
12659651 6610 __nfs4_file_get_access(fp, access);
82c5ff1b 6611 set_access(access, lock_stp);
0997b173
BF
6612}
6613
356a95ec
JL
6614static __be32
6615lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
6616 struct nfs4_ol_stateid *ost,
6617 struct nfsd4_lock *lock,
dd257933 6618 struct nfs4_ol_stateid **plst, bool *new)
64a284d0 6619{
5db1c03f 6620 __be32 status;
11b9164a 6621 struct nfs4_file *fi = ost->st_stid.sc_file;
64a284d0
BF
6622 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
6623 struct nfs4_client *cl = oo->oo_owner.so_client;
2b0143b5 6624 struct inode *inode = d_inode(cstate->current_fh.fh_dentry);
64a284d0 6625 struct nfs4_lockowner *lo;
dd257933 6626 struct nfs4_ol_stateid *lst;
64a284d0
BF
6627 unsigned int strhashval;
6628
c8623999 6629 lo = find_lockowner_str(cl, &lock->lk_new_owner);
c53530da 6630 if (!lo) {
76f6c9e1 6631 strhashval = ownerstr_hashval(&lock->lk_new_owner);
c53530da
JL
6632 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
6633 if (lo == NULL)
6634 return nfserr_jukebox;
6635 } else {
6636 /* with an existing lockowner, seqids must be the same */
5db1c03f 6637 status = nfserr_bad_seqid;
c53530da
JL
6638 if (!cstate->minorversion &&
6639 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5db1c03f 6640 goto out;
64a284d0 6641 }
c53530da 6642
dd257933
JL
6643 lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
6644 if (lst == NULL) {
5db1c03f
JL
6645 status = nfserr_jukebox;
6646 goto out;
64a284d0 6647 }
dd257933 6648
5db1c03f 6649 status = nfs_ok;
dd257933 6650 *plst = lst;
5db1c03f
JL
6651out:
6652 nfs4_put_stateowner(&lo->lo_owner);
6653 return status;
64a284d0
BF
6654}
6655
1da177e4
LT
6656/*
6657 * LOCK operation
6658 */
b37ad28b 6659__be32
ca364317 6660nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 6661 union nfsd4_op_u *u)
1da177e4 6662{
eb69853d 6663 struct nfsd4_lock *lock = &u->lock;
fe0750e5
BF
6664 struct nfs4_openowner *open_sop = NULL;
6665 struct nfs4_lockowner *lock_sop = NULL;
3d0fabd5 6666 struct nfs4_ol_stateid *lock_stp = NULL;
0667b1e9 6667 struct nfs4_ol_stateid *open_stp = NULL;
7214e860 6668 struct nfs4_file *fp;
eb82dd39 6669 struct nfsd_file *nf = NULL;
76d348fa 6670 struct nfsd4_blocked_lock *nbl = NULL;
21179d81
JL
6671 struct file_lock *file_lock = NULL;
6672 struct file_lock *conflock = NULL;
b37ad28b 6673 __be32 status = 0;
b34f27aa 6674 int lkflg;
b8dd7b9a 6675 int err;
5db1c03f 6676 bool new = false;
76d348fa
JL
6677 unsigned char fl_type;
6678 unsigned int fl_flags = FL_POSIX;
3320fef1
SK
6679 struct net *net = SVC_NET(rqstp);
6680 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4
LT
6681
6682 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6683 (long long) lock->lk_offset,
6684 (long long) lock->lk_length);
6685
1da177e4
LT
6686 if (check_lock_length(lock->lk_offset, lock->lk_length))
6687 return nfserr_inval;
6688
ca364317 6689 if ((status = fh_verify(rqstp, &cstate->current_fh,
8837abca 6690 S_IFREG, NFSD_MAY_LOCK))) {
a6f6ef2f
AA
6691 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6692 return status;
6693 }
6694
1da177e4 6695 if (lock->lk_is_new) {
684e5638
BF
6696 if (nfsd4_has_session(cstate))
6697 /* See rfc 5661 18.10.3: given clientid is ignored: */
76f6c9e1 6698 memcpy(&lock->lk_new_clientid,
684e5638
BF
6699 &cstate->session->se_client->cl_clientid,
6700 sizeof(clientid_t));
6701
1da177e4 6702 status = nfserr_stale_clientid;
2c142baa 6703 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
1da177e4 6704 goto out;
1da177e4 6705
1da177e4 6706 /* validate and update open stateid and open seqid */
c0a5d93e 6707 status = nfs4_preprocess_confirmed_seqid_op(cstate,
1da177e4
LT
6708 lock->lk_new_open_seqid,
6709 &lock->lk_new_open_stateid,
3320fef1 6710 &open_stp, nn);
37515177 6711 if (status)
1da177e4 6712 goto out;
feb9dad5 6713 mutex_unlock(&open_stp->st_mutex);
fe0750e5 6714 open_sop = openowner(open_stp->st_stateowner);
b34f27aa 6715 status = nfserr_bad_stateid;
684e5638 6716 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
76f6c9e1 6717 &lock->lk_new_clientid))
b34f27aa 6718 goto out;
64a284d0 6719 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5db1c03f 6720 &lock_stp, &new);
3d0fabd5 6721 } else {
dd453dfd 6722 status = nfs4_preprocess_seqid_op(cstate,
fe0750e5
BF
6723 lock->lk_old_lock_seqid,
6724 &lock->lk_old_lock_stateid,
3320fef1 6725 NFS4_LOCK_STID, &lock_stp, nn);
3d0fabd5 6726 }
e1aaa891
BF
6727 if (status)
6728 goto out;
64a284d0 6729 lock_sop = lockowner(lock_stp->st_stateowner);
1da177e4 6730
b34f27aa
BF
6731 lkflg = setlkflg(lock->lk_type);
6732 status = nfs4_check_openmode(lock_stp, lkflg);
6733 if (status)
6734 goto out;
6735
0dd395dc 6736 status = nfserr_grace;
3320fef1 6737 if (locks_in_grace(net) && !lock->lk_reclaim)
0dd395dc
N
6738 goto out;
6739 status = nfserr_no_grace;
3320fef1 6740 if (!locks_in_grace(net) && lock->lk_reclaim)
0dd395dc
N
6741 goto out;
6742
11b9164a 6743 fp = lock_stp->st_stid.sc_file;
1da177e4 6744 switch (lock->lk_type) {
1da177e4 6745 case NFS4_READW_LT:
76d348fa
JL
6746 if (nfsd4_has_session(cstate))
6747 fl_flags |= FL_SLEEP;
df561f66 6748 fallthrough;
76d348fa 6749 case NFS4_READ_LT:
7214e860 6750 spin_lock(&fp->fi_lock);
eb82dd39
JL
6751 nf = find_readable_file_locked(fp);
6752 if (nf)
0997b173 6753 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
7214e860 6754 spin_unlock(&fp->fi_lock);
76d348fa 6755 fl_type = F_RDLCK;
529d7b2a 6756 break;
1da177e4 6757 case NFS4_WRITEW_LT:
76d348fa
JL
6758 if (nfsd4_has_session(cstate))
6759 fl_flags |= FL_SLEEP;
df561f66 6760 fallthrough;
76d348fa 6761 case NFS4_WRITE_LT:
7214e860 6762 spin_lock(&fp->fi_lock);
eb82dd39
JL
6763 nf = find_writeable_file_locked(fp);
6764 if (nf)
0997b173 6765 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
7214e860 6766 spin_unlock(&fp->fi_lock);
76d348fa 6767 fl_type = F_WRLCK;
529d7b2a 6768 break;
1da177e4
LT
6769 default:
6770 status = nfserr_inval;
6771 goto out;
6772 }
76d348fa 6773
eb82dd39 6774 if (!nf) {
f9d7562f
BF
6775 status = nfserr_openmode;
6776 goto out;
6777 }
aef9583b 6778
76d348fa
JL
6779 nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn);
6780 if (!nbl) {
6781 dprintk("NFSD: %s: unable to allocate block!\n", __func__);
6782 status = nfserr_jukebox;
6783 goto out;
6784 }
6785
6786 file_lock = &nbl->nbl_lock;
6787 file_lock->fl_type = fl_type;
aef9583b 6788 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
21179d81 6789 file_lock->fl_pid = current->tgid;
eb82dd39 6790 file_lock->fl_file = nf->nf_file;
76d348fa 6791 file_lock->fl_flags = fl_flags;
21179d81
JL
6792 file_lock->fl_lmops = &nfsd_posix_mng_ops;
6793 file_lock->fl_start = lock->lk_offset;
6794 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
6795 nfs4_transform_lock_offset(file_lock);
6796
6797 conflock = locks_alloc_lock();
6798 if (!conflock) {
6799 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6800 status = nfserr_jukebox;
6801 goto out;
6802 }
1da177e4 6803
76d348fa 6804 if (fl_flags & FL_SLEEP) {
20b7d86f 6805 nbl->nbl_time = ktime_get_boottime_seconds();
0cc11a61 6806 spin_lock(&nn->blocked_locks_lock);
76d348fa 6807 list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked);
7919d0a2 6808 list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru);
0cc11a61 6809 spin_unlock(&nn->blocked_locks_lock);
76d348fa
JL
6810 }
6811
eb82dd39 6812 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock);
76d348fa 6813 switch (err) {
1da177e4 6814 case 0: /* success! */
9767feb2 6815 nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid);
b8dd7b9a 6816 status = 0;
03f318ca
BF
6817 if (lock->lk_reclaim)
6818 nn->somebody_reclaimed = true;
eb76b3fd 6819 break;
76d348fa
JL
6820 case FILE_LOCK_DEFERRED:
6821 nbl = NULL;
df561f66 6822 fallthrough;
76d348fa 6823 case -EAGAIN: /* conflock holds conflicting lock */
eb76b3fd
AA
6824 status = nfserr_denied;
6825 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
21179d81 6826 nfs4_set_lock_denied(conflock, &lock->lk_denied);
eb76b3fd 6827 break;
76d348fa 6828 case -EDEADLK:
1da177e4 6829 status = nfserr_deadlock;
eb76b3fd 6830 break;
3e772463 6831 default:
fd85b817 6832 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
3e772463 6833 status = nfserrno(err);
eb76b3fd 6834 break;
1da177e4 6835 }
1da177e4 6836out:
76d348fa
JL
6837 if (nbl) {
6838 /* dequeue it if we queued it before */
6839 if (fl_flags & FL_SLEEP) {
0cc11a61 6840 spin_lock(&nn->blocked_locks_lock);
76d348fa 6841 list_del_init(&nbl->nbl_list);
7919d0a2 6842 list_del_init(&nbl->nbl_lru);
0cc11a61 6843 spin_unlock(&nn->blocked_locks_lock);
76d348fa
JL
6844 }
6845 free_blocked_lock(nbl);
6846 }
eb82dd39
JL
6847 if (nf)
6848 nfsd_file_put(nf);
5db1c03f
JL
6849 if (lock_stp) {
6850 /* Bump seqid manually if the 4.0 replay owner is openowner */
6851 if (cstate->replay_owner &&
6852 cstate->replay_owner != &lock_sop->lo_owner &&
6853 seqid_mutating_err(ntohl(status)))
6854 lock_sop->lo_owner.so_seqid++;
6855
6856 /*
6857 * If this is a new, never-before-used stateid, and we are
6858 * returning an error, then just go ahead and release it.
6859 */
25020720 6860 if (status && new)
5db1c03f 6861 release_lock_stateid(lock_stp);
beeca19c
TM
6862
6863 mutex_unlock(&lock_stp->st_mutex);
5db1c03f 6864
3d0fabd5 6865 nfs4_put_stid(&lock_stp->st_stid);
5db1c03f 6866 }
0667b1e9
TM
6867 if (open_stp)
6868 nfs4_put_stid(&open_stp->st_stid);
9411b1d4 6869 nfsd4_bump_seqid(cstate, status);
21179d81
JL
6870 if (conflock)
6871 locks_free_lock(conflock);
1da177e4
LT
6872 return status;
6873}
6874
55ef1274
BF
6875/*
6876 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6877 * so we do a temporary open here just to get an open file to pass to
6878 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6879 * inode operation.)
6880 */
04da6e9d 6881static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
55ef1274 6882{
6b556ca2
JL
6883 struct nfsd_file *nf;
6884 __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf);
04da6e9d 6885 if (!err) {
6b556ca2
JL
6886 err = nfserrno(vfs_test_lock(nf->nf_file, lock));
6887 nfsd_file_put(nf);
04da6e9d 6888 }
55ef1274
BF
6889 return err;
6890}
6891
1da177e4
LT
6892/*
6893 * LOCKT operation
6894 */
b37ad28b 6895__be32
ca364317 6896nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 6897 union nfsd4_op_u *u)
1da177e4 6898{
eb69853d 6899 struct nfsd4_lockt *lockt = &u->lockt;
21179d81 6900 struct file_lock *file_lock = NULL;
5db1c03f 6901 struct nfs4_lockowner *lo = NULL;
b37ad28b 6902 __be32 status;
7f2210fa 6903 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1da177e4 6904
5ccb0066 6905 if (locks_in_grace(SVC_NET(rqstp)))
1da177e4
LT
6906 return nfserr_grace;
6907
6908 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
6909 return nfserr_inval;
6910
9b2ef62b 6911 if (!nfsd4_has_session(cstate)) {
b7342204
OK
6912 status = lookup_clientid(&lockt->lt_clientid, cstate, nn,
6913 false);
9b2ef62b
BF
6914 if (status)
6915 goto out;
6916 }
1da177e4 6917
75c096f7 6918 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
1da177e4 6919 goto out;
1da177e4 6920
21179d81
JL
6921 file_lock = locks_alloc_lock();
6922 if (!file_lock) {
6923 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
6924 status = nfserr_jukebox;
6925 goto out;
6926 }
6cd90662 6927
1da177e4
LT
6928 switch (lockt->lt_type) {
6929 case NFS4_READ_LT:
6930 case NFS4_READW_LT:
21179d81 6931 file_lock->fl_type = F_RDLCK;
f50c9d79 6932 break;
1da177e4
LT
6933 case NFS4_WRITE_LT:
6934 case NFS4_WRITEW_LT:
21179d81 6935 file_lock->fl_type = F_WRLCK;
f50c9d79 6936 break;
1da177e4 6937 default:
2fdada03 6938 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
1da177e4 6939 status = nfserr_inval;
f50c9d79 6940 goto out;
1da177e4
LT
6941 }
6942
c8623999 6943 lo = find_lockowner_str(cstate->clp, &lockt->lt_owner);
fe0750e5 6944 if (lo)
21179d81
JL
6945 file_lock->fl_owner = (fl_owner_t)lo;
6946 file_lock->fl_pid = current->tgid;
6947 file_lock->fl_flags = FL_POSIX;
1da177e4 6948
21179d81
JL
6949 file_lock->fl_start = lockt->lt_offset;
6950 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
1da177e4 6951
21179d81 6952 nfs4_transform_lock_offset(file_lock);
1da177e4 6953
21179d81 6954 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
04da6e9d 6955 if (status)
fd85b817 6956 goto out;
04da6e9d 6957
21179d81 6958 if (file_lock->fl_type != F_UNLCK) {
1da177e4 6959 status = nfserr_denied;
21179d81 6960 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
1da177e4
LT
6961 }
6962out:
5db1c03f
JL
6963 if (lo)
6964 nfs4_put_stateowner(&lo->lo_owner);
21179d81
JL
6965 if (file_lock)
6966 locks_free_lock(file_lock);
1da177e4
LT
6967 return status;
6968}
6969
b37ad28b 6970__be32
ca364317 6971nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
eb69853d 6972 union nfsd4_op_u *u)
1da177e4 6973{
eb69853d 6974 struct nfsd4_locku *locku = &u->locku;
dcef0413 6975 struct nfs4_ol_stateid *stp;
eb82dd39 6976 struct nfsd_file *nf = NULL;
21179d81 6977 struct file_lock *file_lock = NULL;
b37ad28b 6978 __be32 status;
b8dd7b9a 6979 int err;
3320fef1
SK
6980 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
6981
1da177e4
LT
6982 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6983 (long long) locku->lu_offset,
6984 (long long) locku->lu_length);
6985
6986 if (check_lock_length(locku->lu_offset, locku->lu_length))
6987 return nfserr_inval;
6988
9072d5c6 6989 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
3320fef1
SK
6990 &locku->lu_stateid, NFS4_LOCK_STID,
6991 &stp, nn);
9072d5c6 6992 if (status)
1da177e4 6993 goto out;
eb82dd39
JL
6994 nf = find_any_file(stp->st_stid.sc_file);
6995 if (!nf) {
f9d7562f 6996 status = nfserr_lock_range;
858cc573 6997 goto put_stateid;
f9d7562f 6998 }
21179d81
JL
6999 file_lock = locks_alloc_lock();
7000 if (!file_lock) {
7001 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
7002 status = nfserr_jukebox;
eb82dd39 7003 goto put_file;
21179d81 7004 }
6cd90662 7005
21179d81 7006 file_lock->fl_type = F_UNLCK;
aef9583b 7007 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
21179d81 7008 file_lock->fl_pid = current->tgid;
eb82dd39 7009 file_lock->fl_file = nf->nf_file;
21179d81
JL
7010 file_lock->fl_flags = FL_POSIX;
7011 file_lock->fl_lmops = &nfsd_posix_mng_ops;
7012 file_lock->fl_start = locku->lu_offset;
7013
7014 file_lock->fl_end = last_byte_offset(locku->lu_offset,
7015 locku->lu_length);
7016 nfs4_transform_lock_offset(file_lock);
1da177e4 7017
eb82dd39 7018 err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL);
b8dd7b9a 7019 if (err) {
fd85b817 7020 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
1da177e4
LT
7021 goto out_nfserr;
7022 }
9767feb2 7023 nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid);
eb82dd39
JL
7024put_file:
7025 nfsd_file_put(nf);
858cc573 7026put_stateid:
feb9dad5 7027 mutex_unlock(&stp->st_mutex);
858cc573 7028 nfs4_put_stid(&stp->st_stid);
1da177e4 7029out:
9411b1d4 7030 nfsd4_bump_seqid(cstate, status);
21179d81
JL
7031 if (file_lock)
7032 locks_free_lock(file_lock);
1da177e4
LT
7033 return status;
7034
7035out_nfserr:
b8dd7b9a 7036 status = nfserrno(err);
eb82dd39 7037 goto put_file;
1da177e4
LT
7038}
7039
7040/*
7041 * returns
f9c00c3a
JL
7042 * true: locks held by lockowner
7043 * false: no locks held by lockowner
1da177e4 7044 */
f9c00c3a
JL
7045static bool
7046check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
1da177e4 7047{
bd61e0a9 7048 struct file_lock *fl;
f9c00c3a 7049 int status = false;
eb82dd39 7050 struct nfsd_file *nf = find_any_file(fp);
f9c00c3a 7051 struct inode *inode;
bd61e0a9 7052 struct file_lock_context *flctx;
f9c00c3a 7053
eb82dd39 7054 if (!nf) {
f9c00c3a
JL
7055 /* Any valid lock stateid should have some sort of access */
7056 WARN_ON_ONCE(1);
7057 return status;
7058 }
7059
eb82dd39 7060 inode = locks_inode(nf->nf_file);
bd61e0a9
JL
7061 flctx = inode->i_flctx;
7062
7063 if (flctx && !list_empty_careful(&flctx->flc_posix)) {
6109c850 7064 spin_lock(&flctx->flc_lock);
bd61e0a9
JL
7065 list_for_each_entry(fl, &flctx->flc_posix, fl_list) {
7066 if (fl->fl_owner == (fl_owner_t)lowner) {
7067 status = true;
7068 break;
7069 }
796dadfd 7070 }
6109c850 7071 spin_unlock(&flctx->flc_lock);
1da177e4 7072 }
eb82dd39 7073 nfsd_file_put(nf);
1da177e4
LT
7074 return status;
7075}
7076
b37ad28b 7077__be32
b591480b
BF
7078nfsd4_release_lockowner(struct svc_rqst *rqstp,
7079 struct nfsd4_compound_state *cstate,
eb69853d 7080 union nfsd4_op_u *u)
1da177e4 7081{
eb69853d 7082 struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner;
1da177e4 7083 clientid_t *clid = &rlockowner->rl_clientid;
882e9d25
JL
7084 struct nfs4_stateowner *sop;
7085 struct nfs4_lockowner *lo = NULL;
dcef0413 7086 struct nfs4_ol_stateid *stp;
1da177e4 7087 struct xdr_netobj *owner = &rlockowner->rl_owner;
d4f0489f 7088 unsigned int hashval = ownerstr_hashval(owner);
b37ad28b 7089 __be32 status;
7f2210fa 7090 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
c58c6610 7091 struct nfs4_client *clp;
88584818 7092 LIST_HEAD (reaplist);
1da177e4
LT
7093
7094 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7095 clid->cl_boot, clid->cl_id);
7096
b7342204 7097 status = lookup_clientid(clid, cstate, nn, false);
9b2ef62b 7098 if (status)
51f5e783 7099 return status;
9b2ef62b 7100
d4f0489f 7101 clp = cstate->clp;
fd44907c 7102 /* Find the matching lock stateowner */
d4f0489f 7103 spin_lock(&clp->cl_lock);
882e9d25 7104 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
d4f0489f 7105 so_strhash) {
fd44907c 7106
882e9d25
JL
7107 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
7108 continue;
fd44907c 7109
882e9d25
JL
7110 /* see if there are still any locks associated with it */
7111 lo = lockowner(sop);
7112 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
7113 if (check_for_locks(stp->st_stid.sc_file, lo)) {
7114 status = nfserr_locks_held;
7115 spin_unlock(&clp->cl_lock);
51f5e783 7116 return status;
882e9d25 7117 }
5adfd885 7118 }
882e9d25 7119
b5971afa 7120 nfs4_get_stateowner(sop);
882e9d25 7121 break;
1da177e4 7122 }
88584818
CL
7123 if (!lo) {
7124 spin_unlock(&clp->cl_lock);
7125 return status;
7126 }
7127
7128 unhash_lockowner_locked(lo);
7129 while (!list_empty(&lo->lo_owner.so_stateids)) {
7130 stp = list_first_entry(&lo->lo_owner.so_stateids,
7131 struct nfs4_ol_stateid,
7132 st_perstateowner);
7133 WARN_ON(!unhash_lock_stateid(stp));
7134 put_ol_stateid_locked(stp, &reaplist);
7135 }
c58c6610 7136 spin_unlock(&clp->cl_lock);
88584818 7137 free_ol_stateid_reaplist(&reaplist);
68ef3bc3 7138 remove_blocked_locks(lo);
88584818
CL
7139 nfs4_put_stateowner(&lo->lo_owner);
7140
1da177e4
LT
7141 return status;
7142}
7143
7144static inline struct nfs4_client_reclaim *
a55370a3 7145alloc_reclaim(void)
1da177e4 7146{
a55370a3 7147 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
1da177e4
LT
7148}
7149
0ce0c2b5 7150bool
6b189105 7151nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn)
c7b9a459 7152{
0ce0c2b5 7153 struct nfs4_client_reclaim *crp;
c7b9a459 7154
52e19c09 7155 crp = nfsd4_find_reclaim_client(name, nn);
0ce0c2b5 7156 return (crp && crp->cr_clp);
c7b9a459
N
7157}
7158
1da177e4
LT
7159/*
7160 * failure => all reset bets are off, nfserr_no_grace...
6b189105
SM
7161 *
7162 * The caller is responsible for freeing name.data if NULL is returned (it
7163 * will be freed in nfs4_remove_reclaim_record in the normal case).
1da177e4 7164 */
772a9bbb 7165struct nfs4_client_reclaim *
6ee95d1c
SM
7166nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash,
7167 struct nfsd_net *nn)
1da177e4
LT
7168{
7169 unsigned int strhashval;
772a9bbb 7170 struct nfs4_client_reclaim *crp;
1da177e4 7171
dd5e3fbc 7172 trace_nfsd_clid_reclaim(nn, name.len, name.data);
a55370a3 7173 crp = alloc_reclaim();
772a9bbb
JL
7174 if (crp) {
7175 strhashval = clientstr_hashval(name);
7176 INIT_LIST_HEAD(&crp->cr_strhash);
52e19c09 7177 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
6b189105
SM
7178 crp->cr_name.data = name.data;
7179 crp->cr_name.len = name.len;
6ee95d1c
SM
7180 crp->cr_princhash.data = princhash.data;
7181 crp->cr_princhash.len = princhash.len;
0ce0c2b5 7182 crp->cr_clp = NULL;
52e19c09 7183 nn->reclaim_str_hashtbl_size++;
772a9bbb
JL
7184 }
7185 return crp;
1da177e4
LT
7186}
7187
ce30e539 7188void
52e19c09 7189nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
ce30e539
JL
7190{
7191 list_del(&crp->cr_strhash);
6b189105 7192 kfree(crp->cr_name.data);
6ee95d1c 7193 kfree(crp->cr_princhash.data);
ce30e539 7194 kfree(crp);
52e19c09 7195 nn->reclaim_str_hashtbl_size--;
ce30e539
JL
7196}
7197
2a4317c5 7198void
52e19c09 7199nfs4_release_reclaim(struct nfsd_net *nn)
1da177e4
LT
7200{
7201 struct nfs4_client_reclaim *crp = NULL;
7202 int i;
7203
1da177e4 7204 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
52e19c09
SK
7205 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
7206 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
1da177e4 7207 struct nfs4_client_reclaim, cr_strhash);
52e19c09 7208 nfs4_remove_reclaim_record(crp, nn);
1da177e4
LT
7209 }
7210 }
063b0fb9 7211 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
1da177e4
LT
7212}
7213
7214/*
7215 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
2a4317c5 7216struct nfs4_client_reclaim *
6b189105 7217nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn)
1da177e4
LT
7218{
7219 unsigned int strhashval;
1da177e4
LT
7220 struct nfs4_client_reclaim *crp = NULL;
7221
dd5e3fbc 7222 trace_nfsd_clid_find(nn, name.len, name.data);
1da177e4 7223
6b189105 7224 strhashval = clientstr_hashval(name);
52e19c09 7225 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
6b189105 7226 if (compare_blob(&crp->cr_name, &name) == 0) {
1da177e4
LT
7227 return crp;
7228 }
7229 }
7230 return NULL;
7231}
7232
7233/*
7234* Called from OPEN. Look for clientid in reclaim list.
7235*/
b37ad28b 7236__be32
0fe492db
TM
7237nfs4_check_open_reclaim(clientid_t *clid,
7238 struct nfsd4_compound_state *cstate,
7239 struct nfsd_net *nn)
1da177e4 7240{
0fe492db 7241 __be32 status;
a52d726b
JL
7242
7243 /* find clientid in conf_id_hashtbl */
b7342204 7244 status = lookup_clientid(clid, cstate, nn, false);
0fe492db 7245 if (status)
a52d726b
JL
7246 return nfserr_reclaim_bad;
7247
3b3e7b72
JL
7248 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
7249 return nfserr_no_grace;
7250
0fe492db
TM
7251 if (nfsd4_client_record_check(cstate->clp))
7252 return nfserr_reclaim_bad;
7253
7254 return nfs_ok;
1da177e4
LT
7255}
7256
c2f1a551
MS
7257/*
7258 * Since the lifetime of a delegation isn't limited to that of an open, a
7259 * client may quite reasonably hang on to a delegation as long as it has
7260 * the inode cached. This becomes an obvious problem the first time a
7261 * client's inode cache approaches the size of the server's total memory.
7262 *
7263 * For now we avoid this problem by imposing a hard limit on the number
7264 * of delegations, which varies according to the server's memory size.
7265 */
7266static void
7267set_max_delegations(void)
7268{
7269 /*
7270 * Allow at most 4 delegations per megabyte of RAM. Quick
7271 * estimates suggest that in the worst case (where every delegation
7272 * is for a different inode), a delegation could take about 1.5K,
7273 * giving a worst case usage of about 6% of memory.
7274 */
7275 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
7276}
7277
d85ed443 7278static int nfs4_state_create_net(struct net *net)
8daae4dc
SK
7279{
7280 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7281 int i;
7282
6da2ec56
KC
7283 nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7284 sizeof(struct list_head),
7285 GFP_KERNEL);
8daae4dc 7286 if (!nn->conf_id_hashtbl)
382a62e7 7287 goto err;
6da2ec56
KC
7288 nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE,
7289 sizeof(struct list_head),
7290 GFP_KERNEL);
0a7ec377
SK
7291 if (!nn->unconf_id_hashtbl)
7292 goto err_unconf_id;
6da2ec56
KC
7293 nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE,
7294 sizeof(struct list_head),
7295 GFP_KERNEL);
1872de0e
SK
7296 if (!nn->sessionid_hashtbl)
7297 goto err_sessionid;
8daae4dc 7298
382a62e7 7299 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
8daae4dc 7300 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
0a7ec377 7301 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
382a62e7 7302 }
1872de0e
SK
7303 for (i = 0; i < SESSION_HASH_SIZE; i++)
7304 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
382a62e7 7305 nn->conf_name_tree = RB_ROOT;
a99454aa 7306 nn->unconf_name_tree = RB_ROOT;
9cc76801 7307 nn->boot_time = ktime_get_real_seconds();
81833de1
VA
7308 nn->grace_ended = false;
7309 nn->nfsd4_manager.block_opens = true;
7310 INIT_LIST_HEAD(&nn->nfsd4_manager.list);
5ed58bb2 7311 INIT_LIST_HEAD(&nn->client_lru);
73758fed 7312 INIT_LIST_HEAD(&nn->close_lru);
e8c69d17 7313 INIT_LIST_HEAD(&nn->del_recall_lru);
c9a49628 7314 spin_lock_init(&nn->client_lock);
e0639dc5
OK
7315 spin_lock_init(&nn->s2s_cp_lock);
7316 idr_init(&nn->s2s_cp_stateids);
8daae4dc 7317
0cc11a61
JL
7318 spin_lock_init(&nn->blocked_locks_lock);
7319 INIT_LIST_HEAD(&nn->blocked_locks_lru);
7320
09121281 7321 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
d85ed443 7322 get_net(net);
09121281 7323
8daae4dc 7324 return 0;
382a62e7 7325
1872de0e 7326err_sessionid:
9b531137 7327 kfree(nn->unconf_id_hashtbl);
0a7ec377
SK
7328err_unconf_id:
7329 kfree(nn->conf_id_hashtbl);
382a62e7
SK
7330err:
7331 return -ENOMEM;
8daae4dc
SK
7332}
7333
7334static void
4dce0ac9 7335nfs4_state_destroy_net(struct net *net)
8daae4dc
SK
7336{
7337 int i;
7338 struct nfs4_client *clp = NULL;
7339 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
7340
7341 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7342 while (!list_empty(&nn->conf_id_hashtbl[i])) {
7343 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7344 destroy_client(clp);
7345 }
7346 }
a99454aa 7347
68ef3bc3
JL
7348 WARN_ON(!list_empty(&nn->blocked_locks_lru));
7349
2b905635
KM
7350 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
7351 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
7352 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
7353 destroy_client(clp);
7354 }
a99454aa
SK
7355 }
7356
1872de0e 7357 kfree(nn->sessionid_hashtbl);
0a7ec377 7358 kfree(nn->unconf_id_hashtbl);
8daae4dc 7359 kfree(nn->conf_id_hashtbl);
4dce0ac9 7360 put_net(net);
8daae4dc
SK
7361}
7362
f252bc68 7363int
d85ed443 7364nfs4_state_start_net(struct net *net)
ac4d8ff2 7365{
5e1533c7 7366 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
b5a1a81e
BF
7367 int ret;
7368
681370f4 7369 ret = get_nfsdfs(net);
8daae4dc
SK
7370 if (ret)
7371 return ret;
681370f4
BF
7372 ret = nfs4_state_create_net(net);
7373 if (ret) {
7374 mntput(nn->nfsd_mnt);
7375 return ret;
7376 }
d4318acd
JL
7377 locks_start_grace(net, &nn->nfsd4_manager);
7378 nfsd4_client_tracking_init(net);
362063a5
SM
7379 if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0)
7380 goto skip_grace;
20b7d86f 7381 printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n",
7e981a8a 7382 nn->nfsd4_grace, net->ns.inum);
dd5e3fbc 7383 trace_nfsd_grace_start(nn);
5284b44e 7384 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
d85ed443 7385 return 0;
362063a5
SM
7386
7387skip_grace:
7388 printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7389 net->ns.inum);
7390 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ);
7391 nfsd4_end_grace(nn);
7392 return 0;
d85ed443
SK
7393}
7394
7395/* initialization to perform when the nfsd service is started: */
7396
7397int
7398nfs4_state_start(void)
7399{
7400 int ret;
7401
51a54568 7402 laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4");
a6d6b781
JL
7403 if (laundry_wq == NULL) {
7404 ret = -ENOMEM;
a26dd64f 7405 goto out;
a6d6b781 7406 }
b5a1a81e
BF
7407 ret = nfsd4_create_callback_queue();
7408 if (ret)
7409 goto out_free_laundry;
09121281 7410
c2f1a551 7411 set_max_delegations();
b5a1a81e 7412 return 0;
d85ed443 7413
b5a1a81e
BF
7414out_free_laundry:
7415 destroy_workqueue(laundry_wq);
a26dd64f 7416out:
b5a1a81e 7417 return ret;
1da177e4
LT
7418}
7419
f252bc68 7420void
4dce0ac9 7421nfs4_state_shutdown_net(struct net *net)
1da177e4 7422{
1da177e4 7423 struct nfs4_delegation *dp = NULL;
1da177e4 7424 struct list_head *pos, *next, reaplist;
4dce0ac9 7425 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1da177e4 7426
4dce0ac9
SK
7427 cancel_delayed_work_sync(&nn->laundromat_work);
7428 locks_end_grace(&nn->nfsd4_manager);
ac55fdc4 7429
1da177e4 7430 INIT_LIST_HEAD(&reaplist);
cdc97505 7431 spin_lock(&state_lock);
e8c69d17 7432 list_for_each_safe(pos, next, &nn->del_recall_lru) {
1da177e4 7433 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
3fcbbd24 7434 WARN_ON(!unhash_delegation_locked(dp));
42690676 7435 list_add(&dp->dl_recall_lru, &reaplist);
1da177e4 7436 }
cdc97505 7437 spin_unlock(&state_lock);
1da177e4
LT
7438 list_for_each_safe(pos, next, &reaplist) {
7439 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
42690676 7440 list_del_init(&dp->dl_recall_lru);
0af6e690 7441 destroy_unhashed_deleg(dp);
1da177e4
LT
7442 }
7443
3320fef1 7444 nfsd4_client_tracking_exit(net);
4dce0ac9 7445 nfs4_state_destroy_net(net);
681370f4 7446 mntput(nn->nfsd_mnt);
1da177e4
LT
7447}
7448
7449void
7450nfs4_state_shutdown(void)
7451{
5e8d5c29 7452 destroy_workqueue(laundry_wq);
c3935e30 7453 nfsd4_destroy_callback_queue();
1da177e4 7454}
8b70484c
TM
7455
7456static void
7457get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7458{
51100d2b
OK
7459 if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) &&
7460 CURRENT_STATEID(stateid))
37c593c5 7461 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
8b70484c
TM
7462}
7463
7464static void
7465put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
7466{
37c593c5
TM
7467 if (cstate->minorversion) {
7468 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
51100d2b 7469 SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
37c593c5
TM
7470 }
7471}
7472
7473void
7474clear_current_stateid(struct nfsd4_compound_state *cstate)
7475{
51100d2b 7476 CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG);
8b70484c
TM
7477}
7478
62cd4a59
TM
7479/*
7480 * functions to set current state id
7481 */
9428fe1a 7482void
b60e9859
CH
7483nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate,
7484 union nfsd4_op_u *u)
9428fe1a 7485{
b60e9859 7486 put_stateid(cstate, &u->open_downgrade.od_stateid);
9428fe1a
TM
7487}
7488
8b70484c 7489void
b60e9859
CH
7490nfsd4_set_openstateid(struct nfsd4_compound_state *cstate,
7491 union nfsd4_op_u *u)
8b70484c 7492{
b60e9859 7493 put_stateid(cstate, &u->open.op_stateid);
8b70484c
TM
7494}
7495
62cd4a59 7496void
b60e9859
CH
7497nfsd4_set_closestateid(struct nfsd4_compound_state *cstate,
7498 union nfsd4_op_u *u)
62cd4a59 7499{
b60e9859 7500 put_stateid(cstate, &u->close.cl_stateid);
62cd4a59
TM
7501}
7502
7503void
b60e9859
CH
7504nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate,
7505 union nfsd4_op_u *u)
62cd4a59 7506{
b60e9859 7507 put_stateid(cstate, &u->lock.lk_resp_stateid);
62cd4a59
TM
7508}
7509
7510/*
7511 * functions to consume current state id
7512 */
1e97b519 7513
9428fe1a 7514void
57832e7b
CH
7515nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate,
7516 union nfsd4_op_u *u)
9428fe1a 7517{
57832e7b 7518 get_stateid(cstate, &u->open_downgrade.od_stateid);
9428fe1a
TM
7519}
7520
7521void
57832e7b
CH
7522nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate,
7523 union nfsd4_op_u *u)
9428fe1a 7524{
57832e7b 7525 get_stateid(cstate, &u->delegreturn.dr_stateid);
9428fe1a
TM
7526}
7527
1e97b519 7528void
57832e7b
CH
7529nfsd4_get_freestateid(struct nfsd4_compound_state *cstate,
7530 union nfsd4_op_u *u)
1e97b519 7531{
57832e7b 7532 get_stateid(cstate, &u->free_stateid.fr_stateid);
1e97b519
TM
7533}
7534
7535void
57832e7b
CH
7536nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate,
7537 union nfsd4_op_u *u)
1e97b519 7538{
57832e7b 7539 get_stateid(cstate, &u->setattr.sa_stateid);
1e97b519
TM
7540}
7541
8b70484c 7542void
57832e7b
CH
7543nfsd4_get_closestateid(struct nfsd4_compound_state *cstate,
7544 union nfsd4_op_u *u)
8b70484c 7545{
57832e7b 7546 get_stateid(cstate, &u->close.cl_stateid);
8b70484c
TM
7547}
7548
7549void
57832e7b
CH
7550nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate,
7551 union nfsd4_op_u *u)
8b70484c 7552{
57832e7b 7553 get_stateid(cstate, &u->locku.lu_stateid);
8b70484c 7554}
30813e27
TM
7555
7556void
57832e7b
CH
7557nfsd4_get_readstateid(struct nfsd4_compound_state *cstate,
7558 union nfsd4_op_u *u)
30813e27 7559{
57832e7b 7560 get_stateid(cstate, &u->read.rd_stateid);
30813e27
TM
7561}
7562
7563void
57832e7b
CH
7564nfsd4_get_writestateid(struct nfsd4_compound_state *cstate,
7565 union nfsd4_op_u *u)
30813e27 7566{
57832e7b 7567 get_stateid(cstate, &u->write.wr_stateid);
30813e27 7568}