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