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