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