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