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