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