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