Bluetooth: vhci: Fix race at creating hci device
[linux/fpc-iii.git] / fs / nfsd / nfs4state.c
blobb6c3a87923582ed63d011a09372f57f87246760b
1 /*
2 * Copyright (c) 2001 The Regents of the University of Michigan.
3 * All rights reserved.
5 * Kendrick Smith <kmsmith@umich.edu>
6 * Andy Adamson <kandros@umich.edu>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
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.
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/hash.h>
45 #include "xdr4.h"
46 #include "xdr4cb.h"
47 #include "vfs.h"
48 #include "current_stateid.h"
50 #include "netns.h"
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid = {
56 .si_generation = ~0,
57 .si_opaque = all_ones,
59 static const stateid_t zero_stateid = {
60 /* all fields zero */
62 static const stateid_t currentstateid = {
63 .si_generation = 1,
66 static u64 current_sessionid = 1;
68 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
69 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
70 #define CURRENT_STATEID(stateid) (!memcmp((stateid), &currentstateid, sizeof(stateid_t)))
72 /* forward declarations */
73 static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner);
74 static void nfs4_free_ol_stateid(struct nfs4_stid *stid);
76 /* Locking: */
79 * Currently used for the del_recall_lru and file hash table. In an
80 * effort to decrease the scope of the client_mutex, this spinlock may
81 * eventually cover more:
83 static DEFINE_SPINLOCK(state_lock);
86 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
87 * the refcount on the open stateid to drop.
89 static DECLARE_WAIT_QUEUE_HEAD(close_wq);
91 static struct kmem_cache *openowner_slab;
92 static struct kmem_cache *lockowner_slab;
93 static struct kmem_cache *file_slab;
94 static struct kmem_cache *stateid_slab;
95 static struct kmem_cache *deleg_slab;
97 static void free_session(struct nfsd4_session *);
99 static struct nfsd4_callback_ops nfsd4_cb_recall_ops;
101 static bool is_session_dead(struct nfsd4_session *ses)
103 return ses->se_flags & NFS4_SESSION_DEAD;
106 static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me)
108 if (atomic_read(&ses->se_ref) > ref_held_by_me)
109 return nfserr_jukebox;
110 ses->se_flags |= NFS4_SESSION_DEAD;
111 return nfs_ok;
114 static bool is_client_expired(struct nfs4_client *clp)
116 return clp->cl_time == 0;
119 static __be32 get_client_locked(struct nfs4_client *clp)
121 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
123 lockdep_assert_held(&nn->client_lock);
125 if (is_client_expired(clp))
126 return nfserr_expired;
127 atomic_inc(&clp->cl_refcount);
128 return nfs_ok;
131 /* must be called under the client_lock */
132 static inline void
133 renew_client_locked(struct nfs4_client *clp)
135 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
137 if (is_client_expired(clp)) {
138 WARN_ON(1);
139 printk("%s: client (clientid %08x/%08x) already expired\n",
140 __func__,
141 clp->cl_clientid.cl_boot,
142 clp->cl_clientid.cl_id);
143 return;
146 dprintk("renewing client (clientid %08x/%08x)\n",
147 clp->cl_clientid.cl_boot,
148 clp->cl_clientid.cl_id);
149 list_move_tail(&clp->cl_lru, &nn->client_lru);
150 clp->cl_time = get_seconds();
153 static inline void
154 renew_client(struct nfs4_client *clp)
156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
158 spin_lock(&nn->client_lock);
159 renew_client_locked(clp);
160 spin_unlock(&nn->client_lock);
163 static void put_client_renew_locked(struct nfs4_client *clp)
165 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
167 lockdep_assert_held(&nn->client_lock);
169 if (!atomic_dec_and_test(&clp->cl_refcount))
170 return;
171 if (!is_client_expired(clp))
172 renew_client_locked(clp);
175 static void put_client_renew(struct nfs4_client *clp)
177 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
179 if (!atomic_dec_and_lock(&clp->cl_refcount, &nn->client_lock))
180 return;
181 if (!is_client_expired(clp))
182 renew_client_locked(clp);
183 spin_unlock(&nn->client_lock);
186 static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses)
188 __be32 status;
190 if (is_session_dead(ses))
191 return nfserr_badsession;
192 status = get_client_locked(ses->se_client);
193 if (status)
194 return status;
195 atomic_inc(&ses->se_ref);
196 return nfs_ok;
199 static void nfsd4_put_session_locked(struct nfsd4_session *ses)
201 struct nfs4_client *clp = ses->se_client;
202 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
204 lockdep_assert_held(&nn->client_lock);
206 if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses))
207 free_session(ses);
208 put_client_renew_locked(clp);
211 static void nfsd4_put_session(struct nfsd4_session *ses)
213 struct nfs4_client *clp = ses->se_client;
214 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
216 spin_lock(&nn->client_lock);
217 nfsd4_put_session_locked(ses);
218 spin_unlock(&nn->client_lock);
221 static inline struct nfs4_stateowner *
222 nfs4_get_stateowner(struct nfs4_stateowner *sop)
224 atomic_inc(&sop->so_count);
225 return sop;
228 static int
229 same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner)
231 return (sop->so_owner.len == owner->len) &&
232 0 == memcmp(sop->so_owner.data, owner->data, owner->len);
235 static struct nfs4_openowner *
236 find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open,
237 struct nfs4_client *clp)
239 struct nfs4_stateowner *so;
241 lockdep_assert_held(&clp->cl_lock);
243 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval],
244 so_strhash) {
245 if (!so->so_is_open_owner)
246 continue;
247 if (same_owner_str(so, &open->op_owner))
248 return openowner(nfs4_get_stateowner(so));
250 return NULL;
253 static struct nfs4_openowner *
254 find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open,
255 struct nfs4_client *clp)
257 struct nfs4_openowner *oo;
259 spin_lock(&clp->cl_lock);
260 oo = find_openstateowner_str_locked(hashval, open, clp);
261 spin_unlock(&clp->cl_lock);
262 return oo;
265 static inline u32
266 opaque_hashval(const void *ptr, int nbytes)
268 unsigned char *cptr = (unsigned char *) ptr;
270 u32 x = 0;
271 while (nbytes--) {
272 x *= 37;
273 x += *cptr++;
275 return x;
278 static void nfsd4_free_file(struct nfs4_file *f)
280 kmem_cache_free(file_slab, f);
283 static inline void
284 put_nfs4_file(struct nfs4_file *fi)
286 might_lock(&state_lock);
288 if (atomic_dec_and_lock(&fi->fi_ref, &state_lock)) {
289 hlist_del(&fi->fi_hash);
290 spin_unlock(&state_lock);
291 nfsd4_free_file(fi);
295 static inline void
296 get_nfs4_file(struct nfs4_file *fi)
298 atomic_inc(&fi->fi_ref);
301 static struct file *
302 __nfs4_get_fd(struct nfs4_file *f, int oflag)
304 if (f->fi_fds[oflag])
305 return get_file(f->fi_fds[oflag]);
306 return NULL;
309 static struct file *
310 find_writeable_file_locked(struct nfs4_file *f)
312 struct file *ret;
314 lockdep_assert_held(&f->fi_lock);
316 ret = __nfs4_get_fd(f, O_WRONLY);
317 if (!ret)
318 ret = __nfs4_get_fd(f, O_RDWR);
319 return ret;
322 static struct file *
323 find_writeable_file(struct nfs4_file *f)
325 struct file *ret;
327 spin_lock(&f->fi_lock);
328 ret = find_writeable_file_locked(f);
329 spin_unlock(&f->fi_lock);
331 return ret;
334 static struct file *find_readable_file_locked(struct nfs4_file *f)
336 struct file *ret;
338 lockdep_assert_held(&f->fi_lock);
340 ret = __nfs4_get_fd(f, O_RDONLY);
341 if (!ret)
342 ret = __nfs4_get_fd(f, O_RDWR);
343 return ret;
346 static struct file *
347 find_readable_file(struct nfs4_file *f)
349 struct file *ret;
351 spin_lock(&f->fi_lock);
352 ret = find_readable_file_locked(f);
353 spin_unlock(&f->fi_lock);
355 return ret;
358 static struct file *
359 find_any_file(struct nfs4_file *f)
361 struct file *ret;
363 spin_lock(&f->fi_lock);
364 ret = __nfs4_get_fd(f, O_RDWR);
365 if (!ret) {
366 ret = __nfs4_get_fd(f, O_WRONLY);
367 if (!ret)
368 ret = __nfs4_get_fd(f, O_RDONLY);
370 spin_unlock(&f->fi_lock);
371 return ret;
374 static atomic_long_t num_delegations;
375 unsigned long max_delegations;
378 * Open owner state (share locks)
381 /* hash tables for lock and open owners */
382 #define OWNER_HASH_BITS 8
383 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
384 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
386 static unsigned int ownerstr_hashval(struct xdr_netobj *ownername)
388 unsigned int ret;
390 ret = opaque_hashval(ownername->data, ownername->len);
391 return ret & OWNER_HASH_MASK;
394 /* hash table for nfs4_file */
395 #define FILE_HASH_BITS 8
396 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
398 static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh)
400 return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0);
403 static unsigned int file_hashval(struct knfsd_fh *fh)
405 return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1);
408 static bool nfsd_fh_match(struct knfsd_fh *fh1, struct knfsd_fh *fh2)
410 return fh1->fh_size == fh2->fh_size &&
411 !memcmp(fh1->fh_base.fh_pad,
412 fh2->fh_base.fh_pad,
413 fh1->fh_size);
416 static struct hlist_head file_hashtbl[FILE_HASH_SIZE];
418 static void
419 __nfs4_file_get_access(struct nfs4_file *fp, u32 access)
421 lockdep_assert_held(&fp->fi_lock);
423 if (access & NFS4_SHARE_ACCESS_WRITE)
424 atomic_inc(&fp->fi_access[O_WRONLY]);
425 if (access & NFS4_SHARE_ACCESS_READ)
426 atomic_inc(&fp->fi_access[O_RDONLY]);
429 static __be32
430 nfs4_file_get_access(struct nfs4_file *fp, u32 access)
432 lockdep_assert_held(&fp->fi_lock);
434 /* Does this access mode make sense? */
435 if (access & ~NFS4_SHARE_ACCESS_BOTH)
436 return nfserr_inval;
438 /* Does it conflict with a deny mode already set? */
439 if ((access & fp->fi_share_deny) != 0)
440 return nfserr_share_denied;
442 __nfs4_file_get_access(fp, access);
443 return nfs_ok;
446 static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny)
448 /* Common case is that there is no deny mode. */
449 if (deny) {
450 /* Does this deny mode make sense? */
451 if (deny & ~NFS4_SHARE_DENY_BOTH)
452 return nfserr_inval;
454 if ((deny & NFS4_SHARE_DENY_READ) &&
455 atomic_read(&fp->fi_access[O_RDONLY]))
456 return nfserr_share_denied;
458 if ((deny & NFS4_SHARE_DENY_WRITE) &&
459 atomic_read(&fp->fi_access[O_WRONLY]))
460 return nfserr_share_denied;
462 return nfs_ok;
465 static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag)
467 might_lock(&fp->fi_lock);
469 if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) {
470 struct file *f1 = NULL;
471 struct file *f2 = NULL;
473 swap(f1, fp->fi_fds[oflag]);
474 if (atomic_read(&fp->fi_access[1 - oflag]) == 0)
475 swap(f2, fp->fi_fds[O_RDWR]);
476 spin_unlock(&fp->fi_lock);
477 if (f1)
478 fput(f1);
479 if (f2)
480 fput(f2);
484 static void nfs4_file_put_access(struct nfs4_file *fp, u32 access)
486 WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH);
488 if (access & NFS4_SHARE_ACCESS_WRITE)
489 __nfs4_file_put_access(fp, O_WRONLY);
490 if (access & NFS4_SHARE_ACCESS_READ)
491 __nfs4_file_put_access(fp, O_RDONLY);
494 static struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl,
495 struct kmem_cache *slab)
497 struct nfs4_stid *stid;
498 int new_id;
500 stid = kmem_cache_zalloc(slab, GFP_KERNEL);
501 if (!stid)
502 return NULL;
504 idr_preload(GFP_KERNEL);
505 spin_lock(&cl->cl_lock);
506 new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 0, 0, GFP_NOWAIT);
507 spin_unlock(&cl->cl_lock);
508 idr_preload_end();
509 if (new_id < 0)
510 goto out_free;
511 stid->sc_client = cl;
512 stid->sc_stateid.si_opaque.so_id = new_id;
513 stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid;
514 /* Will be incremented before return to client: */
515 atomic_set(&stid->sc_count, 1);
518 * It shouldn't be a problem to reuse an opaque stateid value.
519 * I don't think it is for 4.1. But with 4.0 I worry that, for
520 * example, a stray write retransmission could be accepted by
521 * the server when it should have been rejected. Therefore,
522 * adopt a trick from the sctp code to attempt to maximize the
523 * amount of time until an id is reused, by ensuring they always
524 * "increase" (mod INT_MAX):
526 return stid;
527 out_free:
528 kmem_cache_free(slab, stid);
529 return NULL;
532 static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp)
534 struct nfs4_stid *stid;
535 struct nfs4_ol_stateid *stp;
537 stid = nfs4_alloc_stid(clp, stateid_slab);
538 if (!stid)
539 return NULL;
541 stp = openlockstateid(stid);
542 stp->st_stid.sc_free = nfs4_free_ol_stateid;
543 return stp;
546 static void nfs4_free_deleg(struct nfs4_stid *stid)
548 kmem_cache_free(deleg_slab, stid);
549 atomic_long_dec(&num_delegations);
553 * When we recall a delegation, we should be careful not to hand it
554 * out again straight away.
555 * To ensure this we keep a pair of bloom filters ('new' and 'old')
556 * in which the filehandles of recalled delegations are "stored".
557 * If a filehandle appear in either filter, a delegation is blocked.
558 * When a delegation is recalled, the filehandle is stored in the "new"
559 * filter.
560 * Every 30 seconds we swap the filters and clear the "new" one,
561 * unless both are empty of course.
563 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
564 * low 3 bytes as hash-table indices.
566 * 'blocked_delegations_lock', which is always taken in block_delegations(),
567 * is used to manage concurrent access. Testing does not need the lock
568 * except when swapping the two filters.
570 static DEFINE_SPINLOCK(blocked_delegations_lock);
571 static struct bloom_pair {
572 int entries, old_entries;
573 time_t swap_time;
574 int new; /* index into 'set' */
575 DECLARE_BITMAP(set[2], 256);
576 } blocked_delegations;
578 static int delegation_blocked(struct knfsd_fh *fh)
580 u32 hash;
581 struct bloom_pair *bd = &blocked_delegations;
583 if (bd->entries == 0)
584 return 0;
585 if (seconds_since_boot() - bd->swap_time > 30) {
586 spin_lock(&blocked_delegations_lock);
587 if (seconds_since_boot() - bd->swap_time > 30) {
588 bd->entries -= bd->old_entries;
589 bd->old_entries = bd->entries;
590 memset(bd->set[bd->new], 0,
591 sizeof(bd->set[0]));
592 bd->new = 1-bd->new;
593 bd->swap_time = seconds_since_boot();
595 spin_unlock(&blocked_delegations_lock);
597 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
598 if (test_bit(hash&255, bd->set[0]) &&
599 test_bit((hash>>8)&255, bd->set[0]) &&
600 test_bit((hash>>16)&255, bd->set[0]))
601 return 1;
603 if (test_bit(hash&255, bd->set[1]) &&
604 test_bit((hash>>8)&255, bd->set[1]) &&
605 test_bit((hash>>16)&255, bd->set[1]))
606 return 1;
608 return 0;
611 static void block_delegations(struct knfsd_fh *fh)
613 u32 hash;
614 struct bloom_pair *bd = &blocked_delegations;
616 hash = arch_fast_hash(&fh->fh_base, fh->fh_size, 0);
618 spin_lock(&blocked_delegations_lock);
619 __set_bit(hash&255, bd->set[bd->new]);
620 __set_bit((hash>>8)&255, bd->set[bd->new]);
621 __set_bit((hash>>16)&255, bd->set[bd->new]);
622 if (bd->entries == 0)
623 bd->swap_time = seconds_since_boot();
624 bd->entries += 1;
625 spin_unlock(&blocked_delegations_lock);
628 static struct nfs4_delegation *
629 alloc_init_deleg(struct nfs4_client *clp, struct svc_fh *current_fh)
631 struct nfs4_delegation *dp;
632 long n;
634 dprintk("NFSD alloc_init_deleg\n");
635 n = atomic_long_inc_return(&num_delegations);
636 if (n < 0 || n > max_delegations)
637 goto out_dec;
638 if (delegation_blocked(&current_fh->fh_handle))
639 goto out_dec;
640 dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab));
641 if (dp == NULL)
642 goto out_dec;
644 dp->dl_stid.sc_free = nfs4_free_deleg;
646 * delegation seqid's are never incremented. The 4.1 special
647 * meaning of seqid 0 isn't meaningful, really, but let's avoid
648 * 0 anyway just for consistency and use 1:
650 dp->dl_stid.sc_stateid.si_generation = 1;
651 INIT_LIST_HEAD(&dp->dl_perfile);
652 INIT_LIST_HEAD(&dp->dl_perclnt);
653 INIT_LIST_HEAD(&dp->dl_recall_lru);
654 dp->dl_type = NFS4_OPEN_DELEGATE_READ;
655 dp->dl_retries = 1;
656 nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client,
657 &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL);
658 return dp;
659 out_dec:
660 atomic_long_dec(&num_delegations);
661 return NULL;
664 void
665 nfs4_put_stid(struct nfs4_stid *s)
667 struct nfs4_file *fp = s->sc_file;
668 struct nfs4_client *clp = s->sc_client;
670 might_lock(&clp->cl_lock);
672 if (!atomic_dec_and_lock(&s->sc_count, &clp->cl_lock)) {
673 wake_up_all(&close_wq);
674 return;
676 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
677 spin_unlock(&clp->cl_lock);
678 s->sc_free(s);
679 if (fp)
680 put_nfs4_file(fp);
683 static void nfs4_put_deleg_lease(struct nfs4_file *fp)
685 struct file *filp = NULL;
687 spin_lock(&fp->fi_lock);
688 if (fp->fi_deleg_file && atomic_dec_and_test(&fp->fi_delegees))
689 swap(filp, fp->fi_deleg_file);
690 spin_unlock(&fp->fi_lock);
692 if (filp) {
693 vfs_setlease(filp, F_UNLCK, NULL, NULL);
694 fput(filp);
698 static void unhash_stid(struct nfs4_stid *s)
700 s->sc_type = 0;
703 static void
704 hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp)
706 lockdep_assert_held(&state_lock);
707 lockdep_assert_held(&fp->fi_lock);
709 atomic_inc(&dp->dl_stid.sc_count);
710 dp->dl_stid.sc_type = NFS4_DELEG_STID;
711 list_add(&dp->dl_perfile, &fp->fi_delegations);
712 list_add(&dp->dl_perclnt, &dp->dl_stid.sc_client->cl_delegations);
715 static void
716 unhash_delegation_locked(struct nfs4_delegation *dp)
718 struct nfs4_file *fp = dp->dl_stid.sc_file;
720 lockdep_assert_held(&state_lock);
722 dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID;
723 /* Ensure that deleg break won't try to requeue it */
724 ++dp->dl_time;
725 spin_lock(&fp->fi_lock);
726 list_del_init(&dp->dl_perclnt);
727 list_del_init(&dp->dl_recall_lru);
728 list_del_init(&dp->dl_perfile);
729 spin_unlock(&fp->fi_lock);
732 static void destroy_delegation(struct nfs4_delegation *dp)
734 spin_lock(&state_lock);
735 unhash_delegation_locked(dp);
736 spin_unlock(&state_lock);
737 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
738 nfs4_put_stid(&dp->dl_stid);
741 static void revoke_delegation(struct nfs4_delegation *dp)
743 struct nfs4_client *clp = dp->dl_stid.sc_client;
745 WARN_ON(!list_empty(&dp->dl_recall_lru));
747 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
749 if (clp->cl_minorversion == 0)
750 nfs4_put_stid(&dp->dl_stid);
751 else {
752 dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID;
753 spin_lock(&clp->cl_lock);
754 list_add(&dp->dl_recall_lru, &clp->cl_revoked);
755 spin_unlock(&clp->cl_lock);
760 * SETCLIENTID state
763 static unsigned int clientid_hashval(u32 id)
765 return id & CLIENT_HASH_MASK;
768 static unsigned int clientstr_hashval(const char *name)
770 return opaque_hashval(name, 8) & CLIENT_HASH_MASK;
774 * We store the NONE, READ, WRITE, and BOTH bits separately in the
775 * st_{access,deny}_bmap field of the stateid, in order to track not
776 * only what share bits are currently in force, but also what
777 * combinations of share bits previous opens have used. This allows us
778 * to enforce the recommendation of rfc 3530 14.2.19 that the server
779 * return an error if the client attempt to downgrade to a combination
780 * of share bits not explicable by closing some of its previous opens.
782 * XXX: This enforcement is actually incomplete, since we don't keep
783 * track of access/deny bit combinations; so, e.g., we allow:
785 * OPEN allow read, deny write
786 * OPEN allow both, deny none
787 * DOWNGRADE allow read, deny none
789 * which we should reject.
791 static unsigned int
792 bmap_to_share_mode(unsigned long bmap) {
793 int i;
794 unsigned int access = 0;
796 for (i = 1; i < 4; i++) {
797 if (test_bit(i, &bmap))
798 access |= i;
800 return access;
803 /* set share access for a given stateid */
804 static inline void
805 set_access(u32 access, struct nfs4_ol_stateid *stp)
807 unsigned char mask = 1 << access;
809 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
810 stp->st_access_bmap |= mask;
813 /* clear share access for a given stateid */
814 static inline void
815 clear_access(u32 access, struct nfs4_ol_stateid *stp)
817 unsigned char mask = 1 << access;
819 WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH);
820 stp->st_access_bmap &= ~mask;
823 /* test whether a given stateid has access */
824 static inline bool
825 test_access(u32 access, struct nfs4_ol_stateid *stp)
827 unsigned char mask = 1 << access;
829 return (bool)(stp->st_access_bmap & mask);
832 /* set share deny for a given stateid */
833 static inline void
834 set_deny(u32 deny, struct nfs4_ol_stateid *stp)
836 unsigned char mask = 1 << deny;
838 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
839 stp->st_deny_bmap |= mask;
842 /* clear share deny for a given stateid */
843 static inline void
844 clear_deny(u32 deny, struct nfs4_ol_stateid *stp)
846 unsigned char mask = 1 << deny;
848 WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH);
849 stp->st_deny_bmap &= ~mask;
852 /* test whether a given stateid is denying specific access */
853 static inline bool
854 test_deny(u32 deny, struct nfs4_ol_stateid *stp)
856 unsigned char mask = 1 << deny;
858 return (bool)(stp->st_deny_bmap & mask);
861 static int nfs4_access_to_omode(u32 access)
863 switch (access & NFS4_SHARE_ACCESS_BOTH) {
864 case NFS4_SHARE_ACCESS_READ:
865 return O_RDONLY;
866 case NFS4_SHARE_ACCESS_WRITE:
867 return O_WRONLY;
868 case NFS4_SHARE_ACCESS_BOTH:
869 return O_RDWR;
871 WARN_ON_ONCE(1);
872 return O_RDONLY;
876 * A stateid that had a deny mode associated with it is being released
877 * or downgraded. Recalculate the deny mode on the file.
879 static void
880 recalculate_deny_mode(struct nfs4_file *fp)
882 struct nfs4_ol_stateid *stp;
884 spin_lock(&fp->fi_lock);
885 fp->fi_share_deny = 0;
886 list_for_each_entry(stp, &fp->fi_stateids, st_perfile)
887 fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap);
888 spin_unlock(&fp->fi_lock);
891 static void
892 reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp)
894 int i;
895 bool change = false;
897 for (i = 1; i < 4; i++) {
898 if ((i & deny) != i) {
899 change = true;
900 clear_deny(i, stp);
904 /* Recalculate per-file deny mode if there was a change */
905 if (change)
906 recalculate_deny_mode(stp->st_stid.sc_file);
909 /* release all access and file references for a given stateid */
910 static void
911 release_all_access(struct nfs4_ol_stateid *stp)
913 int i;
914 struct nfs4_file *fp = stp->st_stid.sc_file;
916 if (fp && stp->st_deny_bmap != 0)
917 recalculate_deny_mode(fp);
919 for (i = 1; i < 4; i++) {
920 if (test_access(i, stp))
921 nfs4_file_put_access(stp->st_stid.sc_file, i);
922 clear_access(i, stp);
926 static void nfs4_put_stateowner(struct nfs4_stateowner *sop)
928 struct nfs4_client *clp = sop->so_client;
930 might_lock(&clp->cl_lock);
932 if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock))
933 return;
934 sop->so_ops->so_unhash(sop);
935 spin_unlock(&clp->cl_lock);
936 kfree(sop->so_owner.data);
937 sop->so_ops->so_free(sop);
940 static void unhash_ol_stateid(struct nfs4_ol_stateid *stp)
942 struct nfs4_file *fp = stp->st_stid.sc_file;
944 lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock);
946 spin_lock(&fp->fi_lock);
947 list_del(&stp->st_perfile);
948 spin_unlock(&fp->fi_lock);
949 list_del(&stp->st_perstateowner);
952 static void nfs4_free_ol_stateid(struct nfs4_stid *stid)
954 struct nfs4_ol_stateid *stp = openlockstateid(stid);
956 release_all_access(stp);
957 if (stp->st_stateowner)
958 nfs4_put_stateowner(stp->st_stateowner);
959 kmem_cache_free(stateid_slab, stid);
962 static void nfs4_free_lock_stateid(struct nfs4_stid *stid)
964 struct nfs4_ol_stateid *stp = openlockstateid(stid);
965 struct nfs4_lockowner *lo = lockowner(stp->st_stateowner);
966 struct file *file;
968 file = find_any_file(stp->st_stid.sc_file);
969 if (file)
970 filp_close(file, (fl_owner_t)lo);
971 nfs4_free_ol_stateid(stid);
975 * Put the persistent reference to an already unhashed generic stateid, while
976 * holding the cl_lock. If it's the last reference, then put it onto the
977 * reaplist for later destruction.
979 static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp,
980 struct list_head *reaplist)
982 struct nfs4_stid *s = &stp->st_stid;
983 struct nfs4_client *clp = s->sc_client;
985 lockdep_assert_held(&clp->cl_lock);
987 WARN_ON_ONCE(!list_empty(&stp->st_locks));
989 if (!atomic_dec_and_test(&s->sc_count)) {
990 wake_up_all(&close_wq);
991 return;
994 idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id);
995 list_add(&stp->st_locks, reaplist);
998 static void unhash_lock_stateid(struct nfs4_ol_stateid *stp)
1000 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1002 lockdep_assert_held(&oo->oo_owner.so_client->cl_lock);
1004 list_del_init(&stp->st_locks);
1005 unhash_ol_stateid(stp);
1006 unhash_stid(&stp->st_stid);
1009 static void release_lock_stateid(struct nfs4_ol_stateid *stp)
1011 struct nfs4_openowner *oo = openowner(stp->st_openstp->st_stateowner);
1013 spin_lock(&oo->oo_owner.so_client->cl_lock);
1014 unhash_lock_stateid(stp);
1015 spin_unlock(&oo->oo_owner.so_client->cl_lock);
1016 nfs4_put_stid(&stp->st_stid);
1019 static void unhash_lockowner_locked(struct nfs4_lockowner *lo)
1021 struct nfs4_client *clp = lo->lo_owner.so_client;
1023 lockdep_assert_held(&clp->cl_lock);
1025 list_del_init(&lo->lo_owner.so_strhash);
1029 * Free a list of generic stateids that were collected earlier after being
1030 * fully unhashed.
1032 static void
1033 free_ol_stateid_reaplist(struct list_head *reaplist)
1035 struct nfs4_ol_stateid *stp;
1036 struct nfs4_file *fp;
1038 might_sleep();
1040 while (!list_empty(reaplist)) {
1041 stp = list_first_entry(reaplist, struct nfs4_ol_stateid,
1042 st_locks);
1043 list_del(&stp->st_locks);
1044 fp = stp->st_stid.sc_file;
1045 stp->st_stid.sc_free(&stp->st_stid);
1046 if (fp)
1047 put_nfs4_file(fp);
1051 static void release_lockowner(struct nfs4_lockowner *lo)
1053 struct nfs4_client *clp = lo->lo_owner.so_client;
1054 struct nfs4_ol_stateid *stp;
1055 struct list_head reaplist;
1057 INIT_LIST_HEAD(&reaplist);
1059 spin_lock(&clp->cl_lock);
1060 unhash_lockowner_locked(lo);
1061 while (!list_empty(&lo->lo_owner.so_stateids)) {
1062 stp = list_first_entry(&lo->lo_owner.so_stateids,
1063 struct nfs4_ol_stateid, st_perstateowner);
1064 unhash_lock_stateid(stp);
1065 put_ol_stateid_locked(stp, &reaplist);
1067 spin_unlock(&clp->cl_lock);
1068 free_ol_stateid_reaplist(&reaplist);
1069 nfs4_put_stateowner(&lo->lo_owner);
1072 static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp,
1073 struct list_head *reaplist)
1075 struct nfs4_ol_stateid *stp;
1077 while (!list_empty(&open_stp->st_locks)) {
1078 stp = list_entry(open_stp->st_locks.next,
1079 struct nfs4_ol_stateid, st_locks);
1080 unhash_lock_stateid(stp);
1081 put_ol_stateid_locked(stp, reaplist);
1085 static void unhash_open_stateid(struct nfs4_ol_stateid *stp,
1086 struct list_head *reaplist)
1088 lockdep_assert_held(&stp->st_stid.sc_client->cl_lock);
1090 unhash_ol_stateid(stp);
1091 release_open_stateid_locks(stp, reaplist);
1094 static void release_open_stateid(struct nfs4_ol_stateid *stp)
1096 LIST_HEAD(reaplist);
1098 spin_lock(&stp->st_stid.sc_client->cl_lock);
1099 unhash_open_stateid(stp, &reaplist);
1100 put_ol_stateid_locked(stp, &reaplist);
1101 spin_unlock(&stp->st_stid.sc_client->cl_lock);
1102 free_ol_stateid_reaplist(&reaplist);
1105 static void unhash_openowner_locked(struct nfs4_openowner *oo)
1107 struct nfs4_client *clp = oo->oo_owner.so_client;
1109 lockdep_assert_held(&clp->cl_lock);
1111 list_del_init(&oo->oo_owner.so_strhash);
1112 list_del_init(&oo->oo_perclient);
1115 static void release_last_closed_stateid(struct nfs4_openowner *oo)
1117 struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net,
1118 nfsd_net_id);
1119 struct nfs4_ol_stateid *s;
1121 spin_lock(&nn->client_lock);
1122 s = oo->oo_last_closed_stid;
1123 if (s) {
1124 list_del_init(&oo->oo_close_lru);
1125 oo->oo_last_closed_stid = NULL;
1127 spin_unlock(&nn->client_lock);
1128 if (s)
1129 nfs4_put_stid(&s->st_stid);
1132 static void release_openowner(struct nfs4_openowner *oo)
1134 struct nfs4_ol_stateid *stp;
1135 struct nfs4_client *clp = oo->oo_owner.so_client;
1136 struct list_head reaplist;
1138 INIT_LIST_HEAD(&reaplist);
1140 spin_lock(&clp->cl_lock);
1141 unhash_openowner_locked(oo);
1142 while (!list_empty(&oo->oo_owner.so_stateids)) {
1143 stp = list_first_entry(&oo->oo_owner.so_stateids,
1144 struct nfs4_ol_stateid, st_perstateowner);
1145 unhash_open_stateid(stp, &reaplist);
1146 put_ol_stateid_locked(stp, &reaplist);
1148 spin_unlock(&clp->cl_lock);
1149 free_ol_stateid_reaplist(&reaplist);
1150 release_last_closed_stateid(oo);
1151 nfs4_put_stateowner(&oo->oo_owner);
1154 static inline int
1155 hash_sessionid(struct nfs4_sessionid *sessionid)
1157 struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid;
1159 return sid->sequence % SESSION_HASH_SIZE;
1162 #ifdef NFSD_DEBUG
1163 static inline void
1164 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1166 u32 *ptr = (u32 *)(&sessionid->data[0]);
1167 dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]);
1169 #else
1170 static inline void
1171 dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid)
1174 #endif
1177 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1178 * won't be used for replay.
1180 void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr)
1182 struct nfs4_stateowner *so = cstate->replay_owner;
1184 if (nfserr == nfserr_replay_me)
1185 return;
1187 if (!seqid_mutating_err(ntohl(nfserr))) {
1188 nfsd4_cstate_clear_replay(cstate);
1189 return;
1191 if (!so)
1192 return;
1193 if (so->so_is_open_owner)
1194 release_last_closed_stateid(openowner(so));
1195 so->so_seqid++;
1196 return;
1199 static void
1200 gen_sessionid(struct nfsd4_session *ses)
1202 struct nfs4_client *clp = ses->se_client;
1203 struct nfsd4_sessionid *sid;
1205 sid = (struct nfsd4_sessionid *)ses->se_sessionid.data;
1206 sid->clientid = clp->cl_clientid;
1207 sid->sequence = current_sessionid++;
1208 sid->reserved = 0;
1212 * The protocol defines ca_maxresponssize_cached to include the size of
1213 * the rpc header, but all we need to cache is the data starting after
1214 * the end of the initial SEQUENCE operation--the rest we regenerate
1215 * each time. Therefore we can advertise a ca_maxresponssize_cached
1216 * value that is the number of bytes in our cache plus a few additional
1217 * bytes. In order to stay on the safe side, and not promise more than
1218 * we can cache, those additional bytes must be the minimum possible: 24
1219 * bytes of rpc header (xid through accept state, with AUTH_NULL
1220 * verifier), 12 for the compound header (with zero-length tag), and 44
1221 * for the SEQUENCE op response:
1223 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1225 static void
1226 free_session_slots(struct nfsd4_session *ses)
1228 int i;
1230 for (i = 0; i < ses->se_fchannel.maxreqs; i++)
1231 kfree(ses->se_slots[i]);
1235 * We don't actually need to cache the rpc and session headers, so we
1236 * can allocate a little less for each slot:
1238 static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca)
1240 u32 size;
1242 if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ)
1243 size = 0;
1244 else
1245 size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ;
1246 return size + sizeof(struct nfsd4_slot);
1250 * XXX: If we run out of reserved DRC memory we could (up to a point)
1251 * re-negotiate active sessions and reduce their slot usage to make
1252 * room for new connections. For now we just fail the create session.
1254 static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca)
1256 u32 slotsize = slot_bytes(ca);
1257 u32 num = ca->maxreqs;
1258 int avail;
1260 spin_lock(&nfsd_drc_lock);
1261 avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION,
1262 nfsd_drc_max_mem - nfsd_drc_mem_used);
1263 num = min_t(int, num, avail / slotsize);
1264 nfsd_drc_mem_used += num * slotsize;
1265 spin_unlock(&nfsd_drc_lock);
1267 return num;
1270 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca)
1272 int slotsize = slot_bytes(ca);
1274 spin_lock(&nfsd_drc_lock);
1275 nfsd_drc_mem_used -= slotsize * ca->maxreqs;
1276 spin_unlock(&nfsd_drc_lock);
1279 static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs,
1280 struct nfsd4_channel_attrs *battrs)
1282 int numslots = fattrs->maxreqs;
1283 int slotsize = slot_bytes(fattrs);
1284 struct nfsd4_session *new;
1285 int mem, i;
1287 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *)
1288 + sizeof(struct nfsd4_session) > PAGE_SIZE);
1289 mem = numslots * sizeof(struct nfsd4_slot *);
1291 new = kzalloc(sizeof(*new) + mem, GFP_KERNEL);
1292 if (!new)
1293 return NULL;
1294 /* allocate each struct nfsd4_slot and data cache in one piece */
1295 for (i = 0; i < numslots; i++) {
1296 new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL);
1297 if (!new->se_slots[i])
1298 goto out_free;
1301 memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs));
1302 memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs));
1304 return new;
1305 out_free:
1306 while (i--)
1307 kfree(new->se_slots[i]);
1308 kfree(new);
1309 return NULL;
1312 static void free_conn(struct nfsd4_conn *c)
1314 svc_xprt_put(c->cn_xprt);
1315 kfree(c);
1318 static void nfsd4_conn_lost(struct svc_xpt_user *u)
1320 struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user);
1321 struct nfs4_client *clp = c->cn_session->se_client;
1323 spin_lock(&clp->cl_lock);
1324 if (!list_empty(&c->cn_persession)) {
1325 list_del(&c->cn_persession);
1326 free_conn(c);
1328 nfsd4_probe_callback(clp);
1329 spin_unlock(&clp->cl_lock);
1332 static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags)
1334 struct nfsd4_conn *conn;
1336 conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL);
1337 if (!conn)
1338 return NULL;
1339 svc_xprt_get(rqstp->rq_xprt);
1340 conn->cn_xprt = rqstp->rq_xprt;
1341 conn->cn_flags = flags;
1342 INIT_LIST_HEAD(&conn->cn_xpt_user.list);
1343 return conn;
1346 static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1348 conn->cn_session = ses;
1349 list_add(&conn->cn_persession, &ses->se_conns);
1352 static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses)
1354 struct nfs4_client *clp = ses->se_client;
1356 spin_lock(&clp->cl_lock);
1357 __nfsd4_hash_conn(conn, ses);
1358 spin_unlock(&clp->cl_lock);
1361 static int nfsd4_register_conn(struct nfsd4_conn *conn)
1363 conn->cn_xpt_user.callback = nfsd4_conn_lost;
1364 return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user);
1367 static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses)
1369 int ret;
1371 nfsd4_hash_conn(conn, ses);
1372 ret = nfsd4_register_conn(conn);
1373 if (ret)
1374 /* oops; xprt is already down: */
1375 nfsd4_conn_lost(&conn->cn_xpt_user);
1376 /* We may have gained or lost a callback channel: */
1377 nfsd4_probe_callback_sync(ses->se_client);
1380 static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses)
1382 u32 dir = NFS4_CDFC4_FORE;
1384 if (cses->flags & SESSION4_BACK_CHAN)
1385 dir |= NFS4_CDFC4_BACK;
1386 return alloc_conn(rqstp, dir);
1389 /* must be called under client_lock */
1390 static void nfsd4_del_conns(struct nfsd4_session *s)
1392 struct nfs4_client *clp = s->se_client;
1393 struct nfsd4_conn *c;
1395 spin_lock(&clp->cl_lock);
1396 while (!list_empty(&s->se_conns)) {
1397 c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession);
1398 list_del_init(&c->cn_persession);
1399 spin_unlock(&clp->cl_lock);
1401 unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user);
1402 free_conn(c);
1404 spin_lock(&clp->cl_lock);
1406 spin_unlock(&clp->cl_lock);
1409 static void __free_session(struct nfsd4_session *ses)
1411 free_session_slots(ses);
1412 kfree(ses);
1415 static void free_session(struct nfsd4_session *ses)
1417 nfsd4_del_conns(ses);
1418 nfsd4_put_drc_mem(&ses->se_fchannel);
1419 __free_session(ses);
1422 static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses)
1424 int idx;
1425 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
1427 new->se_client = clp;
1428 gen_sessionid(new);
1430 INIT_LIST_HEAD(&new->se_conns);
1432 new->se_cb_seq_nr = 1;
1433 new->se_flags = cses->flags;
1434 new->se_cb_prog = cses->callback_prog;
1435 new->se_cb_sec = cses->cb_sec;
1436 atomic_set(&new->se_ref, 0);
1437 idx = hash_sessionid(&new->se_sessionid);
1438 list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]);
1439 spin_lock(&clp->cl_lock);
1440 list_add(&new->se_perclnt, &clp->cl_sessions);
1441 spin_unlock(&clp->cl_lock);
1443 if (cses->flags & SESSION4_BACK_CHAN) {
1444 struct sockaddr *sa = svc_addr(rqstp);
1446 * This is a little silly; with sessions there's no real
1447 * use for the callback address. Use the peer address
1448 * as a reasonable default for now, but consider fixing
1449 * the rpc client not to require an address in the
1450 * future:
1452 rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa);
1453 clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa);
1457 /* caller must hold client_lock */
1458 static struct nfsd4_session *
1459 __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net)
1461 struct nfsd4_session *elem;
1462 int idx;
1463 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1465 lockdep_assert_held(&nn->client_lock);
1467 dump_sessionid(__func__, sessionid);
1468 idx = hash_sessionid(sessionid);
1469 /* Search in the appropriate list */
1470 list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) {
1471 if (!memcmp(elem->se_sessionid.data, sessionid->data,
1472 NFS4_MAX_SESSIONID_LEN)) {
1473 return elem;
1477 dprintk("%s: session not found\n", __func__);
1478 return NULL;
1481 static struct nfsd4_session *
1482 find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net,
1483 __be32 *ret)
1485 struct nfsd4_session *session;
1486 __be32 status = nfserr_badsession;
1488 session = __find_in_sessionid_hashtbl(sessionid, net);
1489 if (!session)
1490 goto out;
1491 status = nfsd4_get_session_locked(session);
1492 if (status)
1493 session = NULL;
1494 out:
1495 *ret = status;
1496 return session;
1499 /* caller must hold client_lock */
1500 static void
1501 unhash_session(struct nfsd4_session *ses)
1503 struct nfs4_client *clp = ses->se_client;
1504 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1506 lockdep_assert_held(&nn->client_lock);
1508 list_del(&ses->se_hash);
1509 spin_lock(&ses->se_client->cl_lock);
1510 list_del(&ses->se_perclnt);
1511 spin_unlock(&ses->se_client->cl_lock);
1514 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1515 static int
1516 STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn)
1518 if (clid->cl_boot == nn->boot_time)
1519 return 0;
1520 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1521 clid->cl_boot, clid->cl_id, nn->boot_time);
1522 return 1;
1526 * XXX Should we use a slab cache ?
1527 * This type of memory management is somewhat inefficient, but we use it
1528 * anyway since SETCLIENTID is not a common operation.
1530 static struct nfs4_client *alloc_client(struct xdr_netobj name)
1532 struct nfs4_client *clp;
1533 int i;
1535 clp = kzalloc(sizeof(struct nfs4_client), GFP_KERNEL);
1536 if (clp == NULL)
1537 return NULL;
1538 clp->cl_name.data = kmemdup(name.data, name.len, GFP_KERNEL);
1539 if (clp->cl_name.data == NULL)
1540 goto err_no_name;
1541 clp->cl_ownerstr_hashtbl = kmalloc(sizeof(struct list_head) *
1542 OWNER_HASH_SIZE, GFP_KERNEL);
1543 if (!clp->cl_ownerstr_hashtbl)
1544 goto err_no_hashtbl;
1545 for (i = 0; i < OWNER_HASH_SIZE; i++)
1546 INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]);
1547 clp->cl_name.len = name.len;
1548 INIT_LIST_HEAD(&clp->cl_sessions);
1549 idr_init(&clp->cl_stateids);
1550 atomic_set(&clp->cl_refcount, 0);
1551 clp->cl_cb_state = NFSD4_CB_UNKNOWN;
1552 INIT_LIST_HEAD(&clp->cl_idhash);
1553 INIT_LIST_HEAD(&clp->cl_openowners);
1554 INIT_LIST_HEAD(&clp->cl_delegations);
1555 INIT_LIST_HEAD(&clp->cl_lru);
1556 INIT_LIST_HEAD(&clp->cl_callbacks);
1557 INIT_LIST_HEAD(&clp->cl_revoked);
1558 spin_lock_init(&clp->cl_lock);
1559 rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table");
1560 return clp;
1561 err_no_hashtbl:
1562 kfree(clp->cl_name.data);
1563 err_no_name:
1564 kfree(clp);
1565 return NULL;
1568 static void
1569 free_client(struct nfs4_client *clp)
1571 while (!list_empty(&clp->cl_sessions)) {
1572 struct nfsd4_session *ses;
1573 ses = list_entry(clp->cl_sessions.next, struct nfsd4_session,
1574 se_perclnt);
1575 list_del(&ses->se_perclnt);
1576 WARN_ON_ONCE(atomic_read(&ses->se_ref));
1577 free_session(ses);
1579 rpc_destroy_wait_queue(&clp->cl_cb_waitq);
1580 free_svc_cred(&clp->cl_cred);
1581 kfree(clp->cl_ownerstr_hashtbl);
1582 kfree(clp->cl_name.data);
1583 idr_destroy(&clp->cl_stateids);
1584 kfree(clp);
1587 /* must be called under the client_lock */
1588 static void
1589 unhash_client_locked(struct nfs4_client *clp)
1591 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1592 struct nfsd4_session *ses;
1594 lockdep_assert_held(&nn->client_lock);
1596 /* Mark the client as expired! */
1597 clp->cl_time = 0;
1598 /* Make it invisible */
1599 if (!list_empty(&clp->cl_idhash)) {
1600 list_del_init(&clp->cl_idhash);
1601 if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags))
1602 rb_erase(&clp->cl_namenode, &nn->conf_name_tree);
1603 else
1604 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1606 list_del_init(&clp->cl_lru);
1607 spin_lock(&clp->cl_lock);
1608 list_for_each_entry(ses, &clp->cl_sessions, se_perclnt)
1609 list_del_init(&ses->se_hash);
1610 spin_unlock(&clp->cl_lock);
1613 static void
1614 unhash_client(struct nfs4_client *clp)
1616 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1618 spin_lock(&nn->client_lock);
1619 unhash_client_locked(clp);
1620 spin_unlock(&nn->client_lock);
1623 static __be32 mark_client_expired_locked(struct nfs4_client *clp)
1625 if (atomic_read(&clp->cl_refcount))
1626 return nfserr_jukebox;
1627 unhash_client_locked(clp);
1628 return nfs_ok;
1631 static void
1632 __destroy_client(struct nfs4_client *clp)
1634 struct nfs4_openowner *oo;
1635 struct nfs4_delegation *dp;
1636 struct list_head reaplist;
1638 INIT_LIST_HEAD(&reaplist);
1639 spin_lock(&state_lock);
1640 while (!list_empty(&clp->cl_delegations)) {
1641 dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt);
1642 unhash_delegation_locked(dp);
1643 list_add(&dp->dl_recall_lru, &reaplist);
1645 spin_unlock(&state_lock);
1646 while (!list_empty(&reaplist)) {
1647 dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru);
1648 list_del_init(&dp->dl_recall_lru);
1649 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
1650 nfs4_put_stid(&dp->dl_stid);
1652 while (!list_empty(&clp->cl_revoked)) {
1653 dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru);
1654 list_del_init(&dp->dl_recall_lru);
1655 nfs4_put_stid(&dp->dl_stid);
1657 while (!list_empty(&clp->cl_openowners)) {
1658 oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient);
1659 nfs4_get_stateowner(&oo->oo_owner);
1660 release_openowner(oo);
1662 nfsd4_shutdown_callback(clp);
1663 if (clp->cl_cb_conn.cb_xprt)
1664 svc_xprt_put(clp->cl_cb_conn.cb_xprt);
1665 free_client(clp);
1668 static void
1669 destroy_client(struct nfs4_client *clp)
1671 unhash_client(clp);
1672 __destroy_client(clp);
1675 static void expire_client(struct nfs4_client *clp)
1677 unhash_client(clp);
1678 nfsd4_client_record_remove(clp);
1679 __destroy_client(clp);
1682 static void copy_verf(struct nfs4_client *target, nfs4_verifier *source)
1684 memcpy(target->cl_verifier.data, source->data,
1685 sizeof(target->cl_verifier.data));
1688 static void copy_clid(struct nfs4_client *target, struct nfs4_client *source)
1690 target->cl_clientid.cl_boot = source->cl_clientid.cl_boot;
1691 target->cl_clientid.cl_id = source->cl_clientid.cl_id;
1694 static int copy_cred(struct svc_cred *target, struct svc_cred *source)
1696 if (source->cr_principal) {
1697 target->cr_principal =
1698 kstrdup(source->cr_principal, GFP_KERNEL);
1699 if (target->cr_principal == NULL)
1700 return -ENOMEM;
1701 } else
1702 target->cr_principal = NULL;
1703 target->cr_flavor = source->cr_flavor;
1704 target->cr_uid = source->cr_uid;
1705 target->cr_gid = source->cr_gid;
1706 target->cr_group_info = source->cr_group_info;
1707 get_group_info(target->cr_group_info);
1708 target->cr_gss_mech = source->cr_gss_mech;
1709 if (source->cr_gss_mech)
1710 gss_mech_get(source->cr_gss_mech);
1711 return 0;
1714 static int
1715 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1717 if (o1->len < o2->len)
1718 return -1;
1719 if (o1->len > o2->len)
1720 return 1;
1721 return memcmp(o1->data, o2->data, o1->len);
1724 static int same_name(const char *n1, const char *n2)
1726 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1729 static int
1730 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1732 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1735 static int
1736 same_clid(clientid_t *cl1, clientid_t *cl2)
1738 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1741 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1743 int i;
1745 if (g1->ngroups != g2->ngroups)
1746 return false;
1747 for (i=0; i<g1->ngroups; i++)
1748 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1749 return false;
1750 return true;
1754 * RFC 3530 language requires clid_inuse be returned when the
1755 * "principal" associated with a requests differs from that previously
1756 * used. We use uid, gid's, and gss principal string as our best
1757 * approximation. We also don't want to allow non-gss use of a client
1758 * established using gss: in theory cr_principal should catch that
1759 * change, but in practice cr_principal can be null even in the gss case
1760 * since gssd doesn't always pass down a principal string.
1762 static bool is_gss_cred(struct svc_cred *cr)
1764 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1765 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1769 static bool
1770 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1772 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1773 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1774 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1775 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1776 return false;
1777 if (cr1->cr_principal == cr2->cr_principal)
1778 return true;
1779 if (!cr1->cr_principal || !cr2->cr_principal)
1780 return false;
1781 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1784 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1786 struct svc_cred *cr = &rqstp->rq_cred;
1787 u32 service;
1789 if (!cr->cr_gss_mech)
1790 return false;
1791 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1792 return service == RPC_GSS_SVC_INTEGRITY ||
1793 service == RPC_GSS_SVC_PRIVACY;
1796 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1798 struct svc_cred *cr = &rqstp->rq_cred;
1800 if (!cl->cl_mach_cred)
1801 return true;
1802 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1803 return false;
1804 if (!svc_rqst_integrity_protected(rqstp))
1805 return false;
1806 if (!cr->cr_principal)
1807 return false;
1808 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1811 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1813 __be32 verf[2];
1816 * This is opaque to client, so no need to byte-swap. Use
1817 * __force to keep sparse happy
1819 verf[0] = (__force __be32)get_seconds();
1820 verf[1] = (__force __be32)nn->clientid_counter;
1821 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1824 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1826 clp->cl_clientid.cl_boot = nn->boot_time;
1827 clp->cl_clientid.cl_id = nn->clientid_counter++;
1828 gen_confirm(clp, nn);
1831 static struct nfs4_stid *
1832 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1834 struct nfs4_stid *ret;
1836 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1837 if (!ret || !ret->sc_type)
1838 return NULL;
1839 return ret;
1842 static struct nfs4_stid *
1843 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1845 struct nfs4_stid *s;
1847 spin_lock(&cl->cl_lock);
1848 s = find_stateid_locked(cl, t);
1849 if (s != NULL) {
1850 if (typemask & s->sc_type)
1851 atomic_inc(&s->sc_count);
1852 else
1853 s = NULL;
1855 spin_unlock(&cl->cl_lock);
1856 return s;
1859 static struct nfs4_client *create_client(struct xdr_netobj name,
1860 struct svc_rqst *rqstp, nfs4_verifier *verf)
1862 struct nfs4_client *clp;
1863 struct sockaddr *sa = svc_addr(rqstp);
1864 int ret;
1865 struct net *net = SVC_NET(rqstp);
1867 clp = alloc_client(name);
1868 if (clp == NULL)
1869 return NULL;
1871 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1872 if (ret) {
1873 free_client(clp);
1874 return NULL;
1876 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
1877 clp->cl_time = get_seconds();
1878 clear_bit(0, &clp->cl_cb_slot_busy);
1879 copy_verf(clp, verf);
1880 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1881 clp->cl_cb_session = NULL;
1882 clp->net = net;
1883 return clp;
1886 static void
1887 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1889 struct rb_node **new = &(root->rb_node), *parent = NULL;
1890 struct nfs4_client *clp;
1892 while (*new) {
1893 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1894 parent = *new;
1896 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1897 new = &((*new)->rb_left);
1898 else
1899 new = &((*new)->rb_right);
1902 rb_link_node(&new_clp->cl_namenode, parent, new);
1903 rb_insert_color(&new_clp->cl_namenode, root);
1906 static struct nfs4_client *
1907 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1909 int cmp;
1910 struct rb_node *node = root->rb_node;
1911 struct nfs4_client *clp;
1913 while (node) {
1914 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1915 cmp = compare_blob(&clp->cl_name, name);
1916 if (cmp > 0)
1917 node = node->rb_left;
1918 else if (cmp < 0)
1919 node = node->rb_right;
1920 else
1921 return clp;
1923 return NULL;
1926 static void
1927 add_to_unconfirmed(struct nfs4_client *clp)
1929 unsigned int idhashval;
1930 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1932 lockdep_assert_held(&nn->client_lock);
1934 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1935 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1936 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1937 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1938 renew_client_locked(clp);
1941 static void
1942 move_to_confirmed(struct nfs4_client *clp)
1944 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1945 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1947 lockdep_assert_held(&nn->client_lock);
1949 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1950 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1951 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1952 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1953 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1954 renew_client_locked(clp);
1957 static struct nfs4_client *
1958 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1960 struct nfs4_client *clp;
1961 unsigned int idhashval = clientid_hashval(clid->cl_id);
1963 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1964 if (same_clid(&clp->cl_clientid, clid)) {
1965 if ((bool)clp->cl_minorversion != sessions)
1966 return NULL;
1967 renew_client_locked(clp);
1968 return clp;
1971 return NULL;
1974 static struct nfs4_client *
1975 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1977 struct list_head *tbl = nn->conf_id_hashtbl;
1979 lockdep_assert_held(&nn->client_lock);
1980 return find_client_in_id_table(tbl, clid, sessions);
1983 static struct nfs4_client *
1984 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1986 struct list_head *tbl = nn->unconf_id_hashtbl;
1988 lockdep_assert_held(&nn->client_lock);
1989 return find_client_in_id_table(tbl, clid, sessions);
1992 static bool clp_used_exchangeid(struct nfs4_client *clp)
1994 return clp->cl_exchange_flags != 0;
1997 static struct nfs4_client *
1998 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2000 lockdep_assert_held(&nn->client_lock);
2001 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2004 static struct nfs4_client *
2005 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2007 lockdep_assert_held(&nn->client_lock);
2008 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2011 static void
2012 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2014 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2015 struct sockaddr *sa = svc_addr(rqstp);
2016 u32 scopeid = rpc_get_scope_id(sa);
2017 unsigned short expected_family;
2019 /* Currently, we only support tcp and tcp6 for the callback channel */
2020 if (se->se_callback_netid_len == 3 &&
2021 !memcmp(se->se_callback_netid_val, "tcp", 3))
2022 expected_family = AF_INET;
2023 else if (se->se_callback_netid_len == 4 &&
2024 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2025 expected_family = AF_INET6;
2026 else
2027 goto out_err;
2029 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2030 se->se_callback_addr_len,
2031 (struct sockaddr *)&conn->cb_addr,
2032 sizeof(conn->cb_addr));
2034 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2035 goto out_err;
2037 if (conn->cb_addr.ss_family == AF_INET6)
2038 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2040 conn->cb_prog = se->se_callback_prog;
2041 conn->cb_ident = se->se_callback_ident;
2042 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2043 return;
2044 out_err:
2045 conn->cb_addr.ss_family = AF_UNSPEC;
2046 conn->cb_addrlen = 0;
2047 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2048 "will not receive delegations\n",
2049 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2051 return;
2055 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2057 static void
2058 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2060 struct xdr_buf *buf = resp->xdr.buf;
2061 struct nfsd4_slot *slot = resp->cstate.slot;
2062 unsigned int base;
2064 dprintk("--> %s slot %p\n", __func__, slot);
2066 slot->sl_opcnt = resp->opcnt;
2067 slot->sl_status = resp->cstate.status;
2069 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2070 if (nfsd4_not_cached(resp)) {
2071 slot->sl_datalen = 0;
2072 return;
2074 base = resp->cstate.data_offset;
2075 slot->sl_datalen = buf->len - base;
2076 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2077 WARN("%s: sessions DRC could not cache compound\n", __func__);
2078 return;
2082 * Encode the replay sequence operation from the slot values.
2083 * If cachethis is FALSE encode the uncached rep error on the next
2084 * operation which sets resp->p and increments resp->opcnt for
2085 * nfs4svc_encode_compoundres.
2088 static __be32
2089 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2090 struct nfsd4_compoundres *resp)
2092 struct nfsd4_op *op;
2093 struct nfsd4_slot *slot = resp->cstate.slot;
2095 /* Encode the replayed sequence operation */
2096 op = &args->ops[resp->opcnt - 1];
2097 nfsd4_encode_operation(resp, op);
2099 /* Return nfserr_retry_uncached_rep in next operation. */
2100 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2101 op = &args->ops[resp->opcnt++];
2102 op->status = nfserr_retry_uncached_rep;
2103 nfsd4_encode_operation(resp, op);
2105 return op->status;
2109 * The sequence operation is not cached because we can use the slot and
2110 * session values.
2112 static __be32
2113 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2114 struct nfsd4_sequence *seq)
2116 struct nfsd4_slot *slot = resp->cstate.slot;
2117 struct xdr_stream *xdr = &resp->xdr;
2118 __be32 *p;
2119 __be32 status;
2121 dprintk("--> %s slot %p\n", __func__, slot);
2123 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2124 if (status)
2125 return status;
2127 p = xdr_reserve_space(xdr, slot->sl_datalen);
2128 if (!p) {
2129 WARN_ON_ONCE(1);
2130 return nfserr_serverfault;
2132 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2133 xdr_commit_encode(xdr);
2135 resp->opcnt = slot->sl_opcnt;
2136 return slot->sl_status;
2140 * Set the exchange_id flags returned by the server.
2142 static void
2143 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2145 /* pNFS is not supported */
2146 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2148 /* Referrals are supported, Migration is not. */
2149 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2151 /* set the wire flags to return to client. */
2152 clid->flags = new->cl_exchange_flags;
2155 static bool client_has_state(struct nfs4_client *clp)
2158 * Note clp->cl_openowners check isn't quite right: there's no
2159 * need to count owners without stateid's.
2161 * Also note we should probably be using this in 4.0 case too.
2163 return !list_empty(&clp->cl_openowners)
2164 || !list_empty(&clp->cl_delegations)
2165 || !list_empty(&clp->cl_sessions);
2168 __be32
2169 nfsd4_exchange_id(struct svc_rqst *rqstp,
2170 struct nfsd4_compound_state *cstate,
2171 struct nfsd4_exchange_id *exid)
2173 struct nfs4_client *conf, *new;
2174 struct nfs4_client *unconf = NULL;
2175 __be32 status;
2176 char addr_str[INET6_ADDRSTRLEN];
2177 nfs4_verifier verf = exid->verifier;
2178 struct sockaddr *sa = svc_addr(rqstp);
2179 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2180 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2182 rpc_ntop(sa, addr_str, sizeof(addr_str));
2183 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2184 "ip_addr=%s flags %x, spa_how %d\n",
2185 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2186 addr_str, exid->flags, exid->spa_how);
2188 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2189 return nfserr_inval;
2191 switch (exid->spa_how) {
2192 case SP4_MACH_CRED:
2193 if (!svc_rqst_integrity_protected(rqstp))
2194 return nfserr_inval;
2195 case SP4_NONE:
2196 break;
2197 default: /* checked by xdr code */
2198 WARN_ON_ONCE(1);
2199 case SP4_SSV:
2200 return nfserr_encr_alg_unsupp;
2203 new = create_client(exid->clname, rqstp, &verf);
2204 if (new == NULL)
2205 return nfserr_jukebox;
2207 /* Cases below refer to rfc 5661 section 18.35.4: */
2208 spin_lock(&nn->client_lock);
2209 conf = find_confirmed_client_by_name(&exid->clname, nn);
2210 if (conf) {
2211 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2212 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2214 if (update) {
2215 if (!clp_used_exchangeid(conf)) { /* buggy client */
2216 status = nfserr_inval;
2217 goto out;
2219 if (!mach_creds_match(conf, rqstp)) {
2220 status = nfserr_wrong_cred;
2221 goto out;
2223 if (!creds_match) { /* case 9 */
2224 status = nfserr_perm;
2225 goto out;
2227 if (!verfs_match) { /* case 8 */
2228 status = nfserr_not_same;
2229 goto out;
2231 /* case 6 */
2232 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2233 goto out_copy;
2235 if (!creds_match) { /* case 3 */
2236 if (client_has_state(conf)) {
2237 status = nfserr_clid_inuse;
2238 goto out;
2240 goto out_new;
2242 if (verfs_match) { /* case 2 */
2243 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2244 goto out_copy;
2246 /* case 5, client reboot */
2247 conf = NULL;
2248 goto out_new;
2251 if (update) { /* case 7 */
2252 status = nfserr_noent;
2253 goto out;
2256 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2257 if (unconf) /* case 4, possible retry or client restart */
2258 unhash_client_locked(unconf);
2260 /* case 1 (normal case) */
2261 out_new:
2262 if (conf) {
2263 status = mark_client_expired_locked(conf);
2264 if (status)
2265 goto out;
2267 new->cl_minorversion = cstate->minorversion;
2268 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2270 gen_clid(new, nn);
2271 add_to_unconfirmed(new);
2272 swap(new, conf);
2273 out_copy:
2274 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2275 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2277 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2278 nfsd4_set_ex_flags(conf, exid);
2280 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2281 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2282 status = nfs_ok;
2284 out:
2285 spin_unlock(&nn->client_lock);
2286 if (new)
2287 expire_client(new);
2288 if (unconf)
2289 expire_client(unconf);
2290 return status;
2293 static __be32
2294 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2296 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2297 slot_seqid);
2299 /* The slot is in use, and no response has been sent. */
2300 if (slot_inuse) {
2301 if (seqid == slot_seqid)
2302 return nfserr_jukebox;
2303 else
2304 return nfserr_seq_misordered;
2306 /* Note unsigned 32-bit arithmetic handles wraparound: */
2307 if (likely(seqid == slot_seqid + 1))
2308 return nfs_ok;
2309 if (seqid == slot_seqid)
2310 return nfserr_replay_cache;
2311 return nfserr_seq_misordered;
2315 * Cache the create session result into the create session single DRC
2316 * slot cache by saving the xdr structure. sl_seqid has been set.
2317 * Do this for solo or embedded create session operations.
2319 static void
2320 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2321 struct nfsd4_clid_slot *slot, __be32 nfserr)
2323 slot->sl_status = nfserr;
2324 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2327 static __be32
2328 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2329 struct nfsd4_clid_slot *slot)
2331 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2332 return slot->sl_status;
2335 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2336 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2337 1 + /* MIN tag is length with zero, only length */ \
2338 3 + /* version, opcount, opcode */ \
2339 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2340 /* seqid, slotID, slotID, cache */ \
2341 4 ) * sizeof(__be32))
2343 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2344 2 + /* verifier: AUTH_NULL, length 0 */\
2345 1 + /* status */ \
2346 1 + /* MIN tag is length with zero, only length */ \
2347 3 + /* opcount, opcode, opstatus*/ \
2348 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2349 /* seqid, slotID, slotID, slotID, status */ \
2350 5 ) * sizeof(__be32))
2352 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2354 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2356 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2357 return nfserr_toosmall;
2358 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2359 return nfserr_toosmall;
2360 ca->headerpadsz = 0;
2361 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2362 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2363 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2364 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2365 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2366 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2368 * Note decreasing slot size below client's request may make it
2369 * difficult for client to function correctly, whereas
2370 * decreasing the number of slots will (just?) affect
2371 * performance. When short on memory we therefore prefer to
2372 * decrease number of slots instead of their size. Clients that
2373 * request larger slots than they need will get poor results:
2375 ca->maxreqs = nfsd4_get_drc_mem(ca);
2376 if (!ca->maxreqs)
2377 return nfserr_jukebox;
2379 return nfs_ok;
2382 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2383 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2384 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2385 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2387 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2389 ca->headerpadsz = 0;
2392 * These RPC_MAX_HEADER macros are overkill, especially since we
2393 * don't even do gss on the backchannel yet. But this is still
2394 * less than 1k. Tighten up this estimate in the unlikely event
2395 * it turns out to be a problem for some client:
2397 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2398 return nfserr_toosmall;
2399 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2400 return nfserr_toosmall;
2401 ca->maxresp_cached = 0;
2402 if (ca->maxops < 2)
2403 return nfserr_toosmall;
2405 return nfs_ok;
2408 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2410 switch (cbs->flavor) {
2411 case RPC_AUTH_NULL:
2412 case RPC_AUTH_UNIX:
2413 return nfs_ok;
2414 default:
2416 * GSS case: the spec doesn't allow us to return this
2417 * error. But it also doesn't allow us not to support
2418 * GSS.
2419 * I'd rather this fail hard than return some error the
2420 * client might think it can already handle:
2422 return nfserr_encr_alg_unsupp;
2426 __be32
2427 nfsd4_create_session(struct svc_rqst *rqstp,
2428 struct nfsd4_compound_state *cstate,
2429 struct nfsd4_create_session *cr_ses)
2431 struct sockaddr *sa = svc_addr(rqstp);
2432 struct nfs4_client *conf, *unconf;
2433 struct nfs4_client *old = NULL;
2434 struct nfsd4_session *new;
2435 struct nfsd4_conn *conn;
2436 struct nfsd4_clid_slot *cs_slot = NULL;
2437 __be32 status = 0;
2438 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2440 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2441 return nfserr_inval;
2442 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2443 if (status)
2444 return status;
2445 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2446 if (status)
2447 return status;
2448 status = check_backchannel_attrs(&cr_ses->back_channel);
2449 if (status)
2450 goto out_release_drc_mem;
2451 status = nfserr_jukebox;
2452 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2453 if (!new)
2454 goto out_release_drc_mem;
2455 conn = alloc_conn_from_crses(rqstp, cr_ses);
2456 if (!conn)
2457 goto out_free_session;
2459 spin_lock(&nn->client_lock);
2460 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2461 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2462 WARN_ON_ONCE(conf && unconf);
2464 if (conf) {
2465 status = nfserr_wrong_cred;
2466 if (!mach_creds_match(conf, rqstp))
2467 goto out_free_conn;
2468 cs_slot = &conf->cl_cs_slot;
2469 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2470 if (status == nfserr_replay_cache) {
2471 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2472 goto out_free_conn;
2473 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2474 status = nfserr_seq_misordered;
2475 goto out_free_conn;
2477 } else if (unconf) {
2478 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2479 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2480 status = nfserr_clid_inuse;
2481 goto out_free_conn;
2483 status = nfserr_wrong_cred;
2484 if (!mach_creds_match(unconf, rqstp))
2485 goto out_free_conn;
2486 cs_slot = &unconf->cl_cs_slot;
2487 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2488 if (status) {
2489 /* an unconfirmed replay returns misordered */
2490 status = nfserr_seq_misordered;
2491 goto out_free_conn;
2493 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2494 if (old) {
2495 status = mark_client_expired_locked(old);
2496 if (status) {
2497 old = NULL;
2498 goto out_free_conn;
2501 move_to_confirmed(unconf);
2502 conf = unconf;
2503 } else {
2504 status = nfserr_stale_clientid;
2505 goto out_free_conn;
2507 status = nfs_ok;
2509 * We do not support RDMA or persistent sessions
2511 cr_ses->flags &= ~SESSION4_PERSIST;
2512 cr_ses->flags &= ~SESSION4_RDMA;
2514 init_session(rqstp, new, conf, cr_ses);
2515 nfsd4_get_session_locked(new);
2517 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2518 NFS4_MAX_SESSIONID_LEN);
2519 cs_slot->sl_seqid++;
2520 cr_ses->seqid = cs_slot->sl_seqid;
2522 /* cache solo and embedded create sessions under the client_lock */
2523 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2524 spin_unlock(&nn->client_lock);
2525 /* init connection and backchannel */
2526 nfsd4_init_conn(rqstp, conn, new);
2527 nfsd4_put_session(new);
2528 if (old)
2529 expire_client(old);
2530 return status;
2531 out_free_conn:
2532 spin_unlock(&nn->client_lock);
2533 free_conn(conn);
2534 if (old)
2535 expire_client(old);
2536 out_free_session:
2537 __free_session(new);
2538 out_release_drc_mem:
2539 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2540 return status;
2543 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2545 switch (*dir) {
2546 case NFS4_CDFC4_FORE:
2547 case NFS4_CDFC4_BACK:
2548 return nfs_ok;
2549 case NFS4_CDFC4_FORE_OR_BOTH:
2550 case NFS4_CDFC4_BACK_OR_BOTH:
2551 *dir = NFS4_CDFC4_BOTH;
2552 return nfs_ok;
2554 return nfserr_inval;
2557 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2559 struct nfsd4_session *session = cstate->session;
2560 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2561 __be32 status;
2563 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2564 if (status)
2565 return status;
2566 spin_lock(&nn->client_lock);
2567 session->se_cb_prog = bc->bc_cb_program;
2568 session->se_cb_sec = bc->bc_cb_sec;
2569 spin_unlock(&nn->client_lock);
2571 nfsd4_probe_callback(session->se_client);
2573 return nfs_ok;
2576 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2577 struct nfsd4_compound_state *cstate,
2578 struct nfsd4_bind_conn_to_session *bcts)
2580 __be32 status;
2581 struct nfsd4_conn *conn;
2582 struct nfsd4_session *session;
2583 struct net *net = SVC_NET(rqstp);
2584 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2586 if (!nfsd4_last_compound_op(rqstp))
2587 return nfserr_not_only_op;
2588 spin_lock(&nn->client_lock);
2589 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2590 spin_unlock(&nn->client_lock);
2591 if (!session)
2592 goto out_no_session;
2593 status = nfserr_wrong_cred;
2594 if (!mach_creds_match(session->se_client, rqstp))
2595 goto out;
2596 status = nfsd4_map_bcts_dir(&bcts->dir);
2597 if (status)
2598 goto out;
2599 conn = alloc_conn(rqstp, bcts->dir);
2600 status = nfserr_jukebox;
2601 if (!conn)
2602 goto out;
2603 nfsd4_init_conn(rqstp, conn, session);
2604 status = nfs_ok;
2605 out:
2606 nfsd4_put_session(session);
2607 out_no_session:
2608 return status;
2611 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2613 if (!session)
2614 return 0;
2615 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2618 __be32
2619 nfsd4_destroy_session(struct svc_rqst *r,
2620 struct nfsd4_compound_state *cstate,
2621 struct nfsd4_destroy_session *sessionid)
2623 struct nfsd4_session *ses;
2624 __be32 status;
2625 int ref_held_by_me = 0;
2626 struct net *net = SVC_NET(r);
2627 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2629 status = nfserr_not_only_op;
2630 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2631 if (!nfsd4_last_compound_op(r))
2632 goto out;
2633 ref_held_by_me++;
2635 dump_sessionid(__func__, &sessionid->sessionid);
2636 spin_lock(&nn->client_lock);
2637 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2638 if (!ses)
2639 goto out_client_lock;
2640 status = nfserr_wrong_cred;
2641 if (!mach_creds_match(ses->se_client, r))
2642 goto out_put_session;
2643 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2644 if (status)
2645 goto out_put_session;
2646 unhash_session(ses);
2647 spin_unlock(&nn->client_lock);
2649 nfsd4_probe_callback_sync(ses->se_client);
2651 spin_lock(&nn->client_lock);
2652 status = nfs_ok;
2653 out_put_session:
2654 nfsd4_put_session_locked(ses);
2655 out_client_lock:
2656 spin_unlock(&nn->client_lock);
2657 out:
2658 return status;
2661 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2663 struct nfsd4_conn *c;
2665 list_for_each_entry(c, &s->se_conns, cn_persession) {
2666 if (c->cn_xprt == xpt) {
2667 return c;
2670 return NULL;
2673 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2675 struct nfs4_client *clp = ses->se_client;
2676 struct nfsd4_conn *c;
2677 __be32 status = nfs_ok;
2678 int ret;
2680 spin_lock(&clp->cl_lock);
2681 c = __nfsd4_find_conn(new->cn_xprt, ses);
2682 if (c)
2683 goto out_free;
2684 status = nfserr_conn_not_bound_to_session;
2685 if (clp->cl_mach_cred)
2686 goto out_free;
2687 __nfsd4_hash_conn(new, ses);
2688 spin_unlock(&clp->cl_lock);
2689 ret = nfsd4_register_conn(new);
2690 if (ret)
2691 /* oops; xprt is already down: */
2692 nfsd4_conn_lost(&new->cn_xpt_user);
2693 return nfs_ok;
2694 out_free:
2695 spin_unlock(&clp->cl_lock);
2696 free_conn(new);
2697 return status;
2700 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2702 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2704 return args->opcnt > session->se_fchannel.maxops;
2707 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2708 struct nfsd4_session *session)
2710 struct xdr_buf *xb = &rqstp->rq_arg;
2712 return xb->len > session->se_fchannel.maxreq_sz;
2715 __be32
2716 nfsd4_sequence(struct svc_rqst *rqstp,
2717 struct nfsd4_compound_state *cstate,
2718 struct nfsd4_sequence *seq)
2720 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2721 struct xdr_stream *xdr = &resp->xdr;
2722 struct nfsd4_session *session;
2723 struct nfs4_client *clp;
2724 struct nfsd4_slot *slot;
2725 struct nfsd4_conn *conn;
2726 __be32 status;
2727 int buflen;
2728 struct net *net = SVC_NET(rqstp);
2729 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2731 if (resp->opcnt != 1)
2732 return nfserr_sequence_pos;
2735 * Will be either used or freed by nfsd4_sequence_check_conn
2736 * below.
2738 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2739 if (!conn)
2740 return nfserr_jukebox;
2742 spin_lock(&nn->client_lock);
2743 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2744 if (!session)
2745 goto out_no_session;
2746 clp = session->se_client;
2748 status = nfserr_too_many_ops;
2749 if (nfsd4_session_too_many_ops(rqstp, session))
2750 goto out_put_session;
2752 status = nfserr_req_too_big;
2753 if (nfsd4_request_too_big(rqstp, session))
2754 goto out_put_session;
2756 status = nfserr_badslot;
2757 if (seq->slotid >= session->se_fchannel.maxreqs)
2758 goto out_put_session;
2760 slot = session->se_slots[seq->slotid];
2761 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2763 /* We do not negotiate the number of slots yet, so set the
2764 * maxslots to the session maxreqs which is used to encode
2765 * sr_highest_slotid and the sr_target_slot id to maxslots */
2766 seq->maxslots = session->se_fchannel.maxreqs;
2768 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2769 slot->sl_flags & NFSD4_SLOT_INUSE);
2770 if (status == nfserr_replay_cache) {
2771 status = nfserr_seq_misordered;
2772 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2773 goto out_put_session;
2774 cstate->slot = slot;
2775 cstate->session = session;
2776 cstate->clp = clp;
2777 /* Return the cached reply status and set cstate->status
2778 * for nfsd4_proc_compound processing */
2779 status = nfsd4_replay_cache_entry(resp, seq);
2780 cstate->status = nfserr_replay_cache;
2781 goto out;
2783 if (status)
2784 goto out_put_session;
2786 status = nfsd4_sequence_check_conn(conn, session);
2787 conn = NULL;
2788 if (status)
2789 goto out_put_session;
2791 buflen = (seq->cachethis) ?
2792 session->se_fchannel.maxresp_cached :
2793 session->se_fchannel.maxresp_sz;
2794 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2795 nfserr_rep_too_big;
2796 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2797 goto out_put_session;
2798 svc_reserve(rqstp, buflen);
2800 status = nfs_ok;
2801 /* Success! bump slot seqid */
2802 slot->sl_seqid = seq->seqid;
2803 slot->sl_flags |= NFSD4_SLOT_INUSE;
2804 if (seq->cachethis)
2805 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2806 else
2807 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2809 cstate->slot = slot;
2810 cstate->session = session;
2811 cstate->clp = clp;
2813 out:
2814 switch (clp->cl_cb_state) {
2815 case NFSD4_CB_DOWN:
2816 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2817 break;
2818 case NFSD4_CB_FAULT:
2819 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2820 break;
2821 default:
2822 seq->status_flags = 0;
2824 if (!list_empty(&clp->cl_revoked))
2825 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2826 out_no_session:
2827 if (conn)
2828 free_conn(conn);
2829 spin_unlock(&nn->client_lock);
2830 return status;
2831 out_put_session:
2832 nfsd4_put_session_locked(session);
2833 goto out_no_session;
2836 void
2837 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2839 struct nfsd4_compound_state *cs = &resp->cstate;
2841 if (nfsd4_has_session(cs)) {
2842 if (cs->status != nfserr_replay_cache) {
2843 nfsd4_store_cache_entry(resp);
2844 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2846 /* Drop session reference that was taken in nfsd4_sequence() */
2847 nfsd4_put_session(cs->session);
2848 } else if (cs->clp)
2849 put_client_renew(cs->clp);
2852 __be32
2853 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2855 struct nfs4_client *conf, *unconf;
2856 struct nfs4_client *clp = NULL;
2857 __be32 status = 0;
2858 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2860 spin_lock(&nn->client_lock);
2861 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2862 conf = find_confirmed_client(&dc->clientid, true, nn);
2863 WARN_ON_ONCE(conf && unconf);
2865 if (conf) {
2866 if (client_has_state(conf)) {
2867 status = nfserr_clientid_busy;
2868 goto out;
2870 status = mark_client_expired_locked(conf);
2871 if (status)
2872 goto out;
2873 clp = conf;
2874 } else if (unconf)
2875 clp = unconf;
2876 else {
2877 status = nfserr_stale_clientid;
2878 goto out;
2880 if (!mach_creds_match(clp, rqstp)) {
2881 clp = NULL;
2882 status = nfserr_wrong_cred;
2883 goto out;
2885 unhash_client_locked(clp);
2886 out:
2887 spin_unlock(&nn->client_lock);
2888 if (clp)
2889 expire_client(clp);
2890 return status;
2893 __be32
2894 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2896 __be32 status = 0;
2898 if (rc->rca_one_fs) {
2899 if (!cstate->current_fh.fh_dentry)
2900 return nfserr_nofilehandle;
2902 * We don't take advantage of the rca_one_fs case.
2903 * That's OK, it's optional, we can safely ignore it.
2905 return nfs_ok;
2908 status = nfserr_complete_already;
2909 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2910 &cstate->session->se_client->cl_flags))
2911 goto out;
2913 status = nfserr_stale_clientid;
2914 if (is_client_expired(cstate->session->se_client))
2916 * The following error isn't really legal.
2917 * But we only get here if the client just explicitly
2918 * destroyed the client. Surely it no longer cares what
2919 * error it gets back on an operation for the dead
2920 * client.
2922 goto out;
2924 status = nfs_ok;
2925 nfsd4_client_record_create(cstate->session->se_client);
2926 out:
2927 return status;
2930 __be32
2931 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2932 struct nfsd4_setclientid *setclid)
2934 struct xdr_netobj clname = setclid->se_name;
2935 nfs4_verifier clverifier = setclid->se_verf;
2936 struct nfs4_client *conf, *new;
2937 struct nfs4_client *unconf = NULL;
2938 __be32 status;
2939 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2941 new = create_client(clname, rqstp, &clverifier);
2942 if (new == NULL)
2943 return nfserr_jukebox;
2944 /* Cases below refer to rfc 3530 section 14.2.33: */
2945 spin_lock(&nn->client_lock);
2946 conf = find_confirmed_client_by_name(&clname, nn);
2947 if (conf) {
2948 /* case 0: */
2949 status = nfserr_clid_inuse;
2950 if (clp_used_exchangeid(conf))
2951 goto out;
2952 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2953 char addr_str[INET6_ADDRSTRLEN];
2954 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2955 sizeof(addr_str));
2956 dprintk("NFSD: setclientid: string in use by client "
2957 "at %s\n", addr_str);
2958 goto out;
2961 unconf = find_unconfirmed_client_by_name(&clname, nn);
2962 if (unconf)
2963 unhash_client_locked(unconf);
2964 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2965 /* case 1: probable callback update */
2966 copy_clid(new, conf);
2967 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2968 gen_clid(new, nn);
2969 new->cl_minorversion = 0;
2970 gen_callback(new, setclid, rqstp);
2971 add_to_unconfirmed(new);
2972 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2973 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2974 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2975 new = NULL;
2976 status = nfs_ok;
2977 out:
2978 spin_unlock(&nn->client_lock);
2979 if (new)
2980 free_client(new);
2981 if (unconf)
2982 expire_client(unconf);
2983 return status;
2987 __be32
2988 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2989 struct nfsd4_compound_state *cstate,
2990 struct nfsd4_setclientid_confirm *setclientid_confirm)
2992 struct nfs4_client *conf, *unconf;
2993 struct nfs4_client *old = NULL;
2994 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2995 clientid_t * clid = &setclientid_confirm->sc_clientid;
2996 __be32 status;
2997 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2999 if (STALE_CLIENTID(clid, nn))
3000 return nfserr_stale_clientid;
3002 spin_lock(&nn->client_lock);
3003 conf = find_confirmed_client(clid, false, nn);
3004 unconf = find_unconfirmed_client(clid, false, nn);
3006 * We try hard to give out unique clientid's, so if we get an
3007 * attempt to confirm the same clientid with a different cred,
3008 * there's a bug somewhere. Let's charitably assume it's our
3009 * bug.
3011 status = nfserr_serverfault;
3012 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3013 goto out;
3014 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3015 goto out;
3016 /* cases below refer to rfc 3530 section 14.2.34: */
3017 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3018 if (conf && !unconf) /* case 2: probable retransmit */
3019 status = nfs_ok;
3020 else /* case 4: client hasn't noticed we rebooted yet? */
3021 status = nfserr_stale_clientid;
3022 goto out;
3024 status = nfs_ok;
3025 if (conf) { /* case 1: callback update */
3026 old = unconf;
3027 unhash_client_locked(old);
3028 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3029 } else { /* case 3: normal case; new or rebooted client */
3030 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3031 if (old) {
3032 status = mark_client_expired_locked(old);
3033 if (status) {
3034 old = NULL;
3035 goto out;
3038 move_to_confirmed(unconf);
3039 conf = unconf;
3041 get_client_locked(conf);
3042 spin_unlock(&nn->client_lock);
3043 nfsd4_probe_callback(conf);
3044 spin_lock(&nn->client_lock);
3045 put_client_renew_locked(conf);
3046 out:
3047 spin_unlock(&nn->client_lock);
3048 if (old)
3049 expire_client(old);
3050 return status;
3053 static struct nfs4_file *nfsd4_alloc_file(void)
3055 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3058 /* OPEN Share state helper functions */
3059 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
3061 unsigned int hashval = file_hashval(fh);
3063 lockdep_assert_held(&state_lock);
3065 atomic_set(&fp->fi_ref, 1);
3066 spin_lock_init(&fp->fi_lock);
3067 INIT_LIST_HEAD(&fp->fi_stateids);
3068 INIT_LIST_HEAD(&fp->fi_delegations);
3069 fh_copy_shallow(&fp->fi_fhandle, fh);
3070 fp->fi_deleg_file = NULL;
3071 fp->fi_had_conflict = false;
3072 fp->fi_share_deny = 0;
3073 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3074 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3075 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
3078 void
3079 nfsd4_free_slabs(void)
3081 kmem_cache_destroy(openowner_slab);
3082 kmem_cache_destroy(lockowner_slab);
3083 kmem_cache_destroy(file_slab);
3084 kmem_cache_destroy(stateid_slab);
3085 kmem_cache_destroy(deleg_slab);
3089 nfsd4_init_slabs(void)
3091 openowner_slab = kmem_cache_create("nfsd4_openowners",
3092 sizeof(struct nfs4_openowner), 0, 0, NULL);
3093 if (openowner_slab == NULL)
3094 goto out;
3095 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3096 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3097 if (lockowner_slab == NULL)
3098 goto out_free_openowner_slab;
3099 file_slab = kmem_cache_create("nfsd4_files",
3100 sizeof(struct nfs4_file), 0, 0, NULL);
3101 if (file_slab == NULL)
3102 goto out_free_lockowner_slab;
3103 stateid_slab = kmem_cache_create("nfsd4_stateids",
3104 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3105 if (stateid_slab == NULL)
3106 goto out_free_file_slab;
3107 deleg_slab = kmem_cache_create("nfsd4_delegations",
3108 sizeof(struct nfs4_delegation), 0, 0, NULL);
3109 if (deleg_slab == NULL)
3110 goto out_free_stateid_slab;
3111 return 0;
3113 out_free_stateid_slab:
3114 kmem_cache_destroy(stateid_slab);
3115 out_free_file_slab:
3116 kmem_cache_destroy(file_slab);
3117 out_free_lockowner_slab:
3118 kmem_cache_destroy(lockowner_slab);
3119 out_free_openowner_slab:
3120 kmem_cache_destroy(openowner_slab);
3121 out:
3122 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3123 return -ENOMEM;
3126 static void init_nfs4_replay(struct nfs4_replay *rp)
3128 rp->rp_status = nfserr_serverfault;
3129 rp->rp_buflen = 0;
3130 rp->rp_buf = rp->rp_ibuf;
3131 mutex_init(&rp->rp_mutex);
3134 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3135 struct nfs4_stateowner *so)
3137 if (!nfsd4_has_session(cstate)) {
3138 mutex_lock(&so->so_replay.rp_mutex);
3139 cstate->replay_owner = nfs4_get_stateowner(so);
3143 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3145 struct nfs4_stateowner *so = cstate->replay_owner;
3147 if (so != NULL) {
3148 cstate->replay_owner = NULL;
3149 mutex_unlock(&so->so_replay.rp_mutex);
3150 nfs4_put_stateowner(so);
3154 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3156 struct nfs4_stateowner *sop;
3158 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3159 if (!sop)
3160 return NULL;
3162 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3163 if (!sop->so_owner.data) {
3164 kmem_cache_free(slab, sop);
3165 return NULL;
3167 sop->so_owner.len = owner->len;
3169 INIT_LIST_HEAD(&sop->so_stateids);
3170 sop->so_client = clp;
3171 init_nfs4_replay(&sop->so_replay);
3172 atomic_set(&sop->so_count, 1);
3173 return sop;
3176 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3178 lockdep_assert_held(&clp->cl_lock);
3180 list_add(&oo->oo_owner.so_strhash,
3181 &clp->cl_ownerstr_hashtbl[strhashval]);
3182 list_add(&oo->oo_perclient, &clp->cl_openowners);
3185 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3187 unhash_openowner_locked(openowner(so));
3190 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3192 struct nfs4_openowner *oo = openowner(so);
3194 kmem_cache_free(openowner_slab, oo);
3197 static const struct nfs4_stateowner_operations openowner_ops = {
3198 .so_unhash = nfs4_unhash_openowner,
3199 .so_free = nfs4_free_openowner,
3202 static struct nfs4_openowner *
3203 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3204 struct nfsd4_compound_state *cstate)
3206 struct nfs4_client *clp = cstate->clp;
3207 struct nfs4_openowner *oo, *ret;
3209 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3210 if (!oo)
3211 return NULL;
3212 oo->oo_owner.so_ops = &openowner_ops;
3213 oo->oo_owner.so_is_open_owner = 1;
3214 oo->oo_owner.so_seqid = open->op_seqid;
3215 oo->oo_flags = 0;
3216 if (nfsd4_has_session(cstate))
3217 oo->oo_flags |= NFS4_OO_CONFIRMED;
3218 oo->oo_time = 0;
3219 oo->oo_last_closed_stid = NULL;
3220 INIT_LIST_HEAD(&oo->oo_close_lru);
3221 spin_lock(&clp->cl_lock);
3222 ret = find_openstateowner_str_locked(strhashval, open, clp);
3223 if (ret == NULL) {
3224 hash_openowner(oo, clp, strhashval);
3225 ret = oo;
3226 } else
3227 nfs4_free_openowner(&oo->oo_owner);
3228 spin_unlock(&clp->cl_lock);
3229 return ret;
3232 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3233 struct nfs4_openowner *oo = open->op_openowner;
3235 atomic_inc(&stp->st_stid.sc_count);
3236 stp->st_stid.sc_type = NFS4_OPEN_STID;
3237 INIT_LIST_HEAD(&stp->st_locks);
3238 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3239 get_nfs4_file(fp);
3240 stp->st_stid.sc_file = fp;
3241 stp->st_access_bmap = 0;
3242 stp->st_deny_bmap = 0;
3243 stp->st_openstp = NULL;
3244 init_rwsem(&stp->st_rwsem);
3245 spin_lock(&oo->oo_owner.so_client->cl_lock);
3246 list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids);
3247 spin_lock(&fp->fi_lock);
3248 list_add(&stp->st_perfile, &fp->fi_stateids);
3249 spin_unlock(&fp->fi_lock);
3250 spin_unlock(&oo->oo_owner.so_client->cl_lock);
3254 * In the 4.0 case we need to keep the owners around a little while to handle
3255 * CLOSE replay. We still do need to release any file access that is held by
3256 * them before returning however.
3258 static void
3259 move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net)
3261 struct nfs4_ol_stateid *last;
3262 struct nfs4_openowner *oo = openowner(s->st_stateowner);
3263 struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net,
3264 nfsd_net_id);
3266 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo);
3269 * We know that we hold one reference via nfsd4_close, and another
3270 * "persistent" reference for the client. If the refcount is higher
3271 * than 2, then there are still calls in progress that are using this
3272 * stateid. We can't put the sc_file reference until they are finished.
3273 * Wait for the refcount to drop to 2. Since it has been unhashed,
3274 * there should be no danger of the refcount going back up again at
3275 * this point.
3277 wait_event(close_wq, atomic_read(&s->st_stid.sc_count) == 2);
3279 release_all_access(s);
3280 if (s->st_stid.sc_file) {
3281 put_nfs4_file(s->st_stid.sc_file);
3282 s->st_stid.sc_file = NULL;
3285 spin_lock(&nn->client_lock);
3286 last = oo->oo_last_closed_stid;
3287 oo->oo_last_closed_stid = s;
3288 list_move_tail(&oo->oo_close_lru, &nn->close_lru);
3289 oo->oo_time = get_seconds();
3290 spin_unlock(&nn->client_lock);
3291 if (last)
3292 nfs4_put_stid(&last->st_stid);
3295 /* search file_hashtbl[] for file */
3296 static struct nfs4_file *
3297 find_file_locked(struct knfsd_fh *fh)
3299 unsigned int hashval = file_hashval(fh);
3300 struct nfs4_file *fp;
3302 lockdep_assert_held(&state_lock);
3304 hlist_for_each_entry(fp, &file_hashtbl[hashval], fi_hash) {
3305 if (nfsd_fh_match(&fp->fi_fhandle, fh)) {
3306 get_nfs4_file(fp);
3307 return fp;
3310 return NULL;
3313 static struct nfs4_file *
3314 find_file(struct knfsd_fh *fh)
3316 struct nfs4_file *fp;
3318 spin_lock(&state_lock);
3319 fp = find_file_locked(fh);
3320 spin_unlock(&state_lock);
3321 return fp;
3324 static struct nfs4_file *
3325 find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh)
3327 struct nfs4_file *fp;
3329 spin_lock(&state_lock);
3330 fp = find_file_locked(fh);
3331 if (fp == NULL) {
3332 nfsd4_init_file(new, fh);
3333 fp = new;
3335 spin_unlock(&state_lock);
3337 return fp;
3341 * Called to check deny when READ with all zero stateid or
3342 * WRITE with all zero or all one stateid
3344 static __be32
3345 nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type)
3347 struct nfs4_file *fp;
3348 __be32 ret = nfs_ok;
3350 fp = find_file(&current_fh->fh_handle);
3351 if (!fp)
3352 return ret;
3353 /* Check for conflicting share reservations */
3354 spin_lock(&fp->fi_lock);
3355 if (fp->fi_share_deny & deny_type)
3356 ret = nfserr_locked;
3357 spin_unlock(&fp->fi_lock);
3358 put_nfs4_file(fp);
3359 return ret;
3362 static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb)
3364 struct nfs4_delegation *dp = cb_to_delegation(cb);
3365 struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net,
3366 nfsd_net_id);
3368 block_delegations(&dp->dl_stid.sc_file->fi_fhandle);
3371 * We can't do this in nfsd_break_deleg_cb because it is
3372 * already holding inode->i_lock.
3374 * If the dl_time != 0, then we know that it has already been
3375 * queued for a lease break. Don't queue it again.
3377 spin_lock(&state_lock);
3378 if (dp->dl_time == 0) {
3379 dp->dl_time = get_seconds();
3380 list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru);
3382 spin_unlock(&state_lock);
3385 static int nfsd4_cb_recall_done(struct nfsd4_callback *cb,
3386 struct rpc_task *task)
3388 struct nfs4_delegation *dp = cb_to_delegation(cb);
3390 switch (task->tk_status) {
3391 case 0:
3392 return 1;
3393 case -EBADHANDLE:
3394 case -NFS4ERR_BAD_STATEID:
3396 * Race: client probably got cb_recall before open reply
3397 * granting delegation.
3399 if (dp->dl_retries--) {
3400 rpc_delay(task, 2 * HZ);
3401 return 0;
3403 /*FALLTHRU*/
3404 default:
3405 return -1;
3409 static void nfsd4_cb_recall_release(struct nfsd4_callback *cb)
3411 struct nfs4_delegation *dp = cb_to_delegation(cb);
3413 nfs4_put_stid(&dp->dl_stid);
3416 static struct nfsd4_callback_ops nfsd4_cb_recall_ops = {
3417 .prepare = nfsd4_cb_recall_prepare,
3418 .done = nfsd4_cb_recall_done,
3419 .release = nfsd4_cb_recall_release,
3422 static void nfsd_break_one_deleg(struct nfs4_delegation *dp)
3425 * We're assuming the state code never drops its reference
3426 * without first removing the lease. Since we're in this lease
3427 * callback (and since the lease code is serialized by the kernel
3428 * lock) we know the server hasn't removed the lease yet, we know
3429 * it's safe to take a reference.
3431 atomic_inc(&dp->dl_stid.sc_count);
3432 nfsd4_run_cb(&dp->dl_recall);
3435 /* Called from break_lease() with i_lock held. */
3436 static bool
3437 nfsd_break_deleg_cb(struct file_lock *fl)
3439 bool ret = false;
3440 struct nfs4_file *fp = (struct nfs4_file *)fl->fl_owner;
3441 struct nfs4_delegation *dp;
3443 if (!fp) {
3444 WARN(1, "(%p)->fl_owner NULL\n", fl);
3445 return ret;
3447 if (fp->fi_had_conflict) {
3448 WARN(1, "duplicate break on %p\n", fp);
3449 return ret;
3452 * We don't want the locks code to timeout the lease for us;
3453 * we'll remove it ourself if a delegation isn't returned
3454 * in time:
3456 fl->fl_break_time = 0;
3458 spin_lock(&fp->fi_lock);
3459 fp->fi_had_conflict = true;
3461 * If there are no delegations on the list, then return true
3462 * so that the lease code will go ahead and delete it.
3464 if (list_empty(&fp->fi_delegations))
3465 ret = true;
3466 else
3467 list_for_each_entry(dp, &fp->fi_delegations, dl_perfile)
3468 nfsd_break_one_deleg(dp);
3469 spin_unlock(&fp->fi_lock);
3470 return ret;
3473 static int
3474 nfsd_change_deleg_cb(struct file_lock **onlist, int arg, struct list_head *dispose)
3476 if (arg & F_UNLCK)
3477 return lease_modify(onlist, arg, dispose);
3478 else
3479 return -EAGAIN;
3482 static const struct lock_manager_operations nfsd_lease_mng_ops = {
3483 .lm_break = nfsd_break_deleg_cb,
3484 .lm_change = nfsd_change_deleg_cb,
3487 static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid)
3489 if (nfsd4_has_session(cstate))
3490 return nfs_ok;
3491 if (seqid == so->so_seqid - 1)
3492 return nfserr_replay_me;
3493 if (seqid == so->so_seqid)
3494 return nfs_ok;
3495 return nfserr_bad_seqid;
3498 static __be32 lookup_clientid(clientid_t *clid,
3499 struct nfsd4_compound_state *cstate,
3500 struct nfsd_net *nn)
3502 struct nfs4_client *found;
3504 if (cstate->clp) {
3505 found = cstate->clp;
3506 if (!same_clid(&found->cl_clientid, clid))
3507 return nfserr_stale_clientid;
3508 return nfs_ok;
3511 if (STALE_CLIENTID(clid, nn))
3512 return nfserr_stale_clientid;
3515 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3516 * cached already then we know this is for is for v4.0 and "sessions"
3517 * will be false.
3519 WARN_ON_ONCE(cstate->session);
3520 spin_lock(&nn->client_lock);
3521 found = find_confirmed_client(clid, false, nn);
3522 if (!found) {
3523 spin_unlock(&nn->client_lock);
3524 return nfserr_expired;
3526 atomic_inc(&found->cl_refcount);
3527 spin_unlock(&nn->client_lock);
3529 /* Cache the nfs4_client in cstate! */
3530 cstate->clp = found;
3531 return nfs_ok;
3534 __be32
3535 nfsd4_process_open1(struct nfsd4_compound_state *cstate,
3536 struct nfsd4_open *open, struct nfsd_net *nn)
3538 clientid_t *clientid = &open->op_clientid;
3539 struct nfs4_client *clp = NULL;
3540 unsigned int strhashval;
3541 struct nfs4_openowner *oo = NULL;
3542 __be32 status;
3544 if (STALE_CLIENTID(&open->op_clientid, nn))
3545 return nfserr_stale_clientid;
3547 * In case we need it later, after we've already created the
3548 * file and don't want to risk a further failure:
3550 open->op_file = nfsd4_alloc_file();
3551 if (open->op_file == NULL)
3552 return nfserr_jukebox;
3554 status = lookup_clientid(clientid, cstate, nn);
3555 if (status)
3556 return status;
3557 clp = cstate->clp;
3559 strhashval = ownerstr_hashval(&open->op_owner);
3560 oo = find_openstateowner_str(strhashval, open, clp);
3561 open->op_openowner = oo;
3562 if (!oo) {
3563 goto new_owner;
3565 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
3566 /* Replace unconfirmed owners without checking for replay. */
3567 release_openowner(oo);
3568 open->op_openowner = NULL;
3569 goto new_owner;
3571 status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid);
3572 if (status)
3573 return status;
3574 goto alloc_stateid;
3575 new_owner:
3576 oo = alloc_init_open_stateowner(strhashval, open, cstate);
3577 if (oo == NULL)
3578 return nfserr_jukebox;
3579 open->op_openowner = oo;
3580 alloc_stateid:
3581 open->op_stp = nfs4_alloc_open_stateid(clp);
3582 if (!open->op_stp)
3583 return nfserr_jukebox;
3584 return nfs_ok;
3587 static inline __be32
3588 nfs4_check_delegmode(struct nfs4_delegation *dp, int flags)
3590 if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ))
3591 return nfserr_openmode;
3592 else
3593 return nfs_ok;
3596 static int share_access_to_flags(u32 share_access)
3598 return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE;
3601 static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s)
3603 struct nfs4_stid *ret;
3605 ret = find_stateid_by_type(cl, s, NFS4_DELEG_STID);
3606 if (!ret)
3607 return NULL;
3608 return delegstateid(ret);
3611 static bool nfsd4_is_deleg_cur(struct nfsd4_open *open)
3613 return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR ||
3614 open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH;
3617 static __be32
3618 nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open,
3619 struct nfs4_delegation **dp)
3621 int flags;
3622 __be32 status = nfserr_bad_stateid;
3623 struct nfs4_delegation *deleg;
3625 deleg = find_deleg_stateid(cl, &open->op_delegate_stateid);
3626 if (deleg == NULL)
3627 goto out;
3628 flags = share_access_to_flags(open->op_share_access);
3629 status = nfs4_check_delegmode(deleg, flags);
3630 if (status) {
3631 nfs4_put_stid(&deleg->dl_stid);
3632 goto out;
3634 *dp = deleg;
3635 out:
3636 if (!nfsd4_is_deleg_cur(open))
3637 return nfs_ok;
3638 if (status)
3639 return status;
3640 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3641 return nfs_ok;
3644 static struct nfs4_ol_stateid *
3645 nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open)
3647 struct nfs4_ol_stateid *local, *ret = NULL;
3648 struct nfs4_openowner *oo = open->op_openowner;
3650 spin_lock(&fp->fi_lock);
3651 list_for_each_entry(local, &fp->fi_stateids, st_perfile) {
3652 /* ignore lock owners */
3653 if (local->st_stateowner->so_is_open_owner == 0)
3654 continue;
3655 if (local->st_stateowner == &oo->oo_owner) {
3656 ret = local;
3657 atomic_inc(&ret->st_stid.sc_count);
3658 break;
3661 spin_unlock(&fp->fi_lock);
3662 return ret;
3665 static inline int nfs4_access_to_access(u32 nfs4_access)
3667 int flags = 0;
3669 if (nfs4_access & NFS4_SHARE_ACCESS_READ)
3670 flags |= NFSD_MAY_READ;
3671 if (nfs4_access & NFS4_SHARE_ACCESS_WRITE)
3672 flags |= NFSD_MAY_WRITE;
3673 return flags;
3676 static inline __be32
3677 nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh,
3678 struct nfsd4_open *open)
3680 struct iattr iattr = {
3681 .ia_valid = ATTR_SIZE,
3682 .ia_size = 0,
3684 if (!open->op_truncate)
3685 return 0;
3686 if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE))
3687 return nfserr_inval;
3688 return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0);
3691 static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp,
3692 struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp,
3693 struct nfsd4_open *open)
3695 struct file *filp = NULL;
3696 __be32 status;
3697 int oflag = nfs4_access_to_omode(open->op_share_access);
3698 int access = nfs4_access_to_access(open->op_share_access);
3699 unsigned char old_access_bmap, old_deny_bmap;
3701 spin_lock(&fp->fi_lock);
3704 * Are we trying to set a deny mode that would conflict with
3705 * current access?
3707 status = nfs4_file_check_deny(fp, open->op_share_deny);
3708 if (status != nfs_ok) {
3709 spin_unlock(&fp->fi_lock);
3710 goto out;
3713 /* set access to the file */
3714 status = nfs4_file_get_access(fp, open->op_share_access);
3715 if (status != nfs_ok) {
3716 spin_unlock(&fp->fi_lock);
3717 goto out;
3720 /* Set access bits in stateid */
3721 old_access_bmap = stp->st_access_bmap;
3722 set_access(open->op_share_access, stp);
3724 /* Set new deny mask */
3725 old_deny_bmap = stp->st_deny_bmap;
3726 set_deny(open->op_share_deny, stp);
3727 fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3729 if (!fp->fi_fds[oflag]) {
3730 spin_unlock(&fp->fi_lock);
3731 status = nfsd_open(rqstp, cur_fh, S_IFREG, access, &filp);
3732 if (status)
3733 goto out_put_access;
3734 spin_lock(&fp->fi_lock);
3735 if (!fp->fi_fds[oflag]) {
3736 fp->fi_fds[oflag] = filp;
3737 filp = NULL;
3740 spin_unlock(&fp->fi_lock);
3741 if (filp)
3742 fput(filp);
3744 status = nfsd4_truncate(rqstp, cur_fh, open);
3745 if (status)
3746 goto out_put_access;
3747 out:
3748 return status;
3749 out_put_access:
3750 stp->st_access_bmap = old_access_bmap;
3751 nfs4_file_put_access(fp, open->op_share_access);
3752 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp);
3753 goto out;
3756 static __be32
3757 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)
3759 __be32 status;
3760 unsigned char old_deny_bmap;
3762 if (!test_access(open->op_share_access, stp))
3763 return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open);
3765 /* test and set deny mode */
3766 spin_lock(&fp->fi_lock);
3767 status = nfs4_file_check_deny(fp, open->op_share_deny);
3768 if (status == nfs_ok) {
3769 old_deny_bmap = stp->st_deny_bmap;
3770 set_deny(open->op_share_deny, stp);
3771 fp->fi_share_deny |=
3772 (open->op_share_deny & NFS4_SHARE_DENY_BOTH);
3774 spin_unlock(&fp->fi_lock);
3776 if (status != nfs_ok)
3777 return status;
3779 status = nfsd4_truncate(rqstp, cur_fh, open);
3780 if (status != nfs_ok)
3781 reset_union_bmap_deny(old_deny_bmap, stp);
3782 return status;
3785 static void
3786 nfs4_set_claim_prev(struct nfsd4_open *open, bool has_session)
3788 open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED;
3791 /* Should we give out recallable state?: */
3792 static bool nfsd4_cb_channel_good(struct nfs4_client *clp)
3794 if (clp->cl_cb_state == NFSD4_CB_UP)
3795 return true;
3797 * In the sessions case, since we don't have to establish a
3798 * separate connection for callbacks, we assume it's OK
3799 * until we hear otherwise:
3801 return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN;
3804 static struct file_lock *nfs4_alloc_init_lease(struct nfs4_file *fp, int flag)
3806 struct file_lock *fl;
3808 fl = locks_alloc_lock();
3809 if (!fl)
3810 return NULL;
3811 fl->fl_lmops = &nfsd_lease_mng_ops;
3812 fl->fl_flags = FL_DELEG;
3813 fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK;
3814 fl->fl_end = OFFSET_MAX;
3815 fl->fl_owner = (fl_owner_t)fp;
3816 fl->fl_pid = current->tgid;
3817 return fl;
3820 static int nfs4_setlease(struct nfs4_delegation *dp)
3822 struct nfs4_file *fp = dp->dl_stid.sc_file;
3823 struct file_lock *fl, *ret;
3824 struct file *filp;
3825 int status = 0;
3827 fl = nfs4_alloc_init_lease(fp, NFS4_OPEN_DELEGATE_READ);
3828 if (!fl)
3829 return -ENOMEM;
3830 filp = find_readable_file(fp);
3831 if (!filp) {
3832 /* We should always have a readable file here */
3833 WARN_ON_ONCE(1);
3834 return -EBADF;
3836 fl->fl_file = filp;
3837 ret = fl;
3838 status = vfs_setlease(filp, fl->fl_type, &fl, NULL);
3839 if (fl)
3840 locks_free_lock(fl);
3841 if (status)
3842 goto out_fput;
3843 spin_lock(&state_lock);
3844 spin_lock(&fp->fi_lock);
3845 /* Did the lease get broken before we took the lock? */
3846 status = -EAGAIN;
3847 if (fp->fi_had_conflict)
3848 goto out_unlock;
3849 /* Race breaker */
3850 if (fp->fi_deleg_file) {
3851 status = 0;
3852 atomic_inc(&fp->fi_delegees);
3853 hash_delegation_locked(dp, fp);
3854 goto out_unlock;
3856 fp->fi_deleg_file = filp;
3857 atomic_set(&fp->fi_delegees, 1);
3858 hash_delegation_locked(dp, fp);
3859 spin_unlock(&fp->fi_lock);
3860 spin_unlock(&state_lock);
3861 return 0;
3862 out_unlock:
3863 spin_unlock(&fp->fi_lock);
3864 spin_unlock(&state_lock);
3865 out_fput:
3866 fput(filp);
3867 return status;
3870 static struct nfs4_delegation *
3871 nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh,
3872 struct nfs4_file *fp)
3874 int status;
3875 struct nfs4_delegation *dp;
3877 if (fp->fi_had_conflict)
3878 return ERR_PTR(-EAGAIN);
3880 dp = alloc_init_deleg(clp, fh);
3881 if (!dp)
3882 return ERR_PTR(-ENOMEM);
3884 get_nfs4_file(fp);
3885 spin_lock(&state_lock);
3886 spin_lock(&fp->fi_lock);
3887 dp->dl_stid.sc_file = fp;
3888 if (!fp->fi_deleg_file) {
3889 spin_unlock(&fp->fi_lock);
3890 spin_unlock(&state_lock);
3891 status = nfs4_setlease(dp);
3892 goto out;
3894 if (fp->fi_had_conflict) {
3895 status = -EAGAIN;
3896 goto out_unlock;
3898 atomic_inc(&fp->fi_delegees);
3899 hash_delegation_locked(dp, fp);
3900 status = 0;
3901 out_unlock:
3902 spin_unlock(&fp->fi_lock);
3903 spin_unlock(&state_lock);
3904 out:
3905 if (status) {
3906 nfs4_put_stid(&dp->dl_stid);
3907 return ERR_PTR(status);
3909 return dp;
3912 static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status)
3914 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
3915 if (status == -EAGAIN)
3916 open->op_why_no_deleg = WND4_CONTENTION;
3917 else {
3918 open->op_why_no_deleg = WND4_RESOURCE;
3919 switch (open->op_deleg_want) {
3920 case NFS4_SHARE_WANT_READ_DELEG:
3921 case NFS4_SHARE_WANT_WRITE_DELEG:
3922 case NFS4_SHARE_WANT_ANY_DELEG:
3923 break;
3924 case NFS4_SHARE_WANT_CANCEL:
3925 open->op_why_no_deleg = WND4_CANCELLED;
3926 break;
3927 case NFS4_SHARE_WANT_NO_DELEG:
3928 WARN_ON_ONCE(1);
3934 * Attempt to hand out a delegation.
3936 * Note we don't support write delegations, and won't until the vfs has
3937 * proper support for them.
3939 static void
3940 nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open,
3941 struct nfs4_ol_stateid *stp)
3943 struct nfs4_delegation *dp;
3944 struct nfs4_openowner *oo = openowner(stp->st_stateowner);
3945 struct nfs4_client *clp = stp->st_stid.sc_client;
3946 int cb_up;
3947 int status = 0;
3949 cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client);
3950 open->op_recall = 0;
3951 switch (open->op_claim_type) {
3952 case NFS4_OPEN_CLAIM_PREVIOUS:
3953 if (!cb_up)
3954 open->op_recall = 1;
3955 if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ)
3956 goto out_no_deleg;
3957 break;
3958 case NFS4_OPEN_CLAIM_NULL:
3959 case NFS4_OPEN_CLAIM_FH:
3961 * Let's not give out any delegations till everyone's
3962 * had the chance to reclaim theirs....
3964 if (locks_in_grace(clp->net))
3965 goto out_no_deleg;
3966 if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED))
3967 goto out_no_deleg;
3969 * Also, if the file was opened for write or
3970 * create, there's a good chance the client's
3971 * about to write to it, resulting in an
3972 * immediate recall (since we don't support
3973 * write delegations):
3975 if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE)
3976 goto out_no_deleg;
3977 if (open->op_create == NFS4_OPEN_CREATE)
3978 goto out_no_deleg;
3979 break;
3980 default:
3981 goto out_no_deleg;
3983 dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file);
3984 if (IS_ERR(dp))
3985 goto out_no_deleg;
3987 memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid));
3989 dprintk("NFSD: delegation stateid=" STATEID_FMT "\n",
3990 STATEID_VAL(&dp->dl_stid.sc_stateid));
3991 open->op_delegate_type = NFS4_OPEN_DELEGATE_READ;
3992 nfs4_put_stid(&dp->dl_stid);
3993 return;
3994 out_no_deleg:
3995 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE;
3996 if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS &&
3997 open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) {
3998 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3999 open->op_recall = 1;
4002 /* 4.1 client asking for a delegation? */
4003 if (open->op_deleg_want)
4004 nfsd4_open_deleg_none_ext(open, status);
4005 return;
4008 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open,
4009 struct nfs4_delegation *dp)
4011 if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG &&
4012 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4013 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4014 open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE;
4015 } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG &&
4016 dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) {
4017 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4018 open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE;
4020 /* Otherwise the client must be confused wanting a delegation
4021 * it already has, therefore we don't return
4022 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4026 __be32
4027 nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open)
4029 struct nfsd4_compoundres *resp = rqstp->rq_resp;
4030 struct nfs4_client *cl = open->op_openowner->oo_owner.so_client;
4031 struct nfs4_file *fp = NULL;
4032 struct nfs4_ol_stateid *stp = NULL;
4033 struct nfs4_delegation *dp = NULL;
4034 __be32 status;
4037 * Lookup file; if found, lookup stateid and check open request,
4038 * and check for delegations in the process of being recalled.
4039 * If not found, create the nfs4_file struct
4041 fp = find_or_add_file(open->op_file, &current_fh->fh_handle);
4042 if (fp != open->op_file) {
4043 status = nfs4_check_deleg(cl, open, &dp);
4044 if (status)
4045 goto out;
4046 stp = nfsd4_find_existing_open(fp, open);
4047 } else {
4048 open->op_file = NULL;
4049 status = nfserr_bad_stateid;
4050 if (nfsd4_is_deleg_cur(open))
4051 goto out;
4052 status = nfserr_jukebox;
4056 * OPEN the file, or upgrade an existing OPEN.
4057 * If truncate fails, the OPEN fails.
4059 if (stp) {
4060 /* Stateid was found, this is an OPEN upgrade */
4061 down_read(&stp->st_rwsem);
4062 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4063 if (status) {
4064 up_read(&stp->st_rwsem);
4065 goto out;
4067 } else {
4068 stp = open->op_stp;
4069 open->op_stp = NULL;
4070 init_open_stateid(stp, fp, open);
4071 down_read(&stp->st_rwsem);
4072 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4073 if (status) {
4074 up_read(&stp->st_rwsem);
4075 release_open_stateid(stp);
4076 goto out;
4079 update_stateid(&stp->st_stid.sc_stateid);
4080 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4081 up_read(&stp->st_rwsem);
4083 if (nfsd4_has_session(&resp->cstate)) {
4084 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4085 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4086 open->op_why_no_deleg = WND4_NOT_WANTED;
4087 goto nodeleg;
4092 * Attempt to hand out a delegation. No error return, because the
4093 * OPEN succeeds even if we fail.
4095 nfs4_open_delegation(current_fh, open, stp);
4096 nodeleg:
4097 status = nfs_ok;
4099 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4100 STATEID_VAL(&stp->st_stid.sc_stateid));
4101 out:
4102 /* 4.1 client trying to upgrade/downgrade delegation? */
4103 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4104 open->op_deleg_want)
4105 nfsd4_deleg_xgrade_none_ext(open, dp);
4107 if (fp)
4108 put_nfs4_file(fp);
4109 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4110 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4112 * To finish the open response, we just need to set the rflags.
4114 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4115 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4116 !nfsd4_has_session(&resp->cstate))
4117 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4118 if (dp)
4119 nfs4_put_stid(&dp->dl_stid);
4120 if (stp)
4121 nfs4_put_stid(&stp->st_stid);
4123 return status;
4126 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4127 struct nfsd4_open *open, __be32 status)
4129 if (open->op_openowner) {
4130 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4132 nfsd4_cstate_assign_replay(cstate, so);
4133 nfs4_put_stateowner(so);
4135 if (open->op_file)
4136 nfsd4_free_file(open->op_file);
4137 if (open->op_stp)
4138 nfs4_put_stid(&open->op_stp->st_stid);
4141 __be32
4142 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4143 clientid_t *clid)
4145 struct nfs4_client *clp;
4146 __be32 status;
4147 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4149 dprintk("process_renew(%08x/%08x): starting\n",
4150 clid->cl_boot, clid->cl_id);
4151 status = lookup_clientid(clid, cstate, nn);
4152 if (status)
4153 goto out;
4154 clp = cstate->clp;
4155 status = nfserr_cb_path_down;
4156 if (!list_empty(&clp->cl_delegations)
4157 && clp->cl_cb_state != NFSD4_CB_UP)
4158 goto out;
4159 status = nfs_ok;
4160 out:
4161 return status;
4164 void
4165 nfsd4_end_grace(struct nfsd_net *nn)
4167 /* do nothing if grace period already ended */
4168 if (nn->grace_ended)
4169 return;
4171 dprintk("NFSD: end of grace period\n");
4172 nn->grace_ended = true;
4174 * If the server goes down again right now, an NFSv4
4175 * client will still be allowed to reclaim after it comes back up,
4176 * even if it hasn't yet had a chance to reclaim state this time.
4179 nfsd4_record_grace_done(nn);
4181 * At this point, NFSv4 clients can still reclaim. But if the
4182 * server crashes, any that have not yet reclaimed will be out
4183 * of luck on the next boot.
4185 * (NFSv4.1+ clients are considered to have reclaimed once they
4186 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4187 * have reclaimed after their first OPEN.)
4189 locks_end_grace(&nn->nfsd4_manager);
4191 * At this point, and once lockd and/or any other containers
4192 * exit their grace period, further reclaims will fail and
4193 * regular locking can resume.
4197 static time_t
4198 nfs4_laundromat(struct nfsd_net *nn)
4200 struct nfs4_client *clp;
4201 struct nfs4_openowner *oo;
4202 struct nfs4_delegation *dp;
4203 struct nfs4_ol_stateid *stp;
4204 struct list_head *pos, *next, reaplist;
4205 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4206 time_t t, new_timeo = nn->nfsd4_lease;
4208 dprintk("NFSD: laundromat service - starting\n");
4209 nfsd4_end_grace(nn);
4210 INIT_LIST_HEAD(&reaplist);
4211 spin_lock(&nn->client_lock);
4212 list_for_each_safe(pos, next, &nn->client_lru) {
4213 clp = list_entry(pos, struct nfs4_client, cl_lru);
4214 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4215 t = clp->cl_time - cutoff;
4216 new_timeo = min(new_timeo, t);
4217 break;
4219 if (mark_client_expired_locked(clp)) {
4220 dprintk("NFSD: client in use (clientid %08x)\n",
4221 clp->cl_clientid.cl_id);
4222 continue;
4224 list_add(&clp->cl_lru, &reaplist);
4226 spin_unlock(&nn->client_lock);
4227 list_for_each_safe(pos, next, &reaplist) {
4228 clp = list_entry(pos, struct nfs4_client, cl_lru);
4229 dprintk("NFSD: purging unused client (clientid %08x)\n",
4230 clp->cl_clientid.cl_id);
4231 list_del_init(&clp->cl_lru);
4232 expire_client(clp);
4234 spin_lock(&state_lock);
4235 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4236 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4237 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4238 continue;
4239 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4240 t = dp->dl_time - cutoff;
4241 new_timeo = min(new_timeo, t);
4242 break;
4244 unhash_delegation_locked(dp);
4245 list_add(&dp->dl_recall_lru, &reaplist);
4247 spin_unlock(&state_lock);
4248 while (!list_empty(&reaplist)) {
4249 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4250 dl_recall_lru);
4251 list_del_init(&dp->dl_recall_lru);
4252 revoke_delegation(dp);
4255 spin_lock(&nn->client_lock);
4256 while (!list_empty(&nn->close_lru)) {
4257 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4258 oo_close_lru);
4259 if (time_after((unsigned long)oo->oo_time,
4260 (unsigned long)cutoff)) {
4261 t = oo->oo_time - cutoff;
4262 new_timeo = min(new_timeo, t);
4263 break;
4265 list_del_init(&oo->oo_close_lru);
4266 stp = oo->oo_last_closed_stid;
4267 oo->oo_last_closed_stid = NULL;
4268 spin_unlock(&nn->client_lock);
4269 nfs4_put_stid(&stp->st_stid);
4270 spin_lock(&nn->client_lock);
4272 spin_unlock(&nn->client_lock);
4274 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4275 return new_timeo;
4278 static struct workqueue_struct *laundry_wq;
4279 static void laundromat_main(struct work_struct *);
4281 static void
4282 laundromat_main(struct work_struct *laundry)
4284 time_t t;
4285 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4286 work);
4287 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4288 laundromat_work);
4290 t = nfs4_laundromat(nn);
4291 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4292 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4295 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp)
4297 if (!nfsd_fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle))
4298 return nfserr_bad_stateid;
4299 return nfs_ok;
4302 static inline int
4303 access_permit_read(struct nfs4_ol_stateid *stp)
4305 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4306 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4307 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4310 static inline int
4311 access_permit_write(struct nfs4_ol_stateid *stp)
4313 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4314 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4317 static
4318 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4320 __be32 status = nfserr_openmode;
4322 /* For lock stateid's, we test the parent open, not the lock: */
4323 if (stp->st_openstp)
4324 stp = stp->st_openstp;
4325 if ((flags & WR_STATE) && !access_permit_write(stp))
4326 goto out;
4327 if ((flags & RD_STATE) && !access_permit_read(stp))
4328 goto out;
4329 status = nfs_ok;
4330 out:
4331 return status;
4334 static inline __be32
4335 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4337 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4338 return nfs_ok;
4339 else if (locks_in_grace(net)) {
4340 /* Answer in remaining cases depends on existence of
4341 * conflicting state; so we must wait out the grace period. */
4342 return nfserr_grace;
4343 } else if (flags & WR_STATE)
4344 return nfs4_share_conflict(current_fh,
4345 NFS4_SHARE_DENY_WRITE);
4346 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4347 return nfs4_share_conflict(current_fh,
4348 NFS4_SHARE_DENY_READ);
4352 * Allow READ/WRITE during grace period on recovered state only for files
4353 * that are not able to provide mandatory locking.
4355 static inline int
4356 grace_disallows_io(struct net *net, struct inode *inode)
4358 return locks_in_grace(net) && mandatory_lock(inode);
4361 /* Returns true iff a is later than b: */
4362 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4364 return (s32)(a->si_generation - b->si_generation) > 0;
4367 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4370 * When sessions are used the stateid generation number is ignored
4371 * when it is zero.
4373 if (has_session && in->si_generation == 0)
4374 return nfs_ok;
4376 if (in->si_generation == ref->si_generation)
4377 return nfs_ok;
4379 /* If the client sends us a stateid from the future, it's buggy: */
4380 if (stateid_generation_after(in, ref))
4381 return nfserr_bad_stateid;
4383 * However, we could see a stateid from the past, even from a
4384 * non-buggy client. For example, if the client sends a lock
4385 * while some IO is outstanding, the lock may bump si_generation
4386 * while the IO is still in flight. The client could avoid that
4387 * situation by waiting for responses on all the IO requests,
4388 * but better performance may result in retrying IO that
4389 * receives an old_stateid error if requests are rarely
4390 * reordered in flight:
4392 return nfserr_old_stateid;
4395 static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols)
4397 if (ols->st_stateowner->so_is_open_owner &&
4398 !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4399 return nfserr_bad_stateid;
4400 return nfs_ok;
4403 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4405 struct nfs4_stid *s;
4406 __be32 status = nfserr_bad_stateid;
4408 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4409 return status;
4410 /* Client debugging aid. */
4411 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4412 char addr_str[INET6_ADDRSTRLEN];
4413 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4414 sizeof(addr_str));
4415 pr_warn_ratelimited("NFSD: client %s testing state ID "
4416 "with incorrect client ID\n", addr_str);
4417 return status;
4419 spin_lock(&cl->cl_lock);
4420 s = find_stateid_locked(cl, stateid);
4421 if (!s)
4422 goto out_unlock;
4423 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4424 if (status)
4425 goto out_unlock;
4426 switch (s->sc_type) {
4427 case NFS4_DELEG_STID:
4428 status = nfs_ok;
4429 break;
4430 case NFS4_REVOKED_DELEG_STID:
4431 status = nfserr_deleg_revoked;
4432 break;
4433 case NFS4_OPEN_STID:
4434 case NFS4_LOCK_STID:
4435 status = nfsd4_check_openowner_confirmed(openlockstateid(s));
4436 break;
4437 default:
4438 printk("unknown stateid type %x\n", s->sc_type);
4439 /* Fallthrough */
4440 case NFS4_CLOSED_STID:
4441 case NFS4_CLOSED_DELEG_STID:
4442 status = nfserr_bad_stateid;
4444 out_unlock:
4445 spin_unlock(&cl->cl_lock);
4446 return status;
4449 static __be32
4450 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4451 stateid_t *stateid, unsigned char typemask,
4452 struct nfs4_stid **s, struct nfsd_net *nn)
4454 __be32 status;
4456 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4457 return nfserr_bad_stateid;
4458 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4459 if (status == nfserr_stale_clientid) {
4460 if (cstate->session)
4461 return nfserr_bad_stateid;
4462 return nfserr_stale_stateid;
4464 if (status)
4465 return status;
4466 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4467 if (!*s)
4468 return nfserr_bad_stateid;
4469 return nfs_ok;
4472 static struct file *
4473 nfs4_find_file(struct nfs4_stid *s, int flags)
4475 switch (s->sc_type) {
4476 case NFS4_DELEG_STID:
4477 if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file))
4478 return NULL;
4479 return get_file(s->sc_file->fi_deleg_file);
4480 case NFS4_OPEN_STID:
4481 case NFS4_LOCK_STID:
4482 if (flags & RD_STATE)
4483 return find_readable_file(s->sc_file);
4484 else
4485 return find_writeable_file(s->sc_file);
4486 break;
4489 return NULL;
4492 static __be32
4493 nfs4_check_olstateid(struct svc_fh *fhp, struct nfs4_ol_stateid *ols, int flags)
4495 __be32 status;
4497 status = nfsd4_check_openowner_confirmed(ols);
4498 if (status)
4499 return status;
4500 return nfs4_check_openmode(ols, flags);
4504 * Checks for stateid operations
4506 __be32
4507 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4508 stateid_t *stateid, int flags, struct file **filpp)
4510 struct svc_fh *fhp = &cstate->current_fh;
4511 struct inode *ino = fhp->fh_dentry->d_inode;
4513 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4514 struct nfs4_stid *s;
4515 __be32 status;
4517 if (filpp)
4518 *filpp = NULL;
4520 if (grace_disallows_io(net, ino))
4521 return nfserr_grace;
4523 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4524 return check_special_stateids(net, fhp, stateid, flags);
4526 status = nfsd4_lookup_stateid(cstate, stateid,
4527 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4528 &s, nn);
4529 if (status)
4530 return status;
4531 status = check_stateid_generation(stateid, &s->sc_stateid,
4532 nfsd4_has_session(cstate));
4533 if (status)
4534 goto out;
4536 switch (s->sc_type) {
4537 case NFS4_DELEG_STID:
4538 status = nfs4_check_delegmode(delegstateid(s), flags);
4539 break;
4540 case NFS4_OPEN_STID:
4541 case NFS4_LOCK_STID:
4542 status = nfs4_check_olstateid(fhp, openlockstateid(s), flags);
4543 break;
4544 default:
4545 status = nfserr_bad_stateid;
4546 break;
4548 if (status)
4549 goto out;
4550 status = nfs4_check_fh(fhp, s);
4552 if (!status && filpp) {
4553 *filpp = nfs4_find_file(s, flags);
4554 if (!*filpp)
4555 status = nfserr_serverfault;
4557 out:
4558 nfs4_put_stid(s);
4559 return status;
4563 * Test if the stateid is valid
4565 __be32
4566 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4567 struct nfsd4_test_stateid *test_stateid)
4569 struct nfsd4_test_stateid_id *stateid;
4570 struct nfs4_client *cl = cstate->session->se_client;
4572 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4573 stateid->ts_id_status =
4574 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4576 return nfs_ok;
4579 __be32
4580 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4581 struct nfsd4_free_stateid *free_stateid)
4583 stateid_t *stateid = &free_stateid->fr_stateid;
4584 struct nfs4_stid *s;
4585 struct nfs4_delegation *dp;
4586 struct nfs4_ol_stateid *stp;
4587 struct nfs4_client *cl = cstate->session->se_client;
4588 __be32 ret = nfserr_bad_stateid;
4590 spin_lock(&cl->cl_lock);
4591 s = find_stateid_locked(cl, stateid);
4592 if (!s)
4593 goto out_unlock;
4594 switch (s->sc_type) {
4595 case NFS4_DELEG_STID:
4596 ret = nfserr_locks_held;
4597 break;
4598 case NFS4_OPEN_STID:
4599 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4600 if (ret)
4601 break;
4602 ret = nfserr_locks_held;
4603 break;
4604 case NFS4_LOCK_STID:
4605 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4606 if (ret)
4607 break;
4608 stp = openlockstateid(s);
4609 ret = nfserr_locks_held;
4610 if (check_for_locks(stp->st_stid.sc_file,
4611 lockowner(stp->st_stateowner)))
4612 break;
4613 unhash_lock_stateid(stp);
4614 spin_unlock(&cl->cl_lock);
4615 nfs4_put_stid(s);
4616 ret = nfs_ok;
4617 goto out;
4618 case NFS4_REVOKED_DELEG_STID:
4619 dp = delegstateid(s);
4620 list_del_init(&dp->dl_recall_lru);
4621 spin_unlock(&cl->cl_lock);
4622 nfs4_put_stid(s);
4623 ret = nfs_ok;
4624 goto out;
4625 /* Default falls through and returns nfserr_bad_stateid */
4627 out_unlock:
4628 spin_unlock(&cl->cl_lock);
4629 out:
4630 return ret;
4633 static inline int
4634 setlkflg (int type)
4636 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4637 RD_STATE : WR_STATE;
4640 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4642 struct svc_fh *current_fh = &cstate->current_fh;
4643 struct nfs4_stateowner *sop = stp->st_stateowner;
4644 __be32 status;
4646 status = nfsd4_check_seqid(cstate, sop, seqid);
4647 if (status)
4648 return status;
4649 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4650 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4652 * "Closed" stateid's exist *only* to return
4653 * nfserr_replay_me from the previous step, and
4654 * revoked delegations are kept only for free_stateid.
4656 return nfserr_bad_stateid;
4657 down_write(&stp->st_rwsem);
4658 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4659 if (status == nfs_ok)
4660 status = nfs4_check_fh(current_fh, &stp->st_stid);
4661 if (status != nfs_ok)
4662 up_write(&stp->st_rwsem);
4663 return status;
4667 * Checks for sequence id mutating operations.
4669 static __be32
4670 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4671 stateid_t *stateid, char typemask,
4672 struct nfs4_ol_stateid **stpp,
4673 struct nfsd_net *nn)
4675 __be32 status;
4676 struct nfs4_stid *s;
4677 struct nfs4_ol_stateid *stp = NULL;
4679 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4680 seqid, STATEID_VAL(stateid));
4682 *stpp = NULL;
4683 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4684 if (status)
4685 return status;
4686 stp = openlockstateid(s);
4687 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4689 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4690 if (!status)
4691 *stpp = stp;
4692 else
4693 nfs4_put_stid(&stp->st_stid);
4694 return status;
4697 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4698 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4700 __be32 status;
4701 struct nfs4_openowner *oo;
4702 struct nfs4_ol_stateid *stp;
4704 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4705 NFS4_OPEN_STID, &stp, nn);
4706 if (status)
4707 return status;
4708 oo = openowner(stp->st_stateowner);
4709 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4710 up_write(&stp->st_rwsem);
4711 nfs4_put_stid(&stp->st_stid);
4712 return nfserr_bad_stateid;
4714 *stpp = stp;
4715 return nfs_ok;
4718 __be32
4719 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4720 struct nfsd4_open_confirm *oc)
4722 __be32 status;
4723 struct nfs4_openowner *oo;
4724 struct nfs4_ol_stateid *stp;
4725 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4727 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4728 cstate->current_fh.fh_dentry);
4730 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4731 if (status)
4732 return status;
4734 status = nfs4_preprocess_seqid_op(cstate,
4735 oc->oc_seqid, &oc->oc_req_stateid,
4736 NFS4_OPEN_STID, &stp, nn);
4737 if (status)
4738 goto out;
4739 oo = openowner(stp->st_stateowner);
4740 status = nfserr_bad_stateid;
4741 if (oo->oo_flags & NFS4_OO_CONFIRMED) {
4742 up_write(&stp->st_rwsem);
4743 goto put_stateid;
4745 oo->oo_flags |= NFS4_OO_CONFIRMED;
4746 update_stateid(&stp->st_stid.sc_stateid);
4747 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4748 up_write(&stp->st_rwsem);
4749 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4750 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4752 nfsd4_client_record_create(oo->oo_owner.so_client);
4753 status = nfs_ok;
4754 put_stateid:
4755 nfs4_put_stid(&stp->st_stid);
4756 out:
4757 nfsd4_bump_seqid(cstate, status);
4758 return status;
4761 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4763 if (!test_access(access, stp))
4764 return;
4765 nfs4_file_put_access(stp->st_stid.sc_file, access);
4766 clear_access(access, stp);
4769 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4771 switch (to_access) {
4772 case NFS4_SHARE_ACCESS_READ:
4773 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4774 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4775 break;
4776 case NFS4_SHARE_ACCESS_WRITE:
4777 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4778 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4779 break;
4780 case NFS4_SHARE_ACCESS_BOTH:
4781 break;
4782 default:
4783 WARN_ON_ONCE(1);
4787 __be32
4788 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4789 struct nfsd4_compound_state *cstate,
4790 struct nfsd4_open_downgrade *od)
4792 __be32 status;
4793 struct nfs4_ol_stateid *stp;
4794 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4796 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4797 cstate->current_fh.fh_dentry);
4799 /* We don't yet support WANT bits: */
4800 if (od->od_deleg_want)
4801 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4802 od->od_deleg_want);
4804 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4805 &od->od_stateid, &stp, nn);
4806 if (status)
4807 goto out;
4808 status = nfserr_inval;
4809 if (!test_access(od->od_share_access, stp)) {
4810 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4811 stp->st_access_bmap, od->od_share_access);
4812 goto put_stateid;
4814 if (!test_deny(od->od_share_deny, stp)) {
4815 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4816 stp->st_deny_bmap, od->od_share_deny);
4817 goto put_stateid;
4819 nfs4_stateid_downgrade(stp, od->od_share_access);
4821 reset_union_bmap_deny(od->od_share_deny, stp);
4823 update_stateid(&stp->st_stid.sc_stateid);
4824 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4825 status = nfs_ok;
4826 put_stateid:
4827 up_write(&stp->st_rwsem);
4828 nfs4_put_stid(&stp->st_stid);
4829 out:
4830 nfsd4_bump_seqid(cstate, status);
4831 return status;
4834 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4836 struct nfs4_client *clp = s->st_stid.sc_client;
4837 LIST_HEAD(reaplist);
4839 s->st_stid.sc_type = NFS4_CLOSED_STID;
4840 spin_lock(&clp->cl_lock);
4841 unhash_open_stateid(s, &reaplist);
4843 if (clp->cl_minorversion) {
4844 put_ol_stateid_locked(s, &reaplist);
4845 spin_unlock(&clp->cl_lock);
4846 free_ol_stateid_reaplist(&reaplist);
4847 } else {
4848 spin_unlock(&clp->cl_lock);
4849 free_ol_stateid_reaplist(&reaplist);
4850 move_to_close_lru(s, clp->net);
4855 * nfs4_unlock_state() called after encode
4857 __be32
4858 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4859 struct nfsd4_close *close)
4861 __be32 status;
4862 struct nfs4_ol_stateid *stp;
4863 struct net *net = SVC_NET(rqstp);
4864 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4866 dprintk("NFSD: nfsd4_close on file %pd\n",
4867 cstate->current_fh.fh_dentry);
4869 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4870 &close->cl_stateid,
4871 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4872 &stp, nn);
4873 nfsd4_bump_seqid(cstate, status);
4874 if (status)
4875 goto out;
4876 update_stateid(&stp->st_stid.sc_stateid);
4877 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4878 up_write(&stp->st_rwsem);
4880 nfsd4_close_open_stateid(stp);
4882 /* put reference from nfs4_preprocess_seqid_op */
4883 nfs4_put_stid(&stp->st_stid);
4884 out:
4885 return status;
4888 __be32
4889 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4890 struct nfsd4_delegreturn *dr)
4892 struct nfs4_delegation *dp;
4893 stateid_t *stateid = &dr->dr_stateid;
4894 struct nfs4_stid *s;
4895 __be32 status;
4896 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4898 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4899 return status;
4901 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4902 if (status)
4903 goto out;
4904 dp = delegstateid(s);
4905 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4906 if (status)
4907 goto put_stateid;
4909 destroy_delegation(dp);
4910 put_stateid:
4911 nfs4_put_stid(&dp->dl_stid);
4912 out:
4913 return status;
4917 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4919 static inline u64
4920 end_offset(u64 start, u64 len)
4922 u64 end;
4924 end = start + len;
4925 return end >= start ? end: NFS4_MAX_UINT64;
4928 /* last octet in a range */
4929 static inline u64
4930 last_byte_offset(u64 start, u64 len)
4932 u64 end;
4934 WARN_ON_ONCE(!len);
4935 end = start + len;
4936 return end > start ? end - 1: NFS4_MAX_UINT64;
4940 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4941 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4942 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4943 * locking, this prevents us from being completely protocol-compliant. The
4944 * real solution to this problem is to start using unsigned file offsets in
4945 * the VFS, but this is a very deep change!
4947 static inline void
4948 nfs4_transform_lock_offset(struct file_lock *lock)
4950 if (lock->fl_start < 0)
4951 lock->fl_start = OFFSET_MAX;
4952 if (lock->fl_end < 0)
4953 lock->fl_end = OFFSET_MAX;
4956 static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4958 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4959 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4962 static void nfsd4_fl_put_owner(struct file_lock *fl)
4964 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4966 if (lo) {
4967 nfs4_put_stateowner(&lo->lo_owner);
4968 fl->fl_owner = NULL;
4972 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4973 .lm_get_owner = nfsd4_fl_get_owner,
4974 .lm_put_owner = nfsd4_fl_put_owner,
4977 static inline void
4978 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4980 struct nfs4_lockowner *lo;
4982 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4983 lo = (struct nfs4_lockowner *) fl->fl_owner;
4984 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4985 lo->lo_owner.so_owner.len, GFP_KERNEL);
4986 if (!deny->ld_owner.data)
4987 /* We just don't care that much */
4988 goto nevermind;
4989 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4990 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4991 } else {
4992 nevermind:
4993 deny->ld_owner.len = 0;
4994 deny->ld_owner.data = NULL;
4995 deny->ld_clientid.cl_boot = 0;
4996 deny->ld_clientid.cl_id = 0;
4998 deny->ld_start = fl->fl_start;
4999 deny->ld_length = NFS4_MAX_UINT64;
5000 if (fl->fl_end != NFS4_MAX_UINT64)
5001 deny->ld_length = fl->fl_end - fl->fl_start + 1;
5002 deny->ld_type = NFS4_READ_LT;
5003 if (fl->fl_type != F_RDLCK)
5004 deny->ld_type = NFS4_WRITE_LT;
5007 static struct nfs4_lockowner *
5008 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
5009 struct nfs4_client *clp)
5011 unsigned int strhashval = ownerstr_hashval(owner);
5012 struct nfs4_stateowner *so;
5014 lockdep_assert_held(&clp->cl_lock);
5016 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
5017 so_strhash) {
5018 if (so->so_is_open_owner)
5019 continue;
5020 if (same_owner_str(so, owner))
5021 return lockowner(nfs4_get_stateowner(so));
5023 return NULL;
5026 static struct nfs4_lockowner *
5027 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
5028 struct nfs4_client *clp)
5030 struct nfs4_lockowner *lo;
5032 spin_lock(&clp->cl_lock);
5033 lo = find_lockowner_str_locked(clid, owner, clp);
5034 spin_unlock(&clp->cl_lock);
5035 return lo;
5038 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5040 unhash_lockowner_locked(lockowner(sop));
5043 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5045 struct nfs4_lockowner *lo = lockowner(sop);
5047 kmem_cache_free(lockowner_slab, lo);
5050 static const struct nfs4_stateowner_operations lockowner_ops = {
5051 .so_unhash = nfs4_unhash_lockowner,
5052 .so_free = nfs4_free_lockowner,
5056 * Alloc a lock owner structure.
5057 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5058 * occurred.
5060 * strhashval = ownerstr_hashval
5062 static struct nfs4_lockowner *
5063 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5064 struct nfs4_ol_stateid *open_stp,
5065 struct nfsd4_lock *lock)
5067 struct nfs4_lockowner *lo, *ret;
5069 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5070 if (!lo)
5071 return NULL;
5072 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5073 lo->lo_owner.so_is_open_owner = 0;
5074 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5075 lo->lo_owner.so_ops = &lockowner_ops;
5076 spin_lock(&clp->cl_lock);
5077 ret = find_lockowner_str_locked(&clp->cl_clientid,
5078 &lock->lk_new_owner, clp);
5079 if (ret == NULL) {
5080 list_add(&lo->lo_owner.so_strhash,
5081 &clp->cl_ownerstr_hashtbl[strhashval]);
5082 ret = lo;
5083 } else
5084 nfs4_free_lockowner(&lo->lo_owner);
5085 spin_unlock(&clp->cl_lock);
5086 return ret;
5089 static void
5090 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5091 struct nfs4_file *fp, struct inode *inode,
5092 struct nfs4_ol_stateid *open_stp)
5094 struct nfs4_client *clp = lo->lo_owner.so_client;
5096 lockdep_assert_held(&clp->cl_lock);
5098 atomic_inc(&stp->st_stid.sc_count);
5099 stp->st_stid.sc_type = NFS4_LOCK_STID;
5100 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5101 get_nfs4_file(fp);
5102 stp->st_stid.sc_file = fp;
5103 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5104 stp->st_access_bmap = 0;
5105 stp->st_deny_bmap = open_stp->st_deny_bmap;
5106 stp->st_openstp = open_stp;
5107 init_rwsem(&stp->st_rwsem);
5108 list_add(&stp->st_locks, &open_stp->st_locks);
5109 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5110 spin_lock(&fp->fi_lock);
5111 list_add(&stp->st_perfile, &fp->fi_stateids);
5112 spin_unlock(&fp->fi_lock);
5115 static struct nfs4_ol_stateid *
5116 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5118 struct nfs4_ol_stateid *lst;
5119 struct nfs4_client *clp = lo->lo_owner.so_client;
5121 lockdep_assert_held(&clp->cl_lock);
5123 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5124 if (lst->st_stid.sc_file == fp) {
5125 atomic_inc(&lst->st_stid.sc_count);
5126 return lst;
5129 return NULL;
5132 static struct nfs4_ol_stateid *
5133 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5134 struct inode *inode, struct nfs4_ol_stateid *ost,
5135 bool *new)
5137 struct nfs4_stid *ns = NULL;
5138 struct nfs4_ol_stateid *lst;
5139 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5140 struct nfs4_client *clp = oo->oo_owner.so_client;
5142 spin_lock(&clp->cl_lock);
5143 lst = find_lock_stateid(lo, fi);
5144 if (lst == NULL) {
5145 spin_unlock(&clp->cl_lock);
5146 ns = nfs4_alloc_stid(clp, stateid_slab);
5147 if (ns == NULL)
5148 return NULL;
5150 spin_lock(&clp->cl_lock);
5151 lst = find_lock_stateid(lo, fi);
5152 if (likely(!lst)) {
5153 lst = openlockstateid(ns);
5154 init_lock_stateid(lst, lo, fi, inode, ost);
5155 ns = NULL;
5156 *new = true;
5159 spin_unlock(&clp->cl_lock);
5160 if (ns)
5161 nfs4_put_stid(ns);
5162 return lst;
5165 static int
5166 check_lock_length(u64 offset, u64 length)
5168 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5169 LOFF_OVERFLOW(offset, length)));
5172 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5174 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5176 lockdep_assert_held(&fp->fi_lock);
5178 if (test_access(access, lock_stp))
5179 return;
5180 __nfs4_file_get_access(fp, access);
5181 set_access(access, lock_stp);
5184 static __be32
5185 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5186 struct nfs4_ol_stateid *ost,
5187 struct nfsd4_lock *lock,
5188 struct nfs4_ol_stateid **lst, bool *new)
5190 __be32 status;
5191 struct nfs4_file *fi = ost->st_stid.sc_file;
5192 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5193 struct nfs4_client *cl = oo->oo_owner.so_client;
5194 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5195 struct nfs4_lockowner *lo;
5196 unsigned int strhashval;
5198 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5199 if (!lo) {
5200 strhashval = ownerstr_hashval(&lock->v.new.owner);
5201 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5202 if (lo == NULL)
5203 return nfserr_jukebox;
5204 } else {
5205 /* with an existing lockowner, seqids must be the same */
5206 status = nfserr_bad_seqid;
5207 if (!cstate->minorversion &&
5208 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5209 goto out;
5212 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5213 if (*lst == NULL) {
5214 status = nfserr_jukebox;
5215 goto out;
5217 status = nfs_ok;
5218 out:
5219 nfs4_put_stateowner(&lo->lo_owner);
5220 return status;
5224 * LOCK operation
5226 __be32
5227 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5228 struct nfsd4_lock *lock)
5230 struct nfs4_openowner *open_sop = NULL;
5231 struct nfs4_lockowner *lock_sop = NULL;
5232 struct nfs4_ol_stateid *lock_stp = NULL;
5233 struct nfs4_ol_stateid *open_stp = NULL;
5234 struct nfs4_file *fp;
5235 struct file *filp = NULL;
5236 struct file_lock *file_lock = NULL;
5237 struct file_lock *conflock = NULL;
5238 __be32 status = 0;
5239 int lkflg;
5240 int err;
5241 bool new = false;
5242 struct net *net = SVC_NET(rqstp);
5243 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5245 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5246 (long long) lock->lk_offset,
5247 (long long) lock->lk_length);
5249 if (check_lock_length(lock->lk_offset, lock->lk_length))
5250 return nfserr_inval;
5252 if ((status = fh_verify(rqstp, &cstate->current_fh,
5253 S_IFREG, NFSD_MAY_LOCK))) {
5254 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5255 return status;
5258 if (lock->lk_is_new) {
5259 if (nfsd4_has_session(cstate))
5260 /* See rfc 5661 18.10.3: given clientid is ignored: */
5261 memcpy(&lock->v.new.clientid,
5262 &cstate->session->se_client->cl_clientid,
5263 sizeof(clientid_t));
5265 status = nfserr_stale_clientid;
5266 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5267 goto out;
5269 /* validate and update open stateid and open seqid */
5270 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5271 lock->lk_new_open_seqid,
5272 &lock->lk_new_open_stateid,
5273 &open_stp, nn);
5274 if (status)
5275 goto out;
5276 up_write(&open_stp->st_rwsem);
5277 open_sop = openowner(open_stp->st_stateowner);
5278 status = nfserr_bad_stateid;
5279 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5280 &lock->v.new.clientid))
5281 goto out;
5282 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5283 &lock_stp, &new);
5284 if (status == nfs_ok)
5285 down_write(&lock_stp->st_rwsem);
5286 } else {
5287 status = nfs4_preprocess_seqid_op(cstate,
5288 lock->lk_old_lock_seqid,
5289 &lock->lk_old_lock_stateid,
5290 NFS4_LOCK_STID, &lock_stp, nn);
5292 if (status)
5293 goto out;
5294 lock_sop = lockowner(lock_stp->st_stateowner);
5296 lkflg = setlkflg(lock->lk_type);
5297 status = nfs4_check_openmode(lock_stp, lkflg);
5298 if (status)
5299 goto out;
5301 status = nfserr_grace;
5302 if (locks_in_grace(net) && !lock->lk_reclaim)
5303 goto out;
5304 status = nfserr_no_grace;
5305 if (!locks_in_grace(net) && lock->lk_reclaim)
5306 goto out;
5308 file_lock = locks_alloc_lock();
5309 if (!file_lock) {
5310 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5311 status = nfserr_jukebox;
5312 goto out;
5315 fp = lock_stp->st_stid.sc_file;
5316 switch (lock->lk_type) {
5317 case NFS4_READ_LT:
5318 case NFS4_READW_LT:
5319 spin_lock(&fp->fi_lock);
5320 filp = find_readable_file_locked(fp);
5321 if (filp)
5322 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5323 spin_unlock(&fp->fi_lock);
5324 file_lock->fl_type = F_RDLCK;
5325 break;
5326 case NFS4_WRITE_LT:
5327 case NFS4_WRITEW_LT:
5328 spin_lock(&fp->fi_lock);
5329 filp = find_writeable_file_locked(fp);
5330 if (filp)
5331 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5332 spin_unlock(&fp->fi_lock);
5333 file_lock->fl_type = F_WRLCK;
5334 break;
5335 default:
5336 status = nfserr_inval;
5337 goto out;
5339 if (!filp) {
5340 status = nfserr_openmode;
5341 goto out;
5344 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5345 file_lock->fl_pid = current->tgid;
5346 file_lock->fl_file = filp;
5347 file_lock->fl_flags = FL_POSIX;
5348 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5349 file_lock->fl_start = lock->lk_offset;
5350 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5351 nfs4_transform_lock_offset(file_lock);
5353 conflock = locks_alloc_lock();
5354 if (!conflock) {
5355 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5356 status = nfserr_jukebox;
5357 goto out;
5360 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5361 switch (-err) {
5362 case 0: /* success! */
5363 update_stateid(&lock_stp->st_stid.sc_stateid);
5364 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5365 sizeof(stateid_t));
5366 status = 0;
5367 break;
5368 case (EAGAIN): /* conflock holds conflicting lock */
5369 status = nfserr_denied;
5370 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5371 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5372 break;
5373 case (EDEADLK):
5374 status = nfserr_deadlock;
5375 break;
5376 default:
5377 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5378 status = nfserrno(err);
5379 break;
5381 out:
5382 if (filp)
5383 fput(filp);
5384 if (lock_stp) {
5385 /* Bump seqid manually if the 4.0 replay owner is openowner */
5386 if (cstate->replay_owner &&
5387 cstate->replay_owner != &lock_sop->lo_owner &&
5388 seqid_mutating_err(ntohl(status)))
5389 lock_sop->lo_owner.so_seqid++;
5391 up_write(&lock_stp->st_rwsem);
5394 * If this is a new, never-before-used stateid, and we are
5395 * returning an error, then just go ahead and release it.
5397 if (status && new)
5398 release_lock_stateid(lock_stp);
5400 nfs4_put_stid(&lock_stp->st_stid);
5402 if (open_stp)
5403 nfs4_put_stid(&open_stp->st_stid);
5404 nfsd4_bump_seqid(cstate, status);
5405 if (file_lock)
5406 locks_free_lock(file_lock);
5407 if (conflock)
5408 locks_free_lock(conflock);
5409 return status;
5413 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5414 * so we do a temporary open here just to get an open file to pass to
5415 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5416 * inode operation.)
5418 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5420 struct file *file;
5421 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5422 if (!err) {
5423 err = nfserrno(vfs_test_lock(file, lock));
5424 nfsd_close(file);
5426 return err;
5430 * LOCKT operation
5432 __be32
5433 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5434 struct nfsd4_lockt *lockt)
5436 struct file_lock *file_lock = NULL;
5437 struct nfs4_lockowner *lo = NULL;
5438 __be32 status;
5439 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5441 if (locks_in_grace(SVC_NET(rqstp)))
5442 return nfserr_grace;
5444 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5445 return nfserr_inval;
5447 if (!nfsd4_has_session(cstate)) {
5448 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5449 if (status)
5450 goto out;
5453 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5454 goto out;
5456 file_lock = locks_alloc_lock();
5457 if (!file_lock) {
5458 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5459 status = nfserr_jukebox;
5460 goto out;
5463 switch (lockt->lt_type) {
5464 case NFS4_READ_LT:
5465 case NFS4_READW_LT:
5466 file_lock->fl_type = F_RDLCK;
5467 break;
5468 case NFS4_WRITE_LT:
5469 case NFS4_WRITEW_LT:
5470 file_lock->fl_type = F_WRLCK;
5471 break;
5472 default:
5473 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5474 status = nfserr_inval;
5475 goto out;
5478 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5479 cstate->clp);
5480 if (lo)
5481 file_lock->fl_owner = (fl_owner_t)lo;
5482 file_lock->fl_pid = current->tgid;
5483 file_lock->fl_flags = FL_POSIX;
5485 file_lock->fl_start = lockt->lt_offset;
5486 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5488 nfs4_transform_lock_offset(file_lock);
5490 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5491 if (status)
5492 goto out;
5494 if (file_lock->fl_type != F_UNLCK) {
5495 status = nfserr_denied;
5496 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5498 out:
5499 if (lo)
5500 nfs4_put_stateowner(&lo->lo_owner);
5501 if (file_lock)
5502 locks_free_lock(file_lock);
5503 return status;
5506 __be32
5507 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5508 struct nfsd4_locku *locku)
5510 struct nfs4_ol_stateid *stp;
5511 struct file *filp = NULL;
5512 struct file_lock *file_lock = NULL;
5513 __be32 status;
5514 int err;
5515 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5517 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5518 (long long) locku->lu_offset,
5519 (long long) locku->lu_length);
5521 if (check_lock_length(locku->lu_offset, locku->lu_length))
5522 return nfserr_inval;
5524 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5525 &locku->lu_stateid, NFS4_LOCK_STID,
5526 &stp, nn);
5527 if (status)
5528 goto out;
5529 filp = find_any_file(stp->st_stid.sc_file);
5530 if (!filp) {
5531 status = nfserr_lock_range;
5532 goto put_stateid;
5534 file_lock = locks_alloc_lock();
5535 if (!file_lock) {
5536 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5537 status = nfserr_jukebox;
5538 goto fput;
5541 file_lock->fl_type = F_UNLCK;
5542 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5543 file_lock->fl_pid = current->tgid;
5544 file_lock->fl_file = filp;
5545 file_lock->fl_flags = FL_POSIX;
5546 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5547 file_lock->fl_start = locku->lu_offset;
5549 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5550 locku->lu_length);
5551 nfs4_transform_lock_offset(file_lock);
5553 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5554 if (err) {
5555 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5556 goto out_nfserr;
5558 update_stateid(&stp->st_stid.sc_stateid);
5559 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5560 fput:
5561 fput(filp);
5562 put_stateid:
5563 up_write(&stp->st_rwsem);
5564 nfs4_put_stid(&stp->st_stid);
5565 out:
5566 nfsd4_bump_seqid(cstate, status);
5567 if (file_lock)
5568 locks_free_lock(file_lock);
5569 return status;
5571 out_nfserr:
5572 status = nfserrno(err);
5573 goto fput;
5577 * returns
5578 * true: locks held by lockowner
5579 * false: no locks held by lockowner
5581 static bool
5582 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5584 struct file_lock **flpp;
5585 int status = false;
5586 struct file *filp = find_any_file(fp);
5587 struct inode *inode;
5589 if (!filp) {
5590 /* Any valid lock stateid should have some sort of access */
5591 WARN_ON_ONCE(1);
5592 return status;
5595 inode = file_inode(filp);
5597 spin_lock(&inode->i_lock);
5598 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5599 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5600 status = true;
5601 break;
5604 spin_unlock(&inode->i_lock);
5605 fput(filp);
5606 return status;
5609 __be32
5610 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5611 struct nfsd4_compound_state *cstate,
5612 struct nfsd4_release_lockowner *rlockowner)
5614 clientid_t *clid = &rlockowner->rl_clientid;
5615 struct nfs4_stateowner *sop;
5616 struct nfs4_lockowner *lo = NULL;
5617 struct nfs4_ol_stateid *stp;
5618 struct xdr_netobj *owner = &rlockowner->rl_owner;
5619 unsigned int hashval = ownerstr_hashval(owner);
5620 __be32 status;
5621 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5622 struct nfs4_client *clp;
5624 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5625 clid->cl_boot, clid->cl_id);
5627 status = lookup_clientid(clid, cstate, nn);
5628 if (status)
5629 return status;
5631 clp = cstate->clp;
5632 /* Find the matching lock stateowner */
5633 spin_lock(&clp->cl_lock);
5634 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5635 so_strhash) {
5637 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5638 continue;
5640 /* see if there are still any locks associated with it */
5641 lo = lockowner(sop);
5642 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5643 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5644 status = nfserr_locks_held;
5645 spin_unlock(&clp->cl_lock);
5646 return status;
5650 nfs4_get_stateowner(sop);
5651 break;
5653 spin_unlock(&clp->cl_lock);
5654 if (lo)
5655 release_lockowner(lo);
5656 return status;
5659 static inline struct nfs4_client_reclaim *
5660 alloc_reclaim(void)
5662 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5665 bool
5666 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5668 struct nfs4_client_reclaim *crp;
5670 crp = nfsd4_find_reclaim_client(name, nn);
5671 return (crp && crp->cr_clp);
5675 * failure => all reset bets are off, nfserr_no_grace...
5677 struct nfs4_client_reclaim *
5678 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5680 unsigned int strhashval;
5681 struct nfs4_client_reclaim *crp;
5683 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5684 crp = alloc_reclaim();
5685 if (crp) {
5686 strhashval = clientstr_hashval(name);
5687 INIT_LIST_HEAD(&crp->cr_strhash);
5688 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5689 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5690 crp->cr_clp = NULL;
5691 nn->reclaim_str_hashtbl_size++;
5693 return crp;
5696 void
5697 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5699 list_del(&crp->cr_strhash);
5700 kfree(crp);
5701 nn->reclaim_str_hashtbl_size--;
5704 void
5705 nfs4_release_reclaim(struct nfsd_net *nn)
5707 struct nfs4_client_reclaim *crp = NULL;
5708 int i;
5710 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5711 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5712 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5713 struct nfs4_client_reclaim, cr_strhash);
5714 nfs4_remove_reclaim_record(crp, nn);
5717 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5721 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5722 struct nfs4_client_reclaim *
5723 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5725 unsigned int strhashval;
5726 struct nfs4_client_reclaim *crp = NULL;
5728 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5730 strhashval = clientstr_hashval(recdir);
5731 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5732 if (same_name(crp->cr_recdir, recdir)) {
5733 return crp;
5736 return NULL;
5740 * Called from OPEN. Look for clientid in reclaim list.
5742 __be32
5743 nfs4_check_open_reclaim(clientid_t *clid,
5744 struct nfsd4_compound_state *cstate,
5745 struct nfsd_net *nn)
5747 __be32 status;
5749 /* find clientid in conf_id_hashtbl */
5750 status = lookup_clientid(clid, cstate, nn);
5751 if (status)
5752 return nfserr_reclaim_bad;
5754 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5755 return nfserr_no_grace;
5757 if (nfsd4_client_record_check(cstate->clp))
5758 return nfserr_reclaim_bad;
5760 return nfs_ok;
5763 #ifdef CONFIG_NFSD_FAULT_INJECTION
5764 static inline void
5765 put_client(struct nfs4_client *clp)
5767 atomic_dec(&clp->cl_refcount);
5770 static struct nfs4_client *
5771 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5773 struct nfs4_client *clp;
5774 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5775 nfsd_net_id);
5777 if (!nfsd_netns_ready(nn))
5778 return NULL;
5780 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5781 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5782 return clp;
5784 return NULL;
5788 nfsd_inject_print_clients(void)
5790 struct nfs4_client *clp;
5791 u64 count = 0;
5792 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5793 nfsd_net_id);
5794 char buf[INET6_ADDRSTRLEN];
5796 if (!nfsd_netns_ready(nn))
5797 return 0;
5799 spin_lock(&nn->client_lock);
5800 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5801 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5802 pr_info("NFS Client: %s\n", buf);
5803 ++count;
5805 spin_unlock(&nn->client_lock);
5807 return count;
5811 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5813 u64 count = 0;
5814 struct nfs4_client *clp;
5815 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5816 nfsd_net_id);
5818 if (!nfsd_netns_ready(nn))
5819 return count;
5821 spin_lock(&nn->client_lock);
5822 clp = nfsd_find_client(addr, addr_size);
5823 if (clp) {
5824 if (mark_client_expired_locked(clp) == nfs_ok)
5825 ++count;
5826 else
5827 clp = NULL;
5829 spin_unlock(&nn->client_lock);
5831 if (clp)
5832 expire_client(clp);
5834 return count;
5838 nfsd_inject_forget_clients(u64 max)
5840 u64 count = 0;
5841 struct nfs4_client *clp, *next;
5842 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5843 nfsd_net_id);
5844 LIST_HEAD(reaplist);
5846 if (!nfsd_netns_ready(nn))
5847 return count;
5849 spin_lock(&nn->client_lock);
5850 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5851 if (mark_client_expired_locked(clp) == nfs_ok) {
5852 list_add(&clp->cl_lru, &reaplist);
5853 if (max != 0 && ++count >= max)
5854 break;
5857 spin_unlock(&nn->client_lock);
5859 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5860 expire_client(clp);
5862 return count;
5865 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5866 const char *type)
5868 char buf[INET6_ADDRSTRLEN];
5869 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5870 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5873 static void
5874 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5875 struct list_head *collect)
5877 struct nfs4_client *clp = lst->st_stid.sc_client;
5878 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5879 nfsd_net_id);
5881 if (!collect)
5882 return;
5884 lockdep_assert_held(&nn->client_lock);
5885 atomic_inc(&clp->cl_refcount);
5886 list_add(&lst->st_locks, collect);
5889 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5890 struct list_head *collect,
5891 void (*func)(struct nfs4_ol_stateid *))
5893 struct nfs4_openowner *oop;
5894 struct nfs4_ol_stateid *stp, *st_next;
5895 struct nfs4_ol_stateid *lst, *lst_next;
5896 u64 count = 0;
5898 spin_lock(&clp->cl_lock);
5899 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5900 list_for_each_entry_safe(stp, st_next,
5901 &oop->oo_owner.so_stateids, st_perstateowner) {
5902 list_for_each_entry_safe(lst, lst_next,
5903 &stp->st_locks, st_locks) {
5904 if (func) {
5905 func(lst);
5906 nfsd_inject_add_lock_to_list(lst,
5907 collect);
5909 ++count;
5911 * Despite the fact that these functions deal
5912 * with 64-bit integers for "count", we must
5913 * ensure that it doesn't blow up the
5914 * clp->cl_refcount. Throw a warning if we
5915 * start to approach INT_MAX here.
5917 WARN_ON_ONCE(count == (INT_MAX / 2));
5918 if (count == max)
5919 goto out;
5923 out:
5924 spin_unlock(&clp->cl_lock);
5926 return count;
5929 static u64
5930 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5931 u64 max)
5933 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
5936 static u64
5937 nfsd_print_client_locks(struct nfs4_client *clp)
5939 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
5940 nfsd_print_count(clp, count, "locked files");
5941 return count;
5945 nfsd_inject_print_locks(void)
5947 struct nfs4_client *clp;
5948 u64 count = 0;
5949 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5950 nfsd_net_id);
5952 if (!nfsd_netns_ready(nn))
5953 return 0;
5955 spin_lock(&nn->client_lock);
5956 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5957 count += nfsd_print_client_locks(clp);
5958 spin_unlock(&nn->client_lock);
5960 return count;
5963 static void
5964 nfsd_reap_locks(struct list_head *reaplist)
5966 struct nfs4_client *clp;
5967 struct nfs4_ol_stateid *stp, *next;
5969 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5970 list_del_init(&stp->st_locks);
5971 clp = stp->st_stid.sc_client;
5972 nfs4_put_stid(&stp->st_stid);
5973 put_client(clp);
5978 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
5980 unsigned int count = 0;
5981 struct nfs4_client *clp;
5982 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5983 nfsd_net_id);
5984 LIST_HEAD(reaplist);
5986 if (!nfsd_netns_ready(nn))
5987 return count;
5989 spin_lock(&nn->client_lock);
5990 clp = nfsd_find_client(addr, addr_size);
5991 if (clp)
5992 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5993 spin_unlock(&nn->client_lock);
5994 nfsd_reap_locks(&reaplist);
5995 return count;
5999 nfsd_inject_forget_locks(u64 max)
6001 u64 count = 0;
6002 struct nfs4_client *clp;
6003 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6004 nfsd_net_id);
6005 LIST_HEAD(reaplist);
6007 if (!nfsd_netns_ready(nn))
6008 return count;
6010 spin_lock(&nn->client_lock);
6011 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6012 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
6013 if (max != 0 && count >= max)
6014 break;
6016 spin_unlock(&nn->client_lock);
6017 nfsd_reap_locks(&reaplist);
6018 return count;
6021 static u64
6022 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
6023 struct list_head *collect,
6024 void (*func)(struct nfs4_openowner *))
6026 struct nfs4_openowner *oop, *next;
6027 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6028 nfsd_net_id);
6029 u64 count = 0;
6031 lockdep_assert_held(&nn->client_lock);
6033 spin_lock(&clp->cl_lock);
6034 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6035 if (func) {
6036 func(oop);
6037 if (collect) {
6038 atomic_inc(&clp->cl_refcount);
6039 list_add(&oop->oo_perclient, collect);
6042 ++count;
6044 * Despite the fact that these functions deal with
6045 * 64-bit integers for "count", we must ensure that
6046 * it doesn't blow up the clp->cl_refcount. Throw a
6047 * warning if we start to approach INT_MAX here.
6049 WARN_ON_ONCE(count == (INT_MAX / 2));
6050 if (count == max)
6051 break;
6053 spin_unlock(&clp->cl_lock);
6055 return count;
6058 static u64
6059 nfsd_print_client_openowners(struct nfs4_client *clp)
6061 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6063 nfsd_print_count(clp, count, "openowners");
6064 return count;
6067 static u64
6068 nfsd_collect_client_openowners(struct nfs4_client *clp,
6069 struct list_head *collect, u64 max)
6071 return nfsd_foreach_client_openowner(clp, max, collect,
6072 unhash_openowner_locked);
6076 nfsd_inject_print_openowners(void)
6078 struct nfs4_client *clp;
6079 u64 count = 0;
6080 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6081 nfsd_net_id);
6083 if (!nfsd_netns_ready(nn))
6084 return 0;
6086 spin_lock(&nn->client_lock);
6087 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6088 count += nfsd_print_client_openowners(clp);
6089 spin_unlock(&nn->client_lock);
6091 return count;
6094 static void
6095 nfsd_reap_openowners(struct list_head *reaplist)
6097 struct nfs4_client *clp;
6098 struct nfs4_openowner *oop, *next;
6100 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6101 list_del_init(&oop->oo_perclient);
6102 clp = oop->oo_owner.so_client;
6103 release_openowner(oop);
6104 put_client(clp);
6109 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6110 size_t addr_size)
6112 unsigned int count = 0;
6113 struct nfs4_client *clp;
6114 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6115 nfsd_net_id);
6116 LIST_HEAD(reaplist);
6118 if (!nfsd_netns_ready(nn))
6119 return count;
6121 spin_lock(&nn->client_lock);
6122 clp = nfsd_find_client(addr, addr_size);
6123 if (clp)
6124 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6125 spin_unlock(&nn->client_lock);
6126 nfsd_reap_openowners(&reaplist);
6127 return count;
6131 nfsd_inject_forget_openowners(u64 max)
6133 u64 count = 0;
6134 struct nfs4_client *clp;
6135 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6136 nfsd_net_id);
6137 LIST_HEAD(reaplist);
6139 if (!nfsd_netns_ready(nn))
6140 return count;
6142 spin_lock(&nn->client_lock);
6143 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6144 count += nfsd_collect_client_openowners(clp, &reaplist,
6145 max - count);
6146 if (max != 0 && count >= max)
6147 break;
6149 spin_unlock(&nn->client_lock);
6150 nfsd_reap_openowners(&reaplist);
6151 return count;
6154 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6155 struct list_head *victims)
6157 struct nfs4_delegation *dp, *next;
6158 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6159 nfsd_net_id);
6160 u64 count = 0;
6162 lockdep_assert_held(&nn->client_lock);
6164 spin_lock(&state_lock);
6165 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6166 if (victims) {
6168 * It's not safe to mess with delegations that have a
6169 * non-zero dl_time. They might have already been broken
6170 * and could be processed by the laundromat outside of
6171 * the state_lock. Just leave them be.
6173 if (dp->dl_time != 0)
6174 continue;
6176 atomic_inc(&clp->cl_refcount);
6177 unhash_delegation_locked(dp);
6178 list_add(&dp->dl_recall_lru, victims);
6180 ++count;
6182 * Despite the fact that these functions deal with
6183 * 64-bit integers for "count", we must ensure that
6184 * it doesn't blow up the clp->cl_refcount. Throw a
6185 * warning if we start to approach INT_MAX here.
6187 WARN_ON_ONCE(count == (INT_MAX / 2));
6188 if (count == max)
6189 break;
6191 spin_unlock(&state_lock);
6192 return count;
6195 static u64
6196 nfsd_print_client_delegations(struct nfs4_client *clp)
6198 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6200 nfsd_print_count(clp, count, "delegations");
6201 return count;
6205 nfsd_inject_print_delegations(void)
6207 struct nfs4_client *clp;
6208 u64 count = 0;
6209 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6210 nfsd_net_id);
6212 if (!nfsd_netns_ready(nn))
6213 return 0;
6215 spin_lock(&nn->client_lock);
6216 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6217 count += nfsd_print_client_delegations(clp);
6218 spin_unlock(&nn->client_lock);
6220 return count;
6223 static void
6224 nfsd_forget_delegations(struct list_head *reaplist)
6226 struct nfs4_client *clp;
6227 struct nfs4_delegation *dp, *next;
6229 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6230 list_del_init(&dp->dl_recall_lru);
6231 clp = dp->dl_stid.sc_client;
6232 revoke_delegation(dp);
6233 put_client(clp);
6238 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6239 size_t addr_size)
6241 u64 count = 0;
6242 struct nfs4_client *clp;
6243 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6244 nfsd_net_id);
6245 LIST_HEAD(reaplist);
6247 if (!nfsd_netns_ready(nn))
6248 return count;
6250 spin_lock(&nn->client_lock);
6251 clp = nfsd_find_client(addr, addr_size);
6252 if (clp)
6253 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6254 spin_unlock(&nn->client_lock);
6256 nfsd_forget_delegations(&reaplist);
6257 return count;
6261 nfsd_inject_forget_delegations(u64 max)
6263 u64 count = 0;
6264 struct nfs4_client *clp;
6265 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6266 nfsd_net_id);
6267 LIST_HEAD(reaplist);
6269 if (!nfsd_netns_ready(nn))
6270 return count;
6272 spin_lock(&nn->client_lock);
6273 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6274 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6275 if (max != 0 && count >= max)
6276 break;
6278 spin_unlock(&nn->client_lock);
6279 nfsd_forget_delegations(&reaplist);
6280 return count;
6283 static void
6284 nfsd_recall_delegations(struct list_head *reaplist)
6286 struct nfs4_client *clp;
6287 struct nfs4_delegation *dp, *next;
6289 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6290 list_del_init(&dp->dl_recall_lru);
6291 clp = dp->dl_stid.sc_client;
6293 * We skipped all entries that had a zero dl_time before,
6294 * so we can now reset the dl_time back to 0. If a delegation
6295 * break comes in now, then it won't make any difference since
6296 * we're recalling it either way.
6298 spin_lock(&state_lock);
6299 dp->dl_time = 0;
6300 spin_unlock(&state_lock);
6301 nfsd_break_one_deleg(dp);
6302 put_client(clp);
6307 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6308 size_t addr_size)
6310 u64 count = 0;
6311 struct nfs4_client *clp;
6312 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6313 nfsd_net_id);
6314 LIST_HEAD(reaplist);
6316 if (!nfsd_netns_ready(nn))
6317 return count;
6319 spin_lock(&nn->client_lock);
6320 clp = nfsd_find_client(addr, addr_size);
6321 if (clp)
6322 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6323 spin_unlock(&nn->client_lock);
6325 nfsd_recall_delegations(&reaplist);
6326 return count;
6330 nfsd_inject_recall_delegations(u64 max)
6332 u64 count = 0;
6333 struct nfs4_client *clp, *next;
6334 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6335 nfsd_net_id);
6336 LIST_HEAD(reaplist);
6338 if (!nfsd_netns_ready(nn))
6339 return count;
6341 spin_lock(&nn->client_lock);
6342 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6343 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6344 if (max != 0 && ++count >= max)
6345 break;
6347 spin_unlock(&nn->client_lock);
6348 nfsd_recall_delegations(&reaplist);
6349 return count;
6351 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6354 * Since the lifetime of a delegation isn't limited to that of an open, a
6355 * client may quite reasonably hang on to a delegation as long as it has
6356 * the inode cached. This becomes an obvious problem the first time a
6357 * client's inode cache approaches the size of the server's total memory.
6359 * For now we avoid this problem by imposing a hard limit on the number
6360 * of delegations, which varies according to the server's memory size.
6362 static void
6363 set_max_delegations(void)
6366 * Allow at most 4 delegations per megabyte of RAM. Quick
6367 * estimates suggest that in the worst case (where every delegation
6368 * is for a different inode), a delegation could take about 1.5K,
6369 * giving a worst case usage of about 6% of memory.
6371 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6374 static int nfs4_state_create_net(struct net *net)
6376 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6377 int i;
6379 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6380 CLIENT_HASH_SIZE, GFP_KERNEL);
6381 if (!nn->conf_id_hashtbl)
6382 goto err;
6383 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6384 CLIENT_HASH_SIZE, GFP_KERNEL);
6385 if (!nn->unconf_id_hashtbl)
6386 goto err_unconf_id;
6387 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6388 SESSION_HASH_SIZE, GFP_KERNEL);
6389 if (!nn->sessionid_hashtbl)
6390 goto err_sessionid;
6392 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6393 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6394 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6396 for (i = 0; i < SESSION_HASH_SIZE; i++)
6397 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6398 nn->conf_name_tree = RB_ROOT;
6399 nn->unconf_name_tree = RB_ROOT;
6400 INIT_LIST_HEAD(&nn->client_lru);
6401 INIT_LIST_HEAD(&nn->close_lru);
6402 INIT_LIST_HEAD(&nn->del_recall_lru);
6403 spin_lock_init(&nn->client_lock);
6405 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6406 get_net(net);
6408 return 0;
6410 err_sessionid:
6411 kfree(nn->unconf_id_hashtbl);
6412 err_unconf_id:
6413 kfree(nn->conf_id_hashtbl);
6414 err:
6415 return -ENOMEM;
6418 static void
6419 nfs4_state_destroy_net(struct net *net)
6421 int i;
6422 struct nfs4_client *clp = NULL;
6423 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6425 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6426 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6427 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6428 destroy_client(clp);
6432 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6433 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6434 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6435 destroy_client(clp);
6439 kfree(nn->sessionid_hashtbl);
6440 kfree(nn->unconf_id_hashtbl);
6441 kfree(nn->conf_id_hashtbl);
6442 put_net(net);
6446 nfs4_state_start_net(struct net *net)
6448 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6449 int ret;
6451 ret = nfs4_state_create_net(net);
6452 if (ret)
6453 return ret;
6454 nn->boot_time = get_seconds();
6455 nn->grace_ended = false;
6456 locks_start_grace(net, &nn->nfsd4_manager);
6457 nfsd4_client_tracking_init(net);
6458 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6459 nn->nfsd4_grace, net);
6460 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6461 return 0;
6464 /* initialization to perform when the nfsd service is started: */
6467 nfs4_state_start(void)
6469 int ret;
6471 ret = set_callback_cred();
6472 if (ret)
6473 return -ENOMEM;
6474 laundry_wq = create_singlethread_workqueue("nfsd4");
6475 if (laundry_wq == NULL) {
6476 ret = -ENOMEM;
6477 goto out_recovery;
6479 ret = nfsd4_create_callback_queue();
6480 if (ret)
6481 goto out_free_laundry;
6483 set_max_delegations();
6485 return 0;
6487 out_free_laundry:
6488 destroy_workqueue(laundry_wq);
6489 out_recovery:
6490 return ret;
6493 void
6494 nfs4_state_shutdown_net(struct net *net)
6496 struct nfs4_delegation *dp = NULL;
6497 struct list_head *pos, *next, reaplist;
6498 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6500 cancel_delayed_work_sync(&nn->laundromat_work);
6501 locks_end_grace(&nn->nfsd4_manager);
6503 INIT_LIST_HEAD(&reaplist);
6504 spin_lock(&state_lock);
6505 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6506 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6507 unhash_delegation_locked(dp);
6508 list_add(&dp->dl_recall_lru, &reaplist);
6510 spin_unlock(&state_lock);
6511 list_for_each_safe(pos, next, &reaplist) {
6512 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6513 list_del_init(&dp->dl_recall_lru);
6514 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6515 nfs4_put_stid(&dp->dl_stid);
6518 nfsd4_client_tracking_exit(net);
6519 nfs4_state_destroy_net(net);
6522 void
6523 nfs4_state_shutdown(void)
6525 destroy_workqueue(laundry_wq);
6526 nfsd4_destroy_callback_queue();
6529 static void
6530 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6532 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6533 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6536 static void
6537 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6539 if (cstate->minorversion) {
6540 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6541 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6545 void
6546 clear_current_stateid(struct nfsd4_compound_state *cstate)
6548 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6552 * functions to set current state id
6554 void
6555 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6557 put_stateid(cstate, &odp->od_stateid);
6560 void
6561 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6563 put_stateid(cstate, &open->op_stateid);
6566 void
6567 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6569 put_stateid(cstate, &close->cl_stateid);
6572 void
6573 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6575 put_stateid(cstate, &lock->lk_resp_stateid);
6579 * functions to consume current state id
6582 void
6583 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6585 get_stateid(cstate, &odp->od_stateid);
6588 void
6589 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6591 get_stateid(cstate, &drp->dr_stateid);
6594 void
6595 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6597 get_stateid(cstate, &fsp->fr_stateid);
6600 void
6601 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6603 get_stateid(cstate, &setattr->sa_stateid);
6606 void
6607 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6609 get_stateid(cstate, &close->cl_stateid);
6612 void
6613 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6615 get_stateid(cstate, &locku->lu_stateid);
6618 void
6619 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6621 get_stateid(cstate, &read->rd_stateid);
6624 void
6625 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6627 get_stateid(cstate, &write->wr_stateid);