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 free_svc_cred(&ses
->se_slots
[i
]->sl_cred
);
1477 kfree(ses
->se_slots
[i
]);
1482 * We don't actually need to cache the rpc and session headers, so we
1483 * can allocate a little less for each slot:
1485 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1489 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1492 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1493 return size
+ sizeof(struct nfsd4_slot
);
1497 * XXX: If we run out of reserved DRC memory we could (up to a point)
1498 * re-negotiate active sessions and reduce their slot usage to make
1499 * room for new connections. For now we just fail the create session.
1501 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
)
1503 u32 slotsize
= slot_bytes(ca
);
1504 u32 num
= ca
->maxreqs
;
1507 spin_lock(&nfsd_drc_lock
);
1508 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
,
1509 nfsd_drc_max_mem
- nfsd_drc_mem_used
);
1510 num
= min_t(int, num
, avail
/ slotsize
);
1511 nfsd_drc_mem_used
+= num
* slotsize
;
1512 spin_unlock(&nfsd_drc_lock
);
1517 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1519 int slotsize
= slot_bytes(ca
);
1521 spin_lock(&nfsd_drc_lock
);
1522 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1523 spin_unlock(&nfsd_drc_lock
);
1526 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1527 struct nfsd4_channel_attrs
*battrs
)
1529 int numslots
= fattrs
->maxreqs
;
1530 int slotsize
= slot_bytes(fattrs
);
1531 struct nfsd4_session
*new;
1534 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1535 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1536 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1538 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1541 /* allocate each struct nfsd4_slot and data cache in one piece */
1542 for (i
= 0; i
< numslots
; i
++) {
1543 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1544 if (!new->se_slots
[i
])
1548 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1549 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1554 kfree(new->se_slots
[i
]);
1559 static void free_conn(struct nfsd4_conn
*c
)
1561 svc_xprt_put(c
->cn_xprt
);
1565 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1567 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1568 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1570 spin_lock(&clp
->cl_lock
);
1571 if (!list_empty(&c
->cn_persession
)) {
1572 list_del(&c
->cn_persession
);
1575 nfsd4_probe_callback(clp
);
1576 spin_unlock(&clp
->cl_lock
);
1579 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1581 struct nfsd4_conn
*conn
;
1583 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1586 svc_xprt_get(rqstp
->rq_xprt
);
1587 conn
->cn_xprt
= rqstp
->rq_xprt
;
1588 conn
->cn_flags
= flags
;
1589 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1593 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1595 conn
->cn_session
= ses
;
1596 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1599 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1601 struct nfs4_client
*clp
= ses
->se_client
;
1603 spin_lock(&clp
->cl_lock
);
1604 __nfsd4_hash_conn(conn
, ses
);
1605 spin_unlock(&clp
->cl_lock
);
1608 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1610 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1611 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1614 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1618 nfsd4_hash_conn(conn
, ses
);
1619 ret
= nfsd4_register_conn(conn
);
1621 /* oops; xprt is already down: */
1622 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1623 /* We may have gained or lost a callback channel: */
1624 nfsd4_probe_callback_sync(ses
->se_client
);
1627 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1629 u32 dir
= NFS4_CDFC4_FORE
;
1631 if (cses
->flags
& SESSION4_BACK_CHAN
)
1632 dir
|= NFS4_CDFC4_BACK
;
1633 return alloc_conn(rqstp
, dir
);
1636 /* must be called under client_lock */
1637 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1639 struct nfs4_client
*clp
= s
->se_client
;
1640 struct nfsd4_conn
*c
;
1642 spin_lock(&clp
->cl_lock
);
1643 while (!list_empty(&s
->se_conns
)) {
1644 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1645 list_del_init(&c
->cn_persession
);
1646 spin_unlock(&clp
->cl_lock
);
1648 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1651 spin_lock(&clp
->cl_lock
);
1653 spin_unlock(&clp
->cl_lock
);
1656 static void __free_session(struct nfsd4_session
*ses
)
1658 free_session_slots(ses
);
1662 static void free_session(struct nfsd4_session
*ses
)
1664 nfsd4_del_conns(ses
);
1665 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1666 __free_session(ses
);
1669 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1672 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1674 new->se_client
= clp
;
1677 INIT_LIST_HEAD(&new->se_conns
);
1679 new->se_cb_seq_nr
= 1;
1680 new->se_flags
= cses
->flags
;
1681 new->se_cb_prog
= cses
->callback_prog
;
1682 new->se_cb_sec
= cses
->cb_sec
;
1683 atomic_set(&new->se_ref
, 0);
1684 idx
= hash_sessionid(&new->se_sessionid
);
1685 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1686 spin_lock(&clp
->cl_lock
);
1687 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1688 spin_unlock(&clp
->cl_lock
);
1691 struct sockaddr
*sa
= svc_addr(rqstp
);
1693 * This is a little silly; with sessions there's no real
1694 * use for the callback address. Use the peer address
1695 * as a reasonable default for now, but consider fixing
1696 * the rpc client not to require an address in the
1699 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1700 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1704 /* caller must hold client_lock */
1705 static struct nfsd4_session
*
1706 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1708 struct nfsd4_session
*elem
;
1710 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1712 lockdep_assert_held(&nn
->client_lock
);
1714 dump_sessionid(__func__
, sessionid
);
1715 idx
= hash_sessionid(sessionid
);
1716 /* Search in the appropriate list */
1717 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1718 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1719 NFS4_MAX_SESSIONID_LEN
)) {
1724 dprintk("%s: session not found\n", __func__
);
1728 static struct nfsd4_session
*
1729 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1732 struct nfsd4_session
*session
;
1733 __be32 status
= nfserr_badsession
;
1735 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1738 status
= nfsd4_get_session_locked(session
);
1746 /* caller must hold client_lock */
1748 unhash_session(struct nfsd4_session
*ses
)
1750 struct nfs4_client
*clp
= ses
->se_client
;
1751 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1753 lockdep_assert_held(&nn
->client_lock
);
1755 list_del(&ses
->se_hash
);
1756 spin_lock(&ses
->se_client
->cl_lock
);
1757 list_del(&ses
->se_perclnt
);
1758 spin_unlock(&ses
->se_client
->cl_lock
);
1761 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1763 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1766 * We're assuming the clid was not given out from a boot
1767 * precisely 2^32 (about 136 years) before this one. That seems
1768 * a safe assumption:
1770 if (clid
->cl_boot
== (u32
)nn
->boot_time
)
1772 dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n",
1773 clid
->cl_boot
, clid
->cl_id
, nn
->boot_time
);
1778 * XXX Should we use a slab cache ?
1779 * This type of memory management is somewhat inefficient, but we use it
1780 * anyway since SETCLIENTID is not a common operation.
1782 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1784 struct nfs4_client
*clp
;
1787 clp
= kzalloc(sizeof(struct nfs4_client
), GFP_KERNEL
);
1790 clp
->cl_name
.data
= kmemdup(name
.data
, name
.len
, GFP_KERNEL
);
1791 if (clp
->cl_name
.data
== NULL
)
1793 clp
->cl_ownerstr_hashtbl
= kmalloc(sizeof(struct list_head
) *
1794 OWNER_HASH_SIZE
, GFP_KERNEL
);
1795 if (!clp
->cl_ownerstr_hashtbl
)
1796 goto err_no_hashtbl
;
1797 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
1798 INIT_LIST_HEAD(&clp
->cl_ownerstr_hashtbl
[i
]);
1799 clp
->cl_name
.len
= name
.len
;
1800 INIT_LIST_HEAD(&clp
->cl_sessions
);
1801 idr_init(&clp
->cl_stateids
);
1802 atomic_set(&clp
->cl_refcount
, 0);
1803 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1804 INIT_LIST_HEAD(&clp
->cl_idhash
);
1805 INIT_LIST_HEAD(&clp
->cl_openowners
);
1806 INIT_LIST_HEAD(&clp
->cl_delegations
);
1807 INIT_LIST_HEAD(&clp
->cl_lru
);
1808 INIT_LIST_HEAD(&clp
->cl_revoked
);
1809 #ifdef CONFIG_NFSD_PNFS
1810 INIT_LIST_HEAD(&clp
->cl_lo_states
);
1812 spin_lock_init(&clp
->cl_lock
);
1813 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1816 kfree(clp
->cl_name
.data
);
1823 free_client(struct nfs4_client
*clp
)
1825 while (!list_empty(&clp
->cl_sessions
)) {
1826 struct nfsd4_session
*ses
;
1827 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
1829 list_del(&ses
->se_perclnt
);
1830 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
1833 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
1834 free_svc_cred(&clp
->cl_cred
);
1835 kfree(clp
->cl_ownerstr_hashtbl
);
1836 kfree(clp
->cl_name
.data
);
1837 idr_destroy(&clp
->cl_stateids
);
1841 /* must be called under the client_lock */
1843 unhash_client_locked(struct nfs4_client
*clp
)
1845 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1846 struct nfsd4_session
*ses
;
1848 lockdep_assert_held(&nn
->client_lock
);
1850 /* Mark the client as expired! */
1852 /* Make it invisible */
1853 if (!list_empty(&clp
->cl_idhash
)) {
1854 list_del_init(&clp
->cl_idhash
);
1855 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
1856 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
1858 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
1860 list_del_init(&clp
->cl_lru
);
1861 spin_lock(&clp
->cl_lock
);
1862 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
1863 list_del_init(&ses
->se_hash
);
1864 spin_unlock(&clp
->cl_lock
);
1868 unhash_client(struct nfs4_client
*clp
)
1870 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1872 spin_lock(&nn
->client_lock
);
1873 unhash_client_locked(clp
);
1874 spin_unlock(&nn
->client_lock
);
1877 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
1879 if (atomic_read(&clp
->cl_refcount
))
1880 return nfserr_jukebox
;
1881 unhash_client_locked(clp
);
1886 __destroy_client(struct nfs4_client
*clp
)
1889 struct nfs4_openowner
*oo
;
1890 struct nfs4_delegation
*dp
;
1891 struct list_head reaplist
;
1893 INIT_LIST_HEAD(&reaplist
);
1894 spin_lock(&state_lock
);
1895 while (!list_empty(&clp
->cl_delegations
)) {
1896 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
1897 WARN_ON(!unhash_delegation_locked(dp
));
1898 list_add(&dp
->dl_recall_lru
, &reaplist
);
1900 spin_unlock(&state_lock
);
1901 while (!list_empty(&reaplist
)) {
1902 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
1903 list_del_init(&dp
->dl_recall_lru
);
1904 put_clnt_odstate(dp
->dl_clnt_odstate
);
1905 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
1906 nfs4_put_stid(&dp
->dl_stid
);
1908 while (!list_empty(&clp
->cl_revoked
)) {
1909 dp
= list_entry(clp
->cl_revoked
.next
, struct nfs4_delegation
, dl_recall_lru
);
1910 list_del_init(&dp
->dl_recall_lru
);
1911 nfs4_put_stid(&dp
->dl_stid
);
1913 while (!list_empty(&clp
->cl_openowners
)) {
1914 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
1915 nfs4_get_stateowner(&oo
->oo_owner
);
1916 release_openowner(oo
);
1918 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++) {
1919 struct nfs4_stateowner
*so
, *tmp
;
1921 list_for_each_entry_safe(so
, tmp
, &clp
->cl_ownerstr_hashtbl
[i
],
1923 /* Should be no openowners at this point */
1924 WARN_ON_ONCE(so
->so_is_open_owner
);
1925 remove_blocked_locks(lockowner(so
));
1928 nfsd4_return_all_client_layouts(clp
);
1929 nfsd4_shutdown_callback(clp
);
1930 if (clp
->cl_cb_conn
.cb_xprt
)
1931 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
1936 destroy_client(struct nfs4_client
*clp
)
1939 __destroy_client(clp
);
1942 static void expire_client(struct nfs4_client
*clp
)
1945 nfsd4_client_record_remove(clp
);
1946 __destroy_client(clp
);
1949 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
1951 memcpy(target
->cl_verifier
.data
, source
->data
,
1952 sizeof(target
->cl_verifier
.data
));
1955 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
1957 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
1958 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
1961 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
1963 target
->cr_principal
= kstrdup(source
->cr_principal
, GFP_KERNEL
);
1964 target
->cr_raw_principal
= kstrdup(source
->cr_raw_principal
,
1966 if ((source
->cr_principal
&& ! target
->cr_principal
) ||
1967 (source
->cr_raw_principal
&& ! target
->cr_raw_principal
))
1970 target
->cr_flavor
= source
->cr_flavor
;
1971 target
->cr_uid
= source
->cr_uid
;
1972 target
->cr_gid
= source
->cr_gid
;
1973 target
->cr_group_info
= source
->cr_group_info
;
1974 get_group_info(target
->cr_group_info
);
1975 target
->cr_gss_mech
= source
->cr_gss_mech
;
1976 if (source
->cr_gss_mech
)
1977 gss_mech_get(source
->cr_gss_mech
);
1982 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
1984 if (o1
->len
< o2
->len
)
1986 if (o1
->len
> o2
->len
)
1988 return memcmp(o1
->data
, o2
->data
, o1
->len
);
1991 static int same_name(const char *n1
, const char *n2
)
1993 return 0 == memcmp(n1
, n2
, HEXDIR_LEN
);
1997 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
1999 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
2003 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
2005 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
2008 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
2012 if (g1
->ngroups
!= g2
->ngroups
)
2014 for (i
=0; i
<g1
->ngroups
; i
++)
2015 if (!gid_eq(g1
->gid
[i
], g2
->gid
[i
]))
2021 * RFC 3530 language requires clid_inuse be returned when the
2022 * "principal" associated with a requests differs from that previously
2023 * used. We use uid, gid's, and gss principal string as our best
2024 * approximation. We also don't want to allow non-gss use of a client
2025 * established using gss: in theory cr_principal should catch that
2026 * change, but in practice cr_principal can be null even in the gss case
2027 * since gssd doesn't always pass down a principal string.
2029 static bool is_gss_cred(struct svc_cred
*cr
)
2031 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2032 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
2037 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
2039 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
2040 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
2041 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
2042 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
2044 if (cr1
->cr_principal
== cr2
->cr_principal
)
2046 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
2048 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
2051 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
2053 struct svc_cred
*cr
= &rqstp
->rq_cred
;
2056 if (!cr
->cr_gss_mech
)
2058 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
2059 return service
== RPC_GSS_SVC_INTEGRITY
||
2060 service
== RPC_GSS_SVC_PRIVACY
;
2063 bool nfsd4_mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
2065 struct svc_cred
*cr
= &rqstp
->rq_cred
;
2067 if (!cl
->cl_mach_cred
)
2069 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
2071 if (!svc_rqst_integrity_protected(rqstp
))
2073 if (cl
->cl_cred
.cr_raw_principal
)
2074 return 0 == strcmp(cl
->cl_cred
.cr_raw_principal
,
2075 cr
->cr_raw_principal
);
2076 if (!cr
->cr_principal
)
2078 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
2081 static void gen_confirm(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
2086 * This is opaque to client, so no need to byte-swap. Use
2087 * __force to keep sparse happy
2089 verf
[0] = (__force __be32
)get_seconds();
2090 verf
[1] = (__force __be32
)nn
->clverifier_counter
++;
2091 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
2094 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
2096 clp
->cl_clientid
.cl_boot
= nn
->boot_time
;
2097 clp
->cl_clientid
.cl_id
= nn
->clientid_counter
++;
2098 gen_confirm(clp
, nn
);
2101 static struct nfs4_stid
*
2102 find_stateid_locked(struct nfs4_client
*cl
, stateid_t
*t
)
2104 struct nfs4_stid
*ret
;
2106 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
2107 if (!ret
|| !ret
->sc_type
)
2112 static struct nfs4_stid
*
2113 find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
2115 struct nfs4_stid
*s
;
2117 spin_lock(&cl
->cl_lock
);
2118 s
= find_stateid_locked(cl
, t
);
2120 if (typemask
& s
->sc_type
)
2121 atomic_inc(&s
->sc_count
);
2125 spin_unlock(&cl
->cl_lock
);
2129 static struct nfs4_client
*create_client(struct xdr_netobj name
,
2130 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
2132 struct nfs4_client
*clp
;
2133 struct sockaddr
*sa
= svc_addr(rqstp
);
2135 struct net
*net
= SVC_NET(rqstp
);
2137 clp
= alloc_client(name
);
2141 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
2146 nfsd4_init_cb(&clp
->cl_cb_null
, clp
, NULL
, NFSPROC4_CLNT_CB_NULL
);
2147 clp
->cl_time
= get_seconds();
2148 clear_bit(0, &clp
->cl_cb_slot_busy
);
2149 copy_verf(clp
, verf
);
2150 rpc_copy_addr((struct sockaddr
*) &clp
->cl_addr
, sa
);
2151 clp
->cl_cb_session
= NULL
;
2157 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
2159 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
2160 struct nfs4_client
*clp
;
2163 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
2166 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
2167 new = &((*new)->rb_left
);
2169 new = &((*new)->rb_right
);
2172 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
2173 rb_insert_color(&new_clp
->cl_namenode
, root
);
2176 static struct nfs4_client
*
2177 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
2180 struct rb_node
*node
= root
->rb_node
;
2181 struct nfs4_client
*clp
;
2184 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
2185 cmp
= compare_blob(&clp
->cl_name
, name
);
2187 node
= node
->rb_left
;
2189 node
= node
->rb_right
;
2197 add_to_unconfirmed(struct nfs4_client
*clp
)
2199 unsigned int idhashval
;
2200 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2202 lockdep_assert_held(&nn
->client_lock
);
2204 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2205 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
2206 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2207 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
2208 renew_client_locked(clp
);
2212 move_to_confirmed(struct nfs4_client
*clp
)
2214 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2215 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2217 lockdep_assert_held(&nn
->client_lock
);
2219 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
2220 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
2221 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
2222 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
2223 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2224 renew_client_locked(clp
);
2227 static struct nfs4_client
*
2228 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
2230 struct nfs4_client
*clp
;
2231 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
2233 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
2234 if (same_clid(&clp
->cl_clientid
, clid
)) {
2235 if ((bool)clp
->cl_minorversion
!= sessions
)
2237 renew_client_locked(clp
);
2244 static struct nfs4_client
*
2245 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2247 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
2249 lockdep_assert_held(&nn
->client_lock
);
2250 return find_client_in_id_table(tbl
, clid
, sessions
);
2253 static struct nfs4_client
*
2254 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2256 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
2258 lockdep_assert_held(&nn
->client_lock
);
2259 return find_client_in_id_table(tbl
, clid
, sessions
);
2262 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
2264 return clp
->cl_exchange_flags
!= 0;
2267 static struct nfs4_client
*
2268 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2270 lockdep_assert_held(&nn
->client_lock
);
2271 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
2274 static struct nfs4_client
*
2275 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2277 lockdep_assert_held(&nn
->client_lock
);
2278 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
2282 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
2284 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
2285 struct sockaddr
*sa
= svc_addr(rqstp
);
2286 u32 scopeid
= rpc_get_scope_id(sa
);
2287 unsigned short expected_family
;
2289 /* Currently, we only support tcp and tcp6 for the callback channel */
2290 if (se
->se_callback_netid_len
== 3 &&
2291 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
2292 expected_family
= AF_INET
;
2293 else if (se
->se_callback_netid_len
== 4 &&
2294 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
2295 expected_family
= AF_INET6
;
2299 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
2300 se
->se_callback_addr_len
,
2301 (struct sockaddr
*)&conn
->cb_addr
,
2302 sizeof(conn
->cb_addr
));
2304 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
2307 if (conn
->cb_addr
.ss_family
== AF_INET6
)
2308 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
2310 conn
->cb_prog
= se
->se_callback_prog
;
2311 conn
->cb_ident
= se
->se_callback_ident
;
2312 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
2315 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
2316 conn
->cb_addrlen
= 0;
2317 dprintk("NFSD: this client (clientid %08x/%08x) "
2318 "will not receive delegations\n",
2319 clp
->cl_clientid
.cl_boot
, clp
->cl_clientid
.cl_id
);
2325 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2328 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
2330 struct xdr_buf
*buf
= resp
->xdr
.buf
;
2331 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2334 dprintk("--> %s slot %p\n", __func__
, slot
);
2336 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
2337 slot
->sl_opcnt
= resp
->opcnt
;
2338 slot
->sl_status
= resp
->cstate
.status
;
2339 free_svc_cred(&slot
->sl_cred
);
2340 copy_cred(&slot
->sl_cred
, &resp
->rqstp
->rq_cred
);
2342 if (!nfsd4_cache_this(resp
)) {
2343 slot
->sl_flags
&= ~NFSD4_SLOT_CACHED
;
2346 slot
->sl_flags
|= NFSD4_SLOT_CACHED
;
2348 base
= resp
->cstate
.data_offset
;
2349 slot
->sl_datalen
= buf
->len
- base
;
2350 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
2351 WARN(1, "%s: sessions DRC could not cache compound\n",
2357 * Encode the replay sequence operation from the slot values.
2358 * If cachethis is FALSE encode the uncached rep error on the next
2359 * operation which sets resp->p and increments resp->opcnt for
2360 * nfs4svc_encode_compoundres.
2364 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
2365 struct nfsd4_compoundres
*resp
)
2367 struct nfsd4_op
*op
;
2368 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2370 /* Encode the replayed sequence operation */
2371 op
= &args
->ops
[resp
->opcnt
- 1];
2372 nfsd4_encode_operation(resp
, op
);
2374 if (slot
->sl_flags
& NFSD4_SLOT_CACHED
)
2376 if (args
->opcnt
== 1) {
2378 * The original operation wasn't a solo sequence--we
2379 * always cache those--so this retry must not match the
2382 op
->status
= nfserr_seq_false_retry
;
2384 op
= &args
->ops
[resp
->opcnt
++];
2385 op
->status
= nfserr_retry_uncached_rep
;
2386 nfsd4_encode_operation(resp
, op
);
2392 * The sequence operation is not cached because we can use the slot and
2396 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
2397 struct nfsd4_sequence
*seq
)
2399 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2400 struct xdr_stream
*xdr
= &resp
->xdr
;
2404 dprintk("--> %s slot %p\n", __func__
, slot
);
2406 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
2410 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
2413 return nfserr_serverfault
;
2415 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
2416 xdr_commit_encode(xdr
);
2418 resp
->opcnt
= slot
->sl_opcnt
;
2419 return slot
->sl_status
;
2423 * Set the exchange_id flags returned by the server.
2426 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
2428 #ifdef CONFIG_NFSD_PNFS
2429 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_PNFS_MDS
;
2431 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
2434 /* Referrals are supported, Migration is not. */
2435 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
2437 /* set the wire flags to return to client. */
2438 clid
->flags
= new->cl_exchange_flags
;
2441 static bool client_has_openowners(struct nfs4_client
*clp
)
2443 struct nfs4_openowner
*oo
;
2445 list_for_each_entry(oo
, &clp
->cl_openowners
, oo_perclient
) {
2446 if (!list_empty(&oo
->oo_owner
.so_stateids
))
2452 static bool client_has_state(struct nfs4_client
*clp
)
2454 return client_has_openowners(clp
)
2455 #ifdef CONFIG_NFSD_PNFS
2456 || !list_empty(&clp
->cl_lo_states
)
2458 || !list_empty(&clp
->cl_delegations
)
2459 || !list_empty(&clp
->cl_sessions
);
2463 nfsd4_exchange_id(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
2464 union nfsd4_op_u
*u
)
2466 struct nfsd4_exchange_id
*exid
= &u
->exchange_id
;
2467 struct nfs4_client
*conf
, *new;
2468 struct nfs4_client
*unconf
= NULL
;
2470 char addr_str
[INET6_ADDRSTRLEN
];
2471 nfs4_verifier verf
= exid
->verifier
;
2472 struct sockaddr
*sa
= svc_addr(rqstp
);
2473 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
2474 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2476 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
2477 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
2478 "ip_addr=%s flags %x, spa_how %d\n",
2479 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
2480 addr_str
, exid
->flags
, exid
->spa_how
);
2482 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
2483 return nfserr_inval
;
2485 new = create_client(exid
->clname
, rqstp
, &verf
);
2487 return nfserr_jukebox
;
2489 switch (exid
->spa_how
) {
2491 exid
->spo_must_enforce
[0] = 0;
2492 exid
->spo_must_enforce
[1] = (
2493 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
2494 1 << (OP_EXCHANGE_ID
- 32) |
2495 1 << (OP_CREATE_SESSION
- 32) |
2496 1 << (OP_DESTROY_SESSION
- 32) |
2497 1 << (OP_DESTROY_CLIENTID
- 32));
2499 exid
->spo_must_allow
[0] &= (1 << (OP_CLOSE
) |
2500 1 << (OP_OPEN_DOWNGRADE
) |
2502 1 << (OP_DELEGRETURN
));
2504 exid
->spo_must_allow
[1] &= (
2505 1 << (OP_TEST_STATEID
- 32) |
2506 1 << (OP_FREE_STATEID
- 32));
2507 if (!svc_rqst_integrity_protected(rqstp
)) {
2508 status
= nfserr_inval
;
2512 * Sometimes userspace doesn't give us a principal.
2513 * Which is a bug, really. Anyway, we can't enforce
2514 * MACH_CRED in that case, better to give up now:
2516 if (!new->cl_cred
.cr_principal
&&
2517 !new->cl_cred
.cr_raw_principal
) {
2518 status
= nfserr_serverfault
;
2521 new->cl_mach_cred
= true;
2524 default: /* checked by xdr code */
2527 status
= nfserr_encr_alg_unsupp
;
2531 /* Cases below refer to rfc 5661 section 18.35.4: */
2532 spin_lock(&nn
->client_lock
);
2533 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
2535 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
2536 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
2539 if (!clp_used_exchangeid(conf
)) { /* buggy client */
2540 status
= nfserr_inval
;
2543 if (!nfsd4_mach_creds_match(conf
, rqstp
)) {
2544 status
= nfserr_wrong_cred
;
2547 if (!creds_match
) { /* case 9 */
2548 status
= nfserr_perm
;
2551 if (!verfs_match
) { /* case 8 */
2552 status
= nfserr_not_same
;
2556 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2559 if (!creds_match
) { /* case 3 */
2560 if (client_has_state(conf
)) {
2561 status
= nfserr_clid_inuse
;
2566 if (verfs_match
) { /* case 2 */
2567 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
2570 /* case 5, client reboot */
2575 if (update
) { /* case 7 */
2576 status
= nfserr_noent
;
2580 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
2581 if (unconf
) /* case 4, possible retry or client restart */
2582 unhash_client_locked(unconf
);
2584 /* case 1 (normal case) */
2587 status
= mark_client_expired_locked(conf
);
2591 new->cl_minorversion
= cstate
->minorversion
;
2592 new->cl_spo_must_allow
.u
.words
[0] = exid
->spo_must_allow
[0];
2593 new->cl_spo_must_allow
.u
.words
[1] = exid
->spo_must_allow
[1];
2596 add_to_unconfirmed(new);
2599 exid
->clientid
.cl_boot
= conf
->cl_clientid
.cl_boot
;
2600 exid
->clientid
.cl_id
= conf
->cl_clientid
.cl_id
;
2602 exid
->seqid
= conf
->cl_cs_slot
.sl_seqid
+ 1;
2603 nfsd4_set_ex_flags(conf
, exid
);
2605 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
2606 conf
->cl_cs_slot
.sl_seqid
, conf
->cl_exchange_flags
);
2610 spin_unlock(&nn
->client_lock
);
2615 expire_client(unconf
);
2620 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
2622 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
2625 /* The slot is in use, and no response has been sent. */
2627 if (seqid
== slot_seqid
)
2628 return nfserr_jukebox
;
2630 return nfserr_seq_misordered
;
2632 /* Note unsigned 32-bit arithmetic handles wraparound: */
2633 if (likely(seqid
== slot_seqid
+ 1))
2635 if (seqid
== slot_seqid
)
2636 return nfserr_replay_cache
;
2637 return nfserr_seq_misordered
;
2641 * Cache the create session result into the create session single DRC
2642 * slot cache by saving the xdr structure. sl_seqid has been set.
2643 * Do this for solo or embedded create session operations.
2646 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
2647 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
2649 slot
->sl_status
= nfserr
;
2650 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
2654 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
2655 struct nfsd4_clid_slot
*slot
)
2657 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
2658 return slot
->sl_status
;
2661 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
2662 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
2663 1 + /* MIN tag is length with zero, only length */ \
2664 3 + /* version, opcount, opcode */ \
2665 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2666 /* seqid, slotID, slotID, cache */ \
2667 4 ) * sizeof(__be32))
2669 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
2670 2 + /* verifier: AUTH_NULL, length 0 */\
2672 1 + /* MIN tag is length with zero, only length */ \
2673 3 + /* opcount, opcode, opstatus*/ \
2674 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
2675 /* seqid, slotID, slotID, slotID, status */ \
2676 5 ) * sizeof(__be32))
2678 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
2680 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
2682 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
2683 return nfserr_toosmall
;
2684 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
2685 return nfserr_toosmall
;
2686 ca
->headerpadsz
= 0;
2687 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
2688 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
2689 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
2690 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
2691 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
2692 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
2694 * Note decreasing slot size below client's request may make it
2695 * difficult for client to function correctly, whereas
2696 * decreasing the number of slots will (just?) affect
2697 * performance. When short on memory we therefore prefer to
2698 * decrease number of slots instead of their size. Clients that
2699 * request larger slots than they need will get poor results:
2701 ca
->maxreqs
= nfsd4_get_drc_mem(ca
);
2703 return nfserr_jukebox
;
2709 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
2710 * These are based on similar macros in linux/sunrpc/msg_prot.h .
2712 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
2713 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
2715 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
2716 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
2718 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
2719 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
2720 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
2721 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
2724 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
2726 ca
->headerpadsz
= 0;
2728 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
2729 return nfserr_toosmall
;
2730 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
2731 return nfserr_toosmall
;
2732 ca
->maxresp_cached
= 0;
2734 return nfserr_toosmall
;
2739 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
2741 switch (cbs
->flavor
) {
2747 * GSS case: the spec doesn't allow us to return this
2748 * error. But it also doesn't allow us not to support
2750 * I'd rather this fail hard than return some error the
2751 * client might think it can already handle:
2753 return nfserr_encr_alg_unsupp
;
2758 nfsd4_create_session(struct svc_rqst
*rqstp
,
2759 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
2761 struct nfsd4_create_session
*cr_ses
= &u
->create_session
;
2762 struct sockaddr
*sa
= svc_addr(rqstp
);
2763 struct nfs4_client
*conf
, *unconf
;
2764 struct nfs4_client
*old
= NULL
;
2765 struct nfsd4_session
*new;
2766 struct nfsd4_conn
*conn
;
2767 struct nfsd4_clid_slot
*cs_slot
= NULL
;
2769 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2771 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
2772 return nfserr_inval
;
2773 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
2776 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
2779 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
2781 goto out_release_drc_mem
;
2782 status
= nfserr_jukebox
;
2783 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
2785 goto out_release_drc_mem
;
2786 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
2788 goto out_free_session
;
2790 spin_lock(&nn
->client_lock
);
2791 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
2792 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
2793 WARN_ON_ONCE(conf
&& unconf
);
2796 status
= nfserr_wrong_cred
;
2797 if (!nfsd4_mach_creds_match(conf
, rqstp
))
2799 cs_slot
= &conf
->cl_cs_slot
;
2800 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2802 if (status
== nfserr_replay_cache
)
2803 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
2806 } else if (unconf
) {
2807 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
2808 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
2809 status
= nfserr_clid_inuse
;
2812 status
= nfserr_wrong_cred
;
2813 if (!nfsd4_mach_creds_match(unconf
, rqstp
))
2815 cs_slot
= &unconf
->cl_cs_slot
;
2816 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
2818 /* an unconfirmed replay returns misordered */
2819 status
= nfserr_seq_misordered
;
2822 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
2824 status
= mark_client_expired_locked(old
);
2830 move_to_confirmed(unconf
);
2833 status
= nfserr_stale_clientid
;
2837 /* Persistent sessions are not supported */
2838 cr_ses
->flags
&= ~SESSION4_PERSIST
;
2839 /* Upshifting from TCP to RDMA is not supported */
2840 cr_ses
->flags
&= ~SESSION4_RDMA
;
2842 init_session(rqstp
, new, conf
, cr_ses
);
2843 nfsd4_get_session_locked(new);
2845 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
2846 NFS4_MAX_SESSIONID_LEN
);
2847 cs_slot
->sl_seqid
++;
2848 cr_ses
->seqid
= cs_slot
->sl_seqid
;
2850 /* cache solo and embedded create sessions under the client_lock */
2851 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
2852 spin_unlock(&nn
->client_lock
);
2853 /* init connection and backchannel */
2854 nfsd4_init_conn(rqstp
, conn
, new);
2855 nfsd4_put_session(new);
2860 spin_unlock(&nn
->client_lock
);
2865 __free_session(new);
2866 out_release_drc_mem
:
2867 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
2871 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
2874 case NFS4_CDFC4_FORE
:
2875 case NFS4_CDFC4_BACK
:
2877 case NFS4_CDFC4_FORE_OR_BOTH
:
2878 case NFS4_CDFC4_BACK_OR_BOTH
:
2879 *dir
= NFS4_CDFC4_BOTH
;
2882 return nfserr_inval
;
2885 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
,
2886 struct nfsd4_compound_state
*cstate
,
2887 union nfsd4_op_u
*u
)
2889 struct nfsd4_backchannel_ctl
*bc
= &u
->backchannel_ctl
;
2890 struct nfsd4_session
*session
= cstate
->session
;
2891 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
2894 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
2897 spin_lock(&nn
->client_lock
);
2898 session
->se_cb_prog
= bc
->bc_cb_program
;
2899 session
->se_cb_sec
= bc
->bc_cb_sec
;
2900 spin_unlock(&nn
->client_lock
);
2902 nfsd4_probe_callback(session
->se_client
);
2907 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
2908 struct nfsd4_compound_state
*cstate
,
2909 union nfsd4_op_u
*u
)
2911 struct nfsd4_bind_conn_to_session
*bcts
= &u
->bind_conn_to_session
;
2913 struct nfsd4_conn
*conn
;
2914 struct nfsd4_session
*session
;
2915 struct net
*net
= SVC_NET(rqstp
);
2916 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2918 if (!nfsd4_last_compound_op(rqstp
))
2919 return nfserr_not_only_op
;
2920 spin_lock(&nn
->client_lock
);
2921 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
2922 spin_unlock(&nn
->client_lock
);
2924 goto out_no_session
;
2925 status
= nfserr_wrong_cred
;
2926 if (!nfsd4_mach_creds_match(session
->se_client
, rqstp
))
2928 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
2931 conn
= alloc_conn(rqstp
, bcts
->dir
);
2932 status
= nfserr_jukebox
;
2935 nfsd4_init_conn(rqstp
, conn
, session
);
2938 nfsd4_put_session(session
);
2943 static bool nfsd4_compound_in_session(struct nfsd4_session
*session
, struct nfs4_sessionid
*sid
)
2947 return !memcmp(sid
, &session
->se_sessionid
, sizeof(*sid
));
2951 nfsd4_destroy_session(struct svc_rqst
*r
, struct nfsd4_compound_state
*cstate
,
2952 union nfsd4_op_u
*u
)
2954 struct nfsd4_destroy_session
*sessionid
= &u
->destroy_session
;
2955 struct nfsd4_session
*ses
;
2957 int ref_held_by_me
= 0;
2958 struct net
*net
= SVC_NET(r
);
2959 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2961 status
= nfserr_not_only_op
;
2962 if (nfsd4_compound_in_session(cstate
->session
, &sessionid
->sessionid
)) {
2963 if (!nfsd4_last_compound_op(r
))
2967 dump_sessionid(__func__
, &sessionid
->sessionid
);
2968 spin_lock(&nn
->client_lock
);
2969 ses
= find_in_sessionid_hashtbl(&sessionid
->sessionid
, net
, &status
);
2971 goto out_client_lock
;
2972 status
= nfserr_wrong_cred
;
2973 if (!nfsd4_mach_creds_match(ses
->se_client
, r
))
2974 goto out_put_session
;
2975 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
2977 goto out_put_session
;
2978 unhash_session(ses
);
2979 spin_unlock(&nn
->client_lock
);
2981 nfsd4_probe_callback_sync(ses
->se_client
);
2983 spin_lock(&nn
->client_lock
);
2986 nfsd4_put_session_locked(ses
);
2988 spin_unlock(&nn
->client_lock
);
2993 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
2995 struct nfsd4_conn
*c
;
2997 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
2998 if (c
->cn_xprt
== xpt
) {
3005 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
3007 struct nfs4_client
*clp
= ses
->se_client
;
3008 struct nfsd4_conn
*c
;
3009 __be32 status
= nfs_ok
;
3012 spin_lock(&clp
->cl_lock
);
3013 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
3016 status
= nfserr_conn_not_bound_to_session
;
3017 if (clp
->cl_mach_cred
)
3019 __nfsd4_hash_conn(new, ses
);
3020 spin_unlock(&clp
->cl_lock
);
3021 ret
= nfsd4_register_conn(new);
3023 /* oops; xprt is already down: */
3024 nfsd4_conn_lost(&new->cn_xpt_user
);
3027 spin_unlock(&clp
->cl_lock
);
3032 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
3034 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
3036 return args
->opcnt
> session
->se_fchannel
.maxops
;
3039 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
3040 struct nfsd4_session
*session
)
3042 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
3044 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
3047 static bool replay_matches_cache(struct svc_rqst
*rqstp
,
3048 struct nfsd4_sequence
*seq
, struct nfsd4_slot
*slot
)
3050 struct nfsd4_compoundargs
*argp
= rqstp
->rq_argp
;
3052 if ((bool)(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
) !=
3053 (bool)seq
->cachethis
)
3056 * If there's an error than the reply can have fewer ops than
3057 * the call. But if we cached a reply with *more* ops than the
3058 * call you're sending us now, then this new call is clearly not
3059 * really a replay of the old one:
3061 if (slot
->sl_opcnt
< argp
->opcnt
)
3063 /* This is the only check explicitly called by spec: */
3064 if (!same_creds(&rqstp
->rq_cred
, &slot
->sl_cred
))
3067 * There may be more comparisons we could actually do, but the
3068 * spec doesn't require us to catch every case where the calls
3069 * don't match (that would require caching the call as well as
3070 * the reply), so we don't bother.
3076 nfsd4_sequence(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3077 union nfsd4_op_u
*u
)
3079 struct nfsd4_sequence
*seq
= &u
->sequence
;
3080 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3081 struct xdr_stream
*xdr
= &resp
->xdr
;
3082 struct nfsd4_session
*session
;
3083 struct nfs4_client
*clp
;
3084 struct nfsd4_slot
*slot
;
3085 struct nfsd4_conn
*conn
;
3088 struct net
*net
= SVC_NET(rqstp
);
3089 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3091 if (resp
->opcnt
!= 1)
3092 return nfserr_sequence_pos
;
3095 * Will be either used or freed by nfsd4_sequence_check_conn
3098 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
3100 return nfserr_jukebox
;
3102 spin_lock(&nn
->client_lock
);
3103 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
3105 goto out_no_session
;
3106 clp
= session
->se_client
;
3108 status
= nfserr_too_many_ops
;
3109 if (nfsd4_session_too_many_ops(rqstp
, session
))
3110 goto out_put_session
;
3112 status
= nfserr_req_too_big
;
3113 if (nfsd4_request_too_big(rqstp
, session
))
3114 goto out_put_session
;
3116 status
= nfserr_badslot
;
3117 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
3118 goto out_put_session
;
3120 slot
= session
->se_slots
[seq
->slotid
];
3121 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
3123 /* We do not negotiate the number of slots yet, so set the
3124 * maxslots to the session maxreqs which is used to encode
3125 * sr_highest_slotid and the sr_target_slot id to maxslots */
3126 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
3128 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
3129 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
3130 if (status
== nfserr_replay_cache
) {
3131 status
= nfserr_seq_misordered
;
3132 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
3133 goto out_put_session
;
3134 status
= nfserr_seq_false_retry
;
3135 if (!replay_matches_cache(rqstp
, seq
, slot
))
3136 goto out_put_session
;
3137 cstate
->slot
= slot
;
3138 cstate
->session
= session
;
3140 /* Return the cached reply status and set cstate->status
3141 * for nfsd4_proc_compound processing */
3142 status
= nfsd4_replay_cache_entry(resp
, seq
);
3143 cstate
->status
= nfserr_replay_cache
;
3147 goto out_put_session
;
3149 status
= nfsd4_sequence_check_conn(conn
, session
);
3152 goto out_put_session
;
3154 buflen
= (seq
->cachethis
) ?
3155 session
->se_fchannel
.maxresp_cached
:
3156 session
->se_fchannel
.maxresp_sz
;
3157 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
3159 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
3160 goto out_put_session
;
3161 svc_reserve(rqstp
, buflen
);
3164 /* Success! bump slot seqid */
3165 slot
->sl_seqid
= seq
->seqid
;
3166 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
3168 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
3170 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
3172 cstate
->slot
= slot
;
3173 cstate
->session
= session
;
3177 switch (clp
->cl_cb_state
) {
3179 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
3181 case NFSD4_CB_FAULT
:
3182 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
3185 seq
->status_flags
= 0;
3187 if (!list_empty(&clp
->cl_revoked
))
3188 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
3192 spin_unlock(&nn
->client_lock
);
3195 nfsd4_put_session_locked(session
);
3196 goto out_no_session
;
3200 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
3202 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
3204 if (nfsd4_has_session(cs
)) {
3205 if (cs
->status
!= nfserr_replay_cache
) {
3206 nfsd4_store_cache_entry(resp
);
3207 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
3209 /* Drop session reference that was taken in nfsd4_sequence() */
3210 nfsd4_put_session(cs
->session
);
3212 put_client_renew(cs
->clp
);
3216 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
,
3217 struct nfsd4_compound_state
*cstate
,
3218 union nfsd4_op_u
*u
)
3220 struct nfsd4_destroy_clientid
*dc
= &u
->destroy_clientid
;
3221 struct nfs4_client
*conf
, *unconf
;
3222 struct nfs4_client
*clp
= NULL
;
3224 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3226 spin_lock(&nn
->client_lock
);
3227 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
3228 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
3229 WARN_ON_ONCE(conf
&& unconf
);
3232 if (client_has_state(conf
)) {
3233 status
= nfserr_clientid_busy
;
3236 status
= mark_client_expired_locked(conf
);
3243 status
= nfserr_stale_clientid
;
3246 if (!nfsd4_mach_creds_match(clp
, rqstp
)) {
3248 status
= nfserr_wrong_cred
;
3251 unhash_client_locked(clp
);
3253 spin_unlock(&nn
->client_lock
);
3260 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
,
3261 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
3263 struct nfsd4_reclaim_complete
*rc
= &u
->reclaim_complete
;
3266 if (rc
->rca_one_fs
) {
3267 if (!cstate
->current_fh
.fh_dentry
)
3268 return nfserr_nofilehandle
;
3270 * We don't take advantage of the rca_one_fs case.
3271 * That's OK, it's optional, we can safely ignore it.
3276 status
= nfserr_complete_already
;
3277 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
3278 &cstate
->session
->se_client
->cl_flags
))
3281 status
= nfserr_stale_clientid
;
3282 if (is_client_expired(cstate
->session
->se_client
))
3284 * The following error isn't really legal.
3285 * But we only get here if the client just explicitly
3286 * destroyed the client. Surely it no longer cares what
3287 * error it gets back on an operation for the dead
3293 nfsd4_client_record_create(cstate
->session
->se_client
);
3299 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3300 union nfsd4_op_u
*u
)
3302 struct nfsd4_setclientid
*setclid
= &u
->setclientid
;
3303 struct xdr_netobj clname
= setclid
->se_name
;
3304 nfs4_verifier clverifier
= setclid
->se_verf
;
3305 struct nfs4_client
*conf
, *new;
3306 struct nfs4_client
*unconf
= NULL
;
3308 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3310 new = create_client(clname
, rqstp
, &clverifier
);
3312 return nfserr_jukebox
;
3313 /* Cases below refer to rfc 3530 section 14.2.33: */
3314 spin_lock(&nn
->client_lock
);
3315 conf
= find_confirmed_client_by_name(&clname
, nn
);
3316 if (conf
&& client_has_state(conf
)) {
3318 status
= nfserr_clid_inuse
;
3319 if (clp_used_exchangeid(conf
))
3321 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
3322 char addr_str
[INET6_ADDRSTRLEN
];
3323 rpc_ntop((struct sockaddr
*) &conf
->cl_addr
, addr_str
,
3325 dprintk("NFSD: setclientid: string in use by client "
3326 "at %s\n", addr_str
);
3330 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
3332 unhash_client_locked(unconf
);
3333 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
)) {
3334 /* case 1: probable callback update */
3335 copy_clid(new, conf
);
3336 gen_confirm(new, nn
);
3337 } else /* case 4 (new client) or cases 2, 3 (client reboot): */
3339 new->cl_minorversion
= 0;
3340 gen_callback(new, setclid
, rqstp
);
3341 add_to_unconfirmed(new);
3342 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
3343 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
3344 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
3348 spin_unlock(&nn
->client_lock
);
3352 expire_client(unconf
);
3358 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
3359 struct nfsd4_compound_state
*cstate
,
3360 union nfsd4_op_u
*u
)
3362 struct nfsd4_setclientid_confirm
*setclientid_confirm
=
3363 &u
->setclientid_confirm
;
3364 struct nfs4_client
*conf
, *unconf
;
3365 struct nfs4_client
*old
= NULL
;
3366 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
3367 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
3369 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3371 if (STALE_CLIENTID(clid
, nn
))
3372 return nfserr_stale_clientid
;
3374 spin_lock(&nn
->client_lock
);
3375 conf
= find_confirmed_client(clid
, false, nn
);
3376 unconf
= find_unconfirmed_client(clid
, false, nn
);
3378 * We try hard to give out unique clientid's, so if we get an
3379 * attempt to confirm the same clientid with a different cred,
3380 * the client may be buggy; this should never happen.
3382 * Nevertheless, RFC 7530 recommends INUSE for this case:
3384 status
= nfserr_clid_inuse
;
3385 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
3387 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
3389 /* cases below refer to rfc 3530 section 14.2.34: */
3390 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
3391 if (conf
&& same_verf(&confirm
, &conf
->cl_confirm
)) {
3392 /* case 2: probable retransmit */
3394 } else /* case 4: client hasn't noticed we rebooted yet? */
3395 status
= nfserr_stale_clientid
;
3399 if (conf
) { /* case 1: callback update */
3401 unhash_client_locked(old
);
3402 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
3403 } else { /* case 3: normal case; new or rebooted client */
3404 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
3406 status
= nfserr_clid_inuse
;
3407 if (client_has_state(old
)
3408 && !same_creds(&unconf
->cl_cred
,
3411 status
= mark_client_expired_locked(old
);
3417 move_to_confirmed(unconf
);
3420 get_client_locked(conf
);
3421 spin_unlock(&nn
->client_lock
);
3422 nfsd4_probe_callback(conf
);
3423 spin_lock(&nn
->client_lock
);
3424 put_client_renew_locked(conf
);
3426 spin_unlock(&nn
->client_lock
);
3432 static struct nfs4_file
*nfsd4_alloc_file(void)
3434 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
3437 /* OPEN Share state helper functions */
3438 static void nfsd4_init_file(struct knfsd_fh
*fh
, unsigned int hashval
,
3439 struct nfs4_file
*fp
)
3441 lockdep_assert_held(&state_lock
);
3443 atomic_set(&fp
->fi_ref
, 1);
3444 spin_lock_init(&fp
->fi_lock
);
3445 INIT_LIST_HEAD(&fp
->fi_stateids
);
3446 INIT_LIST_HEAD(&fp
->fi_delegations
);
3447 INIT_LIST_HEAD(&fp
->fi_clnt_odstate
);
3448 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
3449 fp
->fi_deleg_file
= NULL
;
3450 fp
->fi_had_conflict
= false;
3451 fp
->fi_share_deny
= 0;
3452 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
3453 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
3454 #ifdef CONFIG_NFSD_PNFS
3455 INIT_LIST_HEAD(&fp
->fi_lo_states
);
3456 atomic_set(&fp
->fi_lo_recalls
, 0);
3458 hlist_add_head_rcu(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
3462 nfsd4_free_slabs(void)
3464 kmem_cache_destroy(odstate_slab
);
3465 kmem_cache_destroy(openowner_slab
);
3466 kmem_cache_destroy(lockowner_slab
);
3467 kmem_cache_destroy(file_slab
);
3468 kmem_cache_destroy(stateid_slab
);
3469 kmem_cache_destroy(deleg_slab
);
3473 nfsd4_init_slabs(void)
3475 openowner_slab
= kmem_cache_create("nfsd4_openowners",
3476 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
3477 if (openowner_slab
== NULL
)
3479 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
3480 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
3481 if (lockowner_slab
== NULL
)
3482 goto out_free_openowner_slab
;
3483 file_slab
= kmem_cache_create("nfsd4_files",
3484 sizeof(struct nfs4_file
), 0, 0, NULL
);
3485 if (file_slab
== NULL
)
3486 goto out_free_lockowner_slab
;
3487 stateid_slab
= kmem_cache_create("nfsd4_stateids",
3488 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
3489 if (stateid_slab
== NULL
)
3490 goto out_free_file_slab
;
3491 deleg_slab
= kmem_cache_create("nfsd4_delegations",
3492 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
3493 if (deleg_slab
== NULL
)
3494 goto out_free_stateid_slab
;
3495 odstate_slab
= kmem_cache_create("nfsd4_odstate",
3496 sizeof(struct nfs4_clnt_odstate
), 0, 0, NULL
);
3497 if (odstate_slab
== NULL
)
3498 goto out_free_deleg_slab
;
3501 out_free_deleg_slab
:
3502 kmem_cache_destroy(deleg_slab
);
3503 out_free_stateid_slab
:
3504 kmem_cache_destroy(stateid_slab
);
3506 kmem_cache_destroy(file_slab
);
3507 out_free_lockowner_slab
:
3508 kmem_cache_destroy(lockowner_slab
);
3509 out_free_openowner_slab
:
3510 kmem_cache_destroy(openowner_slab
);
3512 dprintk("nfsd4: out of memory while initializing nfsv4\n");
3516 static void init_nfs4_replay(struct nfs4_replay
*rp
)
3518 rp
->rp_status
= nfserr_serverfault
;
3520 rp
->rp_buf
= rp
->rp_ibuf
;
3521 mutex_init(&rp
->rp_mutex
);
3524 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state
*cstate
,
3525 struct nfs4_stateowner
*so
)
3527 if (!nfsd4_has_session(cstate
)) {
3528 mutex_lock(&so
->so_replay
.rp_mutex
);
3529 cstate
->replay_owner
= nfs4_get_stateowner(so
);
3533 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state
*cstate
)
3535 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
3538 cstate
->replay_owner
= NULL
;
3539 mutex_unlock(&so
->so_replay
.rp_mutex
);
3540 nfs4_put_stateowner(so
);
3544 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
3546 struct nfs4_stateowner
*sop
;
3548 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
3552 sop
->so_owner
.data
= kmemdup(owner
->data
, owner
->len
, GFP_KERNEL
);
3553 if (!sop
->so_owner
.data
) {
3554 kmem_cache_free(slab
, sop
);
3557 sop
->so_owner
.len
= owner
->len
;
3559 INIT_LIST_HEAD(&sop
->so_stateids
);
3560 sop
->so_client
= clp
;
3561 init_nfs4_replay(&sop
->so_replay
);
3562 atomic_set(&sop
->so_count
, 1);
3566 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
3568 lockdep_assert_held(&clp
->cl_lock
);
3570 list_add(&oo
->oo_owner
.so_strhash
,
3571 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
3572 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
3575 static void nfs4_unhash_openowner(struct nfs4_stateowner
*so
)
3577 unhash_openowner_locked(openowner(so
));
3580 static void nfs4_free_openowner(struct nfs4_stateowner
*so
)
3582 struct nfs4_openowner
*oo
= openowner(so
);
3584 kmem_cache_free(openowner_slab
, oo
);
3587 static const struct nfs4_stateowner_operations openowner_ops
= {
3588 .so_unhash
= nfs4_unhash_openowner
,
3589 .so_free
= nfs4_free_openowner
,
3592 static struct nfs4_ol_stateid
*
3593 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3595 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
3596 struct nfs4_openowner
*oo
= open
->op_openowner
;
3598 lockdep_assert_held(&fp
->fi_lock
);
3600 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
3601 /* ignore lock owners */
3602 if (local
->st_stateowner
->so_is_open_owner
== 0)
3604 if (local
->st_stateowner
!= &oo
->oo_owner
)
3606 if (local
->st_stid
.sc_type
== NFS4_OPEN_STID
) {
3608 atomic_inc(&ret
->st_stid
.sc_count
);
3616 nfsd4_verify_open_stid(struct nfs4_stid
*s
)
3618 __be32 ret
= nfs_ok
;
3620 switch (s
->sc_type
) {
3623 case NFS4_CLOSED_STID
:
3624 case NFS4_CLOSED_DELEG_STID
:
3625 ret
= nfserr_bad_stateid
;
3627 case NFS4_REVOKED_DELEG_STID
:
3628 ret
= nfserr_deleg_revoked
;
3633 /* Lock the stateid st_mutex, and deal with races with CLOSE */
3635 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid
*stp
)
3639 mutex_lock(&stp
->st_mutex
);
3640 ret
= nfsd4_verify_open_stid(&stp
->st_stid
);
3642 mutex_unlock(&stp
->st_mutex
);
3646 static struct nfs4_ol_stateid
*
3647 nfsd4_find_and_lock_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3649 struct nfs4_ol_stateid
*stp
;
3651 spin_lock(&fp
->fi_lock
);
3652 stp
= nfsd4_find_existing_open(fp
, open
);
3653 spin_unlock(&fp
->fi_lock
);
3654 if (!stp
|| nfsd4_lock_ol_stateid(stp
) == nfs_ok
)
3656 nfs4_put_stid(&stp
->st_stid
);
3661 static struct nfs4_openowner
*
3662 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
3663 struct nfsd4_compound_state
*cstate
)
3665 struct nfs4_client
*clp
= cstate
->clp
;
3666 struct nfs4_openowner
*oo
, *ret
;
3668 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
3671 oo
->oo_owner
.so_ops
= &openowner_ops
;
3672 oo
->oo_owner
.so_is_open_owner
= 1;
3673 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
3675 if (nfsd4_has_session(cstate
))
3676 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
3678 oo
->oo_last_closed_stid
= NULL
;
3679 INIT_LIST_HEAD(&oo
->oo_close_lru
);
3680 spin_lock(&clp
->cl_lock
);
3681 ret
= find_openstateowner_str_locked(strhashval
, open
, clp
);
3683 hash_openowner(oo
, clp
, strhashval
);
3686 nfs4_free_stateowner(&oo
->oo_owner
);
3688 spin_unlock(&clp
->cl_lock
);
3692 static struct nfs4_ol_stateid
*
3693 init_open_stateid(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
3696 struct nfs4_openowner
*oo
= open
->op_openowner
;
3697 struct nfs4_ol_stateid
*retstp
= NULL
;
3698 struct nfs4_ol_stateid
*stp
;
3701 /* We are moving these outside of the spinlocks to avoid the warnings */
3702 mutex_init(&stp
->st_mutex
);
3703 mutex_lock(&stp
->st_mutex
);
3706 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
3707 spin_lock(&fp
->fi_lock
);
3709 retstp
= nfsd4_find_existing_open(fp
, open
);
3713 open
->op_stp
= NULL
;
3714 atomic_inc(&stp
->st_stid
.sc_count
);
3715 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
3716 INIT_LIST_HEAD(&stp
->st_locks
);
3717 stp
->st_stateowner
= nfs4_get_stateowner(&oo
->oo_owner
);
3719 stp
->st_stid
.sc_file
= fp
;
3720 stp
->st_access_bmap
= 0;
3721 stp
->st_deny_bmap
= 0;
3722 stp
->st_openstp
= NULL
;
3723 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
3724 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
3727 spin_unlock(&fp
->fi_lock
);
3728 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
3730 /* Handle races with CLOSE */
3731 if (nfsd4_lock_ol_stateid(retstp
) != nfs_ok
) {
3732 nfs4_put_stid(&retstp
->st_stid
);
3735 /* To keep mutex tracking happy */
3736 mutex_unlock(&stp
->st_mutex
);
3743 * In the 4.0 case we need to keep the owners around a little while to handle
3744 * CLOSE replay. We still do need to release any file access that is held by
3745 * them before returning however.
3748 move_to_close_lru(struct nfs4_ol_stateid
*s
, struct net
*net
)
3750 struct nfs4_ol_stateid
*last
;
3751 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
3752 struct nfsd_net
*nn
= net_generic(s
->st_stid
.sc_client
->net
,
3755 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
3758 * We know that we hold one reference via nfsd4_close, and another
3759 * "persistent" reference for the client. If the refcount is higher
3760 * than 2, then there are still calls in progress that are using this
3761 * stateid. We can't put the sc_file reference until they are finished.
3762 * Wait for the refcount to drop to 2. Since it has been unhashed,
3763 * there should be no danger of the refcount going back up again at
3766 wait_event(close_wq
, atomic_read(&s
->st_stid
.sc_count
) == 2);
3768 release_all_access(s
);
3769 if (s
->st_stid
.sc_file
) {
3770 put_nfs4_file(s
->st_stid
.sc_file
);
3771 s
->st_stid
.sc_file
= NULL
;
3774 spin_lock(&nn
->client_lock
);
3775 last
= oo
->oo_last_closed_stid
;
3776 oo
->oo_last_closed_stid
= s
;
3777 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
3778 oo
->oo_time
= get_seconds();
3779 spin_unlock(&nn
->client_lock
);
3781 nfs4_put_stid(&last
->st_stid
);
3784 /* search file_hashtbl[] for file */
3785 static struct nfs4_file
*
3786 find_file_locked(struct knfsd_fh
*fh
, unsigned int hashval
)
3788 struct nfs4_file
*fp
;
3790 hlist_for_each_entry_rcu(fp
, &file_hashtbl
[hashval
], fi_hash
) {
3791 if (fh_match(&fp
->fi_fhandle
, fh
)) {
3792 if (atomic_inc_not_zero(&fp
->fi_ref
))
3800 find_file(struct knfsd_fh
*fh
)
3802 struct nfs4_file
*fp
;
3803 unsigned int hashval
= file_hashval(fh
);
3806 fp
= find_file_locked(fh
, hashval
);
3811 static struct nfs4_file
*
3812 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
3814 struct nfs4_file
*fp
;
3815 unsigned int hashval
= file_hashval(fh
);
3818 fp
= find_file_locked(fh
, hashval
);
3823 spin_lock(&state_lock
);
3824 fp
= find_file_locked(fh
, hashval
);
3825 if (likely(fp
== NULL
)) {
3826 nfsd4_init_file(fh
, hashval
, new);
3829 spin_unlock(&state_lock
);
3835 * Called to check deny when READ with all zero stateid or
3836 * WRITE with all zero or all one stateid
3839 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
3841 struct nfs4_file
*fp
;
3842 __be32 ret
= nfs_ok
;
3844 fp
= find_file(¤t_fh
->fh_handle
);
3847 /* Check for conflicting share reservations */
3848 spin_lock(&fp
->fi_lock
);
3849 if (fp
->fi_share_deny
& deny_type
)
3850 ret
= nfserr_locked
;
3851 spin_unlock(&fp
->fi_lock
);
3856 static void nfsd4_cb_recall_prepare(struct nfsd4_callback
*cb
)
3858 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3859 struct nfsd_net
*nn
= net_generic(dp
->dl_stid
.sc_client
->net
,
3862 block_delegations(&dp
->dl_stid
.sc_file
->fi_fhandle
);
3865 * We can't do this in nfsd_break_deleg_cb because it is
3866 * already holding inode->i_lock.
3868 * If the dl_time != 0, then we know that it has already been
3869 * queued for a lease break. Don't queue it again.
3871 spin_lock(&state_lock
);
3872 if (dp
->dl_time
== 0) {
3873 dp
->dl_time
= get_seconds();
3874 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
3876 spin_unlock(&state_lock
);
3879 static int nfsd4_cb_recall_done(struct nfsd4_callback
*cb
,
3880 struct rpc_task
*task
)
3882 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3884 if (dp
->dl_stid
.sc_type
== NFS4_CLOSED_DELEG_STID
)
3887 switch (task
->tk_status
) {
3891 case -NFS4ERR_BAD_STATEID
:
3893 * Race: client probably got cb_recall before open reply
3894 * granting delegation.
3896 if (dp
->dl_retries
--) {
3897 rpc_delay(task
, 2 * HZ
);
3906 static void nfsd4_cb_recall_release(struct nfsd4_callback
*cb
)
3908 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
3910 nfs4_put_stid(&dp
->dl_stid
);
3913 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops
= {
3914 .prepare
= nfsd4_cb_recall_prepare
,
3915 .done
= nfsd4_cb_recall_done
,
3916 .release
= nfsd4_cb_recall_release
,
3919 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
3922 * We're assuming the state code never drops its reference
3923 * without first removing the lease. Since we're in this lease
3924 * callback (and since the lease code is serialized by the kernel
3925 * lock) we know the server hasn't removed the lease yet, we know
3926 * it's safe to take a reference.
3928 atomic_inc(&dp
->dl_stid
.sc_count
);
3929 nfsd4_run_cb(&dp
->dl_recall
);
3932 /* Called from break_lease() with i_lock held. */
3934 nfsd_break_deleg_cb(struct file_lock
*fl
)
3937 struct nfs4_file
*fp
= (struct nfs4_file
*)fl
->fl_owner
;
3938 struct nfs4_delegation
*dp
;
3941 WARN(1, "(%p)->fl_owner NULL\n", fl
);
3944 if (fp
->fi_had_conflict
) {
3945 WARN(1, "duplicate break on %p\n", fp
);
3949 * We don't want the locks code to timeout the lease for us;
3950 * we'll remove it ourself if a delegation isn't returned
3953 fl
->fl_break_time
= 0;
3955 spin_lock(&fp
->fi_lock
);
3956 fp
->fi_had_conflict
= true;
3958 * If there are no delegations on the list, then return true
3959 * so that the lease code will go ahead and delete it.
3961 if (list_empty(&fp
->fi_delegations
))
3964 list_for_each_entry(dp
, &fp
->fi_delegations
, dl_perfile
)
3965 nfsd_break_one_deleg(dp
);
3966 spin_unlock(&fp
->fi_lock
);
3971 nfsd_change_deleg_cb(struct file_lock
*onlist
, int arg
,
3972 struct list_head
*dispose
)
3975 return lease_modify(onlist
, arg
, dispose
);
3980 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
3981 .lm_break
= nfsd_break_deleg_cb
,
3982 .lm_change
= nfsd_change_deleg_cb
,
3985 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
3987 if (nfsd4_has_session(cstate
))
3989 if (seqid
== so
->so_seqid
- 1)
3990 return nfserr_replay_me
;
3991 if (seqid
== so
->so_seqid
)
3993 return nfserr_bad_seqid
;
3996 static __be32
lookup_clientid(clientid_t
*clid
,
3997 struct nfsd4_compound_state
*cstate
,
3998 struct nfsd_net
*nn
)
4000 struct nfs4_client
*found
;
4003 found
= cstate
->clp
;
4004 if (!same_clid(&found
->cl_clientid
, clid
))
4005 return nfserr_stale_clientid
;
4009 if (STALE_CLIENTID(clid
, nn
))
4010 return nfserr_stale_clientid
;
4013 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4014 * cached already then we know this is for is for v4.0 and "sessions"
4017 WARN_ON_ONCE(cstate
->session
);
4018 spin_lock(&nn
->client_lock
);
4019 found
= find_confirmed_client(clid
, false, nn
);
4021 spin_unlock(&nn
->client_lock
);
4022 return nfserr_expired
;
4024 atomic_inc(&found
->cl_refcount
);
4025 spin_unlock(&nn
->client_lock
);
4027 /* Cache the nfs4_client in cstate! */
4028 cstate
->clp
= found
;
4033 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
4034 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
4036 clientid_t
*clientid
= &open
->op_clientid
;
4037 struct nfs4_client
*clp
= NULL
;
4038 unsigned int strhashval
;
4039 struct nfs4_openowner
*oo
= NULL
;
4042 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
4043 return nfserr_stale_clientid
;
4045 * In case we need it later, after we've already created the
4046 * file and don't want to risk a further failure:
4048 open
->op_file
= nfsd4_alloc_file();
4049 if (open
->op_file
== NULL
)
4050 return nfserr_jukebox
;
4052 status
= lookup_clientid(clientid
, cstate
, nn
);
4057 strhashval
= ownerstr_hashval(&open
->op_owner
);
4058 oo
= find_openstateowner_str(strhashval
, open
, clp
);
4059 open
->op_openowner
= oo
;
4063 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
4064 /* Replace unconfirmed owners without checking for replay. */
4065 release_openowner(oo
);
4066 open
->op_openowner
= NULL
;
4069 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
4074 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
4076 return nfserr_jukebox
;
4077 open
->op_openowner
= oo
;
4079 open
->op_stp
= nfs4_alloc_open_stateid(clp
);
4081 return nfserr_jukebox
;
4083 if (nfsd4_has_session(cstate
) &&
4084 (cstate
->current_fh
.fh_export
->ex_flags
& NFSEXP_PNFS
)) {
4085 open
->op_odstate
= alloc_clnt_odstate(clp
);
4086 if (!open
->op_odstate
)
4087 return nfserr_jukebox
;
4093 static inline __be32
4094 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
4096 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
4097 return nfserr_openmode
;
4102 static int share_access_to_flags(u32 share_access
)
4104 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
4107 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
4109 struct nfs4_stid
*ret
;
4111 ret
= find_stateid_by_type(cl
, s
,
4112 NFS4_DELEG_STID
|NFS4_REVOKED_DELEG_STID
);
4115 return delegstateid(ret
);
4118 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
4120 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
4121 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
4125 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
4126 struct nfs4_delegation
**dp
)
4129 __be32 status
= nfserr_bad_stateid
;
4130 struct nfs4_delegation
*deleg
;
4132 deleg
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
4135 if (deleg
->dl_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
) {
4136 nfs4_put_stid(&deleg
->dl_stid
);
4137 if (cl
->cl_minorversion
)
4138 status
= nfserr_deleg_revoked
;
4141 flags
= share_access_to_flags(open
->op_share_access
);
4142 status
= nfs4_check_delegmode(deleg
, flags
);
4144 nfs4_put_stid(&deleg
->dl_stid
);
4149 if (!nfsd4_is_deleg_cur(open
))
4153 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
4157 static inline int nfs4_access_to_access(u32 nfs4_access
)
4161 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
4162 flags
|= NFSD_MAY_READ
;
4163 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
4164 flags
|= NFSD_MAY_WRITE
;
4168 static inline __be32
4169 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
4170 struct nfsd4_open
*open
)
4172 struct iattr iattr
= {
4173 .ia_valid
= ATTR_SIZE
,
4176 if (!open
->op_truncate
)
4178 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
4179 return nfserr_inval
;
4180 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time_t)0);
4183 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
4184 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
4185 struct nfsd4_open
*open
)
4187 struct file
*filp
= NULL
;
4189 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
4190 int access
= nfs4_access_to_access(open
->op_share_access
);
4191 unsigned char old_access_bmap
, old_deny_bmap
;
4193 spin_lock(&fp
->fi_lock
);
4196 * Are we trying to set a deny mode that would conflict with
4199 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
4200 if (status
!= nfs_ok
) {
4201 spin_unlock(&fp
->fi_lock
);
4205 /* set access to the file */
4206 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
4207 if (status
!= nfs_ok
) {
4208 spin_unlock(&fp
->fi_lock
);
4212 /* Set access bits in stateid */
4213 old_access_bmap
= stp
->st_access_bmap
;
4214 set_access(open
->op_share_access
, stp
);
4216 /* Set new deny mask */
4217 old_deny_bmap
= stp
->st_deny_bmap
;
4218 set_deny(open
->op_share_deny
, stp
);
4219 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4221 if (!fp
->fi_fds
[oflag
]) {
4222 spin_unlock(&fp
->fi_lock
);
4223 status
= nfsd_open(rqstp
, cur_fh
, S_IFREG
, access
, &filp
);
4225 goto out_put_access
;
4226 spin_lock(&fp
->fi_lock
);
4227 if (!fp
->fi_fds
[oflag
]) {
4228 fp
->fi_fds
[oflag
] = filp
;
4232 spin_unlock(&fp
->fi_lock
);
4236 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4238 goto out_put_access
;
4242 stp
->st_access_bmap
= old_access_bmap
;
4243 nfs4_file_put_access(fp
, open
->op_share_access
);
4244 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
4249 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
)
4252 unsigned char old_deny_bmap
= stp
->st_deny_bmap
;
4254 if (!test_access(open
->op_share_access
, stp
))
4255 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
4257 /* test and set deny mode */
4258 spin_lock(&fp
->fi_lock
);
4259 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
4260 if (status
== nfs_ok
) {
4261 set_deny(open
->op_share_deny
, stp
);
4262 fp
->fi_share_deny
|=
4263 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4265 spin_unlock(&fp
->fi_lock
);
4267 if (status
!= nfs_ok
)
4270 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4271 if (status
!= nfs_ok
)
4272 reset_union_bmap_deny(old_deny_bmap
, stp
);
4276 /* Should we give out recallable state?: */
4277 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
4279 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
4282 * In the sessions case, since we don't have to establish a
4283 * separate connection for callbacks, we assume it's OK
4284 * until we hear otherwise:
4286 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
4289 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_file
*fp
, int flag
)
4291 struct file_lock
*fl
;
4293 fl
= locks_alloc_lock();
4296 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
4297 fl
->fl_flags
= FL_DELEG
;
4298 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
4299 fl
->fl_end
= OFFSET_MAX
;
4300 fl
->fl_owner
= (fl_owner_t
)fp
;
4301 fl
->fl_pid
= current
->tgid
;
4306 * nfs4_setlease - Obtain a delegation by requesting lease from vfs layer
4307 * @dp: a pointer to the nfs4_delegation we're adding.
4310 * On success: Return code will be 0 on success.
4312 * On error: -EAGAIN if there was an existing delegation.
4313 * nonzero if there is an error in other cases.
4317 static int nfs4_setlease(struct nfs4_delegation
*dp
)
4319 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
4320 struct file_lock
*fl
;
4324 fl
= nfs4_alloc_init_lease(fp
, NFS4_OPEN_DELEGATE_READ
);
4327 filp
= find_readable_file(fp
);
4329 /* We should always have a readable file here */
4331 locks_free_lock(fl
);
4335 status
= vfs_setlease(filp
, fl
->fl_type
, &fl
, NULL
);
4337 locks_free_lock(fl
);
4340 spin_lock(&state_lock
);
4341 spin_lock(&fp
->fi_lock
);
4342 /* Did the lease get broken before we took the lock? */
4344 if (fp
->fi_had_conflict
)
4347 if (fp
->fi_deleg_file
) {
4348 status
= hash_delegation_locked(dp
, fp
);
4351 fp
->fi_deleg_file
= filp
;
4352 fp
->fi_delegees
= 0;
4353 status
= hash_delegation_locked(dp
, fp
);
4354 spin_unlock(&fp
->fi_lock
);
4355 spin_unlock(&state_lock
);
4357 /* Should never happen, this is a new fi_deleg_file */
4363 spin_unlock(&fp
->fi_lock
);
4364 spin_unlock(&state_lock
);
4370 static struct nfs4_delegation
*
4371 nfs4_set_delegation(struct nfs4_client
*clp
, struct svc_fh
*fh
,
4372 struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*odstate
)
4375 struct nfs4_delegation
*dp
;
4377 if (fp
->fi_had_conflict
)
4378 return ERR_PTR(-EAGAIN
);
4380 spin_lock(&state_lock
);
4381 spin_lock(&fp
->fi_lock
);
4382 status
= nfs4_get_existing_delegation(clp
, fp
);
4383 spin_unlock(&fp
->fi_lock
);
4384 spin_unlock(&state_lock
);
4387 return ERR_PTR(status
);
4389 dp
= alloc_init_deleg(clp
, fh
, odstate
);
4391 return ERR_PTR(-ENOMEM
);
4394 spin_lock(&state_lock
);
4395 spin_lock(&fp
->fi_lock
);
4396 dp
->dl_stid
.sc_file
= fp
;
4397 if (!fp
->fi_deleg_file
) {
4398 spin_unlock(&fp
->fi_lock
);
4399 spin_unlock(&state_lock
);
4400 status
= nfs4_setlease(dp
);
4403 if (fp
->fi_had_conflict
) {
4407 status
= hash_delegation_locked(dp
, fp
);
4409 spin_unlock(&fp
->fi_lock
);
4410 spin_unlock(&state_lock
);
4413 put_clnt_odstate(dp
->dl_clnt_odstate
);
4414 nfs4_put_stid(&dp
->dl_stid
);
4415 return ERR_PTR(status
);
4420 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
4422 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4423 if (status
== -EAGAIN
)
4424 open
->op_why_no_deleg
= WND4_CONTENTION
;
4426 open
->op_why_no_deleg
= WND4_RESOURCE
;
4427 switch (open
->op_deleg_want
) {
4428 case NFS4_SHARE_WANT_READ_DELEG
:
4429 case NFS4_SHARE_WANT_WRITE_DELEG
:
4430 case NFS4_SHARE_WANT_ANY_DELEG
:
4432 case NFS4_SHARE_WANT_CANCEL
:
4433 open
->op_why_no_deleg
= WND4_CANCELLED
;
4435 case NFS4_SHARE_WANT_NO_DELEG
:
4442 * Attempt to hand out a delegation.
4444 * Note we don't support write delegations, and won't until the vfs has
4445 * proper support for them.
4448 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
,
4449 struct nfs4_ol_stateid
*stp
)
4451 struct nfs4_delegation
*dp
;
4452 struct nfs4_openowner
*oo
= openowner(stp
->st_stateowner
);
4453 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
4457 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
4458 open
->op_recall
= 0;
4459 switch (open
->op_claim_type
) {
4460 case NFS4_OPEN_CLAIM_PREVIOUS
:
4462 open
->op_recall
= 1;
4463 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
4466 case NFS4_OPEN_CLAIM_NULL
:
4467 case NFS4_OPEN_CLAIM_FH
:
4469 * Let's not give out any delegations till everyone's
4470 * had the chance to reclaim theirs, *and* until
4471 * NLM locks have all been reclaimed:
4473 if (locks_in_grace(clp
->net
))
4475 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
4478 * Also, if the file was opened for write or
4479 * create, there's a good chance the client's
4480 * about to write to it, resulting in an
4481 * immediate recall (since we don't support
4482 * write delegations):
4484 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
4486 if (open
->op_create
== NFS4_OPEN_CREATE
)
4492 dp
= nfs4_set_delegation(clp
, fh
, stp
->st_stid
.sc_file
, stp
->st_clnt_odstate
);
4496 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
4498 dprintk("NFSD: delegation stateid=" STATEID_FMT
"\n",
4499 STATEID_VAL(&dp
->dl_stid
.sc_stateid
));
4500 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
4501 nfs4_put_stid(&dp
->dl_stid
);
4504 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
4505 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
4506 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
4507 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
4508 open
->op_recall
= 1;
4511 /* 4.1 client asking for a delegation? */
4512 if (open
->op_deleg_want
)
4513 nfsd4_open_deleg_none_ext(open
, status
);
4517 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
4518 struct nfs4_delegation
*dp
)
4520 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
4521 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4522 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4523 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
4524 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
4525 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
4526 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4527 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
4529 /* Otherwise the client must be confused wanting a delegation
4530 * it already has, therefore we don't return
4531 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
4536 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
4538 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
4539 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
4540 struct nfs4_file
*fp
= NULL
;
4541 struct nfs4_ol_stateid
*stp
= NULL
;
4542 struct nfs4_delegation
*dp
= NULL
;
4544 bool new_stp
= false;
4547 * Lookup file; if found, lookup stateid and check open request,
4548 * and check for delegations in the process of being recalled.
4549 * If not found, create the nfs4_file struct
4551 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
4552 if (fp
!= open
->op_file
) {
4553 status
= nfs4_check_deleg(cl
, open
, &dp
);
4556 stp
= nfsd4_find_and_lock_existing_open(fp
, open
);
4558 open
->op_file
= NULL
;
4559 status
= nfserr_bad_stateid
;
4560 if (nfsd4_is_deleg_cur(open
))
4565 stp
= init_open_stateid(fp
, open
);
4571 * OPEN the file, or upgrade an existing OPEN.
4572 * If truncate fails, the OPEN fails.
4574 * stp is already locked.
4577 /* Stateid was found, this is an OPEN upgrade */
4578 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
4580 mutex_unlock(&stp
->st_mutex
);
4584 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
4586 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
4587 release_open_stateid(stp
);
4588 mutex_unlock(&stp
->st_mutex
);
4592 stp
->st_clnt_odstate
= find_or_hash_clnt_odstate(fp
,
4594 if (stp
->st_clnt_odstate
== open
->op_odstate
)
4595 open
->op_odstate
= NULL
;
4598 nfs4_inc_and_copy_stateid(&open
->op_stateid
, &stp
->st_stid
);
4599 mutex_unlock(&stp
->st_mutex
);
4601 if (nfsd4_has_session(&resp
->cstate
)) {
4602 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
4603 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
4604 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
4610 * Attempt to hand out a delegation. No error return, because the
4611 * OPEN succeeds even if we fail.
4613 nfs4_open_delegation(current_fh
, open
, stp
);
4617 dprintk("%s: stateid=" STATEID_FMT
"\n", __func__
,
4618 STATEID_VAL(&stp
->st_stid
.sc_stateid
));
4620 /* 4.1 client trying to upgrade/downgrade delegation? */
4621 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
4622 open
->op_deleg_want
)
4623 nfsd4_deleg_xgrade_none_ext(open
, dp
);
4627 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
4628 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
4630 * To finish the open response, we just need to set the rflags.
4632 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
4633 if (nfsd4_has_session(&resp
->cstate
))
4634 open
->op_rflags
|= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
;
4635 else if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
))
4636 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
4639 nfs4_put_stid(&dp
->dl_stid
);
4641 nfs4_put_stid(&stp
->st_stid
);
4646 void nfsd4_cleanup_open_state(struct nfsd4_compound_state
*cstate
,
4647 struct nfsd4_open
*open
)
4649 if (open
->op_openowner
) {
4650 struct nfs4_stateowner
*so
= &open
->op_openowner
->oo_owner
;
4652 nfsd4_cstate_assign_replay(cstate
, so
);
4653 nfs4_put_stateowner(so
);
4656 kmem_cache_free(file_slab
, open
->op_file
);
4658 nfs4_put_stid(&open
->op_stp
->st_stid
);
4659 if (open
->op_odstate
)
4660 kmem_cache_free(odstate_slab
, open
->op_odstate
);
4664 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
4665 union nfsd4_op_u
*u
)
4667 clientid_t
*clid
= &u
->renew
;
4668 struct nfs4_client
*clp
;
4670 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
4672 dprintk("process_renew(%08x/%08x): starting\n",
4673 clid
->cl_boot
, clid
->cl_id
);
4674 status
= lookup_clientid(clid
, cstate
, nn
);
4678 status
= nfserr_cb_path_down
;
4679 if (!list_empty(&clp
->cl_delegations
)
4680 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
4688 nfsd4_end_grace(struct nfsd_net
*nn
)
4690 /* do nothing if grace period already ended */
4691 if (nn
->grace_ended
)
4694 dprintk("NFSD: end of grace period\n");
4695 nn
->grace_ended
= true;
4697 * If the server goes down again right now, an NFSv4
4698 * client will still be allowed to reclaim after it comes back up,
4699 * even if it hasn't yet had a chance to reclaim state this time.
4702 nfsd4_record_grace_done(nn
);
4704 * At this point, NFSv4 clients can still reclaim. But if the
4705 * server crashes, any that have not yet reclaimed will be out
4706 * of luck on the next boot.
4708 * (NFSv4.1+ clients are considered to have reclaimed once they
4709 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
4710 * have reclaimed after their first OPEN.)
4712 locks_end_grace(&nn
->nfsd4_manager
);
4714 * At this point, and once lockd and/or any other containers
4715 * exit their grace period, further reclaims will fail and
4716 * regular locking can resume.
4721 nfs4_laundromat(struct nfsd_net
*nn
)
4723 struct nfs4_client
*clp
;
4724 struct nfs4_openowner
*oo
;
4725 struct nfs4_delegation
*dp
;
4726 struct nfs4_ol_stateid
*stp
;
4727 struct nfsd4_blocked_lock
*nbl
;
4728 struct list_head
*pos
, *next
, reaplist
;
4729 time_t cutoff
= get_seconds() - nn
->nfsd4_lease
;
4730 time_t t
, new_timeo
= nn
->nfsd4_lease
;
4732 dprintk("NFSD: laundromat service - starting\n");
4733 nfsd4_end_grace(nn
);
4734 INIT_LIST_HEAD(&reaplist
);
4735 spin_lock(&nn
->client_lock
);
4736 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
4737 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4738 if (time_after((unsigned long)clp
->cl_time
, (unsigned long)cutoff
)) {
4739 t
= clp
->cl_time
- cutoff
;
4740 new_timeo
= min(new_timeo
, t
);
4743 if (mark_client_expired_locked(clp
)) {
4744 dprintk("NFSD: client in use (clientid %08x)\n",
4745 clp
->cl_clientid
.cl_id
);
4748 list_add(&clp
->cl_lru
, &reaplist
);
4750 spin_unlock(&nn
->client_lock
);
4751 list_for_each_safe(pos
, next
, &reaplist
) {
4752 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
4753 dprintk("NFSD: purging unused client (clientid %08x)\n",
4754 clp
->cl_clientid
.cl_id
);
4755 list_del_init(&clp
->cl_lru
);
4758 spin_lock(&state_lock
);
4759 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
4760 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
4761 if (time_after((unsigned long)dp
->dl_time
, (unsigned long)cutoff
)) {
4762 t
= dp
->dl_time
- cutoff
;
4763 new_timeo
= min(new_timeo
, t
);
4766 WARN_ON(!unhash_delegation_locked(dp
));
4767 list_add(&dp
->dl_recall_lru
, &reaplist
);
4769 spin_unlock(&state_lock
);
4770 while (!list_empty(&reaplist
)) {
4771 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
4773 list_del_init(&dp
->dl_recall_lru
);
4774 revoke_delegation(dp
);
4777 spin_lock(&nn
->client_lock
);
4778 while (!list_empty(&nn
->close_lru
)) {
4779 oo
= list_first_entry(&nn
->close_lru
, struct nfs4_openowner
,
4781 if (time_after((unsigned long)oo
->oo_time
,
4782 (unsigned long)cutoff
)) {
4783 t
= oo
->oo_time
- cutoff
;
4784 new_timeo
= min(new_timeo
, t
);
4787 list_del_init(&oo
->oo_close_lru
);
4788 stp
= oo
->oo_last_closed_stid
;
4789 oo
->oo_last_closed_stid
= NULL
;
4790 spin_unlock(&nn
->client_lock
);
4791 nfs4_put_stid(&stp
->st_stid
);
4792 spin_lock(&nn
->client_lock
);
4794 spin_unlock(&nn
->client_lock
);
4797 * It's possible for a client to try and acquire an already held lock
4798 * that is being held for a long time, and then lose interest in it.
4799 * So, we clean out any un-revisited request after a lease period
4800 * under the assumption that the client is no longer interested.
4802 * RFC5661, sec. 9.6 states that the client must not rely on getting
4803 * notifications and must continue to poll for locks, even when the
4804 * server supports them. Thus this shouldn't lead to clients blocking
4805 * indefinitely once the lock does become free.
4807 BUG_ON(!list_empty(&reaplist
));
4808 spin_lock(&nn
->blocked_locks_lock
);
4809 while (!list_empty(&nn
->blocked_locks_lru
)) {
4810 nbl
= list_first_entry(&nn
->blocked_locks_lru
,
4811 struct nfsd4_blocked_lock
, nbl_lru
);
4812 if (time_after((unsigned long)nbl
->nbl_time
,
4813 (unsigned long)cutoff
)) {
4814 t
= nbl
->nbl_time
- cutoff
;
4815 new_timeo
= min(new_timeo
, t
);
4818 list_move(&nbl
->nbl_lru
, &reaplist
);
4819 list_del_init(&nbl
->nbl_list
);
4821 spin_unlock(&nn
->blocked_locks_lock
);
4823 while (!list_empty(&reaplist
)) {
4824 nbl
= list_first_entry(&reaplist
,
4825 struct nfsd4_blocked_lock
, nbl_lru
);
4826 list_del_init(&nbl
->nbl_lru
);
4827 posix_unblock_lock(&nbl
->nbl_lock
);
4828 free_blocked_lock(nbl
);
4831 new_timeo
= max_t(time_t, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
4835 static struct workqueue_struct
*laundry_wq
;
4836 static void laundromat_main(struct work_struct
*);
4839 laundromat_main(struct work_struct
*laundry
)
4842 struct delayed_work
*dwork
= to_delayed_work(laundry
);
4843 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
4846 t
= nfs4_laundromat(nn
);
4847 dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t
);
4848 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
4851 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stid
*stp
)
4853 if (!fh_match(&fhp
->fh_handle
, &stp
->sc_file
->fi_fhandle
))
4854 return nfserr_bad_stateid
;
4859 access_permit_read(struct nfs4_ol_stateid
*stp
)
4861 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
4862 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
4863 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
4867 access_permit_write(struct nfs4_ol_stateid
*stp
)
4869 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
4870 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
4874 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
4876 __be32 status
= nfserr_openmode
;
4878 /* For lock stateid's, we test the parent open, not the lock: */
4879 if (stp
->st_openstp
)
4880 stp
= stp
->st_openstp
;
4881 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
4883 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
4890 static inline __be32
4891 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
4893 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
4895 else if (opens_in_grace(net
)) {
4896 /* Answer in remaining cases depends on existence of
4897 * conflicting state; so we must wait out the grace period. */
4898 return nfserr_grace
;
4899 } else if (flags
& WR_STATE
)
4900 return nfs4_share_conflict(current_fh
,
4901 NFS4_SHARE_DENY_WRITE
);
4902 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
4903 return nfs4_share_conflict(current_fh
,
4904 NFS4_SHARE_DENY_READ
);
4908 * Allow READ/WRITE during grace period on recovered state only for files
4909 * that are not able to provide mandatory locking.
4912 grace_disallows_io(struct net
*net
, struct inode
*inode
)
4914 return opens_in_grace(net
) && mandatory_lock(inode
);
4917 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
4920 * When sessions are used the stateid generation number is ignored
4923 if (has_session
&& in
->si_generation
== 0)
4926 if (in
->si_generation
== ref
->si_generation
)
4929 /* If the client sends us a stateid from the future, it's buggy: */
4930 if (nfsd4_stateid_generation_after(in
, ref
))
4931 return nfserr_bad_stateid
;
4933 * However, we could see a stateid from the past, even from a
4934 * non-buggy client. For example, if the client sends a lock
4935 * while some IO is outstanding, the lock may bump si_generation
4936 * while the IO is still in flight. The client could avoid that
4937 * situation by waiting for responses on all the IO requests,
4938 * but better performance may result in retrying IO that
4939 * receives an old_stateid error if requests are rarely
4940 * reordered in flight:
4942 return nfserr_old_stateid
;
4945 static __be32
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid
*ols
)
4947 if (ols
->st_stateowner
->so_is_open_owner
&&
4948 !(openowner(ols
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
4949 return nfserr_bad_stateid
;
4953 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
4955 struct nfs4_stid
*s
;
4956 __be32 status
= nfserr_bad_stateid
;
4958 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
4959 CLOSE_STATEID(stateid
))
4961 /* Client debugging aid. */
4962 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
)) {
4963 char addr_str
[INET6_ADDRSTRLEN
];
4964 rpc_ntop((struct sockaddr
*)&cl
->cl_addr
, addr_str
,
4966 pr_warn_ratelimited("NFSD: client %s testing state ID "
4967 "with incorrect client ID\n", addr_str
);
4970 spin_lock(&cl
->cl_lock
);
4971 s
= find_stateid_locked(cl
, stateid
);
4974 status
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
4977 switch (s
->sc_type
) {
4978 case NFS4_DELEG_STID
:
4981 case NFS4_REVOKED_DELEG_STID
:
4982 status
= nfserr_deleg_revoked
;
4984 case NFS4_OPEN_STID
:
4985 case NFS4_LOCK_STID
:
4986 status
= nfsd4_check_openowner_confirmed(openlockstateid(s
));
4989 printk("unknown stateid type %x\n", s
->sc_type
);
4991 case NFS4_CLOSED_STID
:
4992 case NFS4_CLOSED_DELEG_STID
:
4993 status
= nfserr_bad_stateid
;
4996 spin_unlock(&cl
->cl_lock
);
5001 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
5002 stateid_t
*stateid
, unsigned char typemask
,
5003 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
5006 bool return_revoked
= false;
5009 * only return revoked delegations if explicitly asked.
5010 * otherwise we report revoked or bad_stateid status.
5012 if (typemask
& NFS4_REVOKED_DELEG_STID
)
5013 return_revoked
= true;
5014 else if (typemask
& NFS4_DELEG_STID
)
5015 typemask
|= NFS4_REVOKED_DELEG_STID
;
5017 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
5018 CLOSE_STATEID(stateid
))
5019 return nfserr_bad_stateid
;
5020 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
);
5021 if (status
== nfserr_stale_clientid
) {
5022 if (cstate
->session
)
5023 return nfserr_bad_stateid
;
5024 return nfserr_stale_stateid
;
5028 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
5030 return nfserr_bad_stateid
;
5031 if (((*s
)->sc_type
== NFS4_REVOKED_DELEG_STID
) && !return_revoked
) {
5033 if (cstate
->minorversion
)
5034 return nfserr_deleg_revoked
;
5035 return nfserr_bad_stateid
;
5040 static struct file
*
5041 nfs4_find_file(struct nfs4_stid
*s
, int flags
)
5046 switch (s
->sc_type
) {
5047 case NFS4_DELEG_STID
:
5048 if (WARN_ON_ONCE(!s
->sc_file
->fi_deleg_file
))
5050 return get_file(s
->sc_file
->fi_deleg_file
);
5051 case NFS4_OPEN_STID
:
5052 case NFS4_LOCK_STID
:
5053 if (flags
& RD_STATE
)
5054 return find_readable_file(s
->sc_file
);
5056 return find_writeable_file(s
->sc_file
);
5064 nfs4_check_olstateid(struct svc_fh
*fhp
, struct nfs4_ol_stateid
*ols
, int flags
)
5068 status
= nfsd4_check_openowner_confirmed(ols
);
5071 return nfs4_check_openmode(ols
, flags
);
5075 nfs4_check_file(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct nfs4_stid
*s
,
5076 struct file
**filpp
, bool *tmp_file
, int flags
)
5078 int acc
= (flags
& RD_STATE
) ? NFSD_MAY_READ
: NFSD_MAY_WRITE
;
5082 file
= nfs4_find_file(s
, flags
);
5084 status
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
5085 acc
| NFSD_MAY_OWNER_OVERRIDE
);
5093 status
= nfsd_open(rqstp
, fhp
, S_IFREG
, acc
, filpp
);
5105 * Checks for stateid operations
5108 nfs4_preprocess_stateid_op(struct svc_rqst
*rqstp
,
5109 struct nfsd4_compound_state
*cstate
, struct svc_fh
*fhp
,
5110 stateid_t
*stateid
, int flags
, struct file
**filpp
, bool *tmp_file
)
5112 struct inode
*ino
= d_inode(fhp
->fh_dentry
);
5113 struct net
*net
= SVC_NET(rqstp
);
5114 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5115 struct nfs4_stid
*s
= NULL
;
5123 if (grace_disallows_io(net
, ino
))
5124 return nfserr_grace
;
5126 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
5127 status
= check_special_stateids(net
, fhp
, stateid
, flags
);
5131 status
= nfsd4_lookup_stateid(cstate
, stateid
,
5132 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
5136 status
= check_stateid_generation(stateid
, &s
->sc_stateid
,
5137 nfsd4_has_session(cstate
));
5141 switch (s
->sc_type
) {
5142 case NFS4_DELEG_STID
:
5143 status
= nfs4_check_delegmode(delegstateid(s
), flags
);
5145 case NFS4_OPEN_STID
:
5146 case NFS4_LOCK_STID
:
5147 status
= nfs4_check_olstateid(fhp
, openlockstateid(s
), flags
);
5150 status
= nfserr_bad_stateid
;
5155 status
= nfs4_check_fh(fhp
, s
);
5158 if (!status
&& filpp
)
5159 status
= nfs4_check_file(rqstp
, fhp
, s
, filpp
, tmp_file
, flags
);
5167 * Test if the stateid is valid
5170 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5171 union nfsd4_op_u
*u
)
5173 struct nfsd4_test_stateid
*test_stateid
= &u
->test_stateid
;
5174 struct nfsd4_test_stateid_id
*stateid
;
5175 struct nfs4_client
*cl
= cstate
->session
->se_client
;
5177 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
5178 stateid
->ts_id_status
=
5179 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
5185 nfsd4_free_lock_stateid(stateid_t
*stateid
, struct nfs4_stid
*s
)
5187 struct nfs4_ol_stateid
*stp
= openlockstateid(s
);
5190 mutex_lock(&stp
->st_mutex
);
5192 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
5196 ret
= nfserr_locks_held
;
5197 if (check_for_locks(stp
->st_stid
.sc_file
,
5198 lockowner(stp
->st_stateowner
)))
5201 release_lock_stateid(stp
);
5205 mutex_unlock(&stp
->st_mutex
);
5211 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5212 union nfsd4_op_u
*u
)
5214 struct nfsd4_free_stateid
*free_stateid
= &u
->free_stateid
;
5215 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
5216 struct nfs4_stid
*s
;
5217 struct nfs4_delegation
*dp
;
5218 struct nfs4_client
*cl
= cstate
->session
->se_client
;
5219 __be32 ret
= nfserr_bad_stateid
;
5221 spin_lock(&cl
->cl_lock
);
5222 s
= find_stateid_locked(cl
, stateid
);
5225 switch (s
->sc_type
) {
5226 case NFS4_DELEG_STID
:
5227 ret
= nfserr_locks_held
;
5229 case NFS4_OPEN_STID
:
5230 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
5233 ret
= nfserr_locks_held
;
5235 case NFS4_LOCK_STID
:
5236 atomic_inc(&s
->sc_count
);
5237 spin_unlock(&cl
->cl_lock
);
5238 ret
= nfsd4_free_lock_stateid(stateid
, s
);
5240 case NFS4_REVOKED_DELEG_STID
:
5241 dp
= delegstateid(s
);
5242 list_del_init(&dp
->dl_recall_lru
);
5243 spin_unlock(&cl
->cl_lock
);
5247 /* Default falls through and returns nfserr_bad_stateid */
5250 spin_unlock(&cl
->cl_lock
);
5258 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
5259 RD_STATE
: WR_STATE
;
5262 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
5264 struct svc_fh
*current_fh
= &cstate
->current_fh
;
5265 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
5268 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
5271 status
= nfsd4_lock_ol_stateid(stp
);
5272 if (status
!= nfs_ok
)
5274 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5275 if (status
== nfs_ok
)
5276 status
= nfs4_check_fh(current_fh
, &stp
->st_stid
);
5277 if (status
!= nfs_ok
)
5278 mutex_unlock(&stp
->st_mutex
);
5283 * Checks for sequence id mutating operations.
5286 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
5287 stateid_t
*stateid
, char typemask
,
5288 struct nfs4_ol_stateid
**stpp
,
5289 struct nfsd_net
*nn
)
5292 struct nfs4_stid
*s
;
5293 struct nfs4_ol_stateid
*stp
= NULL
;
5295 dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT
"\n", __func__
,
5296 seqid
, STATEID_VAL(stateid
));
5299 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
5302 stp
= openlockstateid(s
);
5303 nfsd4_cstate_assign_replay(cstate
, stp
->st_stateowner
);
5305 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
5309 nfs4_put_stid(&stp
->st_stid
);
5313 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
5314 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
5317 struct nfs4_openowner
*oo
;
5318 struct nfs4_ol_stateid
*stp
;
5320 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
5321 NFS4_OPEN_STID
, &stp
, nn
);
5324 oo
= openowner(stp
->st_stateowner
);
5325 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
5326 mutex_unlock(&stp
->st_mutex
);
5327 nfs4_put_stid(&stp
->st_stid
);
5328 return nfserr_bad_stateid
;
5335 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5336 union nfsd4_op_u
*u
)
5338 struct nfsd4_open_confirm
*oc
= &u
->open_confirm
;
5340 struct nfs4_openowner
*oo
;
5341 struct nfs4_ol_stateid
*stp
;
5342 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5344 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
5345 cstate
->current_fh
.fh_dentry
);
5347 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
5351 status
= nfs4_preprocess_seqid_op(cstate
,
5352 oc
->oc_seqid
, &oc
->oc_req_stateid
,
5353 NFS4_OPEN_STID
, &stp
, nn
);
5356 oo
= openowner(stp
->st_stateowner
);
5357 status
= nfserr_bad_stateid
;
5358 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
) {
5359 mutex_unlock(&stp
->st_mutex
);
5362 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
5363 nfs4_inc_and_copy_stateid(&oc
->oc_resp_stateid
, &stp
->st_stid
);
5364 mutex_unlock(&stp
->st_mutex
);
5365 dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT
"\n",
5366 __func__
, oc
->oc_seqid
, STATEID_VAL(&stp
->st_stid
.sc_stateid
));
5368 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
5371 nfs4_put_stid(&stp
->st_stid
);
5373 nfsd4_bump_seqid(cstate
, status
);
5377 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
5379 if (!test_access(access
, stp
))
5381 nfs4_file_put_access(stp
->st_stid
.sc_file
, access
);
5382 clear_access(access
, stp
);
5385 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
5387 switch (to_access
) {
5388 case NFS4_SHARE_ACCESS_READ
:
5389 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
5390 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
5392 case NFS4_SHARE_ACCESS_WRITE
:
5393 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
5394 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
5396 case NFS4_SHARE_ACCESS_BOTH
:
5404 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
5405 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
5407 struct nfsd4_open_downgrade
*od
= &u
->open_downgrade
;
5409 struct nfs4_ol_stateid
*stp
;
5410 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5412 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
5413 cstate
->current_fh
.fh_dentry
);
5415 /* We don't yet support WANT bits: */
5416 if (od
->od_deleg_want
)
5417 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
5420 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
5421 &od
->od_stateid
, &stp
, nn
);
5424 status
= nfserr_inval
;
5425 if (!test_access(od
->od_share_access
, stp
)) {
5426 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
5427 stp
->st_access_bmap
, od
->od_share_access
);
5430 if (!test_deny(od
->od_share_deny
, stp
)) {
5431 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
5432 stp
->st_deny_bmap
, od
->od_share_deny
);
5435 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
5436 reset_union_bmap_deny(od
->od_share_deny
, stp
);
5437 nfs4_inc_and_copy_stateid(&od
->od_stateid
, &stp
->st_stid
);
5440 mutex_unlock(&stp
->st_mutex
);
5441 nfs4_put_stid(&stp
->st_stid
);
5443 nfsd4_bump_seqid(cstate
, status
);
5447 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
5449 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
5451 LIST_HEAD(reaplist
);
5453 spin_lock(&clp
->cl_lock
);
5454 unhashed
= unhash_open_stateid(s
, &reaplist
);
5456 if (clp
->cl_minorversion
) {
5458 put_ol_stateid_locked(s
, &reaplist
);
5459 spin_unlock(&clp
->cl_lock
);
5460 free_ol_stateid_reaplist(&reaplist
);
5462 spin_unlock(&clp
->cl_lock
);
5463 free_ol_stateid_reaplist(&reaplist
);
5465 move_to_close_lru(s
, clp
->net
);
5470 * nfs4_unlock_state() called after encode
5473 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5474 union nfsd4_op_u
*u
)
5476 struct nfsd4_close
*close
= &u
->close
;
5478 struct nfs4_ol_stateid
*stp
;
5479 struct net
*net
= SVC_NET(rqstp
);
5480 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5482 dprintk("NFSD: nfsd4_close on file %pd\n",
5483 cstate
->current_fh
.fh_dentry
);
5485 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
5487 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
5489 nfsd4_bump_seqid(cstate
, status
);
5493 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
5494 nfs4_inc_and_copy_stateid(&close
->cl_stateid
, &stp
->st_stid
);
5496 nfsd4_close_open_stateid(stp
);
5497 mutex_unlock(&stp
->st_mutex
);
5499 /* See RFC5661 sectionm 18.2.4 */
5500 if (stp
->st_stid
.sc_client
->cl_minorversion
)
5501 memcpy(&close
->cl_stateid
, &close_stateid
,
5502 sizeof(close
->cl_stateid
));
5504 /* put reference from nfs4_preprocess_seqid_op */
5505 nfs4_put_stid(&stp
->st_stid
);
5511 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5512 union nfsd4_op_u
*u
)
5514 struct nfsd4_delegreturn
*dr
= &u
->delegreturn
;
5515 struct nfs4_delegation
*dp
;
5516 stateid_t
*stateid
= &dr
->dr_stateid
;
5517 struct nfs4_stid
*s
;
5519 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5521 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
5524 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
5527 dp
= delegstateid(s
);
5528 status
= check_stateid_generation(stateid
, &dp
->dl_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5532 destroy_delegation(dp
);
5534 nfs4_put_stid(&dp
->dl_stid
);
5540 end_offset(u64 start
, u64 len
)
5545 return end
>= start
? end
: NFS4_MAX_UINT64
;
5548 /* last octet in a range */
5550 last_byte_offset(u64 start
, u64 len
)
5556 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
5560 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
5561 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
5562 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
5563 * locking, this prevents us from being completely protocol-compliant. The
5564 * real solution to this problem is to start using unsigned file offsets in
5565 * the VFS, but this is a very deep change!
5568 nfs4_transform_lock_offset(struct file_lock
*lock
)
5570 if (lock
->fl_start
< 0)
5571 lock
->fl_start
= OFFSET_MAX
;
5572 if (lock
->fl_end
< 0)
5573 lock
->fl_end
= OFFSET_MAX
;
5577 nfsd4_fl_get_owner(fl_owner_t owner
)
5579 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5581 nfs4_get_stateowner(&lo
->lo_owner
);
5586 nfsd4_fl_put_owner(fl_owner_t owner
)
5588 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
5591 nfs4_put_stateowner(&lo
->lo_owner
);
5595 nfsd4_lm_notify(struct file_lock
*fl
)
5597 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)fl
->fl_owner
;
5598 struct net
*net
= lo
->lo_owner
.so_client
->net
;
5599 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5600 struct nfsd4_blocked_lock
*nbl
= container_of(fl
,
5601 struct nfsd4_blocked_lock
, nbl_lock
);
5604 /* An empty list means that something else is going to be using it */
5605 spin_lock(&nn
->blocked_locks_lock
);
5606 if (!list_empty(&nbl
->nbl_list
)) {
5607 list_del_init(&nbl
->nbl_list
);
5608 list_del_init(&nbl
->nbl_lru
);
5611 spin_unlock(&nn
->blocked_locks_lock
);
5614 nfsd4_run_cb(&nbl
->nbl_cb
);
5617 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
5618 .lm_notify
= nfsd4_lm_notify
,
5619 .lm_get_owner
= nfsd4_fl_get_owner
,
5620 .lm_put_owner
= nfsd4_fl_put_owner
,
5624 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
5626 struct nfs4_lockowner
*lo
;
5628 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
5629 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
5630 deny
->ld_owner
.data
= kmemdup(lo
->lo_owner
.so_owner
.data
,
5631 lo
->lo_owner
.so_owner
.len
, GFP_KERNEL
);
5632 if (!deny
->ld_owner
.data
)
5633 /* We just don't care that much */
5635 deny
->ld_owner
.len
= lo
->lo_owner
.so_owner
.len
;
5636 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
5639 deny
->ld_owner
.len
= 0;
5640 deny
->ld_owner
.data
= NULL
;
5641 deny
->ld_clientid
.cl_boot
= 0;
5642 deny
->ld_clientid
.cl_id
= 0;
5644 deny
->ld_start
= fl
->fl_start
;
5645 deny
->ld_length
= NFS4_MAX_UINT64
;
5646 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
5647 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
5648 deny
->ld_type
= NFS4_READ_LT
;
5649 if (fl
->fl_type
!= F_RDLCK
)
5650 deny
->ld_type
= NFS4_WRITE_LT
;
5653 static struct nfs4_lockowner
*
5654 find_lockowner_str_locked(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5656 unsigned int strhashval
= ownerstr_hashval(owner
);
5657 struct nfs4_stateowner
*so
;
5659 lockdep_assert_held(&clp
->cl_lock
);
5661 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[strhashval
],
5663 if (so
->so_is_open_owner
)
5665 if (same_owner_str(so
, owner
))
5666 return lockowner(nfs4_get_stateowner(so
));
5671 static struct nfs4_lockowner
*
5672 find_lockowner_str(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
5674 struct nfs4_lockowner
*lo
;
5676 spin_lock(&clp
->cl_lock
);
5677 lo
= find_lockowner_str_locked(clp
, owner
);
5678 spin_unlock(&clp
->cl_lock
);
5682 static void nfs4_unhash_lockowner(struct nfs4_stateowner
*sop
)
5684 unhash_lockowner_locked(lockowner(sop
));
5687 static void nfs4_free_lockowner(struct nfs4_stateowner
*sop
)
5689 struct nfs4_lockowner
*lo
= lockowner(sop
);
5691 kmem_cache_free(lockowner_slab
, lo
);
5694 static const struct nfs4_stateowner_operations lockowner_ops
= {
5695 .so_unhash
= nfs4_unhash_lockowner
,
5696 .so_free
= nfs4_free_lockowner
,
5700 * Alloc a lock owner structure.
5701 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
5704 * strhashval = ownerstr_hashval
5706 static struct nfs4_lockowner
*
5707 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
,
5708 struct nfs4_ol_stateid
*open_stp
,
5709 struct nfsd4_lock
*lock
)
5711 struct nfs4_lockowner
*lo
, *ret
;
5713 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
5716 INIT_LIST_HEAD(&lo
->lo_blocked
);
5717 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
5718 lo
->lo_owner
.so_is_open_owner
= 0;
5719 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
;
5720 lo
->lo_owner
.so_ops
= &lockowner_ops
;
5721 spin_lock(&clp
->cl_lock
);
5722 ret
= find_lockowner_str_locked(clp
, &lock
->lk_new_owner
);
5724 list_add(&lo
->lo_owner
.so_strhash
,
5725 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
5728 nfs4_free_stateowner(&lo
->lo_owner
);
5730 spin_unlock(&clp
->cl_lock
);
5735 init_lock_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_lockowner
*lo
,
5736 struct nfs4_file
*fp
, struct inode
*inode
,
5737 struct nfs4_ol_stateid
*open_stp
)
5739 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5741 lockdep_assert_held(&clp
->cl_lock
);
5743 atomic_inc(&stp
->st_stid
.sc_count
);
5744 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
5745 stp
->st_stateowner
= nfs4_get_stateowner(&lo
->lo_owner
);
5747 stp
->st_stid
.sc_file
= fp
;
5748 stp
->st_access_bmap
= 0;
5749 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
5750 stp
->st_openstp
= open_stp
;
5751 mutex_init(&stp
->st_mutex
);
5752 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
5753 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
5754 spin_lock(&fp
->fi_lock
);
5755 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
5756 spin_unlock(&fp
->fi_lock
);
5759 static struct nfs4_ol_stateid
*
5760 find_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fp
)
5762 struct nfs4_ol_stateid
*lst
;
5763 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
5765 lockdep_assert_held(&clp
->cl_lock
);
5767 list_for_each_entry(lst
, &lo
->lo_owner
.so_stateids
, st_perstateowner
) {
5768 if (lst
->st_stid
.sc_file
== fp
) {
5769 atomic_inc(&lst
->st_stid
.sc_count
);
5776 static struct nfs4_ol_stateid
*
5777 find_or_create_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fi
,
5778 struct inode
*inode
, struct nfs4_ol_stateid
*ost
,
5781 struct nfs4_stid
*ns
= NULL
;
5782 struct nfs4_ol_stateid
*lst
;
5783 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5784 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
5786 spin_lock(&clp
->cl_lock
);
5787 lst
= find_lock_stateid(lo
, fi
);
5789 spin_unlock(&clp
->cl_lock
);
5790 ns
= nfs4_alloc_stid(clp
, stateid_slab
, nfs4_free_lock_stateid
);
5794 spin_lock(&clp
->cl_lock
);
5795 lst
= find_lock_stateid(lo
, fi
);
5797 lst
= openlockstateid(ns
);
5798 init_lock_stateid(lst
, lo
, fi
, inode
, ost
);
5803 spin_unlock(&clp
->cl_lock
);
5810 check_lock_length(u64 offset
, u64 length
)
5812 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
5813 (length
> ~offset
)));
5816 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
5818 struct nfs4_file
*fp
= lock_stp
->st_stid
.sc_file
;
5820 lockdep_assert_held(&fp
->fi_lock
);
5822 if (test_access(access
, lock_stp
))
5824 __nfs4_file_get_access(fp
, access
);
5825 set_access(access
, lock_stp
);
5829 lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
,
5830 struct nfs4_ol_stateid
*ost
,
5831 struct nfsd4_lock
*lock
,
5832 struct nfs4_ol_stateid
**plst
, bool *new)
5835 struct nfs4_file
*fi
= ost
->st_stid
.sc_file
;
5836 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
5837 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
5838 struct inode
*inode
= d_inode(cstate
->current_fh
.fh_dentry
);
5839 struct nfs4_lockowner
*lo
;
5840 struct nfs4_ol_stateid
*lst
;
5841 unsigned int strhashval
;
5844 lo
= find_lockowner_str(cl
, &lock
->lk_new_owner
);
5846 strhashval
= ownerstr_hashval(&lock
->lk_new_owner
);
5847 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
5849 return nfserr_jukebox
;
5851 /* with an existing lockowner, seqids must be the same */
5852 status
= nfserr_bad_seqid
;
5853 if (!cstate
->minorversion
&&
5854 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
5859 lst
= find_or_create_lock_stateid(lo
, fi
, inode
, ost
, new);
5861 status
= nfserr_jukebox
;
5865 mutex_lock(&lst
->st_mutex
);
5867 /* See if it's still hashed to avoid race with FREE_STATEID */
5868 spin_lock(&cl
->cl_lock
);
5869 hashed
= !list_empty(&lst
->st_perfile
);
5870 spin_unlock(&cl
->cl_lock
);
5873 mutex_unlock(&lst
->st_mutex
);
5874 nfs4_put_stid(&lst
->st_stid
);
5880 nfs4_put_stateowner(&lo
->lo_owner
);
5888 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5889 union nfsd4_op_u
*u
)
5891 struct nfsd4_lock
*lock
= &u
->lock
;
5892 struct nfs4_openowner
*open_sop
= NULL
;
5893 struct nfs4_lockowner
*lock_sop
= NULL
;
5894 struct nfs4_ol_stateid
*lock_stp
= NULL
;
5895 struct nfs4_ol_stateid
*open_stp
= NULL
;
5896 struct nfs4_file
*fp
;
5897 struct file
*filp
= NULL
;
5898 struct nfsd4_blocked_lock
*nbl
= NULL
;
5899 struct file_lock
*file_lock
= NULL
;
5900 struct file_lock
*conflock
= NULL
;
5905 unsigned char fl_type
;
5906 unsigned int fl_flags
= FL_POSIX
;
5907 struct net
*net
= SVC_NET(rqstp
);
5908 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5910 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
5911 (long long) lock
->lk_offset
,
5912 (long long) lock
->lk_length
);
5914 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
5915 return nfserr_inval
;
5917 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
5918 S_IFREG
, NFSD_MAY_LOCK
))) {
5919 dprintk("NFSD: nfsd4_lock: permission denied!\n");
5923 if (lock
->lk_is_new
) {
5924 if (nfsd4_has_session(cstate
))
5925 /* See rfc 5661 18.10.3: given clientid is ignored: */
5926 memcpy(&lock
->lk_new_clientid
,
5927 &cstate
->session
->se_client
->cl_clientid
,
5928 sizeof(clientid_t
));
5930 status
= nfserr_stale_clientid
;
5931 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
5934 /* validate and update open stateid and open seqid */
5935 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
5936 lock
->lk_new_open_seqid
,
5937 &lock
->lk_new_open_stateid
,
5941 mutex_unlock(&open_stp
->st_mutex
);
5942 open_sop
= openowner(open_stp
->st_stateowner
);
5943 status
= nfserr_bad_stateid
;
5944 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
5945 &lock
->lk_new_clientid
))
5947 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
5950 status
= nfs4_preprocess_seqid_op(cstate
,
5951 lock
->lk_old_lock_seqid
,
5952 &lock
->lk_old_lock_stateid
,
5953 NFS4_LOCK_STID
, &lock_stp
, nn
);
5957 lock_sop
= lockowner(lock_stp
->st_stateowner
);
5959 lkflg
= setlkflg(lock
->lk_type
);
5960 status
= nfs4_check_openmode(lock_stp
, lkflg
);
5964 status
= nfserr_grace
;
5965 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
5967 status
= nfserr_no_grace
;
5968 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
5971 fp
= lock_stp
->st_stid
.sc_file
;
5972 switch (lock
->lk_type
) {
5974 if (nfsd4_has_session(cstate
))
5975 fl_flags
|= FL_SLEEP
;
5978 spin_lock(&fp
->fi_lock
);
5979 filp
= find_readable_file_locked(fp
);
5981 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
5982 spin_unlock(&fp
->fi_lock
);
5985 case NFS4_WRITEW_LT
:
5986 if (nfsd4_has_session(cstate
))
5987 fl_flags
|= FL_SLEEP
;
5990 spin_lock(&fp
->fi_lock
);
5991 filp
= find_writeable_file_locked(fp
);
5993 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
5994 spin_unlock(&fp
->fi_lock
);
5998 status
= nfserr_inval
;
6003 status
= nfserr_openmode
;
6007 nbl
= find_or_allocate_block(lock_sop
, &fp
->fi_fhandle
, nn
);
6009 dprintk("NFSD: %s: unable to allocate block!\n", __func__
);
6010 status
= nfserr_jukebox
;
6014 file_lock
= &nbl
->nbl_lock
;
6015 file_lock
->fl_type
= fl_type
;
6016 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lock_sop
->lo_owner
));
6017 file_lock
->fl_pid
= current
->tgid
;
6018 file_lock
->fl_file
= filp
;
6019 file_lock
->fl_flags
= fl_flags
;
6020 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
6021 file_lock
->fl_start
= lock
->lk_offset
;
6022 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
6023 nfs4_transform_lock_offset(file_lock
);
6025 conflock
= locks_alloc_lock();
6027 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6028 status
= nfserr_jukebox
;
6032 if (fl_flags
& FL_SLEEP
) {
6033 nbl
->nbl_time
= jiffies
;
6034 spin_lock(&nn
->blocked_locks_lock
);
6035 list_add_tail(&nbl
->nbl_list
, &lock_sop
->lo_blocked
);
6036 list_add_tail(&nbl
->nbl_lru
, &nn
->blocked_locks_lru
);
6037 spin_unlock(&nn
->blocked_locks_lock
);
6040 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, conflock
);
6042 case 0: /* success! */
6043 nfs4_inc_and_copy_stateid(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
);
6046 case FILE_LOCK_DEFERRED
:
6049 case -EAGAIN
: /* conflock holds conflicting lock */
6050 status
= nfserr_denied
;
6051 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6052 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
6055 status
= nfserr_deadlock
;
6058 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
6059 status
= nfserrno(err
);
6064 /* dequeue it if we queued it before */
6065 if (fl_flags
& FL_SLEEP
) {
6066 spin_lock(&nn
->blocked_locks_lock
);
6067 list_del_init(&nbl
->nbl_list
);
6068 list_del_init(&nbl
->nbl_lru
);
6069 spin_unlock(&nn
->blocked_locks_lock
);
6071 free_blocked_lock(nbl
);
6076 /* Bump seqid manually if the 4.0 replay owner is openowner */
6077 if (cstate
->replay_owner
&&
6078 cstate
->replay_owner
!= &lock_sop
->lo_owner
&&
6079 seqid_mutating_err(ntohl(status
)))
6080 lock_sop
->lo_owner
.so_seqid
++;
6082 mutex_unlock(&lock_stp
->st_mutex
);
6085 * If this is a new, never-before-used stateid, and we are
6086 * returning an error, then just go ahead and release it.
6089 release_lock_stateid(lock_stp
);
6091 nfs4_put_stid(&lock_stp
->st_stid
);
6094 nfs4_put_stid(&open_stp
->st_stid
);
6095 nfsd4_bump_seqid(cstate
, status
);
6097 locks_free_lock(conflock
);
6102 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6103 * so we do a temporary open here just to get an open file to pass to
6104 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6107 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
6110 __be32 err
= nfsd_open(rqstp
, fhp
, S_IFREG
, NFSD_MAY_READ
, &file
);
6112 err
= nfserrno(vfs_test_lock(file
, lock
));
6122 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6123 union nfsd4_op_u
*u
)
6125 struct nfsd4_lockt
*lockt
= &u
->lockt
;
6126 struct file_lock
*file_lock
= NULL
;
6127 struct nfs4_lockowner
*lo
= NULL
;
6129 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6131 if (locks_in_grace(SVC_NET(rqstp
)))
6132 return nfserr_grace
;
6134 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
6135 return nfserr_inval
;
6137 if (!nfsd4_has_session(cstate
)) {
6138 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
);
6143 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
6146 file_lock
= locks_alloc_lock();
6148 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6149 status
= nfserr_jukebox
;
6153 switch (lockt
->lt_type
) {
6156 file_lock
->fl_type
= F_RDLCK
;
6159 case NFS4_WRITEW_LT
:
6160 file_lock
->fl_type
= F_WRLCK
;
6163 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6164 status
= nfserr_inval
;
6168 lo
= find_lockowner_str(cstate
->clp
, &lockt
->lt_owner
);
6170 file_lock
->fl_owner
= (fl_owner_t
)lo
;
6171 file_lock
->fl_pid
= current
->tgid
;
6172 file_lock
->fl_flags
= FL_POSIX
;
6174 file_lock
->fl_start
= lockt
->lt_offset
;
6175 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
6177 nfs4_transform_lock_offset(file_lock
);
6179 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
6183 if (file_lock
->fl_type
!= F_UNLCK
) {
6184 status
= nfserr_denied
;
6185 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
6189 nfs4_put_stateowner(&lo
->lo_owner
);
6191 locks_free_lock(file_lock
);
6196 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6197 union nfsd4_op_u
*u
)
6199 struct nfsd4_locku
*locku
= &u
->locku
;
6200 struct nfs4_ol_stateid
*stp
;
6201 struct file
*filp
= NULL
;
6202 struct file_lock
*file_lock
= NULL
;
6205 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6207 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6208 (long long) locku
->lu_offset
,
6209 (long long) locku
->lu_length
);
6211 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
6212 return nfserr_inval
;
6214 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
6215 &locku
->lu_stateid
, NFS4_LOCK_STID
,
6219 filp
= find_any_file(stp
->st_stid
.sc_file
);
6221 status
= nfserr_lock_range
;
6224 file_lock
= locks_alloc_lock();
6226 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6227 status
= nfserr_jukebox
;
6231 file_lock
->fl_type
= F_UNLCK
;
6232 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(stp
->st_stateowner
));
6233 file_lock
->fl_pid
= current
->tgid
;
6234 file_lock
->fl_file
= filp
;
6235 file_lock
->fl_flags
= FL_POSIX
;
6236 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
6237 file_lock
->fl_start
= locku
->lu_offset
;
6239 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
6241 nfs4_transform_lock_offset(file_lock
);
6243 err
= vfs_lock_file(filp
, F_SETLK
, file_lock
, NULL
);
6245 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6248 nfs4_inc_and_copy_stateid(&locku
->lu_stateid
, &stp
->st_stid
);
6252 mutex_unlock(&stp
->st_mutex
);
6253 nfs4_put_stid(&stp
->st_stid
);
6255 nfsd4_bump_seqid(cstate
, status
);
6257 locks_free_lock(file_lock
);
6261 status
= nfserrno(err
);
6267 * true: locks held by lockowner
6268 * false: no locks held by lockowner
6271 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
6273 struct file_lock
*fl
;
6275 struct file
*filp
= find_any_file(fp
);
6276 struct inode
*inode
;
6277 struct file_lock_context
*flctx
;
6280 /* Any valid lock stateid should have some sort of access */
6285 inode
= file_inode(filp
);
6286 flctx
= inode
->i_flctx
;
6288 if (flctx
&& !list_empty_careful(&flctx
->flc_posix
)) {
6289 spin_lock(&flctx
->flc_lock
);
6290 list_for_each_entry(fl
, &flctx
->flc_posix
, fl_list
) {
6291 if (fl
->fl_owner
== (fl_owner_t
)lowner
) {
6296 spin_unlock(&flctx
->flc_lock
);
6303 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
6304 struct nfsd4_compound_state
*cstate
,
6305 union nfsd4_op_u
*u
)
6307 struct nfsd4_release_lockowner
*rlockowner
= &u
->release_lockowner
;
6308 clientid_t
*clid
= &rlockowner
->rl_clientid
;
6309 struct nfs4_stateowner
*sop
;
6310 struct nfs4_lockowner
*lo
= NULL
;
6311 struct nfs4_ol_stateid
*stp
;
6312 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
6313 unsigned int hashval
= ownerstr_hashval(owner
);
6315 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6316 struct nfs4_client
*clp
;
6317 LIST_HEAD (reaplist
);
6319 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
6320 clid
->cl_boot
, clid
->cl_id
);
6322 status
= lookup_clientid(clid
, cstate
, nn
);
6327 /* Find the matching lock stateowner */
6328 spin_lock(&clp
->cl_lock
);
6329 list_for_each_entry(sop
, &clp
->cl_ownerstr_hashtbl
[hashval
],
6332 if (sop
->so_is_open_owner
|| !same_owner_str(sop
, owner
))
6335 /* see if there are still any locks associated with it */
6336 lo
= lockowner(sop
);
6337 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
6338 if (check_for_locks(stp
->st_stid
.sc_file
, lo
)) {
6339 status
= nfserr_locks_held
;
6340 spin_unlock(&clp
->cl_lock
);
6345 nfs4_get_stateowner(sop
);
6349 spin_unlock(&clp
->cl_lock
);
6353 unhash_lockowner_locked(lo
);
6354 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
6355 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
6356 struct nfs4_ol_stateid
,
6358 WARN_ON(!unhash_lock_stateid(stp
));
6359 put_ol_stateid_locked(stp
, &reaplist
);
6361 spin_unlock(&clp
->cl_lock
);
6362 free_ol_stateid_reaplist(&reaplist
);
6363 remove_blocked_locks(lo
);
6364 nfs4_put_stateowner(&lo
->lo_owner
);
6369 static inline struct nfs4_client_reclaim
*
6372 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
6376 nfs4_has_reclaimed_state(const char *name
, struct nfsd_net
*nn
)
6378 struct nfs4_client_reclaim
*crp
;
6380 crp
= nfsd4_find_reclaim_client(name
, nn
);
6381 return (crp
&& crp
->cr_clp
);
6385 * failure => all reset bets are off, nfserr_no_grace...
6387 struct nfs4_client_reclaim
*
6388 nfs4_client_to_reclaim(const char *name
, struct nfsd_net
*nn
)
6390 unsigned int strhashval
;
6391 struct nfs4_client_reclaim
*crp
;
6393 dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", HEXDIR_LEN
, name
);
6394 crp
= alloc_reclaim();
6396 strhashval
= clientstr_hashval(name
);
6397 INIT_LIST_HEAD(&crp
->cr_strhash
);
6398 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
6399 memcpy(crp
->cr_recdir
, name
, HEXDIR_LEN
);
6401 nn
->reclaim_str_hashtbl_size
++;
6407 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
6409 list_del(&crp
->cr_strhash
);
6411 nn
->reclaim_str_hashtbl_size
--;
6415 nfs4_release_reclaim(struct nfsd_net
*nn
)
6417 struct nfs4_client_reclaim
*crp
= NULL
;
6420 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
6421 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
6422 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
6423 struct nfs4_client_reclaim
, cr_strhash
);
6424 nfs4_remove_reclaim_record(crp
, nn
);
6427 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
6431 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
6432 struct nfs4_client_reclaim
*
6433 nfsd4_find_reclaim_client(const char *recdir
, struct nfsd_net
*nn
)
6435 unsigned int strhashval
;
6436 struct nfs4_client_reclaim
*crp
= NULL
;
6438 dprintk("NFSD: nfs4_find_reclaim_client for recdir %s\n", recdir
);
6440 strhashval
= clientstr_hashval(recdir
);
6441 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
6442 if (same_name(crp
->cr_recdir
, recdir
)) {
6450 * Called from OPEN. Look for clientid in reclaim list.
6453 nfs4_check_open_reclaim(clientid_t
*clid
,
6454 struct nfsd4_compound_state
*cstate
,
6455 struct nfsd_net
*nn
)
6459 /* find clientid in conf_id_hashtbl */
6460 status
= lookup_clientid(clid
, cstate
, nn
);
6462 return nfserr_reclaim_bad
;
6464 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
, &cstate
->clp
->cl_flags
))
6465 return nfserr_no_grace
;
6467 if (nfsd4_client_record_check(cstate
->clp
))
6468 return nfserr_reclaim_bad
;
6473 #ifdef CONFIG_NFSD_FAULT_INJECTION
6475 put_client(struct nfs4_client
*clp
)
6477 atomic_dec(&clp
->cl_refcount
);
6480 static struct nfs4_client
*
6481 nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
6483 struct nfs4_client
*clp
;
6484 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6487 if (!nfsd_netns_ready(nn
))
6490 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6491 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
6498 nfsd_inject_print_clients(void)
6500 struct nfs4_client
*clp
;
6502 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6504 char buf
[INET6_ADDRSTRLEN
];
6506 if (!nfsd_netns_ready(nn
))
6509 spin_lock(&nn
->client_lock
);
6510 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6511 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
6512 pr_info("NFS Client: %s\n", buf
);
6515 spin_unlock(&nn
->client_lock
);
6521 nfsd_inject_forget_client(struct sockaddr_storage
*addr
, size_t addr_size
)
6524 struct nfs4_client
*clp
;
6525 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6528 if (!nfsd_netns_ready(nn
))
6531 spin_lock(&nn
->client_lock
);
6532 clp
= nfsd_find_client(addr
, addr_size
);
6534 if (mark_client_expired_locked(clp
) == nfs_ok
)
6539 spin_unlock(&nn
->client_lock
);
6548 nfsd_inject_forget_clients(u64 max
)
6551 struct nfs4_client
*clp
, *next
;
6552 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6554 LIST_HEAD(reaplist
);
6556 if (!nfsd_netns_ready(nn
))
6559 spin_lock(&nn
->client_lock
);
6560 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
6561 if (mark_client_expired_locked(clp
) == nfs_ok
) {
6562 list_add(&clp
->cl_lru
, &reaplist
);
6563 if (max
!= 0 && ++count
>= max
)
6567 spin_unlock(&nn
->client_lock
);
6569 list_for_each_entry_safe(clp
, next
, &reaplist
, cl_lru
)
6575 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
6578 char buf
[INET6_ADDRSTRLEN
];
6579 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
6580 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
6584 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid
*lst
,
6585 struct list_head
*collect
)
6587 struct nfs4_client
*clp
= lst
->st_stid
.sc_client
;
6588 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6594 lockdep_assert_held(&nn
->client_lock
);
6595 atomic_inc(&clp
->cl_refcount
);
6596 list_add(&lst
->st_locks
, collect
);
6599 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
6600 struct list_head
*collect
,
6601 bool (*func
)(struct nfs4_ol_stateid
*))
6603 struct nfs4_openowner
*oop
;
6604 struct nfs4_ol_stateid
*stp
, *st_next
;
6605 struct nfs4_ol_stateid
*lst
, *lst_next
;
6608 spin_lock(&clp
->cl_lock
);
6609 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
6610 list_for_each_entry_safe(stp
, st_next
,
6611 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
6612 list_for_each_entry_safe(lst
, lst_next
,
6613 &stp
->st_locks
, st_locks
) {
6616 nfsd_inject_add_lock_to_list(lst
,
6621 * Despite the fact that these functions deal
6622 * with 64-bit integers for "count", we must
6623 * ensure that it doesn't blow up the
6624 * clp->cl_refcount. Throw a warning if we
6625 * start to approach INT_MAX here.
6627 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6634 spin_unlock(&clp
->cl_lock
);
6640 nfsd_collect_client_locks(struct nfs4_client
*clp
, struct list_head
*collect
,
6643 return nfsd_foreach_client_lock(clp
, max
, collect
, unhash_lock_stateid
);
6647 nfsd_print_client_locks(struct nfs4_client
*clp
)
6649 u64 count
= nfsd_foreach_client_lock(clp
, 0, NULL
, NULL
);
6650 nfsd_print_count(clp
, count
, "locked files");
6655 nfsd_inject_print_locks(void)
6657 struct nfs4_client
*clp
;
6659 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6662 if (!nfsd_netns_ready(nn
))
6665 spin_lock(&nn
->client_lock
);
6666 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6667 count
+= nfsd_print_client_locks(clp
);
6668 spin_unlock(&nn
->client_lock
);
6674 nfsd_reap_locks(struct list_head
*reaplist
)
6676 struct nfs4_client
*clp
;
6677 struct nfs4_ol_stateid
*stp
, *next
;
6679 list_for_each_entry_safe(stp
, next
, reaplist
, st_locks
) {
6680 list_del_init(&stp
->st_locks
);
6681 clp
= stp
->st_stid
.sc_client
;
6682 nfs4_put_stid(&stp
->st_stid
);
6688 nfsd_inject_forget_client_locks(struct sockaddr_storage
*addr
, size_t addr_size
)
6690 unsigned int count
= 0;
6691 struct nfs4_client
*clp
;
6692 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6694 LIST_HEAD(reaplist
);
6696 if (!nfsd_netns_ready(nn
))
6699 spin_lock(&nn
->client_lock
);
6700 clp
= nfsd_find_client(addr
, addr_size
);
6702 count
= nfsd_collect_client_locks(clp
, &reaplist
, 0);
6703 spin_unlock(&nn
->client_lock
);
6704 nfsd_reap_locks(&reaplist
);
6709 nfsd_inject_forget_locks(u64 max
)
6712 struct nfs4_client
*clp
;
6713 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6715 LIST_HEAD(reaplist
);
6717 if (!nfsd_netns_ready(nn
))
6720 spin_lock(&nn
->client_lock
);
6721 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6722 count
+= nfsd_collect_client_locks(clp
, &reaplist
, max
- count
);
6723 if (max
!= 0 && count
>= max
)
6726 spin_unlock(&nn
->client_lock
);
6727 nfsd_reap_locks(&reaplist
);
6732 nfsd_foreach_client_openowner(struct nfs4_client
*clp
, u64 max
,
6733 struct list_head
*collect
,
6734 void (*func
)(struct nfs4_openowner
*))
6736 struct nfs4_openowner
*oop
, *next
;
6737 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6741 lockdep_assert_held(&nn
->client_lock
);
6743 spin_lock(&clp
->cl_lock
);
6744 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
6748 atomic_inc(&clp
->cl_refcount
);
6749 list_add(&oop
->oo_perclient
, collect
);
6754 * Despite the fact that these functions deal with
6755 * 64-bit integers for "count", we must ensure that
6756 * it doesn't blow up the clp->cl_refcount. Throw a
6757 * warning if we start to approach INT_MAX here.
6759 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6763 spin_unlock(&clp
->cl_lock
);
6769 nfsd_print_client_openowners(struct nfs4_client
*clp
)
6771 u64 count
= nfsd_foreach_client_openowner(clp
, 0, NULL
, NULL
);
6773 nfsd_print_count(clp
, count
, "openowners");
6778 nfsd_collect_client_openowners(struct nfs4_client
*clp
,
6779 struct list_head
*collect
, u64 max
)
6781 return nfsd_foreach_client_openowner(clp
, max
, collect
,
6782 unhash_openowner_locked
);
6786 nfsd_inject_print_openowners(void)
6788 struct nfs4_client
*clp
;
6790 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6793 if (!nfsd_netns_ready(nn
))
6796 spin_lock(&nn
->client_lock
);
6797 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6798 count
+= nfsd_print_client_openowners(clp
);
6799 spin_unlock(&nn
->client_lock
);
6805 nfsd_reap_openowners(struct list_head
*reaplist
)
6807 struct nfs4_client
*clp
;
6808 struct nfs4_openowner
*oop
, *next
;
6810 list_for_each_entry_safe(oop
, next
, reaplist
, oo_perclient
) {
6811 list_del_init(&oop
->oo_perclient
);
6812 clp
= oop
->oo_owner
.so_client
;
6813 release_openowner(oop
);
6819 nfsd_inject_forget_client_openowners(struct sockaddr_storage
*addr
,
6822 unsigned int count
= 0;
6823 struct nfs4_client
*clp
;
6824 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6826 LIST_HEAD(reaplist
);
6828 if (!nfsd_netns_ready(nn
))
6831 spin_lock(&nn
->client_lock
);
6832 clp
= nfsd_find_client(addr
, addr_size
);
6834 count
= nfsd_collect_client_openowners(clp
, &reaplist
, 0);
6835 spin_unlock(&nn
->client_lock
);
6836 nfsd_reap_openowners(&reaplist
);
6841 nfsd_inject_forget_openowners(u64 max
)
6844 struct nfs4_client
*clp
;
6845 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6847 LIST_HEAD(reaplist
);
6849 if (!nfsd_netns_ready(nn
))
6852 spin_lock(&nn
->client_lock
);
6853 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6854 count
+= nfsd_collect_client_openowners(clp
, &reaplist
,
6856 if (max
!= 0 && count
>= max
)
6859 spin_unlock(&nn
->client_lock
);
6860 nfsd_reap_openowners(&reaplist
);
6864 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
6865 struct list_head
*victims
)
6867 struct nfs4_delegation
*dp
, *next
;
6868 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6872 lockdep_assert_held(&nn
->client_lock
);
6874 spin_lock(&state_lock
);
6875 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
6878 * It's not safe to mess with delegations that have a
6879 * non-zero dl_time. They might have already been broken
6880 * and could be processed by the laundromat outside of
6881 * the state_lock. Just leave them be.
6883 if (dp
->dl_time
!= 0)
6886 atomic_inc(&clp
->cl_refcount
);
6887 WARN_ON(!unhash_delegation_locked(dp
));
6888 list_add(&dp
->dl_recall_lru
, victims
);
6892 * Despite the fact that these functions deal with
6893 * 64-bit integers for "count", we must ensure that
6894 * it doesn't blow up the clp->cl_refcount. Throw a
6895 * warning if we start to approach INT_MAX here.
6897 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
6901 spin_unlock(&state_lock
);
6906 nfsd_print_client_delegations(struct nfs4_client
*clp
)
6908 u64 count
= nfsd_find_all_delegations(clp
, 0, NULL
);
6910 nfsd_print_count(clp
, count
, "delegations");
6915 nfsd_inject_print_delegations(void)
6917 struct nfs4_client
*clp
;
6919 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6922 if (!nfsd_netns_ready(nn
))
6925 spin_lock(&nn
->client_lock
);
6926 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
6927 count
+= nfsd_print_client_delegations(clp
);
6928 spin_unlock(&nn
->client_lock
);
6934 nfsd_forget_delegations(struct list_head
*reaplist
)
6936 struct nfs4_client
*clp
;
6937 struct nfs4_delegation
*dp
, *next
;
6939 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
6940 list_del_init(&dp
->dl_recall_lru
);
6941 clp
= dp
->dl_stid
.sc_client
;
6942 revoke_delegation(dp
);
6948 nfsd_inject_forget_client_delegations(struct sockaddr_storage
*addr
,
6952 struct nfs4_client
*clp
;
6953 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6955 LIST_HEAD(reaplist
);
6957 if (!nfsd_netns_ready(nn
))
6960 spin_lock(&nn
->client_lock
);
6961 clp
= nfsd_find_client(addr
, addr_size
);
6963 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
6964 spin_unlock(&nn
->client_lock
);
6966 nfsd_forget_delegations(&reaplist
);
6971 nfsd_inject_forget_delegations(u64 max
)
6974 struct nfs4_client
*clp
;
6975 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
6977 LIST_HEAD(reaplist
);
6979 if (!nfsd_netns_ready(nn
))
6982 spin_lock(&nn
->client_lock
);
6983 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
6984 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
6985 if (max
!= 0 && count
>= max
)
6988 spin_unlock(&nn
->client_lock
);
6989 nfsd_forget_delegations(&reaplist
);
6994 nfsd_recall_delegations(struct list_head
*reaplist
)
6996 struct nfs4_client
*clp
;
6997 struct nfs4_delegation
*dp
, *next
;
6999 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
7000 list_del_init(&dp
->dl_recall_lru
);
7001 clp
= dp
->dl_stid
.sc_client
;
7003 * We skipped all entries that had a zero dl_time before,
7004 * so we can now reset the dl_time back to 0. If a delegation
7005 * break comes in now, then it won't make any difference since
7006 * we're recalling it either way.
7008 spin_lock(&state_lock
);
7010 spin_unlock(&state_lock
);
7011 nfsd_break_one_deleg(dp
);
7017 nfsd_inject_recall_client_delegations(struct sockaddr_storage
*addr
,
7021 struct nfs4_client
*clp
;
7022 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7024 LIST_HEAD(reaplist
);
7026 if (!nfsd_netns_ready(nn
))
7029 spin_lock(&nn
->client_lock
);
7030 clp
= nfsd_find_client(addr
, addr_size
);
7032 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
7033 spin_unlock(&nn
->client_lock
);
7035 nfsd_recall_delegations(&reaplist
);
7040 nfsd_inject_recall_delegations(u64 max
)
7043 struct nfs4_client
*clp
, *next
;
7044 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7046 LIST_HEAD(reaplist
);
7048 if (!nfsd_netns_ready(nn
))
7051 spin_lock(&nn
->client_lock
);
7052 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
7053 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
7054 if (max
!= 0 && ++count
>= max
)
7057 spin_unlock(&nn
->client_lock
);
7058 nfsd_recall_delegations(&reaplist
);
7061 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7064 * Since the lifetime of a delegation isn't limited to that of an open, a
7065 * client may quite reasonably hang on to a delegation as long as it has
7066 * the inode cached. This becomes an obvious problem the first time a
7067 * client's inode cache approaches the size of the server's total memory.
7069 * For now we avoid this problem by imposing a hard limit on the number
7070 * of delegations, which varies according to the server's memory size.
7073 set_max_delegations(void)
7076 * Allow at most 4 delegations per megabyte of RAM. Quick
7077 * estimates suggest that in the worst case (where every delegation
7078 * is for a different inode), a delegation could take about 1.5K,
7079 * giving a worst case usage of about 6% of memory.
7081 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
7084 static int nfs4_state_create_net(struct net
*net
)
7086 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7089 nn
->conf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
7090 CLIENT_HASH_SIZE
, GFP_KERNEL
);
7091 if (!nn
->conf_id_hashtbl
)
7093 nn
->unconf_id_hashtbl
= kmalloc(sizeof(struct list_head
) *
7094 CLIENT_HASH_SIZE
, GFP_KERNEL
);
7095 if (!nn
->unconf_id_hashtbl
)
7097 nn
->sessionid_hashtbl
= kmalloc(sizeof(struct list_head
) *
7098 SESSION_HASH_SIZE
, GFP_KERNEL
);
7099 if (!nn
->sessionid_hashtbl
)
7102 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7103 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
7104 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
7106 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
7107 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
7108 nn
->conf_name_tree
= RB_ROOT
;
7109 nn
->unconf_name_tree
= RB_ROOT
;
7110 nn
->boot_time
= get_seconds();
7111 nn
->grace_ended
= false;
7112 nn
->nfsd4_manager
.block_opens
= true;
7113 INIT_LIST_HEAD(&nn
->nfsd4_manager
.list
);
7114 INIT_LIST_HEAD(&nn
->client_lru
);
7115 INIT_LIST_HEAD(&nn
->close_lru
);
7116 INIT_LIST_HEAD(&nn
->del_recall_lru
);
7117 spin_lock_init(&nn
->client_lock
);
7119 spin_lock_init(&nn
->blocked_locks_lock
);
7120 INIT_LIST_HEAD(&nn
->blocked_locks_lru
);
7122 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
7128 kfree(nn
->unconf_id_hashtbl
);
7130 kfree(nn
->conf_id_hashtbl
);
7136 nfs4_state_destroy_net(struct net
*net
)
7139 struct nfs4_client
*clp
= NULL
;
7140 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7142 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7143 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
7144 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
7145 destroy_client(clp
);
7149 WARN_ON(!list_empty(&nn
->blocked_locks_lru
));
7151 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7152 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
7153 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
7154 destroy_client(clp
);
7158 kfree(nn
->sessionid_hashtbl
);
7159 kfree(nn
->unconf_id_hashtbl
);
7160 kfree(nn
->conf_id_hashtbl
);
7165 nfs4_state_start_net(struct net
*net
)
7167 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7170 ret
= nfs4_state_create_net(net
);
7173 locks_start_grace(net
, &nn
->nfsd4_manager
);
7174 nfsd4_client_tracking_init(net
);
7175 printk(KERN_INFO
"NFSD: starting %ld-second grace period (net %p)\n",
7176 nn
->nfsd4_grace
, net
);
7177 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
7181 /* initialization to perform when the nfsd service is started: */
7184 nfs4_state_start(void)
7188 ret
= set_callback_cred();
7192 laundry_wq
= alloc_workqueue("%s", WQ_UNBOUND
, 0, "nfsd4");
7193 if (laundry_wq
== NULL
) {
7195 goto out_cleanup_cred
;
7197 ret
= nfsd4_create_callback_queue();
7199 goto out_free_laundry
;
7201 set_max_delegations();
7205 destroy_workqueue(laundry_wq
);
7207 cleanup_callback_cred();
7212 nfs4_state_shutdown_net(struct net
*net
)
7214 struct nfs4_delegation
*dp
= NULL
;
7215 struct list_head
*pos
, *next
, reaplist
;
7216 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7218 cancel_delayed_work_sync(&nn
->laundromat_work
);
7219 locks_end_grace(&nn
->nfsd4_manager
);
7221 INIT_LIST_HEAD(&reaplist
);
7222 spin_lock(&state_lock
);
7223 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
7224 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
7225 WARN_ON(!unhash_delegation_locked(dp
));
7226 list_add(&dp
->dl_recall_lru
, &reaplist
);
7228 spin_unlock(&state_lock
);
7229 list_for_each_safe(pos
, next
, &reaplist
) {
7230 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
7231 list_del_init(&dp
->dl_recall_lru
);
7232 put_clnt_odstate(dp
->dl_clnt_odstate
);
7233 nfs4_put_deleg_lease(dp
->dl_stid
.sc_file
);
7234 nfs4_put_stid(&dp
->dl_stid
);
7237 nfsd4_client_tracking_exit(net
);
7238 nfs4_state_destroy_net(net
);
7242 nfs4_state_shutdown(void)
7244 destroy_workqueue(laundry_wq
);
7245 nfsd4_destroy_callback_queue();
7246 cleanup_callback_cred();
7250 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
7252 if (HAS_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
) && CURRENT_STATEID(stateid
))
7253 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
7257 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
7259 if (cstate
->minorversion
) {
7260 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
7261 SET_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
7266 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
7268 CLEAR_STATE_ID(cstate
, CURRENT_STATE_ID_FLAG
);
7272 * functions to set current state id
7275 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
,
7276 union nfsd4_op_u
*u
)
7278 put_stateid(cstate
, &u
->open_downgrade
.od_stateid
);
7282 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
,
7283 union nfsd4_op_u
*u
)
7285 put_stateid(cstate
, &u
->open
.op_stateid
);
7289 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
,
7290 union nfsd4_op_u
*u
)
7292 put_stateid(cstate
, &u
->close
.cl_stateid
);
7296 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
,
7297 union nfsd4_op_u
*u
)
7299 put_stateid(cstate
, &u
->lock
.lk_resp_stateid
);
7303 * functions to consume current state id
7307 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
,
7308 union nfsd4_op_u
*u
)
7310 get_stateid(cstate
, &u
->open_downgrade
.od_stateid
);
7314 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
,
7315 union nfsd4_op_u
*u
)
7317 get_stateid(cstate
, &u
->delegreturn
.dr_stateid
);
7321 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
,
7322 union nfsd4_op_u
*u
)
7324 get_stateid(cstate
, &u
->free_stateid
.fr_stateid
);
7328 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
,
7329 union nfsd4_op_u
*u
)
7331 get_stateid(cstate
, &u
->setattr
.sa_stateid
);
7335 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
,
7336 union nfsd4_op_u
*u
)
7338 get_stateid(cstate
, &u
->close
.cl_stateid
);
7342 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
,
7343 union nfsd4_op_u
*u
)
7345 get_stateid(cstate
, &u
->locku
.lu_stateid
);
7349 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
,
7350 union nfsd4_op_u
*u
)
7352 get_stateid(cstate
, &u
->read
.rd_stateid
);
7356 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
,
7357 union nfsd4_op_u
*u
)
7359 get_stateid(cstate
, &u
->write
.wr_stateid
);