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