init: allow CONFIG_INIT_FALLBACK=n to disable defaults if init= fails
[linux-2.6/btrfs-unstable.git] / fs / nfsd / nfs4state.c
blobe9c3afe4b5d3c306a5dd61692ed34805ca661f7a
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(reaplist.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 long long
1715 compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2)
1717 long long res;
1719 res = o1->len - o2->len;
1720 if (res)
1721 return res;
1722 return (long long)memcmp(o1->data, o2->data, o1->len);
1725 static int same_name(const char *n1, const char *n2)
1727 return 0 == memcmp(n1, n2, HEXDIR_LEN);
1730 static int
1731 same_verf(nfs4_verifier *v1, nfs4_verifier *v2)
1733 return 0 == memcmp(v1->data, v2->data, sizeof(v1->data));
1736 static int
1737 same_clid(clientid_t *cl1, clientid_t *cl2)
1739 return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id);
1742 static bool groups_equal(struct group_info *g1, struct group_info *g2)
1744 int i;
1746 if (g1->ngroups != g2->ngroups)
1747 return false;
1748 for (i=0; i<g1->ngroups; i++)
1749 if (!gid_eq(GROUP_AT(g1, i), GROUP_AT(g2, i)))
1750 return false;
1751 return true;
1755 * RFC 3530 language requires clid_inuse be returned when the
1756 * "principal" associated with a requests differs from that previously
1757 * used. We use uid, gid's, and gss principal string as our best
1758 * approximation. We also don't want to allow non-gss use of a client
1759 * established using gss: in theory cr_principal should catch that
1760 * change, but in practice cr_principal can be null even in the gss case
1761 * since gssd doesn't always pass down a principal string.
1763 static bool is_gss_cred(struct svc_cred *cr)
1765 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1766 return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR);
1770 static bool
1771 same_creds(struct svc_cred *cr1, struct svc_cred *cr2)
1773 if ((is_gss_cred(cr1) != is_gss_cred(cr2))
1774 || (!uid_eq(cr1->cr_uid, cr2->cr_uid))
1775 || (!gid_eq(cr1->cr_gid, cr2->cr_gid))
1776 || !groups_equal(cr1->cr_group_info, cr2->cr_group_info))
1777 return false;
1778 if (cr1->cr_principal == cr2->cr_principal)
1779 return true;
1780 if (!cr1->cr_principal || !cr2->cr_principal)
1781 return false;
1782 return 0 == strcmp(cr1->cr_principal, cr2->cr_principal);
1785 static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp)
1787 struct svc_cred *cr = &rqstp->rq_cred;
1788 u32 service;
1790 if (!cr->cr_gss_mech)
1791 return false;
1792 service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor);
1793 return service == RPC_GSS_SVC_INTEGRITY ||
1794 service == RPC_GSS_SVC_PRIVACY;
1797 static bool mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp)
1799 struct svc_cred *cr = &rqstp->rq_cred;
1801 if (!cl->cl_mach_cred)
1802 return true;
1803 if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech)
1804 return false;
1805 if (!svc_rqst_integrity_protected(rqstp))
1806 return false;
1807 if (!cr->cr_principal)
1808 return false;
1809 return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal);
1812 static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn)
1814 __be32 verf[2];
1817 * This is opaque to client, so no need to byte-swap. Use
1818 * __force to keep sparse happy
1820 verf[0] = (__force __be32)get_seconds();
1821 verf[1] = (__force __be32)nn->clientid_counter;
1822 memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data));
1825 static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn)
1827 clp->cl_clientid.cl_boot = nn->boot_time;
1828 clp->cl_clientid.cl_id = nn->clientid_counter++;
1829 gen_confirm(clp, nn);
1832 static struct nfs4_stid *
1833 find_stateid_locked(struct nfs4_client *cl, stateid_t *t)
1835 struct nfs4_stid *ret;
1837 ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id);
1838 if (!ret || !ret->sc_type)
1839 return NULL;
1840 return ret;
1843 static struct nfs4_stid *
1844 find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask)
1846 struct nfs4_stid *s;
1848 spin_lock(&cl->cl_lock);
1849 s = find_stateid_locked(cl, t);
1850 if (s != NULL) {
1851 if (typemask & s->sc_type)
1852 atomic_inc(&s->sc_count);
1853 else
1854 s = NULL;
1856 spin_unlock(&cl->cl_lock);
1857 return s;
1860 static struct nfs4_client *create_client(struct xdr_netobj name,
1861 struct svc_rqst *rqstp, nfs4_verifier *verf)
1863 struct nfs4_client *clp;
1864 struct sockaddr *sa = svc_addr(rqstp);
1865 int ret;
1866 struct net *net = SVC_NET(rqstp);
1868 clp = alloc_client(name);
1869 if (clp == NULL)
1870 return NULL;
1872 ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred);
1873 if (ret) {
1874 free_client(clp);
1875 return NULL;
1877 nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL);
1878 clp->cl_time = get_seconds();
1879 clear_bit(0, &clp->cl_cb_slot_busy);
1880 copy_verf(clp, verf);
1881 rpc_copy_addr((struct sockaddr *) &clp->cl_addr, sa);
1882 clp->cl_cb_session = NULL;
1883 clp->net = net;
1884 return clp;
1887 static void
1888 add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root)
1890 struct rb_node **new = &(root->rb_node), *parent = NULL;
1891 struct nfs4_client *clp;
1893 while (*new) {
1894 clp = rb_entry(*new, struct nfs4_client, cl_namenode);
1895 parent = *new;
1897 if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0)
1898 new = &((*new)->rb_left);
1899 else
1900 new = &((*new)->rb_right);
1903 rb_link_node(&new_clp->cl_namenode, parent, new);
1904 rb_insert_color(&new_clp->cl_namenode, root);
1907 static struct nfs4_client *
1908 find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root)
1910 long long cmp;
1911 struct rb_node *node = root->rb_node;
1912 struct nfs4_client *clp;
1914 while (node) {
1915 clp = rb_entry(node, struct nfs4_client, cl_namenode);
1916 cmp = compare_blob(&clp->cl_name, name);
1917 if (cmp > 0)
1918 node = node->rb_left;
1919 else if (cmp < 0)
1920 node = node->rb_right;
1921 else
1922 return clp;
1924 return NULL;
1927 static void
1928 add_to_unconfirmed(struct nfs4_client *clp)
1930 unsigned int idhashval;
1931 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1933 lockdep_assert_held(&nn->client_lock);
1935 clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1936 add_clp_to_name_tree(clp, &nn->unconf_name_tree);
1937 idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1938 list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]);
1939 renew_client_locked(clp);
1942 static void
1943 move_to_confirmed(struct nfs4_client *clp)
1945 unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id);
1946 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id);
1948 lockdep_assert_held(&nn->client_lock);
1950 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp);
1951 list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]);
1952 rb_erase(&clp->cl_namenode, &nn->unconf_name_tree);
1953 add_clp_to_name_tree(clp, &nn->conf_name_tree);
1954 set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags);
1955 renew_client_locked(clp);
1958 static struct nfs4_client *
1959 find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions)
1961 struct nfs4_client *clp;
1962 unsigned int idhashval = clientid_hashval(clid->cl_id);
1964 list_for_each_entry(clp, &tbl[idhashval], cl_idhash) {
1965 if (same_clid(&clp->cl_clientid, clid)) {
1966 if ((bool)clp->cl_minorversion != sessions)
1967 return NULL;
1968 renew_client_locked(clp);
1969 return clp;
1972 return NULL;
1975 static struct nfs4_client *
1976 find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1978 struct list_head *tbl = nn->conf_id_hashtbl;
1980 lockdep_assert_held(&nn->client_lock);
1981 return find_client_in_id_table(tbl, clid, sessions);
1984 static struct nfs4_client *
1985 find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn)
1987 struct list_head *tbl = nn->unconf_id_hashtbl;
1989 lockdep_assert_held(&nn->client_lock);
1990 return find_client_in_id_table(tbl, clid, sessions);
1993 static bool clp_used_exchangeid(struct nfs4_client *clp)
1995 return clp->cl_exchange_flags != 0;
1998 static struct nfs4_client *
1999 find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2001 lockdep_assert_held(&nn->client_lock);
2002 return find_clp_in_name_tree(name, &nn->conf_name_tree);
2005 static struct nfs4_client *
2006 find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn)
2008 lockdep_assert_held(&nn->client_lock);
2009 return find_clp_in_name_tree(name, &nn->unconf_name_tree);
2012 static void
2013 gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp)
2015 struct nfs4_cb_conn *conn = &clp->cl_cb_conn;
2016 struct sockaddr *sa = svc_addr(rqstp);
2017 u32 scopeid = rpc_get_scope_id(sa);
2018 unsigned short expected_family;
2020 /* Currently, we only support tcp and tcp6 for the callback channel */
2021 if (se->se_callback_netid_len == 3 &&
2022 !memcmp(se->se_callback_netid_val, "tcp", 3))
2023 expected_family = AF_INET;
2024 else if (se->se_callback_netid_len == 4 &&
2025 !memcmp(se->se_callback_netid_val, "tcp6", 4))
2026 expected_family = AF_INET6;
2027 else
2028 goto out_err;
2030 conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val,
2031 se->se_callback_addr_len,
2032 (struct sockaddr *)&conn->cb_addr,
2033 sizeof(conn->cb_addr));
2035 if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family)
2036 goto out_err;
2038 if (conn->cb_addr.ss_family == AF_INET6)
2039 ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid;
2041 conn->cb_prog = se->se_callback_prog;
2042 conn->cb_ident = se->se_callback_ident;
2043 memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen);
2044 return;
2045 out_err:
2046 conn->cb_addr.ss_family = AF_UNSPEC;
2047 conn->cb_addrlen = 0;
2048 dprintk(KERN_INFO "NFSD: this client (clientid %08x/%08x) "
2049 "will not receive delegations\n",
2050 clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id);
2052 return;
2056 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2058 static void
2059 nfsd4_store_cache_entry(struct nfsd4_compoundres *resp)
2061 struct xdr_buf *buf = resp->xdr.buf;
2062 struct nfsd4_slot *slot = resp->cstate.slot;
2063 unsigned int base;
2065 dprintk("--> %s slot %p\n", __func__, slot);
2067 slot->sl_opcnt = resp->opcnt;
2068 slot->sl_status = resp->cstate.status;
2070 slot->sl_flags |= NFSD4_SLOT_INITIALIZED;
2071 if (nfsd4_not_cached(resp)) {
2072 slot->sl_datalen = 0;
2073 return;
2075 base = resp->cstate.data_offset;
2076 slot->sl_datalen = buf->len - base;
2077 if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen))
2078 WARN("%s: sessions DRC could not cache compound\n", __func__);
2079 return;
2083 * Encode the replay sequence operation from the slot values.
2084 * If cachethis is FALSE encode the uncached rep error on the next
2085 * operation which sets resp->p and increments resp->opcnt for
2086 * nfs4svc_encode_compoundres.
2089 static __be32
2090 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args,
2091 struct nfsd4_compoundres *resp)
2093 struct nfsd4_op *op;
2094 struct nfsd4_slot *slot = resp->cstate.slot;
2096 /* Encode the replayed sequence operation */
2097 op = &args->ops[resp->opcnt - 1];
2098 nfsd4_encode_operation(resp, op);
2100 /* Return nfserr_retry_uncached_rep in next operation. */
2101 if (args->opcnt > 1 && !(slot->sl_flags & NFSD4_SLOT_CACHETHIS)) {
2102 op = &args->ops[resp->opcnt++];
2103 op->status = nfserr_retry_uncached_rep;
2104 nfsd4_encode_operation(resp, op);
2106 return op->status;
2110 * The sequence operation is not cached because we can use the slot and
2111 * session values.
2113 static __be32
2114 nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp,
2115 struct nfsd4_sequence *seq)
2117 struct nfsd4_slot *slot = resp->cstate.slot;
2118 struct xdr_stream *xdr = &resp->xdr;
2119 __be32 *p;
2120 __be32 status;
2122 dprintk("--> %s slot %p\n", __func__, slot);
2124 status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp);
2125 if (status)
2126 return status;
2128 p = xdr_reserve_space(xdr, slot->sl_datalen);
2129 if (!p) {
2130 WARN_ON_ONCE(1);
2131 return nfserr_serverfault;
2133 xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen);
2134 xdr_commit_encode(xdr);
2136 resp->opcnt = slot->sl_opcnt;
2137 return slot->sl_status;
2141 * Set the exchange_id flags returned by the server.
2143 static void
2144 nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid)
2146 /* pNFS is not supported */
2147 new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS;
2149 /* Referrals are supported, Migration is not. */
2150 new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER;
2152 /* set the wire flags to return to client. */
2153 clid->flags = new->cl_exchange_flags;
2156 static bool client_has_state(struct nfs4_client *clp)
2159 * Note clp->cl_openowners check isn't quite right: there's no
2160 * need to count owners without stateid's.
2162 * Also note we should probably be using this in 4.0 case too.
2164 return !list_empty(&clp->cl_openowners)
2165 || !list_empty(&clp->cl_delegations)
2166 || !list_empty(&clp->cl_sessions);
2169 __be32
2170 nfsd4_exchange_id(struct svc_rqst *rqstp,
2171 struct nfsd4_compound_state *cstate,
2172 struct nfsd4_exchange_id *exid)
2174 struct nfs4_client *conf, *new;
2175 struct nfs4_client *unconf = NULL;
2176 __be32 status;
2177 char addr_str[INET6_ADDRSTRLEN];
2178 nfs4_verifier verf = exid->verifier;
2179 struct sockaddr *sa = svc_addr(rqstp);
2180 bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A;
2181 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2183 rpc_ntop(sa, addr_str, sizeof(addr_str));
2184 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2185 "ip_addr=%s flags %x, spa_how %d\n",
2186 __func__, rqstp, exid, exid->clname.len, exid->clname.data,
2187 addr_str, exid->flags, exid->spa_how);
2189 if (exid->flags & ~EXCHGID4_FLAG_MASK_A)
2190 return nfserr_inval;
2192 switch (exid->spa_how) {
2193 case SP4_MACH_CRED:
2194 if (!svc_rqst_integrity_protected(rqstp))
2195 return nfserr_inval;
2196 case SP4_NONE:
2197 break;
2198 default: /* checked by xdr code */
2199 WARN_ON_ONCE(1);
2200 case SP4_SSV:
2201 return nfserr_encr_alg_unsupp;
2204 new = create_client(exid->clname, rqstp, &verf);
2205 if (new == NULL)
2206 return nfserr_jukebox;
2208 /* Cases below refer to rfc 5661 section 18.35.4: */
2209 spin_lock(&nn->client_lock);
2210 conf = find_confirmed_client_by_name(&exid->clname, nn);
2211 if (conf) {
2212 bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred);
2213 bool verfs_match = same_verf(&verf, &conf->cl_verifier);
2215 if (update) {
2216 if (!clp_used_exchangeid(conf)) { /* buggy client */
2217 status = nfserr_inval;
2218 goto out;
2220 if (!mach_creds_match(conf, rqstp)) {
2221 status = nfserr_wrong_cred;
2222 goto out;
2224 if (!creds_match) { /* case 9 */
2225 status = nfserr_perm;
2226 goto out;
2228 if (!verfs_match) { /* case 8 */
2229 status = nfserr_not_same;
2230 goto out;
2232 /* case 6 */
2233 exid->flags |= EXCHGID4_FLAG_CONFIRMED_R;
2234 goto out_copy;
2236 if (!creds_match) { /* case 3 */
2237 if (client_has_state(conf)) {
2238 status = nfserr_clid_inuse;
2239 goto out;
2241 goto out_new;
2243 if (verfs_match) { /* case 2 */
2244 conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
2245 goto out_copy;
2247 /* case 5, client reboot */
2248 conf = NULL;
2249 goto out_new;
2252 if (update) { /* case 7 */
2253 status = nfserr_noent;
2254 goto out;
2257 unconf = find_unconfirmed_client_by_name(&exid->clname, nn);
2258 if (unconf) /* case 4, possible retry or client restart */
2259 unhash_client_locked(unconf);
2261 /* case 1 (normal case) */
2262 out_new:
2263 if (conf) {
2264 status = mark_client_expired_locked(conf);
2265 if (status)
2266 goto out;
2268 new->cl_minorversion = cstate->minorversion;
2269 new->cl_mach_cred = (exid->spa_how == SP4_MACH_CRED);
2271 gen_clid(new, nn);
2272 add_to_unconfirmed(new);
2273 swap(new, conf);
2274 out_copy:
2275 exid->clientid.cl_boot = conf->cl_clientid.cl_boot;
2276 exid->clientid.cl_id = conf->cl_clientid.cl_id;
2278 exid->seqid = conf->cl_cs_slot.sl_seqid + 1;
2279 nfsd4_set_ex_flags(conf, exid);
2281 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2282 conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags);
2283 status = nfs_ok;
2285 out:
2286 spin_unlock(&nn->client_lock);
2287 if (new)
2288 expire_client(new);
2289 if (unconf)
2290 expire_client(unconf);
2291 return status;
2294 static __be32
2295 check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse)
2297 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid,
2298 slot_seqid);
2300 /* The slot is in use, and no response has been sent. */
2301 if (slot_inuse) {
2302 if (seqid == slot_seqid)
2303 return nfserr_jukebox;
2304 else
2305 return nfserr_seq_misordered;
2307 /* Note unsigned 32-bit arithmetic handles wraparound: */
2308 if (likely(seqid == slot_seqid + 1))
2309 return nfs_ok;
2310 if (seqid == slot_seqid)
2311 return nfserr_replay_cache;
2312 return nfserr_seq_misordered;
2316 * Cache the create session result into the create session single DRC
2317 * slot cache by saving the xdr structure. sl_seqid has been set.
2318 * Do this for solo or embedded create session operations.
2320 static void
2321 nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses,
2322 struct nfsd4_clid_slot *slot, __be32 nfserr)
2324 slot->sl_status = nfserr;
2325 memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses));
2328 static __be32
2329 nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses,
2330 struct nfsd4_clid_slot *slot)
2332 memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses));
2333 return slot->sl_status;
2336 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2337 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2338 1 + /* MIN tag is length with zero, only length */ \
2339 3 + /* version, opcount, opcode */ \
2340 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2341 /* seqid, slotID, slotID, cache */ \
2342 4 ) * sizeof(__be32))
2344 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2345 2 + /* verifier: AUTH_NULL, length 0 */\
2346 1 + /* status */ \
2347 1 + /* MIN tag is length with zero, only length */ \
2348 3 + /* opcount, opcode, opstatus*/ \
2349 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2350 /* seqid, slotID, slotID, slotID, status */ \
2351 5 ) * sizeof(__be32))
2353 static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn)
2355 u32 maxrpc = nn->nfsd_serv->sv_max_mesg;
2357 if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ)
2358 return nfserr_toosmall;
2359 if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ)
2360 return nfserr_toosmall;
2361 ca->headerpadsz = 0;
2362 ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc);
2363 ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc);
2364 ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND);
2365 ca->maxresp_cached = min_t(u32, ca->maxresp_cached,
2366 NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ);
2367 ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION);
2369 * Note decreasing slot size below client's request may make it
2370 * difficult for client to function correctly, whereas
2371 * decreasing the number of slots will (just?) affect
2372 * performance. When short on memory we therefore prefer to
2373 * decrease number of slots instead of their size. Clients that
2374 * request larger slots than they need will get poor results:
2376 ca->maxreqs = nfsd4_get_drc_mem(ca);
2377 if (!ca->maxreqs)
2378 return nfserr_jukebox;
2380 return nfs_ok;
2383 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2384 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2385 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2386 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2388 static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca)
2390 ca->headerpadsz = 0;
2393 * These RPC_MAX_HEADER macros are overkill, especially since we
2394 * don't even do gss on the backchannel yet. But this is still
2395 * less than 1k. Tighten up this estimate in the unlikely event
2396 * it turns out to be a problem for some client:
2398 if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ)
2399 return nfserr_toosmall;
2400 if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ)
2401 return nfserr_toosmall;
2402 ca->maxresp_cached = 0;
2403 if (ca->maxops < 2)
2404 return nfserr_toosmall;
2406 return nfs_ok;
2409 static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs)
2411 switch (cbs->flavor) {
2412 case RPC_AUTH_NULL:
2413 case RPC_AUTH_UNIX:
2414 return nfs_ok;
2415 default:
2417 * GSS case: the spec doesn't allow us to return this
2418 * error. But it also doesn't allow us not to support
2419 * GSS.
2420 * I'd rather this fail hard than return some error the
2421 * client might think it can already handle:
2423 return nfserr_encr_alg_unsupp;
2427 __be32
2428 nfsd4_create_session(struct svc_rqst *rqstp,
2429 struct nfsd4_compound_state *cstate,
2430 struct nfsd4_create_session *cr_ses)
2432 struct sockaddr *sa = svc_addr(rqstp);
2433 struct nfs4_client *conf, *unconf;
2434 struct nfs4_client *old = NULL;
2435 struct nfsd4_session *new;
2436 struct nfsd4_conn *conn;
2437 struct nfsd4_clid_slot *cs_slot = NULL;
2438 __be32 status = 0;
2439 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2441 if (cr_ses->flags & ~SESSION4_FLAG_MASK_A)
2442 return nfserr_inval;
2443 status = nfsd4_check_cb_sec(&cr_ses->cb_sec);
2444 if (status)
2445 return status;
2446 status = check_forechannel_attrs(&cr_ses->fore_channel, nn);
2447 if (status)
2448 return status;
2449 status = check_backchannel_attrs(&cr_ses->back_channel);
2450 if (status)
2451 goto out_release_drc_mem;
2452 status = nfserr_jukebox;
2453 new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel);
2454 if (!new)
2455 goto out_release_drc_mem;
2456 conn = alloc_conn_from_crses(rqstp, cr_ses);
2457 if (!conn)
2458 goto out_free_session;
2460 spin_lock(&nn->client_lock);
2461 unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn);
2462 conf = find_confirmed_client(&cr_ses->clientid, true, nn);
2463 WARN_ON_ONCE(conf && unconf);
2465 if (conf) {
2466 status = nfserr_wrong_cred;
2467 if (!mach_creds_match(conf, rqstp))
2468 goto out_free_conn;
2469 cs_slot = &conf->cl_cs_slot;
2470 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2471 if (status == nfserr_replay_cache) {
2472 status = nfsd4_replay_create_session(cr_ses, cs_slot);
2473 goto out_free_conn;
2474 } else if (cr_ses->seqid != cs_slot->sl_seqid + 1) {
2475 status = nfserr_seq_misordered;
2476 goto out_free_conn;
2478 } else if (unconf) {
2479 if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) ||
2480 !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) {
2481 status = nfserr_clid_inuse;
2482 goto out_free_conn;
2484 status = nfserr_wrong_cred;
2485 if (!mach_creds_match(unconf, rqstp))
2486 goto out_free_conn;
2487 cs_slot = &unconf->cl_cs_slot;
2488 status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0);
2489 if (status) {
2490 /* an unconfirmed replay returns misordered */
2491 status = nfserr_seq_misordered;
2492 goto out_free_conn;
2494 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
2495 if (old) {
2496 status = mark_client_expired_locked(old);
2497 if (status) {
2498 old = NULL;
2499 goto out_free_conn;
2502 move_to_confirmed(unconf);
2503 conf = unconf;
2504 } else {
2505 status = nfserr_stale_clientid;
2506 goto out_free_conn;
2508 status = nfs_ok;
2510 * We do not support RDMA or persistent sessions
2512 cr_ses->flags &= ~SESSION4_PERSIST;
2513 cr_ses->flags &= ~SESSION4_RDMA;
2515 init_session(rqstp, new, conf, cr_ses);
2516 nfsd4_get_session_locked(new);
2518 memcpy(cr_ses->sessionid.data, new->se_sessionid.data,
2519 NFS4_MAX_SESSIONID_LEN);
2520 cs_slot->sl_seqid++;
2521 cr_ses->seqid = cs_slot->sl_seqid;
2523 /* cache solo and embedded create sessions under the client_lock */
2524 nfsd4_cache_create_session(cr_ses, cs_slot, status);
2525 spin_unlock(&nn->client_lock);
2526 /* init connection and backchannel */
2527 nfsd4_init_conn(rqstp, conn, new);
2528 nfsd4_put_session(new);
2529 if (old)
2530 expire_client(old);
2531 return status;
2532 out_free_conn:
2533 spin_unlock(&nn->client_lock);
2534 free_conn(conn);
2535 if (old)
2536 expire_client(old);
2537 out_free_session:
2538 __free_session(new);
2539 out_release_drc_mem:
2540 nfsd4_put_drc_mem(&cr_ses->fore_channel);
2541 return status;
2544 static __be32 nfsd4_map_bcts_dir(u32 *dir)
2546 switch (*dir) {
2547 case NFS4_CDFC4_FORE:
2548 case NFS4_CDFC4_BACK:
2549 return nfs_ok;
2550 case NFS4_CDFC4_FORE_OR_BOTH:
2551 case NFS4_CDFC4_BACK_OR_BOTH:
2552 *dir = NFS4_CDFC4_BOTH;
2553 return nfs_ok;
2555 return nfserr_inval;
2558 __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_backchannel_ctl *bc)
2560 struct nfsd4_session *session = cstate->session;
2561 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2562 __be32 status;
2564 status = nfsd4_check_cb_sec(&bc->bc_cb_sec);
2565 if (status)
2566 return status;
2567 spin_lock(&nn->client_lock);
2568 session->se_cb_prog = bc->bc_cb_program;
2569 session->se_cb_sec = bc->bc_cb_sec;
2570 spin_unlock(&nn->client_lock);
2572 nfsd4_probe_callback(session->se_client);
2574 return nfs_ok;
2577 __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp,
2578 struct nfsd4_compound_state *cstate,
2579 struct nfsd4_bind_conn_to_session *bcts)
2581 __be32 status;
2582 struct nfsd4_conn *conn;
2583 struct nfsd4_session *session;
2584 struct net *net = SVC_NET(rqstp);
2585 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2587 if (!nfsd4_last_compound_op(rqstp))
2588 return nfserr_not_only_op;
2589 spin_lock(&nn->client_lock);
2590 session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status);
2591 spin_unlock(&nn->client_lock);
2592 if (!session)
2593 goto out_no_session;
2594 status = nfserr_wrong_cred;
2595 if (!mach_creds_match(session->se_client, rqstp))
2596 goto out;
2597 status = nfsd4_map_bcts_dir(&bcts->dir);
2598 if (status)
2599 goto out;
2600 conn = alloc_conn(rqstp, bcts->dir);
2601 status = nfserr_jukebox;
2602 if (!conn)
2603 goto out;
2604 nfsd4_init_conn(rqstp, conn, session);
2605 status = nfs_ok;
2606 out:
2607 nfsd4_put_session(session);
2608 out_no_session:
2609 return status;
2612 static bool nfsd4_compound_in_session(struct nfsd4_session *session, struct nfs4_sessionid *sid)
2614 if (!session)
2615 return 0;
2616 return !memcmp(sid, &session->se_sessionid, sizeof(*sid));
2619 __be32
2620 nfsd4_destroy_session(struct svc_rqst *r,
2621 struct nfsd4_compound_state *cstate,
2622 struct nfsd4_destroy_session *sessionid)
2624 struct nfsd4_session *ses;
2625 __be32 status;
2626 int ref_held_by_me = 0;
2627 struct net *net = SVC_NET(r);
2628 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2630 status = nfserr_not_only_op;
2631 if (nfsd4_compound_in_session(cstate->session, &sessionid->sessionid)) {
2632 if (!nfsd4_last_compound_op(r))
2633 goto out;
2634 ref_held_by_me++;
2636 dump_sessionid(__func__, &sessionid->sessionid);
2637 spin_lock(&nn->client_lock);
2638 ses = find_in_sessionid_hashtbl(&sessionid->sessionid, net, &status);
2639 if (!ses)
2640 goto out_client_lock;
2641 status = nfserr_wrong_cred;
2642 if (!mach_creds_match(ses->se_client, r))
2643 goto out_put_session;
2644 status = mark_session_dead_locked(ses, 1 + ref_held_by_me);
2645 if (status)
2646 goto out_put_session;
2647 unhash_session(ses);
2648 spin_unlock(&nn->client_lock);
2650 nfsd4_probe_callback_sync(ses->se_client);
2652 spin_lock(&nn->client_lock);
2653 status = nfs_ok;
2654 out_put_session:
2655 nfsd4_put_session_locked(ses);
2656 out_client_lock:
2657 spin_unlock(&nn->client_lock);
2658 out:
2659 return status;
2662 static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s)
2664 struct nfsd4_conn *c;
2666 list_for_each_entry(c, &s->se_conns, cn_persession) {
2667 if (c->cn_xprt == xpt) {
2668 return c;
2671 return NULL;
2674 static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses)
2676 struct nfs4_client *clp = ses->se_client;
2677 struct nfsd4_conn *c;
2678 __be32 status = nfs_ok;
2679 int ret;
2681 spin_lock(&clp->cl_lock);
2682 c = __nfsd4_find_conn(new->cn_xprt, ses);
2683 if (c)
2684 goto out_free;
2685 status = nfserr_conn_not_bound_to_session;
2686 if (clp->cl_mach_cred)
2687 goto out_free;
2688 __nfsd4_hash_conn(new, ses);
2689 spin_unlock(&clp->cl_lock);
2690 ret = nfsd4_register_conn(new);
2691 if (ret)
2692 /* oops; xprt is already down: */
2693 nfsd4_conn_lost(&new->cn_xpt_user);
2694 return nfs_ok;
2695 out_free:
2696 spin_unlock(&clp->cl_lock);
2697 free_conn(new);
2698 return status;
2701 static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session)
2703 struct nfsd4_compoundargs *args = rqstp->rq_argp;
2705 return args->opcnt > session->se_fchannel.maxops;
2708 static bool nfsd4_request_too_big(struct svc_rqst *rqstp,
2709 struct nfsd4_session *session)
2711 struct xdr_buf *xb = &rqstp->rq_arg;
2713 return xb->len > session->se_fchannel.maxreq_sz;
2716 __be32
2717 nfsd4_sequence(struct svc_rqst *rqstp,
2718 struct nfsd4_compound_state *cstate,
2719 struct nfsd4_sequence *seq)
2721 struct nfsd4_compoundres *resp = rqstp->rq_resp;
2722 struct xdr_stream *xdr = &resp->xdr;
2723 struct nfsd4_session *session;
2724 struct nfs4_client *clp;
2725 struct nfsd4_slot *slot;
2726 struct nfsd4_conn *conn;
2727 __be32 status;
2728 int buflen;
2729 struct net *net = SVC_NET(rqstp);
2730 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2732 if (resp->opcnt != 1)
2733 return nfserr_sequence_pos;
2736 * Will be either used or freed by nfsd4_sequence_check_conn
2737 * below.
2739 conn = alloc_conn(rqstp, NFS4_CDFC4_FORE);
2740 if (!conn)
2741 return nfserr_jukebox;
2743 spin_lock(&nn->client_lock);
2744 session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status);
2745 if (!session)
2746 goto out_no_session;
2747 clp = session->se_client;
2749 status = nfserr_too_many_ops;
2750 if (nfsd4_session_too_many_ops(rqstp, session))
2751 goto out_put_session;
2753 status = nfserr_req_too_big;
2754 if (nfsd4_request_too_big(rqstp, session))
2755 goto out_put_session;
2757 status = nfserr_badslot;
2758 if (seq->slotid >= session->se_fchannel.maxreqs)
2759 goto out_put_session;
2761 slot = session->se_slots[seq->slotid];
2762 dprintk("%s: slotid %d\n", __func__, seq->slotid);
2764 /* We do not negotiate the number of slots yet, so set the
2765 * maxslots to the session maxreqs which is used to encode
2766 * sr_highest_slotid and the sr_target_slot id to maxslots */
2767 seq->maxslots = session->se_fchannel.maxreqs;
2769 status = check_slot_seqid(seq->seqid, slot->sl_seqid,
2770 slot->sl_flags & NFSD4_SLOT_INUSE);
2771 if (status == nfserr_replay_cache) {
2772 status = nfserr_seq_misordered;
2773 if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED))
2774 goto out_put_session;
2775 cstate->slot = slot;
2776 cstate->session = session;
2777 cstate->clp = clp;
2778 /* Return the cached reply status and set cstate->status
2779 * for nfsd4_proc_compound processing */
2780 status = nfsd4_replay_cache_entry(resp, seq);
2781 cstate->status = nfserr_replay_cache;
2782 goto out;
2784 if (status)
2785 goto out_put_session;
2787 status = nfsd4_sequence_check_conn(conn, session);
2788 conn = NULL;
2789 if (status)
2790 goto out_put_session;
2792 buflen = (seq->cachethis) ?
2793 session->se_fchannel.maxresp_cached :
2794 session->se_fchannel.maxresp_sz;
2795 status = (seq->cachethis) ? nfserr_rep_too_big_to_cache :
2796 nfserr_rep_too_big;
2797 if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack))
2798 goto out_put_session;
2799 svc_reserve(rqstp, buflen);
2801 status = nfs_ok;
2802 /* Success! bump slot seqid */
2803 slot->sl_seqid = seq->seqid;
2804 slot->sl_flags |= NFSD4_SLOT_INUSE;
2805 if (seq->cachethis)
2806 slot->sl_flags |= NFSD4_SLOT_CACHETHIS;
2807 else
2808 slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS;
2810 cstate->slot = slot;
2811 cstate->session = session;
2812 cstate->clp = clp;
2814 out:
2815 switch (clp->cl_cb_state) {
2816 case NFSD4_CB_DOWN:
2817 seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN;
2818 break;
2819 case NFSD4_CB_FAULT:
2820 seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT;
2821 break;
2822 default:
2823 seq->status_flags = 0;
2825 if (!list_empty(&clp->cl_revoked))
2826 seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED;
2827 out_no_session:
2828 if (conn)
2829 free_conn(conn);
2830 spin_unlock(&nn->client_lock);
2831 return status;
2832 out_put_session:
2833 nfsd4_put_session_locked(session);
2834 goto out_no_session;
2837 void
2838 nfsd4_sequence_done(struct nfsd4_compoundres *resp)
2840 struct nfsd4_compound_state *cs = &resp->cstate;
2842 if (nfsd4_has_session(cs)) {
2843 if (cs->status != nfserr_replay_cache) {
2844 nfsd4_store_cache_entry(resp);
2845 cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE;
2847 /* Drop session reference that was taken in nfsd4_sequence() */
2848 nfsd4_put_session(cs->session);
2849 } else if (cs->clp)
2850 put_client_renew(cs->clp);
2853 __be32
2854 nfsd4_destroy_clientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_destroy_clientid *dc)
2856 struct nfs4_client *conf, *unconf;
2857 struct nfs4_client *clp = NULL;
2858 __be32 status = 0;
2859 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2861 spin_lock(&nn->client_lock);
2862 unconf = find_unconfirmed_client(&dc->clientid, true, nn);
2863 conf = find_confirmed_client(&dc->clientid, true, nn);
2864 WARN_ON_ONCE(conf && unconf);
2866 if (conf) {
2867 if (client_has_state(conf)) {
2868 status = nfserr_clientid_busy;
2869 goto out;
2871 status = mark_client_expired_locked(conf);
2872 if (status)
2873 goto out;
2874 clp = conf;
2875 } else if (unconf)
2876 clp = unconf;
2877 else {
2878 status = nfserr_stale_clientid;
2879 goto out;
2881 if (!mach_creds_match(clp, rqstp)) {
2882 clp = NULL;
2883 status = nfserr_wrong_cred;
2884 goto out;
2886 unhash_client_locked(clp);
2887 out:
2888 spin_unlock(&nn->client_lock);
2889 if (clp)
2890 expire_client(clp);
2891 return status;
2894 __be32
2895 nfsd4_reclaim_complete(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, struct nfsd4_reclaim_complete *rc)
2897 __be32 status = 0;
2899 if (rc->rca_one_fs) {
2900 if (!cstate->current_fh.fh_dentry)
2901 return nfserr_nofilehandle;
2903 * We don't take advantage of the rca_one_fs case.
2904 * That's OK, it's optional, we can safely ignore it.
2906 return nfs_ok;
2909 status = nfserr_complete_already;
2910 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE,
2911 &cstate->session->se_client->cl_flags))
2912 goto out;
2914 status = nfserr_stale_clientid;
2915 if (is_client_expired(cstate->session->se_client))
2917 * The following error isn't really legal.
2918 * But we only get here if the client just explicitly
2919 * destroyed the client. Surely it no longer cares what
2920 * error it gets back on an operation for the dead
2921 * client.
2923 goto out;
2925 status = nfs_ok;
2926 nfsd4_client_record_create(cstate->session->se_client);
2927 out:
2928 return status;
2931 __be32
2932 nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
2933 struct nfsd4_setclientid *setclid)
2935 struct xdr_netobj clname = setclid->se_name;
2936 nfs4_verifier clverifier = setclid->se_verf;
2937 struct nfs4_client *conf, *new;
2938 struct nfs4_client *unconf = NULL;
2939 __be32 status;
2940 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
2942 new = create_client(clname, rqstp, &clverifier);
2943 if (new == NULL)
2944 return nfserr_jukebox;
2945 /* Cases below refer to rfc 3530 section 14.2.33: */
2946 spin_lock(&nn->client_lock);
2947 conf = find_confirmed_client_by_name(&clname, nn);
2948 if (conf) {
2949 /* case 0: */
2950 status = nfserr_clid_inuse;
2951 if (clp_used_exchangeid(conf))
2952 goto out;
2953 if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) {
2954 char addr_str[INET6_ADDRSTRLEN];
2955 rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str,
2956 sizeof(addr_str));
2957 dprintk("NFSD: setclientid: string in use by client "
2958 "at %s\n", addr_str);
2959 goto out;
2962 unconf = find_unconfirmed_client_by_name(&clname, nn);
2963 if (unconf)
2964 unhash_client_locked(unconf);
2965 if (conf && same_verf(&conf->cl_verifier, &clverifier))
2966 /* case 1: probable callback update */
2967 copy_clid(new, conf);
2968 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2969 gen_clid(new, nn);
2970 new->cl_minorversion = 0;
2971 gen_callback(new, setclid, rqstp);
2972 add_to_unconfirmed(new);
2973 setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot;
2974 setclid->se_clientid.cl_id = new->cl_clientid.cl_id;
2975 memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data));
2976 new = NULL;
2977 status = nfs_ok;
2978 out:
2979 spin_unlock(&nn->client_lock);
2980 if (new)
2981 free_client(new);
2982 if (unconf)
2983 expire_client(unconf);
2984 return status;
2988 __be32
2989 nfsd4_setclientid_confirm(struct svc_rqst *rqstp,
2990 struct nfsd4_compound_state *cstate,
2991 struct nfsd4_setclientid_confirm *setclientid_confirm)
2993 struct nfs4_client *conf, *unconf;
2994 struct nfs4_client *old = NULL;
2995 nfs4_verifier confirm = setclientid_confirm->sc_confirm;
2996 clientid_t * clid = &setclientid_confirm->sc_clientid;
2997 __be32 status;
2998 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
3000 if (STALE_CLIENTID(clid, nn))
3001 return nfserr_stale_clientid;
3003 spin_lock(&nn->client_lock);
3004 conf = find_confirmed_client(clid, false, nn);
3005 unconf = find_unconfirmed_client(clid, false, nn);
3007 * We try hard to give out unique clientid's, so if we get an
3008 * attempt to confirm the same clientid with a different cred,
3009 * there's a bug somewhere. Let's charitably assume it's our
3010 * bug.
3012 status = nfserr_serverfault;
3013 if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred))
3014 goto out;
3015 if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred))
3016 goto out;
3017 /* cases below refer to rfc 3530 section 14.2.34: */
3018 if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) {
3019 if (conf && !unconf) /* case 2: probable retransmit */
3020 status = nfs_ok;
3021 else /* case 4: client hasn't noticed we rebooted yet? */
3022 status = nfserr_stale_clientid;
3023 goto out;
3025 status = nfs_ok;
3026 if (conf) { /* case 1: callback update */
3027 old = unconf;
3028 unhash_client_locked(old);
3029 nfsd4_change_callback(conf, &unconf->cl_cb_conn);
3030 } else { /* case 3: normal case; new or rebooted client */
3031 old = find_confirmed_client_by_name(&unconf->cl_name, nn);
3032 if (old) {
3033 status = mark_client_expired_locked(old);
3034 if (status) {
3035 old = NULL;
3036 goto out;
3039 move_to_confirmed(unconf);
3040 conf = unconf;
3042 get_client_locked(conf);
3043 spin_unlock(&nn->client_lock);
3044 nfsd4_probe_callback(conf);
3045 spin_lock(&nn->client_lock);
3046 put_client_renew_locked(conf);
3047 out:
3048 spin_unlock(&nn->client_lock);
3049 if (old)
3050 expire_client(old);
3051 return status;
3054 static struct nfs4_file *nfsd4_alloc_file(void)
3056 return kmem_cache_alloc(file_slab, GFP_KERNEL);
3059 /* OPEN Share state helper functions */
3060 static void nfsd4_init_file(struct nfs4_file *fp, struct knfsd_fh *fh)
3062 unsigned int hashval = file_hashval(fh);
3064 lockdep_assert_held(&state_lock);
3066 atomic_set(&fp->fi_ref, 1);
3067 spin_lock_init(&fp->fi_lock);
3068 INIT_LIST_HEAD(&fp->fi_stateids);
3069 INIT_LIST_HEAD(&fp->fi_delegations);
3070 fh_copy_shallow(&fp->fi_fhandle, fh);
3071 fp->fi_deleg_file = NULL;
3072 fp->fi_had_conflict = false;
3073 fp->fi_share_deny = 0;
3074 memset(fp->fi_fds, 0, sizeof(fp->fi_fds));
3075 memset(fp->fi_access, 0, sizeof(fp->fi_access));
3076 hlist_add_head(&fp->fi_hash, &file_hashtbl[hashval]);
3079 void
3080 nfsd4_free_slabs(void)
3082 kmem_cache_destroy(openowner_slab);
3083 kmem_cache_destroy(lockowner_slab);
3084 kmem_cache_destroy(file_slab);
3085 kmem_cache_destroy(stateid_slab);
3086 kmem_cache_destroy(deleg_slab);
3090 nfsd4_init_slabs(void)
3092 openowner_slab = kmem_cache_create("nfsd4_openowners",
3093 sizeof(struct nfs4_openowner), 0, 0, NULL);
3094 if (openowner_slab == NULL)
3095 goto out;
3096 lockowner_slab = kmem_cache_create("nfsd4_lockowners",
3097 sizeof(struct nfs4_lockowner), 0, 0, NULL);
3098 if (lockowner_slab == NULL)
3099 goto out_free_openowner_slab;
3100 file_slab = kmem_cache_create("nfsd4_files",
3101 sizeof(struct nfs4_file), 0, 0, NULL);
3102 if (file_slab == NULL)
3103 goto out_free_lockowner_slab;
3104 stateid_slab = kmem_cache_create("nfsd4_stateids",
3105 sizeof(struct nfs4_ol_stateid), 0, 0, NULL);
3106 if (stateid_slab == NULL)
3107 goto out_free_file_slab;
3108 deleg_slab = kmem_cache_create("nfsd4_delegations",
3109 sizeof(struct nfs4_delegation), 0, 0, NULL);
3110 if (deleg_slab == NULL)
3111 goto out_free_stateid_slab;
3112 return 0;
3114 out_free_stateid_slab:
3115 kmem_cache_destroy(stateid_slab);
3116 out_free_file_slab:
3117 kmem_cache_destroy(file_slab);
3118 out_free_lockowner_slab:
3119 kmem_cache_destroy(lockowner_slab);
3120 out_free_openowner_slab:
3121 kmem_cache_destroy(openowner_slab);
3122 out:
3123 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3124 return -ENOMEM;
3127 static void init_nfs4_replay(struct nfs4_replay *rp)
3129 rp->rp_status = nfserr_serverfault;
3130 rp->rp_buflen = 0;
3131 rp->rp_buf = rp->rp_ibuf;
3132 mutex_init(&rp->rp_mutex);
3135 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate,
3136 struct nfs4_stateowner *so)
3138 if (!nfsd4_has_session(cstate)) {
3139 mutex_lock(&so->so_replay.rp_mutex);
3140 cstate->replay_owner = nfs4_get_stateowner(so);
3144 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate)
3146 struct nfs4_stateowner *so = cstate->replay_owner;
3148 if (so != NULL) {
3149 cstate->replay_owner = NULL;
3150 mutex_unlock(&so->so_replay.rp_mutex);
3151 nfs4_put_stateowner(so);
3155 static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp)
3157 struct nfs4_stateowner *sop;
3159 sop = kmem_cache_alloc(slab, GFP_KERNEL);
3160 if (!sop)
3161 return NULL;
3163 sop->so_owner.data = kmemdup(owner->data, owner->len, GFP_KERNEL);
3164 if (!sop->so_owner.data) {
3165 kmem_cache_free(slab, sop);
3166 return NULL;
3168 sop->so_owner.len = owner->len;
3170 INIT_LIST_HEAD(&sop->so_stateids);
3171 sop->so_client = clp;
3172 init_nfs4_replay(&sop->so_replay);
3173 atomic_set(&sop->so_count, 1);
3174 return sop;
3177 static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval)
3179 lockdep_assert_held(&clp->cl_lock);
3181 list_add(&oo->oo_owner.so_strhash,
3182 &clp->cl_ownerstr_hashtbl[strhashval]);
3183 list_add(&oo->oo_perclient, &clp->cl_openowners);
3186 static void nfs4_unhash_openowner(struct nfs4_stateowner *so)
3188 unhash_openowner_locked(openowner(so));
3191 static void nfs4_free_openowner(struct nfs4_stateowner *so)
3193 struct nfs4_openowner *oo = openowner(so);
3195 kmem_cache_free(openowner_slab, oo);
3198 static const struct nfs4_stateowner_operations openowner_ops = {
3199 .so_unhash = nfs4_unhash_openowner,
3200 .so_free = nfs4_free_openowner,
3203 static struct nfs4_openowner *
3204 alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open,
3205 struct nfsd4_compound_state *cstate)
3207 struct nfs4_client *clp = cstate->clp;
3208 struct nfs4_openowner *oo, *ret;
3210 oo = alloc_stateowner(openowner_slab, &open->op_owner, clp);
3211 if (!oo)
3212 return NULL;
3213 oo->oo_owner.so_ops = &openowner_ops;
3214 oo->oo_owner.so_is_open_owner = 1;
3215 oo->oo_owner.so_seqid = open->op_seqid;
3216 oo->oo_flags = 0;
3217 if (nfsd4_has_session(cstate))
3218 oo->oo_flags |= NFS4_OO_CONFIRMED;
3219 oo->oo_time = 0;
3220 oo->oo_last_closed_stid = NULL;
3221 INIT_LIST_HEAD(&oo->oo_close_lru);
3222 spin_lock(&clp->cl_lock);
3223 ret = find_openstateowner_str_locked(strhashval, open, clp);
3224 if (ret == NULL) {
3225 hash_openowner(oo, clp, strhashval);
3226 ret = oo;
3227 } else
3228 nfs4_free_openowner(&oo->oo_owner);
3229 spin_unlock(&clp->cl_lock);
3230 return oo;
3233 static void init_open_stateid(struct nfs4_ol_stateid *stp, struct nfs4_file *fp, struct nfsd4_open *open) {
3234 struct nfs4_openowner *oo = open->op_openowner;
3236 atomic_inc(&stp->st_stid.sc_count);
3237 stp->st_stid.sc_type = NFS4_OPEN_STID;
3238 INIT_LIST_HEAD(&stp->st_locks);
3239 stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner);
3240 get_nfs4_file(fp);
3241 stp->st_stid.sc_file = fp;
3242 stp->st_access_bmap = 0;
3243 stp->st_deny_bmap = 0;
3244 stp->st_openstp = NULL;
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 atomic_inc(&fp->fi_delegees);
3895 if (fp->fi_had_conflict) {
3896 status = -EAGAIN;
3897 goto out_unlock;
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 status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open);
4062 if (status)
4063 goto out;
4064 } else {
4065 stp = open->op_stp;
4066 open->op_stp = NULL;
4067 init_open_stateid(stp, fp, open);
4068 status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open);
4069 if (status) {
4070 release_open_stateid(stp);
4071 goto out;
4074 update_stateid(&stp->st_stid.sc_stateid);
4075 memcpy(&open->op_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4077 if (nfsd4_has_session(&resp->cstate)) {
4078 if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) {
4079 open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT;
4080 open->op_why_no_deleg = WND4_NOT_WANTED;
4081 goto nodeleg;
4086 * Attempt to hand out a delegation. No error return, because the
4087 * OPEN succeeds even if we fail.
4089 nfs4_open_delegation(current_fh, open, stp);
4090 nodeleg:
4091 status = nfs_ok;
4093 dprintk("%s: stateid=" STATEID_FMT "\n", __func__,
4094 STATEID_VAL(&stp->st_stid.sc_stateid));
4095 out:
4096 /* 4.1 client trying to upgrade/downgrade delegation? */
4097 if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp &&
4098 open->op_deleg_want)
4099 nfsd4_deleg_xgrade_none_ext(open, dp);
4101 if (fp)
4102 put_nfs4_file(fp);
4103 if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS)
4104 nfs4_set_claim_prev(open, nfsd4_has_session(&resp->cstate));
4106 * To finish the open response, we just need to set the rflags.
4108 open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX;
4109 if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED) &&
4110 !nfsd4_has_session(&resp->cstate))
4111 open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM;
4112 if (dp)
4113 nfs4_put_stid(&dp->dl_stid);
4114 if (stp)
4115 nfs4_put_stid(&stp->st_stid);
4117 return status;
4120 void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate,
4121 struct nfsd4_open *open, __be32 status)
4123 if (open->op_openowner) {
4124 struct nfs4_stateowner *so = &open->op_openowner->oo_owner;
4126 nfsd4_cstate_assign_replay(cstate, so);
4127 nfs4_put_stateowner(so);
4129 if (open->op_file)
4130 nfsd4_free_file(open->op_file);
4131 if (open->op_stp)
4132 nfs4_put_stid(&open->op_stp->st_stid);
4135 __be32
4136 nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4137 clientid_t *clid)
4139 struct nfs4_client *clp;
4140 __be32 status;
4141 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4143 dprintk("process_renew(%08x/%08x): starting\n",
4144 clid->cl_boot, clid->cl_id);
4145 status = lookup_clientid(clid, cstate, nn);
4146 if (status)
4147 goto out;
4148 clp = cstate->clp;
4149 status = nfserr_cb_path_down;
4150 if (!list_empty(&clp->cl_delegations)
4151 && clp->cl_cb_state != NFSD4_CB_UP)
4152 goto out;
4153 status = nfs_ok;
4154 out:
4155 return status;
4158 void
4159 nfsd4_end_grace(struct nfsd_net *nn)
4161 /* do nothing if grace period already ended */
4162 if (nn->grace_ended)
4163 return;
4165 dprintk("NFSD: end of grace period\n");
4166 nn->grace_ended = true;
4168 * If the server goes down again right now, an NFSv4
4169 * client will still be allowed to reclaim after it comes back up,
4170 * even if it hasn't yet had a chance to reclaim state this time.
4173 nfsd4_record_grace_done(nn);
4175 * At this point, NFSv4 clients can still reclaim. But if the
4176 * server crashes, any that have not yet reclaimed will be out
4177 * of luck on the next boot.
4179 * (NFSv4.1+ clients are considered to have reclaimed once they
4180 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4181 * have reclaimed after their first OPEN.)
4183 locks_end_grace(&nn->nfsd4_manager);
4185 * At this point, and once lockd and/or any other containers
4186 * exit their grace period, further reclaims will fail and
4187 * regular locking can resume.
4191 static time_t
4192 nfs4_laundromat(struct nfsd_net *nn)
4194 struct nfs4_client *clp;
4195 struct nfs4_openowner *oo;
4196 struct nfs4_delegation *dp;
4197 struct nfs4_ol_stateid *stp;
4198 struct list_head *pos, *next, reaplist;
4199 time_t cutoff = get_seconds() - nn->nfsd4_lease;
4200 time_t t, new_timeo = nn->nfsd4_lease;
4202 dprintk("NFSD: laundromat service - starting\n");
4203 nfsd4_end_grace(nn);
4204 INIT_LIST_HEAD(&reaplist);
4205 spin_lock(&nn->client_lock);
4206 list_for_each_safe(pos, next, &nn->client_lru) {
4207 clp = list_entry(pos, struct nfs4_client, cl_lru);
4208 if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) {
4209 t = clp->cl_time - cutoff;
4210 new_timeo = min(new_timeo, t);
4211 break;
4213 if (mark_client_expired_locked(clp)) {
4214 dprintk("NFSD: client in use (clientid %08x)\n",
4215 clp->cl_clientid.cl_id);
4216 continue;
4218 list_add(&clp->cl_lru, &reaplist);
4220 spin_unlock(&nn->client_lock);
4221 list_for_each_safe(pos, next, &reaplist) {
4222 clp = list_entry(pos, struct nfs4_client, cl_lru);
4223 dprintk("NFSD: purging unused client (clientid %08x)\n",
4224 clp->cl_clientid.cl_id);
4225 list_del_init(&clp->cl_lru);
4226 expire_client(clp);
4228 spin_lock(&state_lock);
4229 list_for_each_safe(pos, next, &nn->del_recall_lru) {
4230 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
4231 if (net_generic(dp->dl_stid.sc_client->net, nfsd_net_id) != nn)
4232 continue;
4233 if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) {
4234 t = dp->dl_time - cutoff;
4235 new_timeo = min(new_timeo, t);
4236 break;
4238 unhash_delegation_locked(dp);
4239 list_add(&dp->dl_recall_lru, &reaplist);
4241 spin_unlock(&state_lock);
4242 while (!list_empty(&reaplist)) {
4243 dp = list_first_entry(&reaplist, struct nfs4_delegation,
4244 dl_recall_lru);
4245 list_del_init(&dp->dl_recall_lru);
4246 revoke_delegation(dp);
4249 spin_lock(&nn->client_lock);
4250 while (!list_empty(&nn->close_lru)) {
4251 oo = list_first_entry(&nn->close_lru, struct nfs4_openowner,
4252 oo_close_lru);
4253 if (time_after((unsigned long)oo->oo_time,
4254 (unsigned long)cutoff)) {
4255 t = oo->oo_time - cutoff;
4256 new_timeo = min(new_timeo, t);
4257 break;
4259 list_del_init(&oo->oo_close_lru);
4260 stp = oo->oo_last_closed_stid;
4261 oo->oo_last_closed_stid = NULL;
4262 spin_unlock(&nn->client_lock);
4263 nfs4_put_stid(&stp->st_stid);
4264 spin_lock(&nn->client_lock);
4266 spin_unlock(&nn->client_lock);
4268 new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT);
4269 return new_timeo;
4272 static struct workqueue_struct *laundry_wq;
4273 static void laundromat_main(struct work_struct *);
4275 static void
4276 laundromat_main(struct work_struct *laundry)
4278 time_t t;
4279 struct delayed_work *dwork = container_of(laundry, struct delayed_work,
4280 work);
4281 struct nfsd_net *nn = container_of(dwork, struct nfsd_net,
4282 laundromat_work);
4284 t = nfs4_laundromat(nn);
4285 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t);
4286 queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ);
4289 static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_ol_stateid *stp)
4291 if (!nfsd_fh_match(&fhp->fh_handle, &stp->st_stid.sc_file->fi_fhandle))
4292 return nfserr_bad_stateid;
4293 return nfs_ok;
4296 static inline int
4297 access_permit_read(struct nfs4_ol_stateid *stp)
4299 return test_access(NFS4_SHARE_ACCESS_READ, stp) ||
4300 test_access(NFS4_SHARE_ACCESS_BOTH, stp) ||
4301 test_access(NFS4_SHARE_ACCESS_WRITE, stp);
4304 static inline int
4305 access_permit_write(struct nfs4_ol_stateid *stp)
4307 return test_access(NFS4_SHARE_ACCESS_WRITE, stp) ||
4308 test_access(NFS4_SHARE_ACCESS_BOTH, stp);
4311 static
4312 __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags)
4314 __be32 status = nfserr_openmode;
4316 /* For lock stateid's, we test the parent open, not the lock: */
4317 if (stp->st_openstp)
4318 stp = stp->st_openstp;
4319 if ((flags & WR_STATE) && !access_permit_write(stp))
4320 goto out;
4321 if ((flags & RD_STATE) && !access_permit_read(stp))
4322 goto out;
4323 status = nfs_ok;
4324 out:
4325 return status;
4328 static inline __be32
4329 check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags)
4331 if (ONE_STATEID(stateid) && (flags & RD_STATE))
4332 return nfs_ok;
4333 else if (locks_in_grace(net)) {
4334 /* Answer in remaining cases depends on existence of
4335 * conflicting state; so we must wait out the grace period. */
4336 return nfserr_grace;
4337 } else if (flags & WR_STATE)
4338 return nfs4_share_conflict(current_fh,
4339 NFS4_SHARE_DENY_WRITE);
4340 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4341 return nfs4_share_conflict(current_fh,
4342 NFS4_SHARE_DENY_READ);
4346 * Allow READ/WRITE during grace period on recovered state only for files
4347 * that are not able to provide mandatory locking.
4349 static inline int
4350 grace_disallows_io(struct net *net, struct inode *inode)
4352 return locks_in_grace(net) && mandatory_lock(inode);
4355 /* Returns true iff a is later than b: */
4356 static bool stateid_generation_after(stateid_t *a, stateid_t *b)
4358 return (s32)(a->si_generation - b->si_generation) > 0;
4361 static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session)
4364 * When sessions are used the stateid generation number is ignored
4365 * when it is zero.
4367 if (has_session && in->si_generation == 0)
4368 return nfs_ok;
4370 if (in->si_generation == ref->si_generation)
4371 return nfs_ok;
4373 /* If the client sends us a stateid from the future, it's buggy: */
4374 if (stateid_generation_after(in, ref))
4375 return nfserr_bad_stateid;
4377 * However, we could see a stateid from the past, even from a
4378 * non-buggy client. For example, if the client sends a lock
4379 * while some IO is outstanding, the lock may bump si_generation
4380 * while the IO is still in flight. The client could avoid that
4381 * situation by waiting for responses on all the IO requests,
4382 * but better performance may result in retrying IO that
4383 * receives an old_stateid error if requests are rarely
4384 * reordered in flight:
4386 return nfserr_old_stateid;
4389 static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid)
4391 struct nfs4_stid *s;
4392 struct nfs4_ol_stateid *ols;
4393 __be32 status = nfserr_bad_stateid;
4395 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4396 return status;
4397 /* Client debugging aid. */
4398 if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) {
4399 char addr_str[INET6_ADDRSTRLEN];
4400 rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str,
4401 sizeof(addr_str));
4402 pr_warn_ratelimited("NFSD: client %s testing state ID "
4403 "with incorrect client ID\n", addr_str);
4404 return status;
4406 spin_lock(&cl->cl_lock);
4407 s = find_stateid_locked(cl, stateid);
4408 if (!s)
4409 goto out_unlock;
4410 status = check_stateid_generation(stateid, &s->sc_stateid, 1);
4411 if (status)
4412 goto out_unlock;
4413 switch (s->sc_type) {
4414 case NFS4_DELEG_STID:
4415 status = nfs_ok;
4416 break;
4417 case NFS4_REVOKED_DELEG_STID:
4418 status = nfserr_deleg_revoked;
4419 break;
4420 case NFS4_OPEN_STID:
4421 case NFS4_LOCK_STID:
4422 ols = openlockstateid(s);
4423 if (ols->st_stateowner->so_is_open_owner
4424 && !(openowner(ols->st_stateowner)->oo_flags
4425 & NFS4_OO_CONFIRMED))
4426 status = nfserr_bad_stateid;
4427 else
4428 status = nfs_ok;
4429 break;
4430 default:
4431 printk("unknown stateid type %x\n", s->sc_type);
4432 /* Fallthrough */
4433 case NFS4_CLOSED_STID:
4434 case NFS4_CLOSED_DELEG_STID:
4435 status = nfserr_bad_stateid;
4437 out_unlock:
4438 spin_unlock(&cl->cl_lock);
4439 return status;
4442 static __be32
4443 nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate,
4444 stateid_t *stateid, unsigned char typemask,
4445 struct nfs4_stid **s, struct nfsd_net *nn)
4447 __be32 status;
4449 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4450 return nfserr_bad_stateid;
4451 status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn);
4452 if (status == nfserr_stale_clientid) {
4453 if (cstate->session)
4454 return nfserr_bad_stateid;
4455 return nfserr_stale_stateid;
4457 if (status)
4458 return status;
4459 *s = find_stateid_by_type(cstate->clp, stateid, typemask);
4460 if (!*s)
4461 return nfserr_bad_stateid;
4462 return nfs_ok;
4466 * Checks for stateid operations
4468 __be32
4469 nfs4_preprocess_stateid_op(struct net *net, struct nfsd4_compound_state *cstate,
4470 stateid_t *stateid, int flags, struct file **filpp)
4472 struct nfs4_stid *s;
4473 struct nfs4_ol_stateid *stp = NULL;
4474 struct nfs4_delegation *dp = NULL;
4475 struct svc_fh *current_fh = &cstate->current_fh;
4476 struct inode *ino = current_fh->fh_dentry->d_inode;
4477 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4478 struct file *file = NULL;
4479 __be32 status;
4481 if (filpp)
4482 *filpp = NULL;
4484 if (grace_disallows_io(net, ino))
4485 return nfserr_grace;
4487 if (ZERO_STATEID(stateid) || ONE_STATEID(stateid))
4488 return check_special_stateids(net, current_fh, stateid, flags);
4490 status = nfsd4_lookup_stateid(cstate, stateid,
4491 NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID,
4492 &s, nn);
4493 if (status)
4494 return status;
4495 status = check_stateid_generation(stateid, &s->sc_stateid, nfsd4_has_session(cstate));
4496 if (status)
4497 goto out;
4498 switch (s->sc_type) {
4499 case NFS4_DELEG_STID:
4500 dp = delegstateid(s);
4501 status = nfs4_check_delegmode(dp, flags);
4502 if (status)
4503 goto out;
4504 if (filpp) {
4505 file = dp->dl_stid.sc_file->fi_deleg_file;
4506 if (!file) {
4507 WARN_ON_ONCE(1);
4508 status = nfserr_serverfault;
4509 goto out;
4511 get_file(file);
4513 break;
4514 case NFS4_OPEN_STID:
4515 case NFS4_LOCK_STID:
4516 stp = openlockstateid(s);
4517 status = nfs4_check_fh(current_fh, stp);
4518 if (status)
4519 goto out;
4520 if (stp->st_stateowner->so_is_open_owner
4521 && !(openowner(stp->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED))
4522 goto out;
4523 status = nfs4_check_openmode(stp, flags);
4524 if (status)
4525 goto out;
4526 if (filpp) {
4527 struct nfs4_file *fp = stp->st_stid.sc_file;
4529 if (flags & RD_STATE)
4530 file = find_readable_file(fp);
4531 else
4532 file = find_writeable_file(fp);
4534 break;
4535 default:
4536 status = nfserr_bad_stateid;
4537 goto out;
4539 status = nfs_ok;
4540 if (file)
4541 *filpp = file;
4542 out:
4543 nfs4_put_stid(s);
4544 return status;
4548 * Test if the stateid is valid
4550 __be32
4551 nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4552 struct nfsd4_test_stateid *test_stateid)
4554 struct nfsd4_test_stateid_id *stateid;
4555 struct nfs4_client *cl = cstate->session->se_client;
4557 list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list)
4558 stateid->ts_id_status =
4559 nfsd4_validate_stateid(cl, &stateid->ts_id_stateid);
4561 return nfs_ok;
4564 __be32
4565 nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4566 struct nfsd4_free_stateid *free_stateid)
4568 stateid_t *stateid = &free_stateid->fr_stateid;
4569 struct nfs4_stid *s;
4570 struct nfs4_delegation *dp;
4571 struct nfs4_ol_stateid *stp;
4572 struct nfs4_client *cl = cstate->session->se_client;
4573 __be32 ret = nfserr_bad_stateid;
4575 spin_lock(&cl->cl_lock);
4576 s = find_stateid_locked(cl, stateid);
4577 if (!s)
4578 goto out_unlock;
4579 switch (s->sc_type) {
4580 case NFS4_DELEG_STID:
4581 ret = nfserr_locks_held;
4582 break;
4583 case NFS4_OPEN_STID:
4584 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4585 if (ret)
4586 break;
4587 ret = nfserr_locks_held;
4588 break;
4589 case NFS4_LOCK_STID:
4590 ret = check_stateid_generation(stateid, &s->sc_stateid, 1);
4591 if (ret)
4592 break;
4593 stp = openlockstateid(s);
4594 ret = nfserr_locks_held;
4595 if (check_for_locks(stp->st_stid.sc_file,
4596 lockowner(stp->st_stateowner)))
4597 break;
4598 unhash_lock_stateid(stp);
4599 spin_unlock(&cl->cl_lock);
4600 nfs4_put_stid(s);
4601 ret = nfs_ok;
4602 goto out;
4603 case NFS4_REVOKED_DELEG_STID:
4604 dp = delegstateid(s);
4605 list_del_init(&dp->dl_recall_lru);
4606 spin_unlock(&cl->cl_lock);
4607 nfs4_put_stid(s);
4608 ret = nfs_ok;
4609 goto out;
4610 /* Default falls through and returns nfserr_bad_stateid */
4612 out_unlock:
4613 spin_unlock(&cl->cl_lock);
4614 out:
4615 return ret;
4618 static inline int
4619 setlkflg (int type)
4621 return (type == NFS4_READW_LT || type == NFS4_READ_LT) ?
4622 RD_STATE : WR_STATE;
4625 static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp)
4627 struct svc_fh *current_fh = &cstate->current_fh;
4628 struct nfs4_stateowner *sop = stp->st_stateowner;
4629 __be32 status;
4631 status = nfsd4_check_seqid(cstate, sop, seqid);
4632 if (status)
4633 return status;
4634 if (stp->st_stid.sc_type == NFS4_CLOSED_STID
4635 || stp->st_stid.sc_type == NFS4_REVOKED_DELEG_STID)
4637 * "Closed" stateid's exist *only* to return
4638 * nfserr_replay_me from the previous step, and
4639 * revoked delegations are kept only for free_stateid.
4641 return nfserr_bad_stateid;
4642 status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate));
4643 if (status)
4644 return status;
4645 return nfs4_check_fh(current_fh, stp);
4649 * Checks for sequence id mutating operations.
4651 static __be32
4652 nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4653 stateid_t *stateid, char typemask,
4654 struct nfs4_ol_stateid **stpp,
4655 struct nfsd_net *nn)
4657 __be32 status;
4658 struct nfs4_stid *s;
4659 struct nfs4_ol_stateid *stp = NULL;
4661 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__,
4662 seqid, STATEID_VAL(stateid));
4664 *stpp = NULL;
4665 status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn);
4666 if (status)
4667 return status;
4668 stp = openlockstateid(s);
4669 nfsd4_cstate_assign_replay(cstate, stp->st_stateowner);
4671 status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp);
4672 if (!status)
4673 *stpp = stp;
4674 else
4675 nfs4_put_stid(&stp->st_stid);
4676 return status;
4679 static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid,
4680 stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn)
4682 __be32 status;
4683 struct nfs4_openowner *oo;
4684 struct nfs4_ol_stateid *stp;
4686 status = nfs4_preprocess_seqid_op(cstate, seqid, stateid,
4687 NFS4_OPEN_STID, &stp, nn);
4688 if (status)
4689 return status;
4690 oo = openowner(stp->st_stateowner);
4691 if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) {
4692 nfs4_put_stid(&stp->st_stid);
4693 return nfserr_bad_stateid;
4695 *stpp = stp;
4696 return nfs_ok;
4699 __be32
4700 nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4701 struct nfsd4_open_confirm *oc)
4703 __be32 status;
4704 struct nfs4_openowner *oo;
4705 struct nfs4_ol_stateid *stp;
4706 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4708 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4709 cstate->current_fh.fh_dentry);
4711 status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0);
4712 if (status)
4713 return status;
4715 status = nfs4_preprocess_seqid_op(cstate,
4716 oc->oc_seqid, &oc->oc_req_stateid,
4717 NFS4_OPEN_STID, &stp, nn);
4718 if (status)
4719 goto out;
4720 oo = openowner(stp->st_stateowner);
4721 status = nfserr_bad_stateid;
4722 if (oo->oo_flags & NFS4_OO_CONFIRMED)
4723 goto put_stateid;
4724 oo->oo_flags |= NFS4_OO_CONFIRMED;
4725 update_stateid(&stp->st_stid.sc_stateid);
4726 memcpy(&oc->oc_resp_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4727 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n",
4728 __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid));
4730 nfsd4_client_record_create(oo->oo_owner.so_client);
4731 status = nfs_ok;
4732 put_stateid:
4733 nfs4_put_stid(&stp->st_stid);
4734 out:
4735 nfsd4_bump_seqid(cstate, status);
4736 return status;
4739 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access)
4741 if (!test_access(access, stp))
4742 return;
4743 nfs4_file_put_access(stp->st_stid.sc_file, access);
4744 clear_access(access, stp);
4747 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access)
4749 switch (to_access) {
4750 case NFS4_SHARE_ACCESS_READ:
4751 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE);
4752 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4753 break;
4754 case NFS4_SHARE_ACCESS_WRITE:
4755 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ);
4756 nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH);
4757 break;
4758 case NFS4_SHARE_ACCESS_BOTH:
4759 break;
4760 default:
4761 WARN_ON_ONCE(1);
4765 __be32
4766 nfsd4_open_downgrade(struct svc_rqst *rqstp,
4767 struct nfsd4_compound_state *cstate,
4768 struct nfsd4_open_downgrade *od)
4770 __be32 status;
4771 struct nfs4_ol_stateid *stp;
4772 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4774 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4775 cstate->current_fh.fh_dentry);
4777 /* We don't yet support WANT bits: */
4778 if (od->od_deleg_want)
4779 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__,
4780 od->od_deleg_want);
4782 status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid,
4783 &od->od_stateid, &stp, nn);
4784 if (status)
4785 goto out;
4786 status = nfserr_inval;
4787 if (!test_access(od->od_share_access, stp)) {
4788 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4789 stp->st_access_bmap, od->od_share_access);
4790 goto put_stateid;
4792 if (!test_deny(od->od_share_deny, stp)) {
4793 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4794 stp->st_deny_bmap, od->od_share_deny);
4795 goto put_stateid;
4797 nfs4_stateid_downgrade(stp, od->od_share_access);
4799 reset_union_bmap_deny(od->od_share_deny, stp);
4801 update_stateid(&stp->st_stid.sc_stateid);
4802 memcpy(&od->od_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4803 status = nfs_ok;
4804 put_stateid:
4805 nfs4_put_stid(&stp->st_stid);
4806 out:
4807 nfsd4_bump_seqid(cstate, status);
4808 return status;
4811 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s)
4813 struct nfs4_client *clp = s->st_stid.sc_client;
4814 LIST_HEAD(reaplist);
4816 s->st_stid.sc_type = NFS4_CLOSED_STID;
4817 spin_lock(&clp->cl_lock);
4818 unhash_open_stateid(s, &reaplist);
4820 if (clp->cl_minorversion) {
4821 put_ol_stateid_locked(s, &reaplist);
4822 spin_unlock(&clp->cl_lock);
4823 free_ol_stateid_reaplist(&reaplist);
4824 } else {
4825 spin_unlock(&clp->cl_lock);
4826 free_ol_stateid_reaplist(&reaplist);
4827 move_to_close_lru(s, clp->net);
4832 * nfs4_unlock_state() called after encode
4834 __be32
4835 nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4836 struct nfsd4_close *close)
4838 __be32 status;
4839 struct nfs4_ol_stateid *stp;
4840 struct net *net = SVC_NET(rqstp);
4841 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
4843 dprintk("NFSD: nfsd4_close on file %pd\n",
4844 cstate->current_fh.fh_dentry);
4846 status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid,
4847 &close->cl_stateid,
4848 NFS4_OPEN_STID|NFS4_CLOSED_STID,
4849 &stp, nn);
4850 nfsd4_bump_seqid(cstate, status);
4851 if (status)
4852 goto out;
4853 update_stateid(&stp->st_stid.sc_stateid);
4854 memcpy(&close->cl_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
4856 nfsd4_close_open_stateid(stp);
4858 /* put reference from nfs4_preprocess_seqid_op */
4859 nfs4_put_stid(&stp->st_stid);
4860 out:
4861 return status;
4864 __be32
4865 nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
4866 struct nfsd4_delegreturn *dr)
4868 struct nfs4_delegation *dp;
4869 stateid_t *stateid = &dr->dr_stateid;
4870 struct nfs4_stid *s;
4871 __be32 status;
4872 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
4874 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
4875 return status;
4877 status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn);
4878 if (status)
4879 goto out;
4880 dp = delegstateid(s);
4881 status = check_stateid_generation(stateid, &dp->dl_stid.sc_stateid, nfsd4_has_session(cstate));
4882 if (status)
4883 goto put_stateid;
4885 destroy_delegation(dp);
4886 put_stateid:
4887 nfs4_put_stid(&dp->dl_stid);
4888 out:
4889 return status;
4893 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4895 static inline u64
4896 end_offset(u64 start, u64 len)
4898 u64 end;
4900 end = start + len;
4901 return end >= start ? end: NFS4_MAX_UINT64;
4904 /* last octet in a range */
4905 static inline u64
4906 last_byte_offset(u64 start, u64 len)
4908 u64 end;
4910 WARN_ON_ONCE(!len);
4911 end = start + len;
4912 return end > start ? end - 1: NFS4_MAX_UINT64;
4916 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4917 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4918 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4919 * locking, this prevents us from being completely protocol-compliant. The
4920 * real solution to this problem is to start using unsigned file offsets in
4921 * the VFS, but this is a very deep change!
4923 static inline void
4924 nfs4_transform_lock_offset(struct file_lock *lock)
4926 if (lock->fl_start < 0)
4927 lock->fl_start = OFFSET_MAX;
4928 if (lock->fl_end < 0)
4929 lock->fl_end = OFFSET_MAX;
4932 static void nfsd4_fl_get_owner(struct file_lock *dst, struct file_lock *src)
4934 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)src->fl_owner;
4935 dst->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lo->lo_owner));
4938 static void nfsd4_fl_put_owner(struct file_lock *fl)
4940 struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner;
4942 if (lo) {
4943 nfs4_put_stateowner(&lo->lo_owner);
4944 fl->fl_owner = NULL;
4948 static const struct lock_manager_operations nfsd_posix_mng_ops = {
4949 .lm_get_owner = nfsd4_fl_get_owner,
4950 .lm_put_owner = nfsd4_fl_put_owner,
4953 static inline void
4954 nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny)
4956 struct nfs4_lockowner *lo;
4958 if (fl->fl_lmops == &nfsd_posix_mng_ops) {
4959 lo = (struct nfs4_lockowner *) fl->fl_owner;
4960 deny->ld_owner.data = kmemdup(lo->lo_owner.so_owner.data,
4961 lo->lo_owner.so_owner.len, GFP_KERNEL);
4962 if (!deny->ld_owner.data)
4963 /* We just don't care that much */
4964 goto nevermind;
4965 deny->ld_owner.len = lo->lo_owner.so_owner.len;
4966 deny->ld_clientid = lo->lo_owner.so_client->cl_clientid;
4967 } else {
4968 nevermind:
4969 deny->ld_owner.len = 0;
4970 deny->ld_owner.data = NULL;
4971 deny->ld_clientid.cl_boot = 0;
4972 deny->ld_clientid.cl_id = 0;
4974 deny->ld_start = fl->fl_start;
4975 deny->ld_length = NFS4_MAX_UINT64;
4976 if (fl->fl_end != NFS4_MAX_UINT64)
4977 deny->ld_length = fl->fl_end - fl->fl_start + 1;
4978 deny->ld_type = NFS4_READ_LT;
4979 if (fl->fl_type != F_RDLCK)
4980 deny->ld_type = NFS4_WRITE_LT;
4983 static struct nfs4_lockowner *
4984 find_lockowner_str_locked(clientid_t *clid, struct xdr_netobj *owner,
4985 struct nfs4_client *clp)
4987 unsigned int strhashval = ownerstr_hashval(owner);
4988 struct nfs4_stateowner *so;
4990 lockdep_assert_held(&clp->cl_lock);
4992 list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval],
4993 so_strhash) {
4994 if (so->so_is_open_owner)
4995 continue;
4996 if (same_owner_str(so, owner))
4997 return lockowner(nfs4_get_stateowner(so));
4999 return NULL;
5002 static struct nfs4_lockowner *
5003 find_lockowner_str(clientid_t *clid, struct xdr_netobj *owner,
5004 struct nfs4_client *clp)
5006 struct nfs4_lockowner *lo;
5008 spin_lock(&clp->cl_lock);
5009 lo = find_lockowner_str_locked(clid, owner, clp);
5010 spin_unlock(&clp->cl_lock);
5011 return lo;
5014 static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop)
5016 unhash_lockowner_locked(lockowner(sop));
5019 static void nfs4_free_lockowner(struct nfs4_stateowner *sop)
5021 struct nfs4_lockowner *lo = lockowner(sop);
5023 kmem_cache_free(lockowner_slab, lo);
5026 static const struct nfs4_stateowner_operations lockowner_ops = {
5027 .so_unhash = nfs4_unhash_lockowner,
5028 .so_free = nfs4_free_lockowner,
5032 * Alloc a lock owner structure.
5033 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5034 * occurred.
5036 * strhashval = ownerstr_hashval
5038 static struct nfs4_lockowner *
5039 alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp,
5040 struct nfs4_ol_stateid *open_stp,
5041 struct nfsd4_lock *lock)
5043 struct nfs4_lockowner *lo, *ret;
5045 lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp);
5046 if (!lo)
5047 return NULL;
5048 INIT_LIST_HEAD(&lo->lo_owner.so_stateids);
5049 lo->lo_owner.so_is_open_owner = 0;
5050 lo->lo_owner.so_seqid = lock->lk_new_lock_seqid;
5051 lo->lo_owner.so_ops = &lockowner_ops;
5052 spin_lock(&clp->cl_lock);
5053 ret = find_lockowner_str_locked(&clp->cl_clientid,
5054 &lock->lk_new_owner, clp);
5055 if (ret == NULL) {
5056 list_add(&lo->lo_owner.so_strhash,
5057 &clp->cl_ownerstr_hashtbl[strhashval]);
5058 ret = lo;
5059 } else
5060 nfs4_free_lockowner(&lo->lo_owner);
5061 spin_unlock(&clp->cl_lock);
5062 return lo;
5065 static void
5066 init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo,
5067 struct nfs4_file *fp, struct inode *inode,
5068 struct nfs4_ol_stateid *open_stp)
5070 struct nfs4_client *clp = lo->lo_owner.so_client;
5072 lockdep_assert_held(&clp->cl_lock);
5074 atomic_inc(&stp->st_stid.sc_count);
5075 stp->st_stid.sc_type = NFS4_LOCK_STID;
5076 stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner);
5077 get_nfs4_file(fp);
5078 stp->st_stid.sc_file = fp;
5079 stp->st_stid.sc_free = nfs4_free_lock_stateid;
5080 stp->st_access_bmap = 0;
5081 stp->st_deny_bmap = open_stp->st_deny_bmap;
5082 stp->st_openstp = open_stp;
5083 list_add(&stp->st_locks, &open_stp->st_locks);
5084 list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids);
5085 spin_lock(&fp->fi_lock);
5086 list_add(&stp->st_perfile, &fp->fi_stateids);
5087 spin_unlock(&fp->fi_lock);
5090 static struct nfs4_ol_stateid *
5091 find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp)
5093 struct nfs4_ol_stateid *lst;
5094 struct nfs4_client *clp = lo->lo_owner.so_client;
5096 lockdep_assert_held(&clp->cl_lock);
5098 list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) {
5099 if (lst->st_stid.sc_file == fp) {
5100 atomic_inc(&lst->st_stid.sc_count);
5101 return lst;
5104 return NULL;
5107 static struct nfs4_ol_stateid *
5108 find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi,
5109 struct inode *inode, struct nfs4_ol_stateid *ost,
5110 bool *new)
5112 struct nfs4_stid *ns = NULL;
5113 struct nfs4_ol_stateid *lst;
5114 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5115 struct nfs4_client *clp = oo->oo_owner.so_client;
5117 spin_lock(&clp->cl_lock);
5118 lst = find_lock_stateid(lo, fi);
5119 if (lst == NULL) {
5120 spin_unlock(&clp->cl_lock);
5121 ns = nfs4_alloc_stid(clp, stateid_slab);
5122 if (ns == NULL)
5123 return NULL;
5125 spin_lock(&clp->cl_lock);
5126 lst = find_lock_stateid(lo, fi);
5127 if (likely(!lst)) {
5128 lst = openlockstateid(ns);
5129 init_lock_stateid(lst, lo, fi, inode, ost);
5130 ns = NULL;
5131 *new = true;
5134 spin_unlock(&clp->cl_lock);
5135 if (ns)
5136 nfs4_put_stid(ns);
5137 return lst;
5140 static int
5141 check_lock_length(u64 offset, u64 length)
5143 return ((length == 0) || ((length != NFS4_MAX_UINT64) &&
5144 LOFF_OVERFLOW(offset, length)));
5147 static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access)
5149 struct nfs4_file *fp = lock_stp->st_stid.sc_file;
5151 lockdep_assert_held(&fp->fi_lock);
5153 if (test_access(access, lock_stp))
5154 return;
5155 __nfs4_file_get_access(fp, access);
5156 set_access(access, lock_stp);
5159 static __be32
5160 lookup_or_create_lock_state(struct nfsd4_compound_state *cstate,
5161 struct nfs4_ol_stateid *ost,
5162 struct nfsd4_lock *lock,
5163 struct nfs4_ol_stateid **lst, bool *new)
5165 __be32 status;
5166 struct nfs4_file *fi = ost->st_stid.sc_file;
5167 struct nfs4_openowner *oo = openowner(ost->st_stateowner);
5168 struct nfs4_client *cl = oo->oo_owner.so_client;
5169 struct inode *inode = cstate->current_fh.fh_dentry->d_inode;
5170 struct nfs4_lockowner *lo;
5171 unsigned int strhashval;
5173 lo = find_lockowner_str(&cl->cl_clientid, &lock->v.new.owner, cl);
5174 if (!lo) {
5175 strhashval = ownerstr_hashval(&lock->v.new.owner);
5176 lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock);
5177 if (lo == NULL)
5178 return nfserr_jukebox;
5179 } else {
5180 /* with an existing lockowner, seqids must be the same */
5181 status = nfserr_bad_seqid;
5182 if (!cstate->minorversion &&
5183 lock->lk_new_lock_seqid != lo->lo_owner.so_seqid)
5184 goto out;
5187 *lst = find_or_create_lock_stateid(lo, fi, inode, ost, new);
5188 if (*lst == NULL) {
5189 status = nfserr_jukebox;
5190 goto out;
5192 status = nfs_ok;
5193 out:
5194 nfs4_put_stateowner(&lo->lo_owner);
5195 return status;
5199 * LOCK operation
5201 __be32
5202 nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5203 struct nfsd4_lock *lock)
5205 struct nfs4_openowner *open_sop = NULL;
5206 struct nfs4_lockowner *lock_sop = NULL;
5207 struct nfs4_ol_stateid *lock_stp = NULL;
5208 struct nfs4_ol_stateid *open_stp = NULL;
5209 struct nfs4_file *fp;
5210 struct file *filp = NULL;
5211 struct file_lock *file_lock = NULL;
5212 struct file_lock *conflock = NULL;
5213 __be32 status = 0;
5214 int lkflg;
5215 int err;
5216 bool new = false;
5217 struct net *net = SVC_NET(rqstp);
5218 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
5220 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5221 (long long) lock->lk_offset,
5222 (long long) lock->lk_length);
5224 if (check_lock_length(lock->lk_offset, lock->lk_length))
5225 return nfserr_inval;
5227 if ((status = fh_verify(rqstp, &cstate->current_fh,
5228 S_IFREG, NFSD_MAY_LOCK))) {
5229 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5230 return status;
5233 if (lock->lk_is_new) {
5234 if (nfsd4_has_session(cstate))
5235 /* See rfc 5661 18.10.3: given clientid is ignored: */
5236 memcpy(&lock->v.new.clientid,
5237 &cstate->session->se_client->cl_clientid,
5238 sizeof(clientid_t));
5240 status = nfserr_stale_clientid;
5241 if (STALE_CLIENTID(&lock->lk_new_clientid, nn))
5242 goto out;
5244 /* validate and update open stateid and open seqid */
5245 status = nfs4_preprocess_confirmed_seqid_op(cstate,
5246 lock->lk_new_open_seqid,
5247 &lock->lk_new_open_stateid,
5248 &open_stp, nn);
5249 if (status)
5250 goto out;
5251 open_sop = openowner(open_stp->st_stateowner);
5252 status = nfserr_bad_stateid;
5253 if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid,
5254 &lock->v.new.clientid))
5255 goto out;
5256 status = lookup_or_create_lock_state(cstate, open_stp, lock,
5257 &lock_stp, &new);
5258 } else {
5259 status = nfs4_preprocess_seqid_op(cstate,
5260 lock->lk_old_lock_seqid,
5261 &lock->lk_old_lock_stateid,
5262 NFS4_LOCK_STID, &lock_stp, nn);
5264 if (status)
5265 goto out;
5266 lock_sop = lockowner(lock_stp->st_stateowner);
5268 lkflg = setlkflg(lock->lk_type);
5269 status = nfs4_check_openmode(lock_stp, lkflg);
5270 if (status)
5271 goto out;
5273 status = nfserr_grace;
5274 if (locks_in_grace(net) && !lock->lk_reclaim)
5275 goto out;
5276 status = nfserr_no_grace;
5277 if (!locks_in_grace(net) && lock->lk_reclaim)
5278 goto out;
5280 file_lock = locks_alloc_lock();
5281 if (!file_lock) {
5282 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5283 status = nfserr_jukebox;
5284 goto out;
5287 fp = lock_stp->st_stid.sc_file;
5288 switch (lock->lk_type) {
5289 case NFS4_READ_LT:
5290 case NFS4_READW_LT:
5291 spin_lock(&fp->fi_lock);
5292 filp = find_readable_file_locked(fp);
5293 if (filp)
5294 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ);
5295 spin_unlock(&fp->fi_lock);
5296 file_lock->fl_type = F_RDLCK;
5297 break;
5298 case NFS4_WRITE_LT:
5299 case NFS4_WRITEW_LT:
5300 spin_lock(&fp->fi_lock);
5301 filp = find_writeable_file_locked(fp);
5302 if (filp)
5303 get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE);
5304 spin_unlock(&fp->fi_lock);
5305 file_lock->fl_type = F_WRLCK;
5306 break;
5307 default:
5308 status = nfserr_inval;
5309 goto out;
5311 if (!filp) {
5312 status = nfserr_openmode;
5313 goto out;
5316 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner));
5317 file_lock->fl_pid = current->tgid;
5318 file_lock->fl_file = filp;
5319 file_lock->fl_flags = FL_POSIX;
5320 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5321 file_lock->fl_start = lock->lk_offset;
5322 file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length);
5323 nfs4_transform_lock_offset(file_lock);
5325 conflock = locks_alloc_lock();
5326 if (!conflock) {
5327 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5328 status = nfserr_jukebox;
5329 goto out;
5332 err = vfs_lock_file(filp, F_SETLK, file_lock, conflock);
5333 switch (-err) {
5334 case 0: /* success! */
5335 update_stateid(&lock_stp->st_stid.sc_stateid);
5336 memcpy(&lock->lk_resp_stateid, &lock_stp->st_stid.sc_stateid,
5337 sizeof(stateid_t));
5338 status = 0;
5339 break;
5340 case (EAGAIN): /* conflock holds conflicting lock */
5341 status = nfserr_denied;
5342 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5343 nfs4_set_lock_denied(conflock, &lock->lk_denied);
5344 break;
5345 case (EDEADLK):
5346 status = nfserr_deadlock;
5347 break;
5348 default:
5349 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err);
5350 status = nfserrno(err);
5351 break;
5353 out:
5354 if (filp)
5355 fput(filp);
5356 if (lock_stp) {
5357 /* Bump seqid manually if the 4.0 replay owner is openowner */
5358 if (cstate->replay_owner &&
5359 cstate->replay_owner != &lock_sop->lo_owner &&
5360 seqid_mutating_err(ntohl(status)))
5361 lock_sop->lo_owner.so_seqid++;
5364 * If this is a new, never-before-used stateid, and we are
5365 * returning an error, then just go ahead and release it.
5367 if (status && new)
5368 release_lock_stateid(lock_stp);
5370 nfs4_put_stid(&lock_stp->st_stid);
5372 if (open_stp)
5373 nfs4_put_stid(&open_stp->st_stid);
5374 nfsd4_bump_seqid(cstate, status);
5375 if (file_lock)
5376 locks_free_lock(file_lock);
5377 if (conflock)
5378 locks_free_lock(conflock);
5379 return status;
5383 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5384 * so we do a temporary open here just to get an open file to pass to
5385 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5386 * inode operation.)
5388 static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock)
5390 struct file *file;
5391 __be32 err = nfsd_open(rqstp, fhp, S_IFREG, NFSD_MAY_READ, &file);
5392 if (!err) {
5393 err = nfserrno(vfs_test_lock(file, lock));
5394 nfsd_close(file);
5396 return err;
5400 * LOCKT operation
5402 __be32
5403 nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5404 struct nfsd4_lockt *lockt)
5406 struct file_lock *file_lock = NULL;
5407 struct nfs4_lockowner *lo = NULL;
5408 __be32 status;
5409 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5411 if (locks_in_grace(SVC_NET(rqstp)))
5412 return nfserr_grace;
5414 if (check_lock_length(lockt->lt_offset, lockt->lt_length))
5415 return nfserr_inval;
5417 if (!nfsd4_has_session(cstate)) {
5418 status = lookup_clientid(&lockt->lt_clientid, cstate, nn);
5419 if (status)
5420 goto out;
5423 if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0)))
5424 goto out;
5426 file_lock = locks_alloc_lock();
5427 if (!file_lock) {
5428 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5429 status = nfserr_jukebox;
5430 goto out;
5433 switch (lockt->lt_type) {
5434 case NFS4_READ_LT:
5435 case NFS4_READW_LT:
5436 file_lock->fl_type = F_RDLCK;
5437 break;
5438 case NFS4_WRITE_LT:
5439 case NFS4_WRITEW_LT:
5440 file_lock->fl_type = F_WRLCK;
5441 break;
5442 default:
5443 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5444 status = nfserr_inval;
5445 goto out;
5448 lo = find_lockowner_str(&lockt->lt_clientid, &lockt->lt_owner,
5449 cstate->clp);
5450 if (lo)
5451 file_lock->fl_owner = (fl_owner_t)lo;
5452 file_lock->fl_pid = current->tgid;
5453 file_lock->fl_flags = FL_POSIX;
5455 file_lock->fl_start = lockt->lt_offset;
5456 file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length);
5458 nfs4_transform_lock_offset(file_lock);
5460 status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock);
5461 if (status)
5462 goto out;
5464 if (file_lock->fl_type != F_UNLCK) {
5465 status = nfserr_denied;
5466 nfs4_set_lock_denied(file_lock, &lockt->lt_denied);
5468 out:
5469 if (lo)
5470 nfs4_put_stateowner(&lo->lo_owner);
5471 if (file_lock)
5472 locks_free_lock(file_lock);
5473 return status;
5476 __be32
5477 nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate,
5478 struct nfsd4_locku *locku)
5480 struct nfs4_ol_stateid *stp;
5481 struct file *filp = NULL;
5482 struct file_lock *file_lock = NULL;
5483 __be32 status;
5484 int err;
5485 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5487 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5488 (long long) locku->lu_offset,
5489 (long long) locku->lu_length);
5491 if (check_lock_length(locku->lu_offset, locku->lu_length))
5492 return nfserr_inval;
5494 status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid,
5495 &locku->lu_stateid, NFS4_LOCK_STID,
5496 &stp, nn);
5497 if (status)
5498 goto out;
5499 filp = find_any_file(stp->st_stid.sc_file);
5500 if (!filp) {
5501 status = nfserr_lock_range;
5502 goto put_stateid;
5504 file_lock = locks_alloc_lock();
5505 if (!file_lock) {
5506 dprintk("NFSD: %s: unable to allocate lock!\n", __func__);
5507 status = nfserr_jukebox;
5508 goto fput;
5511 file_lock->fl_type = F_UNLCK;
5512 file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner));
5513 file_lock->fl_pid = current->tgid;
5514 file_lock->fl_file = filp;
5515 file_lock->fl_flags = FL_POSIX;
5516 file_lock->fl_lmops = &nfsd_posix_mng_ops;
5517 file_lock->fl_start = locku->lu_offset;
5519 file_lock->fl_end = last_byte_offset(locku->lu_offset,
5520 locku->lu_length);
5521 nfs4_transform_lock_offset(file_lock);
5523 err = vfs_lock_file(filp, F_SETLK, file_lock, NULL);
5524 if (err) {
5525 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5526 goto out_nfserr;
5528 update_stateid(&stp->st_stid.sc_stateid);
5529 memcpy(&locku->lu_stateid, &stp->st_stid.sc_stateid, sizeof(stateid_t));
5530 fput:
5531 fput(filp);
5532 put_stateid:
5533 nfs4_put_stid(&stp->st_stid);
5534 out:
5535 nfsd4_bump_seqid(cstate, status);
5536 if (file_lock)
5537 locks_free_lock(file_lock);
5538 return status;
5540 out_nfserr:
5541 status = nfserrno(err);
5542 goto fput;
5546 * returns
5547 * true: locks held by lockowner
5548 * false: no locks held by lockowner
5550 static bool
5551 check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner)
5553 struct file_lock **flpp;
5554 int status = false;
5555 struct file *filp = find_any_file(fp);
5556 struct inode *inode;
5558 if (!filp) {
5559 /* Any valid lock stateid should have some sort of access */
5560 WARN_ON_ONCE(1);
5561 return status;
5564 inode = file_inode(filp);
5566 spin_lock(&inode->i_lock);
5567 for (flpp = &inode->i_flock; *flpp != NULL; flpp = &(*flpp)->fl_next) {
5568 if ((*flpp)->fl_owner == (fl_owner_t)lowner) {
5569 status = true;
5570 break;
5573 spin_unlock(&inode->i_lock);
5574 fput(filp);
5575 return status;
5578 __be32
5579 nfsd4_release_lockowner(struct svc_rqst *rqstp,
5580 struct nfsd4_compound_state *cstate,
5581 struct nfsd4_release_lockowner *rlockowner)
5583 clientid_t *clid = &rlockowner->rl_clientid;
5584 struct nfs4_stateowner *sop;
5585 struct nfs4_lockowner *lo = NULL;
5586 struct nfs4_ol_stateid *stp;
5587 struct xdr_netobj *owner = &rlockowner->rl_owner;
5588 unsigned int hashval = ownerstr_hashval(owner);
5589 __be32 status;
5590 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
5591 struct nfs4_client *clp;
5593 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5594 clid->cl_boot, clid->cl_id);
5596 status = lookup_clientid(clid, cstate, nn);
5597 if (status)
5598 return status;
5600 clp = cstate->clp;
5601 /* Find the matching lock stateowner */
5602 spin_lock(&clp->cl_lock);
5603 list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval],
5604 so_strhash) {
5606 if (sop->so_is_open_owner || !same_owner_str(sop, owner))
5607 continue;
5609 /* see if there are still any locks associated with it */
5610 lo = lockowner(sop);
5611 list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) {
5612 if (check_for_locks(stp->st_stid.sc_file, lo)) {
5613 status = nfserr_locks_held;
5614 spin_unlock(&clp->cl_lock);
5615 return status;
5619 nfs4_get_stateowner(sop);
5620 break;
5622 spin_unlock(&clp->cl_lock);
5623 if (lo)
5624 release_lockowner(lo);
5625 return status;
5628 static inline struct nfs4_client_reclaim *
5629 alloc_reclaim(void)
5631 return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL);
5634 bool
5635 nfs4_has_reclaimed_state(const char *name, struct nfsd_net *nn)
5637 struct nfs4_client_reclaim *crp;
5639 crp = nfsd4_find_reclaim_client(name, nn);
5640 return (crp && crp->cr_clp);
5644 * failure => all reset bets are off, nfserr_no_grace...
5646 struct nfs4_client_reclaim *
5647 nfs4_client_to_reclaim(const char *name, struct nfsd_net *nn)
5649 unsigned int strhashval;
5650 struct nfs4_client_reclaim *crp;
5652 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN, name);
5653 crp = alloc_reclaim();
5654 if (crp) {
5655 strhashval = clientstr_hashval(name);
5656 INIT_LIST_HEAD(&crp->cr_strhash);
5657 list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]);
5658 memcpy(crp->cr_recdir, name, HEXDIR_LEN);
5659 crp->cr_clp = NULL;
5660 nn->reclaim_str_hashtbl_size++;
5662 return crp;
5665 void
5666 nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn)
5668 list_del(&crp->cr_strhash);
5669 kfree(crp);
5670 nn->reclaim_str_hashtbl_size--;
5673 void
5674 nfs4_release_reclaim(struct nfsd_net *nn)
5676 struct nfs4_client_reclaim *crp = NULL;
5677 int i;
5679 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
5680 while (!list_empty(&nn->reclaim_str_hashtbl[i])) {
5681 crp = list_entry(nn->reclaim_str_hashtbl[i].next,
5682 struct nfs4_client_reclaim, cr_strhash);
5683 nfs4_remove_reclaim_record(crp, nn);
5686 WARN_ON_ONCE(nn->reclaim_str_hashtbl_size);
5690 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5691 struct nfs4_client_reclaim *
5692 nfsd4_find_reclaim_client(const char *recdir, struct nfsd_net *nn)
5694 unsigned int strhashval;
5695 struct nfs4_client_reclaim *crp = NULL;
5697 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir);
5699 strhashval = clientstr_hashval(recdir);
5700 list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) {
5701 if (same_name(crp->cr_recdir, recdir)) {
5702 return crp;
5705 return NULL;
5709 * Called from OPEN. Look for clientid in reclaim list.
5711 __be32
5712 nfs4_check_open_reclaim(clientid_t *clid,
5713 struct nfsd4_compound_state *cstate,
5714 struct nfsd_net *nn)
5716 __be32 status;
5718 /* find clientid in conf_id_hashtbl */
5719 status = lookup_clientid(clid, cstate, nn);
5720 if (status)
5721 return nfserr_reclaim_bad;
5723 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags))
5724 return nfserr_no_grace;
5726 if (nfsd4_client_record_check(cstate->clp))
5727 return nfserr_reclaim_bad;
5729 return nfs_ok;
5732 #ifdef CONFIG_NFSD_FAULT_INJECTION
5733 static inline void
5734 put_client(struct nfs4_client *clp)
5736 atomic_dec(&clp->cl_refcount);
5739 static struct nfs4_client *
5740 nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size)
5742 struct nfs4_client *clp;
5743 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5744 nfsd_net_id);
5746 if (!nfsd_netns_ready(nn))
5747 return NULL;
5749 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5750 if (memcmp(&clp->cl_addr, addr, addr_size) == 0)
5751 return clp;
5753 return NULL;
5757 nfsd_inject_print_clients(void)
5759 struct nfs4_client *clp;
5760 u64 count = 0;
5761 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5762 nfsd_net_id);
5763 char buf[INET6_ADDRSTRLEN];
5765 if (!nfsd_netns_ready(nn))
5766 return 0;
5768 spin_lock(&nn->client_lock);
5769 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5770 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5771 pr_info("NFS Client: %s\n", buf);
5772 ++count;
5774 spin_unlock(&nn->client_lock);
5776 return count;
5780 nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size)
5782 u64 count = 0;
5783 struct nfs4_client *clp;
5784 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5785 nfsd_net_id);
5787 if (!nfsd_netns_ready(nn))
5788 return count;
5790 spin_lock(&nn->client_lock);
5791 clp = nfsd_find_client(addr, addr_size);
5792 if (clp) {
5793 if (mark_client_expired_locked(clp) == nfs_ok)
5794 ++count;
5795 else
5796 clp = NULL;
5798 spin_unlock(&nn->client_lock);
5800 if (clp)
5801 expire_client(clp);
5803 return count;
5807 nfsd_inject_forget_clients(u64 max)
5809 u64 count = 0;
5810 struct nfs4_client *clp, *next;
5811 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5812 nfsd_net_id);
5813 LIST_HEAD(reaplist);
5815 if (!nfsd_netns_ready(nn))
5816 return count;
5818 spin_lock(&nn->client_lock);
5819 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
5820 if (mark_client_expired_locked(clp) == nfs_ok) {
5821 list_add(&clp->cl_lru, &reaplist);
5822 if (max != 0 && ++count >= max)
5823 break;
5826 spin_unlock(&nn->client_lock);
5828 list_for_each_entry_safe(clp, next, &reaplist, cl_lru)
5829 expire_client(clp);
5831 return count;
5834 static void nfsd_print_count(struct nfs4_client *clp, unsigned int count,
5835 const char *type)
5837 char buf[INET6_ADDRSTRLEN];
5838 rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf));
5839 printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type);
5842 static void
5843 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst,
5844 struct list_head *collect)
5846 struct nfs4_client *clp = lst->st_stid.sc_client;
5847 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5848 nfsd_net_id);
5850 if (!collect)
5851 return;
5853 lockdep_assert_held(&nn->client_lock);
5854 atomic_inc(&clp->cl_refcount);
5855 list_add(&lst->st_locks, collect);
5858 static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max,
5859 struct list_head *collect,
5860 void (*func)(struct nfs4_ol_stateid *))
5862 struct nfs4_openowner *oop;
5863 struct nfs4_ol_stateid *stp, *st_next;
5864 struct nfs4_ol_stateid *lst, *lst_next;
5865 u64 count = 0;
5867 spin_lock(&clp->cl_lock);
5868 list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) {
5869 list_for_each_entry_safe(stp, st_next,
5870 &oop->oo_owner.so_stateids, st_perstateowner) {
5871 list_for_each_entry_safe(lst, lst_next,
5872 &stp->st_locks, st_locks) {
5873 if (func) {
5874 func(lst);
5875 nfsd_inject_add_lock_to_list(lst,
5876 collect);
5878 ++count;
5880 * Despite the fact that these functions deal
5881 * with 64-bit integers for "count", we must
5882 * ensure that it doesn't blow up the
5883 * clp->cl_refcount. Throw a warning if we
5884 * start to approach INT_MAX here.
5886 WARN_ON_ONCE(count == (INT_MAX / 2));
5887 if (count == max)
5888 goto out;
5892 out:
5893 spin_unlock(&clp->cl_lock);
5895 return count;
5898 static u64
5899 nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect,
5900 u64 max)
5902 return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid);
5905 static u64
5906 nfsd_print_client_locks(struct nfs4_client *clp)
5908 u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL);
5909 nfsd_print_count(clp, count, "locked files");
5910 return count;
5914 nfsd_inject_print_locks(void)
5916 struct nfs4_client *clp;
5917 u64 count = 0;
5918 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5919 nfsd_net_id);
5921 if (!nfsd_netns_ready(nn))
5922 return 0;
5924 spin_lock(&nn->client_lock);
5925 list_for_each_entry(clp, &nn->client_lru, cl_lru)
5926 count += nfsd_print_client_locks(clp);
5927 spin_unlock(&nn->client_lock);
5929 return count;
5932 static void
5933 nfsd_reap_locks(struct list_head *reaplist)
5935 struct nfs4_client *clp;
5936 struct nfs4_ol_stateid *stp, *next;
5938 list_for_each_entry_safe(stp, next, reaplist, st_locks) {
5939 list_del_init(&stp->st_locks);
5940 clp = stp->st_stid.sc_client;
5941 nfs4_put_stid(&stp->st_stid);
5942 put_client(clp);
5947 nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size)
5949 unsigned int count = 0;
5950 struct nfs4_client *clp;
5951 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5952 nfsd_net_id);
5953 LIST_HEAD(reaplist);
5955 if (!nfsd_netns_ready(nn))
5956 return count;
5958 spin_lock(&nn->client_lock);
5959 clp = nfsd_find_client(addr, addr_size);
5960 if (clp)
5961 count = nfsd_collect_client_locks(clp, &reaplist, 0);
5962 spin_unlock(&nn->client_lock);
5963 nfsd_reap_locks(&reaplist);
5964 return count;
5968 nfsd_inject_forget_locks(u64 max)
5970 u64 count = 0;
5971 struct nfs4_client *clp;
5972 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5973 nfsd_net_id);
5974 LIST_HEAD(reaplist);
5976 if (!nfsd_netns_ready(nn))
5977 return count;
5979 spin_lock(&nn->client_lock);
5980 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
5981 count += nfsd_collect_client_locks(clp, &reaplist, max - count);
5982 if (max != 0 && count >= max)
5983 break;
5985 spin_unlock(&nn->client_lock);
5986 nfsd_reap_locks(&reaplist);
5987 return count;
5990 static u64
5991 nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max,
5992 struct list_head *collect,
5993 void (*func)(struct nfs4_openowner *))
5995 struct nfs4_openowner *oop, *next;
5996 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
5997 nfsd_net_id);
5998 u64 count = 0;
6000 lockdep_assert_held(&nn->client_lock);
6002 spin_lock(&clp->cl_lock);
6003 list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) {
6004 if (func) {
6005 func(oop);
6006 if (collect) {
6007 atomic_inc(&clp->cl_refcount);
6008 list_add(&oop->oo_perclient, collect);
6011 ++count;
6013 * Despite the fact that these functions deal with
6014 * 64-bit integers for "count", we must ensure that
6015 * it doesn't blow up the clp->cl_refcount. Throw a
6016 * warning if we start to approach INT_MAX here.
6018 WARN_ON_ONCE(count == (INT_MAX / 2));
6019 if (count == max)
6020 break;
6022 spin_unlock(&clp->cl_lock);
6024 return count;
6027 static u64
6028 nfsd_print_client_openowners(struct nfs4_client *clp)
6030 u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL);
6032 nfsd_print_count(clp, count, "openowners");
6033 return count;
6036 static u64
6037 nfsd_collect_client_openowners(struct nfs4_client *clp,
6038 struct list_head *collect, u64 max)
6040 return nfsd_foreach_client_openowner(clp, max, collect,
6041 unhash_openowner_locked);
6045 nfsd_inject_print_openowners(void)
6047 struct nfs4_client *clp;
6048 u64 count = 0;
6049 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6050 nfsd_net_id);
6052 if (!nfsd_netns_ready(nn))
6053 return 0;
6055 spin_lock(&nn->client_lock);
6056 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6057 count += nfsd_print_client_openowners(clp);
6058 spin_unlock(&nn->client_lock);
6060 return count;
6063 static void
6064 nfsd_reap_openowners(struct list_head *reaplist)
6066 struct nfs4_client *clp;
6067 struct nfs4_openowner *oop, *next;
6069 list_for_each_entry_safe(oop, next, reaplist, oo_perclient) {
6070 list_del_init(&oop->oo_perclient);
6071 clp = oop->oo_owner.so_client;
6072 release_openowner(oop);
6073 put_client(clp);
6078 nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr,
6079 size_t addr_size)
6081 unsigned int count = 0;
6082 struct nfs4_client *clp;
6083 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6084 nfsd_net_id);
6085 LIST_HEAD(reaplist);
6087 if (!nfsd_netns_ready(nn))
6088 return count;
6090 spin_lock(&nn->client_lock);
6091 clp = nfsd_find_client(addr, addr_size);
6092 if (clp)
6093 count = nfsd_collect_client_openowners(clp, &reaplist, 0);
6094 spin_unlock(&nn->client_lock);
6095 nfsd_reap_openowners(&reaplist);
6096 return count;
6100 nfsd_inject_forget_openowners(u64 max)
6102 u64 count = 0;
6103 struct nfs4_client *clp;
6104 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6105 nfsd_net_id);
6106 LIST_HEAD(reaplist);
6108 if (!nfsd_netns_ready(nn))
6109 return count;
6111 spin_lock(&nn->client_lock);
6112 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6113 count += nfsd_collect_client_openowners(clp, &reaplist,
6114 max - count);
6115 if (max != 0 && count >= max)
6116 break;
6118 spin_unlock(&nn->client_lock);
6119 nfsd_reap_openowners(&reaplist);
6120 return count;
6123 static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max,
6124 struct list_head *victims)
6126 struct nfs4_delegation *dp, *next;
6127 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6128 nfsd_net_id);
6129 u64 count = 0;
6131 lockdep_assert_held(&nn->client_lock);
6133 spin_lock(&state_lock);
6134 list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) {
6135 if (victims) {
6137 * It's not safe to mess with delegations that have a
6138 * non-zero dl_time. They might have already been broken
6139 * and could be processed by the laundromat outside of
6140 * the state_lock. Just leave them be.
6142 if (dp->dl_time != 0)
6143 continue;
6145 atomic_inc(&clp->cl_refcount);
6146 unhash_delegation_locked(dp);
6147 list_add(&dp->dl_recall_lru, victims);
6149 ++count;
6151 * Despite the fact that these functions deal with
6152 * 64-bit integers for "count", we must ensure that
6153 * it doesn't blow up the clp->cl_refcount. Throw a
6154 * warning if we start to approach INT_MAX here.
6156 WARN_ON_ONCE(count == (INT_MAX / 2));
6157 if (count == max)
6158 break;
6160 spin_unlock(&state_lock);
6161 return count;
6164 static u64
6165 nfsd_print_client_delegations(struct nfs4_client *clp)
6167 u64 count = nfsd_find_all_delegations(clp, 0, NULL);
6169 nfsd_print_count(clp, count, "delegations");
6170 return count;
6174 nfsd_inject_print_delegations(void)
6176 struct nfs4_client *clp;
6177 u64 count = 0;
6178 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6179 nfsd_net_id);
6181 if (!nfsd_netns_ready(nn))
6182 return 0;
6184 spin_lock(&nn->client_lock);
6185 list_for_each_entry(clp, &nn->client_lru, cl_lru)
6186 count += nfsd_print_client_delegations(clp);
6187 spin_unlock(&nn->client_lock);
6189 return count;
6192 static void
6193 nfsd_forget_delegations(struct list_head *reaplist)
6195 struct nfs4_client *clp;
6196 struct nfs4_delegation *dp, *next;
6198 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6199 list_del_init(&dp->dl_recall_lru);
6200 clp = dp->dl_stid.sc_client;
6201 revoke_delegation(dp);
6202 put_client(clp);
6207 nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr,
6208 size_t addr_size)
6210 u64 count = 0;
6211 struct nfs4_client *clp;
6212 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6213 nfsd_net_id);
6214 LIST_HEAD(reaplist);
6216 if (!nfsd_netns_ready(nn))
6217 return count;
6219 spin_lock(&nn->client_lock);
6220 clp = nfsd_find_client(addr, addr_size);
6221 if (clp)
6222 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6223 spin_unlock(&nn->client_lock);
6225 nfsd_forget_delegations(&reaplist);
6226 return count;
6230 nfsd_inject_forget_delegations(u64 max)
6232 u64 count = 0;
6233 struct nfs4_client *clp;
6234 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6235 nfsd_net_id);
6236 LIST_HEAD(reaplist);
6238 if (!nfsd_netns_ready(nn))
6239 return count;
6241 spin_lock(&nn->client_lock);
6242 list_for_each_entry(clp, &nn->client_lru, cl_lru) {
6243 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6244 if (max != 0 && count >= max)
6245 break;
6247 spin_unlock(&nn->client_lock);
6248 nfsd_forget_delegations(&reaplist);
6249 return count;
6252 static void
6253 nfsd_recall_delegations(struct list_head *reaplist)
6255 struct nfs4_client *clp;
6256 struct nfs4_delegation *dp, *next;
6258 list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) {
6259 list_del_init(&dp->dl_recall_lru);
6260 clp = dp->dl_stid.sc_client;
6262 * We skipped all entries that had a zero dl_time before,
6263 * so we can now reset the dl_time back to 0. If a delegation
6264 * break comes in now, then it won't make any difference since
6265 * we're recalling it either way.
6267 spin_lock(&state_lock);
6268 dp->dl_time = 0;
6269 spin_unlock(&state_lock);
6270 nfsd_break_one_deleg(dp);
6271 put_client(clp);
6276 nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr,
6277 size_t addr_size)
6279 u64 count = 0;
6280 struct nfs4_client *clp;
6281 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6282 nfsd_net_id);
6283 LIST_HEAD(reaplist);
6285 if (!nfsd_netns_ready(nn))
6286 return count;
6288 spin_lock(&nn->client_lock);
6289 clp = nfsd_find_client(addr, addr_size);
6290 if (clp)
6291 count = nfsd_find_all_delegations(clp, 0, &reaplist);
6292 spin_unlock(&nn->client_lock);
6294 nfsd_recall_delegations(&reaplist);
6295 return count;
6299 nfsd_inject_recall_delegations(u64 max)
6301 u64 count = 0;
6302 struct nfs4_client *clp, *next;
6303 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
6304 nfsd_net_id);
6305 LIST_HEAD(reaplist);
6307 if (!nfsd_netns_ready(nn))
6308 return count;
6310 spin_lock(&nn->client_lock);
6311 list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) {
6312 count += nfsd_find_all_delegations(clp, max - count, &reaplist);
6313 if (max != 0 && ++count >= max)
6314 break;
6316 spin_unlock(&nn->client_lock);
6317 nfsd_recall_delegations(&reaplist);
6318 return count;
6320 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6323 * Since the lifetime of a delegation isn't limited to that of an open, a
6324 * client may quite reasonably hang on to a delegation as long as it has
6325 * the inode cached. This becomes an obvious problem the first time a
6326 * client's inode cache approaches the size of the server's total memory.
6328 * For now we avoid this problem by imposing a hard limit on the number
6329 * of delegations, which varies according to the server's memory size.
6331 static void
6332 set_max_delegations(void)
6335 * Allow at most 4 delegations per megabyte of RAM. Quick
6336 * estimates suggest that in the worst case (where every delegation
6337 * is for a different inode), a delegation could take about 1.5K,
6338 * giving a worst case usage of about 6% of memory.
6340 max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT);
6343 static int nfs4_state_create_net(struct net *net)
6345 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6346 int i;
6348 nn->conf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6349 CLIENT_HASH_SIZE, GFP_KERNEL);
6350 if (!nn->conf_id_hashtbl)
6351 goto err;
6352 nn->unconf_id_hashtbl = kmalloc(sizeof(struct list_head) *
6353 CLIENT_HASH_SIZE, GFP_KERNEL);
6354 if (!nn->unconf_id_hashtbl)
6355 goto err_unconf_id;
6356 nn->sessionid_hashtbl = kmalloc(sizeof(struct list_head) *
6357 SESSION_HASH_SIZE, GFP_KERNEL);
6358 if (!nn->sessionid_hashtbl)
6359 goto err_sessionid;
6361 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6362 INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]);
6363 INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]);
6365 for (i = 0; i < SESSION_HASH_SIZE; i++)
6366 INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]);
6367 nn->conf_name_tree = RB_ROOT;
6368 nn->unconf_name_tree = RB_ROOT;
6369 INIT_LIST_HEAD(&nn->client_lru);
6370 INIT_LIST_HEAD(&nn->close_lru);
6371 INIT_LIST_HEAD(&nn->del_recall_lru);
6372 spin_lock_init(&nn->client_lock);
6374 INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main);
6375 get_net(net);
6377 return 0;
6379 err_sessionid:
6380 kfree(nn->unconf_id_hashtbl);
6381 err_unconf_id:
6382 kfree(nn->conf_id_hashtbl);
6383 err:
6384 return -ENOMEM;
6387 static void
6388 nfs4_state_destroy_net(struct net *net)
6390 int i;
6391 struct nfs4_client *clp = NULL;
6392 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6394 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6395 while (!list_empty(&nn->conf_id_hashtbl[i])) {
6396 clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6397 destroy_client(clp);
6401 for (i = 0; i < CLIENT_HASH_SIZE; i++) {
6402 while (!list_empty(&nn->unconf_id_hashtbl[i])) {
6403 clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash);
6404 destroy_client(clp);
6408 kfree(nn->sessionid_hashtbl);
6409 kfree(nn->unconf_id_hashtbl);
6410 kfree(nn->conf_id_hashtbl);
6411 put_net(net);
6415 nfs4_state_start_net(struct net *net)
6417 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6418 int ret;
6420 ret = nfs4_state_create_net(net);
6421 if (ret)
6422 return ret;
6423 nn->boot_time = get_seconds();
6424 nn->grace_ended = false;
6425 locks_start_grace(net, &nn->nfsd4_manager);
6426 nfsd4_client_tracking_init(net);
6427 printk(KERN_INFO "NFSD: starting %ld-second grace period (net %p)\n",
6428 nn->nfsd4_grace, net);
6429 queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ);
6430 return 0;
6433 /* initialization to perform when the nfsd service is started: */
6436 nfs4_state_start(void)
6438 int ret;
6440 ret = set_callback_cred();
6441 if (ret)
6442 return -ENOMEM;
6443 laundry_wq = create_singlethread_workqueue("nfsd4");
6444 if (laundry_wq == NULL) {
6445 ret = -ENOMEM;
6446 goto out_recovery;
6448 ret = nfsd4_create_callback_queue();
6449 if (ret)
6450 goto out_free_laundry;
6452 set_max_delegations();
6454 return 0;
6456 out_free_laundry:
6457 destroy_workqueue(laundry_wq);
6458 out_recovery:
6459 return ret;
6462 void
6463 nfs4_state_shutdown_net(struct net *net)
6465 struct nfs4_delegation *dp = NULL;
6466 struct list_head *pos, *next, reaplist;
6467 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
6469 cancel_delayed_work_sync(&nn->laundromat_work);
6470 locks_end_grace(&nn->nfsd4_manager);
6472 INIT_LIST_HEAD(&reaplist);
6473 spin_lock(&state_lock);
6474 list_for_each_safe(pos, next, &nn->del_recall_lru) {
6475 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6476 unhash_delegation_locked(dp);
6477 list_add(&dp->dl_recall_lru, &reaplist);
6479 spin_unlock(&state_lock);
6480 list_for_each_safe(pos, next, &reaplist) {
6481 dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru);
6482 list_del_init(&dp->dl_recall_lru);
6483 nfs4_put_deleg_lease(dp->dl_stid.sc_file);
6484 nfs4_put_stid(&dp->dl_stid);
6487 nfsd4_client_tracking_exit(net);
6488 nfs4_state_destroy_net(net);
6491 void
6492 nfs4_state_shutdown(void)
6494 destroy_workqueue(laundry_wq);
6495 nfsd4_destroy_callback_queue();
6498 static void
6499 get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6501 if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid))
6502 memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t));
6505 static void
6506 put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid)
6508 if (cstate->minorversion) {
6509 memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t));
6510 SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6514 void
6515 clear_current_stateid(struct nfsd4_compound_state *cstate)
6517 CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG);
6521 * functions to set current state id
6523 void
6524 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6526 put_stateid(cstate, &odp->od_stateid);
6529 void
6530 nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, struct nfsd4_open *open)
6532 put_stateid(cstate, &open->op_stateid);
6535 void
6536 nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6538 put_stateid(cstate, &close->cl_stateid);
6541 void
6542 nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, struct nfsd4_lock *lock)
6544 put_stateid(cstate, &lock->lk_resp_stateid);
6548 * functions to consume current state id
6551 void
6552 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, struct nfsd4_open_downgrade *odp)
6554 get_stateid(cstate, &odp->od_stateid);
6557 void
6558 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, struct nfsd4_delegreturn *drp)
6560 get_stateid(cstate, &drp->dr_stateid);
6563 void
6564 nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, struct nfsd4_free_stateid *fsp)
6566 get_stateid(cstate, &fsp->fr_stateid);
6569 void
6570 nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, struct nfsd4_setattr *setattr)
6572 get_stateid(cstate, &setattr->sa_stateid);
6575 void
6576 nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, struct nfsd4_close *close)
6578 get_stateid(cstate, &close->cl_stateid);
6581 void
6582 nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, struct nfsd4_locku *locku)
6584 get_stateid(cstate, &locku->lu_stateid);
6587 void
6588 nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, struct nfsd4_read *read)
6590 get_stateid(cstate, &read->rd_stateid);
6593 void
6594 nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, struct nfsd4_write *write)
6596 get_stateid(cstate, &write->wr_stateid);