2 * Copyright (c) 2001 The Regents of the University of Michigan.
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
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>
37 #include <linux/slab.h>
38 #include <linux/namei.h>
39 #include <linux/swap.h>
40 #include <linux/pagemap.h>
41 #include <linux/ratelimit.h>
42 #include <linux/sunrpc/svcauth_gss.h>
43 #include <linux/sunrpc/addr.h>
44 #include <linux/jhash.h>
48 #include "current_stateid.h"
52 #define NFSDDBG_FACILITY NFSDDBG_PROC
54 #define all_ones {{~0,~0},~0}
55 static const stateid_t one_stateid
= {
57 .si_opaque
= all_ones
,
59 static const stateid_t zero_stateid
= {
62 static const stateid_t currentstateid
= {
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), ¤tstateid, 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
);
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
;
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
);
131 /* must be called under the client_lock */
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
)) {
139 printk("%s: client (clientid %08x/%08x) already expired\n",
141 clp
->cl_clientid
.cl_boot
,
142 clp
->cl_clientid
.cl_id
);
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 void put_client_renew_locked(struct nfs4_client
*clp
)
155 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
157 lockdep_assert_held(&nn
->client_lock
);
159 if (!atomic_dec_and_test(&clp
->cl_refcount
))
161 if (!is_client_expired(clp
))
162 renew_client_locked(clp
);
165 static void put_client_renew(struct nfs4_client
*clp
)
167 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
169 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
171 if (!is_client_expired(clp
))
172 renew_client_locked(clp
);
173 spin_unlock(&nn
->client_lock
);
176 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
180 if (is_session_dead(ses
))
181 return nfserr_badsession
;
182 status
= get_client_locked(ses
->se_client
);
185 atomic_inc(&ses
->se_ref
);
189 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
191 struct nfs4_client
*clp
= ses
->se_client
;
192 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
194 lockdep_assert_held(&nn
->client_lock
);
196 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
198 put_client_renew_locked(clp
);
201 static void nfsd4_put_session(struct nfsd4_session
*ses
)
203 struct nfs4_client
*clp
= ses
->se_client
;
204 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
206 spin_lock(&nn
->client_lock
);
207 nfsd4_put_session_locked(ses
);
208 spin_unlock(&nn
->client_lock
);
211 static inline struct nfs4_stateowner
*
212 nfs4_get_stateowner(struct nfs4_stateowner
*sop
)
214 atomic_inc(&sop
->so_count
);
219 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
)
221 return (sop
->so_owner
.len
== owner
->len
) &&
222 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
);
225 static struct nfs4_openowner
*
226 find_openstateowner_str_locked(unsigned int hashval
, struct nfsd4_open
*open
,
227 struct nfs4_client
*clp
)
229 struct nfs4_stateowner
*so
;
231 lockdep_assert_held(&clp
->cl_lock
);
233 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[hashval
],
235 if (!so
->so_is_open_owner
)
237 if (same_owner_str(so
, &open
->op_owner
))
238 return openowner(nfs4_get_stateowner(so
));
243 static struct nfs4_openowner
*
244 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
245 struct nfs4_client
*clp
)
247 struct nfs4_openowner
*oo
;
249 spin_lock(&clp
->cl_lock
);
250 oo
= find_openstateowner_str_locked(hashval
, open
, clp
);
251 spin_unlock(&clp
->cl_lock
);
256 opaque_hashval(const void *ptr
, int nbytes
)
258 unsigned char *cptr
= (unsigned char *) ptr
;
268 static void nfsd4_free_file_rcu(struct rcu_head
*rcu
)
270 struct nfs4_file
*fp
= container_of(rcu
, struct nfs4_file
, fi_rcu
);
272 kmem_cache_free(file_slab
, fp
);
276 put_nfs4_file(struct nfs4_file
*fi
)
278 might_lock(&state_lock
);
280 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
281 hlist_del_rcu(&fi
->fi_hash
);
282 spin_unlock(&state_lock
);
283 WARN_ON_ONCE(!list_empty(&fi
->fi_delegations
));
284 call_rcu(&fi
->fi_rcu
, nfsd4_free_file_rcu
);
289 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
291 if (f
->fi_fds
[oflag
])
292 return get_file(f
->fi_fds
[oflag
]);
297 find_writeable_file_locked(struct nfs4_file
*f
)
301 lockdep_assert_held(&f
->fi_lock
);
303 ret
= __nfs4_get_fd(f
, O_WRONLY
);
305 ret
= __nfs4_get_fd(f
, O_RDWR
);
310 find_writeable_file(struct nfs4_file
*f
)
314 spin_lock(&f
->fi_lock
);
315 ret
= find_writeable_file_locked(f
);
316 spin_unlock(&f
->fi_lock
);
321 static struct file
*find_readable_file_locked(struct nfs4_file
*f
)
325 lockdep_assert_held(&f
->fi_lock
);
327 ret
= __nfs4_get_fd(f
, O_RDONLY
);
329 ret
= __nfs4_get_fd(f
, O_RDWR
);
334 find_readable_file(struct nfs4_file
*f
)
338 spin_lock(&f
->fi_lock
);
339 ret
= find_readable_file_locked(f
);
340 spin_unlock(&f
->fi_lock
);
346 find_any_file(struct nfs4_file
*f
)
350 spin_lock(&f
->fi_lock
);
351 ret
= __nfs4_get_fd(f
, O_RDWR
);
353 ret
= __nfs4_get_fd(f
, O_WRONLY
);
355 ret
= __nfs4_get_fd(f
, O_RDONLY
);
357 spin_unlock(&f
->fi_lock
);
361 static atomic_long_t num_delegations
;
362 unsigned long max_delegations
;
365 * Open owner state (share locks)
368 /* hash tables for lock and open owners */
369 #define OWNER_HASH_BITS 8
370 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
371 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
373 static unsigned int ownerstr_hashval(struct xdr_netobj
*ownername
)
377 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
378 return ret
& OWNER_HASH_MASK
;
381 /* hash table for nfs4_file */
382 #define FILE_HASH_BITS 8
383 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
385 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
387 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
390 static unsigned int file_hashval(struct knfsd_fh
*fh
)
392 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
395 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
398 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
400 lockdep_assert_held(&fp
->fi_lock
);
402 if (access
& NFS4_SHARE_ACCESS_WRITE
)
403 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
404 if (access
& NFS4_SHARE_ACCESS_READ
)
405 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
409 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
411 lockdep_assert_held(&fp
->fi_lock
);
413 /* Does this access mode make sense? */
414 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
417 /* Does it conflict with a deny mode already set? */
418 if ((access
& fp
->fi_share_deny
) != 0)
419 return nfserr_share_denied
;
421 __nfs4_file_get_access(fp
, access
);
425 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
427 /* Common case is that there is no deny mode. */
429 /* Does this deny mode make sense? */
430 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
433 if ((deny
& NFS4_SHARE_DENY_READ
) &&
434 atomic_read(&fp
->fi_access
[O_RDONLY
]))
435 return nfserr_share_denied
;
437 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
438 atomic_read(&fp
->fi_access
[O_WRONLY
]))
439 return nfserr_share_denied
;
444 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
446 might_lock(&fp
->fi_lock
);
448 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
449 struct file
*f1
= NULL
;
450 struct file
*f2
= NULL
;
452 swap(f1
, fp
->fi_fds
[oflag
]);
453 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
454 swap(f2
, fp
->fi_fds
[O_RDWR
]);
455 spin_unlock(&fp
->fi_lock
);
463 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
465 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
467 if (access
& NFS4_SHARE_ACCESS_WRITE
)
468 __nfs4_file_put_access(fp
, O_WRONLY
);
469 if (access
& NFS4_SHARE_ACCESS_READ
)
470 __nfs4_file_put_access(fp
, O_RDONLY
);
473 struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
,
474 struct kmem_cache
*slab
)
476 struct nfs4_stid
*stid
;
479 stid
= kmem_cache_zalloc(slab
, GFP_KERNEL
);
483 idr_preload(GFP_KERNEL
);
484 spin_lock(&cl
->cl_lock
);
485 new_id
= idr_alloc_cyclic(&cl
->cl_stateids
, stid
, 0, 0, GFP_NOWAIT
);
486 spin_unlock(&cl
->cl_lock
);
490 stid
->sc_client
= cl
;
491 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
492 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
493 /* Will be incremented before return to client: */
494 atomic_set(&stid
->sc_count
, 1);
497 * It shouldn't be a problem to reuse an opaque stateid value.
498 * I don't think it is for 4.1. But with 4.0 I worry that, for
499 * example, a stray write retransmission could be accepted by
500 * the server when it should have been rejected. Therefore,
501 * adopt a trick from the sctp code to attempt to maximize the
502 * amount of time until an id is reused, by ensuring they always
503 * "increase" (mod INT_MAX):
507 kmem_cache_free(slab
, stid
);
511 static struct nfs4_ol_stateid
* nfs4_alloc_open_stateid(struct nfs4_client
*clp
)
513 struct nfs4_stid
*stid
;
514 struct nfs4_ol_stateid
*stp
;
516 stid
= nfs4_alloc_stid(clp
, stateid_slab
);
520 stp
= openlockstateid(stid
);
521 stp
->st_stid
.sc_free
= nfs4_free_ol_stateid
;
525 static void nfs4_free_deleg(struct nfs4_stid
*stid
)
527 kmem_cache_free(deleg_slab
, stid
);
528 atomic_long_dec(&num_delegations
);
532 * When we recall a delegation, we should be careful not to hand it
533 * out again straight away.
534 * To ensure this we keep a pair of bloom filters ('new' and 'old')
535 * in which the filehandles of recalled delegations are "stored".
536 * If a filehandle appear in either filter, a delegation is blocked.
537 * When a delegation is recalled, the filehandle is stored in the "new"
539 * Every 30 seconds we swap the filters and clear the "new" one,
540 * unless both are empty of course.
542 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
543 * low 3 bytes as hash-table indices.
545 * 'blocked_delegations_lock', which is always taken in block_delegations(),
546 * is used to manage concurrent access. Testing does not need the lock
547 * except when swapping the two filters.
549 static DEFINE_SPINLOCK(blocked_delegations_lock
);
550 static struct bloom_pair
{
551 int entries
, old_entries
;
553 int new; /* index into 'set' */
554 DECLARE_BITMAP(set
[2], 256);
555 } blocked_delegations
;
557 static int delegation_blocked(struct knfsd_fh
*fh
)
560 struct bloom_pair
*bd
= &blocked_delegations
;
562 if (bd
->entries
== 0)
564 if (seconds_since_boot() - bd
->swap_time
> 30) {
565 spin_lock(&blocked_delegations_lock
);
566 if (seconds_since_boot() - bd
->swap_time
> 30) {
567 bd
->entries
-= bd
->old_entries
;
568 bd
->old_entries
= bd
->entries
;
569 memset(bd
->set
[bd
->new], 0,
572 bd
->swap_time
= seconds_since_boot();
574 spin_unlock(&blocked_delegations_lock
);
576 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
577 if (test_bit(hash
&255, bd
->set
[0]) &&
578 test_bit((hash
>>8)&255, bd
->set
[0]) &&
579 test_bit((hash
>>16)&255, bd
->set
[0]))
582 if (test_bit(hash
&255, bd
->set
[1]) &&
583 test_bit((hash
>>8)&255, bd
->set
[1]) &&
584 test_bit((hash
>>16)&255, bd
->set
[1]))
590 static void block_delegations(struct knfsd_fh
*fh
)
593 struct bloom_pair
*bd
= &blocked_delegations
;
595 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
597 spin_lock(&blocked_delegations_lock
);
598 __set_bit(hash
&255, bd
->set
[bd
->new]);
599 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
600 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
601 if (bd
->entries
== 0)
602 bd
->swap_time
= seconds_since_boot();
604 spin_unlock(&blocked_delegations_lock
);
607 static struct nfs4_delegation
*
608 alloc_init_deleg(struct nfs4_client
*clp
, struct svc_fh
*current_fh
)
610 struct nfs4_delegation
*dp
;
613 dprintk("NFSD alloc_init_deleg\n");
614 n
= atomic_long_inc_return(&num_delegations
);
615 if (n
< 0 || n
> max_delegations
)
617 if (delegation_blocked(¤t_fh
->fh_handle
))
619 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
));
623 dp
->dl_stid
.sc_free
= nfs4_free_deleg
;
625 * delegation seqid's are never incremented. The 4.1 special
626 * meaning of seqid 0 isn't meaningful, really, but let's avoid
627 * 0 anyway just for consistency and use 1:
629 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
630 INIT_LIST_HEAD(&dp
->dl_perfile
);
631 INIT_LIST_HEAD(&dp
->dl_perclnt
);
632 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
633 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
635 nfsd4_init_cb(&dp
->dl_recall
, dp
->dl_stid
.sc_client
,
636 &nfsd4_cb_recall_ops
, NFSPROC4_CLNT_CB_RECALL
);
639 atomic_long_dec(&num_delegations
);
644 nfs4_put_stid(struct nfs4_stid
*s
)
646 struct nfs4_file
*fp
= s
->sc_file
;
647 struct nfs4_client
*clp
= s
->sc_client
;
649 might_lock(&clp
->cl_lock
);
651 if (!atomic_dec_and_lock(&s
->sc_count
, &clp
->cl_lock
)) {
652 wake_up_all(&close_wq
);
655 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
656 spin_unlock(&clp
->cl_lock
);
662 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
664 struct file
*filp
= NULL
;
666 spin_lock(&fp
->fi_lock
);
667 if (fp
->fi_deleg_file
&& --fp
->fi_delegees
== 0)
668 swap(filp
, fp
->fi_deleg_file
);
669 spin_unlock(&fp
->fi_lock
);
672 vfs_setlease(filp
, F_UNLCK
, NULL
, (void **)&fp
);
677 void nfs4_unhash_stid(struct nfs4_stid
*s
)
683 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
685 lockdep_assert_held(&state_lock
);
686 lockdep_assert_held(&fp
->fi_lock
);
688 atomic_inc(&dp
->dl_stid
.sc_count
);
689 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
690 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
691 list_add(&dp
->dl_perclnt
, &dp
->dl_stid
.sc_client
->cl_delegations
);
695 unhash_delegation_locked(struct nfs4_delegation
*dp
)
697 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
699 lockdep_assert_held(&state_lock
);
701 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
702 /* Ensure that deleg break won't try to requeue it */
704 spin_lock(&fp
->fi_lock
);
705 list_del_init(&dp
->dl_perclnt
);
706 list_del_init(&dp
->dl_recall_lru
);
707 list_del_init(&dp
->dl_perfile
);
708 spin_unlock(&fp
->fi_lock
);
711 static void destroy_delegation(struct nfs4_delegation
*dp
)
713 spin_lock(&state_lock
);
714 unhash_delegation_locked(dp
);
715 spin_unlock(&state_lock
);
716 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
717 nfs4_put_stid(&dp
->dl_stid
);
720 static void revoke_delegation(struct nfs4_delegation
*dp
)
722 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
724 WARN_ON(!list_empty(&dp
->dl_recall_lru
));
726 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
728 if (clp
->cl_minorversion
== 0)
729 nfs4_put_stid(&dp
->dl_stid
);
731 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
732 spin_lock(&clp
->cl_lock
);
733 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
734 spin_unlock(&clp
->cl_lock
);
742 static unsigned int clientid_hashval(u32 id
)
744 return id
& CLIENT_HASH_MASK
;
747 static unsigned int clientstr_hashval(const char *name
)
749 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
753 * We store the NONE, READ, WRITE, and BOTH bits separately in the
754 * st_{access,deny}_bmap field of the stateid, in order to track not
755 * only what share bits are currently in force, but also what
756 * combinations of share bits previous opens have used. This allows us
757 * to enforce the recommendation of rfc 3530 14.2.19 that the server
758 * return an error if the client attempt to downgrade to a combination
759 * of share bits not explicable by closing some of its previous opens.
761 * XXX: This enforcement is actually incomplete, since we don't keep
762 * track of access/deny bit combinations; so, e.g., we allow:
764 * OPEN allow read, deny write
765 * OPEN allow both, deny none
766 * DOWNGRADE allow read, deny none
768 * which we should reject.
771 bmap_to_share_mode(unsigned long bmap
) {
773 unsigned int access
= 0;
775 for (i
= 1; i
< 4; i
++) {
776 if (test_bit(i
, &bmap
))
782 /* set share access for a given stateid */
784 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
786 unsigned char mask
= 1 << access
;
788 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
789 stp
->st_access_bmap
|= mask
;
792 /* clear share access for a given stateid */
794 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
796 unsigned char mask
= 1 << access
;
798 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
799 stp
->st_access_bmap
&= ~mask
;
802 /* test whether a given stateid has access */
804 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
806 unsigned char mask
= 1 << access
;
808 return (bool)(stp
->st_access_bmap
& mask
);
811 /* set share deny for a given stateid */
813 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
815 unsigned char mask
= 1 << deny
;
817 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
818 stp
->st_deny_bmap
|= mask
;
821 /* clear share deny for a given stateid */
823 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
825 unsigned char mask
= 1 << deny
;
827 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
828 stp
->st_deny_bmap
&= ~mask
;
831 /* test whether a given stateid is denying specific access */
833 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
835 unsigned char mask
= 1 << deny
;
837 return (bool)(stp
->st_deny_bmap
& mask
);
840 static int nfs4_access_to_omode(u32 access
)
842 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
843 case NFS4_SHARE_ACCESS_READ
:
845 case NFS4_SHARE_ACCESS_WRITE
:
847 case NFS4_SHARE_ACCESS_BOTH
:
855 * A stateid that had a deny mode associated with it is being released
856 * or downgraded. Recalculate the deny mode on the file.
859 recalculate_deny_mode(struct nfs4_file
*fp
)
861 struct nfs4_ol_stateid
*stp
;
863 spin_lock(&fp
->fi_lock
);
864 fp
->fi_share_deny
= 0;
865 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
866 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
867 spin_unlock(&fp
->fi_lock
);
871 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
876 for (i
= 1; i
< 4; i
++) {
877 if ((i
& deny
) != i
) {
883 /* Recalculate per-file deny mode if there was a change */
885 recalculate_deny_mode(stp
->st_stid
.sc_file
);
888 /* release all access and file references for a given stateid */
890 release_all_access(struct nfs4_ol_stateid
*stp
)
893 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
895 if (fp
&& stp
->st_deny_bmap
!= 0)
896 recalculate_deny_mode(fp
);
898 for (i
= 1; i
< 4; i
++) {
899 if (test_access(i
, stp
))
900 nfs4_file_put_access(stp
->st_stid
.sc_file
, i
);
901 clear_access(i
, stp
);
905 static void nfs4_put_stateowner(struct nfs4_stateowner
*sop
)
907 struct nfs4_client
*clp
= sop
->so_client
;
909 might_lock(&clp
->cl_lock
);
911 if (!atomic_dec_and_lock(&sop
->so_count
, &clp
->cl_lock
))
913 sop
->so_ops
->so_unhash(sop
);
914 spin_unlock(&clp
->cl_lock
);
915 kfree(sop
->so_owner
.data
);
916 sop
->so_ops
->so_free(sop
);
919 static void unhash_ol_stateid(struct nfs4_ol_stateid
*stp
)
921 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
923 lockdep_assert_held(&stp
->st_stateowner
->so_client
->cl_lock
);
925 spin_lock(&fp
->fi_lock
);
926 list_del(&stp
->st_perfile
);
927 spin_unlock(&fp
->fi_lock
);
928 list_del(&stp
->st_perstateowner
);
931 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
)
933 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
935 release_all_access(stp
);
936 if (stp
->st_stateowner
)
937 nfs4_put_stateowner(stp
->st_stateowner
);
938 kmem_cache_free(stateid_slab
, stid
);
941 static void nfs4_free_lock_stateid(struct nfs4_stid
*stid
)
943 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
944 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
947 file
= find_any_file(stp
->st_stid
.sc_file
);
949 filp_close(file
, (fl_owner_t
)lo
);
950 nfs4_free_ol_stateid(stid
);
954 * Put the persistent reference to an already unhashed generic stateid, while
955 * holding the cl_lock. If it's the last reference, then put it onto the
956 * reaplist for later destruction.
958 static void put_ol_stateid_locked(struct nfs4_ol_stateid
*stp
,
959 struct list_head
*reaplist
)
961 struct nfs4_stid
*s
= &stp
->st_stid
;
962 struct nfs4_client
*clp
= s
->sc_client
;
964 lockdep_assert_held(&clp
->cl_lock
);
966 WARN_ON_ONCE(!list_empty(&stp
->st_locks
));
968 if (!atomic_dec_and_test(&s
->sc_count
)) {
969 wake_up_all(&close_wq
);
973 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
974 list_add(&stp
->st_locks
, reaplist
);
977 static void unhash_lock_stateid(struct nfs4_ol_stateid
*stp
)
979 struct nfs4_openowner
*oo
= openowner(stp
->st_openstp
->st_stateowner
);
981 lockdep_assert_held(&oo
->oo_owner
.so_client
->cl_lock
);
983 list_del_init(&stp
->st_locks
);
984 unhash_ol_stateid(stp
);
985 nfs4_unhash_stid(&stp
->st_stid
);
988 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
990 struct nfs4_openowner
*oo
= openowner(stp
->st_openstp
->st_stateowner
);
992 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
993 unhash_lock_stateid(stp
);
994 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
995 nfs4_put_stid(&stp
->st_stid
);
998 static void unhash_lockowner_locked(struct nfs4_lockowner
*lo
)
1000 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1002 lockdep_assert_held(&clp
->cl_lock
);
1004 list_del_init(&lo
->lo_owner
.so_strhash
);
1008 * Free a list of generic stateids that were collected earlier after being
1012 free_ol_stateid_reaplist(struct list_head
*reaplist
)
1014 struct nfs4_ol_stateid
*stp
;
1015 struct nfs4_file
*fp
;
1019 while (!list_empty(reaplist
)) {
1020 stp
= list_first_entry(reaplist
, struct nfs4_ol_stateid
,
1022 list_del(&stp
->st_locks
);
1023 fp
= stp
->st_stid
.sc_file
;
1024 stp
->st_stid
.sc_free(&stp
->st_stid
);
1030 static void release_lockowner(struct nfs4_lockowner
*lo
)
1032 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1033 struct nfs4_ol_stateid
*stp
;
1034 struct list_head reaplist
;
1036 INIT_LIST_HEAD(&reaplist
);
1038 spin_lock(&clp
->cl_lock
);
1039 unhash_lockowner_locked(lo
);
1040 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
1041 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
1042 struct nfs4_ol_stateid
, st_perstateowner
);
1043 unhash_lock_stateid(stp
);
1044 put_ol_stateid_locked(stp
, &reaplist
);
1046 spin_unlock(&clp
->cl_lock
);
1047 free_ol_stateid_reaplist(&reaplist
);
1048 nfs4_put_stateowner(&lo
->lo_owner
);
1051 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
,
1052 struct list_head
*reaplist
)
1054 struct nfs4_ol_stateid
*stp
;
1056 while (!list_empty(&open_stp
->st_locks
)) {
1057 stp
= list_entry(open_stp
->st_locks
.next
,
1058 struct nfs4_ol_stateid
, st_locks
);
1059 unhash_lock_stateid(stp
);
1060 put_ol_stateid_locked(stp
, reaplist
);
1064 static void unhash_open_stateid(struct nfs4_ol_stateid
*stp
,
1065 struct list_head
*reaplist
)
1067 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1069 unhash_ol_stateid(stp
);
1070 release_open_stateid_locks(stp
, reaplist
);
1073 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
1075 LIST_HEAD(reaplist
);
1077 spin_lock(&stp
->st_stid
.sc_client
->cl_lock
);
1078 unhash_open_stateid(stp
, &reaplist
);
1079 put_ol_stateid_locked(stp
, &reaplist
);
1080 spin_unlock(&stp
->st_stid
.sc_client
->cl_lock
);
1081 free_ol_stateid_reaplist(&reaplist
);
1084 static void unhash_openowner_locked(struct nfs4_openowner
*oo
)
1086 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1088 lockdep_assert_held(&clp
->cl_lock
);
1090 list_del_init(&oo
->oo_owner
.so_strhash
);
1091 list_del_init(&oo
->oo_perclient
);
1094 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
1096 struct nfsd_net
*nn
= net_generic(oo
->oo_owner
.so_client
->net
,
1098 struct nfs4_ol_stateid
*s
;
1100 spin_lock(&nn
->client_lock
);
1101 s
= oo
->oo_last_closed_stid
;
1103 list_del_init(&oo
->oo_close_lru
);
1104 oo
->oo_last_closed_stid
= NULL
;
1106 spin_unlock(&nn
->client_lock
);
1108 nfs4_put_stid(&s
->st_stid
);
1111 static void release_openowner(struct nfs4_openowner
*oo
)
1113 struct nfs4_ol_stateid
*stp
;
1114 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1115 struct list_head reaplist
;
1117 INIT_LIST_HEAD(&reaplist
);
1119 spin_lock(&clp
->cl_lock
);
1120 unhash_openowner_locked(oo
);
1121 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
1122 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
1123 struct nfs4_ol_stateid
, st_perstateowner
);
1124 unhash_open_stateid(stp
, &reaplist
);
1125 put_ol_stateid_locked(stp
, &reaplist
);
1127 spin_unlock(&clp
->cl_lock
);
1128 free_ol_stateid_reaplist(&reaplist
);
1129 release_last_closed_stateid(oo
);
1130 nfs4_put_stateowner(&oo
->oo_owner
);
1134 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1136 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1138 return sid
->sequence
% SESSION_HASH_SIZE
;
1143 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1145 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1146 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1150 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1156 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1157 * won't be used for replay.
1159 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1161 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1163 if (nfserr
== nfserr_replay_me
)
1166 if (!seqid_mutating_err(ntohl(nfserr
))) {
1167 nfsd4_cstate_clear_replay(cstate
);
1172 if (so
->so_is_open_owner
)
1173 release_last_closed_stateid(openowner(so
));
1179 gen_sessionid(struct nfsd4_session
*ses
)
1181 struct nfs4_client
*clp
= ses
->se_client
;
1182 struct nfsd4_sessionid
*sid
;
1184 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1185 sid
->clientid
= clp
->cl_clientid
;
1186 sid
->sequence
= current_sessionid
++;
1191 * The protocol defines ca_maxresponssize_cached to include the size of
1192 * the rpc header, but all we need to cache is the data starting after
1193 * the end of the initial SEQUENCE operation--the rest we regenerate
1194 * each time. Therefore we can advertise a ca_maxresponssize_cached
1195 * value that is the number of bytes in our cache plus a few additional
1196 * bytes. In order to stay on the safe side, and not promise more than
1197 * we can cache, those additional bytes must be the minimum possible: 24
1198 * bytes of rpc header (xid through accept state, with AUTH_NULL
1199 * verifier), 12 for the compound header (with zero-length tag), and 44
1200 * for the SEQUENCE op response:
1202 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1205 free_session_slots(struct nfsd4_session
*ses
)
1209 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1210 kfree(ses
->se_slots
[i
]);
1214 * We don't actually need to cache the rpc and session headers, so we
1215 * can allocate a little less for each slot:
1217 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1221 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1224 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1225 return size
+ sizeof(struct nfsd4_slot
);
1229 * XXX: If we run out of reserved DRC memory we could (up to a point)
1230 * re-negotiate active sessions and reduce their slot usage to make
1231 * room for new connections. For now we just fail the create session.
1233 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1235 u32 slotsize
= slot_bytes(ca
);
1236 u32 num
= ca
->maxreqs
;
1239 spin_lock(&nfsd_drc_lock
);
1240 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1241 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1242 num
= min_t(int, num
, avail
/ slotsize
);
1243 nfsd_drc_mem_used
+= num
* slotsize
;
1244 spin_unlock(&nfsd_drc_lock
);
1249 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1251 int slotsize
= slot_bytes(ca
);
1253 spin_lock(&nfsd_drc_lock
);
1254 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1255 spin_unlock(&nfsd_drc_lock
);
1258 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1259 struct nfsd4_channel_attrs
*battrs
)
1261 int numslots
= fattrs
->maxreqs
;
1262 int slotsize
= slot_bytes(fattrs
);
1263 struct nfsd4_session
*new;
1266 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1267 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1268 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1270 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1273 /* allocate each struct nfsd4_slot and data cache in one piece */
1274 for (i
= 0; i
< numslots
; i
++) {
1275 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1276 if (!new->se_slots
[i
])
1280 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1281 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1286 kfree(new->se_slots
[i
]);
1291 static void free_conn(struct nfsd4_conn
*c
)
1293 svc_xprt_put(c
->cn_xprt
);
1297 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1299 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1300 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1302 spin_lock(&clp
->cl_lock
);
1303 if (!list_empty(&c
->cn_persession
)) {
1304 list_del(&c
->cn_persession
);
1307 nfsd4_probe_callback(clp
);
1308 spin_unlock(&clp
->cl_lock
);
1311 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1313 struct nfsd4_conn
*conn
;
1315 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1318 svc_xprt_get(rqstp
->rq_xprt
);
1319 conn
->cn_xprt
= rqstp
->rq_xprt
;
1320 conn
->cn_flags
= flags
;
1321 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1325 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1327 conn
->cn_session
= ses
;
1328 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1331 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1333 struct nfs4_client
*clp
= ses
->se_client
;
1335 spin_lock(&clp
->cl_lock
);
1336 __nfsd4_hash_conn(conn
, ses
);
1337 spin_unlock(&clp
->cl_lock
);
1340 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1342 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1343 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1346 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1350 nfsd4_hash_conn(conn
, ses
);
1351 ret
= nfsd4_register_conn(conn
);
1353 /* oops; xprt is already down: */
1354 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1355 /* We may have gained or lost a callback channel: */
1356 nfsd4_probe_callback_sync(ses
->se_client
);
1359 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1361 u32 dir
= NFS4_CDFC4_FORE
;
1363 if (cses
->flags
& SESSION4_BACK_CHAN
)
1364 dir
|= NFS4_CDFC4_BACK
;
1365 return alloc_conn(rqstp
, dir
);
1368 /* must be called under client_lock */
1369 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1371 struct nfs4_client
*clp
= s
->se_client
;
1372 struct nfsd4_conn
*c
;
1374 spin_lock(&clp
->cl_lock
);
1375 while (!list_empty(&s
->se_conns
)) {
1376 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1377 list_del_init(&c
->cn_persession
);
1378 spin_unlock(&clp
->cl_lock
);
1380 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1383 spin_lock(&clp
->cl_lock
);
1385 spin_unlock(&clp
->cl_lock
);
1388 static void __free_session(struct nfsd4_session
*ses
)
1390 free_session_slots(ses
);
1394 static void free_session(struct nfsd4_session
*ses
)
1396 nfsd4_del_conns(ses
);
1397 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1398 __free_session(ses
);
1401 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1404 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1406 new->se_client
= clp
;
1409 INIT_LIST_HEAD(&new->se_conns
);
1411 new->se_cb_seq_nr
= 1;
1412 new->se_flags
= cses
->flags
;
1413 new->se_cb_prog
= cses
->callback_prog
;
1414 new->se_cb_sec
= cses
->cb_sec
;
1415 atomic_set(&new->se_ref
, 0);
1416 idx
= hash_sessionid(&new->se_sessionid
);
1417 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1418 spin_lock(&clp
->cl_lock
);
1419 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1420 spin_unlock(&clp
->cl_lock
);
1423 struct sockaddr
*sa
= svc_addr(rqstp
);
1425 * This is a little silly; with sessions there's no real
1426 * use for the callback address. Use the peer address
1427 * as a reasonable default for now, but consider fixing
1428 * the rpc client not to require an address in the
1431 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1432 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1436 /* caller must hold client_lock */
1437 static struct nfsd4_session
*
1438 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1440 struct nfsd4_session
*elem
;
1442 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1444 lockdep_assert_held(&nn
->client_lock
);
1446 dump_sessionid(__func__
, sessionid
);
1447 idx
= hash_sessionid(sessionid
);
1448 /* Search in the appropriate list */
1449 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1450 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1451 NFS4_MAX_SESSIONID_LEN
)) {
1456 dprintk("%s: session not found\n", __func__
);
1460 static struct nfsd4_session
*
1461 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1464 struct nfsd4_session
*session
;
1465 __be32 status
= nfserr_badsession
;
1467 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1470 status
= nfsd4_get_session_locked(session
);
1478 /* caller must hold client_lock */
1480 unhash_session(struct nfsd4_session
*ses
)
1482 struct nfs4_client
*clp
= ses
->se_client
;
1483 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1485 lockdep_assert_held(&nn
->client_lock
);
1487 list_del(&ses
->se_hash
);
1488 spin_lock(&ses
->se_client
->cl_lock
);
1489 list_del(&ses
->se_perclnt
);
1490 spin_unlock(&ses
->se_client
->cl_lock
);
1493 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1495 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1498 * We're assuming the clid was not given out from a boot
1499 * precisely 2^32 (about 136 years) before this one. That seems
1500 * a safe assumption:
1502 if (clid
->cl_boot
== (u32
)nn
->boot_time
)
1504 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1505 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1510 * XXX Should we use a slab cache ?
1511 * This type of memory management is somewhat inefficient, but we use it
1512 * anyway since SETCLIENTID is not a common operation.
1514 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1516 struct nfs4_client
*clp
;
1519 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1522 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1523 if (clp
->cl_name
.data
== NULL
)
1525 clp
->cl_ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
1526 OWNER_HASH_SIZE
, GFP_KERNEL
);
1527 if (!clp
->cl_ownerstr_hashtbl
)
1528 goto err_no_hashtbl
;
1529 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
1530 INIT_LIST_HEAD(&clp
->cl_ownerstr_hashtbl
[i
]);
1531 clp
->cl_name
.len
= name
.len
;
1532 INIT_LIST_HEAD(&clp
->cl_sessions
);
1533 idr_init(&clp
->cl_stateids
);
1534 atomic_set(&clp
->cl_refcount
, 0);
1535 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1536 INIT_LIST_HEAD(&clp
->cl_idhash
);
1537 INIT_LIST_HEAD(&clp
->cl_openowners
);
1538 INIT_LIST_HEAD(&clp
->cl_delegations
);
1539 INIT_LIST_HEAD(&clp
->cl_lru
);
1540 INIT_LIST_HEAD(&clp
->cl_callbacks
);
1541 INIT_LIST_HEAD(&clp
->cl_revoked
);
1542 spin_lock_init(&clp
->cl_lock
);
1543 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1546 kfree(clp
->cl_name
.data
);
1553 free_client(struct nfs4_client
*clp
)
1555 while (!list_empty(&clp
->cl_sessions
)) {
1556 struct nfsd4_session
*ses
;
1557 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1559 list_del(&ses
->se_perclnt
);
1560 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1563 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1564 free_svc_cred(&clp
->cl_cred
);
1565 kfree(clp
->cl_ownerstr_hashtbl
);
1566 kfree(clp
->cl_name
.data
);
1567 idr_destroy(&clp
->cl_stateids
);
1571 /* must be called under the client_lock */
1573 unhash_client_locked(struct nfs4_client
*clp
)
1575 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1576 struct nfsd4_session
*ses
;
1578 lockdep_assert_held(&nn
->client_lock
);
1580 /* Mark the client as expired! */
1582 /* Make it invisible */
1583 if (!list_empty(&clp
->cl_idhash
)) {
1584 list_del_init(&clp
->cl_idhash
);
1585 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1586 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1588 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1590 list_del_init(&clp
->cl_lru
);
1591 spin_lock(&clp
->cl_lock
);
1592 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1593 list_del_init(&ses
->se_hash
);
1594 spin_unlock(&clp
->cl_lock
);
1598 unhash_client(struct nfs4_client
*clp
)
1600 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1602 spin_lock(&nn
->client_lock
);
1603 unhash_client_locked(clp
);
1604 spin_unlock(&nn
->client_lock
);
1607 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
1609 if (atomic_read(&clp
->cl_refcount
))
1610 return nfserr_jukebox
;
1611 unhash_client_locked(clp
);
1616 __destroy_client(struct nfs4_client
*clp
)
1618 struct nfs4_openowner
*oo
;
1619 struct nfs4_delegation
*dp
;
1620 struct list_head reaplist
;
1622 INIT_LIST_HEAD(&reaplist
);
1623 spin_lock(&state_lock
);
1624 while (!list_empty(&clp
->cl_delegations
)) {
1625 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1626 unhash_delegation_locked(dp
);
1627 list_add(&dp
->dl_recall_lru
, &reaplist
);
1629 spin_unlock(&state_lock
);
1630 while (!list_empty(&reaplist
)) {
1631 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1632 list_del_init(&dp
->dl_recall_lru
);
1633 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
1634 nfs4_put_stid(&dp
->dl_stid
);
1636 while (!list_empty(&clp
->cl_revoked
)) {
1637 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1638 list_del_init(&dp
->dl_recall_lru
);
1639 nfs4_put_stid(&dp
->dl_stid
);
1641 while (!list_empty(&clp
->cl_openowners
)) {
1642 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1643 nfs4_get_stateowner(&oo
->oo_owner
);
1644 release_openowner(oo
);
1646 nfsd4_shutdown_callback(clp
);
1647 if (clp
->cl_cb_conn
.cb_xprt
)
1648 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1653 destroy_client(struct nfs4_client
*clp
)
1656 __destroy_client(clp
);
1659 static void expire_client(struct nfs4_client
*clp
)
1662 nfsd4_client_record_remove(clp
);
1663 __destroy_client(clp
);
1666 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1668 memcpy(target
->cl_verifier
.data
, source
->data
,
1669 sizeof(target
->cl_verifier
.data
));
1672 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1674 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1675 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1678 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1680 if (source
->cr_principal
) {
1681 target
->cr_principal
=
1682 kstrdup(source
->cr_principal
, GFP_KERNEL
);
1683 if (target
->cr_principal
== NULL
)
1686 target
->cr_principal
= NULL
;
1687 target
->cr_flavor
= source
->cr_flavor
;
1688 target
->cr_uid
= source
->cr_uid
;
1689 target
->cr_gid
= source
->cr_gid
;
1690 target
->cr_group_info
= source
->cr_group_info
;
1691 get_group_info(target
->cr_group_info
);
1692 target
->cr_gss_mech
= source
->cr_gss_mech
;
1693 if (source
->cr_gss_mech
)
1694 gss_mech_get(source
->cr_gss_mech
);
1699 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1701 if (o1
->len
< o2
->len
)
1703 if (o1
->len
> o2
->len
)
1705 return memcmp(o1
->data
, o2
->data
, o1
->len
);
1708 static int same_name(const char *n1
, const char *n2
)
1710 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1714 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1716 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
1720 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
1722 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
1725 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
1729 if (g1
->ngroups
!= g2
->ngroups
)
1731 for (i
=0; i
<g1
->ngroups
; i
++)
1732 if (!gid_eq(GROUP_AT(g1
, i
), GROUP_AT(g2
, i
)))
1738 * RFC 3530 language requires clid_inuse be returned when the
1739 * "principal" associated with a requests differs from that previously
1740 * used. We use uid, gid's, and gss principal string as our best
1741 * approximation. We also don't want to allow non-gss use of a client
1742 * established using gss: in theory cr_principal should catch that
1743 * change, but in practice cr_principal can be null even in the gss case
1744 * since gssd doesn't always pass down a principal string.
1746 static bool is_gss_cred(struct svc_cred
*cr
)
1748 /* Is cr_flavor one of the gss "pseudoflavors"?: */
1749 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
1754 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
1756 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
1757 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
1758 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
1759 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
1761 if (cr1
->cr_principal
== cr2
->cr_principal
)
1763 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
1765 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
1768 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
1770 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1773 if (!cr
->cr_gss_mech
)
1775 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
1776 return service
== RPC_GSS_SVC_INTEGRITY
||
1777 service
== RPC_GSS_SVC_PRIVACY
;
1780 static bool mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
1782 struct svc_cred
*cr
= &rqstp
->rq_cred
;
1784 if (!cl
->cl_mach_cred
)
1786 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
1788 if (!svc_rqst_integrity_protected(rqstp
))
1790 if (!cr
->cr_principal
)
1792 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
1795 static void gen_confirm(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1800 * This is opaque to client, so no need to byte-swap. Use
1801 * __force to keep sparse happy
1803 verf
[0] = (__force __be32
)get_seconds();
1804 verf
[1] = (__force __be32
)nn
->clientid_counter
;
1805 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
1808 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
1810 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
1811 clp
->cl_clientid
.cl_id
= nn
->clientid_counter
++;
1812 gen_confirm(clp
, nn
);
1815 static struct nfs4_stid
*
1816 find_stateid_locked(struct nfs4_client
*cl
, stateid_t
*t
)
1818 struct nfs4_stid
*ret
;
1820 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
1821 if (!ret
|| !ret
->sc_type
)
1826 static struct nfs4_stid
*
1827 find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
1829 struct nfs4_stid
*s
;
1831 spin_lock(&cl
->cl_lock
);
1832 s
= find_stateid_locked(cl
, t
);
1834 if (typemask
& s
->sc_type
)
1835 atomic_inc(&s
->sc_count
);
1839 spin_unlock(&cl
->cl_lock
);
1843 static struct nfs4_client
*create_client(struct xdr_netobj name
,
1844 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
1846 struct nfs4_client
*clp
;
1847 struct sockaddr
*sa
= svc_addr(rqstp
);
1849 struct net
*net
= SVC_NET(rqstp
);
1851 clp
= alloc_client(name
);
1855 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
1860 nfsd4_init_cb(&clp
->cl_cb_null
, clp
, NULL
, NFSPROC4_CLNT_CB_NULL
);
1861 clp
->cl_time
= get_seconds();
1862 clear_bit(0, &clp
->cl_cb_slot_busy
);
1863 copy_verf(clp
, verf
);
1864 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
1865 clp
->cl_cb_session
= NULL
;
1871 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
1873 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
1874 struct nfs4_client
*clp
;
1877 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
1880 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
1881 new = &((*new)->rb_left
);
1883 new = &((*new)->rb_right
);
1886 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
1887 rb_insert_color(&new_clp
->cl_namenode
, root
);
1890 static struct nfs4_client
*
1891 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
1894 struct rb_node
*node
= root
->rb_node
;
1895 struct nfs4_client
*clp
;
1898 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
1899 cmp
= compare_blob(&clp
->cl_name
, name
);
1901 node
= node
->rb_left
;
1903 node
= node
->rb_right
;
1911 add_to_unconfirmed(struct nfs4_client
*clp
)
1913 unsigned int idhashval
;
1914 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1916 lockdep_assert_held(&nn
->client_lock
);
1918 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1919 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
1920 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1921 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
1922 renew_client_locked(clp
);
1926 move_to_confirmed(struct nfs4_client
*clp
)
1928 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
1929 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1931 lockdep_assert_held(&nn
->client_lock
);
1933 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
1934 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
1935 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1936 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
1937 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
1938 renew_client_locked(clp
);
1941 static struct nfs4_client
*
1942 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
1944 struct nfs4_client
*clp
;
1945 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
1947 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
1948 if (same_clid(&clp
->cl_clientid
, clid
)) {
1949 if ((bool)clp
->cl_minorversion
!= sessions
)
1951 renew_client_locked(clp
);
1958 static struct nfs4_client
*
1959 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1961 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
1963 lockdep_assert_held(&nn
->client_lock
);
1964 return find_client_in_id_table(tbl
, clid
, sessions
);
1967 static struct nfs4_client
*
1968 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
1970 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
1972 lockdep_assert_held(&nn
->client_lock
);
1973 return find_client_in_id_table(tbl
, clid
, sessions
);
1976 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
1978 return clp
->cl_exchange_flags
!= 0;
1981 static struct nfs4_client
*
1982 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1984 lockdep_assert_held(&nn
->client_lock
);
1985 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
1988 static struct nfs4_client
*
1989 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
1991 lockdep_assert_held(&nn
->client_lock
);
1992 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
1996 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
1998 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
1999 struct sockaddr
*sa
= svc_addr(rqstp
);
2000 u32 scopeid
= rpc_get_scope_id(sa
);
2001 unsigned short expected_family
;
2003 /* Currently, we only support tcp and tcp6 for the callback channel */
2004 if (se
->se_callback_netid_len
== 3 &&
2005 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
2006 expected_family
= AF_INET
;
2007 else if (se
->se_callback_netid_len
== 4 &&
2008 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
2009 expected_family
= AF_INET6
;
2013 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
2014 se
->se_callback_addr_len
,
2015 (struct sockaddr
*)&conn
->cb_addr
,
2016 sizeof(conn
->cb_addr
));
2018 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
2021 if (conn
->cb_addr
.ss_family
== AF_INET6
)
2022 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
2024 conn
->cb_prog
= se
->se_callback_prog
;
2025 conn
->cb_ident
= se
->se_callback_ident
;
2026 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
2029 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
2030 conn
->cb_addrlen
= 0;
2031 dprintk(KERN_INFO
"NFSD: this client (clientid %08x/%08x) "
2032 "will not receive delegations\n",
2033 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
2039 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2042 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
2044 struct xdr_buf
*buf
= resp
->xdr
.buf
;
2045 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2048 dprintk("--> %s slot %p\n", __func__
, slot
);
2050 slot
->sl_opcnt
= resp
->opcnt
;
2051 slot
->sl_status
= resp
->cstate
.status
;
2053 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
2054 if (nfsd4_not_cached(resp
)) {
2055 slot
->sl_datalen
= 0;
2058 base
= resp
->cstate
.data_offset
;
2059 slot
->sl_datalen
= buf
->len
- base
;
2060 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
2061 WARN("%s: sessions DRC could not cache compound\n", __func__
);
2066 * Encode the replay sequence operation from the slot values.
2067 * If cachethis is FALSE encode the uncached rep error on the next
2068 * operation which sets resp->p and increments resp->opcnt for
2069 * nfs4svc_encode_compoundres.
2073 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
2074 struct nfsd4_compoundres
*resp
)
2076 struct nfsd4_op
*op
;
2077 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2079 /* Encode the replayed sequence operation */
2080 op
= &args
->ops
[resp
->opcnt
- 1];
2081 nfsd4_encode_operation(resp
, op
);
2083 /* Return nfserr_retry_uncached_rep in next operation. */
2084 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
2085 op
= &args
->ops
[resp
->opcnt
++];
2086 op
->status
= nfserr_retry_uncached_rep
;
2087 nfsd4_encode_operation(resp
, op
);
2093 * The sequence operation is not cached because we can use the slot and
2097 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
2098 struct nfsd4_sequence
*seq
)
2100 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2101 struct xdr_stream
*xdr
= &resp
->xdr
;
2105 dprintk("--> %s slot %p\n", __func__
, slot
);
2107 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
2111 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
2114 return nfserr_serverfault
;
2116 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
2117 xdr_commit_encode(xdr
);
2119 resp
->opcnt
= slot
->sl_opcnt
;
2120 return slot
->sl_status
;
2124 * Set the exchange_id flags returned by the server.
2127 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
2129 /* pNFS is not supported */
2130 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
2132 /* Referrals are supported, Migration is not. */
2133 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
2135 /* set the wire flags to return to client. */
2136 clid
->flags
= new->cl_exchange_flags
;
2139 static bool client_has_state(struct nfs4_client
*clp
)
2142 * Note clp->cl_openowners check isn't quite right: there's no
2143 * need to count owners without stateid's.
2145 * Also note we should probably be using this in 4.0 case too.
2147 return !list_empty(&clp
->cl_openowners
)
2148 || !list_empty(&clp
->cl_delegations
)
2149 || !list_empty(&clp
->cl_sessions
);
2153 nfsd4_exchange_id(struct svc_rqst
*rqstp
,
2154 struct nfsd4_compound_state
*cstate
,
2155 struct nfsd4_exchange_id
*exid
)
2157 struct nfs4_client
*conf
, *new;
2158 struct nfs4_client
*unconf
= NULL
;
2160 char addr_str
[INET6_ADDRSTRLEN
];
2161 nfs4_verifier verf
= exid
->verifier
;
2162 struct sockaddr
*sa
= svc_addr(rqstp
);
2163 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
2164 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2166 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2167 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2168 "ip_addr=%s flags %x, spa_how %d\n",
2169 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2170 addr_str
, exid
->flags
, exid
->spa_how
);
2172 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2173 return nfserr_inval
;
2175 switch (exid
->spa_how
) {
2177 if (!svc_rqst_integrity_protected(rqstp
))
2178 return nfserr_inval
;
2181 default: /* checked by xdr code */
2184 return nfserr_encr_alg_unsupp
;
2187 new = create_client(exid
->clname
, rqstp
, &verf
);
2189 return nfserr_jukebox
;
2191 /* Cases below refer to rfc 5661 section 18.35.4: */
2192 spin_lock(&nn
->client_lock
);
2193 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2195 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2196 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2199 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2200 status
= nfserr_inval
;
2203 if (!mach_creds_match(conf
, rqstp
)) {
2204 status
= nfserr_wrong_cred
;
2207 if (!creds_match
) { /* case 9 */
2208 status
= nfserr_perm
;
2211 if (!verfs_match
) { /* case 8 */
2212 status
= nfserr_not_same
;
2216 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2219 if (!creds_match
) { /* case 3 */
2220 if (client_has_state(conf
)) {
2221 status
= nfserr_clid_inuse
;
2226 if (verfs_match
) { /* case 2 */
2227 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2230 /* case 5, client reboot */
2235 if (update
) { /* case 7 */
2236 status
= nfserr_noent
;
2240 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2241 if (unconf
) /* case 4, possible retry or client restart */
2242 unhash_client_locked(unconf
);
2244 /* case 1 (normal case) */
2247 status
= mark_client_expired_locked(conf
);
2251 new->cl_minorversion
= cstate
->minorversion
;
2252 new->cl_mach_cred
= (exid
->spa_how
== SP4_MACH_CRED
);
2255 add_to_unconfirmed(new);
2258 exid
->clientid
.cl_boot
= conf
->cl_clientid
.cl_boot
;
2259 exid
->clientid
.cl_id
= conf
->cl_clientid
.cl_id
;
2261 exid
->seqid
= conf
->cl_cs_slot
.sl_seqid
+ 1;
2262 nfsd4_set_ex_flags(conf
, exid
);
2264 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2265 conf
->cl_cs_slot
.sl_seqid
, conf
->cl_exchange_flags
);
2269 spin_unlock(&nn
->client_lock
);
2273 expire_client(unconf
);
2278 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2280 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2283 /* The slot is in use, and no response has been sent. */
2285 if (seqid
== slot_seqid
)
2286 return nfserr_jukebox
;
2288 return nfserr_seq_misordered
;
2290 /* Note unsigned 32-bit arithmetic handles wraparound: */
2291 if (likely(seqid
== slot_seqid
+ 1))
2293 if (seqid
== slot_seqid
)
2294 return nfserr_replay_cache
;
2295 return nfserr_seq_misordered
;
2299 * Cache the create session result into the create session single DRC
2300 * slot cache by saving the xdr structure. sl_seqid has been set.
2301 * Do this for solo or embedded create session operations.
2304 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2305 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2307 slot
->sl_status
= nfserr
;
2308 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2312 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2313 struct nfsd4_clid_slot
*slot
)
2315 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2316 return slot
->sl_status
;
2319 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2320 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2321 1 + /* MIN tag is length with zero, only length */ \
2322 3 + /* version, opcount, opcode */ \
2323 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2324 /* seqid, slotID, slotID, cache */ \
2325 4 ) * sizeof(__be32))
2327 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2328 2 + /* verifier: AUTH_NULL, length 0 */\
2330 1 + /* MIN tag is length with zero, only length */ \
2331 3 + /* opcount, opcode, opstatus*/ \
2332 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2333 /* seqid, slotID, slotID, slotID, status */ \
2334 5 ) * sizeof(__be32))
2336 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2338 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2340 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2341 return nfserr_toosmall
;
2342 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2343 return nfserr_toosmall
;
2344 ca
->headerpadsz
= 0;
2345 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2346 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2347 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2348 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2349 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2350 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2352 * Note decreasing slot size below client's request may make it
2353 * difficult for client to function correctly, whereas
2354 * decreasing the number of slots will (just?) affect
2355 * performance. When short on memory we therefore prefer to
2356 * decrease number of slots instead of their size. Clients that
2357 * request larger slots than they need will get poor results:
2359 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2361 return nfserr_jukebox
;
2366 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2367 RPC_MAX_HEADER_WITH_AUTH) * sizeof(__be32))
2368 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2369 RPC_MAX_REPHEADER_WITH_AUTH) * sizeof(__be32))
2371 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2373 ca
->headerpadsz
= 0;
2376 * These RPC_MAX_HEADER macros are overkill, especially since we
2377 * don't even do gss on the backchannel yet. But this is still
2378 * less than 1k. Tighten up this estimate in the unlikely event
2379 * it turns out to be a problem for some client:
2381 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2382 return nfserr_toosmall
;
2383 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2384 return nfserr_toosmall
;
2385 ca
->maxresp_cached
= 0;
2387 return nfserr_toosmall
;
2392 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2394 switch (cbs
->flavor
) {
2400 * GSS case: the spec doesn't allow us to return this
2401 * error. But it also doesn't allow us not to support
2403 * I'd rather this fail hard than return some error the
2404 * client might think it can already handle:
2406 return nfserr_encr_alg_unsupp
;
2411 nfsd4_create_session(struct svc_rqst
*rqstp
,
2412 struct nfsd4_compound_state
*cstate
,
2413 struct nfsd4_create_session
*cr_ses
)
2415 struct sockaddr
*sa
= svc_addr(rqstp
);
2416 struct nfs4_client
*conf
, *unconf
;
2417 struct nfs4_client
*old
= NULL
;
2418 struct nfsd4_session
*new;
2419 struct nfsd4_conn
*conn
;
2420 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2422 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2424 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2425 return nfserr_inval
;
2426 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2429 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2432 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2434 goto out_release_drc_mem
;
2435 status
= nfserr_jukebox
;
2436 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2438 goto out_release_drc_mem
;
2439 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2441 goto out_free_session
;
2443 spin_lock(&nn
->client_lock
);
2444 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2445 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2446 WARN_ON_ONCE(conf
&& unconf
);
2449 status
= nfserr_wrong_cred
;
2450 if (!mach_creds_match(conf
, rqstp
))
2452 cs_slot
= &conf
->cl_cs_slot
;
2453 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2454 if (status
== nfserr_replay_cache
) {
2455 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2457 } else if (cr_ses
->seqid
!= cs_slot
->sl_seqid
+ 1) {
2458 status
= nfserr_seq_misordered
;
2461 } else if (unconf
) {
2462 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2463 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2464 status
= nfserr_clid_inuse
;
2467 status
= nfserr_wrong_cred
;
2468 if (!mach_creds_match(unconf
, rqstp
))
2470 cs_slot
= &unconf
->cl_cs_slot
;
2471 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2473 /* an unconfirmed replay returns misordered */
2474 status
= nfserr_seq_misordered
;
2477 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2479 status
= mark_client_expired_locked(old
);
2485 move_to_confirmed(unconf
);
2488 status
= nfserr_stale_clientid
;
2493 * We do not support RDMA or persistent sessions
2495 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2496 cr_ses
->flags
&= ~SESSION4_RDMA
;
2498 init_session(rqstp
, new, conf
, cr_ses
);
2499 nfsd4_get_session_locked(new);
2501 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2502 NFS4_MAX_SESSIONID_LEN
);
2503 cs_slot
->sl_seqid
++;
2504 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2506 /* cache solo and embedded create sessions under the client_lock */
2507 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2508 spin_unlock(&nn
->client_lock
);
2509 /* init connection and backchannel */
2510 nfsd4_init_conn(rqstp
, conn
, new);
2511 nfsd4_put_session(new);
2516 spin_unlock(&nn
->client_lock
);
2521 __free_session(new);
2522 out_release_drc_mem
:
2523 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2527 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2530 case NFS4_CDFC4_FORE
:
2531 case NFS4_CDFC4_BACK
:
2533 case NFS4_CDFC4_FORE_OR_BOTH
:
2534 case NFS4_CDFC4_BACK_OR_BOTH
:
2535 *dir
= NFS4_CDFC4_BOTH
;
2538 return nfserr_inval
;
2541 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_backchannel_ctl
*bc
)
2543 struct nfsd4_session
*session
= cstate
->session
;
2544 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2547 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2550 spin_lock(&nn
->client_lock
);
2551 session
->se_cb_prog
= bc
->bc_cb_program
;
2552 session
->se_cb_sec
= bc
->bc_cb_sec
;
2553 spin_unlock(&nn
->client_lock
);
2555 nfsd4_probe_callback(session
->se_client
);
2560 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2561 struct nfsd4_compound_state
*cstate
,
2562 struct nfsd4_bind_conn_to_session
*bcts
)
2565 struct nfsd4_conn
*conn
;
2566 struct nfsd4_session
*session
;
2567 struct net
*net
= SVC_NET(rqstp
);
2568 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2570 if (!nfsd4_last_compound_op(rqstp
))
2571 return nfserr_not_only_op
;
2572 spin_lock(&nn
->client_lock
);
2573 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2574 spin_unlock(&nn
->client_lock
);
2576 goto out_no_session
;
2577 status
= nfserr_wrong_cred
;
2578 if (!mach_creds_match(session
->se_client
, rqstp
))
2580 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2583 conn
= alloc_conn(rqstp
, bcts
->dir
);
2584 status
= nfserr_jukebox
;
2587 nfsd4_init_conn(rqstp
, conn
, session
);
2590 nfsd4_put_session(session
);
2595 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2599 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2603 nfsd4_destroy_session(struct svc_rqst
*r
,
2604 struct nfsd4_compound_state
*cstate
,
2605 struct nfsd4_destroy_session
*sessionid
)
2607 struct nfsd4_session
*ses
;
2609 int ref_held_by_me
= 0;
2610 struct net
*net
= SVC_NET(r
);
2611 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2613 status
= nfserr_not_only_op
;
2614 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2615 if (!nfsd4_last_compound_op(r
))
2619 dump_sessionid(__func__
, &sessionid
->sessionid
);
2620 spin_lock(&nn
->client_lock
);
2621 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2623 goto out_client_lock
;
2624 status
= nfserr_wrong_cred
;
2625 if (!mach_creds_match(ses
->se_client
, r
))
2626 goto out_put_session
;
2627 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2629 goto out_put_session
;
2630 unhash_session(ses
);
2631 spin_unlock(&nn
->client_lock
);
2633 nfsd4_probe_callback_sync(ses
->se_client
);
2635 spin_lock(&nn
->client_lock
);
2638 nfsd4_put_session_locked(ses
);
2640 spin_unlock(&nn
->client_lock
);
2645 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2647 struct nfsd4_conn
*c
;
2649 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2650 if (c
->cn_xprt
== xpt
) {
2657 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2659 struct nfs4_client
*clp
= ses
->se_client
;
2660 struct nfsd4_conn
*c
;
2661 __be32 status
= nfs_ok
;
2664 spin_lock(&clp
->cl_lock
);
2665 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
2668 status
= nfserr_conn_not_bound_to_session
;
2669 if (clp
->cl_mach_cred
)
2671 __nfsd4_hash_conn(new, ses
);
2672 spin_unlock(&clp
->cl_lock
);
2673 ret
= nfsd4_register_conn(new);
2675 /* oops; xprt is already down: */
2676 nfsd4_conn_lost(&new->cn_xpt_user
);
2679 spin_unlock(&clp
->cl_lock
);
2684 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
2686 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
2688 return args
->opcnt
> session
->se_fchannel
.maxops
;
2691 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
2692 struct nfsd4_session
*session
)
2694 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
2696 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
2700 nfsd4_sequence(struct svc_rqst
*rqstp
,
2701 struct nfsd4_compound_state
*cstate
,
2702 struct nfsd4_sequence
*seq
)
2704 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
2705 struct xdr_stream
*xdr
= &resp
->xdr
;
2706 struct nfsd4_session
*session
;
2707 struct nfs4_client
*clp
;
2708 struct nfsd4_slot
*slot
;
2709 struct nfsd4_conn
*conn
;
2712 struct net
*net
= SVC_NET(rqstp
);
2713 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2715 if (resp
->opcnt
!= 1)
2716 return nfserr_sequence_pos
;
2719 * Will be either used or freed by nfsd4_sequence_check_conn
2722 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
2724 return nfserr_jukebox
;
2726 spin_lock(&nn
->client_lock
);
2727 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
2729 goto out_no_session
;
2730 clp
= session
->se_client
;
2732 status
= nfserr_too_many_ops
;
2733 if (nfsd4_session_too_many_ops(rqstp
, session
))
2734 goto out_put_session
;
2736 status
= nfserr_req_too_big
;
2737 if (nfsd4_request_too_big(rqstp
, session
))
2738 goto out_put_session
;
2740 status
= nfserr_badslot
;
2741 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
2742 goto out_put_session
;
2744 slot
= session
->se_slots
[seq
->slotid
];
2745 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
2747 /* We do not negotiate the number of slots yet, so set the
2748 * maxslots to the session maxreqs which is used to encode
2749 * sr_highest_slotid and the sr_target_slot id to maxslots */
2750 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
2752 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
2753 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
2754 if (status
== nfserr_replay_cache
) {
2755 status
= nfserr_seq_misordered
;
2756 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
2757 goto out_put_session
;
2758 cstate
->slot
= slot
;
2759 cstate
->session
= session
;
2761 /* Return the cached reply status and set cstate->status
2762 * for nfsd4_proc_compound processing */
2763 status
= nfsd4_replay_cache_entry(resp
, seq
);
2764 cstate
->status
= nfserr_replay_cache
;
2768 goto out_put_session
;
2770 status
= nfsd4_sequence_check_conn(conn
, session
);
2773 goto out_put_session
;
2775 buflen
= (seq
->cachethis
) ?
2776 session
->se_fchannel
.maxresp_cached
:
2777 session
->se_fchannel
.maxresp_sz
;
2778 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
2780 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
2781 goto out_put_session
;
2782 svc_reserve(rqstp
, buflen
);
2785 /* Success! bump slot seqid */
2786 slot
->sl_seqid
= seq
->seqid
;
2787 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
2789 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
2791 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
2793 cstate
->slot
= slot
;
2794 cstate
->session
= session
;
2798 switch (clp
->cl_cb_state
) {
2800 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
2802 case NFSD4_CB_FAULT
:
2803 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
2806 seq
->status_flags
= 0;
2808 if (!list_empty(&clp
->cl_revoked
))
2809 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
2813 spin_unlock(&nn
->client_lock
);
2816 nfsd4_put_session_locked(session
);
2817 goto out_no_session
;
2821 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
2823 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
2825 if (nfsd4_has_session(cs
)) {
2826 if (cs
->status
!= nfserr_replay_cache
) {
2827 nfsd4_store_cache_entry(resp
);
2828 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
2830 /* Drop session reference that was taken in nfsd4_sequence() */
2831 nfsd4_put_session(cs
->session
);
2833 put_client_renew(cs
->clp
);
2837 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_destroy_clientid
*dc
)
2839 struct nfs4_client
*conf
, *unconf
;
2840 struct nfs4_client
*clp
= NULL
;
2842 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2844 spin_lock(&nn
->client_lock
);
2845 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
2846 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
2847 WARN_ON_ONCE(conf
&& unconf
);
2850 if (client_has_state(conf
)) {
2851 status
= nfserr_clientid_busy
;
2854 status
= mark_client_expired_locked(conf
);
2861 status
= nfserr_stale_clientid
;
2864 if (!mach_creds_match(clp
, rqstp
)) {
2866 status
= nfserr_wrong_cred
;
2869 unhash_client_locked(clp
);
2871 spin_unlock(&nn
->client_lock
);
2878 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
, struct nfsd4_reclaim_complete
*rc
)
2882 if (rc
->rca_one_fs
) {
2883 if (!cstate
->current_fh
.fh_dentry
)
2884 return nfserr_nofilehandle
;
2886 * We don't take advantage of the rca_one_fs case.
2887 * That's OK, it's optional, we can safely ignore it.
2892 status
= nfserr_complete_already
;
2893 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
2894 &cstate
->session
->se_client
->cl_flags
))
2897 status
= nfserr_stale_clientid
;
2898 if (is_client_expired(cstate
->session
->se_client
))
2900 * The following error isn't really legal.
2901 * But we only get here if the client just explicitly
2902 * destroyed the client. Surely it no longer cares what
2903 * error it gets back on an operation for the dead
2909 nfsd4_client_record_create(cstate
->session
->se_client
);
2915 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2916 struct nfsd4_setclientid
*setclid
)
2918 struct xdr_netobj clname
= setclid
->se_name
;
2919 nfs4_verifier clverifier
= setclid
->se_verf
;
2920 struct nfs4_client
*conf
, *new;
2921 struct nfs4_client
*unconf
= NULL
;
2923 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2925 new = create_client(clname
, rqstp
, &clverifier
);
2927 return nfserr_jukebox
;
2928 /* Cases below refer to rfc 3530 section 14.2.33: */
2929 spin_lock(&nn
->client_lock
);
2930 conf
= find_confirmed_client_by_name(&clname
, nn
);
2933 status
= nfserr_clid_inuse
;
2934 if (clp_used_exchangeid(conf
))
2936 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
2937 char addr_str
[INET6_ADDRSTRLEN
];
2938 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
2940 dprintk("NFSD: setclientid: string in use by client "
2941 "at %s\n", addr_str
);
2945 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
2947 unhash_client_locked(unconf
);
2948 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
))
2949 /* case 1: probable callback update */
2950 copy_clid(new, conf
);
2951 else /* case 4 (new client) or cases 2, 3 (client reboot): */
2953 new->cl_minorversion
= 0;
2954 gen_callback(new, setclid
, rqstp
);
2955 add_to_unconfirmed(new);
2956 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
2957 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
2958 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
2962 spin_unlock(&nn
->client_lock
);
2966 expire_client(unconf
);
2972 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
2973 struct nfsd4_compound_state
*cstate
,
2974 struct nfsd4_setclientid_confirm
*setclientid_confirm
)
2976 struct nfs4_client
*conf
, *unconf
;
2977 struct nfs4_client
*old
= NULL
;
2978 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
2979 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
2981 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2983 if (STALE_CLIENTID(clid
, nn
))
2984 return nfserr_stale_clientid
;
2986 spin_lock(&nn
->client_lock
);
2987 conf
= find_confirmed_client(clid
, false, nn
);
2988 unconf
= find_unconfirmed_client(clid
, false, nn
);
2990 * We try hard to give out unique clientid's, so if we get an
2991 * attempt to confirm the same clientid with a different cred,
2992 * there's a bug somewhere. Let's charitably assume it's our
2995 status
= nfserr_serverfault
;
2996 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
2998 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
3000 /* cases below refer to rfc 3530 section 14.2.34: */
3001 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
3002 if (conf
&& !unconf
) /* case 2: probable retransmit */
3004 else /* case 4: client hasn't noticed we rebooted yet? */
3005 status
= nfserr_stale_clientid
;
3009 if (conf
) { /* case 1: callback update */
3011 unhash_client_locked(old
);
3012 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
3013 } else { /* case 3: normal case; new or rebooted client */
3014 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
3016 status
= mark_client_expired_locked(old
);
3022 move_to_confirmed(unconf
);
3025 get_client_locked(conf
);
3026 spin_unlock(&nn
->client_lock
);
3027 nfsd4_probe_callback(conf
);
3028 spin_lock(&nn
->client_lock
);
3029 put_client_renew_locked(conf
);
3031 spin_unlock(&nn
->client_lock
);
3037 static struct nfs4_file
*nfsd4_alloc_file(void)
3039 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
3042 /* OPEN Share state helper functions */
3043 static void nfsd4_init_file(struct knfsd_fh
*fh
, unsigned int hashval
,
3044 struct nfs4_file
*fp
)
3046 lockdep_assert_held(&state_lock
);
3048 atomic_set(&fp
->fi_ref
, 1);
3049 spin_lock_init(&fp
->fi_lock
);
3050 INIT_LIST_HEAD(&fp
->fi_stateids
);
3051 INIT_LIST_HEAD(&fp
->fi_delegations
);
3052 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
3053 fp
->fi_deleg_file
= NULL
;
3054 fp
->fi_had_conflict
= false;
3055 fp
->fi_share_deny
= 0;
3056 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
3057 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
3058 hlist_add_head_rcu(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
3062 nfsd4_free_slabs(void)
3064 kmem_cache_destroy(openowner_slab
);
3065 kmem_cache_destroy(lockowner_slab
);
3066 kmem_cache_destroy(file_slab
);
3067 kmem_cache_destroy(stateid_slab
);
3068 kmem_cache_destroy(deleg_slab
);
3072 nfsd4_init_slabs(void)
3074 openowner_slab
= kmem_cache_create("nfsd4_openowners",
3075 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
3076 if (openowner_slab
== NULL
)
3078 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
3079 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
3080 if (lockowner_slab
== NULL
)
3081 goto out_free_openowner_slab
;
3082 file_slab
= kmem_cache_create("nfsd4_files",
3083 sizeof(struct nfs4_file
), 0, 0, NULL
);
3084 if (file_slab
== NULL
)
3085 goto out_free_lockowner_slab
;
3086 stateid_slab
= kmem_cache_create("nfsd4_stateids",
3087 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
3088 if (stateid_slab
== NULL
)
3089 goto out_free_file_slab
;
3090 deleg_slab
= kmem_cache_create("nfsd4_delegations",
3091 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
3092 if (deleg_slab
== NULL
)
3093 goto out_free_stateid_slab
;
3096 out_free_stateid_slab
:
3097 kmem_cache_destroy(stateid_slab
);
3099 kmem_cache_destroy(file_slab
);
3100 out_free_lockowner_slab
:
3101 kmem_cache_destroy(lockowner_slab
);
3102 out_free_openowner_slab
:
3103 kmem_cache_destroy(openowner_slab
);
3105 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3109 static void init_nfs4_replay(struct nfs4_replay
*rp
)
3111 rp
->rp_status
= nfserr_serverfault
;
3113 rp
->rp_buf
= rp
->rp_ibuf
;
3114 mutex_init(&rp
->rp_mutex
);
3117 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state
*cstate
,
3118 struct nfs4_stateowner
*so
)
3120 if (!nfsd4_has_session(cstate
)) {
3121 mutex_lock(&so
->so_replay
.rp_mutex
);
3122 cstate
->replay_owner
= nfs4_get_stateowner(so
);
3126 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state
*cstate
)
3128 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
3131 cstate
->replay_owner
= NULL
;
3132 mutex_unlock(&so
->so_replay
.rp_mutex
);
3133 nfs4_put_stateowner(so
);
3137 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
3139 struct nfs4_stateowner
*sop
;
3141 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
3145 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
3146 if (!sop
->so_owner
.data
) {
3147 kmem_cache_free(slab
, sop
);
3150 sop
->so_owner
.len
= owner
->len
;
3152 INIT_LIST_HEAD(&sop
->so_stateids
);
3153 sop
->so_client
= clp
;
3154 init_nfs4_replay(&sop
->so_replay
);
3155 atomic_set(&sop
->so_count
, 1);
3159 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
3161 lockdep_assert_held(&clp
->cl_lock
);
3163 list_add(&oo
->oo_owner
.so_strhash
,
3164 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
3165 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
3168 static void nfs4_unhash_openowner(struct nfs4_stateowner
*so
)
3170 unhash_openowner_locked(openowner(so
));
3173 static void nfs4_free_openowner(struct nfs4_stateowner
*so
)
3175 struct nfs4_openowner
*oo
= openowner(so
);
3177 kmem_cache_free(openowner_slab
, oo
);
3180 static const struct nfs4_stateowner_operations openowner_ops
= {
3181 .so_unhash
= nfs4_unhash_openowner
,
3182 .so_free
= nfs4_free_openowner
,
3185 static struct nfs4_openowner
*
3186 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
3187 struct nfsd4_compound_state
*cstate
)
3189 struct nfs4_client
*clp
= cstate
->clp
;
3190 struct nfs4_openowner
*oo
, *ret
;
3192 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
3195 oo
->oo_owner
.so_ops
= &openowner_ops
;
3196 oo
->oo_owner
.so_is_open_owner
= 1;
3197 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
3199 if (nfsd4_has_session(cstate
))
3200 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3202 oo
->oo_last_closed_stid
= NULL
;
3203 INIT_LIST_HEAD(&oo
->oo_close_lru
);
3204 spin_lock(&clp
->cl_lock
);
3205 ret
= find_openstateowner_str_locked(strhashval
, open
, clp
);
3207 hash_openowner(oo
, clp
, strhashval
);
3210 nfs4_free_openowner(&oo
->oo_owner
);
3211 spin_unlock(&clp
->cl_lock
);
3215 static void init_open_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_file
*fp
, struct nfsd4_open
*open
) {
3216 struct nfs4_openowner
*oo
= open
->op_openowner
;
3218 atomic_inc(&stp
->st_stid
.sc_count
);
3219 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
3220 INIT_LIST_HEAD(&stp
->st_locks
);
3221 stp
->st_stateowner
= nfs4_get_stateowner(&oo
->oo_owner
);
3223 stp
->st_stid
.sc_file
= fp
;
3224 stp
->st_access_bmap
= 0;
3225 stp
->st_deny_bmap
= 0;
3226 stp
->st_openstp
= NULL
;
3227 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
3228 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
3229 spin_lock(&fp
->fi_lock
);
3230 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3231 spin_unlock(&fp
->fi_lock
);
3232 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
3236 * In the 4.0 case we need to keep the owners around a little while to handle
3237 * CLOSE replay. We still do need to release any file access that is held by
3238 * them before returning however.
3241 move_to_close_lru(struct nfs4_ol_stateid
*s
, struct net
*net
)
3243 struct nfs4_ol_stateid
*last
;
3244 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
3245 struct nfsd_net
*nn
= net_generic(s
->st_stid
.sc_client
->net
,
3248 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
3251 * We know that we hold one reference via nfsd4_close, and another
3252 * "persistent" reference for the client. If the refcount is higher
3253 * than 2, then there are still calls in progress that are using this
3254 * stateid. We can't put the sc_file reference until they are finished.
3255 * Wait for the refcount to drop to 2. Since it has been unhashed,
3256 * there should be no danger of the refcount going back up again at
3259 wait_event(close_wq
, atomic_read(&s
->st_stid
.sc_count
) == 2);
3261 release_all_access(s
);
3262 if (s
->st_stid
.sc_file
) {
3263 put_nfs4_file(s
->st_stid
.sc_file
);
3264 s
->st_stid
.sc_file
= NULL
;
3267 spin_lock(&nn
->client_lock
);
3268 last
= oo
->oo_last_closed_stid
;
3269 oo
->oo_last_closed_stid
= s
;
3270 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
3271 oo
->oo_time
= get_seconds();
3272 spin_unlock(&nn
->client_lock
);
3274 nfs4_put_stid(&last
->st_stid
);
3277 /* search file_hashtbl[] for file */
3278 static struct nfs4_file
*
3279 find_file_locked(struct knfsd_fh
*fh
, unsigned int hashval
)
3281 struct nfs4_file
*fp
;
3283 hlist_for_each_entry_rcu(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3284 if (fh_match(&fp
->fi_fhandle
, fh
)) {
3285 if (atomic_inc_not_zero(&fp
->fi_ref
))
3293 find_file(struct knfsd_fh
*fh
)
3295 struct nfs4_file
*fp
;
3296 unsigned int hashval
= file_hashval(fh
);
3299 fp
= find_file_locked(fh
, hashval
);
3304 static struct nfs4_file
*
3305 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3307 struct nfs4_file
*fp
;
3308 unsigned int hashval
= file_hashval(fh
);
3311 fp
= find_file_locked(fh
, hashval
);
3316 spin_lock(&state_lock
);
3317 fp
= find_file_locked(fh
, hashval
);
3318 if (likely(fp
== NULL
)) {
3319 nfsd4_init_file(fh
, hashval
, new);
3322 spin_unlock(&state_lock
);
3328 * Called to check deny when READ with all zero stateid or
3329 * WRITE with all zero or all one stateid
3332 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3334 struct nfs4_file
*fp
;
3335 __be32 ret
= nfs_ok
;
3337 fp
= find_file(¤t_fh
->fh_handle
);
3340 /* Check for conflicting share reservations */
3341 spin_lock(&fp
->fi_lock
);
3342 if (fp
->fi_share_deny
& deny_type
)
3343 ret
= nfserr_locked
;
3344 spin_unlock(&fp
->fi_lock
);
3349 static void nfsd4_cb_recall_prepare(struct nfsd4_callback
*cb
)
3351 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3352 struct nfsd_net
*nn
= net_generic(dp
->dl_stid
.sc_client
->net
,
3355 block_delegations(&dp
->dl_stid
.sc_file
->fi_fhandle
);
3358 * We can't do this in nfsd_break_deleg_cb because it is
3359 * already holding inode->i_lock.
3361 * If the dl_time != 0, then we know that it has already been
3362 * queued for a lease break. Don't queue it again.
3364 spin_lock(&state_lock
);
3365 if (dp
->dl_time
== 0) {
3366 dp
->dl_time
= get_seconds();
3367 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3369 spin_unlock(&state_lock
);
3372 static int nfsd4_cb_recall_done(struct nfsd4_callback
*cb
,
3373 struct rpc_task
*task
)
3375 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3377 switch (task
->tk_status
) {
3381 case -NFS4ERR_BAD_STATEID
:
3383 * Race: client probably got cb_recall before open reply
3384 * granting delegation.
3386 if (dp
->dl_retries
--) {
3387 rpc_delay(task
, 2 * HZ
);
3396 static void nfsd4_cb_recall_release(struct nfsd4_callback
*cb
)
3398 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3400 nfs4_put_stid(&dp
->dl_stid
);
3403 static struct nfsd4_callback_ops nfsd4_cb_recall_ops
= {
3404 .prepare
= nfsd4_cb_recall_prepare
,
3405 .done
= nfsd4_cb_recall_done
,
3406 .release
= nfsd4_cb_recall_release
,
3409 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3412 * We're assuming the state code never drops its reference
3413 * without first removing the lease. Since we're in this lease
3414 * callback (and since the lease code is serialized by the kernel
3415 * lock) we know the server hasn't removed the lease yet, we know
3416 * it's safe to take a reference.
3418 atomic_inc(&dp
->dl_stid
.sc_count
);
3419 nfsd4_run_cb(&dp
->dl_recall
);
3422 /* Called from break_lease() with i_lock held. */
3424 nfsd_break_deleg_cb(struct file_lock
*fl
)
3427 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3428 struct nfs4_delegation
*dp
;
3431 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3434 if (fp
->fi_had_conflict
) {
3435 WARN(1, "duplicate break on %p\n", fp
);
3439 * We don't want the locks code to timeout the lease for us;
3440 * we'll remove it ourself if a delegation isn't returned
3443 fl
->fl_break_time
= 0;
3445 spin_lock(&fp
->fi_lock
);
3446 fp
->fi_had_conflict
= true;
3448 * If there are no delegations on the list, then return true
3449 * so that the lease code will go ahead and delete it.
3451 if (list_empty(&fp
->fi_delegations
))
3454 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3455 nfsd_break_one_deleg(dp
);
3456 spin_unlock(&fp
->fi_lock
);
3461 nfsd_change_deleg_cb(struct file_lock
*onlist
, int arg
,
3462 struct list_head
*dispose
)
3465 return lease_modify(onlist
, arg
, dispose
);
3470 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3471 .lm_break
= nfsd_break_deleg_cb
,
3472 .lm_change
= nfsd_change_deleg_cb
,
3475 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3477 if (nfsd4_has_session(cstate
))
3479 if (seqid
== so
->so_seqid
- 1)
3480 return nfserr_replay_me
;
3481 if (seqid
== so
->so_seqid
)
3483 return nfserr_bad_seqid
;
3486 static __be32
lookup_clientid(clientid_t
*clid
,
3487 struct nfsd4_compound_state
*cstate
,
3488 struct nfsd_net
*nn
)
3490 struct nfs4_client
*found
;
3493 found
= cstate
->clp
;
3494 if (!same_clid(&found
->cl_clientid
, clid
))
3495 return nfserr_stale_clientid
;
3499 if (STALE_CLIENTID(clid
, nn
))
3500 return nfserr_stale_clientid
;
3503 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3504 * cached already then we know this is for is for v4.0 and "sessions"
3507 WARN_ON_ONCE(cstate
->session
);
3508 spin_lock(&nn
->client_lock
);
3509 found
= find_confirmed_client(clid
, false, nn
);
3511 spin_unlock(&nn
->client_lock
);
3512 return nfserr_expired
;
3514 atomic_inc(&found
->cl_refcount
);
3515 spin_unlock(&nn
->client_lock
);
3517 /* Cache the nfs4_client in cstate! */
3518 cstate
->clp
= found
;
3523 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3524 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3526 clientid_t
*clientid
= &open
->op_clientid
;
3527 struct nfs4_client
*clp
= NULL
;
3528 unsigned int strhashval
;
3529 struct nfs4_openowner
*oo
= NULL
;
3532 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3533 return nfserr_stale_clientid
;
3535 * In case we need it later, after we've already created the
3536 * file and don't want to risk a further failure:
3538 open
->op_file
= nfsd4_alloc_file();
3539 if (open
->op_file
== NULL
)
3540 return nfserr_jukebox
;
3542 status
= lookup_clientid(clientid
, cstate
, nn
);
3547 strhashval
= ownerstr_hashval(&open
->op_owner
);
3548 oo
= find_openstateowner_str(strhashval
, open
, clp
);
3549 open
->op_openowner
= oo
;
3553 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
3554 /* Replace unconfirmed owners without checking for replay. */
3555 release_openowner(oo
);
3556 open
->op_openowner
= NULL
;
3559 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
3564 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
3566 return nfserr_jukebox
;
3567 open
->op_openowner
= oo
;
3569 open
->op_stp
= nfs4_alloc_open_stateid(clp
);
3571 return nfserr_jukebox
;
3575 static inline __be32
3576 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
3578 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
3579 return nfserr_openmode
;
3584 static int share_access_to_flags(u32 share_access
)
3586 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
3589 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
3591 struct nfs4_stid
*ret
;
3593 ret
= find_stateid_by_type(cl
, s
, NFS4_DELEG_STID
);
3596 return delegstateid(ret
);
3599 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
3601 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
3602 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
3606 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
3607 struct nfs4_delegation
**dp
)
3610 __be32 status
= nfserr_bad_stateid
;
3611 struct nfs4_delegation
*deleg
;
3613 deleg
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
3616 flags
= share_access_to_flags(open
->op_share_access
);
3617 status
= nfs4_check_delegmode(deleg
, flags
);
3619 nfs4_put_stid(&deleg
->dl_stid
);
3624 if (!nfsd4_is_deleg_cur(open
))
3628 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3632 static struct nfs4_ol_stateid
*
3633 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3635 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3636 struct nfs4_openowner
*oo
= open
->op_openowner
;
3638 spin_lock(&fp
->fi_lock
);
3639 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3640 /* ignore lock owners */
3641 if (local
->st_stateowner
->so_is_open_owner
== 0)
3643 if (local
->st_stateowner
== &oo
->oo_owner
) {
3645 atomic_inc(&ret
->st_stid
.sc_count
);
3649 spin_unlock(&fp
->fi_lock
);
3653 static inline int nfs4_access_to_access(u32 nfs4_access
)
3657 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
3658 flags
|= NFSD_MAY_READ
;
3659 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
3660 flags
|= NFSD_MAY_WRITE
;
3664 static inline __be32
3665 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
3666 struct nfsd4_open
*open
)
3668 struct iattr iattr
= {
3669 .ia_valid
= ATTR_SIZE
,
3672 if (!open
->op_truncate
)
3674 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
3675 return nfserr_inval
;
3676 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
3679 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
3680 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
3681 struct nfsd4_open
*open
)
3683 struct file
*filp
= NULL
;
3685 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
3686 int access
= nfs4_access_to_access(open
->op_share_access
);
3687 unsigned char old_access_bmap
, old_deny_bmap
;
3689 spin_lock(&fp
->fi_lock
);
3692 * Are we trying to set a deny mode that would conflict with
3695 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3696 if (status
!= nfs_ok
) {
3697 spin_unlock(&fp
->fi_lock
);
3701 /* set access to the file */
3702 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
3703 if (status
!= nfs_ok
) {
3704 spin_unlock(&fp
->fi_lock
);
3708 /* Set access bits in stateid */
3709 old_access_bmap
= stp
->st_access_bmap
;
3710 set_access(open
->op_share_access
, stp
);
3712 /* Set new deny mask */
3713 old_deny_bmap
= stp
->st_deny_bmap
;
3714 set_deny(open
->op_share_deny
, stp
);
3715 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3717 if (!fp
->fi_fds
[oflag
]) {
3718 spin_unlock(&fp
->fi_lock
);
3719 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
3721 goto out_put_access
;
3722 spin_lock(&fp
->fi_lock
);
3723 if (!fp
->fi_fds
[oflag
]) {
3724 fp
->fi_fds
[oflag
] = filp
;
3728 spin_unlock(&fp
->fi_lock
);
3732 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3734 goto out_put_access
;
3738 stp
->st_access_bmap
= old_access_bmap
;
3739 nfs4_file_put_access(fp
, open
->op_share_access
);
3740 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
3745 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
)
3748 unsigned char old_deny_bmap
;
3750 if (!test_access(open
->op_share_access
, stp
))
3751 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
3753 /* test and set deny mode */
3754 spin_lock(&fp
->fi_lock
);
3755 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
3756 if (status
== nfs_ok
) {
3757 old_deny_bmap
= stp
->st_deny_bmap
;
3758 set_deny(open
->op_share_deny
, stp
);
3759 fp
->fi_share_deny
|=
3760 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
3762 spin_unlock(&fp
->fi_lock
);
3764 if (status
!= nfs_ok
)
3767 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
3768 if (status
!= nfs_ok
)
3769 reset_union_bmap_deny(old_deny_bmap
, stp
);
3774 nfs4_set_claim_prev(struct nfsd4_open
*open
, bool has_session
)
3776 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
3779 /* Should we give out recallable state?: */
3780 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
3782 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
3785 * In the sessions case, since we don't have to establish a
3786 * separate connection for callbacks, we assume it's OK
3787 * until we hear otherwise:
3789 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
3792 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
3794 struct file_lock
*fl
;
3796 fl
= locks_alloc_lock();
3799 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
3800 fl
->fl_flags
= FL_DELEG
;
3801 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
3802 fl
->fl_end
= OFFSET_MAX
;
3803 fl
->fl_owner
= (fl_owner_t
)fp
;
3804 fl
->fl_pid
= current
->tgid
;
3808 static int nfs4_setlease(struct nfs4_delegation
*dp
)
3810 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
3811 struct file_lock
*fl
, *ret
;
3815 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
3818 filp
= find_readable_file(fp
);
3820 /* We should always have a readable file here */
3826 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
, NULL
);
3828 locks_free_lock(fl
);
3831 spin_lock(&state_lock
);
3832 spin_lock(&fp
->fi_lock
);
3833 /* Did the lease get broken before we took the lock? */
3835 if (fp
->fi_had_conflict
)
3838 if (fp
->fi_deleg_file
) {
3841 hash_delegation_locked(dp
, fp
);
3844 fp
->fi_deleg_file
= filp
;
3845 fp
->fi_delegees
= 1;
3846 hash_delegation_locked(dp
, fp
);
3847 spin_unlock(&fp
->fi_lock
);
3848 spin_unlock(&state_lock
);
3851 spin_unlock(&fp
->fi_lock
);
3852 spin_unlock(&state_lock
);
3858 static struct nfs4_delegation
*
3859 nfs4_set_delegation(struct nfs4_client
*clp
, struct svc_fh
*fh
,
3860 struct nfs4_file
*fp
)
3863 struct nfs4_delegation
*dp
;
3865 if (fp
->fi_had_conflict
)
3866 return ERR_PTR(-EAGAIN
);
3868 dp
= alloc_init_deleg(clp
, fh
);
3870 return ERR_PTR(-ENOMEM
);
3873 spin_lock(&state_lock
);
3874 spin_lock(&fp
->fi_lock
);
3875 dp
->dl_stid
.sc_file
= fp
;
3876 if (!fp
->fi_deleg_file
) {
3877 spin_unlock(&fp
->fi_lock
);
3878 spin_unlock(&state_lock
);
3879 status
= nfs4_setlease(dp
);
3882 if (fp
->fi_had_conflict
) {
3887 hash_delegation_locked(dp
, fp
);
3890 spin_unlock(&fp
->fi_lock
);
3891 spin_unlock(&state_lock
);
3894 nfs4_put_stid(&dp
->dl_stid
);
3895 return ERR_PTR(status
);
3900 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
3902 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
3903 if (status
== -EAGAIN
)
3904 open
->op_why_no_deleg
= WND4_CONTENTION
;
3906 open
->op_why_no_deleg
= WND4_RESOURCE
;
3907 switch (open
->op_deleg_want
) {
3908 case NFS4_SHARE_WANT_READ_DELEG
:
3909 case NFS4_SHARE_WANT_WRITE_DELEG
:
3910 case NFS4_SHARE_WANT_ANY_DELEG
:
3912 case NFS4_SHARE_WANT_CANCEL
:
3913 open
->op_why_no_deleg
= WND4_CANCELLED
;
3915 case NFS4_SHARE_WANT_NO_DELEG
:
3922 * Attempt to hand out a delegation.
3924 * Note we don't support write delegations, and won't until the vfs has
3925 * proper support for them.
3928 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
,
3929 struct nfs4_ol_stateid
*stp
)
3931 struct nfs4_delegation
*dp
;
3932 struct nfs4_openowner
*oo
= openowner(stp
->st_stateowner
);
3933 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
3937 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
3938 open
->op_recall
= 0;
3939 switch (open
->op_claim_type
) {
3940 case NFS4_OPEN_CLAIM_PREVIOUS
:
3942 open
->op_recall
= 1;
3943 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
3946 case NFS4_OPEN_CLAIM_NULL
:
3947 case NFS4_OPEN_CLAIM_FH
:
3949 * Let's not give out any delegations till everyone's
3950 * had the chance to reclaim theirs....
3952 if (locks_in_grace(clp
->net
))
3954 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
3957 * Also, if the file was opened for write or
3958 * create, there's a good chance the client's
3959 * about to write to it, resulting in an
3960 * immediate recall (since we don't support
3961 * write delegations):
3963 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
3965 if (open
->op_create
== NFS4_OPEN_CREATE
)
3971 dp
= nfs4_set_delegation(clp
, fh
, stp
->st_stid
.sc_file
);
3975 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
3977 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
3978 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
3979 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
3980 nfs4_put_stid(&dp
->dl_stid
);
3983 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
3984 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
3985 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
3986 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
3987 open
->op_recall
= 1;
3990 /* 4.1 client asking for a delegation? */
3991 if (open
->op_deleg_want
)
3992 nfsd4_open_deleg_none_ext(open
, status
);
3996 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
3997 struct nfs4_delegation
*dp
)
3999 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
4000 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4001 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4002 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
4003 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
4004 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4005 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4006 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
4008 /* Otherwise the client must be confused wanting a delegation
4009 * it already has, therefore we don't return
4010 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4015 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
4017 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
4018 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
4019 struct nfs4_file
*fp
= NULL
;
4020 struct nfs4_ol_stateid
*stp
= NULL
;
4021 struct nfs4_delegation
*dp
= NULL
;
4025 * Lookup file; if found, lookup stateid and check open request,
4026 * and check for delegations in the process of being recalled.
4027 * If not found, create the nfs4_file struct
4029 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
4030 if (fp
!= open
->op_file
) {
4031 status
= nfs4_check_deleg(cl
, open
, &dp
);
4034 stp
= nfsd4_find_existing_open(fp
, open
);
4036 open
->op_file
= NULL
;
4037 status
= nfserr_bad_stateid
;
4038 if (nfsd4_is_deleg_cur(open
))
4040 status
= nfserr_jukebox
;
4044 * OPEN the file, or upgrade an existing OPEN.
4045 * If truncate fails, the OPEN fails.
4048 /* Stateid was found, this is an OPEN upgrade */
4049 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
4054 open
->op_stp
= NULL
;
4055 init_open_stateid(stp
, fp
, open
);
4056 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
4058 release_open_stateid(stp
);
4062 update_stateid(&stp
->st_stid
.sc_stateid
);
4063 memcpy(&open
->op_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4065 if (nfsd4_has_session(&resp
->cstate
)) {
4066 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
4067 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4068 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
4074 * Attempt to hand out a delegation. No error return, because the
4075 * OPEN succeeds even if we fail.
4077 nfs4_open_delegation(current_fh
, open
, stp
);
4081 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
4082 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4084 /* 4.1 client trying to upgrade/downgrade delegation? */
4085 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
4086 open
->op_deleg_want
)
4087 nfsd4_deleg_xgrade_none_ext(open
, dp
);
4091 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
4092 nfs4_set_claim_prev(open
, nfsd4_has_session(&resp
->cstate
));
4094 * To finish the open response, we just need to set the rflags.
4096 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
4097 if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
) &&
4098 !nfsd4_has_session(&resp
->cstate
))
4099 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
4101 nfs4_put_stid(&dp
->dl_stid
);
4103 nfs4_put_stid(&stp
->st_stid
);
4108 void nfsd4_cleanup_open_state(struct nfsd4_compound_state
*cstate
,
4109 struct nfsd4_open
*open
, __be32 status
)
4111 if (open
->op_openowner
) {
4112 struct nfs4_stateowner
*so
= &open
->op_openowner
->oo_owner
;
4114 nfsd4_cstate_assign_replay(cstate
, so
);
4115 nfs4_put_stateowner(so
);
4118 kmem_cache_free(file_slab
, open
->op_file
);
4120 nfs4_put_stid(&open
->op_stp
->st_stid
);
4124 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4127 struct nfs4_client
*clp
;
4129 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4131 dprintk("process_renew(%08x/%08x): starting\n",
4132 clid
->cl_boot
, clid
->cl_id
);
4133 status
= lookup_clientid(clid
, cstate
, nn
);
4137 status
= nfserr_cb_path_down
;
4138 if (!list_empty(&clp
->cl_delegations
)
4139 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
4147 nfsd4_end_grace(struct nfsd_net
*nn
)
4149 /* do nothing if grace period already ended */
4150 if (nn
->grace_ended
)
4153 dprintk("NFSD: end of grace period\n");
4154 nn
->grace_ended
= true;
4156 * If the server goes down again right now, an NFSv4
4157 * client will still be allowed to reclaim after it comes back up,
4158 * even if it hasn't yet had a chance to reclaim state this time.
4161 nfsd4_record_grace_done(nn
);
4163 * At this point, NFSv4 clients can still reclaim. But if the
4164 * server crashes, any that have not yet reclaimed will be out
4165 * of luck on the next boot.
4167 * (NFSv4.1+ clients are considered to have reclaimed once they
4168 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4169 * have reclaimed after their first OPEN.)
4171 locks_end_grace(&nn
->nfsd4_manager
);
4173 * At this point, and once lockd and/or any other containers
4174 * exit their grace period, further reclaims will fail and
4175 * regular locking can resume.
4180 nfs4_laundromat(struct nfsd_net
*nn
)
4182 struct nfs4_client
*clp
;
4183 struct nfs4_openowner
*oo
;
4184 struct nfs4_delegation
*dp
;
4185 struct nfs4_ol_stateid
*stp
;
4186 struct list_head
*pos
, *next
, reaplist
;
4187 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
4188 time_t t
, new_timeo
= nn
->nfsd4_lease
;
4190 dprintk("NFSD: laundromat service - starting\n");
4191 nfsd4_end_grace(nn
);
4192 INIT_LIST_HEAD(&reaplist
);
4193 spin_lock(&nn
->client_lock
);
4194 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
4195 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4196 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
4197 t
= clp
->cl_time
- cutoff
;
4198 new_timeo
= min(new_timeo
, t
);
4201 if (mark_client_expired_locked(clp
)) {
4202 dprintk("NFSD: client in use (clientid %08x)\n",
4203 clp
->cl_clientid
.cl_id
);
4206 list_add(&clp
->cl_lru
, &reaplist
);
4208 spin_unlock(&nn
->client_lock
);
4209 list_for_each_safe(pos
, next
, &reaplist
) {
4210 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4211 dprintk("NFSD: purging unused client (clientid %08x)\n",
4212 clp
->cl_clientid
.cl_id
);
4213 list_del_init(&clp
->cl_lru
);
4216 spin_lock(&state_lock
);
4217 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
4218 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4219 if (net_generic(dp
->dl_stid
.sc_client
->net
, nfsd_net_id
) != nn
)
4221 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
4222 t
= dp
->dl_time
- cutoff
;
4223 new_timeo
= min(new_timeo
, t
);
4226 unhash_delegation_locked(dp
);
4227 list_add(&dp
->dl_recall_lru
, &reaplist
);
4229 spin_unlock(&state_lock
);
4230 while (!list_empty(&reaplist
)) {
4231 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
4233 list_del_init(&dp
->dl_recall_lru
);
4234 revoke_delegation(dp
);
4237 spin_lock(&nn
->client_lock
);
4238 while (!list_empty(&nn
->close_lru
)) {
4239 oo
= list_first_entry(&nn
->close_lru
, struct nfs4_openowner
,
4241 if (time_after((unsigned long)oo
->oo_time
,
4242 (unsigned long)cutoff
)) {
4243 t
= oo
->oo_time
- cutoff
;
4244 new_timeo
= min(new_timeo
, t
);
4247 list_del_init(&oo
->oo_close_lru
);
4248 stp
= oo
->oo_last_closed_stid
;
4249 oo
->oo_last_closed_stid
= NULL
;
4250 spin_unlock(&nn
->client_lock
);
4251 nfs4_put_stid(&stp
->st_stid
);
4252 spin_lock(&nn
->client_lock
);
4254 spin_unlock(&nn
->client_lock
);
4256 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
4260 static struct workqueue_struct
*laundry_wq
;
4261 static void laundromat_main(struct work_struct
*);
4264 laundromat_main(struct work_struct
*laundry
)
4267 struct delayed_work
*dwork
= container_of(laundry
, struct delayed_work
,
4269 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
4272 t
= nfs4_laundromat(nn
);
4273 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
4274 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
4277 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*stp
)
4279 if (!fh_match(&fhp
->fh_handle
, &stp
->st_stid
.sc_file
->fi_fhandle
))
4280 return nfserr_bad_stateid
;
4285 access_permit_read(struct nfs4_ol_stateid
*stp
)
4287 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
4288 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
4289 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
4293 access_permit_write(struct nfs4_ol_stateid
*stp
)
4295 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
4296 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
4300 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
4302 __be32 status
= nfserr_openmode
;
4304 /* For lock stateid's, we test the parent open, not the lock: */
4305 if (stp
->st_openstp
)
4306 stp
= stp
->st_openstp
;
4307 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
4309 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
4316 static inline __be32
4317 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
4319 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
4321 else if (locks_in_grace(net
)) {
4322 /* Answer in remaining cases depends on existence of
4323 * conflicting state; so we must wait out the grace period. */
4324 return nfserr_grace
;
4325 } else if (flags
& WR_STATE
)
4326 return nfs4_share_conflict(current_fh
,
4327 NFS4_SHARE_DENY_WRITE
);
4328 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4329 return nfs4_share_conflict(current_fh
,
4330 NFS4_SHARE_DENY_READ
);
4334 * Allow READ/WRITE during grace period on recovered state only for files
4335 * that are not able to provide mandatory locking.
4338 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4340 return locks_in_grace(net
) && mandatory_lock(inode
);
4343 /* Returns true iff a is later than b: */
4344 static bool stateid_generation_after(stateid_t
*a
, stateid_t
*b
)
4346 return (s32
)(a
->si_generation
- b
->si_generation
) > 0;
4349 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4352 * When sessions are used the stateid generation number is ignored
4355 if (has_session
&& in
->si_generation
== 0)
4358 if (in
->si_generation
== ref
->si_generation
)
4361 /* If the client sends us a stateid from the future, it's buggy: */
4362 if (stateid_generation_after(in
, ref
))
4363 return nfserr_bad_stateid
;
4365 * However, we could see a stateid from the past, even from a
4366 * non-buggy client. For example, if the client sends a lock
4367 * while some IO is outstanding, the lock may bump si_generation
4368 * while the IO is still in flight. The client could avoid that
4369 * situation by waiting for responses on all the IO requests,
4370 * but better performance may result in retrying IO that
4371 * receives an old_stateid error if requests are rarely
4372 * reordered in flight:
4374 return nfserr_old_stateid
;
4377 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4379 struct nfs4_stid
*s
;
4380 struct nfs4_ol_stateid
*ols
;
4381 __be32 status
= nfserr_bad_stateid
;
4383 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4385 /* Client debugging aid. */
4386 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4387 char addr_str
[INET6_ADDRSTRLEN
];
4388 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4390 pr_warn_ratelimited("NFSD: client %s testing state ID "
4391 "with incorrect client ID\n", addr_str
);
4394 spin_lock(&cl
->cl_lock
);
4395 s
= find_stateid_locked(cl
, stateid
);
4398 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4401 switch (s
->sc_type
) {
4402 case NFS4_DELEG_STID
:
4405 case NFS4_REVOKED_DELEG_STID
:
4406 status
= nfserr_deleg_revoked
;
4408 case NFS4_OPEN_STID
:
4409 case NFS4_LOCK_STID
:
4410 ols
= openlockstateid(s
);
4411 if (ols
->st_stateowner
->so_is_open_owner
4412 && !(openowner(ols
->st_stateowner
)->oo_flags
4413 & NFS4_OO_CONFIRMED
))
4414 status
= nfserr_bad_stateid
;
4419 printk("unknown stateid type %x\n", s
->sc_type
);
4421 case NFS4_CLOSED_STID
:
4422 case NFS4_CLOSED_DELEG_STID
:
4423 status
= nfserr_bad_stateid
;
4426 spin_unlock(&cl
->cl_lock
);
4431 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4432 stateid_t
*stateid
, unsigned char typemask
,
4433 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4437 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4438 return nfserr_bad_stateid
;
4439 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4440 if (status
== nfserr_stale_clientid
) {
4441 if (cstate
->session
)
4442 return nfserr_bad_stateid
;
4443 return nfserr_stale_stateid
;
4447 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4449 return nfserr_bad_stateid
;
4454 * Checks for stateid operations
4457 nfs4_preprocess_stateid_op(struct net
*net
, struct nfsd4_compound_state
*cstate
,
4458 stateid_t
*stateid
, int flags
, struct file
**filpp
)
4460 struct nfs4_stid
*s
;
4461 struct nfs4_ol_stateid
*stp
= NULL
;
4462 struct nfs4_delegation
*dp
= NULL
;
4463 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4464 struct inode
*ino
= current_fh
->fh_dentry
->d_inode
;
4465 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4466 struct file
*file
= NULL
;
4472 if (grace_disallows_io(net
, ino
))
4473 return nfserr_grace
;
4475 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
))
4476 return check_special_stateids(net
, current_fh
, stateid
, flags
);
4478 status
= nfsd4_lookup_stateid(cstate
, stateid
,
4479 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
4483 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, nfsd4_has_session(cstate
));
4486 switch (s
->sc_type
) {
4487 case NFS4_DELEG_STID
:
4488 dp
= delegstateid(s
);
4489 status
= nfs4_check_delegmode(dp
, flags
);
4493 file
= dp
->dl_stid
.sc_file
->fi_deleg_file
;
4496 status
= nfserr_serverfault
;
4502 case NFS4_OPEN_STID
:
4503 case NFS4_LOCK_STID
:
4504 stp
= openlockstateid(s
);
4505 status
= nfs4_check_fh(current_fh
, stp
);
4508 if (stp
->st_stateowner
->so_is_open_owner
4509 && !(openowner(stp
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4511 status
= nfs4_check_openmode(stp
, flags
);
4515 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
4517 if (flags
& RD_STATE
)
4518 file
= find_readable_file(fp
);
4520 file
= find_writeable_file(fp
);
4524 status
= nfserr_bad_stateid
;
4536 * Test if the stateid is valid
4539 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4540 struct nfsd4_test_stateid
*test_stateid
)
4542 struct nfsd4_test_stateid_id
*stateid
;
4543 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4545 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
4546 stateid
->ts_id_status
=
4547 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
4553 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4554 struct nfsd4_free_stateid
*free_stateid
)
4556 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
4557 struct nfs4_stid
*s
;
4558 struct nfs4_delegation
*dp
;
4559 struct nfs4_ol_stateid
*stp
;
4560 struct nfs4_client
*cl
= cstate
->session
->se_client
;
4561 __be32 ret
= nfserr_bad_stateid
;
4563 spin_lock(&cl
->cl_lock
);
4564 s
= find_stateid_locked(cl
, stateid
);
4567 switch (s
->sc_type
) {
4568 case NFS4_DELEG_STID
:
4569 ret
= nfserr_locks_held
;
4571 case NFS4_OPEN_STID
:
4572 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4575 ret
= nfserr_locks_held
;
4577 case NFS4_LOCK_STID
:
4578 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4581 stp
= openlockstateid(s
);
4582 ret
= nfserr_locks_held
;
4583 if (check_for_locks(stp
->st_stid
.sc_file
,
4584 lockowner(stp
->st_stateowner
)))
4586 unhash_lock_stateid(stp
);
4587 spin_unlock(&cl
->cl_lock
);
4591 case NFS4_REVOKED_DELEG_STID
:
4592 dp
= delegstateid(s
);
4593 list_del_init(&dp
->dl_recall_lru
);
4594 spin_unlock(&cl
->cl_lock
);
4598 /* Default falls through and returns nfserr_bad_stateid */
4601 spin_unlock(&cl
->cl_lock
);
4609 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
4610 RD_STATE
: WR_STATE
;
4613 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
4615 struct svc_fh
*current_fh
= &cstate
->current_fh
;
4616 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
4619 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
4622 if (stp
->st_stid
.sc_type
== NFS4_CLOSED_STID
4623 || stp
->st_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
)
4625 * "Closed" stateid's exist *only* to return
4626 * nfserr_replay_me from the previous step, and
4627 * revoked delegations are kept only for free_stateid.
4629 return nfserr_bad_stateid
;
4630 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4633 return nfs4_check_fh(current_fh
, stp
);
4637 * Checks for sequence id mutating operations.
4640 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4641 stateid_t
*stateid
, char typemask
,
4642 struct nfs4_ol_stateid
**stpp
,
4643 struct nfsd_net
*nn
)
4646 struct nfs4_stid
*s
;
4647 struct nfs4_ol_stateid
*stp
= NULL
;
4649 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
4650 seqid
, STATEID_VAL(stateid
));
4653 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
4656 stp
= openlockstateid(s
);
4657 nfsd4_cstate_assign_replay(cstate
, stp
->st_stateowner
);
4659 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
4663 nfs4_put_stid(&stp
->st_stid
);
4667 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
4668 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
4671 struct nfs4_openowner
*oo
;
4672 struct nfs4_ol_stateid
*stp
;
4674 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
4675 NFS4_OPEN_STID
, &stp
, nn
);
4678 oo
= openowner(stp
->st_stateowner
);
4679 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
4680 nfs4_put_stid(&stp
->st_stid
);
4681 return nfserr_bad_stateid
;
4688 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4689 struct nfsd4_open_confirm
*oc
)
4692 struct nfs4_openowner
*oo
;
4693 struct nfs4_ol_stateid
*stp
;
4694 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4696 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
4697 cstate
->current_fh
.fh_dentry
);
4699 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
4703 status
= nfs4_preprocess_seqid_op(cstate
,
4704 oc
->oc_seqid
, &oc
->oc_req_stateid
,
4705 NFS4_OPEN_STID
, &stp
, nn
);
4708 oo
= openowner(stp
->st_stateowner
);
4709 status
= nfserr_bad_stateid
;
4710 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
)
4712 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4713 update_stateid(&stp
->st_stid
.sc_stateid
);
4714 memcpy(&oc
->oc_resp_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4715 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
4716 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4718 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
4721 nfs4_put_stid(&stp
->st_stid
);
4723 nfsd4_bump_seqid(cstate
, status
);
4727 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
4729 if (!test_access(access
, stp
))
4731 nfs4_file_put_access(stp
->st_stid
.sc_file
, access
);
4732 clear_access(access
, stp
);
4735 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
4737 switch (to_access
) {
4738 case NFS4_SHARE_ACCESS_READ
:
4739 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
4740 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4742 case NFS4_SHARE_ACCESS_WRITE
:
4743 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
4744 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
4746 case NFS4_SHARE_ACCESS_BOTH
:
4754 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
4755 struct nfsd4_compound_state
*cstate
,
4756 struct nfsd4_open_downgrade
*od
)
4759 struct nfs4_ol_stateid
*stp
;
4760 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4762 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
4763 cstate
->current_fh
.fh_dentry
);
4765 /* We don't yet support WANT bits: */
4766 if (od
->od_deleg_want
)
4767 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
4770 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
4771 &od
->od_stateid
, &stp
, nn
);
4774 status
= nfserr_inval
;
4775 if (!test_access(od
->od_share_access
, stp
)) {
4776 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
4777 stp
->st_access_bmap
, od
->od_share_access
);
4780 if (!test_deny(od
->od_share_deny
, stp
)) {
4781 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
4782 stp
->st_deny_bmap
, od
->od_share_deny
);
4785 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
4787 reset_union_bmap_deny(od
->od_share_deny
, stp
);
4789 update_stateid(&stp
->st_stid
.sc_stateid
);
4790 memcpy(&od
->od_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4793 nfs4_put_stid(&stp
->st_stid
);
4795 nfsd4_bump_seqid(cstate
, status
);
4799 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
4801 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
4802 LIST_HEAD(reaplist
);
4804 s
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4805 spin_lock(&clp
->cl_lock
);
4806 unhash_open_stateid(s
, &reaplist
);
4808 if (clp
->cl_minorversion
) {
4809 put_ol_stateid_locked(s
, &reaplist
);
4810 spin_unlock(&clp
->cl_lock
);
4811 free_ol_stateid_reaplist(&reaplist
);
4813 spin_unlock(&clp
->cl_lock
);
4814 free_ol_stateid_reaplist(&reaplist
);
4815 move_to_close_lru(s
, clp
->net
);
4820 * nfs4_unlock_state() called after encode
4823 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4824 struct nfsd4_close
*close
)
4827 struct nfs4_ol_stateid
*stp
;
4828 struct net
*net
= SVC_NET(rqstp
);
4829 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
4831 dprintk("NFSD: nfsd4_close on file %pd\n",
4832 cstate
->current_fh
.fh_dentry
);
4834 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
4836 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
4838 nfsd4_bump_seqid(cstate
, status
);
4841 update_stateid(&stp
->st_stid
.sc_stateid
);
4842 memcpy(&close
->cl_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
4844 nfsd4_close_open_stateid(stp
);
4846 /* put reference from nfs4_preprocess_seqid_op */
4847 nfs4_put_stid(&stp
->st_stid
);
4853 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4854 struct nfsd4_delegreturn
*dr
)
4856 struct nfs4_delegation
*dp
;
4857 stateid_t
*stateid
= &dr
->dr_stateid
;
4858 struct nfs4_stid
*s
;
4860 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4862 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
4865 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
4868 dp
= delegstateid(s
);
4869 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
4873 destroy_delegation(dp
);
4875 nfs4_put_stid(&dp
->dl_stid
);
4881 #define LOFF_OVERFLOW(start, len) ((u64)(len) > ~(u64)(start))
4884 end_offset(u64 start
, u64 len
)
4889 return end
>= start
? end
: NFS4_MAX_UINT64
;
4892 /* last octet in a range */
4894 last_byte_offset(u64 start
, u64 len
)
4900 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
4904 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
4905 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
4906 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
4907 * locking, this prevents us from being completely protocol-compliant. The
4908 * real solution to this problem is to start using unsigned file offsets in
4909 * the VFS, but this is a very deep change!
4912 nfs4_transform_lock_offset(struct file_lock
*lock
)
4914 if (lock
->fl_start
< 0)
4915 lock
->fl_start
= OFFSET_MAX
;
4916 if (lock
->fl_end
< 0)
4917 lock
->fl_end
= OFFSET_MAX
;
4920 static void nfsd4_fl_get_owner(struct file_lock
*dst
, struct file_lock
*src
)
4922 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)src
->fl_owner
;
4923 dst
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lo
->lo_owner
));
4926 static void nfsd4_fl_put_owner(struct file_lock
*fl
)
4928 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)fl
->fl_owner
;
4931 nfs4_put_stateowner(&lo
->lo_owner
);
4932 fl
->fl_owner
= NULL
;
4936 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
4937 .lm_get_owner
= nfsd4_fl_get_owner
,
4938 .lm_put_owner
= nfsd4_fl_put_owner
,
4942 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
4944 struct nfs4_lockowner
*lo
;
4946 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
4947 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
4948 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
4949 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
4950 if (!deny
->ld_owner
.data
)
4951 /* We just don't care that much */
4953 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
4954 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
4957 deny
->ld_owner
.len
= 0;
4958 deny
->ld_owner
.data
= NULL
;
4959 deny
->ld_clientid
.cl_boot
= 0;
4960 deny
->ld_clientid
.cl_id
= 0;
4962 deny
->ld_start
= fl
->fl_start
;
4963 deny
->ld_length
= NFS4_MAX_UINT64
;
4964 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
4965 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
4966 deny
->ld_type
= NFS4_READ_LT
;
4967 if (fl
->fl_type
!= F_RDLCK
)
4968 deny
->ld_type
= NFS4_WRITE_LT
;
4971 static struct nfs4_lockowner
*
4972 find_lockowner_str_locked(clientid_t
*clid
, struct xdr_netobj
*owner
,
4973 struct nfs4_client
*clp
)
4975 unsigned int strhashval
= ownerstr_hashval(owner
);
4976 struct nfs4_stateowner
*so
;
4978 lockdep_assert_held(&clp
->cl_lock
);
4980 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[strhashval
],
4982 if (so
->so_is_open_owner
)
4984 if (same_owner_str(so
, owner
))
4985 return lockowner(nfs4_get_stateowner(so
));
4990 static struct nfs4_lockowner
*
4991 find_lockowner_str(clientid_t
*clid
, struct xdr_netobj
*owner
,
4992 struct nfs4_client
*clp
)
4994 struct nfs4_lockowner
*lo
;
4996 spin_lock(&clp
->cl_lock
);
4997 lo
= find_lockowner_str_locked(clid
, owner
, clp
);
4998 spin_unlock(&clp
->cl_lock
);
5002 static void nfs4_unhash_lockowner(struct nfs4_stateowner
*sop
)
5004 unhash_lockowner_locked(lockowner(sop
));
5007 static void nfs4_free_lockowner(struct nfs4_stateowner
*sop
)
5009 struct nfs4_lockowner
*lo
= lockowner(sop
);
5011 kmem_cache_free(lockowner_slab
, lo
);
5014 static const struct nfs4_stateowner_operations lockowner_ops
= {
5015 .so_unhash
= nfs4_unhash_lockowner
,
5016 .so_free
= nfs4_free_lockowner
,
5020 * Alloc a lock owner structure.
5021 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5024 * strhashval = ownerstr_hashval
5026 static struct nfs4_lockowner
*
5027 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
,
5028 struct nfs4_ol_stateid
*open_stp
,
5029 struct nfsd4_lock
*lock
)
5031 struct nfs4_lockowner
*lo
, *ret
;
5033 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
5036 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
5037 lo
->lo_owner
.so_is_open_owner
= 0;
5038 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
;
5039 lo
->lo_owner
.so_ops
= &lockowner_ops
;
5040 spin_lock(&clp
->cl_lock
);
5041 ret
= find_lockowner_str_locked(&clp
->cl_clientid
,
5042 &lock
->lk_new_owner
, clp
);
5044 list_add(&lo
->lo_owner
.so_strhash
,
5045 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
5048 nfs4_free_lockowner(&lo
->lo_owner
);
5049 spin_unlock(&clp
->cl_lock
);
5054 init_lock_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_lockowner
*lo
,
5055 struct nfs4_file
*fp
, struct inode
*inode
,
5056 struct nfs4_ol_stateid
*open_stp
)
5058 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5060 lockdep_assert_held(&clp
->cl_lock
);
5062 atomic_inc(&stp
->st_stid
.sc_count
);
5063 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
5064 stp
->st_stateowner
= nfs4_get_stateowner(&lo
->lo_owner
);
5066 stp
->st_stid
.sc_file
= fp
;
5067 stp
->st_stid
.sc_free
= nfs4_free_lock_stateid
;
5068 stp
->st_access_bmap
= 0;
5069 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
5070 stp
->st_openstp
= open_stp
;
5071 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
5072 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
5073 spin_lock(&fp
->fi_lock
);
5074 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
5075 spin_unlock(&fp
->fi_lock
);
5078 static struct nfs4_ol_stateid
*
5079 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
5081 struct nfs4_ol_stateid
*lst
;
5082 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5084 lockdep_assert_held(&clp
->cl_lock
);
5086 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
5087 if (lst
->st_stid
.sc_file
== fp
) {
5088 atomic_inc(&lst
->st_stid
.sc_count
);
5095 static struct nfs4_ol_stateid
*
5096 find_or_create_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fi
,
5097 struct inode
*inode
, struct nfs4_ol_stateid
*ost
,
5100 struct nfs4_stid
*ns
= NULL
;
5101 struct nfs4_ol_stateid
*lst
;
5102 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5103 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
5105 spin_lock(&clp
->cl_lock
);
5106 lst
= find_lock_stateid(lo
, fi
);
5108 spin_unlock(&clp
->cl_lock
);
5109 ns
= nfs4_alloc_stid(clp
, stateid_slab
);
5113 spin_lock(&clp
->cl_lock
);
5114 lst
= find_lock_stateid(lo
, fi
);
5116 lst
= openlockstateid(ns
);
5117 init_lock_stateid(lst
, lo
, fi
, inode
, ost
);
5122 spin_unlock(&clp
->cl_lock
);
5129 check_lock_length(u64 offset
, u64 length
)
5131 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
5132 LOFF_OVERFLOW(offset
, length
)));
5135 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
5137 struct nfs4_file
*fp
= lock_stp
->st_stid
.sc_file
;
5139 lockdep_assert_held(&fp
->fi_lock
);
5141 if (test_access(access
, lock_stp
))
5143 __nfs4_file_get_access(fp
, access
);
5144 set_access(access
, lock_stp
);
5148 lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
,
5149 struct nfs4_ol_stateid
*ost
,
5150 struct nfsd4_lock
*lock
,
5151 struct nfs4_ol_stateid
**lst
, bool *new)
5154 struct nfs4_file
*fi
= ost
->st_stid
.sc_file
;
5155 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5156 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
5157 struct inode
*inode
= cstate
->current_fh
.fh_dentry
->d_inode
;
5158 struct nfs4_lockowner
*lo
;
5159 unsigned int strhashval
;
5161 lo
= find_lockowner_str(&cl
->cl_clientid
, &lock
->v
.new.owner
, cl
);
5163 strhashval
= ownerstr_hashval(&lock
->v
.new.owner
);
5164 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
5166 return nfserr_jukebox
;
5168 /* with an existing lockowner, seqids must be the same */
5169 status
= nfserr_bad_seqid
;
5170 if (!cstate
->minorversion
&&
5171 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
5175 *lst
= find_or_create_lock_stateid(lo
, fi
, inode
, ost
, new);
5177 status
= nfserr_jukebox
;
5182 nfs4_put_stateowner(&lo
->lo_owner
);
5190 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5191 struct nfsd4_lock
*lock
)
5193 struct nfs4_openowner
*open_sop
= NULL
;
5194 struct nfs4_lockowner
*lock_sop
= NULL
;
5195 struct nfs4_ol_stateid
*lock_stp
= NULL
;
5196 struct nfs4_ol_stateid
*open_stp
= NULL
;
5197 struct nfs4_file
*fp
;
5198 struct file
*filp
= NULL
;
5199 struct file_lock
*file_lock
= NULL
;
5200 struct file_lock
*conflock
= NULL
;
5205 struct net
*net
= SVC_NET(rqstp
);
5206 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5208 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5209 (long long) lock
->lk_offset
,
5210 (long long) lock
->lk_length
);
5212 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
5213 return nfserr_inval
;
5215 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
5216 S_IFREG
, NFSD_MAY_LOCK
))) {
5217 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5221 if (lock
->lk_is_new
) {
5222 if (nfsd4_has_session(cstate
))
5223 /* See rfc 5661 18.10.3: given clientid is ignored: */
5224 memcpy(&lock
->v
.new.clientid
,
5225 &cstate
->session
->se_client
->cl_clientid
,
5226 sizeof(clientid_t
));
5228 status
= nfserr_stale_clientid
;
5229 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
5232 /* validate and update open stateid and open seqid */
5233 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
5234 lock
->lk_new_open_seqid
,
5235 &lock
->lk_new_open_stateid
,
5239 open_sop
= openowner(open_stp
->st_stateowner
);
5240 status
= nfserr_bad_stateid
;
5241 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
5242 &lock
->v
.new.clientid
))
5244 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
5247 status
= nfs4_preprocess_seqid_op(cstate
,
5248 lock
->lk_old_lock_seqid
,
5249 &lock
->lk_old_lock_stateid
,
5250 NFS4_LOCK_STID
, &lock_stp
, nn
);
5254 lock_sop
= lockowner(lock_stp
->st_stateowner
);
5256 lkflg
= setlkflg(lock
->lk_type
);
5257 status
= nfs4_check_openmode(lock_stp
, lkflg
);
5261 status
= nfserr_grace
;
5262 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
5264 status
= nfserr_no_grace
;
5265 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
5268 file_lock
= locks_alloc_lock();
5270 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5271 status
= nfserr_jukebox
;
5275 fp
= lock_stp
->st_stid
.sc_file
;
5276 switch (lock
->lk_type
) {
5279 spin_lock(&fp
->fi_lock
);
5280 filp
= find_readable_file_locked(fp
);
5282 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
5283 spin_unlock(&fp
->fi_lock
);
5284 file_lock
->fl_type
= F_RDLCK
;
5287 case NFS4_WRITEW_LT
:
5288 spin_lock(&fp
->fi_lock
);
5289 filp
= find_writeable_file_locked(fp
);
5291 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
5292 spin_unlock(&fp
->fi_lock
);
5293 file_lock
->fl_type
= F_WRLCK
;
5296 status
= nfserr_inval
;
5300 status
= nfserr_openmode
;
5304 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lock_sop
->lo_owner
));
5305 file_lock
->fl_pid
= current
->tgid
;
5306 file_lock
->fl_file
= filp
;
5307 file_lock
->fl_flags
= FL_POSIX
;
5308 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5309 file_lock
->fl_start
= lock
->lk_offset
;
5310 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
5311 nfs4_transform_lock_offset(file_lock
);
5313 conflock
= locks_alloc_lock();
5315 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5316 status
= nfserr_jukebox
;
5320 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
5322 case 0: /* success! */
5323 update_stateid(&lock_stp
->st_stid
.sc_stateid
);
5324 memcpy(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
.sc_stateid
,
5328 case (EAGAIN
): /* conflock holds conflicting lock */
5329 status
= nfserr_denied
;
5330 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
5331 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
5334 status
= nfserr_deadlock
;
5337 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
5338 status
= nfserrno(err
);
5345 /* Bump seqid manually if the 4.0 replay owner is openowner */
5346 if (cstate
->replay_owner
&&
5347 cstate
->replay_owner
!= &lock_sop
->lo_owner
&&
5348 seqid_mutating_err(ntohl(status
)))
5349 lock_sop
->lo_owner
.so_seqid
++;
5352 * If this is a new, never-before-used stateid, and we are
5353 * returning an error, then just go ahead and release it.
5356 release_lock_stateid(lock_stp
);
5358 nfs4_put_stid(&lock_stp
->st_stid
);
5361 nfs4_put_stid(&open_stp
->st_stid
);
5362 nfsd4_bump_seqid(cstate
, status
);
5364 locks_free_lock(file_lock
);
5366 locks_free_lock(conflock
);
5371 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
5372 * so we do a temporary open here just to get an open file to pass to
5373 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
5376 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
5379 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
5381 err
= nfserrno(vfs_test_lock(file
, lock
));
5391 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5392 struct nfsd4_lockt
*lockt
)
5394 struct file_lock
*file_lock
= NULL
;
5395 struct nfs4_lockowner
*lo
= NULL
;
5397 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5399 if (locks_in_grace(SVC_NET(rqstp
)))
5400 return nfserr_grace
;
5402 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
5403 return nfserr_inval
;
5405 if (!nfsd4_has_session(cstate
)) {
5406 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
5411 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5414 file_lock
= locks_alloc_lock();
5416 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5417 status
= nfserr_jukebox
;
5421 switch (lockt
->lt_type
) {
5424 file_lock
->fl_type
= F_RDLCK
;
5427 case NFS4_WRITEW_LT
:
5428 file_lock
->fl_type
= F_WRLCK
;
5431 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
5432 status
= nfserr_inval
;
5436 lo
= find_lockowner_str(&lockt
->lt_clientid
, &lockt
->lt_owner
,
5439 file_lock
->fl_owner
= (fl_owner_t
)lo
;
5440 file_lock
->fl_pid
= current
->tgid
;
5441 file_lock
->fl_flags
= FL_POSIX
;
5443 file_lock
->fl_start
= lockt
->lt_offset
;
5444 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
5446 nfs4_transform_lock_offset(file_lock
);
5448 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
5452 if (file_lock
->fl_type
!= F_UNLCK
) {
5453 status
= nfserr_denied
;
5454 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
5458 nfs4_put_stateowner(&lo
->lo_owner
);
5460 locks_free_lock(file_lock
);
5465 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5466 struct nfsd4_locku
*locku
)
5468 struct nfs4_ol_stateid
*stp
;
5469 struct file
*filp
= NULL
;
5470 struct file_lock
*file_lock
= NULL
;
5473 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5475 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
5476 (long long) locku
->lu_offset
,
5477 (long long) locku
->lu_length
);
5479 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
5480 return nfserr_inval
;
5482 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
5483 &locku
->lu_stateid
, NFS4_LOCK_STID
,
5487 filp
= find_any_file(stp
->st_stid
.sc_file
);
5489 status
= nfserr_lock_range
;
5492 file_lock
= locks_alloc_lock();
5494 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5495 status
= nfserr_jukebox
;
5499 file_lock
->fl_type
= F_UNLCK
;
5500 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(stp
->st_stateowner
));
5501 file_lock
->fl_pid
= current
->tgid
;
5502 file_lock
->fl_file
= filp
;
5503 file_lock
->fl_flags
= FL_POSIX
;
5504 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5505 file_lock
->fl_start
= locku
->lu_offset
;
5507 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
5509 nfs4_transform_lock_offset(file_lock
);
5511 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
5513 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
5516 update_stateid(&stp
->st_stid
.sc_stateid
);
5517 memcpy(&locku
->lu_stateid
, &stp
->st_stid
.sc_stateid
, sizeof(stateid_t
));
5521 nfs4_put_stid(&stp
->st_stid
);
5523 nfsd4_bump_seqid(cstate
, status
);
5525 locks_free_lock(file_lock
);
5529 status
= nfserrno(err
);
5535 * true: locks held by lockowner
5536 * false: no locks held by lockowner
5539 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
5541 struct file_lock
*fl
;
5543 struct file
*filp
= find_any_file(fp
);
5544 struct inode
*inode
;
5545 struct file_lock_context
*flctx
;
5548 /* Any valid lock stateid should have some sort of access */
5553 inode
= file_inode(filp
);
5554 flctx
= inode
->i_flctx
;
5556 if (flctx
&& !list_empty_careful(&flctx
->flc_posix
)) {
5557 spin_lock(&flctx
->flc_lock
);
5558 list_for_each_entry(fl
, &flctx
->flc_posix
, fl_list
) {
5559 if (fl
->fl_owner
== (fl_owner_t
)lowner
) {
5564 spin_unlock(&flctx
->flc_lock
);
5571 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
5572 struct nfsd4_compound_state
*cstate
,
5573 struct nfsd4_release_lockowner
*rlockowner
)
5575 clientid_t
*clid
= &rlockowner
->rl_clientid
;
5576 struct nfs4_stateowner
*sop
;
5577 struct nfs4_lockowner
*lo
= NULL
;
5578 struct nfs4_ol_stateid
*stp
;
5579 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
5580 unsigned int hashval
= ownerstr_hashval(owner
);
5582 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5583 struct nfs4_client
*clp
;
5585 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
5586 clid
->cl_boot
, clid
->cl_id
);
5588 status
= lookup_clientid(clid
, cstate
, nn
);
5593 /* Find the matching lock stateowner */
5594 spin_lock(&clp
->cl_lock
);
5595 list_for_each_entry(sop
, &clp
->cl_ownerstr_hashtbl
[hashval
],
5598 if (sop
->so_is_open_owner
|| !same_owner_str(sop
, owner
))
5601 /* see if there are still any locks associated with it */
5602 lo
= lockowner(sop
);
5603 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
5604 if (check_for_locks(stp
->st_stid
.sc_file
, lo
)) {
5605 status
= nfserr_locks_held
;
5606 spin_unlock(&clp
->cl_lock
);
5611 nfs4_get_stateowner(sop
);
5614 spin_unlock(&clp
->cl_lock
);
5616 release_lockowner(lo
);
5620 static inline struct nfs4_client_reclaim
*
5623 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
5627 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
5629 struct nfs4_client_reclaim
*crp
;
5631 crp
= nfsd4_find_reclaim_client(name
, nn
);
5632 return (crp
&& crp
->cr_clp
);
5636 * failure => all reset bets are off, nfserr_no_grace...
5638 struct nfs4_client_reclaim
*
5639 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
5641 unsigned int strhashval
;
5642 struct nfs4_client_reclaim
*crp
;
5644 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
5645 crp
= alloc_reclaim();
5647 strhashval
= clientstr_hashval(name
);
5648 INIT_LIST_HEAD(&crp
->cr_strhash
);
5649 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
5650 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
5652 nn
->reclaim_str_hashtbl_size
++;
5658 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
5660 list_del(&crp
->cr_strhash
);
5662 nn
->reclaim_str_hashtbl_size
--;
5666 nfs4_release_reclaim(struct nfsd_net
*nn
)
5668 struct nfs4_client_reclaim
*crp
= NULL
;
5671 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
5672 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
5673 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
5674 struct nfs4_client_reclaim
, cr_strhash
);
5675 nfs4_remove_reclaim_record(crp
, nn
);
5678 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
5682 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
5683 struct nfs4_client_reclaim
*
5684 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
5686 unsigned int strhashval
;
5687 struct nfs4_client_reclaim
*crp
= NULL
;
5689 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
5691 strhashval
= clientstr_hashval(recdir
);
5692 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
5693 if (same_name(crp
->cr_recdir
, recdir
)) {
5701 * Called from OPEN. Look for clientid in reclaim list.
5704 nfs4_check_open_reclaim(clientid_t
*clid
,
5705 struct nfsd4_compound_state
*cstate
,
5706 struct nfsd_net
*nn
)
5710 /* find clientid in conf_id_hashtbl */
5711 status
= lookup_clientid(clid
, cstate
, nn
);
5713 return nfserr_reclaim_bad
;
5715 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
, &cstate
->clp
->cl_flags
))
5716 return nfserr_no_grace
;
5718 if (nfsd4_client_record_check(cstate
->clp
))
5719 return nfserr_reclaim_bad
;
5724 #ifdef CONFIG_NFSD_FAULT_INJECTION
5726 put_client(struct nfs4_client
*clp
)
5728 atomic_dec(&clp
->cl_refcount
);
5731 static struct nfs4_client
*
5732 nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5734 struct nfs4_client
*clp
;
5735 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5738 if (!nfsd_netns_ready(nn
))
5741 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5742 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
5749 nfsd_inject_print_clients(void)
5751 struct nfs4_client
*clp
;
5753 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5755 char buf
[INET6_ADDRSTRLEN
];
5757 if (!nfsd_netns_ready(nn
))
5760 spin_lock(&nn
->client_lock
);
5761 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5762 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5763 pr_info("NFS Client: %s\n", buf
);
5766 spin_unlock(&nn
->client_lock
);
5772 nfsd_inject_forget_client(struct sockaddr_storage
*addr
, size_t addr_size
)
5775 struct nfs4_client
*clp
;
5776 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5779 if (!nfsd_netns_ready(nn
))
5782 spin_lock(&nn
->client_lock
);
5783 clp
= nfsd_find_client(addr
, addr_size
);
5785 if (mark_client_expired_locked(clp
) == nfs_ok
)
5790 spin_unlock(&nn
->client_lock
);
5799 nfsd_inject_forget_clients(u64 max
)
5802 struct nfs4_client
*clp
, *next
;
5803 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5805 LIST_HEAD(reaplist
);
5807 if (!nfsd_netns_ready(nn
))
5810 spin_lock(&nn
->client_lock
);
5811 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
5812 if (mark_client_expired_locked(clp
) == nfs_ok
) {
5813 list_add(&clp
->cl_lru
, &reaplist
);
5814 if (max
!= 0 && ++count
>= max
)
5818 spin_unlock(&nn
->client_lock
);
5820 list_for_each_entry_safe(clp
, next
, &reaplist
, cl_lru
)
5826 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
5829 char buf
[INET6_ADDRSTRLEN
];
5830 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
5831 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
5835 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid
*lst
,
5836 struct list_head
*collect
)
5838 struct nfs4_client
*clp
= lst
->st_stid
.sc_client
;
5839 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5845 lockdep_assert_held(&nn
->client_lock
);
5846 atomic_inc(&clp
->cl_refcount
);
5847 list_add(&lst
->st_locks
, collect
);
5850 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
5851 struct list_head
*collect
,
5852 void (*func
)(struct nfs4_ol_stateid
*))
5854 struct nfs4_openowner
*oop
;
5855 struct nfs4_ol_stateid
*stp
, *st_next
;
5856 struct nfs4_ol_stateid
*lst
, *lst_next
;
5859 spin_lock(&clp
->cl_lock
);
5860 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
5861 list_for_each_entry_safe(stp
, st_next
,
5862 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
5863 list_for_each_entry_safe(lst
, lst_next
,
5864 &stp
->st_locks
, st_locks
) {
5867 nfsd_inject_add_lock_to_list(lst
,
5872 * Despite the fact that these functions deal
5873 * with 64-bit integers for "count", we must
5874 * ensure that it doesn't blow up the
5875 * clp->cl_refcount. Throw a warning if we
5876 * start to approach INT_MAX here.
5878 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
5885 spin_unlock(&clp
->cl_lock
);
5891 nfsd_collect_client_locks(struct nfs4_client
*clp
, struct list_head
*collect
,
5894 return nfsd_foreach_client_lock(clp
, max
, collect
, unhash_lock_stateid
);
5898 nfsd_print_client_locks(struct nfs4_client
*clp
)
5900 u64 count
= nfsd_foreach_client_lock(clp
, 0, NULL
, NULL
);
5901 nfsd_print_count(clp
, count
, "locked files");
5906 nfsd_inject_print_locks(void)
5908 struct nfs4_client
*clp
;
5910 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5913 if (!nfsd_netns_ready(nn
))
5916 spin_lock(&nn
->client_lock
);
5917 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
5918 count
+= nfsd_print_client_locks(clp
);
5919 spin_unlock(&nn
->client_lock
);
5925 nfsd_reap_locks(struct list_head
*reaplist
)
5927 struct nfs4_client
*clp
;
5928 struct nfs4_ol_stateid
*stp
, *next
;
5930 list_for_each_entry_safe(stp
, next
, reaplist
, st_locks
) {
5931 list_del_init(&stp
->st_locks
);
5932 clp
= stp
->st_stid
.sc_client
;
5933 nfs4_put_stid(&stp
->st_stid
);
5939 nfsd_inject_forget_client_locks(struct sockaddr_storage
*addr
, size_t addr_size
)
5941 unsigned int count
= 0;
5942 struct nfs4_client
*clp
;
5943 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5945 LIST_HEAD(reaplist
);
5947 if (!nfsd_netns_ready(nn
))
5950 spin_lock(&nn
->client_lock
);
5951 clp
= nfsd_find_client(addr
, addr_size
);
5953 count
= nfsd_collect_client_locks(clp
, &reaplist
, 0);
5954 spin_unlock(&nn
->client_lock
);
5955 nfsd_reap_locks(&reaplist
);
5960 nfsd_inject_forget_locks(u64 max
)
5963 struct nfs4_client
*clp
;
5964 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5966 LIST_HEAD(reaplist
);
5968 if (!nfsd_netns_ready(nn
))
5971 spin_lock(&nn
->client_lock
);
5972 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
5973 count
+= nfsd_collect_client_locks(clp
, &reaplist
, max
- count
);
5974 if (max
!= 0 && count
>= max
)
5977 spin_unlock(&nn
->client_lock
);
5978 nfsd_reap_locks(&reaplist
);
5983 nfsd_foreach_client_openowner(struct nfs4_client
*clp
, u64 max
,
5984 struct list_head
*collect
,
5985 void (*func
)(struct nfs4_openowner
*))
5987 struct nfs4_openowner
*oop
, *next
;
5988 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
5992 lockdep_assert_held(&nn
->client_lock
);
5994 spin_lock(&clp
->cl_lock
);
5995 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
5999 atomic_inc(&clp
->cl_refcount
);
6000 list_add(&oop
->oo_perclient
, collect
);
6005 * Despite the fact that these functions deal with
6006 * 64-bit integers for "count", we must ensure that
6007 * it doesn't blow up the clp->cl_refcount. Throw a
6008 * warning if we start to approach INT_MAX here.
6010 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6014 spin_unlock(&clp
->cl_lock
);
6020 nfsd_print_client_openowners(struct nfs4_client
*clp
)
6022 u64 count
= nfsd_foreach_client_openowner(clp
, 0, NULL
, NULL
);
6024 nfsd_print_count(clp
, count
, "openowners");
6029 nfsd_collect_client_openowners(struct nfs4_client
*clp
,
6030 struct list_head
*collect
, u64 max
)
6032 return nfsd_foreach_client_openowner(clp
, max
, collect
,
6033 unhash_openowner_locked
);
6037 nfsd_inject_print_openowners(void)
6039 struct nfs4_client
*clp
;
6041 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6044 if (!nfsd_netns_ready(nn
))
6047 spin_lock(&nn
->client_lock
);
6048 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6049 count
+= nfsd_print_client_openowners(clp
);
6050 spin_unlock(&nn
->client_lock
);
6056 nfsd_reap_openowners(struct list_head
*reaplist
)
6058 struct nfs4_client
*clp
;
6059 struct nfs4_openowner
*oop
, *next
;
6061 list_for_each_entry_safe(oop
, next
, reaplist
, oo_perclient
) {
6062 list_del_init(&oop
->oo_perclient
);
6063 clp
= oop
->oo_owner
.so_client
;
6064 release_openowner(oop
);
6070 nfsd_inject_forget_client_openowners(struct sockaddr_storage
*addr
,
6073 unsigned int count
= 0;
6074 struct nfs4_client
*clp
;
6075 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6077 LIST_HEAD(reaplist
);
6079 if (!nfsd_netns_ready(nn
))
6082 spin_lock(&nn
->client_lock
);
6083 clp
= nfsd_find_client(addr
, addr_size
);
6085 count
= nfsd_collect_client_openowners(clp
, &reaplist
, 0);
6086 spin_unlock(&nn
->client_lock
);
6087 nfsd_reap_openowners(&reaplist
);
6092 nfsd_inject_forget_openowners(u64 max
)
6095 struct nfs4_client
*clp
;
6096 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6098 LIST_HEAD(reaplist
);
6100 if (!nfsd_netns_ready(nn
))
6103 spin_lock(&nn
->client_lock
);
6104 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6105 count
+= nfsd_collect_client_openowners(clp
, &reaplist
,
6107 if (max
!= 0 && count
>= max
)
6110 spin_unlock(&nn
->client_lock
);
6111 nfsd_reap_openowners(&reaplist
);
6115 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
6116 struct list_head
*victims
)
6118 struct nfs4_delegation
*dp
, *next
;
6119 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6123 lockdep_assert_held(&nn
->client_lock
);
6125 spin_lock(&state_lock
);
6126 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
6129 * It's not safe to mess with delegations that have a
6130 * non-zero dl_time. They might have already been broken
6131 * and could be processed by the laundromat outside of
6132 * the state_lock. Just leave them be.
6134 if (dp
->dl_time
!= 0)
6137 atomic_inc(&clp
->cl_refcount
);
6138 unhash_delegation_locked(dp
);
6139 list_add(&dp
->dl_recall_lru
, victims
);
6143 * Despite the fact that these functions deal with
6144 * 64-bit integers for "count", we must ensure that
6145 * it doesn't blow up the clp->cl_refcount. Throw a
6146 * warning if we start to approach INT_MAX here.
6148 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6152 spin_unlock(&state_lock
);
6157 nfsd_print_client_delegations(struct nfs4_client
*clp
)
6159 u64 count
= nfsd_find_all_delegations(clp
, 0, NULL
);
6161 nfsd_print_count(clp
, count
, "delegations");
6166 nfsd_inject_print_delegations(void)
6168 struct nfs4_client
*clp
;
6170 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6173 if (!nfsd_netns_ready(nn
))
6176 spin_lock(&nn
->client_lock
);
6177 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6178 count
+= nfsd_print_client_delegations(clp
);
6179 spin_unlock(&nn
->client_lock
);
6185 nfsd_forget_delegations(struct list_head
*reaplist
)
6187 struct nfs4_client
*clp
;
6188 struct nfs4_delegation
*dp
, *next
;
6190 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6191 list_del_init(&dp
->dl_recall_lru
);
6192 clp
= dp
->dl_stid
.sc_client
;
6193 revoke_delegation(dp
);
6199 nfsd_inject_forget_client_delegations(struct sockaddr_storage
*addr
,
6203 struct nfs4_client
*clp
;
6204 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6206 LIST_HEAD(reaplist
);
6208 if (!nfsd_netns_ready(nn
))
6211 spin_lock(&nn
->client_lock
);
6212 clp
= nfsd_find_client(addr
, addr_size
);
6214 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6215 spin_unlock(&nn
->client_lock
);
6217 nfsd_forget_delegations(&reaplist
);
6222 nfsd_inject_forget_delegations(u64 max
)
6225 struct nfs4_client
*clp
;
6226 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6228 LIST_HEAD(reaplist
);
6230 if (!nfsd_netns_ready(nn
))
6233 spin_lock(&nn
->client_lock
);
6234 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6235 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6236 if (max
!= 0 && count
>= max
)
6239 spin_unlock(&nn
->client_lock
);
6240 nfsd_forget_delegations(&reaplist
);
6245 nfsd_recall_delegations(struct list_head
*reaplist
)
6247 struct nfs4_client
*clp
;
6248 struct nfs4_delegation
*dp
, *next
;
6250 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6251 list_del_init(&dp
->dl_recall_lru
);
6252 clp
= dp
->dl_stid
.sc_client
;
6254 * We skipped all entries that had a zero dl_time before,
6255 * so we can now reset the dl_time back to 0. If a delegation
6256 * break comes in now, then it won't make any difference since
6257 * we're recalling it either way.
6259 spin_lock(&state_lock
);
6261 spin_unlock(&state_lock
);
6262 nfsd_break_one_deleg(dp
);
6268 nfsd_inject_recall_client_delegations(struct sockaddr_storage
*addr
,
6272 struct nfs4_client
*clp
;
6273 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6275 LIST_HEAD(reaplist
);
6277 if (!nfsd_netns_ready(nn
))
6280 spin_lock(&nn
->client_lock
);
6281 clp
= nfsd_find_client(addr
, addr_size
);
6283 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6284 spin_unlock(&nn
->client_lock
);
6286 nfsd_recall_delegations(&reaplist
);
6291 nfsd_inject_recall_delegations(u64 max
)
6294 struct nfs4_client
*clp
, *next
;
6295 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6297 LIST_HEAD(reaplist
);
6299 if (!nfsd_netns_ready(nn
))
6302 spin_lock(&nn
->client_lock
);
6303 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6304 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6305 if (max
!= 0 && ++count
>= max
)
6308 spin_unlock(&nn
->client_lock
);
6309 nfsd_recall_delegations(&reaplist
);
6312 #endif /* CONFIG_NFSD_FAULT_INJECTION */
6315 * Since the lifetime of a delegation isn't limited to that of an open, a
6316 * client may quite reasonably hang on to a delegation as long as it has
6317 * the inode cached. This becomes an obvious problem the first time a
6318 * client's inode cache approaches the size of the server's total memory.
6320 * For now we avoid this problem by imposing a hard limit on the number
6321 * of delegations, which varies according to the server's memory size.
6324 set_max_delegations(void)
6327 * Allow at most 4 delegations per megabyte of RAM. Quick
6328 * estimates suggest that in the worst case (where every delegation
6329 * is for a different inode), a delegation could take about 1.5K,
6330 * giving a worst case usage of about 6% of memory.
6332 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
6335 static int nfs4_state_create_net(struct net
*net
)
6337 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6340 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6341 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6342 if (!nn
->conf_id_hashtbl
)
6344 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
6345 CLIENT_HASH_SIZE
, GFP_KERNEL
);
6346 if (!nn
->unconf_id_hashtbl
)
6348 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
6349 SESSION_HASH_SIZE
, GFP_KERNEL
);
6350 if (!nn
->sessionid_hashtbl
)
6353 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6354 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
6355 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
6357 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
6358 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
6359 nn
->conf_name_tree
= RB_ROOT
;
6360 nn
->unconf_name_tree
= RB_ROOT
;
6361 INIT_LIST_HEAD(&nn
->client_lru
);
6362 INIT_LIST_HEAD(&nn
->close_lru
);
6363 INIT_LIST_HEAD(&nn
->del_recall_lru
);
6364 spin_lock_init(&nn
->client_lock
);
6366 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
6372 kfree(nn
->unconf_id_hashtbl
);
6374 kfree(nn
->conf_id_hashtbl
);
6380 nfs4_state_destroy_net(struct net
*net
)
6383 struct nfs4_client
*clp
= NULL
;
6384 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6386 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6387 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
6388 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6389 destroy_client(clp
);
6393 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6394 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
6395 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
6396 destroy_client(clp
);
6400 kfree(nn
->sessionid_hashtbl
);
6401 kfree(nn
->unconf_id_hashtbl
);
6402 kfree(nn
->conf_id_hashtbl
);
6407 nfs4_state_start_net(struct net
*net
)
6409 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6412 ret
= nfs4_state_create_net(net
);
6415 nn
->boot_time
= get_seconds();
6416 nn
->grace_ended
= false;
6417 locks_start_grace(net
, &nn
->nfsd4_manager
);
6418 nfsd4_client_tracking_init(net
);
6419 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
6420 nn
->nfsd4_grace
, net
);
6421 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
6425 /* initialization to perform when the nfsd service is started: */
6428 nfs4_state_start(void)
6432 ret
= set_callback_cred();
6435 laundry_wq
= create_singlethread_workqueue("nfsd4");
6436 if (laundry_wq
== NULL
) {
6440 ret
= nfsd4_create_callback_queue();
6442 goto out_free_laundry
;
6444 set_max_delegations();
6449 destroy_workqueue(laundry_wq
);
6455 nfs4_state_shutdown_net(struct net
*net
)
6457 struct nfs4_delegation
*dp
= NULL
;
6458 struct list_head
*pos
, *next
, reaplist
;
6459 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6461 cancel_delayed_work_sync(&nn
->laundromat_work
);
6462 locks_end_grace(&nn
->nfsd4_manager
);
6464 INIT_LIST_HEAD(&reaplist
);
6465 spin_lock(&state_lock
);
6466 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
6467 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6468 unhash_delegation_locked(dp
);
6469 list_add(&dp
->dl_recall_lru
, &reaplist
);
6471 spin_unlock(&state_lock
);
6472 list_for_each_safe(pos
, next
, &reaplist
) {
6473 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
6474 list_del_init(&dp
->dl_recall_lru
);
6475 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
6476 nfs4_put_stid(&dp
->dl_stid
);
6479 nfsd4_client_tracking_exit(net
);
6480 nfs4_state_destroy_net(net
);
6484 nfs4_state_shutdown(void)
6486 destroy_workqueue(laundry_wq
);
6487 nfsd4_destroy_callback_queue();
6491 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6493 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
6494 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
6498 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
6500 if (cstate
->minorversion
) {
6501 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
6502 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6507 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
6509 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
6513 * functions to set current state id
6516 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6518 put_stateid(cstate
, &odp
->od_stateid
);
6522 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open
*open
)
6524 put_stateid(cstate
, &open
->op_stateid
);
6528 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
6530 put_stateid(cstate
, &close
->cl_stateid
);
6534 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_lock
*lock
)
6536 put_stateid(cstate
, &lock
->lk_resp_stateid
);
6540 * functions to consume current state id
6544 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_open_downgrade
*odp
)
6546 get_stateid(cstate
, &odp
->od_stateid
);
6550 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_delegreturn
*drp
)
6552 get_stateid(cstate
, &drp
->dr_stateid
);
6556 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_free_stateid
*fsp
)
6558 get_stateid(cstate
, &fsp
->fr_stateid
);
6562 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_setattr
*setattr
)
6564 get_stateid(cstate
, &setattr
->sa_stateid
);
6568 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_close
*close
)
6570 get_stateid(cstate
, &close
->cl_stateid
);
6574 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_locku
*locku
)
6576 get_stateid(cstate
, &locku
->lu_stateid
);
6580 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_read
*read
)
6582 get_stateid(cstate
, &read
->rd_stateid
);
6586 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
, struct nfsd4_write
*write
)
6588 get_stateid(cstate
, &write
->wr_stateid
);