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