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