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"
53 #define NFSDDBG_FACILITY NFSDDBG_PROC
55 #define all_ones {{~0,~0},~0}
56 static const stateid_t one_stateid
= {
58 .si_opaque
= all_ones
,
60 static const stateid_t zero_stateid
= {
63 static const stateid_t currentstateid
= {
66 static const stateid_t close_stateid
= {
67 .si_generation
= 0xffffffffU
,
70 static u64 current_sessionid
= 1;
72 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
73 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
74 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
75 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
77 /* forward declarations */
78 static bool check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
);
79 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
);
84 * Currently used for the del_recall_lru and file hash table. In an
85 * effort to decrease the scope of the client_mutex, this spinlock may
86 * eventually cover more:
88 static DEFINE_SPINLOCK(state_lock
);
91 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
92 * the refcount on the open stateid to drop.
94 static DECLARE_WAIT_QUEUE_HEAD(close_wq
);
96 static struct kmem_cache
*openowner_slab
;
97 static struct kmem_cache
*lockowner_slab
;
98 static struct kmem_cache
*file_slab
;
99 static struct kmem_cache
*stateid_slab
;
100 static struct kmem_cache
*deleg_slab
;
101 static struct kmem_cache
*odstate_slab
;
103 static void free_session(struct nfsd4_session
*);
105 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops
;
106 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops
;
108 static bool is_session_dead(struct nfsd4_session
*ses
)
110 return ses
->se_flags
& NFS4_SESSION_DEAD
;
113 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
115 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
116 return nfserr_jukebox
;
117 ses
->se_flags
|= NFS4_SESSION_DEAD
;
121 static bool is_client_expired(struct nfs4_client
*clp
)
123 return clp
->cl_time
== 0;
126 static __be32
get_client_locked(struct nfs4_client
*clp
)
128 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
130 lockdep_assert_held(&nn
->client_lock
);
132 if (is_client_expired(clp
))
133 return nfserr_expired
;
134 atomic_inc(&clp
->cl_refcount
);
138 /* must be called under the client_lock */
140 renew_client_locked(struct nfs4_client
*clp
)
142 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
144 if (is_client_expired(clp
)) {
146 printk("%s: client (clientid %08x/%08x) already expired\n",
148 clp
->cl_clientid
.cl_boot
,
149 clp
->cl_clientid
.cl_id
);
153 dprintk("renewing client (clientid %08x/%08x)\n",
154 clp
->cl_clientid
.cl_boot
,
155 clp
->cl_clientid
.cl_id
);
156 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
157 clp
->cl_time
= get_seconds();
160 static void put_client_renew_locked(struct nfs4_client
*clp
)
162 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
164 lockdep_assert_held(&nn
->client_lock
);
166 if (!atomic_dec_and_test(&clp
->cl_refcount
))
168 if (!is_client_expired(clp
))
169 renew_client_locked(clp
);
172 static void put_client_renew(struct nfs4_client
*clp
)
174 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
176 if (!atomic_dec_and_lock(&clp
->cl_refcount
, &nn
->client_lock
))
178 if (!is_client_expired(clp
))
179 renew_client_locked(clp
);
180 spin_unlock(&nn
->client_lock
);
183 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
187 if (is_session_dead(ses
))
188 return nfserr_badsession
;
189 status
= get_client_locked(ses
->se_client
);
192 atomic_inc(&ses
->se_ref
);
196 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
198 struct nfs4_client
*clp
= ses
->se_client
;
199 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
201 lockdep_assert_held(&nn
->client_lock
);
203 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
205 put_client_renew_locked(clp
);
208 static void nfsd4_put_session(struct nfsd4_session
*ses
)
210 struct nfs4_client
*clp
= ses
->se_client
;
211 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
213 spin_lock(&nn
->client_lock
);
214 nfsd4_put_session_locked(ses
);
215 spin_unlock(&nn
->client_lock
);
218 static struct nfsd4_blocked_lock
*
219 find_blocked_lock(struct nfs4_lockowner
*lo
, struct knfsd_fh
*fh
,
222 struct nfsd4_blocked_lock
*cur
, *found
= NULL
;
224 spin_lock(&nn
->blocked_locks_lock
);
225 list_for_each_entry(cur
, &lo
->lo_blocked
, nbl_list
) {
226 if (fh_match(fh
, &cur
->nbl_fh
)) {
227 list_del_init(&cur
->nbl_list
);
228 list_del_init(&cur
->nbl_lru
);
233 spin_unlock(&nn
->blocked_locks_lock
);
235 posix_unblock_lock(&found
->nbl_lock
);
239 static struct nfsd4_blocked_lock
*
240 find_or_allocate_block(struct nfs4_lockowner
*lo
, struct knfsd_fh
*fh
,
243 struct nfsd4_blocked_lock
*nbl
;
245 nbl
= find_blocked_lock(lo
, fh
, nn
);
247 nbl
= kmalloc(sizeof(*nbl
), GFP_KERNEL
);
249 fh_copy_shallow(&nbl
->nbl_fh
, fh
);
250 locks_init_lock(&nbl
->nbl_lock
);
251 nfsd4_init_cb(&nbl
->nbl_cb
, lo
->lo_owner
.so_client
,
252 &nfsd4_cb_notify_lock_ops
,
253 NFSPROC4_CLNT_CB_NOTIFY_LOCK
);
260 free_blocked_lock(struct nfsd4_blocked_lock
*nbl
)
262 locks_release_private(&nbl
->nbl_lock
);
267 remove_blocked_locks(struct nfs4_lockowner
*lo
)
269 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
270 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
271 struct nfsd4_blocked_lock
*nbl
;
274 /* Dequeue all blocked locks */
275 spin_lock(&nn
->blocked_locks_lock
);
276 while (!list_empty(&lo
->lo_blocked
)) {
277 nbl
= list_first_entry(&lo
->lo_blocked
,
278 struct nfsd4_blocked_lock
,
280 list_del_init(&nbl
->nbl_list
);
281 list_move(&nbl
->nbl_lru
, &reaplist
);
283 spin_unlock(&nn
->blocked_locks_lock
);
286 while (!list_empty(&reaplist
)) {
287 nbl
= list_first_entry(&reaplist
, struct nfsd4_blocked_lock
,
289 list_del_init(&nbl
->nbl_lru
);
290 posix_unblock_lock(&nbl
->nbl_lock
);
291 free_blocked_lock(nbl
);
296 nfsd4_cb_notify_lock_done(struct nfsd4_callback
*cb
, struct rpc_task
*task
)
299 * Since this is just an optimization, we don't try very hard if it
300 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
301 * just quit trying on anything else.
303 switch (task
->tk_status
) {
305 rpc_delay(task
, 1 * HZ
);
313 nfsd4_cb_notify_lock_release(struct nfsd4_callback
*cb
)
315 struct nfsd4_blocked_lock
*nbl
= container_of(cb
,
316 struct nfsd4_blocked_lock
, nbl_cb
);
318 free_blocked_lock(nbl
);
321 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops
= {
322 .done
= nfsd4_cb_notify_lock_done
,
323 .release
= nfsd4_cb_notify_lock_release
,
326 static inline struct nfs4_stateowner
*
327 nfs4_get_stateowner(struct nfs4_stateowner
*sop
)
329 atomic_inc(&sop
->so_count
);
334 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
)
336 return (sop
->so_owner
.len
== owner
->len
) &&
337 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
);
340 static struct nfs4_openowner
*
341 find_openstateowner_str_locked(unsigned int hashval
, struct nfsd4_open
*open
,
342 struct nfs4_client
*clp
)
344 struct nfs4_stateowner
*so
;
346 lockdep_assert_held(&clp
->cl_lock
);
348 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[hashval
],
350 if (!so
->so_is_open_owner
)
352 if (same_owner_str(so
, &open
->op_owner
))
353 return openowner(nfs4_get_stateowner(so
));
358 static struct nfs4_openowner
*
359 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
360 struct nfs4_client
*clp
)
362 struct nfs4_openowner
*oo
;
364 spin_lock(&clp
->cl_lock
);
365 oo
= find_openstateowner_str_locked(hashval
, open
, clp
);
366 spin_unlock(&clp
->cl_lock
);
371 opaque_hashval(const void *ptr
, int nbytes
)
373 unsigned char *cptr
= (unsigned char *) ptr
;
383 static void nfsd4_free_file_rcu(struct rcu_head
*rcu
)
385 struct nfs4_file
*fp
= container_of(rcu
, struct nfs4_file
, fi_rcu
);
387 kmem_cache_free(file_slab
, fp
);
391 put_nfs4_file(struct nfs4_file
*fi
)
393 might_lock(&state_lock
);
395 if (atomic_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
396 hlist_del_rcu(&fi
->fi_hash
);
397 spin_unlock(&state_lock
);
398 WARN_ON_ONCE(!list_empty(&fi
->fi_clnt_odstate
));
399 WARN_ON_ONCE(!list_empty(&fi
->fi_delegations
));
400 call_rcu(&fi
->fi_rcu
, nfsd4_free_file_rcu
);
405 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
407 if (f
->fi_fds
[oflag
])
408 return get_file(f
->fi_fds
[oflag
]);
413 find_writeable_file_locked(struct nfs4_file
*f
)
417 lockdep_assert_held(&f
->fi_lock
);
419 ret
= __nfs4_get_fd(f
, O_WRONLY
);
421 ret
= __nfs4_get_fd(f
, O_RDWR
);
426 find_writeable_file(struct nfs4_file
*f
)
430 spin_lock(&f
->fi_lock
);
431 ret
= find_writeable_file_locked(f
);
432 spin_unlock(&f
->fi_lock
);
437 static struct file
*find_readable_file_locked(struct nfs4_file
*f
)
441 lockdep_assert_held(&f
->fi_lock
);
443 ret
= __nfs4_get_fd(f
, O_RDONLY
);
445 ret
= __nfs4_get_fd(f
, O_RDWR
);
450 find_readable_file(struct nfs4_file
*f
)
454 spin_lock(&f
->fi_lock
);
455 ret
= find_readable_file_locked(f
);
456 spin_unlock(&f
->fi_lock
);
462 find_any_file(struct nfs4_file
*f
)
466 spin_lock(&f
->fi_lock
);
467 ret
= __nfs4_get_fd(f
, O_RDWR
);
469 ret
= __nfs4_get_fd(f
, O_WRONLY
);
471 ret
= __nfs4_get_fd(f
, O_RDONLY
);
473 spin_unlock(&f
->fi_lock
);
477 static atomic_long_t num_delegations
;
478 unsigned long max_delegations
;
481 * Open owner state (share locks)
484 /* hash tables for lock and open owners */
485 #define OWNER_HASH_BITS 8
486 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
487 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
489 static unsigned int ownerstr_hashval(struct xdr_netobj
*ownername
)
493 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
494 return ret
& OWNER_HASH_MASK
;
497 /* hash table for nfs4_file */
498 #define FILE_HASH_BITS 8
499 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
501 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
503 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
506 static unsigned int file_hashval(struct knfsd_fh
*fh
)
508 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
511 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
514 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
516 lockdep_assert_held(&fp
->fi_lock
);
518 if (access
& NFS4_SHARE_ACCESS_WRITE
)
519 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
520 if (access
& NFS4_SHARE_ACCESS_READ
)
521 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
525 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
527 lockdep_assert_held(&fp
->fi_lock
);
529 /* Does this access mode make sense? */
530 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
533 /* Does it conflict with a deny mode already set? */
534 if ((access
& fp
->fi_share_deny
) != 0)
535 return nfserr_share_denied
;
537 __nfs4_file_get_access(fp
, access
);
541 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
543 /* Common case is that there is no deny mode. */
545 /* Does this deny mode make sense? */
546 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
549 if ((deny
& NFS4_SHARE_DENY_READ
) &&
550 atomic_read(&fp
->fi_access
[O_RDONLY
]))
551 return nfserr_share_denied
;
553 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
554 atomic_read(&fp
->fi_access
[O_WRONLY
]))
555 return nfserr_share_denied
;
560 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
562 might_lock(&fp
->fi_lock
);
564 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
565 struct file
*f1
= NULL
;
566 struct file
*f2
= NULL
;
568 swap(f1
, fp
->fi_fds
[oflag
]);
569 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
570 swap(f2
, fp
->fi_fds
[O_RDWR
]);
571 spin_unlock(&fp
->fi_lock
);
579 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
581 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
583 if (access
& NFS4_SHARE_ACCESS_WRITE
)
584 __nfs4_file_put_access(fp
, O_WRONLY
);
585 if (access
& NFS4_SHARE_ACCESS_READ
)
586 __nfs4_file_put_access(fp
, O_RDONLY
);
590 * Allocate a new open/delegation state counter. This is needed for
591 * pNFS for proper return on close semantics.
593 * Note that we only allocate it for pNFS-enabled exports, otherwise
594 * all pointers to struct nfs4_clnt_odstate are always NULL.
596 static struct nfs4_clnt_odstate
*
597 alloc_clnt_odstate(struct nfs4_client
*clp
)
599 struct nfs4_clnt_odstate
*co
;
601 co
= kmem_cache_zalloc(odstate_slab
, GFP_KERNEL
);
604 atomic_set(&co
->co_odcount
, 1);
610 hash_clnt_odstate_locked(struct nfs4_clnt_odstate
*co
)
612 struct nfs4_file
*fp
= co
->co_file
;
614 lockdep_assert_held(&fp
->fi_lock
);
615 list_add(&co
->co_perfile
, &fp
->fi_clnt_odstate
);
619 get_clnt_odstate(struct nfs4_clnt_odstate
*co
)
622 atomic_inc(&co
->co_odcount
);
626 put_clnt_odstate(struct nfs4_clnt_odstate
*co
)
628 struct nfs4_file
*fp
;
634 if (atomic_dec_and_lock(&co
->co_odcount
, &fp
->fi_lock
)) {
635 list_del(&co
->co_perfile
);
636 spin_unlock(&fp
->fi_lock
);
638 nfsd4_return_all_file_layouts(co
->co_client
, fp
);
639 kmem_cache_free(odstate_slab
, co
);
643 static struct nfs4_clnt_odstate
*
644 find_or_hash_clnt_odstate(struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*new)
646 struct nfs4_clnt_odstate
*co
;
647 struct nfs4_client
*cl
;
654 spin_lock(&fp
->fi_lock
);
655 list_for_each_entry(co
, &fp
->fi_clnt_odstate
, co_perfile
) {
656 if (co
->co_client
== cl
) {
657 get_clnt_odstate(co
);
663 hash_clnt_odstate_locked(new);
665 spin_unlock(&fp
->fi_lock
);
669 struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct kmem_cache
*slab
,
670 void (*sc_free
)(struct nfs4_stid
*))
672 struct nfs4_stid
*stid
;
675 stid
= kmem_cache_zalloc(slab
, GFP_KERNEL
);
679 idr_preload(GFP_KERNEL
);
680 spin_lock(&cl
->cl_lock
);
681 new_id
= idr_alloc_cyclic(&cl
->cl_stateids
, stid
, 0, 0, GFP_NOWAIT
);
682 spin_unlock(&cl
->cl_lock
);
687 stid
->sc_free
= sc_free
;
688 stid
->sc_client
= cl
;
689 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
690 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
691 /* Will be incremented before return to client: */
692 atomic_set(&stid
->sc_count
, 1);
693 spin_lock_init(&stid
->sc_lock
);
696 * It shouldn't be a problem to reuse an opaque stateid value.
697 * I don't think it is for 4.1. But with 4.0 I worry that, for
698 * example, a stray write retransmission could be accepted by
699 * the server when it should have been rejected. Therefore,
700 * adopt a trick from the sctp code to attempt to maximize the
701 * amount of time until an id is reused, by ensuring they always
702 * "increase" (mod INT_MAX):
706 kmem_cache_free(slab
, stid
);
710 static struct nfs4_ol_stateid
* nfs4_alloc_open_stateid(struct nfs4_client
*clp
)
712 struct nfs4_stid
*stid
;
714 stid
= nfs4_alloc_stid(clp
, stateid_slab
, nfs4_free_ol_stateid
);
718 return openlockstateid(stid
);
721 static void nfs4_free_deleg(struct nfs4_stid
*stid
)
723 kmem_cache_free(deleg_slab
, stid
);
724 atomic_long_dec(&num_delegations
);
728 * When we recall a delegation, we should be careful not to hand it
729 * out again straight away.
730 * To ensure this we keep a pair of bloom filters ('new' and 'old')
731 * in which the filehandles of recalled delegations are "stored".
732 * If a filehandle appear in either filter, a delegation is blocked.
733 * When a delegation is recalled, the filehandle is stored in the "new"
735 * Every 30 seconds we swap the filters and clear the "new" one,
736 * unless both are empty of course.
738 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
739 * low 3 bytes as hash-table indices.
741 * 'blocked_delegations_lock', which is always taken in block_delegations(),
742 * is used to manage concurrent access. Testing does not need the lock
743 * except when swapping the two filters.
745 static DEFINE_SPINLOCK(blocked_delegations_lock
);
746 static struct bloom_pair
{
747 int entries
, old_entries
;
749 int new; /* index into 'set' */
750 DECLARE_BITMAP(set
[2], 256);
751 } blocked_delegations
;
753 static int delegation_blocked(struct knfsd_fh
*fh
)
756 struct bloom_pair
*bd
= &blocked_delegations
;
758 if (bd
->entries
== 0)
760 if (seconds_since_boot() - bd
->swap_time
> 30) {
761 spin_lock(&blocked_delegations_lock
);
762 if (seconds_since_boot() - bd
->swap_time
> 30) {
763 bd
->entries
-= bd
->old_entries
;
764 bd
->old_entries
= bd
->entries
;
765 memset(bd
->set
[bd
->new], 0,
768 bd
->swap_time
= seconds_since_boot();
770 spin_unlock(&blocked_delegations_lock
);
772 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
773 if (test_bit(hash
&255, bd
->set
[0]) &&
774 test_bit((hash
>>8)&255, bd
->set
[0]) &&
775 test_bit((hash
>>16)&255, bd
->set
[0]))
778 if (test_bit(hash
&255, bd
->set
[1]) &&
779 test_bit((hash
>>8)&255, bd
->set
[1]) &&
780 test_bit((hash
>>16)&255, bd
->set
[1]))
786 static void block_delegations(struct knfsd_fh
*fh
)
789 struct bloom_pair
*bd
= &blocked_delegations
;
791 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
793 spin_lock(&blocked_delegations_lock
);
794 __set_bit(hash
&255, bd
->set
[bd
->new]);
795 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
796 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
797 if (bd
->entries
== 0)
798 bd
->swap_time
= seconds_since_boot();
800 spin_unlock(&blocked_delegations_lock
);
803 static struct nfs4_delegation
*
804 alloc_init_deleg(struct nfs4_client
*clp
, struct svc_fh
*current_fh
,
805 struct nfs4_clnt_odstate
*odstate
)
807 struct nfs4_delegation
*dp
;
810 dprintk("NFSD alloc_init_deleg\n");
811 n
= atomic_long_inc_return(&num_delegations
);
812 if (n
< 0 || n
> max_delegations
)
814 if (delegation_blocked(¤t_fh
->fh_handle
))
816 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
, nfs4_free_deleg
));
821 * delegation seqid's are never incremented. The 4.1 special
822 * meaning of seqid 0 isn't meaningful, really, but let's avoid
823 * 0 anyway just for consistency and use 1:
825 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
826 INIT_LIST_HEAD(&dp
->dl_perfile
);
827 INIT_LIST_HEAD(&dp
->dl_perclnt
);
828 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
829 dp
->dl_clnt_odstate
= odstate
;
830 get_clnt_odstate(odstate
);
831 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
833 nfsd4_init_cb(&dp
->dl_recall
, dp
->dl_stid
.sc_client
,
834 &nfsd4_cb_recall_ops
, NFSPROC4_CLNT_CB_RECALL
);
837 atomic_long_dec(&num_delegations
);
842 nfs4_put_stid(struct nfs4_stid
*s
)
844 struct nfs4_file
*fp
= s
->sc_file
;
845 struct nfs4_client
*clp
= s
->sc_client
;
847 might_lock(&clp
->cl_lock
);
849 if (!atomic_dec_and_lock(&s
->sc_count
, &clp
->cl_lock
)) {
850 wake_up_all(&close_wq
);
853 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
854 spin_unlock(&clp
->cl_lock
);
861 nfs4_inc_and_copy_stateid(stateid_t
*dst
, struct nfs4_stid
*stid
)
863 stateid_t
*src
= &stid
->sc_stateid
;
865 spin_lock(&stid
->sc_lock
);
866 if (unlikely(++src
->si_generation
== 0))
867 src
->si_generation
= 1;
868 memcpy(dst
, src
, sizeof(*dst
));
869 spin_unlock(&stid
->sc_lock
);
872 static void nfs4_put_deleg_lease(struct nfs4_file
*fp
)
874 struct file
*filp
= NULL
;
876 spin_lock(&fp
->fi_lock
);
877 if (fp
->fi_deleg_file
&& --fp
->fi_delegees
== 0)
878 swap(filp
, fp
->fi_deleg_file
);
879 spin_unlock(&fp
->fi_lock
);
882 vfs_setlease(filp
, F_UNLCK
, NULL
, (void **)&fp
);
887 void nfs4_unhash_stid(struct nfs4_stid
*s
)
893 * nfs4_get_existing_delegation - Discover if this delegation already exists
894 * @clp: a pointer to the nfs4_client we're granting a delegation to
895 * @fp: a pointer to the nfs4_file we're granting a delegation on
898 * On success: NULL if an existing delegation was not found.
900 * On error: -EAGAIN if one was previously granted to this nfs4_client
901 * for this nfs4_file.
906 nfs4_get_existing_delegation(struct nfs4_client
*clp
, struct nfs4_file
*fp
)
908 struct nfs4_delegation
*searchdp
= NULL
;
909 struct nfs4_client
*searchclp
= NULL
;
911 lockdep_assert_held(&state_lock
);
912 lockdep_assert_held(&fp
->fi_lock
);
914 list_for_each_entry(searchdp
, &fp
->fi_delegations
, dl_perfile
) {
915 searchclp
= searchdp
->dl_stid
.sc_client
;
916 if (clp
== searchclp
) {
924 * hash_delegation_locked - Add a delegation to the appropriate lists
925 * @dp: a pointer to the nfs4_delegation we are adding.
926 * @fp: a pointer to the nfs4_file we're granting a delegation on
929 * On success: NULL if the delegation was successfully hashed.
931 * On error: -EAGAIN if one was previously granted to this
932 * nfs4_client for this nfs4_file. Delegation is not hashed.
937 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
940 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
942 lockdep_assert_held(&state_lock
);
943 lockdep_assert_held(&fp
->fi_lock
);
945 status
= nfs4_get_existing_delegation(clp
, fp
);
949 atomic_inc(&dp
->dl_stid
.sc_count
);
950 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
951 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
952 list_add(&dp
->dl_perclnt
, &clp
->cl_delegations
);
957 unhash_delegation_locked(struct nfs4_delegation
*dp
)
959 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
961 lockdep_assert_held(&state_lock
);
963 if (list_empty(&dp
->dl_perfile
))
966 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
967 /* Ensure that deleg break won't try to requeue it */
969 spin_lock(&fp
->fi_lock
);
970 list_del_init(&dp
->dl_perclnt
);
971 list_del_init(&dp
->dl_recall_lru
);
972 list_del_init(&dp
->dl_perfile
);
973 spin_unlock(&fp
->fi_lock
);
977 static void destroy_delegation(struct nfs4_delegation
*dp
)
981 spin_lock(&state_lock
);
982 unhashed
= unhash_delegation_locked(dp
);
983 spin_unlock(&state_lock
);
985 put_clnt_odstate(dp
->dl_clnt_odstate
);
986 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
987 nfs4_put_stid(&dp
->dl_stid
);
991 static void revoke_delegation(struct nfs4_delegation
*dp
)
993 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
995 WARN_ON(!list_empty(&dp
->dl_recall_lru
));
997 put_clnt_odstate(dp
->dl_clnt_odstate
);
998 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
1000 if (clp
->cl_minorversion
== 0)
1001 nfs4_put_stid(&dp
->dl_stid
);
1003 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
1004 spin_lock(&clp
->cl_lock
);
1005 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
1006 spin_unlock(&clp
->cl_lock
);
1014 static unsigned int clientid_hashval(u32 id
)
1016 return id
& CLIENT_HASH_MASK
;
1019 static unsigned int clientstr_hashval(const char *name
)
1021 return opaque_hashval(name
, 8) & CLIENT_HASH_MASK
;
1025 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1026 * st_{access,deny}_bmap field of the stateid, in order to track not
1027 * only what share bits are currently in force, but also what
1028 * combinations of share bits previous opens have used. This allows us
1029 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1030 * return an error if the client attempt to downgrade to a combination
1031 * of share bits not explicable by closing some of its previous opens.
1033 * XXX: This enforcement is actually incomplete, since we don't keep
1034 * track of access/deny bit combinations; so, e.g., we allow:
1036 * OPEN allow read, deny write
1037 * OPEN allow both, deny none
1038 * DOWNGRADE allow read, deny none
1040 * which we should reject.
1043 bmap_to_share_mode(unsigned long bmap
) {
1045 unsigned int access
= 0;
1047 for (i
= 1; i
< 4; i
++) {
1048 if (test_bit(i
, &bmap
))
1054 /* set share access for a given stateid */
1056 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
1058 unsigned char mask
= 1 << access
;
1060 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
1061 stp
->st_access_bmap
|= mask
;
1064 /* clear share access for a given stateid */
1066 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
1068 unsigned char mask
= 1 << access
;
1070 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
1071 stp
->st_access_bmap
&= ~mask
;
1074 /* test whether a given stateid has access */
1076 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
1078 unsigned char mask
= 1 << access
;
1080 return (bool)(stp
->st_access_bmap
& mask
);
1083 /* set share deny for a given stateid */
1085 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1087 unsigned char mask
= 1 << deny
;
1089 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
1090 stp
->st_deny_bmap
|= mask
;
1093 /* clear share deny for a given stateid */
1095 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1097 unsigned char mask
= 1 << deny
;
1099 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
1100 stp
->st_deny_bmap
&= ~mask
;
1103 /* test whether a given stateid is denying specific access */
1105 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1107 unsigned char mask
= 1 << deny
;
1109 return (bool)(stp
->st_deny_bmap
& mask
);
1112 static int nfs4_access_to_omode(u32 access
)
1114 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
1115 case NFS4_SHARE_ACCESS_READ
:
1117 case NFS4_SHARE_ACCESS_WRITE
:
1119 case NFS4_SHARE_ACCESS_BOTH
:
1127 * A stateid that had a deny mode associated with it is being released
1128 * or downgraded. Recalculate the deny mode on the file.
1131 recalculate_deny_mode(struct nfs4_file
*fp
)
1133 struct nfs4_ol_stateid
*stp
;
1135 spin_lock(&fp
->fi_lock
);
1136 fp
->fi_share_deny
= 0;
1137 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
1138 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
1139 spin_unlock(&fp
->fi_lock
);
1143 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1146 bool change
= false;
1148 for (i
= 1; i
< 4; i
++) {
1149 if ((i
& deny
) != i
) {
1155 /* Recalculate per-file deny mode if there was a change */
1157 recalculate_deny_mode(stp
->st_stid
.sc_file
);
1160 /* release all access and file references for a given stateid */
1162 release_all_access(struct nfs4_ol_stateid
*stp
)
1165 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
1167 if (fp
&& stp
->st_deny_bmap
!= 0)
1168 recalculate_deny_mode(fp
);
1170 for (i
= 1; i
< 4; i
++) {
1171 if (test_access(i
, stp
))
1172 nfs4_file_put_access(stp
->st_stid
.sc_file
, i
);
1173 clear_access(i
, stp
);
1177 static inline void nfs4_free_stateowner(struct nfs4_stateowner
*sop
)
1179 kfree(sop
->so_owner
.data
);
1180 sop
->so_ops
->so_free(sop
);
1183 static void nfs4_put_stateowner(struct nfs4_stateowner
*sop
)
1185 struct nfs4_client
*clp
= sop
->so_client
;
1187 might_lock(&clp
->cl_lock
);
1189 if (!atomic_dec_and_lock(&sop
->so_count
, &clp
->cl_lock
))
1191 sop
->so_ops
->so_unhash(sop
);
1192 spin_unlock(&clp
->cl_lock
);
1193 nfs4_free_stateowner(sop
);
1196 static bool unhash_ol_stateid(struct nfs4_ol_stateid
*stp
)
1198 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
1200 lockdep_assert_held(&stp
->st_stateowner
->so_client
->cl_lock
);
1202 if (list_empty(&stp
->st_perfile
))
1205 spin_lock(&fp
->fi_lock
);
1206 list_del_init(&stp
->st_perfile
);
1207 spin_unlock(&fp
->fi_lock
);
1208 list_del(&stp
->st_perstateowner
);
1212 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
)
1214 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1216 put_clnt_odstate(stp
->st_clnt_odstate
);
1217 release_all_access(stp
);
1218 if (stp
->st_stateowner
)
1219 nfs4_put_stateowner(stp
->st_stateowner
);
1220 kmem_cache_free(stateid_slab
, stid
);
1223 static void nfs4_free_lock_stateid(struct nfs4_stid
*stid
)
1225 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1226 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
1229 file
= find_any_file(stp
->st_stid
.sc_file
);
1231 filp_close(file
, (fl_owner_t
)lo
);
1232 nfs4_free_ol_stateid(stid
);
1236 * Put the persistent reference to an already unhashed generic stateid, while
1237 * holding the cl_lock. If it's the last reference, then put it onto the
1238 * reaplist for later destruction.
1240 static void put_ol_stateid_locked(struct nfs4_ol_stateid
*stp
,
1241 struct list_head
*reaplist
)
1243 struct nfs4_stid
*s
= &stp
->st_stid
;
1244 struct nfs4_client
*clp
= s
->sc_client
;
1246 lockdep_assert_held(&clp
->cl_lock
);
1248 WARN_ON_ONCE(!list_empty(&stp
->st_locks
));
1250 if (!atomic_dec_and_test(&s
->sc_count
)) {
1251 wake_up_all(&close_wq
);
1255 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
1256 list_add(&stp
->st_locks
, reaplist
);
1259 static bool unhash_lock_stateid(struct nfs4_ol_stateid
*stp
)
1261 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1263 list_del_init(&stp
->st_locks
);
1264 nfs4_unhash_stid(&stp
->st_stid
);
1265 return unhash_ol_stateid(stp
);
1268 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
1270 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
1273 spin_lock(&clp
->cl_lock
);
1274 unhashed
= unhash_lock_stateid(stp
);
1275 spin_unlock(&clp
->cl_lock
);
1277 nfs4_put_stid(&stp
->st_stid
);
1280 static void unhash_lockowner_locked(struct nfs4_lockowner
*lo
)
1282 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1284 lockdep_assert_held(&clp
->cl_lock
);
1286 list_del_init(&lo
->lo_owner
.so_strhash
);
1290 * Free a list of generic stateids that were collected earlier after being
1294 free_ol_stateid_reaplist(struct list_head
*reaplist
)
1296 struct nfs4_ol_stateid
*stp
;
1297 struct nfs4_file
*fp
;
1301 while (!list_empty(reaplist
)) {
1302 stp
= list_first_entry(reaplist
, struct nfs4_ol_stateid
,
1304 list_del(&stp
->st_locks
);
1305 fp
= stp
->st_stid
.sc_file
;
1306 stp
->st_stid
.sc_free(&stp
->st_stid
);
1312 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
,
1313 struct list_head
*reaplist
)
1315 struct nfs4_ol_stateid
*stp
;
1317 lockdep_assert_held(&open_stp
->st_stid
.sc_client
->cl_lock
);
1319 while (!list_empty(&open_stp
->st_locks
)) {
1320 stp
= list_entry(open_stp
->st_locks
.next
,
1321 struct nfs4_ol_stateid
, st_locks
);
1322 WARN_ON(!unhash_lock_stateid(stp
));
1323 put_ol_stateid_locked(stp
, reaplist
);
1327 static bool unhash_open_stateid(struct nfs4_ol_stateid
*stp
,
1328 struct list_head
*reaplist
)
1332 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1334 unhashed
= unhash_ol_stateid(stp
);
1335 release_open_stateid_locks(stp
, reaplist
);
1339 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
1341 LIST_HEAD(reaplist
);
1343 spin_lock(&stp
->st_stid
.sc_client
->cl_lock
);
1344 if (unhash_open_stateid(stp
, &reaplist
))
1345 put_ol_stateid_locked(stp
, &reaplist
);
1346 spin_unlock(&stp
->st_stid
.sc_client
->cl_lock
);
1347 free_ol_stateid_reaplist(&reaplist
);
1350 static void unhash_openowner_locked(struct nfs4_openowner
*oo
)
1352 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1354 lockdep_assert_held(&clp
->cl_lock
);
1356 list_del_init(&oo
->oo_owner
.so_strhash
);
1357 list_del_init(&oo
->oo_perclient
);
1360 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
1362 struct nfsd_net
*nn
= net_generic(oo
->oo_owner
.so_client
->net
,
1364 struct nfs4_ol_stateid
*s
;
1366 spin_lock(&nn
->client_lock
);
1367 s
= oo
->oo_last_closed_stid
;
1369 list_del_init(&oo
->oo_close_lru
);
1370 oo
->oo_last_closed_stid
= NULL
;
1372 spin_unlock(&nn
->client_lock
);
1374 nfs4_put_stid(&s
->st_stid
);
1377 static void release_openowner(struct nfs4_openowner
*oo
)
1379 struct nfs4_ol_stateid
*stp
;
1380 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1381 struct list_head reaplist
;
1383 INIT_LIST_HEAD(&reaplist
);
1385 spin_lock(&clp
->cl_lock
);
1386 unhash_openowner_locked(oo
);
1387 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
1388 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
1389 struct nfs4_ol_stateid
, st_perstateowner
);
1390 if (unhash_open_stateid(stp
, &reaplist
))
1391 put_ol_stateid_locked(stp
, &reaplist
);
1393 spin_unlock(&clp
->cl_lock
);
1394 free_ol_stateid_reaplist(&reaplist
);
1395 release_last_closed_stateid(oo
);
1396 nfs4_put_stateowner(&oo
->oo_owner
);
1400 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1402 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1404 return sid
->sequence
% SESSION_HASH_SIZE
;
1407 #ifdef CONFIG_SUNRPC_DEBUG
1409 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1411 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1412 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1416 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1422 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1423 * won't be used for replay.
1425 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1427 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1429 if (nfserr
== nfserr_replay_me
)
1432 if (!seqid_mutating_err(ntohl(nfserr
))) {
1433 nfsd4_cstate_clear_replay(cstate
);
1438 if (so
->so_is_open_owner
)
1439 release_last_closed_stateid(openowner(so
));
1445 gen_sessionid(struct nfsd4_session
*ses
)
1447 struct nfs4_client
*clp
= ses
->se_client
;
1448 struct nfsd4_sessionid
*sid
;
1450 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1451 sid
->clientid
= clp
->cl_clientid
;
1452 sid
->sequence
= current_sessionid
++;
1457 * The protocol defines ca_maxresponssize_cached to include the size of
1458 * the rpc header, but all we need to cache is the data starting after
1459 * the end of the initial SEQUENCE operation--the rest we regenerate
1460 * each time. Therefore we can advertise a ca_maxresponssize_cached
1461 * value that is the number of bytes in our cache plus a few additional
1462 * bytes. In order to stay on the safe side, and not promise more than
1463 * we can cache, those additional bytes must be the minimum possible: 24
1464 * bytes of rpc header (xid through accept state, with AUTH_NULL
1465 * verifier), 12 for the compound header (with zero-length tag), and 44
1466 * for the SEQUENCE op response:
1468 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1471 free_session_slots(struct nfsd4_session
*ses
)
1475 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++)
1476 kfree(ses
->se_slots
[i
]);
1480 * We don't actually need to cache the rpc and session headers, so we
1481 * can allocate a little less for each slot:
1483 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1487 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1490 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1491 return size
+ sizeof(struct nfsd4_slot
);
1495 * XXX: If we run out of reserved DRC memory we could (up to a point)
1496 * re-negotiate active sessions and reduce their slot usage to make
1497 * room for new connections. For now we just fail the create session.
1499 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1501 u32 slotsize
= slot_bytes(ca
);
1502 u32 num
= ca
->maxreqs
;
1505 spin_lock(&nfsd_drc_lock
);
1506 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1507 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1508 num
= min_t(int, num
, avail
/ slotsize
);
1509 nfsd_drc_mem_used
+= num
* slotsize
;
1510 spin_unlock(&nfsd_drc_lock
);
1515 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1517 int slotsize
= slot_bytes(ca
);
1519 spin_lock(&nfsd_drc_lock
);
1520 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1521 spin_unlock(&nfsd_drc_lock
);
1524 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1525 struct nfsd4_channel_attrs
*battrs
)
1527 int numslots
= fattrs
->maxreqs
;
1528 int slotsize
= slot_bytes(fattrs
);
1529 struct nfsd4_session
*new;
1532 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1533 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1534 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1536 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1539 /* allocate each struct nfsd4_slot and data cache in one piece */
1540 for (i
= 0; i
< numslots
; i
++) {
1541 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1542 if (!new->se_slots
[i
])
1546 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1547 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1552 kfree(new->se_slots
[i
]);
1557 static void free_conn(struct nfsd4_conn
*c
)
1559 svc_xprt_put(c
->cn_xprt
);
1563 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1565 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1566 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1568 spin_lock(&clp
->cl_lock
);
1569 if (!list_empty(&c
->cn_persession
)) {
1570 list_del(&c
->cn_persession
);
1573 nfsd4_probe_callback(clp
);
1574 spin_unlock(&clp
->cl_lock
);
1577 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1579 struct nfsd4_conn
*conn
;
1581 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1584 svc_xprt_get(rqstp
->rq_xprt
);
1585 conn
->cn_xprt
= rqstp
->rq_xprt
;
1586 conn
->cn_flags
= flags
;
1587 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1591 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1593 conn
->cn_session
= ses
;
1594 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1597 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1599 struct nfs4_client
*clp
= ses
->se_client
;
1601 spin_lock(&clp
->cl_lock
);
1602 __nfsd4_hash_conn(conn
, ses
);
1603 spin_unlock(&clp
->cl_lock
);
1606 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1608 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1609 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1612 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1616 nfsd4_hash_conn(conn
, ses
);
1617 ret
= nfsd4_register_conn(conn
);
1619 /* oops; xprt is already down: */
1620 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1621 /* We may have gained or lost a callback channel: */
1622 nfsd4_probe_callback_sync(ses
->se_client
);
1625 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1627 u32 dir
= NFS4_CDFC4_FORE
;
1629 if (cses
->flags
& SESSION4_BACK_CHAN
)
1630 dir
|= NFS4_CDFC4_BACK
;
1631 return alloc_conn(rqstp
, dir
);
1634 /* must be called under client_lock */
1635 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1637 struct nfs4_client
*clp
= s
->se_client
;
1638 struct nfsd4_conn
*c
;
1640 spin_lock(&clp
->cl_lock
);
1641 while (!list_empty(&s
->se_conns
)) {
1642 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1643 list_del_init(&c
->cn_persession
);
1644 spin_unlock(&clp
->cl_lock
);
1646 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1649 spin_lock(&clp
->cl_lock
);
1651 spin_unlock(&clp
->cl_lock
);
1654 static void __free_session(struct nfsd4_session
*ses
)
1656 free_session_slots(ses
);
1660 static void free_session(struct nfsd4_session
*ses
)
1662 nfsd4_del_conns(ses
);
1663 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1664 __free_session(ses
);
1667 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1670 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1672 new->se_client
= clp
;
1675 INIT_LIST_HEAD(&new->se_conns
);
1677 new->se_cb_seq_nr
= 1;
1678 new->se_flags
= cses
->flags
;
1679 new->se_cb_prog
= cses
->callback_prog
;
1680 new->se_cb_sec
= cses
->cb_sec
;
1681 atomic_set(&new->se_ref
, 0);
1682 idx
= hash_sessionid(&new->se_sessionid
);
1683 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1684 spin_lock(&clp
->cl_lock
);
1685 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1686 spin_unlock(&clp
->cl_lock
);
1689 struct sockaddr
*sa
= svc_addr(rqstp
);
1691 * This is a little silly; with sessions there's no real
1692 * use for the callback address. Use the peer address
1693 * as a reasonable default for now, but consider fixing
1694 * the rpc client not to require an address in the
1697 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1698 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1702 /* caller must hold client_lock */
1703 static struct nfsd4_session
*
1704 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1706 struct nfsd4_session
*elem
;
1708 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1710 lockdep_assert_held(&nn
->client_lock
);
1712 dump_sessionid(__func__
, sessionid
);
1713 idx
= hash_sessionid(sessionid
);
1714 /* Search in the appropriate list */
1715 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1716 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1717 NFS4_MAX_SESSIONID_LEN
)) {
1722 dprintk("%s: session not found\n", __func__
);
1726 static struct nfsd4_session
*
1727 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1730 struct nfsd4_session
*session
;
1731 __be32 status
= nfserr_badsession
;
1733 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1736 status
= nfsd4_get_session_locked(session
);
1744 /* caller must hold client_lock */
1746 unhash_session(struct nfsd4_session
*ses
)
1748 struct nfs4_client
*clp
= ses
->se_client
;
1749 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1751 lockdep_assert_held(&nn
->client_lock
);
1753 list_del(&ses
->se_hash
);
1754 spin_lock(&ses
->se_client
->cl_lock
);
1755 list_del(&ses
->se_perclnt
);
1756 spin_unlock(&ses
->se_client
->cl_lock
);
1759 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1761 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1764 * We're assuming the clid was not given out from a boot
1765 * precisely 2^32 (about 136 years) before this one. That seems
1766 * a safe assumption:
1768 if (clid
->cl_boot
== (u32
)nn
->boot_time
)
1770 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1771 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1776 * XXX Should we use a slab cache ?
1777 * This type of memory management is somewhat inefficient, but we use it
1778 * anyway since SETCLIENTID is not a common operation.
1780 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1782 struct nfs4_client
*clp
;
1785 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1788 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1789 if (clp
->cl_name
.data
== NULL
)
1791 clp
->cl_ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
1792 OWNER_HASH_SIZE
, GFP_KERNEL
);
1793 if (!clp
->cl_ownerstr_hashtbl
)
1794 goto err_no_hashtbl
;
1795 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
1796 INIT_LIST_HEAD(&clp
->cl_ownerstr_hashtbl
[i
]);
1797 clp
->cl_name
.len
= name
.len
;
1798 INIT_LIST_HEAD(&clp
->cl_sessions
);
1799 idr_init(&clp
->cl_stateids
);
1800 atomic_set(&clp
->cl_refcount
, 0);
1801 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1802 INIT_LIST_HEAD(&clp
->cl_idhash
);
1803 INIT_LIST_HEAD(&clp
->cl_openowners
);
1804 INIT_LIST_HEAD(&clp
->cl_delegations
);
1805 INIT_LIST_HEAD(&clp
->cl_lru
);
1806 INIT_LIST_HEAD(&clp
->cl_revoked
);
1807 #ifdef CONFIG_NFSD_PNFS
1808 INIT_LIST_HEAD(&clp
->cl_lo_states
);
1810 spin_lock_init(&clp
->cl_lock
);
1811 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1814 kfree(clp
->cl_name
.data
);
1821 free_client(struct nfs4_client
*clp
)
1823 while (!list_empty(&clp
->cl_sessions
)) {
1824 struct nfsd4_session
*ses
;
1825 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1827 list_del(&ses
->se_perclnt
);
1828 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1831 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1832 free_svc_cred(&clp
->cl_cred
);
1833 kfree(clp
->cl_ownerstr_hashtbl
);
1834 kfree(clp
->cl_name
.data
);
1835 idr_destroy(&clp
->cl_stateids
);
1839 /* must be called under the client_lock */
1841 unhash_client_locked(struct nfs4_client
*clp
)
1843 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1844 struct nfsd4_session
*ses
;
1846 lockdep_assert_held(&nn
->client_lock
);
1848 /* Mark the client as expired! */
1850 /* Make it invisible */
1851 if (!list_empty(&clp
->cl_idhash
)) {
1852 list_del_init(&clp
->cl_idhash
);
1853 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1854 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1856 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1858 list_del_init(&clp
->cl_lru
);
1859 spin_lock(&clp
->cl_lock
);
1860 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1861 list_del_init(&ses
->se_hash
);
1862 spin_unlock(&clp
->cl_lock
);
1866 unhash_client(struct nfs4_client
*clp
)
1868 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1870 spin_lock(&nn
->client_lock
);
1871 unhash_client_locked(clp
);
1872 spin_unlock(&nn
->client_lock
);
1875 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
1877 if (atomic_read(&clp
->cl_refcount
))
1878 return nfserr_jukebox
;
1879 unhash_client_locked(clp
);
1884 __destroy_client(struct nfs4_client
*clp
)
1887 struct nfs4_openowner
*oo
;
1888 struct nfs4_delegation
*dp
;
1889 struct list_head reaplist
;
1891 INIT_LIST_HEAD(&reaplist
);
1892 spin_lock(&state_lock
);
1893 while (!list_empty(&clp
->cl_delegations
)) {
1894 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1895 WARN_ON(!unhash_delegation_locked(dp
));
1896 list_add(&dp
->dl_recall_lru
, &reaplist
);
1898 spin_unlock(&state_lock
);
1899 while (!list_empty(&reaplist
)) {
1900 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1901 list_del_init(&dp
->dl_recall_lru
);
1902 put_clnt_odstate(dp
->dl_clnt_odstate
);
1903 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
1904 nfs4_put_stid(&dp
->dl_stid
);
1906 while (!list_empty(&clp
->cl_revoked
)) {
1907 dp
= list_entry(clp
->cl_revoked
.next
, struct nfs4_delegation
, dl_recall_lru
);
1908 list_del_init(&dp
->dl_recall_lru
);
1909 nfs4_put_stid(&dp
->dl_stid
);
1911 while (!list_empty(&clp
->cl_openowners
)) {
1912 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1913 nfs4_get_stateowner(&oo
->oo_owner
);
1914 release_openowner(oo
);
1916 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++) {
1917 struct nfs4_stateowner
*so
, *tmp
;
1919 list_for_each_entry_safe(so
, tmp
, &clp
->cl_ownerstr_hashtbl
[i
],
1921 /* Should be no openowners at this point */
1922 WARN_ON_ONCE(so
->so_is_open_owner
);
1923 remove_blocked_locks(lockowner(so
));
1926 nfsd4_return_all_client_layouts(clp
);
1927 nfsd4_shutdown_callback(clp
);
1928 if (clp
->cl_cb_conn
.cb_xprt
)
1929 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1934 destroy_client(struct nfs4_client
*clp
)
1937 __destroy_client(clp
);
1940 static void expire_client(struct nfs4_client
*clp
)
1943 nfsd4_client_record_remove(clp
);
1944 __destroy_client(clp
);
1947 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1949 memcpy(target
->cl_verifier
.data
, source
->data
,
1950 sizeof(target
->cl_verifier
.data
));
1953 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1955 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1956 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1959 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1961 target
->cr_principal
= kstrdup(source
->cr_principal
, GFP_KERNEL
);
1962 target
->cr_raw_principal
= kstrdup(source
->cr_raw_principal
,
1964 if ((source
->cr_principal
&& ! target
->cr_principal
) ||
1965 (source
->cr_raw_principal
&& ! target
->cr_raw_principal
))
1968 target
->cr_flavor
= source
->cr_flavor
;
1969 target
->cr_uid
= source
->cr_uid
;
1970 target
->cr_gid
= source
->cr_gid
;
1971 target
->cr_group_info
= source
->cr_group_info
;
1972 get_group_info(target
->cr_group_info
);
1973 target
->cr_gss_mech
= source
->cr_gss_mech
;
1974 if (source
->cr_gss_mech
)
1975 gss_mech_get(source
->cr_gss_mech
);
1980 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1982 if (o1
->len
< o2
->len
)
1984 if (o1
->len
> o2
->len
)
1986 return memcmp(o1
->data
, o2
->data
, o1
->len
);
1989 static int same_name(const char *n1
, const char *n2
)
1991 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1995 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1997 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
2001 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
2003 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
2006 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
2010 if (g1
->ngroups
!= g2
->ngroups
)
2012 for (i
=0; i
<g1
->ngroups
; i
++)
2013 if (!gid_eq(g1
->gid
[i
], g2
->gid
[i
]))
2019 * RFC 3530 language requires clid_inuse be returned when the
2020 * "principal" associated with a requests differs from that previously
2021 * used. We use uid, gid's, and gss principal string as our best
2022 * approximation. We also don't want to allow non-gss use of a client
2023 * established using gss: in theory cr_principal should catch that
2024 * change, but in practice cr_principal can be null even in the gss case
2025 * since gssd doesn't always pass down a principal string.
2027 static bool is_gss_cred(struct svc_cred
*cr
)
2029 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2030 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
2035 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
2037 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
2038 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
2039 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
2040 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
2042 if (cr1
->cr_principal
== cr2
->cr_principal
)
2044 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
2046 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
2049 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
2051 struct svc_cred
*cr
= &rqstp
->rq_cred
;
2054 if (!cr
->cr_gss_mech
)
2056 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
2057 return service
== RPC_GSS_SVC_INTEGRITY
||
2058 service
== RPC_GSS_SVC_PRIVACY
;
2061 bool nfsd4_mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
2063 struct svc_cred
*cr
= &rqstp
->rq_cred
;
2065 if (!cl
->cl_mach_cred
)
2067 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
2069 if (!svc_rqst_integrity_protected(rqstp
))
2071 if (cl
->cl_cred
.cr_raw_principal
)
2072 return 0 == strcmp(cl
->cl_cred
.cr_raw_principal
,
2073 cr
->cr_raw_principal
);
2074 if (!cr
->cr_principal
)
2076 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
2079 static void gen_confirm(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
2084 * This is opaque to client, so no need to byte-swap. Use
2085 * __force to keep sparse happy
2087 verf
[0] = (__force __be32
)get_seconds();
2088 verf
[1] = (__force __be32
)nn
->clverifier_counter
++;
2089 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
2092 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
2094 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
2095 clp
->cl_clientid
.cl_id
= nn
->clientid_counter
++;
2096 gen_confirm(clp
, nn
);
2099 static struct nfs4_stid
*
2100 find_stateid_locked(struct nfs4_client
*cl
, stateid_t
*t
)
2102 struct nfs4_stid
*ret
;
2104 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
2105 if (!ret
|| !ret
->sc_type
)
2110 static struct nfs4_stid
*
2111 find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
2113 struct nfs4_stid
*s
;
2115 spin_lock(&cl
->cl_lock
);
2116 s
= find_stateid_locked(cl
, t
);
2118 if (typemask
& s
->sc_type
)
2119 atomic_inc(&s
->sc_count
);
2123 spin_unlock(&cl
->cl_lock
);
2127 static struct nfs4_client
*create_client(struct xdr_netobj name
,
2128 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
2130 struct nfs4_client
*clp
;
2131 struct sockaddr
*sa
= svc_addr(rqstp
);
2133 struct net
*net
= SVC_NET(rqstp
);
2135 clp
= alloc_client(name
);
2139 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
2144 nfsd4_init_cb(&clp
->cl_cb_null
, clp
, NULL
, NFSPROC4_CLNT_CB_NULL
);
2145 clp
->cl_time
= get_seconds();
2146 clear_bit(0, &clp
->cl_cb_slot_busy
);
2147 copy_verf(clp
, verf
);
2148 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
2149 clp
->cl_cb_session
= NULL
;
2155 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
2157 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
2158 struct nfs4_client
*clp
;
2161 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
2164 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
2165 new = &((*new)->rb_left
);
2167 new = &((*new)->rb_right
);
2170 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
2171 rb_insert_color(&new_clp
->cl_namenode
, root
);
2174 static struct nfs4_client
*
2175 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
2178 struct rb_node
*node
= root
->rb_node
;
2179 struct nfs4_client
*clp
;
2182 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
2183 cmp
= compare_blob(&clp
->cl_name
, name
);
2185 node
= node
->rb_left
;
2187 node
= node
->rb_right
;
2195 add_to_unconfirmed(struct nfs4_client
*clp
)
2197 unsigned int idhashval
;
2198 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2200 lockdep_assert_held(&nn
->client_lock
);
2202 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2203 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
2204 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2205 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
2206 renew_client_locked(clp
);
2210 move_to_confirmed(struct nfs4_client
*clp
)
2212 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2213 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2215 lockdep_assert_held(&nn
->client_lock
);
2217 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
2218 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
2219 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
2220 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
2221 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2222 renew_client_locked(clp
);
2225 static struct nfs4_client
*
2226 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
2228 struct nfs4_client
*clp
;
2229 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
2231 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
2232 if (same_clid(&clp
->cl_clientid
, clid
)) {
2233 if ((bool)clp
->cl_minorversion
!= sessions
)
2235 renew_client_locked(clp
);
2242 static struct nfs4_client
*
2243 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2245 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
2247 lockdep_assert_held(&nn
->client_lock
);
2248 return find_client_in_id_table(tbl
, clid
, sessions
);
2251 static struct nfs4_client
*
2252 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2254 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
2256 lockdep_assert_held(&nn
->client_lock
);
2257 return find_client_in_id_table(tbl
, clid
, sessions
);
2260 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
2262 return clp
->cl_exchange_flags
!= 0;
2265 static struct nfs4_client
*
2266 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2268 lockdep_assert_held(&nn
->client_lock
);
2269 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
2272 static struct nfs4_client
*
2273 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2275 lockdep_assert_held(&nn
->client_lock
);
2276 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
2280 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
2282 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
2283 struct sockaddr
*sa
= svc_addr(rqstp
);
2284 u32 scopeid
= rpc_get_scope_id(sa
);
2285 unsigned short expected_family
;
2287 /* Currently, we only support tcp and tcp6 for the callback channel */
2288 if (se
->se_callback_netid_len
== 3 &&
2289 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
2290 expected_family
= AF_INET
;
2291 else if (se
->se_callback_netid_len
== 4 &&
2292 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
2293 expected_family
= AF_INET6
;
2297 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
2298 se
->se_callback_addr_len
,
2299 (struct sockaddr
*)&conn
->cb_addr
,
2300 sizeof(conn
->cb_addr
));
2302 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
2305 if (conn
->cb_addr
.ss_family
== AF_INET6
)
2306 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
2308 conn
->cb_prog
= se
->se_callback_prog
;
2309 conn
->cb_ident
= se
->se_callback_ident
;
2310 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
2313 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
2314 conn
->cb_addrlen
= 0;
2315 dprintk("NFSD: this client (clientid %08x/%08x) "
2316 "will not receive delegations\n",
2317 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
2323 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2326 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
2328 struct xdr_buf
*buf
= resp
->xdr
.buf
;
2329 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2332 dprintk("--> %s slot %p\n", __func__
, slot
);
2334 slot
->sl_opcnt
= resp
->opcnt
;
2335 slot
->sl_status
= resp
->cstate
.status
;
2337 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
2338 if (nfsd4_not_cached(resp
)) {
2339 slot
->sl_datalen
= 0;
2342 base
= resp
->cstate
.data_offset
;
2343 slot
->sl_datalen
= buf
->len
- base
;
2344 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
2345 WARN(1, "%s: sessions DRC could not cache compound\n",
2351 * Encode the replay sequence operation from the slot values.
2352 * If cachethis is FALSE encode the uncached rep error on the next
2353 * operation which sets resp->p and increments resp->opcnt for
2354 * nfs4svc_encode_compoundres.
2358 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
2359 struct nfsd4_compoundres
*resp
)
2361 struct nfsd4_op
*op
;
2362 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2364 /* Encode the replayed sequence operation */
2365 op
= &args
->ops
[resp
->opcnt
- 1];
2366 nfsd4_encode_operation(resp
, op
);
2368 /* Return nfserr_retry_uncached_rep in next operation. */
2369 if (args
->opcnt
> 1 && !(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
)) {
2370 op
= &args
->ops
[resp
->opcnt
++];
2371 op
->status
= nfserr_retry_uncached_rep
;
2372 nfsd4_encode_operation(resp
, op
);
2378 * The sequence operation is not cached because we can use the slot and
2382 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
2383 struct nfsd4_sequence
*seq
)
2385 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2386 struct xdr_stream
*xdr
= &resp
->xdr
;
2390 dprintk("--> %s slot %p\n", __func__
, slot
);
2392 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
2396 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
2399 return nfserr_serverfault
;
2401 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
2402 xdr_commit_encode(xdr
);
2404 resp
->opcnt
= slot
->sl_opcnt
;
2405 return slot
->sl_status
;
2409 * Set the exchange_id flags returned by the server.
2412 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
2414 #ifdef CONFIG_NFSD_PNFS
2415 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_PNFS_MDS
;
2417 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
2420 /* Referrals are supported, Migration is not. */
2421 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
2423 /* set the wire flags to return to client. */
2424 clid
->flags
= new->cl_exchange_flags
;
2427 static bool client_has_openowners(struct nfs4_client
*clp
)
2429 struct nfs4_openowner
*oo
;
2431 list_for_each_entry(oo
, &clp
->cl_openowners
, oo_perclient
) {
2432 if (!list_empty(&oo
->oo_owner
.so_stateids
))
2438 static bool client_has_state(struct nfs4_client
*clp
)
2440 return client_has_openowners(clp
)
2441 #ifdef CONFIG_NFSD_PNFS
2442 || !list_empty(&clp
->cl_lo_states
)
2444 || !list_empty(&clp
->cl_delegations
)
2445 || !list_empty(&clp
->cl_sessions
);
2449 nfsd4_exchange_id(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2450 union nfsd4_op_u
*u
)
2452 struct nfsd4_exchange_id
*exid
= &u
->exchange_id
;
2453 struct nfs4_client
*conf
, *new;
2454 struct nfs4_client
*unconf
= NULL
;
2456 char addr_str
[INET6_ADDRSTRLEN
];
2457 nfs4_verifier verf
= exid
->verifier
;
2458 struct sockaddr
*sa
= svc_addr(rqstp
);
2459 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
2460 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2462 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2463 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2464 "ip_addr=%s flags %x, spa_how %d\n",
2465 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2466 addr_str
, exid
->flags
, exid
->spa_how
);
2468 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2469 return nfserr_inval
;
2471 new = create_client(exid
->clname
, rqstp
, &verf
);
2473 return nfserr_jukebox
;
2475 switch (exid
->spa_how
) {
2477 exid
->spo_must_enforce
[0] = 0;
2478 exid
->spo_must_enforce
[1] = (
2479 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
2480 1 << (OP_EXCHANGE_ID
- 32) |
2481 1 << (OP_CREATE_SESSION
- 32) |
2482 1 << (OP_DESTROY_SESSION
- 32) |
2483 1 << (OP_DESTROY_CLIENTID
- 32));
2485 exid
->spo_must_allow
[0] &= (1 << (OP_CLOSE
) |
2486 1 << (OP_OPEN_DOWNGRADE
) |
2488 1 << (OP_DELEGRETURN
));
2490 exid
->spo_must_allow
[1] &= (
2491 1 << (OP_TEST_STATEID
- 32) |
2492 1 << (OP_FREE_STATEID
- 32));
2493 if (!svc_rqst_integrity_protected(rqstp
)) {
2494 status
= nfserr_inval
;
2498 * Sometimes userspace doesn't give us a principal.
2499 * Which is a bug, really. Anyway, we can't enforce
2500 * MACH_CRED in that case, better to give up now:
2502 if (!new->cl_cred
.cr_principal
&&
2503 !new->cl_cred
.cr_raw_principal
) {
2504 status
= nfserr_serverfault
;
2507 new->cl_mach_cred
= true;
2510 default: /* checked by xdr code */
2513 status
= nfserr_encr_alg_unsupp
;
2517 /* Cases below refer to rfc 5661 section 18.35.4: */
2518 spin_lock(&nn
->client_lock
);
2519 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2521 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2522 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2525 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2526 status
= nfserr_inval
;
2529 if (!nfsd4_mach_creds_match(conf
, rqstp
)) {
2530 status
= nfserr_wrong_cred
;
2533 if (!creds_match
) { /* case 9 */
2534 status
= nfserr_perm
;
2537 if (!verfs_match
) { /* case 8 */
2538 status
= nfserr_not_same
;
2542 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2545 if (!creds_match
) { /* case 3 */
2546 if (client_has_state(conf
)) {
2547 status
= nfserr_clid_inuse
;
2552 if (verfs_match
) { /* case 2 */
2553 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2556 /* case 5, client reboot */
2561 if (update
) { /* case 7 */
2562 status
= nfserr_noent
;
2566 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2567 if (unconf
) /* case 4, possible retry or client restart */
2568 unhash_client_locked(unconf
);
2570 /* case 1 (normal case) */
2573 status
= mark_client_expired_locked(conf
);
2577 new->cl_minorversion
= cstate
->minorversion
;
2578 new->cl_spo_must_allow
.u
.words
[0] = exid
->spo_must_allow
[0];
2579 new->cl_spo_must_allow
.u
.words
[1] = exid
->spo_must_allow
[1];
2582 add_to_unconfirmed(new);
2585 exid
->clientid
.cl_boot
= conf
->cl_clientid
.cl_boot
;
2586 exid
->clientid
.cl_id
= conf
->cl_clientid
.cl_id
;
2588 exid
->seqid
= conf
->cl_cs_slot
.sl_seqid
+ 1;
2589 nfsd4_set_ex_flags(conf
, exid
);
2591 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2592 conf
->cl_cs_slot
.sl_seqid
, conf
->cl_exchange_flags
);
2596 spin_unlock(&nn
->client_lock
);
2601 expire_client(unconf
);
2606 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2608 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2611 /* The slot is in use, and no response has been sent. */
2613 if (seqid
== slot_seqid
)
2614 return nfserr_jukebox
;
2616 return nfserr_seq_misordered
;
2618 /* Note unsigned 32-bit arithmetic handles wraparound: */
2619 if (likely(seqid
== slot_seqid
+ 1))
2621 if (seqid
== slot_seqid
)
2622 return nfserr_replay_cache
;
2623 return nfserr_seq_misordered
;
2627 * Cache the create session result into the create session single DRC
2628 * slot cache by saving the xdr structure. sl_seqid has been set.
2629 * Do this for solo or embedded create session operations.
2632 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2633 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2635 slot
->sl_status
= nfserr
;
2636 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2640 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2641 struct nfsd4_clid_slot
*slot
)
2643 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2644 return slot
->sl_status
;
2647 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2648 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2649 1 + /* MIN tag is length with zero, only length */ \
2650 3 + /* version, opcount, opcode */ \
2651 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2652 /* seqid, slotID, slotID, cache */ \
2653 4 ) * sizeof(__be32))
2655 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2656 2 + /* verifier: AUTH_NULL, length 0 */\
2658 1 + /* MIN tag is length with zero, only length */ \
2659 3 + /* opcount, opcode, opstatus*/ \
2660 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2661 /* seqid, slotID, slotID, slotID, status */ \
2662 5 ) * sizeof(__be32))
2664 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2666 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2668 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2669 return nfserr_toosmall
;
2670 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2671 return nfserr_toosmall
;
2672 ca
->headerpadsz
= 0;
2673 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2674 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2675 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2676 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2677 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2678 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2680 * Note decreasing slot size below client's request may make it
2681 * difficult for client to function correctly, whereas
2682 * decreasing the number of slots will (just?) affect
2683 * performance. When short on memory we therefore prefer to
2684 * decrease number of slots instead of their size. Clients that
2685 * request larger slots than they need will get poor results:
2687 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2689 return nfserr_jukebox
;
2695 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2696 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2698 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2699 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2701 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2702 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2704 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2705 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2706 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2707 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2710 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2712 ca
->headerpadsz
= 0;
2714 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2715 return nfserr_toosmall
;
2716 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2717 return nfserr_toosmall
;
2718 ca
->maxresp_cached
= 0;
2720 return nfserr_toosmall
;
2725 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2727 switch (cbs
->flavor
) {
2733 * GSS case: the spec doesn't allow us to return this
2734 * error. But it also doesn't allow us not to support
2736 * I'd rather this fail hard than return some error the
2737 * client might think it can already handle:
2739 return nfserr_encr_alg_unsupp
;
2744 nfsd4_create_session(struct svc_rqst
*rqstp
,
2745 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
2747 struct nfsd4_create_session
*cr_ses
= &u
->create_session
;
2748 struct sockaddr
*sa
= svc_addr(rqstp
);
2749 struct nfs4_client
*conf
, *unconf
;
2750 struct nfs4_client
*old
= NULL
;
2751 struct nfsd4_session
*new;
2752 struct nfsd4_conn
*conn
;
2753 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2755 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2757 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2758 return nfserr_inval
;
2759 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2762 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2765 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2767 goto out_release_drc_mem
;
2768 status
= nfserr_jukebox
;
2769 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2771 goto out_release_drc_mem
;
2772 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2774 goto out_free_session
;
2776 spin_lock(&nn
->client_lock
);
2777 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2778 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2779 WARN_ON_ONCE(conf
&& unconf
);
2782 status
= nfserr_wrong_cred
;
2783 if (!nfsd4_mach_creds_match(conf
, rqstp
))
2785 cs_slot
= &conf
->cl_cs_slot
;
2786 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2788 if (status
== nfserr_replay_cache
)
2789 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2792 } else if (unconf
) {
2793 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2794 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2795 status
= nfserr_clid_inuse
;
2798 status
= nfserr_wrong_cred
;
2799 if (!nfsd4_mach_creds_match(unconf
, rqstp
))
2801 cs_slot
= &unconf
->cl_cs_slot
;
2802 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2804 /* an unconfirmed replay returns misordered */
2805 status
= nfserr_seq_misordered
;
2808 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2810 status
= mark_client_expired_locked(old
);
2816 move_to_confirmed(unconf
);
2819 status
= nfserr_stale_clientid
;
2823 /* Persistent sessions are not supported */
2824 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2825 /* Upshifting from TCP to RDMA is not supported */
2826 cr_ses
->flags
&= ~SESSION4_RDMA
;
2828 init_session(rqstp
, new, conf
, cr_ses
);
2829 nfsd4_get_session_locked(new);
2831 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2832 NFS4_MAX_SESSIONID_LEN
);
2833 cs_slot
->sl_seqid
++;
2834 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2836 /* cache solo and embedded create sessions under the client_lock */
2837 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2838 spin_unlock(&nn
->client_lock
);
2839 /* init connection and backchannel */
2840 nfsd4_init_conn(rqstp
, conn
, new);
2841 nfsd4_put_session(new);
2846 spin_unlock(&nn
->client_lock
);
2851 __free_session(new);
2852 out_release_drc_mem
:
2853 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2857 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2860 case NFS4_CDFC4_FORE
:
2861 case NFS4_CDFC4_BACK
:
2863 case NFS4_CDFC4_FORE_OR_BOTH
:
2864 case NFS4_CDFC4_BACK_OR_BOTH
:
2865 *dir
= NFS4_CDFC4_BOTH
;
2868 return nfserr_inval
;
2871 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
,
2872 struct nfsd4_compound_state
*cstate
,
2873 union nfsd4_op_u
*u
)
2875 struct nfsd4_backchannel_ctl
*bc
= &u
->backchannel_ctl
;
2876 struct nfsd4_session
*session
= cstate
->session
;
2877 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2880 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2883 spin_lock(&nn
->client_lock
);
2884 session
->se_cb_prog
= bc
->bc_cb_program
;
2885 session
->se_cb_sec
= bc
->bc_cb_sec
;
2886 spin_unlock(&nn
->client_lock
);
2888 nfsd4_probe_callback(session
->se_client
);
2893 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2894 struct nfsd4_compound_state
*cstate
,
2895 union nfsd4_op_u
*u
)
2897 struct nfsd4_bind_conn_to_session
*bcts
= &u
->bind_conn_to_session
;
2899 struct nfsd4_conn
*conn
;
2900 struct nfsd4_session
*session
;
2901 struct net
*net
= SVC_NET(rqstp
);
2902 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2904 if (!nfsd4_last_compound_op(rqstp
))
2905 return nfserr_not_only_op
;
2906 spin_lock(&nn
->client_lock
);
2907 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2908 spin_unlock(&nn
->client_lock
);
2910 goto out_no_session
;
2911 status
= nfserr_wrong_cred
;
2912 if (!nfsd4_mach_creds_match(session
->se_client
, rqstp
))
2914 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2917 conn
= alloc_conn(rqstp
, bcts
->dir
);
2918 status
= nfserr_jukebox
;
2921 nfsd4_init_conn(rqstp
, conn
, session
);
2924 nfsd4_put_session(session
);
2929 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2933 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2937 nfsd4_destroy_session(struct svc_rqst
*r
, struct nfsd4_compound_state
*cstate
,
2938 union nfsd4_op_u
*u
)
2940 struct nfsd4_destroy_session
*sessionid
= &u
->destroy_session
;
2941 struct nfsd4_session
*ses
;
2943 int ref_held_by_me
= 0;
2944 struct net
*net
= SVC_NET(r
);
2945 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2947 status
= nfserr_not_only_op
;
2948 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2949 if (!nfsd4_last_compound_op(r
))
2953 dump_sessionid(__func__
, &sessionid
->sessionid
);
2954 spin_lock(&nn
->client_lock
);
2955 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2957 goto out_client_lock
;
2958 status
= nfserr_wrong_cred
;
2959 if (!nfsd4_mach_creds_match(ses
->se_client
, r
))
2960 goto out_put_session
;
2961 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2963 goto out_put_session
;
2964 unhash_session(ses
);
2965 spin_unlock(&nn
->client_lock
);
2967 nfsd4_probe_callback_sync(ses
->se_client
);
2969 spin_lock(&nn
->client_lock
);
2972 nfsd4_put_session_locked(ses
);
2974 spin_unlock(&nn
->client_lock
);
2979 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2981 struct nfsd4_conn
*c
;
2983 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2984 if (c
->cn_xprt
== xpt
) {
2991 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
2993 struct nfs4_client
*clp
= ses
->se_client
;
2994 struct nfsd4_conn
*c
;
2995 __be32 status
= nfs_ok
;
2998 spin_lock(&clp
->cl_lock
);
2999 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
3002 status
= nfserr_conn_not_bound_to_session
;
3003 if (clp
->cl_mach_cred
)
3005 __nfsd4_hash_conn(new, ses
);
3006 spin_unlock(&clp
->cl_lock
);
3007 ret
= nfsd4_register_conn(new);
3009 /* oops; xprt is already down: */
3010 nfsd4_conn_lost(&new->cn_xpt_user
);
3013 spin_unlock(&clp
->cl_lock
);
3018 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
3020 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
3022 return args
->opcnt
> session
->se_fchannel
.maxops
;
3025 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
3026 struct nfsd4_session
*session
)
3028 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
3030 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
3034 nfsd4_sequence(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3035 union nfsd4_op_u
*u
)
3037 struct nfsd4_sequence
*seq
= &u
->sequence
;
3038 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3039 struct xdr_stream
*xdr
= &resp
->xdr
;
3040 struct nfsd4_session
*session
;
3041 struct nfs4_client
*clp
;
3042 struct nfsd4_slot
*slot
;
3043 struct nfsd4_conn
*conn
;
3046 struct net
*net
= SVC_NET(rqstp
);
3047 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3049 if (resp
->opcnt
!= 1)
3050 return nfserr_sequence_pos
;
3053 * Will be either used or freed by nfsd4_sequence_check_conn
3056 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
3058 return nfserr_jukebox
;
3060 spin_lock(&nn
->client_lock
);
3061 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
3063 goto out_no_session
;
3064 clp
= session
->se_client
;
3066 status
= nfserr_too_many_ops
;
3067 if (nfsd4_session_too_many_ops(rqstp
, session
))
3068 goto out_put_session
;
3070 status
= nfserr_req_too_big
;
3071 if (nfsd4_request_too_big(rqstp
, session
))
3072 goto out_put_session
;
3074 status
= nfserr_badslot
;
3075 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
3076 goto out_put_session
;
3078 slot
= session
->se_slots
[seq
->slotid
];
3079 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
3081 /* We do not negotiate the number of slots yet, so set the
3082 * maxslots to the session maxreqs which is used to encode
3083 * sr_highest_slotid and the sr_target_slot id to maxslots */
3084 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
3086 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
3087 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
3088 if (status
== nfserr_replay_cache
) {
3089 status
= nfserr_seq_misordered
;
3090 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
3091 goto out_put_session
;
3092 cstate
->slot
= slot
;
3093 cstate
->session
= session
;
3095 /* Return the cached reply status and set cstate->status
3096 * for nfsd4_proc_compound processing */
3097 status
= nfsd4_replay_cache_entry(resp
, seq
);
3098 cstate
->status
= nfserr_replay_cache
;
3102 goto out_put_session
;
3104 status
= nfsd4_sequence_check_conn(conn
, session
);
3107 goto out_put_session
;
3109 buflen
= (seq
->cachethis
) ?
3110 session
->se_fchannel
.maxresp_cached
:
3111 session
->se_fchannel
.maxresp_sz
;
3112 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
3114 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
3115 goto out_put_session
;
3116 svc_reserve(rqstp
, buflen
);
3119 /* Success! bump slot seqid */
3120 slot
->sl_seqid
= seq
->seqid
;
3121 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
3123 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
3125 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
3127 cstate
->slot
= slot
;
3128 cstate
->session
= session
;
3132 switch (clp
->cl_cb_state
) {
3134 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
3136 case NFSD4_CB_FAULT
:
3137 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
3140 seq
->status_flags
= 0;
3142 if (!list_empty(&clp
->cl_revoked
))
3143 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
3147 spin_unlock(&nn
->client_lock
);
3150 nfsd4_put_session_locked(session
);
3151 goto out_no_session
;
3155 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
3157 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
3159 if (nfsd4_has_session(cs
)) {
3160 if (cs
->status
!= nfserr_replay_cache
) {
3161 nfsd4_store_cache_entry(resp
);
3162 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
3164 /* Drop session reference that was taken in nfsd4_sequence() */
3165 nfsd4_put_session(cs
->session
);
3167 put_client_renew(cs
->clp
);
3171 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
,
3172 struct nfsd4_compound_state
*cstate
,
3173 union nfsd4_op_u
*u
)
3175 struct nfsd4_destroy_clientid
*dc
= &u
->destroy_clientid
;
3176 struct nfs4_client
*conf
, *unconf
;
3177 struct nfs4_client
*clp
= NULL
;
3179 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3181 spin_lock(&nn
->client_lock
);
3182 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
3183 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
3184 WARN_ON_ONCE(conf
&& unconf
);
3187 if (client_has_state(conf
)) {
3188 status
= nfserr_clientid_busy
;
3191 status
= mark_client_expired_locked(conf
);
3198 status
= nfserr_stale_clientid
;
3201 if (!nfsd4_mach_creds_match(clp
, rqstp
)) {
3203 status
= nfserr_wrong_cred
;
3206 unhash_client_locked(clp
);
3208 spin_unlock(&nn
->client_lock
);
3215 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
,
3216 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
3218 struct nfsd4_reclaim_complete
*rc
= &u
->reclaim_complete
;
3221 if (rc
->rca_one_fs
) {
3222 if (!cstate
->current_fh
.fh_dentry
)
3223 return nfserr_nofilehandle
;
3225 * We don't take advantage of the rca_one_fs case.
3226 * That's OK, it's optional, we can safely ignore it.
3231 status
= nfserr_complete_already
;
3232 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
3233 &cstate
->session
->se_client
->cl_flags
))
3236 status
= nfserr_stale_clientid
;
3237 if (is_client_expired(cstate
->session
->se_client
))
3239 * The following error isn't really legal.
3240 * But we only get here if the client just explicitly
3241 * destroyed the client. Surely it no longer cares what
3242 * error it gets back on an operation for the dead
3248 nfsd4_client_record_create(cstate
->session
->se_client
);
3254 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3255 union nfsd4_op_u
*u
)
3257 struct nfsd4_setclientid
*setclid
= &u
->setclientid
;
3258 struct xdr_netobj clname
= setclid
->se_name
;
3259 nfs4_verifier clverifier
= setclid
->se_verf
;
3260 struct nfs4_client
*conf
, *new;
3261 struct nfs4_client
*unconf
= NULL
;
3263 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3265 new = create_client(clname
, rqstp
, &clverifier
);
3267 return nfserr_jukebox
;
3268 /* Cases below refer to rfc 3530 section 14.2.33: */
3269 spin_lock(&nn
->client_lock
);
3270 conf
= find_confirmed_client_by_name(&clname
, nn
);
3271 if (conf
&& client_has_state(conf
)) {
3273 status
= nfserr_clid_inuse
;
3274 if (clp_used_exchangeid(conf
))
3276 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
3277 char addr_str
[INET6_ADDRSTRLEN
];
3278 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
3280 dprintk("NFSD: setclientid: string in use by client "
3281 "at %s\n", addr_str
);
3285 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
3287 unhash_client_locked(unconf
);
3288 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
)) {
3289 /* case 1: probable callback update */
3290 copy_clid(new, conf
);
3291 gen_confirm(new, nn
);
3292 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3294 new->cl_minorversion
= 0;
3295 gen_callback(new, setclid
, rqstp
);
3296 add_to_unconfirmed(new);
3297 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
3298 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
3299 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
3303 spin_unlock(&nn
->client_lock
);
3307 expire_client(unconf
);
3313 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
3314 struct nfsd4_compound_state
*cstate
,
3315 union nfsd4_op_u
*u
)
3317 struct nfsd4_setclientid_confirm
*setclientid_confirm
=
3318 &u
->setclientid_confirm
;
3319 struct nfs4_client
*conf
, *unconf
;
3320 struct nfs4_client
*old
= NULL
;
3321 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
3322 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
3324 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3326 if (STALE_CLIENTID(clid
, nn
))
3327 return nfserr_stale_clientid
;
3329 spin_lock(&nn
->client_lock
);
3330 conf
= find_confirmed_client(clid
, false, nn
);
3331 unconf
= find_unconfirmed_client(clid
, false, nn
);
3333 * We try hard to give out unique clientid's, so if we get an
3334 * attempt to confirm the same clientid with a different cred,
3335 * the client may be buggy; this should never happen.
3337 * Nevertheless, RFC 7530 recommends INUSE for this case:
3339 status
= nfserr_clid_inuse
;
3340 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
3342 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
3344 /* cases below refer to rfc 3530 section 14.2.34: */
3345 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
3346 if (conf
&& same_verf(&confirm
, &conf
->cl_confirm
)) {
3347 /* case 2: probable retransmit */
3349 } else /* case 4: client hasn't noticed we rebooted yet? */
3350 status
= nfserr_stale_clientid
;
3354 if (conf
) { /* case 1: callback update */
3356 unhash_client_locked(old
);
3357 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
3358 } else { /* case 3: normal case; new or rebooted client */
3359 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
3361 status
= nfserr_clid_inuse
;
3362 if (client_has_state(old
)
3363 && !same_creds(&unconf
->cl_cred
,
3366 status
= mark_client_expired_locked(old
);
3372 move_to_confirmed(unconf
);
3375 get_client_locked(conf
);
3376 spin_unlock(&nn
->client_lock
);
3377 nfsd4_probe_callback(conf
);
3378 spin_lock(&nn
->client_lock
);
3379 put_client_renew_locked(conf
);
3381 spin_unlock(&nn
->client_lock
);
3387 static struct nfs4_file
*nfsd4_alloc_file(void)
3389 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
3392 /* OPEN Share state helper functions */
3393 static void nfsd4_init_file(struct knfsd_fh
*fh
, unsigned int hashval
,
3394 struct nfs4_file
*fp
)
3396 lockdep_assert_held(&state_lock
);
3398 atomic_set(&fp
->fi_ref
, 1);
3399 spin_lock_init(&fp
->fi_lock
);
3400 INIT_LIST_HEAD(&fp
->fi_stateids
);
3401 INIT_LIST_HEAD(&fp
->fi_delegations
);
3402 INIT_LIST_HEAD(&fp
->fi_clnt_odstate
);
3403 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
3404 fp
->fi_deleg_file
= NULL
;
3405 fp
->fi_had_conflict
= false;
3406 fp
->fi_share_deny
= 0;
3407 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
3408 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
3409 #ifdef CONFIG_NFSD_PNFS
3410 INIT_LIST_HEAD(&fp
->fi_lo_states
);
3411 atomic_set(&fp
->fi_lo_recalls
, 0);
3413 hlist_add_head_rcu(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
3417 nfsd4_free_slabs(void)
3419 kmem_cache_destroy(odstate_slab
);
3420 kmem_cache_destroy(openowner_slab
);
3421 kmem_cache_destroy(lockowner_slab
);
3422 kmem_cache_destroy(file_slab
);
3423 kmem_cache_destroy(stateid_slab
);
3424 kmem_cache_destroy(deleg_slab
);
3428 nfsd4_init_slabs(void)
3430 openowner_slab
= kmem_cache_create("nfsd4_openowners",
3431 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
3432 if (openowner_slab
== NULL
)
3434 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
3435 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
3436 if (lockowner_slab
== NULL
)
3437 goto out_free_openowner_slab
;
3438 file_slab
= kmem_cache_create("nfsd4_files",
3439 sizeof(struct nfs4_file
), 0, 0, NULL
);
3440 if (file_slab
== NULL
)
3441 goto out_free_lockowner_slab
;
3442 stateid_slab
= kmem_cache_create("nfsd4_stateids",
3443 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
3444 if (stateid_slab
== NULL
)
3445 goto out_free_file_slab
;
3446 deleg_slab
= kmem_cache_create("nfsd4_delegations",
3447 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
3448 if (deleg_slab
== NULL
)
3449 goto out_free_stateid_slab
;
3450 odstate_slab
= kmem_cache_create("nfsd4_odstate",
3451 sizeof(struct nfs4_clnt_odstate
), 0, 0, NULL
);
3452 if (odstate_slab
== NULL
)
3453 goto out_free_deleg_slab
;
3456 out_free_deleg_slab
:
3457 kmem_cache_destroy(deleg_slab
);
3458 out_free_stateid_slab
:
3459 kmem_cache_destroy(stateid_slab
);
3461 kmem_cache_destroy(file_slab
);
3462 out_free_lockowner_slab
:
3463 kmem_cache_destroy(lockowner_slab
);
3464 out_free_openowner_slab
:
3465 kmem_cache_destroy(openowner_slab
);
3467 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3471 static void init_nfs4_replay(struct nfs4_replay
*rp
)
3473 rp
->rp_status
= nfserr_serverfault
;
3475 rp
->rp_buf
= rp
->rp_ibuf
;
3476 mutex_init(&rp
->rp_mutex
);
3479 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state
*cstate
,
3480 struct nfs4_stateowner
*so
)
3482 if (!nfsd4_has_session(cstate
)) {
3483 mutex_lock(&so
->so_replay
.rp_mutex
);
3484 cstate
->replay_owner
= nfs4_get_stateowner(so
);
3488 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state
*cstate
)
3490 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
3493 cstate
->replay_owner
= NULL
;
3494 mutex_unlock(&so
->so_replay
.rp_mutex
);
3495 nfs4_put_stateowner(so
);
3499 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
3501 struct nfs4_stateowner
*sop
;
3503 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
3507 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
3508 if (!sop
->so_owner
.data
) {
3509 kmem_cache_free(slab
, sop
);
3512 sop
->so_owner
.len
= owner
->len
;
3514 INIT_LIST_HEAD(&sop
->so_stateids
);
3515 sop
->so_client
= clp
;
3516 init_nfs4_replay(&sop
->so_replay
);
3517 atomic_set(&sop
->so_count
, 1);
3521 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
3523 lockdep_assert_held(&clp
->cl_lock
);
3525 list_add(&oo
->oo_owner
.so_strhash
,
3526 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
3527 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
3530 static void nfs4_unhash_openowner(struct nfs4_stateowner
*so
)
3532 unhash_openowner_locked(openowner(so
));
3535 static void nfs4_free_openowner(struct nfs4_stateowner
*so
)
3537 struct nfs4_openowner
*oo
= openowner(so
);
3539 kmem_cache_free(openowner_slab
, oo
);
3542 static const struct nfs4_stateowner_operations openowner_ops
= {
3543 .so_unhash
= nfs4_unhash_openowner
,
3544 .so_free
= nfs4_free_openowner
,
3547 static struct nfs4_ol_stateid
*
3548 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3550 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3551 struct nfs4_openowner
*oo
= open
->op_openowner
;
3553 lockdep_assert_held(&fp
->fi_lock
);
3555 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3556 /* ignore lock owners */
3557 if (local
->st_stateowner
->so_is_open_owner
== 0)
3559 if (local
->st_stateowner
!= &oo
->oo_owner
)
3561 if (local
->st_stid
.sc_type
== NFS4_OPEN_STID
) {
3563 atomic_inc(&ret
->st_stid
.sc_count
);
3571 nfsd4_verify_open_stid(struct nfs4_stid
*s
)
3573 __be32 ret
= nfs_ok
;
3575 switch (s
->sc_type
) {
3578 case NFS4_CLOSED_STID
:
3579 case NFS4_CLOSED_DELEG_STID
:
3580 ret
= nfserr_bad_stateid
;
3582 case NFS4_REVOKED_DELEG_STID
:
3583 ret
= nfserr_deleg_revoked
;
3588 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3590 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid
*stp
)
3594 mutex_lock(&stp
->st_mutex
);
3595 ret
= nfsd4_verify_open_stid(&stp
->st_stid
);
3597 mutex_unlock(&stp
->st_mutex
);
3601 static struct nfs4_ol_stateid
*
3602 nfsd4_find_and_lock_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3604 struct nfs4_ol_stateid
*stp
;
3606 spin_lock(&fp
->fi_lock
);
3607 stp
= nfsd4_find_existing_open(fp
, open
);
3608 spin_unlock(&fp
->fi_lock
);
3609 if (!stp
|| nfsd4_lock_ol_stateid(stp
) == nfs_ok
)
3611 nfs4_put_stid(&stp
->st_stid
);
3616 static struct nfs4_openowner
*
3617 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
3618 struct nfsd4_compound_state
*cstate
)
3620 struct nfs4_client
*clp
= cstate
->clp
;
3621 struct nfs4_openowner
*oo
, *ret
;
3623 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
3626 oo
->oo_owner
.so_ops
= &openowner_ops
;
3627 oo
->oo_owner
.so_is_open_owner
= 1;
3628 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
3630 if (nfsd4_has_session(cstate
))
3631 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3633 oo
->oo_last_closed_stid
= NULL
;
3634 INIT_LIST_HEAD(&oo
->oo_close_lru
);
3635 spin_lock(&clp
->cl_lock
);
3636 ret
= find_openstateowner_str_locked(strhashval
, open
, clp
);
3638 hash_openowner(oo
, clp
, strhashval
);
3641 nfs4_free_stateowner(&oo
->oo_owner
);
3643 spin_unlock(&clp
->cl_lock
);
3647 static struct nfs4_ol_stateid
*
3648 init_open_stateid(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3651 struct nfs4_openowner
*oo
= open
->op_openowner
;
3652 struct nfs4_ol_stateid
*retstp
= NULL
;
3653 struct nfs4_ol_stateid
*stp
;
3656 /* We are moving these outside of the spinlocks to avoid the warnings */
3657 mutex_init(&stp
->st_mutex
);
3658 mutex_lock(&stp
->st_mutex
);
3661 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
3662 spin_lock(&fp
->fi_lock
);
3664 retstp
= nfsd4_find_existing_open(fp
, open
);
3668 open
->op_stp
= NULL
;
3669 atomic_inc(&stp
->st_stid
.sc_count
);
3670 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
3671 INIT_LIST_HEAD(&stp
->st_locks
);
3672 stp
->st_stateowner
= nfs4_get_stateowner(&oo
->oo_owner
);
3674 stp
->st_stid
.sc_file
= fp
;
3675 stp
->st_access_bmap
= 0;
3676 stp
->st_deny_bmap
= 0;
3677 stp
->st_openstp
= NULL
;
3678 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
3679 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3682 spin_unlock(&fp
->fi_lock
);
3683 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
3685 /* Handle races with CLOSE */
3686 if (nfsd4_lock_ol_stateid(retstp
) != nfs_ok
) {
3687 nfs4_put_stid(&retstp
->st_stid
);
3690 /* To keep mutex tracking happy */
3691 mutex_unlock(&stp
->st_mutex
);
3698 * In the 4.0 case we need to keep the owners around a little while to handle
3699 * CLOSE replay. We still do need to release any file access that is held by
3700 * them before returning however.
3703 move_to_close_lru(struct nfs4_ol_stateid
*s
, struct net
*net
)
3705 struct nfs4_ol_stateid
*last
;
3706 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
3707 struct nfsd_net
*nn
= net_generic(s
->st_stid
.sc_client
->net
,
3710 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
3713 * We know that we hold one reference via nfsd4_close, and another
3714 * "persistent" reference for the client. If the refcount is higher
3715 * than 2, then there are still calls in progress that are using this
3716 * stateid. We can't put the sc_file reference until they are finished.
3717 * Wait for the refcount to drop to 2. Since it has been unhashed,
3718 * there should be no danger of the refcount going back up again at
3721 wait_event(close_wq
, atomic_read(&s
->st_stid
.sc_count
) == 2);
3723 release_all_access(s
);
3724 if (s
->st_stid
.sc_file
) {
3725 put_nfs4_file(s
->st_stid
.sc_file
);
3726 s
->st_stid
.sc_file
= NULL
;
3729 spin_lock(&nn
->client_lock
);
3730 last
= oo
->oo_last_closed_stid
;
3731 oo
->oo_last_closed_stid
= s
;
3732 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
3733 oo
->oo_time
= get_seconds();
3734 spin_unlock(&nn
->client_lock
);
3736 nfs4_put_stid(&last
->st_stid
);
3739 /* search file_hashtbl[] for file */
3740 static struct nfs4_file
*
3741 find_file_locked(struct knfsd_fh
*fh
, unsigned int hashval
)
3743 struct nfs4_file
*fp
;
3745 hlist_for_each_entry_rcu(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3746 if (fh_match(&fp
->fi_fhandle
, fh
)) {
3747 if (atomic_inc_not_zero(&fp
->fi_ref
))
3755 find_file(struct knfsd_fh
*fh
)
3757 struct nfs4_file
*fp
;
3758 unsigned int hashval
= file_hashval(fh
);
3761 fp
= find_file_locked(fh
, hashval
);
3766 static struct nfs4_file
*
3767 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3769 struct nfs4_file
*fp
;
3770 unsigned int hashval
= file_hashval(fh
);
3773 fp
= find_file_locked(fh
, hashval
);
3778 spin_lock(&state_lock
);
3779 fp
= find_file_locked(fh
, hashval
);
3780 if (likely(fp
== NULL
)) {
3781 nfsd4_init_file(fh
, hashval
, new);
3784 spin_unlock(&state_lock
);
3790 * Called to check deny when READ with all zero stateid or
3791 * WRITE with all zero or all one stateid
3794 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3796 struct nfs4_file
*fp
;
3797 __be32 ret
= nfs_ok
;
3799 fp
= find_file(¤t_fh
->fh_handle
);
3802 /* Check for conflicting share reservations */
3803 spin_lock(&fp
->fi_lock
);
3804 if (fp
->fi_share_deny
& deny_type
)
3805 ret
= nfserr_locked
;
3806 spin_unlock(&fp
->fi_lock
);
3811 static void nfsd4_cb_recall_prepare(struct nfsd4_callback
*cb
)
3813 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3814 struct nfsd_net
*nn
= net_generic(dp
->dl_stid
.sc_client
->net
,
3817 block_delegations(&dp
->dl_stid
.sc_file
->fi_fhandle
);
3820 * We can't do this in nfsd_break_deleg_cb because it is
3821 * already holding inode->i_lock.
3823 * If the dl_time != 0, then we know that it has already been
3824 * queued for a lease break. Don't queue it again.
3826 spin_lock(&state_lock
);
3827 if (dp
->dl_time
== 0) {
3828 dp
->dl_time
= get_seconds();
3829 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3831 spin_unlock(&state_lock
);
3834 static int nfsd4_cb_recall_done(struct nfsd4_callback
*cb
,
3835 struct rpc_task
*task
)
3837 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3839 if (dp
->dl_stid
.sc_type
== NFS4_CLOSED_DELEG_STID
)
3842 switch (task
->tk_status
) {
3846 case -NFS4ERR_BAD_STATEID
:
3848 * Race: client probably got cb_recall before open reply
3849 * granting delegation.
3851 if (dp
->dl_retries
--) {
3852 rpc_delay(task
, 2 * HZ
);
3861 static void nfsd4_cb_recall_release(struct nfsd4_callback
*cb
)
3863 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3865 nfs4_put_stid(&dp
->dl_stid
);
3868 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops
= {
3869 .prepare
= nfsd4_cb_recall_prepare
,
3870 .done
= nfsd4_cb_recall_done
,
3871 .release
= nfsd4_cb_recall_release
,
3874 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3877 * We're assuming the state code never drops its reference
3878 * without first removing the lease. Since we're in this lease
3879 * callback (and since the lease code is serialized by the kernel
3880 * lock) we know the server hasn't removed the lease yet, we know
3881 * it's safe to take a reference.
3883 atomic_inc(&dp
->dl_stid
.sc_count
);
3884 nfsd4_run_cb(&dp
->dl_recall
);
3887 /* Called from break_lease() with i_lock held. */
3889 nfsd_break_deleg_cb(struct file_lock
*fl
)
3892 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3893 struct nfs4_delegation
*dp
;
3896 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3899 if (fp
->fi_had_conflict
) {
3900 WARN(1, "duplicate break on %p\n", fp
);
3904 * We don't want the locks code to timeout the lease for us;
3905 * we'll remove it ourself if a delegation isn't returned
3908 fl
->fl_break_time
= 0;
3910 spin_lock(&fp
->fi_lock
);
3911 fp
->fi_had_conflict
= true;
3913 * If there are no delegations on the list, then return true
3914 * so that the lease code will go ahead and delete it.
3916 if (list_empty(&fp
->fi_delegations
))
3919 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3920 nfsd_break_one_deleg(dp
);
3921 spin_unlock(&fp
->fi_lock
);
3926 nfsd_change_deleg_cb(struct file_lock
*onlist
, int arg
,
3927 struct list_head
*dispose
)
3930 return lease_modify(onlist
, arg
, dispose
);
3935 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3936 .lm_break
= nfsd_break_deleg_cb
,
3937 .lm_change
= nfsd_change_deleg_cb
,
3940 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3942 if (nfsd4_has_session(cstate
))
3944 if (seqid
== so
->so_seqid
- 1)
3945 return nfserr_replay_me
;
3946 if (seqid
== so
->so_seqid
)
3948 return nfserr_bad_seqid
;
3951 static __be32
lookup_clientid(clientid_t
*clid
,
3952 struct nfsd4_compound_state
*cstate
,
3953 struct nfsd_net
*nn
)
3955 struct nfs4_client
*found
;
3958 found
= cstate
->clp
;
3959 if (!same_clid(&found
->cl_clientid
, clid
))
3960 return nfserr_stale_clientid
;
3964 if (STALE_CLIENTID(clid
, nn
))
3965 return nfserr_stale_clientid
;
3968 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
3969 * cached already then we know this is for is for v4.0 and "sessions"
3972 WARN_ON_ONCE(cstate
->session
);
3973 spin_lock(&nn
->client_lock
);
3974 found
= find_confirmed_client(clid
, false, nn
);
3976 spin_unlock(&nn
->client_lock
);
3977 return nfserr_expired
;
3979 atomic_inc(&found
->cl_refcount
);
3980 spin_unlock(&nn
->client_lock
);
3982 /* Cache the nfs4_client in cstate! */
3983 cstate
->clp
= found
;
3988 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
3989 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
3991 clientid_t
*clientid
= &open
->op_clientid
;
3992 struct nfs4_client
*clp
= NULL
;
3993 unsigned int strhashval
;
3994 struct nfs4_openowner
*oo
= NULL
;
3997 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
3998 return nfserr_stale_clientid
;
4000 * In case we need it later, after we've already created the
4001 * file and don't want to risk a further failure:
4003 open
->op_file
= nfsd4_alloc_file();
4004 if (open
->op_file
== NULL
)
4005 return nfserr_jukebox
;
4007 status
= lookup_clientid(clientid
, cstate
, nn
);
4012 strhashval
= ownerstr_hashval(&open
->op_owner
);
4013 oo
= find_openstateowner_str(strhashval
, open
, clp
);
4014 open
->op_openowner
= oo
;
4018 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
4019 /* Replace unconfirmed owners without checking for replay. */
4020 release_openowner(oo
);
4021 open
->op_openowner
= NULL
;
4024 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
4029 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
4031 return nfserr_jukebox
;
4032 open
->op_openowner
= oo
;
4034 open
->op_stp
= nfs4_alloc_open_stateid(clp
);
4036 return nfserr_jukebox
;
4038 if (nfsd4_has_session(cstate
) &&
4039 (cstate
->current_fh
.fh_export
->ex_flags
& NFSEXP_PNFS
)) {
4040 open
->op_odstate
= alloc_clnt_odstate(clp
);
4041 if (!open
->op_odstate
)
4042 return nfserr_jukebox
;
4048 static inline __be32
4049 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
4051 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
4052 return nfserr_openmode
;
4057 static int share_access_to_flags(u32 share_access
)
4059 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
4062 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
4064 struct nfs4_stid
*ret
;
4066 ret
= find_stateid_by_type(cl
, s
,
4067 NFS4_DELEG_STID
|NFS4_REVOKED_DELEG_STID
);
4070 return delegstateid(ret
);
4073 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
4075 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
4076 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
4080 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
4081 struct nfs4_delegation
**dp
)
4084 __be32 status
= nfserr_bad_stateid
;
4085 struct nfs4_delegation
*deleg
;
4087 deleg
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
4090 if (deleg
->dl_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
) {
4091 nfs4_put_stid(&deleg
->dl_stid
);
4092 if (cl
->cl_minorversion
)
4093 status
= nfserr_deleg_revoked
;
4096 flags
= share_access_to_flags(open
->op_share_access
);
4097 status
= nfs4_check_delegmode(deleg
, flags
);
4099 nfs4_put_stid(&deleg
->dl_stid
);
4104 if (!nfsd4_is_deleg_cur(open
))
4108 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
4112 static inline int nfs4_access_to_access(u32 nfs4_access
)
4116 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
4117 flags
|= NFSD_MAY_READ
;
4118 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
4119 flags
|= NFSD_MAY_WRITE
;
4123 static inline __be32
4124 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
4125 struct nfsd4_open
*open
)
4127 struct iattr iattr
= {
4128 .ia_valid
= ATTR_SIZE
,
4131 if (!open
->op_truncate
)
4133 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
4134 return nfserr_inval
;
4135 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
4138 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
4139 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
4140 struct nfsd4_open
*open
)
4142 struct file
*filp
= NULL
;
4144 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
4145 int access
= nfs4_access_to_access(open
->op_share_access
);
4146 unsigned char old_access_bmap
, old_deny_bmap
;
4148 spin_lock(&fp
->fi_lock
);
4151 * Are we trying to set a deny mode that would conflict with
4154 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
4155 if (status
!= nfs_ok
) {
4156 spin_unlock(&fp
->fi_lock
);
4160 /* set access to the file */
4161 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
4162 if (status
!= nfs_ok
) {
4163 spin_unlock(&fp
->fi_lock
);
4167 /* Set access bits in stateid */
4168 old_access_bmap
= stp
->st_access_bmap
;
4169 set_access(open
->op_share_access
, stp
);
4171 /* Set new deny mask */
4172 old_deny_bmap
= stp
->st_deny_bmap
;
4173 set_deny(open
->op_share_deny
, stp
);
4174 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4176 if (!fp
->fi_fds
[oflag
]) {
4177 spin_unlock(&fp
->fi_lock
);
4178 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
4180 goto out_put_access
;
4181 spin_lock(&fp
->fi_lock
);
4182 if (!fp
->fi_fds
[oflag
]) {
4183 fp
->fi_fds
[oflag
] = filp
;
4187 spin_unlock(&fp
->fi_lock
);
4191 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4193 goto out_put_access
;
4197 stp
->st_access_bmap
= old_access_bmap
;
4198 nfs4_file_put_access(fp
, open
->op_share_access
);
4199 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
4204 nfs4_upgrade_open(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
, struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
, struct nfsd4_open
*open
)
4207 unsigned char old_deny_bmap
= stp
->st_deny_bmap
;
4209 if (!test_access(open
->op_share_access
, stp
))
4210 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
4212 /* test and set deny mode */
4213 spin_lock(&fp
->fi_lock
);
4214 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
4215 if (status
== nfs_ok
) {
4216 set_deny(open
->op_share_deny
, stp
);
4217 fp
->fi_share_deny
|=
4218 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4220 spin_unlock(&fp
->fi_lock
);
4222 if (status
!= nfs_ok
)
4225 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4226 if (status
!= nfs_ok
)
4227 reset_union_bmap_deny(old_deny_bmap
, stp
);
4231 /* Should we give out recallable state?: */
4232 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
4234 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
4237 * In the sessions case, since we don't have to establish a
4238 * separate connection for callbacks, we assume it's OK
4239 * until we hear otherwise:
4241 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
4244 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
4246 struct file_lock
*fl
;
4248 fl
= locks_alloc_lock();
4251 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
4252 fl
->fl_flags
= FL_DELEG
;
4253 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
4254 fl
->fl_end
= OFFSET_MAX
;
4255 fl
->fl_owner
= (fl_owner_t
)fp
;
4256 fl
->fl_pid
= current
->tgid
;
4261 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4262 * @dp: a pointer to the nfs4_delegation we're adding.
4265 * On success: Return code will be 0 on success.
4267 * On error: -EAGAIN if there was an existing delegation.
4268 * nonzero if there is an error in other cases.
4272 static int nfs4_setlease(struct nfs4_delegation
*dp
)
4274 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
4275 struct file_lock
*fl
;
4279 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
4282 filp
= find_readable_file(fp
);
4284 /* We should always have a readable file here */
4286 locks_free_lock(fl
);
4290 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
, NULL
);
4292 locks_free_lock(fl
);
4295 spin_lock(&state_lock
);
4296 spin_lock(&fp
->fi_lock
);
4297 /* Did the lease get broken before we took the lock? */
4299 if (fp
->fi_had_conflict
)
4302 if (fp
->fi_deleg_file
) {
4303 status
= hash_delegation_locked(dp
, fp
);
4306 fp
->fi_deleg_file
= filp
;
4307 fp
->fi_delegees
= 0;
4308 status
= hash_delegation_locked(dp
, fp
);
4309 spin_unlock(&fp
->fi_lock
);
4310 spin_unlock(&state_lock
);
4312 /* Should never happen, this is a new fi_deleg_file */
4318 spin_unlock(&fp
->fi_lock
);
4319 spin_unlock(&state_lock
);
4325 static struct nfs4_delegation
*
4326 nfs4_set_delegation(struct nfs4_client
*clp
, struct svc_fh
*fh
,
4327 struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*odstate
)
4330 struct nfs4_delegation
*dp
;
4332 if (fp
->fi_had_conflict
)
4333 return ERR_PTR(-EAGAIN
);
4335 spin_lock(&state_lock
);
4336 spin_lock(&fp
->fi_lock
);
4337 status
= nfs4_get_existing_delegation(clp
, fp
);
4338 spin_unlock(&fp
->fi_lock
);
4339 spin_unlock(&state_lock
);
4342 return ERR_PTR(status
);
4344 dp
= alloc_init_deleg(clp
, fh
, odstate
);
4346 return ERR_PTR(-ENOMEM
);
4349 spin_lock(&state_lock
);
4350 spin_lock(&fp
->fi_lock
);
4351 dp
->dl_stid
.sc_file
= fp
;
4352 if (!fp
->fi_deleg_file
) {
4353 spin_unlock(&fp
->fi_lock
);
4354 spin_unlock(&state_lock
);
4355 status
= nfs4_setlease(dp
);
4358 if (fp
->fi_had_conflict
) {
4362 status
= hash_delegation_locked(dp
, fp
);
4364 spin_unlock(&fp
->fi_lock
);
4365 spin_unlock(&state_lock
);
4368 put_clnt_odstate(dp
->dl_clnt_odstate
);
4369 nfs4_put_stid(&dp
->dl_stid
);
4370 return ERR_PTR(status
);
4375 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
4377 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4378 if (status
== -EAGAIN
)
4379 open
->op_why_no_deleg
= WND4_CONTENTION
;
4381 open
->op_why_no_deleg
= WND4_RESOURCE
;
4382 switch (open
->op_deleg_want
) {
4383 case NFS4_SHARE_WANT_READ_DELEG
:
4384 case NFS4_SHARE_WANT_WRITE_DELEG
:
4385 case NFS4_SHARE_WANT_ANY_DELEG
:
4387 case NFS4_SHARE_WANT_CANCEL
:
4388 open
->op_why_no_deleg
= WND4_CANCELLED
;
4390 case NFS4_SHARE_WANT_NO_DELEG
:
4397 * Attempt to hand out a delegation.
4399 * Note we don't support write delegations, and won't until the vfs has
4400 * proper support for them.
4403 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
,
4404 struct nfs4_ol_stateid
*stp
)
4406 struct nfs4_delegation
*dp
;
4407 struct nfs4_openowner
*oo
= openowner(stp
->st_stateowner
);
4408 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
4412 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
4413 open
->op_recall
= 0;
4414 switch (open
->op_claim_type
) {
4415 case NFS4_OPEN_CLAIM_PREVIOUS
:
4417 open
->op_recall
= 1;
4418 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
4421 case NFS4_OPEN_CLAIM_NULL
:
4422 case NFS4_OPEN_CLAIM_FH
:
4424 * Let's not give out any delegations till everyone's
4425 * had the chance to reclaim theirs, *and* until
4426 * NLM locks have all been reclaimed:
4428 if (locks_in_grace(clp
->net
))
4430 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4433 * Also, if the file was opened for write or
4434 * create, there's a good chance the client's
4435 * about to write to it, resulting in an
4436 * immediate recall (since we don't support
4437 * write delegations):
4439 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
4441 if (open
->op_create
== NFS4_OPEN_CREATE
)
4447 dp
= nfs4_set_delegation(clp
, fh
, stp
->st_stid
.sc_file
, stp
->st_clnt_odstate
);
4451 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
4453 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
4454 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
4455 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
4456 nfs4_put_stid(&dp
->dl_stid
);
4459 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
4460 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
4461 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
4462 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4463 open
->op_recall
= 1;
4466 /* 4.1 client asking for a delegation? */
4467 if (open
->op_deleg_want
)
4468 nfsd4_open_deleg_none_ext(open
, status
);
4472 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
4473 struct nfs4_delegation
*dp
)
4475 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
4476 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4477 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4478 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
4479 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
4480 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4481 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4482 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
4484 /* Otherwise the client must be confused wanting a delegation
4485 * it already has, therefore we don't return
4486 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4491 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
4493 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
4494 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
4495 struct nfs4_file
*fp
= NULL
;
4496 struct nfs4_ol_stateid
*stp
= NULL
;
4497 struct nfs4_delegation
*dp
= NULL
;
4499 bool new_stp
= false;
4502 * Lookup file; if found, lookup stateid and check open request,
4503 * and check for delegations in the process of being recalled.
4504 * If not found, create the nfs4_file struct
4506 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
4507 if (fp
!= open
->op_file
) {
4508 status
= nfs4_check_deleg(cl
, open
, &dp
);
4511 stp
= nfsd4_find_and_lock_existing_open(fp
, open
);
4513 open
->op_file
= NULL
;
4514 status
= nfserr_bad_stateid
;
4515 if (nfsd4_is_deleg_cur(open
))
4520 stp
= init_open_stateid(fp
, open
);
4526 * OPEN the file, or upgrade an existing OPEN.
4527 * If truncate fails, the OPEN fails.
4529 * stp is already locked.
4532 /* Stateid was found, this is an OPEN upgrade */
4533 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
4535 mutex_unlock(&stp
->st_mutex
);
4539 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
4541 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4542 release_open_stateid(stp
);
4543 mutex_unlock(&stp
->st_mutex
);
4547 stp
->st_clnt_odstate
= find_or_hash_clnt_odstate(fp
,
4549 if (stp
->st_clnt_odstate
== open
->op_odstate
)
4550 open
->op_odstate
= NULL
;
4553 nfs4_inc_and_copy_stateid(&open
->op_stateid
, &stp
->st_stid
);
4554 mutex_unlock(&stp
->st_mutex
);
4556 if (nfsd4_has_session(&resp
->cstate
)) {
4557 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
4558 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4559 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
4565 * Attempt to hand out a delegation. No error return, because the
4566 * OPEN succeeds even if we fail.
4568 nfs4_open_delegation(current_fh
, open
, stp
);
4572 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
4573 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4575 /* 4.1 client trying to upgrade/downgrade delegation? */
4576 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
4577 open
->op_deleg_want
)
4578 nfsd4_deleg_xgrade_none_ext(open
, dp
);
4582 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
4583 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
4585 * To finish the open response, we just need to set the rflags.
4587 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
4588 if (nfsd4_has_session(&resp
->cstate
))
4589 open
->op_rflags
|= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
;
4590 else if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
))
4591 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
4594 nfs4_put_stid(&dp
->dl_stid
);
4596 nfs4_put_stid(&stp
->st_stid
);
4601 void nfsd4_cleanup_open_state(struct nfsd4_compound_state
*cstate
,
4602 struct nfsd4_open
*open
)
4604 if (open
->op_openowner
) {
4605 struct nfs4_stateowner
*so
= &open
->op_openowner
->oo_owner
;
4607 nfsd4_cstate_assign_replay(cstate
, so
);
4608 nfs4_put_stateowner(so
);
4611 kmem_cache_free(file_slab
, open
->op_file
);
4613 nfs4_put_stid(&open
->op_stp
->st_stid
);
4614 if (open
->op_odstate
)
4615 kmem_cache_free(odstate_slab
, open
->op_odstate
);
4619 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4620 union nfsd4_op_u
*u
)
4622 clientid_t
*clid
= &u
->renew
;
4623 struct nfs4_client
*clp
;
4625 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4627 dprintk("process_renew(%08x/%08x): starting\n",
4628 clid
->cl_boot
, clid
->cl_id
);
4629 status
= lookup_clientid(clid
, cstate
, nn
);
4633 status
= nfserr_cb_path_down
;
4634 if (!list_empty(&clp
->cl_delegations
)
4635 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
4643 nfsd4_end_grace(struct nfsd_net
*nn
)
4645 /* do nothing if grace period already ended */
4646 if (nn
->grace_ended
)
4649 dprintk("NFSD: end of grace period\n");
4650 nn
->grace_ended
= true;
4652 * If the server goes down again right now, an NFSv4
4653 * client will still be allowed to reclaim after it comes back up,
4654 * even if it hasn't yet had a chance to reclaim state this time.
4657 nfsd4_record_grace_done(nn
);
4659 * At this point, NFSv4 clients can still reclaim. But if the
4660 * server crashes, any that have not yet reclaimed will be out
4661 * of luck on the next boot.
4663 * (NFSv4.1+ clients are considered to have reclaimed once they
4664 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4665 * have reclaimed after their first OPEN.)
4667 locks_end_grace(&nn
->nfsd4_manager
);
4669 * At this point, and once lockd and/or any other containers
4670 * exit their grace period, further reclaims will fail and
4671 * regular locking can resume.
4676 nfs4_laundromat(struct nfsd_net
*nn
)
4678 struct nfs4_client
*clp
;
4679 struct nfs4_openowner
*oo
;
4680 struct nfs4_delegation
*dp
;
4681 struct nfs4_ol_stateid
*stp
;
4682 struct nfsd4_blocked_lock
*nbl
;
4683 struct list_head
*pos
, *next
, reaplist
;
4684 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
4685 time_t t
, new_timeo
= nn
->nfsd4_lease
;
4687 dprintk("NFSD: laundromat service - starting\n");
4688 nfsd4_end_grace(nn
);
4689 INIT_LIST_HEAD(&reaplist
);
4690 spin_lock(&nn
->client_lock
);
4691 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
4692 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4693 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
4694 t
= clp
->cl_time
- cutoff
;
4695 new_timeo
= min(new_timeo
, t
);
4698 if (mark_client_expired_locked(clp
)) {
4699 dprintk("NFSD: client in use (clientid %08x)\n",
4700 clp
->cl_clientid
.cl_id
);
4703 list_add(&clp
->cl_lru
, &reaplist
);
4705 spin_unlock(&nn
->client_lock
);
4706 list_for_each_safe(pos
, next
, &reaplist
) {
4707 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4708 dprintk("NFSD: purging unused client (clientid %08x)\n",
4709 clp
->cl_clientid
.cl_id
);
4710 list_del_init(&clp
->cl_lru
);
4713 spin_lock(&state_lock
);
4714 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
4715 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4716 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
4717 t
= dp
->dl_time
- cutoff
;
4718 new_timeo
= min(new_timeo
, t
);
4721 WARN_ON(!unhash_delegation_locked(dp
));
4722 list_add(&dp
->dl_recall_lru
, &reaplist
);
4724 spin_unlock(&state_lock
);
4725 while (!list_empty(&reaplist
)) {
4726 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
4728 list_del_init(&dp
->dl_recall_lru
);
4729 revoke_delegation(dp
);
4732 spin_lock(&nn
->client_lock
);
4733 while (!list_empty(&nn
->close_lru
)) {
4734 oo
= list_first_entry(&nn
->close_lru
, struct nfs4_openowner
,
4736 if (time_after((unsigned long)oo
->oo_time
,
4737 (unsigned long)cutoff
)) {
4738 t
= oo
->oo_time
- cutoff
;
4739 new_timeo
= min(new_timeo
, t
);
4742 list_del_init(&oo
->oo_close_lru
);
4743 stp
= oo
->oo_last_closed_stid
;
4744 oo
->oo_last_closed_stid
= NULL
;
4745 spin_unlock(&nn
->client_lock
);
4746 nfs4_put_stid(&stp
->st_stid
);
4747 spin_lock(&nn
->client_lock
);
4749 spin_unlock(&nn
->client_lock
);
4752 * It's possible for a client to try and acquire an already held lock
4753 * that is being held for a long time, and then lose interest in it.
4754 * So, we clean out any un-revisited request after a lease period
4755 * under the assumption that the client is no longer interested.
4757 * RFC5661, sec. 9.6 states that the client must not rely on getting
4758 * notifications and must continue to poll for locks, even when the
4759 * server supports them. Thus this shouldn't lead to clients blocking
4760 * indefinitely once the lock does become free.
4762 BUG_ON(!list_empty(&reaplist
));
4763 spin_lock(&nn
->blocked_locks_lock
);
4764 while (!list_empty(&nn
->blocked_locks_lru
)) {
4765 nbl
= list_first_entry(&nn
->blocked_locks_lru
,
4766 struct nfsd4_blocked_lock
, nbl_lru
);
4767 if (time_after((unsigned long)nbl
->nbl_time
,
4768 (unsigned long)cutoff
)) {
4769 t
= nbl
->nbl_time
- cutoff
;
4770 new_timeo
= min(new_timeo
, t
);
4773 list_move(&nbl
->nbl_lru
, &reaplist
);
4774 list_del_init(&nbl
->nbl_list
);
4776 spin_unlock(&nn
->blocked_locks_lock
);
4778 while (!list_empty(&reaplist
)) {
4779 nbl
= list_first_entry(&reaplist
,
4780 struct nfsd4_blocked_lock
, nbl_lru
);
4781 list_del_init(&nbl
->nbl_lru
);
4782 posix_unblock_lock(&nbl
->nbl_lock
);
4783 free_blocked_lock(nbl
);
4786 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
4790 static struct workqueue_struct
*laundry_wq
;
4791 static void laundromat_main(struct work_struct
*);
4794 laundromat_main(struct work_struct
*laundry
)
4797 struct delayed_work
*dwork
= to_delayed_work(laundry
);
4798 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
4801 t
= nfs4_laundromat(nn
);
4802 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
4803 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
4806 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stid
*stp
)
4808 if (!fh_match(&fhp
->fh_handle
, &stp
->sc_file
->fi_fhandle
))
4809 return nfserr_bad_stateid
;
4814 access_permit_read(struct nfs4_ol_stateid
*stp
)
4816 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
4817 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
4818 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
4822 access_permit_write(struct nfs4_ol_stateid
*stp
)
4824 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
4825 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
4829 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
4831 __be32 status
= nfserr_openmode
;
4833 /* For lock stateid's, we test the parent open, not the lock: */
4834 if (stp
->st_openstp
)
4835 stp
= stp
->st_openstp
;
4836 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
4838 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
4845 static inline __be32
4846 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
4848 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
4850 else if (opens_in_grace(net
)) {
4851 /* Answer in remaining cases depends on existence of
4852 * conflicting state; so we must wait out the grace period. */
4853 return nfserr_grace
;
4854 } else if (flags
& WR_STATE
)
4855 return nfs4_share_conflict(current_fh
,
4856 NFS4_SHARE_DENY_WRITE
);
4857 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4858 return nfs4_share_conflict(current_fh
,
4859 NFS4_SHARE_DENY_READ
);
4863 * Allow READ/WRITE during grace period on recovered state only for files
4864 * that are not able to provide mandatory locking.
4867 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4869 return opens_in_grace(net
) && mandatory_lock(inode
);
4872 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4875 * When sessions are used the stateid generation number is ignored
4878 if (has_session
&& in
->si_generation
== 0)
4881 if (in
->si_generation
== ref
->si_generation
)
4884 /* If the client sends us a stateid from the future, it's buggy: */
4885 if (nfsd4_stateid_generation_after(in
, ref
))
4886 return nfserr_bad_stateid
;
4888 * However, we could see a stateid from the past, even from a
4889 * non-buggy client. For example, if the client sends a lock
4890 * while some IO is outstanding, the lock may bump si_generation
4891 * while the IO is still in flight. The client could avoid that
4892 * situation by waiting for responses on all the IO requests,
4893 * but better performance may result in retrying IO that
4894 * receives an old_stateid error if requests are rarely
4895 * reordered in flight:
4897 return nfserr_old_stateid
;
4900 static __be32
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid
*ols
)
4902 if (ols
->st_stateowner
->so_is_open_owner
&&
4903 !(openowner(ols
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4904 return nfserr_bad_stateid
;
4908 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4910 struct nfs4_stid
*s
;
4911 __be32 status
= nfserr_bad_stateid
;
4913 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
4914 CLOSE_STATEID(stateid
))
4916 /* Client debugging aid. */
4917 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4918 char addr_str
[INET6_ADDRSTRLEN
];
4919 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4921 pr_warn_ratelimited("NFSD: client %s testing state ID "
4922 "with incorrect client ID\n", addr_str
);
4925 spin_lock(&cl
->cl_lock
);
4926 s
= find_stateid_locked(cl
, stateid
);
4929 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4932 switch (s
->sc_type
) {
4933 case NFS4_DELEG_STID
:
4936 case NFS4_REVOKED_DELEG_STID
:
4937 status
= nfserr_deleg_revoked
;
4939 case NFS4_OPEN_STID
:
4940 case NFS4_LOCK_STID
:
4941 status
= nfsd4_check_openowner_confirmed(openlockstateid(s
));
4944 printk("unknown stateid type %x\n", s
->sc_type
);
4946 case NFS4_CLOSED_STID
:
4947 case NFS4_CLOSED_DELEG_STID
:
4948 status
= nfserr_bad_stateid
;
4951 spin_unlock(&cl
->cl_lock
);
4956 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
4957 stateid_t
*stateid
, unsigned char typemask
,
4958 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
4961 bool return_revoked
= false;
4964 * only return revoked delegations if explicitly asked.
4965 * otherwise we report revoked or bad_stateid status.
4967 if (typemask
& NFS4_REVOKED_DELEG_STID
)
4968 return_revoked
= true;
4969 else if (typemask
& NFS4_DELEG_STID
)
4970 typemask
|= NFS4_REVOKED_DELEG_STID
;
4972 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
4973 CLOSE_STATEID(stateid
))
4974 return nfserr_bad_stateid
;
4975 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
4976 if (status
== nfserr_stale_clientid
) {
4977 if (cstate
->session
)
4978 return nfserr_bad_stateid
;
4979 return nfserr_stale_stateid
;
4983 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
4985 return nfserr_bad_stateid
;
4986 if (((*s
)->sc_type
== NFS4_REVOKED_DELEG_STID
) && !return_revoked
) {
4988 if (cstate
->minorversion
)
4989 return nfserr_deleg_revoked
;
4990 return nfserr_bad_stateid
;
4995 static struct file
*
4996 nfs4_find_file(struct nfs4_stid
*s
, int flags
)
5001 switch (s
->sc_type
) {
5002 case NFS4_DELEG_STID
:
5003 if (WARN_ON_ONCE(!s
->sc_file
->fi_deleg_file
))
5005 return get_file(s
->sc_file
->fi_deleg_file
);
5006 case NFS4_OPEN_STID
:
5007 case NFS4_LOCK_STID
:
5008 if (flags
& RD_STATE
)
5009 return find_readable_file(s
->sc_file
);
5011 return find_writeable_file(s
->sc_file
);
5019 nfs4_check_olstateid(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*ols
, int flags
)
5023 status
= nfsd4_check_openowner_confirmed(ols
);
5026 return nfs4_check_openmode(ols
, flags
);
5030 nfs4_check_file(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct nfs4_stid
*s
,
5031 struct file
**filpp
, bool *tmp_file
, int flags
)
5033 int acc
= (flags
& RD_STATE
) ? NFSD_MAY_READ
: NFSD_MAY_WRITE
;
5037 file
= nfs4_find_file(s
, flags
);
5039 status
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
5040 acc
| NFSD_MAY_OWNER_OVERRIDE
);
5048 status
= nfsd_open(rqstp
, fhp
, S_IFREG
, acc
, filpp
);
5060 * Checks for stateid operations
5063 nfs4_preprocess_stateid_op(struct svc_rqst
*rqstp
,
5064 struct nfsd4_compound_state
*cstate
, struct svc_fh
*fhp
,
5065 stateid_t
*stateid
, int flags
, struct file
**filpp
, bool *tmp_file
)
5067 struct inode
*ino
= d_inode(fhp
->fh_dentry
);
5068 struct net
*net
= SVC_NET(rqstp
);
5069 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5070 struct nfs4_stid
*s
= NULL
;
5078 if (grace_disallows_io(net
, ino
))
5079 return nfserr_grace
;
5081 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
5082 status
= check_special_stateids(net
, fhp
, stateid
, flags
);
5086 status
= nfsd4_lookup_stateid(cstate
, stateid
,
5087 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
5091 status
= check_stateid_generation(stateid
, &s
->sc_stateid
,
5092 nfsd4_has_session(cstate
));
5096 switch (s
->sc_type
) {
5097 case NFS4_DELEG_STID
:
5098 status
= nfs4_check_delegmode(delegstateid(s
), flags
);
5100 case NFS4_OPEN_STID
:
5101 case NFS4_LOCK_STID
:
5102 status
= nfs4_check_olstateid(fhp
, openlockstateid(s
), flags
);
5105 status
= nfserr_bad_stateid
;
5110 status
= nfs4_check_fh(fhp
, s
);
5113 if (!status
&& filpp
)
5114 status
= nfs4_check_file(rqstp
, fhp
, s
, filpp
, tmp_file
, flags
);
5122 * Test if the stateid is valid
5125 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5126 union nfsd4_op_u
*u
)
5128 struct nfsd4_test_stateid
*test_stateid
= &u
->test_stateid
;
5129 struct nfsd4_test_stateid_id
*stateid
;
5130 struct nfs4_client
*cl
= cstate
->session
->se_client
;
5132 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
5133 stateid
->ts_id_status
=
5134 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
5140 nfsd4_free_lock_stateid(stateid_t
*stateid
, struct nfs4_stid
*s
)
5142 struct nfs4_ol_stateid
*stp
= openlockstateid(s
);
5145 mutex_lock(&stp
->st_mutex
);
5147 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
5151 ret
= nfserr_locks_held
;
5152 if (check_for_locks(stp
->st_stid
.sc_file
,
5153 lockowner(stp
->st_stateowner
)))
5156 release_lock_stateid(stp
);
5160 mutex_unlock(&stp
->st_mutex
);
5166 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5167 union nfsd4_op_u
*u
)
5169 struct nfsd4_free_stateid
*free_stateid
= &u
->free_stateid
;
5170 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
5171 struct nfs4_stid
*s
;
5172 struct nfs4_delegation
*dp
;
5173 struct nfs4_client
*cl
= cstate
->session
->se_client
;
5174 __be32 ret
= nfserr_bad_stateid
;
5176 spin_lock(&cl
->cl_lock
);
5177 s
= find_stateid_locked(cl
, stateid
);
5180 switch (s
->sc_type
) {
5181 case NFS4_DELEG_STID
:
5182 ret
= nfserr_locks_held
;
5184 case NFS4_OPEN_STID
:
5185 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
5188 ret
= nfserr_locks_held
;
5190 case NFS4_LOCK_STID
:
5191 atomic_inc(&s
->sc_count
);
5192 spin_unlock(&cl
->cl_lock
);
5193 ret
= nfsd4_free_lock_stateid(stateid
, s
);
5195 case NFS4_REVOKED_DELEG_STID
:
5196 dp
= delegstateid(s
);
5197 list_del_init(&dp
->dl_recall_lru
);
5198 spin_unlock(&cl
->cl_lock
);
5202 /* Default falls through and returns nfserr_bad_stateid */
5205 spin_unlock(&cl
->cl_lock
);
5213 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
5214 RD_STATE
: WR_STATE
;
5217 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
5219 struct svc_fh
*current_fh
= &cstate
->current_fh
;
5220 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
5223 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
5226 status
= nfsd4_lock_ol_stateid(stp
);
5227 if (status
!= nfs_ok
)
5229 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5230 if (status
== nfs_ok
)
5231 status
= nfs4_check_fh(current_fh
, &stp
->st_stid
);
5232 if (status
!= nfs_ok
)
5233 mutex_unlock(&stp
->st_mutex
);
5238 * Checks for sequence id mutating operations.
5241 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
5242 stateid_t
*stateid
, char typemask
,
5243 struct nfs4_ol_stateid
**stpp
,
5244 struct nfsd_net
*nn
)
5247 struct nfs4_stid
*s
;
5248 struct nfs4_ol_stateid
*stp
= NULL
;
5250 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
5251 seqid
, STATEID_VAL(stateid
));
5254 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
5257 stp
= openlockstateid(s
);
5258 nfsd4_cstate_assign_replay(cstate
, stp
->st_stateowner
);
5260 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
5264 nfs4_put_stid(&stp
->st_stid
);
5268 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
5269 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
5272 struct nfs4_openowner
*oo
;
5273 struct nfs4_ol_stateid
*stp
;
5275 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
5276 NFS4_OPEN_STID
, &stp
, nn
);
5279 oo
= openowner(stp
->st_stateowner
);
5280 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
5281 mutex_unlock(&stp
->st_mutex
);
5282 nfs4_put_stid(&stp
->st_stid
);
5283 return nfserr_bad_stateid
;
5290 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5291 union nfsd4_op_u
*u
)
5293 struct nfsd4_open_confirm
*oc
= &u
->open_confirm
;
5295 struct nfs4_openowner
*oo
;
5296 struct nfs4_ol_stateid
*stp
;
5297 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5299 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5300 cstate
->current_fh
.fh_dentry
);
5302 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
5306 status
= nfs4_preprocess_seqid_op(cstate
,
5307 oc
->oc_seqid
, &oc
->oc_req_stateid
,
5308 NFS4_OPEN_STID
, &stp
, nn
);
5311 oo
= openowner(stp
->st_stateowner
);
5312 status
= nfserr_bad_stateid
;
5313 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
) {
5314 mutex_unlock(&stp
->st_mutex
);
5317 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
5318 nfs4_inc_and_copy_stateid(&oc
->oc_resp_stateid
, &stp
->st_stid
);
5319 mutex_unlock(&stp
->st_mutex
);
5320 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
5321 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
5323 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
5326 nfs4_put_stid(&stp
->st_stid
);
5328 nfsd4_bump_seqid(cstate
, status
);
5332 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
5334 if (!test_access(access
, stp
))
5336 nfs4_file_put_access(stp
->st_stid
.sc_file
, access
);
5337 clear_access(access
, stp
);
5340 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
5342 switch (to_access
) {
5343 case NFS4_SHARE_ACCESS_READ
:
5344 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
5345 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
5347 case NFS4_SHARE_ACCESS_WRITE
:
5348 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
5349 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
5351 case NFS4_SHARE_ACCESS_BOTH
:
5359 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
5360 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
5362 struct nfsd4_open_downgrade
*od
= &u
->open_downgrade
;
5364 struct nfs4_ol_stateid
*stp
;
5365 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5367 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5368 cstate
->current_fh
.fh_dentry
);
5370 /* We don't yet support WANT bits: */
5371 if (od
->od_deleg_want
)
5372 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
5375 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
5376 &od
->od_stateid
, &stp
, nn
);
5379 status
= nfserr_inval
;
5380 if (!test_access(od
->od_share_access
, stp
)) {
5381 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5382 stp
->st_access_bmap
, od
->od_share_access
);
5385 if (!test_deny(od
->od_share_deny
, stp
)) {
5386 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5387 stp
->st_deny_bmap
, od
->od_share_deny
);
5390 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
5391 reset_union_bmap_deny(od
->od_share_deny
, stp
);
5392 nfs4_inc_and_copy_stateid(&od
->od_stateid
, &stp
->st_stid
);
5395 mutex_unlock(&stp
->st_mutex
);
5396 nfs4_put_stid(&stp
->st_stid
);
5398 nfsd4_bump_seqid(cstate
, status
);
5402 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
5404 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
5406 LIST_HEAD(reaplist
);
5408 spin_lock(&clp
->cl_lock
);
5409 unhashed
= unhash_open_stateid(s
, &reaplist
);
5411 if (clp
->cl_minorversion
) {
5413 put_ol_stateid_locked(s
, &reaplist
);
5414 spin_unlock(&clp
->cl_lock
);
5415 free_ol_stateid_reaplist(&reaplist
);
5417 spin_unlock(&clp
->cl_lock
);
5418 free_ol_stateid_reaplist(&reaplist
);
5420 move_to_close_lru(s
, clp
->net
);
5425 * nfs4_unlock_state() called after encode
5428 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5429 union nfsd4_op_u
*u
)
5431 struct nfsd4_close
*close
= &u
->close
;
5433 struct nfs4_ol_stateid
*stp
;
5434 struct net
*net
= SVC_NET(rqstp
);
5435 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5437 dprintk("NFSD: nfsd4_close on file %pd\n",
5438 cstate
->current_fh
.fh_dentry
);
5440 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
5442 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
5444 nfsd4_bump_seqid(cstate
, status
);
5448 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
5449 nfs4_inc_and_copy_stateid(&close
->cl_stateid
, &stp
->st_stid
);
5451 nfsd4_close_open_stateid(stp
);
5452 mutex_unlock(&stp
->st_mutex
);
5454 /* See RFC5661 sectionm 18.2.4 */
5455 if (stp
->st_stid
.sc_client
->cl_minorversion
)
5456 memcpy(&close
->cl_stateid
, &close_stateid
,
5457 sizeof(close
->cl_stateid
));
5459 /* put reference from nfs4_preprocess_seqid_op */
5460 nfs4_put_stid(&stp
->st_stid
);
5466 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5467 union nfsd4_op_u
*u
)
5469 struct nfsd4_delegreturn
*dr
= &u
->delegreturn
;
5470 struct nfs4_delegation
*dp
;
5471 stateid_t
*stateid
= &dr
->dr_stateid
;
5472 struct nfs4_stid
*s
;
5474 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5476 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5479 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
5482 dp
= delegstateid(s
);
5483 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5487 destroy_delegation(dp
);
5489 nfs4_put_stid(&dp
->dl_stid
);
5495 end_offset(u64 start
, u64 len
)
5500 return end
>= start
? end
: NFS4_MAX_UINT64
;
5503 /* last octet in a range */
5505 last_byte_offset(u64 start
, u64 len
)
5511 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
5515 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5516 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5517 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5518 * locking, this prevents us from being completely protocol-compliant. The
5519 * real solution to this problem is to start using unsigned file offsets in
5520 * the VFS, but this is a very deep change!
5523 nfs4_transform_lock_offset(struct file_lock
*lock
)
5525 if (lock
->fl_start
< 0)
5526 lock
->fl_start
= OFFSET_MAX
;
5527 if (lock
->fl_end
< 0)
5528 lock
->fl_end
= OFFSET_MAX
;
5532 nfsd4_fl_get_owner(fl_owner_t owner
)
5534 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5536 nfs4_get_stateowner(&lo
->lo_owner
);
5541 nfsd4_fl_put_owner(fl_owner_t owner
)
5543 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5546 nfs4_put_stateowner(&lo
->lo_owner
);
5550 nfsd4_lm_notify(struct file_lock
*fl
)
5552 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)fl
->fl_owner
;
5553 struct net
*net
= lo
->lo_owner
.so_client
->net
;
5554 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5555 struct nfsd4_blocked_lock
*nbl
= container_of(fl
,
5556 struct nfsd4_blocked_lock
, nbl_lock
);
5559 /* An empty list means that something else is going to be using it */
5560 spin_lock(&nn
->blocked_locks_lock
);
5561 if (!list_empty(&nbl
->nbl_list
)) {
5562 list_del_init(&nbl
->nbl_list
);
5563 list_del_init(&nbl
->nbl_lru
);
5566 spin_unlock(&nn
->blocked_locks_lock
);
5569 nfsd4_run_cb(&nbl
->nbl_cb
);
5572 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
5573 .lm_notify
= nfsd4_lm_notify
,
5574 .lm_get_owner
= nfsd4_fl_get_owner
,
5575 .lm_put_owner
= nfsd4_fl_put_owner
,
5579 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
5581 struct nfs4_lockowner
*lo
;
5583 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
5584 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
5585 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
5586 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
5587 if (!deny
->ld_owner
.data
)
5588 /* We just don't care that much */
5590 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
5591 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
5594 deny
->ld_owner
.len
= 0;
5595 deny
->ld_owner
.data
= NULL
;
5596 deny
->ld_clientid
.cl_boot
= 0;
5597 deny
->ld_clientid
.cl_id
= 0;
5599 deny
->ld_start
= fl
->fl_start
;
5600 deny
->ld_length
= NFS4_MAX_UINT64
;
5601 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
5602 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
5603 deny
->ld_type
= NFS4_READ_LT
;
5604 if (fl
->fl_type
!= F_RDLCK
)
5605 deny
->ld_type
= NFS4_WRITE_LT
;
5608 static struct nfs4_lockowner
*
5609 find_lockowner_str_locked(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5611 unsigned int strhashval
= ownerstr_hashval(owner
);
5612 struct nfs4_stateowner
*so
;
5614 lockdep_assert_held(&clp
->cl_lock
);
5616 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[strhashval
],
5618 if (so
->so_is_open_owner
)
5620 if (same_owner_str(so
, owner
))
5621 return lockowner(nfs4_get_stateowner(so
));
5626 static struct nfs4_lockowner
*
5627 find_lockowner_str(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5629 struct nfs4_lockowner
*lo
;
5631 spin_lock(&clp
->cl_lock
);
5632 lo
= find_lockowner_str_locked(clp
, owner
);
5633 spin_unlock(&clp
->cl_lock
);
5637 static void nfs4_unhash_lockowner(struct nfs4_stateowner
*sop
)
5639 unhash_lockowner_locked(lockowner(sop
));
5642 static void nfs4_free_lockowner(struct nfs4_stateowner
*sop
)
5644 struct nfs4_lockowner
*lo
= lockowner(sop
);
5646 kmem_cache_free(lockowner_slab
, lo
);
5649 static const struct nfs4_stateowner_operations lockowner_ops
= {
5650 .so_unhash
= nfs4_unhash_lockowner
,
5651 .so_free
= nfs4_free_lockowner
,
5655 * Alloc a lock owner structure.
5656 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5659 * strhashval = ownerstr_hashval
5661 static struct nfs4_lockowner
*
5662 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
,
5663 struct nfs4_ol_stateid
*open_stp
,
5664 struct nfsd4_lock
*lock
)
5666 struct nfs4_lockowner
*lo
, *ret
;
5668 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
5671 INIT_LIST_HEAD(&lo
->lo_blocked
);
5672 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
5673 lo
->lo_owner
.so_is_open_owner
= 0;
5674 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
;
5675 lo
->lo_owner
.so_ops
= &lockowner_ops
;
5676 spin_lock(&clp
->cl_lock
);
5677 ret
= find_lockowner_str_locked(clp
, &lock
->lk_new_owner
);
5679 list_add(&lo
->lo_owner
.so_strhash
,
5680 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
5683 nfs4_free_stateowner(&lo
->lo_owner
);
5685 spin_unlock(&clp
->cl_lock
);
5690 init_lock_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_lockowner
*lo
,
5691 struct nfs4_file
*fp
, struct inode
*inode
,
5692 struct nfs4_ol_stateid
*open_stp
)
5694 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5696 lockdep_assert_held(&clp
->cl_lock
);
5698 atomic_inc(&stp
->st_stid
.sc_count
);
5699 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
5700 stp
->st_stateowner
= nfs4_get_stateowner(&lo
->lo_owner
);
5702 stp
->st_stid
.sc_file
= fp
;
5703 stp
->st_access_bmap
= 0;
5704 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
5705 stp
->st_openstp
= open_stp
;
5706 mutex_init(&stp
->st_mutex
);
5707 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
5708 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
5709 spin_lock(&fp
->fi_lock
);
5710 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
5711 spin_unlock(&fp
->fi_lock
);
5714 static struct nfs4_ol_stateid
*
5715 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
5717 struct nfs4_ol_stateid
*lst
;
5718 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5720 lockdep_assert_held(&clp
->cl_lock
);
5722 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
5723 if (lst
->st_stid
.sc_file
== fp
) {
5724 atomic_inc(&lst
->st_stid
.sc_count
);
5731 static struct nfs4_ol_stateid
*
5732 find_or_create_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fi
,
5733 struct inode
*inode
, struct nfs4_ol_stateid
*ost
,
5736 struct nfs4_stid
*ns
= NULL
;
5737 struct nfs4_ol_stateid
*lst
;
5738 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5739 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
5741 spin_lock(&clp
->cl_lock
);
5742 lst
= find_lock_stateid(lo
, fi
);
5744 spin_unlock(&clp
->cl_lock
);
5745 ns
= nfs4_alloc_stid(clp
, stateid_slab
, nfs4_free_lock_stateid
);
5749 spin_lock(&clp
->cl_lock
);
5750 lst
= find_lock_stateid(lo
, fi
);
5752 lst
= openlockstateid(ns
);
5753 init_lock_stateid(lst
, lo
, fi
, inode
, ost
);
5758 spin_unlock(&clp
->cl_lock
);
5765 check_lock_length(u64 offset
, u64 length
)
5767 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
5768 (length
> ~offset
)));
5771 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
5773 struct nfs4_file
*fp
= lock_stp
->st_stid
.sc_file
;
5775 lockdep_assert_held(&fp
->fi_lock
);
5777 if (test_access(access
, lock_stp
))
5779 __nfs4_file_get_access(fp
, access
);
5780 set_access(access
, lock_stp
);
5784 lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
,
5785 struct nfs4_ol_stateid
*ost
,
5786 struct nfsd4_lock
*lock
,
5787 struct nfs4_ol_stateid
**plst
, bool *new)
5790 struct nfs4_file
*fi
= ost
->st_stid
.sc_file
;
5791 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5792 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
5793 struct inode
*inode
= d_inode(cstate
->current_fh
.fh_dentry
);
5794 struct nfs4_lockowner
*lo
;
5795 struct nfs4_ol_stateid
*lst
;
5796 unsigned int strhashval
;
5799 lo
= find_lockowner_str(cl
, &lock
->lk_new_owner
);
5801 strhashval
= ownerstr_hashval(&lock
->lk_new_owner
);
5802 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
5804 return nfserr_jukebox
;
5806 /* with an existing lockowner, seqids must be the same */
5807 status
= nfserr_bad_seqid
;
5808 if (!cstate
->minorversion
&&
5809 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
5814 lst
= find_or_create_lock_stateid(lo
, fi
, inode
, ost
, new);
5816 status
= nfserr_jukebox
;
5820 mutex_lock(&lst
->st_mutex
);
5822 /* See if it's still hashed to avoid race with FREE_STATEID */
5823 spin_lock(&cl
->cl_lock
);
5824 hashed
= !list_empty(&lst
->st_perfile
);
5825 spin_unlock(&cl
->cl_lock
);
5828 mutex_unlock(&lst
->st_mutex
);
5829 nfs4_put_stid(&lst
->st_stid
);
5835 nfs4_put_stateowner(&lo
->lo_owner
);
5843 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5844 union nfsd4_op_u
*u
)
5846 struct nfsd4_lock
*lock
= &u
->lock
;
5847 struct nfs4_openowner
*open_sop
= NULL
;
5848 struct nfs4_lockowner
*lock_sop
= NULL
;
5849 struct nfs4_ol_stateid
*lock_stp
= NULL
;
5850 struct nfs4_ol_stateid
*open_stp
= NULL
;
5851 struct nfs4_file
*fp
;
5852 struct file
*filp
= NULL
;
5853 struct nfsd4_blocked_lock
*nbl
= NULL
;
5854 struct file_lock
*file_lock
= NULL
;
5855 struct file_lock
*conflock
= NULL
;
5860 unsigned char fl_type
;
5861 unsigned int fl_flags
= FL_POSIX
;
5862 struct net
*net
= SVC_NET(rqstp
);
5863 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5865 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5866 (long long) lock
->lk_offset
,
5867 (long long) lock
->lk_length
);
5869 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
5870 return nfserr_inval
;
5872 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
5873 S_IFREG
, NFSD_MAY_LOCK
))) {
5874 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5878 if (lock
->lk_is_new
) {
5879 if (nfsd4_has_session(cstate
))
5880 /* See rfc 5661 18.10.3: given clientid is ignored: */
5881 memcpy(&lock
->lk_new_clientid
,
5882 &cstate
->session
->se_client
->cl_clientid
,
5883 sizeof(clientid_t
));
5885 status
= nfserr_stale_clientid
;
5886 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
5889 /* validate and update open stateid and open seqid */
5890 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
5891 lock
->lk_new_open_seqid
,
5892 &lock
->lk_new_open_stateid
,
5896 mutex_unlock(&open_stp
->st_mutex
);
5897 open_sop
= openowner(open_stp
->st_stateowner
);
5898 status
= nfserr_bad_stateid
;
5899 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
5900 &lock
->lk_new_clientid
))
5902 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
5905 status
= nfs4_preprocess_seqid_op(cstate
,
5906 lock
->lk_old_lock_seqid
,
5907 &lock
->lk_old_lock_stateid
,
5908 NFS4_LOCK_STID
, &lock_stp
, nn
);
5912 lock_sop
= lockowner(lock_stp
->st_stateowner
);
5914 lkflg
= setlkflg(lock
->lk_type
);
5915 status
= nfs4_check_openmode(lock_stp
, lkflg
);
5919 status
= nfserr_grace
;
5920 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
5922 status
= nfserr_no_grace
;
5923 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
5926 fp
= lock_stp
->st_stid
.sc_file
;
5927 switch (lock
->lk_type
) {
5929 if (nfsd4_has_session(cstate
))
5930 fl_flags
|= FL_SLEEP
;
5933 spin_lock(&fp
->fi_lock
);
5934 filp
= find_readable_file_locked(fp
);
5936 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
5937 spin_unlock(&fp
->fi_lock
);
5940 case NFS4_WRITEW_LT
:
5941 if (nfsd4_has_session(cstate
))
5942 fl_flags
|= FL_SLEEP
;
5945 spin_lock(&fp
->fi_lock
);
5946 filp
= find_writeable_file_locked(fp
);
5948 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
5949 spin_unlock(&fp
->fi_lock
);
5953 status
= nfserr_inval
;
5958 status
= nfserr_openmode
;
5962 nbl
= find_or_allocate_block(lock_sop
, &fp
->fi_fhandle
, nn
);
5964 dprintk("NFSD: %s: unable to allocate block!\n", __func__
);
5965 status
= nfserr_jukebox
;
5969 file_lock
= &nbl
->nbl_lock
;
5970 file_lock
->fl_type
= fl_type
;
5971 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lock_sop
->lo_owner
));
5972 file_lock
->fl_pid
= current
->tgid
;
5973 file_lock
->fl_file
= filp
;
5974 file_lock
->fl_flags
= fl_flags
;
5975 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
5976 file_lock
->fl_start
= lock
->lk_offset
;
5977 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
5978 nfs4_transform_lock_offset(file_lock
);
5980 conflock
= locks_alloc_lock();
5982 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
5983 status
= nfserr_jukebox
;
5987 if (fl_flags
& FL_SLEEP
) {
5988 nbl
->nbl_time
= jiffies
;
5989 spin_lock(&nn
->blocked_locks_lock
);
5990 list_add_tail(&nbl
->nbl_list
, &lock_sop
->lo_blocked
);
5991 list_add_tail(&nbl
->nbl_lru
, &nn
->blocked_locks_lru
);
5992 spin_unlock(&nn
->blocked_locks_lock
);
5995 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
5997 case 0: /* success! */
5998 nfs4_inc_and_copy_stateid(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
);
6001 case FILE_LOCK_DEFERRED
:
6004 case -EAGAIN
: /* conflock holds conflicting lock */
6005 status
= nfserr_denied
;
6006 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6007 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
6010 status
= nfserr_deadlock
;
6013 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
6014 status
= nfserrno(err
);
6019 /* dequeue it if we queued it before */
6020 if (fl_flags
& FL_SLEEP
) {
6021 spin_lock(&nn
->blocked_locks_lock
);
6022 list_del_init(&nbl
->nbl_list
);
6023 list_del_init(&nbl
->nbl_lru
);
6024 spin_unlock(&nn
->blocked_locks_lock
);
6026 free_blocked_lock(nbl
);
6031 /* Bump seqid manually if the 4.0 replay owner is openowner */
6032 if (cstate
->replay_owner
&&
6033 cstate
->replay_owner
!= &lock_sop
->lo_owner
&&
6034 seqid_mutating_err(ntohl(status
)))
6035 lock_sop
->lo_owner
.so_seqid
++;
6037 mutex_unlock(&lock_stp
->st_mutex
);
6040 * If this is a new, never-before-used stateid, and we are
6041 * returning an error, then just go ahead and release it.
6044 release_lock_stateid(lock_stp
);
6046 nfs4_put_stid(&lock_stp
->st_stid
);
6049 nfs4_put_stid(&open_stp
->st_stid
);
6050 nfsd4_bump_seqid(cstate
, status
);
6052 locks_free_lock(conflock
);
6057 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6058 * so we do a temporary open here just to get an open file to pass to
6059 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6062 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
6065 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
6067 err
= nfserrno(vfs_test_lock(file
, lock
));
6077 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6078 union nfsd4_op_u
*u
)
6080 struct nfsd4_lockt
*lockt
= &u
->lockt
;
6081 struct file_lock
*file_lock
= NULL
;
6082 struct nfs4_lockowner
*lo
= NULL
;
6084 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6086 if (locks_in_grace(SVC_NET(rqstp
)))
6087 return nfserr_grace
;
6089 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
6090 return nfserr_inval
;
6092 if (!nfsd4_has_session(cstate
)) {
6093 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
6098 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
6101 file_lock
= locks_alloc_lock();
6103 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6104 status
= nfserr_jukebox
;
6108 switch (lockt
->lt_type
) {
6111 file_lock
->fl_type
= F_RDLCK
;
6114 case NFS4_WRITEW_LT
:
6115 file_lock
->fl_type
= F_WRLCK
;
6118 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6119 status
= nfserr_inval
;
6123 lo
= find_lockowner_str(cstate
->clp
, &lockt
->lt_owner
);
6125 file_lock
->fl_owner
= (fl_owner_t
)lo
;
6126 file_lock
->fl_pid
= current
->tgid
;
6127 file_lock
->fl_flags
= FL_POSIX
;
6129 file_lock
->fl_start
= lockt
->lt_offset
;
6130 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
6132 nfs4_transform_lock_offset(file_lock
);
6134 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
6138 if (file_lock
->fl_type
!= F_UNLCK
) {
6139 status
= nfserr_denied
;
6140 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
6144 nfs4_put_stateowner(&lo
->lo_owner
);
6146 locks_free_lock(file_lock
);
6151 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6152 union nfsd4_op_u
*u
)
6154 struct nfsd4_locku
*locku
= &u
->locku
;
6155 struct nfs4_ol_stateid
*stp
;
6156 struct file
*filp
= NULL
;
6157 struct file_lock
*file_lock
= NULL
;
6160 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6162 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6163 (long long) locku
->lu_offset
,
6164 (long long) locku
->lu_length
);
6166 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
6167 return nfserr_inval
;
6169 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
6170 &locku
->lu_stateid
, NFS4_LOCK_STID
,
6174 filp
= find_any_file(stp
->st_stid
.sc_file
);
6176 status
= nfserr_lock_range
;
6179 file_lock
= locks_alloc_lock();
6181 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6182 status
= nfserr_jukebox
;
6186 file_lock
->fl_type
= F_UNLCK
;
6187 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(stp
->st_stateowner
));
6188 file_lock
->fl_pid
= current
->tgid
;
6189 file_lock
->fl_file
= filp
;
6190 file_lock
->fl_flags
= FL_POSIX
;
6191 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
6192 file_lock
->fl_start
= locku
->lu_offset
;
6194 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
6196 nfs4_transform_lock_offset(file_lock
);
6198 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
6200 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6203 nfs4_inc_and_copy_stateid(&locku
->lu_stateid
, &stp
->st_stid
);
6207 mutex_unlock(&stp
->st_mutex
);
6208 nfs4_put_stid(&stp
->st_stid
);
6210 nfsd4_bump_seqid(cstate
, status
);
6212 locks_free_lock(file_lock
);
6216 status
= nfserrno(err
);
6222 * true: locks held by lockowner
6223 * false: no locks held by lockowner
6226 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
6228 struct file_lock
*fl
;
6230 struct file
*filp
= find_any_file(fp
);
6231 struct inode
*inode
;
6232 struct file_lock_context
*flctx
;
6235 /* Any valid lock stateid should have some sort of access */
6240 inode
= file_inode(filp
);
6241 flctx
= inode
->i_flctx
;
6243 if (flctx
&& !list_empty_careful(&flctx
->flc_posix
)) {
6244 spin_lock(&flctx
->flc_lock
);
6245 list_for_each_entry(fl
, &flctx
->flc_posix
, fl_list
) {
6246 if (fl
->fl_owner
== (fl_owner_t
)lowner
) {
6251 spin_unlock(&flctx
->flc_lock
);
6258 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
6259 struct nfsd4_compound_state
*cstate
,
6260 union nfsd4_op_u
*u
)
6262 struct nfsd4_release_lockowner
*rlockowner
= &u
->release_lockowner
;
6263 clientid_t
*clid
= &rlockowner
->rl_clientid
;
6264 struct nfs4_stateowner
*sop
;
6265 struct nfs4_lockowner
*lo
= NULL
;
6266 struct nfs4_ol_stateid
*stp
;
6267 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
6268 unsigned int hashval
= ownerstr_hashval(owner
);
6270 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6271 struct nfs4_client
*clp
;
6272 LIST_HEAD (reaplist
);
6274 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6275 clid
->cl_boot
, clid
->cl_id
);
6277 status
= lookup_clientid(clid
, cstate
, nn
);
6282 /* Find the matching lock stateowner */
6283 spin_lock(&clp
->cl_lock
);
6284 list_for_each_entry(sop
, &clp
->cl_ownerstr_hashtbl
[hashval
],
6287 if (sop
->so_is_open_owner
|| !same_owner_str(sop
, owner
))
6290 /* see if there are still any locks associated with it */
6291 lo
= lockowner(sop
);
6292 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
6293 if (check_for_locks(stp
->st_stid
.sc_file
, lo
)) {
6294 status
= nfserr_locks_held
;
6295 spin_unlock(&clp
->cl_lock
);
6300 nfs4_get_stateowner(sop
);
6304 spin_unlock(&clp
->cl_lock
);
6308 unhash_lockowner_locked(lo
);
6309 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
6310 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
6311 struct nfs4_ol_stateid
,
6313 WARN_ON(!unhash_lock_stateid(stp
));
6314 put_ol_stateid_locked(stp
, &reaplist
);
6316 spin_unlock(&clp
->cl_lock
);
6317 free_ol_stateid_reaplist(&reaplist
);
6318 remove_blocked_locks(lo
);
6319 nfs4_put_stateowner(&lo
->lo_owner
);
6324 static inline struct nfs4_client_reclaim
*
6327 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
6331 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
6333 struct nfs4_client_reclaim
*crp
;
6335 crp
= nfsd4_find_reclaim_client(name
, nn
);
6336 return (crp
&& crp
->cr_clp
);
6340 * failure => all reset bets are off, nfserr_no_grace...
6342 struct nfs4_client_reclaim
*
6343 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
6345 unsigned int strhashval
;
6346 struct nfs4_client_reclaim
*crp
;
6348 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
6349 crp
= alloc_reclaim();
6351 strhashval
= clientstr_hashval(name
);
6352 INIT_LIST_HEAD(&crp
->cr_strhash
);
6353 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
6354 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
6356 nn
->reclaim_str_hashtbl_size
++;
6362 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
6364 list_del(&crp
->cr_strhash
);
6366 nn
->reclaim_str_hashtbl_size
--;
6370 nfs4_release_reclaim(struct nfsd_net
*nn
)
6372 struct nfs4_client_reclaim
*crp
= NULL
;
6375 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6376 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
6377 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
6378 struct nfs4_client_reclaim
, cr_strhash
);
6379 nfs4_remove_reclaim_record(crp
, nn
);
6382 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
6386 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6387 struct nfs4_client_reclaim
*
6388 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
6390 unsigned int strhashval
;
6391 struct nfs4_client_reclaim
*crp
= NULL
;
6393 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
6395 strhashval
= clientstr_hashval(recdir
);
6396 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
6397 if (same_name(crp
->cr_recdir
, recdir
)) {
6405 * Called from OPEN. Look for clientid in reclaim list.
6408 nfs4_check_open_reclaim(clientid_t
*clid
,
6409 struct nfsd4_compound_state
*cstate
,
6410 struct nfsd_net
*nn
)
6414 /* find clientid in conf_id_hashtbl */
6415 status
= lookup_clientid(clid
, cstate
, nn
);
6417 return nfserr_reclaim_bad
;
6419 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
, &cstate
->clp
->cl_flags
))
6420 return nfserr_no_grace
;
6422 if (nfsd4_client_record_check(cstate
->clp
))
6423 return nfserr_reclaim_bad
;
6428 #ifdef CONFIG_NFSD_FAULT_INJECTION
6430 put_client(struct nfs4_client
*clp
)
6432 atomic_dec(&clp
->cl_refcount
);
6435 static struct nfs4_client
*
6436 nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
6438 struct nfs4_client
*clp
;
6439 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6442 if (!nfsd_netns_ready(nn
))
6445 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6446 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
6453 nfsd_inject_print_clients(void)
6455 struct nfs4_client
*clp
;
6457 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6459 char buf
[INET6_ADDRSTRLEN
];
6461 if (!nfsd_netns_ready(nn
))
6464 spin_lock(&nn
->client_lock
);
6465 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6466 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
6467 pr_info("NFS Client: %s\n", buf
);
6470 spin_unlock(&nn
->client_lock
);
6476 nfsd_inject_forget_client(struct sockaddr_storage
*addr
, size_t addr_size
)
6479 struct nfs4_client
*clp
;
6480 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6483 if (!nfsd_netns_ready(nn
))
6486 spin_lock(&nn
->client_lock
);
6487 clp
= nfsd_find_client(addr
, addr_size
);
6489 if (mark_client_expired_locked(clp
) == nfs_ok
)
6494 spin_unlock(&nn
->client_lock
);
6503 nfsd_inject_forget_clients(u64 max
)
6506 struct nfs4_client
*clp
, *next
;
6507 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6509 LIST_HEAD(reaplist
);
6511 if (!nfsd_netns_ready(nn
))
6514 spin_lock(&nn
->client_lock
);
6515 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6516 if (mark_client_expired_locked(clp
) == nfs_ok
) {
6517 list_add(&clp
->cl_lru
, &reaplist
);
6518 if (max
!= 0 && ++count
>= max
)
6522 spin_unlock(&nn
->client_lock
);
6524 list_for_each_entry_safe(clp
, next
, &reaplist
, cl_lru
)
6530 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
6533 char buf
[INET6_ADDRSTRLEN
];
6534 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
6535 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
6539 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid
*lst
,
6540 struct list_head
*collect
)
6542 struct nfs4_client
*clp
= lst
->st_stid
.sc_client
;
6543 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6549 lockdep_assert_held(&nn
->client_lock
);
6550 atomic_inc(&clp
->cl_refcount
);
6551 list_add(&lst
->st_locks
, collect
);
6554 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
6555 struct list_head
*collect
,
6556 bool (*func
)(struct nfs4_ol_stateid
*))
6558 struct nfs4_openowner
*oop
;
6559 struct nfs4_ol_stateid
*stp
, *st_next
;
6560 struct nfs4_ol_stateid
*lst
, *lst_next
;
6563 spin_lock(&clp
->cl_lock
);
6564 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
6565 list_for_each_entry_safe(stp
, st_next
,
6566 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
6567 list_for_each_entry_safe(lst
, lst_next
,
6568 &stp
->st_locks
, st_locks
) {
6571 nfsd_inject_add_lock_to_list(lst
,
6576 * Despite the fact that these functions deal
6577 * with 64-bit integers for "count", we must
6578 * ensure that it doesn't blow up the
6579 * clp->cl_refcount. Throw a warning if we
6580 * start to approach INT_MAX here.
6582 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6589 spin_unlock(&clp
->cl_lock
);
6595 nfsd_collect_client_locks(struct nfs4_client
*clp
, struct list_head
*collect
,
6598 return nfsd_foreach_client_lock(clp
, max
, collect
, unhash_lock_stateid
);
6602 nfsd_print_client_locks(struct nfs4_client
*clp
)
6604 u64 count
= nfsd_foreach_client_lock(clp
, 0, NULL
, NULL
);
6605 nfsd_print_count(clp
, count
, "locked files");
6610 nfsd_inject_print_locks(void)
6612 struct nfs4_client
*clp
;
6614 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6617 if (!nfsd_netns_ready(nn
))
6620 spin_lock(&nn
->client_lock
);
6621 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6622 count
+= nfsd_print_client_locks(clp
);
6623 spin_unlock(&nn
->client_lock
);
6629 nfsd_reap_locks(struct list_head
*reaplist
)
6631 struct nfs4_client
*clp
;
6632 struct nfs4_ol_stateid
*stp
, *next
;
6634 list_for_each_entry_safe(stp
, next
, reaplist
, st_locks
) {
6635 list_del_init(&stp
->st_locks
);
6636 clp
= stp
->st_stid
.sc_client
;
6637 nfs4_put_stid(&stp
->st_stid
);
6643 nfsd_inject_forget_client_locks(struct sockaddr_storage
*addr
, size_t addr_size
)
6645 unsigned int count
= 0;
6646 struct nfs4_client
*clp
;
6647 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6649 LIST_HEAD(reaplist
);
6651 if (!nfsd_netns_ready(nn
))
6654 spin_lock(&nn
->client_lock
);
6655 clp
= nfsd_find_client(addr
, addr_size
);
6657 count
= nfsd_collect_client_locks(clp
, &reaplist
, 0);
6658 spin_unlock(&nn
->client_lock
);
6659 nfsd_reap_locks(&reaplist
);
6664 nfsd_inject_forget_locks(u64 max
)
6667 struct nfs4_client
*clp
;
6668 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6670 LIST_HEAD(reaplist
);
6672 if (!nfsd_netns_ready(nn
))
6675 spin_lock(&nn
->client_lock
);
6676 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6677 count
+= nfsd_collect_client_locks(clp
, &reaplist
, max
- count
);
6678 if (max
!= 0 && count
>= max
)
6681 spin_unlock(&nn
->client_lock
);
6682 nfsd_reap_locks(&reaplist
);
6687 nfsd_foreach_client_openowner(struct nfs4_client
*clp
, u64 max
,
6688 struct list_head
*collect
,
6689 void (*func
)(struct nfs4_openowner
*))
6691 struct nfs4_openowner
*oop
, *next
;
6692 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6696 lockdep_assert_held(&nn
->client_lock
);
6698 spin_lock(&clp
->cl_lock
);
6699 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
6703 atomic_inc(&clp
->cl_refcount
);
6704 list_add(&oop
->oo_perclient
, collect
);
6709 * Despite the fact that these functions deal with
6710 * 64-bit integers for "count", we must ensure that
6711 * it doesn't blow up the clp->cl_refcount. Throw a
6712 * warning if we start to approach INT_MAX here.
6714 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6718 spin_unlock(&clp
->cl_lock
);
6724 nfsd_print_client_openowners(struct nfs4_client
*clp
)
6726 u64 count
= nfsd_foreach_client_openowner(clp
, 0, NULL
, NULL
);
6728 nfsd_print_count(clp
, count
, "openowners");
6733 nfsd_collect_client_openowners(struct nfs4_client
*clp
,
6734 struct list_head
*collect
, u64 max
)
6736 return nfsd_foreach_client_openowner(clp
, max
, collect
,
6737 unhash_openowner_locked
);
6741 nfsd_inject_print_openowners(void)
6743 struct nfs4_client
*clp
;
6745 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6748 if (!nfsd_netns_ready(nn
))
6751 spin_lock(&nn
->client_lock
);
6752 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6753 count
+= nfsd_print_client_openowners(clp
);
6754 spin_unlock(&nn
->client_lock
);
6760 nfsd_reap_openowners(struct list_head
*reaplist
)
6762 struct nfs4_client
*clp
;
6763 struct nfs4_openowner
*oop
, *next
;
6765 list_for_each_entry_safe(oop
, next
, reaplist
, oo_perclient
) {
6766 list_del_init(&oop
->oo_perclient
);
6767 clp
= oop
->oo_owner
.so_client
;
6768 release_openowner(oop
);
6774 nfsd_inject_forget_client_openowners(struct sockaddr_storage
*addr
,
6777 unsigned int count
= 0;
6778 struct nfs4_client
*clp
;
6779 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6781 LIST_HEAD(reaplist
);
6783 if (!nfsd_netns_ready(nn
))
6786 spin_lock(&nn
->client_lock
);
6787 clp
= nfsd_find_client(addr
, addr_size
);
6789 count
= nfsd_collect_client_openowners(clp
, &reaplist
, 0);
6790 spin_unlock(&nn
->client_lock
);
6791 nfsd_reap_openowners(&reaplist
);
6796 nfsd_inject_forget_openowners(u64 max
)
6799 struct nfs4_client
*clp
;
6800 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6802 LIST_HEAD(reaplist
);
6804 if (!nfsd_netns_ready(nn
))
6807 spin_lock(&nn
->client_lock
);
6808 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6809 count
+= nfsd_collect_client_openowners(clp
, &reaplist
,
6811 if (max
!= 0 && count
>= max
)
6814 spin_unlock(&nn
->client_lock
);
6815 nfsd_reap_openowners(&reaplist
);
6819 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
6820 struct list_head
*victims
)
6822 struct nfs4_delegation
*dp
, *next
;
6823 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6827 lockdep_assert_held(&nn
->client_lock
);
6829 spin_lock(&state_lock
);
6830 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
6833 * It's not safe to mess with delegations that have a
6834 * non-zero dl_time. They might have already been broken
6835 * and could be processed by the laundromat outside of
6836 * the state_lock. Just leave them be.
6838 if (dp
->dl_time
!= 0)
6841 atomic_inc(&clp
->cl_refcount
);
6842 WARN_ON(!unhash_delegation_locked(dp
));
6843 list_add(&dp
->dl_recall_lru
, victims
);
6847 * Despite the fact that these functions deal with
6848 * 64-bit integers for "count", we must ensure that
6849 * it doesn't blow up the clp->cl_refcount. Throw a
6850 * warning if we start to approach INT_MAX here.
6852 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6856 spin_unlock(&state_lock
);
6861 nfsd_print_client_delegations(struct nfs4_client
*clp
)
6863 u64 count
= nfsd_find_all_delegations(clp
, 0, NULL
);
6865 nfsd_print_count(clp
, count
, "delegations");
6870 nfsd_inject_print_delegations(void)
6872 struct nfs4_client
*clp
;
6874 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6877 if (!nfsd_netns_ready(nn
))
6880 spin_lock(&nn
->client_lock
);
6881 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6882 count
+= nfsd_print_client_delegations(clp
);
6883 spin_unlock(&nn
->client_lock
);
6889 nfsd_forget_delegations(struct list_head
*reaplist
)
6891 struct nfs4_client
*clp
;
6892 struct nfs4_delegation
*dp
, *next
;
6894 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6895 list_del_init(&dp
->dl_recall_lru
);
6896 clp
= dp
->dl_stid
.sc_client
;
6897 revoke_delegation(dp
);
6903 nfsd_inject_forget_client_delegations(struct sockaddr_storage
*addr
,
6907 struct nfs4_client
*clp
;
6908 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6910 LIST_HEAD(reaplist
);
6912 if (!nfsd_netns_ready(nn
))
6915 spin_lock(&nn
->client_lock
);
6916 clp
= nfsd_find_client(addr
, addr_size
);
6918 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6919 spin_unlock(&nn
->client_lock
);
6921 nfsd_forget_delegations(&reaplist
);
6926 nfsd_inject_forget_delegations(u64 max
)
6929 struct nfs4_client
*clp
;
6930 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6932 LIST_HEAD(reaplist
);
6934 if (!nfsd_netns_ready(nn
))
6937 spin_lock(&nn
->client_lock
);
6938 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6939 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6940 if (max
!= 0 && count
>= max
)
6943 spin_unlock(&nn
->client_lock
);
6944 nfsd_forget_delegations(&reaplist
);
6949 nfsd_recall_delegations(struct list_head
*reaplist
)
6951 struct nfs4_client
*clp
;
6952 struct nfs4_delegation
*dp
, *next
;
6954 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6955 list_del_init(&dp
->dl_recall_lru
);
6956 clp
= dp
->dl_stid
.sc_client
;
6958 * We skipped all entries that had a zero dl_time before,
6959 * so we can now reset the dl_time back to 0. If a delegation
6960 * break comes in now, then it won't make any difference since
6961 * we're recalling it either way.
6963 spin_lock(&state_lock
);
6965 spin_unlock(&state_lock
);
6966 nfsd_break_one_deleg(dp
);
6972 nfsd_inject_recall_client_delegations(struct sockaddr_storage
*addr
,
6976 struct nfs4_client
*clp
;
6977 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6979 LIST_HEAD(reaplist
);
6981 if (!nfsd_netns_ready(nn
))
6984 spin_lock(&nn
->client_lock
);
6985 clp
= nfsd_find_client(addr
, addr_size
);
6987 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6988 spin_unlock(&nn
->client_lock
);
6990 nfsd_recall_delegations(&reaplist
);
6995 nfsd_inject_recall_delegations(u64 max
)
6998 struct nfs4_client
*clp
, *next
;
6999 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7001 LIST_HEAD(reaplist
);
7003 if (!nfsd_netns_ready(nn
))
7006 spin_lock(&nn
->client_lock
);
7007 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
7008 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
7009 if (max
!= 0 && ++count
>= max
)
7012 spin_unlock(&nn
->client_lock
);
7013 nfsd_recall_delegations(&reaplist
);
7016 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7019 * Since the lifetime of a delegation isn't limited to that of an open, a
7020 * client may quite reasonably hang on to a delegation as long as it has
7021 * the inode cached. This becomes an obvious problem the first time a
7022 * client's inode cache approaches the size of the server's total memory.
7024 * For now we avoid this problem by imposing a hard limit on the number
7025 * of delegations, which varies according to the server's memory size.
7028 set_max_delegations(void)
7031 * Allow at most 4 delegations per megabyte of RAM. Quick
7032 * estimates suggest that in the worst case (where every delegation
7033 * is for a different inode), a delegation could take about 1.5K,
7034 * giving a worst case usage of about 6% of memory.
7036 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
7039 static int nfs4_state_create_net(struct net
*net
)
7041 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7044 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
7045 CLIENT_HASH_SIZE
, GFP_KERNEL
);
7046 if (!nn
->conf_id_hashtbl
)
7048 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
7049 CLIENT_HASH_SIZE
, GFP_KERNEL
);
7050 if (!nn
->unconf_id_hashtbl
)
7052 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
7053 SESSION_HASH_SIZE
, GFP_KERNEL
);
7054 if (!nn
->sessionid_hashtbl
)
7057 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7058 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
7059 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
7061 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
7062 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
7063 nn
->conf_name_tree
= RB_ROOT
;
7064 nn
->unconf_name_tree
= RB_ROOT
;
7065 nn
->boot_time
= get_seconds();
7066 nn
->grace_ended
= false;
7067 nn
->nfsd4_manager
.block_opens
= true;
7068 INIT_LIST_HEAD(&nn
->nfsd4_manager
.list
);
7069 INIT_LIST_HEAD(&nn
->client_lru
);
7070 INIT_LIST_HEAD(&nn
->close_lru
);
7071 INIT_LIST_HEAD(&nn
->del_recall_lru
);
7072 spin_lock_init(&nn
->client_lock
);
7074 spin_lock_init(&nn
->blocked_locks_lock
);
7075 INIT_LIST_HEAD(&nn
->blocked_locks_lru
);
7077 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
7083 kfree(nn
->unconf_id_hashtbl
);
7085 kfree(nn
->conf_id_hashtbl
);
7091 nfs4_state_destroy_net(struct net
*net
)
7094 struct nfs4_client
*clp
= NULL
;
7095 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7097 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7098 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
7099 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
7100 destroy_client(clp
);
7104 WARN_ON(!list_empty(&nn
->blocked_locks_lru
));
7106 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7107 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
7108 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
7109 destroy_client(clp
);
7113 kfree(nn
->sessionid_hashtbl
);
7114 kfree(nn
->unconf_id_hashtbl
);
7115 kfree(nn
->conf_id_hashtbl
);
7120 nfs4_state_start_net(struct net
*net
)
7122 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7125 ret
= nfs4_state_create_net(net
);
7128 locks_start_grace(net
, &nn
->nfsd4_manager
);
7129 nfsd4_client_tracking_init(net
);
7130 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
7131 nn
->nfsd4_grace
, net
);
7132 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
7136 /* initialization to perform when the nfsd service is started: */
7139 nfs4_state_start(void)
7143 ret
= set_callback_cred();
7147 laundry_wq
= alloc_workqueue("%s", WQ_UNBOUND
, 0, "nfsd4");
7148 if (laundry_wq
== NULL
) {
7150 goto out_cleanup_cred
;
7152 ret
= nfsd4_create_callback_queue();
7154 goto out_free_laundry
;
7156 set_max_delegations();
7160 destroy_workqueue(laundry_wq
);
7162 cleanup_callback_cred();
7167 nfs4_state_shutdown_net(struct net
*net
)
7169 struct nfs4_delegation
*dp
= NULL
;
7170 struct list_head
*pos
, *next
, reaplist
;
7171 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7173 cancel_delayed_work_sync(&nn
->laundromat_work
);
7174 locks_end_grace(&nn
->nfsd4_manager
);
7176 INIT_LIST_HEAD(&reaplist
);
7177 spin_lock(&state_lock
);
7178 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
7179 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
7180 WARN_ON(!unhash_delegation_locked(dp
));
7181 list_add(&dp
->dl_recall_lru
, &reaplist
);
7183 spin_unlock(&state_lock
);
7184 list_for_each_safe(pos
, next
, &reaplist
) {
7185 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
7186 list_del_init(&dp
->dl_recall_lru
);
7187 put_clnt_odstate(dp
->dl_clnt_odstate
);
7188 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
7189 nfs4_put_stid(&dp
->dl_stid
);
7192 nfsd4_client_tracking_exit(net
);
7193 nfs4_state_destroy_net(net
);
7197 nfs4_state_shutdown(void)
7199 destroy_workqueue(laundry_wq
);
7200 nfsd4_destroy_callback_queue();
7201 cleanup_callback_cred();
7205 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
7207 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
7208 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
7212 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
7214 if (cstate
->minorversion
) {
7215 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
7216 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
7221 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
7223 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
7227 * functions to set current state id
7230 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
,
7231 union nfsd4_op_u
*u
)
7233 put_stateid(cstate
, &u
->open_downgrade
.od_stateid
);
7237 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
,
7238 union nfsd4_op_u
*u
)
7240 put_stateid(cstate
, &u
->open
.op_stateid
);
7244 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
,
7245 union nfsd4_op_u
*u
)
7247 put_stateid(cstate
, &u
->close
.cl_stateid
);
7251 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
,
7252 union nfsd4_op_u
*u
)
7254 put_stateid(cstate
, &u
->lock
.lk_resp_stateid
);
7258 * functions to consume current state id
7262 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
,
7263 union nfsd4_op_u
*u
)
7265 get_stateid(cstate
, &u
->open_downgrade
.od_stateid
);
7269 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
,
7270 union nfsd4_op_u
*u
)
7272 get_stateid(cstate
, &u
->delegreturn
.dr_stateid
);
7276 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
,
7277 union nfsd4_op_u
*u
)
7279 get_stateid(cstate
, &u
->free_stateid
.fr_stateid
);
7283 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
,
7284 union nfsd4_op_u
*u
)
7286 get_stateid(cstate
, &u
->setattr
.sa_stateid
);
7290 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
,
7291 union nfsd4_op_u
*u
)
7293 get_stateid(cstate
, &u
->close
.cl_stateid
);
7297 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
,
7298 union nfsd4_op_u
*u
)
7300 get_stateid(cstate
, &u
->locku
.lu_stateid
);
7304 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
,
7305 union nfsd4_op_u
*u
)
7307 get_stateid(cstate
, &u
->read
.rd_stateid
);
7311 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
,
7312 union nfsd4_op_u
*u
)
7314 get_stateid(cstate
, &u
->write
.wr_stateid
);