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>
45 #include <linux/string_helpers.h>
49 #include "current_stateid.h"
53 #include "filecache.h"
56 #define NFSDDBG_FACILITY NFSDDBG_PROC
58 #define all_ones {{~0,~0},~0}
59 static const stateid_t one_stateid
= {
61 .si_opaque
= all_ones
,
63 static const stateid_t zero_stateid
= {
66 static const stateid_t currentstateid
= {
69 static const stateid_t close_stateid
= {
70 .si_generation
= 0xffffffffU
,
73 static u64 current_sessionid
= 1;
75 #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t)))
76 #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t)))
77 #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t)))
78 #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t)))
80 /* forward declarations */
81 static bool check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
);
82 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
);
83 void nfsd4_end_grace(struct nfsd_net
*nn
);
84 static void _free_cpntf_state_locked(struct nfsd_net
*nn
, struct nfs4_cpntf_state
*cps
);
89 * Currently used for the del_recall_lru and file hash table. In an
90 * effort to decrease the scope of the client_mutex, this spinlock may
91 * eventually cover more:
93 static DEFINE_SPINLOCK(state_lock
);
95 enum nfsd4_st_mutex_lock_subclass
{
96 OPEN_STATEID_MUTEX
= 0,
97 LOCK_STATEID_MUTEX
= 1,
101 * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for
102 * the refcount on the open stateid to drop.
104 static DECLARE_WAIT_QUEUE_HEAD(close_wq
);
107 * A waitqueue where a writer to clients/#/ctl destroying a client can
108 * wait for cl_rpc_users to drop to 0 and then for the client to be
111 static DECLARE_WAIT_QUEUE_HEAD(expiry_wq
);
113 static struct kmem_cache
*client_slab
;
114 static struct kmem_cache
*openowner_slab
;
115 static struct kmem_cache
*lockowner_slab
;
116 static struct kmem_cache
*file_slab
;
117 static struct kmem_cache
*stateid_slab
;
118 static struct kmem_cache
*deleg_slab
;
119 static struct kmem_cache
*odstate_slab
;
121 static void free_session(struct nfsd4_session
*);
123 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops
;
124 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops
;
126 static bool is_session_dead(struct nfsd4_session
*ses
)
128 return ses
->se_flags
& NFS4_SESSION_DEAD
;
131 static __be32
mark_session_dead_locked(struct nfsd4_session
*ses
, int ref_held_by_me
)
133 if (atomic_read(&ses
->se_ref
) > ref_held_by_me
)
134 return nfserr_jukebox
;
135 ses
->se_flags
|= NFS4_SESSION_DEAD
;
139 static bool is_client_expired(struct nfs4_client
*clp
)
141 return clp
->cl_time
== 0;
144 static __be32
get_client_locked(struct nfs4_client
*clp
)
146 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
148 lockdep_assert_held(&nn
->client_lock
);
150 if (is_client_expired(clp
))
151 return nfserr_expired
;
152 atomic_inc(&clp
->cl_rpc_users
);
156 /* must be called under the client_lock */
158 renew_client_locked(struct nfs4_client
*clp
)
160 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
162 if (is_client_expired(clp
)) {
164 printk("%s: client (clientid %08x/%08x) already expired\n",
166 clp
->cl_clientid
.cl_boot
,
167 clp
->cl_clientid
.cl_id
);
171 list_move_tail(&clp
->cl_lru
, &nn
->client_lru
);
172 clp
->cl_time
= ktime_get_boottime_seconds();
175 static void put_client_renew_locked(struct nfs4_client
*clp
)
177 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
179 lockdep_assert_held(&nn
->client_lock
);
181 if (!atomic_dec_and_test(&clp
->cl_rpc_users
))
183 if (!is_client_expired(clp
))
184 renew_client_locked(clp
);
186 wake_up_all(&expiry_wq
);
189 static void put_client_renew(struct nfs4_client
*clp
)
191 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
193 if (!atomic_dec_and_lock(&clp
->cl_rpc_users
, &nn
->client_lock
))
195 if (!is_client_expired(clp
))
196 renew_client_locked(clp
);
198 wake_up_all(&expiry_wq
);
199 spin_unlock(&nn
->client_lock
);
202 static __be32
nfsd4_get_session_locked(struct nfsd4_session
*ses
)
206 if (is_session_dead(ses
))
207 return nfserr_badsession
;
208 status
= get_client_locked(ses
->se_client
);
211 atomic_inc(&ses
->se_ref
);
215 static void nfsd4_put_session_locked(struct nfsd4_session
*ses
)
217 struct nfs4_client
*clp
= ses
->se_client
;
218 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
220 lockdep_assert_held(&nn
->client_lock
);
222 if (atomic_dec_and_test(&ses
->se_ref
) && is_session_dead(ses
))
224 put_client_renew_locked(clp
);
227 static void nfsd4_put_session(struct nfsd4_session
*ses
)
229 struct nfs4_client
*clp
= ses
->se_client
;
230 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
232 spin_lock(&nn
->client_lock
);
233 nfsd4_put_session_locked(ses
);
234 spin_unlock(&nn
->client_lock
);
237 static struct nfsd4_blocked_lock
*
238 find_blocked_lock(struct nfs4_lockowner
*lo
, struct knfsd_fh
*fh
,
241 struct nfsd4_blocked_lock
*cur
, *found
= NULL
;
243 spin_lock(&nn
->blocked_locks_lock
);
244 list_for_each_entry(cur
, &lo
->lo_blocked
, nbl_list
) {
245 if (fh_match(fh
, &cur
->nbl_fh
)) {
246 list_del_init(&cur
->nbl_list
);
247 list_del_init(&cur
->nbl_lru
);
252 spin_unlock(&nn
->blocked_locks_lock
);
254 locks_delete_block(&found
->nbl_lock
);
258 static struct nfsd4_blocked_lock
*
259 find_or_allocate_block(struct nfs4_lockowner
*lo
, struct knfsd_fh
*fh
,
262 struct nfsd4_blocked_lock
*nbl
;
264 nbl
= find_blocked_lock(lo
, fh
, nn
);
266 nbl
= kmalloc(sizeof(*nbl
), GFP_KERNEL
);
268 INIT_LIST_HEAD(&nbl
->nbl_list
);
269 INIT_LIST_HEAD(&nbl
->nbl_lru
);
270 fh_copy_shallow(&nbl
->nbl_fh
, fh
);
271 locks_init_lock(&nbl
->nbl_lock
);
272 nfsd4_init_cb(&nbl
->nbl_cb
, lo
->lo_owner
.so_client
,
273 &nfsd4_cb_notify_lock_ops
,
274 NFSPROC4_CLNT_CB_NOTIFY_LOCK
);
281 free_blocked_lock(struct nfsd4_blocked_lock
*nbl
)
283 locks_delete_block(&nbl
->nbl_lock
);
284 locks_release_private(&nbl
->nbl_lock
);
289 remove_blocked_locks(struct nfs4_lockowner
*lo
)
291 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
292 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
293 struct nfsd4_blocked_lock
*nbl
;
296 /* Dequeue all blocked locks */
297 spin_lock(&nn
->blocked_locks_lock
);
298 while (!list_empty(&lo
->lo_blocked
)) {
299 nbl
= list_first_entry(&lo
->lo_blocked
,
300 struct nfsd4_blocked_lock
,
302 list_del_init(&nbl
->nbl_list
);
303 list_move(&nbl
->nbl_lru
, &reaplist
);
305 spin_unlock(&nn
->blocked_locks_lock
);
308 while (!list_empty(&reaplist
)) {
309 nbl
= list_first_entry(&reaplist
, struct nfsd4_blocked_lock
,
311 list_del_init(&nbl
->nbl_lru
);
312 free_blocked_lock(nbl
);
317 nfsd4_cb_notify_lock_prepare(struct nfsd4_callback
*cb
)
319 struct nfsd4_blocked_lock
*nbl
= container_of(cb
,
320 struct nfsd4_blocked_lock
, nbl_cb
);
321 locks_delete_block(&nbl
->nbl_lock
);
325 nfsd4_cb_notify_lock_done(struct nfsd4_callback
*cb
, struct rpc_task
*task
)
328 * Since this is just an optimization, we don't try very hard if it
329 * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and
330 * just quit trying on anything else.
332 switch (task
->tk_status
) {
334 rpc_delay(task
, 1 * HZ
);
342 nfsd4_cb_notify_lock_release(struct nfsd4_callback
*cb
)
344 struct nfsd4_blocked_lock
*nbl
= container_of(cb
,
345 struct nfsd4_blocked_lock
, nbl_cb
);
347 free_blocked_lock(nbl
);
350 static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops
= {
351 .prepare
= nfsd4_cb_notify_lock_prepare
,
352 .done
= nfsd4_cb_notify_lock_done
,
353 .release
= nfsd4_cb_notify_lock_release
,
356 static inline struct nfs4_stateowner
*
357 nfs4_get_stateowner(struct nfs4_stateowner
*sop
)
359 atomic_inc(&sop
->so_count
);
364 same_owner_str(struct nfs4_stateowner
*sop
, struct xdr_netobj
*owner
)
366 return (sop
->so_owner
.len
== owner
->len
) &&
367 0 == memcmp(sop
->so_owner
.data
, owner
->data
, owner
->len
);
370 static struct nfs4_openowner
*
371 find_openstateowner_str_locked(unsigned int hashval
, struct nfsd4_open
*open
,
372 struct nfs4_client
*clp
)
374 struct nfs4_stateowner
*so
;
376 lockdep_assert_held(&clp
->cl_lock
);
378 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[hashval
],
380 if (!so
->so_is_open_owner
)
382 if (same_owner_str(so
, &open
->op_owner
))
383 return openowner(nfs4_get_stateowner(so
));
388 static struct nfs4_openowner
*
389 find_openstateowner_str(unsigned int hashval
, struct nfsd4_open
*open
,
390 struct nfs4_client
*clp
)
392 struct nfs4_openowner
*oo
;
394 spin_lock(&clp
->cl_lock
);
395 oo
= find_openstateowner_str_locked(hashval
, open
, clp
);
396 spin_unlock(&clp
->cl_lock
);
401 opaque_hashval(const void *ptr
, int nbytes
)
403 unsigned char *cptr
= (unsigned char *) ptr
;
413 static void nfsd4_free_file_rcu(struct rcu_head
*rcu
)
415 struct nfs4_file
*fp
= container_of(rcu
, struct nfs4_file
, fi_rcu
);
417 kmem_cache_free(file_slab
, fp
);
421 put_nfs4_file(struct nfs4_file
*fi
)
423 might_lock(&state_lock
);
425 if (refcount_dec_and_lock(&fi
->fi_ref
, &state_lock
)) {
426 hlist_del_rcu(&fi
->fi_hash
);
427 spin_unlock(&state_lock
);
428 WARN_ON_ONCE(!list_empty(&fi
->fi_clnt_odstate
));
429 WARN_ON_ONCE(!list_empty(&fi
->fi_delegations
));
430 call_rcu(&fi
->fi_rcu
, nfsd4_free_file_rcu
);
434 static struct nfsd_file
*
435 __nfs4_get_fd(struct nfs4_file
*f
, int oflag
)
437 if (f
->fi_fds
[oflag
])
438 return nfsd_file_get(f
->fi_fds
[oflag
]);
442 static struct nfsd_file
*
443 find_writeable_file_locked(struct nfs4_file
*f
)
445 struct nfsd_file
*ret
;
447 lockdep_assert_held(&f
->fi_lock
);
449 ret
= __nfs4_get_fd(f
, O_WRONLY
);
451 ret
= __nfs4_get_fd(f
, O_RDWR
);
455 static struct nfsd_file
*
456 find_writeable_file(struct nfs4_file
*f
)
458 struct nfsd_file
*ret
;
460 spin_lock(&f
->fi_lock
);
461 ret
= find_writeable_file_locked(f
);
462 spin_unlock(&f
->fi_lock
);
467 static struct nfsd_file
*
468 find_readable_file_locked(struct nfs4_file
*f
)
470 struct nfsd_file
*ret
;
472 lockdep_assert_held(&f
->fi_lock
);
474 ret
= __nfs4_get_fd(f
, O_RDONLY
);
476 ret
= __nfs4_get_fd(f
, O_RDWR
);
480 static struct nfsd_file
*
481 find_readable_file(struct nfs4_file
*f
)
483 struct nfsd_file
*ret
;
485 spin_lock(&f
->fi_lock
);
486 ret
= find_readable_file_locked(f
);
487 spin_unlock(&f
->fi_lock
);
493 find_any_file(struct nfs4_file
*f
)
495 struct nfsd_file
*ret
;
499 spin_lock(&f
->fi_lock
);
500 ret
= __nfs4_get_fd(f
, O_RDWR
);
502 ret
= __nfs4_get_fd(f
, O_WRONLY
);
504 ret
= __nfs4_get_fd(f
, O_RDONLY
);
506 spin_unlock(&f
->fi_lock
);
510 static atomic_long_t num_delegations
;
511 unsigned long max_delegations
;
514 * Open owner state (share locks)
517 /* hash tables for lock and open owners */
518 #define OWNER_HASH_BITS 8
519 #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS)
520 #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1)
522 static unsigned int ownerstr_hashval(struct xdr_netobj
*ownername
)
526 ret
= opaque_hashval(ownername
->data
, ownername
->len
);
527 return ret
& OWNER_HASH_MASK
;
530 /* hash table for nfs4_file */
531 #define FILE_HASH_BITS 8
532 #define FILE_HASH_SIZE (1 << FILE_HASH_BITS)
534 static unsigned int nfsd_fh_hashval(struct knfsd_fh
*fh
)
536 return jhash2(fh
->fh_base
.fh_pad
, XDR_QUADLEN(fh
->fh_size
), 0);
539 static unsigned int file_hashval(struct knfsd_fh
*fh
)
541 return nfsd_fh_hashval(fh
) & (FILE_HASH_SIZE
- 1);
544 static struct hlist_head file_hashtbl
[FILE_HASH_SIZE
];
547 __nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
549 lockdep_assert_held(&fp
->fi_lock
);
551 if (access
& NFS4_SHARE_ACCESS_WRITE
)
552 atomic_inc(&fp
->fi_access
[O_WRONLY
]);
553 if (access
& NFS4_SHARE_ACCESS_READ
)
554 atomic_inc(&fp
->fi_access
[O_RDONLY
]);
558 nfs4_file_get_access(struct nfs4_file
*fp
, u32 access
)
560 lockdep_assert_held(&fp
->fi_lock
);
562 /* Does this access mode make sense? */
563 if (access
& ~NFS4_SHARE_ACCESS_BOTH
)
566 /* Does it conflict with a deny mode already set? */
567 if ((access
& fp
->fi_share_deny
) != 0)
568 return nfserr_share_denied
;
570 __nfs4_file_get_access(fp
, access
);
574 static __be32
nfs4_file_check_deny(struct nfs4_file
*fp
, u32 deny
)
576 /* Common case is that there is no deny mode. */
578 /* Does this deny mode make sense? */
579 if (deny
& ~NFS4_SHARE_DENY_BOTH
)
582 if ((deny
& NFS4_SHARE_DENY_READ
) &&
583 atomic_read(&fp
->fi_access
[O_RDONLY
]))
584 return nfserr_share_denied
;
586 if ((deny
& NFS4_SHARE_DENY_WRITE
) &&
587 atomic_read(&fp
->fi_access
[O_WRONLY
]))
588 return nfserr_share_denied
;
593 static void __nfs4_file_put_access(struct nfs4_file
*fp
, int oflag
)
595 might_lock(&fp
->fi_lock
);
597 if (atomic_dec_and_lock(&fp
->fi_access
[oflag
], &fp
->fi_lock
)) {
598 struct nfsd_file
*f1
= NULL
;
599 struct nfsd_file
*f2
= NULL
;
601 swap(f1
, fp
->fi_fds
[oflag
]);
602 if (atomic_read(&fp
->fi_access
[1 - oflag
]) == 0)
603 swap(f2
, fp
->fi_fds
[O_RDWR
]);
604 spin_unlock(&fp
->fi_lock
);
612 static void nfs4_file_put_access(struct nfs4_file
*fp
, u32 access
)
614 WARN_ON_ONCE(access
& ~NFS4_SHARE_ACCESS_BOTH
);
616 if (access
& NFS4_SHARE_ACCESS_WRITE
)
617 __nfs4_file_put_access(fp
, O_WRONLY
);
618 if (access
& NFS4_SHARE_ACCESS_READ
)
619 __nfs4_file_put_access(fp
, O_RDONLY
);
623 * Allocate a new open/delegation state counter. This is needed for
624 * pNFS for proper return on close semantics.
626 * Note that we only allocate it for pNFS-enabled exports, otherwise
627 * all pointers to struct nfs4_clnt_odstate are always NULL.
629 static struct nfs4_clnt_odstate
*
630 alloc_clnt_odstate(struct nfs4_client
*clp
)
632 struct nfs4_clnt_odstate
*co
;
634 co
= kmem_cache_zalloc(odstate_slab
, GFP_KERNEL
);
637 refcount_set(&co
->co_odcount
, 1);
643 hash_clnt_odstate_locked(struct nfs4_clnt_odstate
*co
)
645 struct nfs4_file
*fp
= co
->co_file
;
647 lockdep_assert_held(&fp
->fi_lock
);
648 list_add(&co
->co_perfile
, &fp
->fi_clnt_odstate
);
652 get_clnt_odstate(struct nfs4_clnt_odstate
*co
)
655 refcount_inc(&co
->co_odcount
);
659 put_clnt_odstate(struct nfs4_clnt_odstate
*co
)
661 struct nfs4_file
*fp
;
667 if (refcount_dec_and_lock(&co
->co_odcount
, &fp
->fi_lock
)) {
668 list_del(&co
->co_perfile
);
669 spin_unlock(&fp
->fi_lock
);
671 nfsd4_return_all_file_layouts(co
->co_client
, fp
);
672 kmem_cache_free(odstate_slab
, co
);
676 static struct nfs4_clnt_odstate
*
677 find_or_hash_clnt_odstate(struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*new)
679 struct nfs4_clnt_odstate
*co
;
680 struct nfs4_client
*cl
;
687 spin_lock(&fp
->fi_lock
);
688 list_for_each_entry(co
, &fp
->fi_clnt_odstate
, co_perfile
) {
689 if (co
->co_client
== cl
) {
690 get_clnt_odstate(co
);
696 hash_clnt_odstate_locked(new);
698 spin_unlock(&fp
->fi_lock
);
702 struct nfs4_stid
*nfs4_alloc_stid(struct nfs4_client
*cl
, struct kmem_cache
*slab
,
703 void (*sc_free
)(struct nfs4_stid
*))
705 struct nfs4_stid
*stid
;
708 stid
= kmem_cache_zalloc(slab
, GFP_KERNEL
);
712 idr_preload(GFP_KERNEL
);
713 spin_lock(&cl
->cl_lock
);
714 /* Reserving 0 for start of file in nfsdfs "states" file: */
715 new_id
= idr_alloc_cyclic(&cl
->cl_stateids
, stid
, 1, 0, GFP_NOWAIT
);
716 spin_unlock(&cl
->cl_lock
);
721 stid
->sc_free
= sc_free
;
722 stid
->sc_client
= cl
;
723 stid
->sc_stateid
.si_opaque
.so_id
= new_id
;
724 stid
->sc_stateid
.si_opaque
.so_clid
= cl
->cl_clientid
;
725 /* Will be incremented before return to client: */
726 refcount_set(&stid
->sc_count
, 1);
727 spin_lock_init(&stid
->sc_lock
);
728 INIT_LIST_HEAD(&stid
->sc_cp_list
);
731 * It shouldn't be a problem to reuse an opaque stateid value.
732 * I don't think it is for 4.1. But with 4.0 I worry that, for
733 * example, a stray write retransmission could be accepted by
734 * the server when it should have been rejected. Therefore,
735 * adopt a trick from the sctp code to attempt to maximize the
736 * amount of time until an id is reused, by ensuring they always
737 * "increase" (mod INT_MAX):
741 kmem_cache_free(slab
, stid
);
746 * Create a unique stateid_t to represent each COPY.
748 static int nfs4_init_cp_state(struct nfsd_net
*nn
, copy_stateid_t
*stid
,
749 unsigned char sc_type
)
753 stid
->stid
.si_opaque
.so_clid
.cl_boot
= (u32
)nn
->boot_time
;
754 stid
->stid
.si_opaque
.so_clid
.cl_id
= nn
->s2s_cp_cl_id
;
755 stid
->sc_type
= sc_type
;
757 idr_preload(GFP_KERNEL
);
758 spin_lock(&nn
->s2s_cp_lock
);
759 new_id
= idr_alloc_cyclic(&nn
->s2s_cp_stateids
, stid
, 0, 0, GFP_NOWAIT
);
760 stid
->stid
.si_opaque
.so_id
= new_id
;
761 spin_unlock(&nn
->s2s_cp_lock
);
768 int nfs4_init_copy_state(struct nfsd_net
*nn
, struct nfsd4_copy
*copy
)
770 return nfs4_init_cp_state(nn
, ©
->cp_stateid
, NFS4_COPY_STID
);
773 struct nfs4_cpntf_state
*nfs4_alloc_init_cpntf_state(struct nfsd_net
*nn
,
774 struct nfs4_stid
*p_stid
)
776 struct nfs4_cpntf_state
*cps
;
778 cps
= kzalloc(sizeof(struct nfs4_cpntf_state
), GFP_KERNEL
);
781 cps
->cpntf_time
= ktime_get_boottime_seconds();
782 refcount_set(&cps
->cp_stateid
.sc_count
, 1);
783 if (!nfs4_init_cp_state(nn
, &cps
->cp_stateid
, NFS4_COPYNOTIFY_STID
))
785 spin_lock(&nn
->s2s_cp_lock
);
786 list_add(&cps
->cp_list
, &p_stid
->sc_cp_list
);
787 spin_unlock(&nn
->s2s_cp_lock
);
794 void nfs4_free_copy_state(struct nfsd4_copy
*copy
)
798 WARN_ON_ONCE(copy
->cp_stateid
.sc_type
!= NFS4_COPY_STID
);
799 nn
= net_generic(copy
->cp_clp
->net
, nfsd_net_id
);
800 spin_lock(&nn
->s2s_cp_lock
);
801 idr_remove(&nn
->s2s_cp_stateids
,
802 copy
->cp_stateid
.stid
.si_opaque
.so_id
);
803 spin_unlock(&nn
->s2s_cp_lock
);
806 static void nfs4_free_cpntf_statelist(struct net
*net
, struct nfs4_stid
*stid
)
808 struct nfs4_cpntf_state
*cps
;
811 nn
= net_generic(net
, nfsd_net_id
);
812 spin_lock(&nn
->s2s_cp_lock
);
813 while (!list_empty(&stid
->sc_cp_list
)) {
814 cps
= list_first_entry(&stid
->sc_cp_list
,
815 struct nfs4_cpntf_state
, cp_list
);
816 _free_cpntf_state_locked(nn
, cps
);
818 spin_unlock(&nn
->s2s_cp_lock
);
821 static struct nfs4_ol_stateid
* nfs4_alloc_open_stateid(struct nfs4_client
*clp
)
823 struct nfs4_stid
*stid
;
825 stid
= nfs4_alloc_stid(clp
, stateid_slab
, nfs4_free_ol_stateid
);
829 return openlockstateid(stid
);
832 static void nfs4_free_deleg(struct nfs4_stid
*stid
)
834 kmem_cache_free(deleg_slab
, stid
);
835 atomic_long_dec(&num_delegations
);
839 * When we recall a delegation, we should be careful not to hand it
840 * out again straight away.
841 * To ensure this we keep a pair of bloom filters ('new' and 'old')
842 * in which the filehandles of recalled delegations are "stored".
843 * If a filehandle appear in either filter, a delegation is blocked.
844 * When a delegation is recalled, the filehandle is stored in the "new"
846 * Every 30 seconds we swap the filters and clear the "new" one,
847 * unless both are empty of course.
849 * Each filter is 256 bits. We hash the filehandle to 32bit and use the
850 * low 3 bytes as hash-table indices.
852 * 'blocked_delegations_lock', which is always taken in block_delegations(),
853 * is used to manage concurrent access. Testing does not need the lock
854 * except when swapping the two filters.
856 static DEFINE_SPINLOCK(blocked_delegations_lock
);
857 static struct bloom_pair
{
858 int entries
, old_entries
;
860 int new; /* index into 'set' */
861 DECLARE_BITMAP(set
[2], 256);
862 } blocked_delegations
;
864 static int delegation_blocked(struct knfsd_fh
*fh
)
867 struct bloom_pair
*bd
= &blocked_delegations
;
869 if (bd
->entries
== 0)
871 if (ktime_get_seconds() - bd
->swap_time
> 30) {
872 spin_lock(&blocked_delegations_lock
);
873 if (ktime_get_seconds() - bd
->swap_time
> 30) {
874 bd
->entries
-= bd
->old_entries
;
875 bd
->old_entries
= bd
->entries
;
876 memset(bd
->set
[bd
->new], 0,
879 bd
->swap_time
= ktime_get_seconds();
881 spin_unlock(&blocked_delegations_lock
);
883 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
884 if (test_bit(hash
&255, bd
->set
[0]) &&
885 test_bit((hash
>>8)&255, bd
->set
[0]) &&
886 test_bit((hash
>>16)&255, bd
->set
[0]))
889 if (test_bit(hash
&255, bd
->set
[1]) &&
890 test_bit((hash
>>8)&255, bd
->set
[1]) &&
891 test_bit((hash
>>16)&255, bd
->set
[1]))
897 static void block_delegations(struct knfsd_fh
*fh
)
900 struct bloom_pair
*bd
= &blocked_delegations
;
902 hash
= jhash(&fh
->fh_base
, fh
->fh_size
, 0);
904 spin_lock(&blocked_delegations_lock
);
905 __set_bit(hash
&255, bd
->set
[bd
->new]);
906 __set_bit((hash
>>8)&255, bd
->set
[bd
->new]);
907 __set_bit((hash
>>16)&255, bd
->set
[bd
->new]);
908 if (bd
->entries
== 0)
909 bd
->swap_time
= ktime_get_seconds();
911 spin_unlock(&blocked_delegations_lock
);
914 static struct nfs4_delegation
*
915 alloc_init_deleg(struct nfs4_client
*clp
, struct nfs4_file
*fp
,
916 struct svc_fh
*current_fh
,
917 struct nfs4_clnt_odstate
*odstate
)
919 struct nfs4_delegation
*dp
;
922 dprintk("NFSD alloc_init_deleg\n");
923 n
= atomic_long_inc_return(&num_delegations
);
924 if (n
< 0 || n
> max_delegations
)
926 if (delegation_blocked(¤t_fh
->fh_handle
))
928 dp
= delegstateid(nfs4_alloc_stid(clp
, deleg_slab
, nfs4_free_deleg
));
933 * delegation seqid's are never incremented. The 4.1 special
934 * meaning of seqid 0 isn't meaningful, really, but let's avoid
935 * 0 anyway just for consistency and use 1:
937 dp
->dl_stid
.sc_stateid
.si_generation
= 1;
938 INIT_LIST_HEAD(&dp
->dl_perfile
);
939 INIT_LIST_HEAD(&dp
->dl_perclnt
);
940 INIT_LIST_HEAD(&dp
->dl_recall_lru
);
941 dp
->dl_clnt_odstate
= odstate
;
942 get_clnt_odstate(odstate
);
943 dp
->dl_type
= NFS4_OPEN_DELEGATE_READ
;
945 nfsd4_init_cb(&dp
->dl_recall
, dp
->dl_stid
.sc_client
,
946 &nfsd4_cb_recall_ops
, NFSPROC4_CLNT_CB_RECALL
);
948 dp
->dl_stid
.sc_file
= fp
;
951 atomic_long_dec(&num_delegations
);
956 nfs4_put_stid(struct nfs4_stid
*s
)
958 struct nfs4_file
*fp
= s
->sc_file
;
959 struct nfs4_client
*clp
= s
->sc_client
;
961 might_lock(&clp
->cl_lock
);
963 if (!refcount_dec_and_lock(&s
->sc_count
, &clp
->cl_lock
)) {
964 wake_up_all(&close_wq
);
967 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
968 nfs4_free_cpntf_statelist(clp
->net
, s
);
969 spin_unlock(&clp
->cl_lock
);
976 nfs4_inc_and_copy_stateid(stateid_t
*dst
, struct nfs4_stid
*stid
)
978 stateid_t
*src
= &stid
->sc_stateid
;
980 spin_lock(&stid
->sc_lock
);
981 if (unlikely(++src
->si_generation
== 0))
982 src
->si_generation
= 1;
983 memcpy(dst
, src
, sizeof(*dst
));
984 spin_unlock(&stid
->sc_lock
);
987 static void put_deleg_file(struct nfs4_file
*fp
)
989 struct nfsd_file
*nf
= NULL
;
991 spin_lock(&fp
->fi_lock
);
992 if (--fp
->fi_delegees
== 0)
993 swap(nf
, fp
->fi_deleg_file
);
994 spin_unlock(&fp
->fi_lock
);
1000 static void nfs4_unlock_deleg_lease(struct nfs4_delegation
*dp
)
1002 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
1003 struct nfsd_file
*nf
= fp
->fi_deleg_file
;
1005 WARN_ON_ONCE(!fp
->fi_delegees
);
1007 vfs_setlease(nf
->nf_file
, F_UNLCK
, NULL
, (void **)&dp
);
1011 static void destroy_unhashed_deleg(struct nfs4_delegation
*dp
)
1013 put_clnt_odstate(dp
->dl_clnt_odstate
);
1014 nfs4_unlock_deleg_lease(dp
);
1015 nfs4_put_stid(&dp
->dl_stid
);
1018 void nfs4_unhash_stid(struct nfs4_stid
*s
)
1024 * nfs4_delegation_exists - Discover if this delegation already exists
1025 * @clp: a pointer to the nfs4_client we're granting a delegation to
1026 * @fp: a pointer to the nfs4_file we're granting a delegation on
1029 * On success: true iff an existing delegation is found
1033 nfs4_delegation_exists(struct nfs4_client
*clp
, struct nfs4_file
*fp
)
1035 struct nfs4_delegation
*searchdp
= NULL
;
1036 struct nfs4_client
*searchclp
= NULL
;
1038 lockdep_assert_held(&state_lock
);
1039 lockdep_assert_held(&fp
->fi_lock
);
1041 list_for_each_entry(searchdp
, &fp
->fi_delegations
, dl_perfile
) {
1042 searchclp
= searchdp
->dl_stid
.sc_client
;
1043 if (clp
== searchclp
) {
1051 * hash_delegation_locked - Add a delegation to the appropriate lists
1052 * @dp: a pointer to the nfs4_delegation we are adding.
1053 * @fp: a pointer to the nfs4_file we're granting a delegation on
1056 * On success: NULL if the delegation was successfully hashed.
1058 * On error: -EAGAIN if one was previously granted to this
1059 * nfs4_client for this nfs4_file. Delegation is not hashed.
1064 hash_delegation_locked(struct nfs4_delegation
*dp
, struct nfs4_file
*fp
)
1066 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
1068 lockdep_assert_held(&state_lock
);
1069 lockdep_assert_held(&fp
->fi_lock
);
1071 if (nfs4_delegation_exists(clp
, fp
))
1073 refcount_inc(&dp
->dl_stid
.sc_count
);
1074 dp
->dl_stid
.sc_type
= NFS4_DELEG_STID
;
1075 list_add(&dp
->dl_perfile
, &fp
->fi_delegations
);
1076 list_add(&dp
->dl_perclnt
, &clp
->cl_delegations
);
1081 unhash_delegation_locked(struct nfs4_delegation
*dp
)
1083 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
1085 lockdep_assert_held(&state_lock
);
1087 if (list_empty(&dp
->dl_perfile
))
1090 dp
->dl_stid
.sc_type
= NFS4_CLOSED_DELEG_STID
;
1091 /* Ensure that deleg break won't try to requeue it */
1093 spin_lock(&fp
->fi_lock
);
1094 list_del_init(&dp
->dl_perclnt
);
1095 list_del_init(&dp
->dl_recall_lru
);
1096 list_del_init(&dp
->dl_perfile
);
1097 spin_unlock(&fp
->fi_lock
);
1101 static void destroy_delegation(struct nfs4_delegation
*dp
)
1105 spin_lock(&state_lock
);
1106 unhashed
= unhash_delegation_locked(dp
);
1107 spin_unlock(&state_lock
);
1109 destroy_unhashed_deleg(dp
);
1112 static void revoke_delegation(struct nfs4_delegation
*dp
)
1114 struct nfs4_client
*clp
= dp
->dl_stid
.sc_client
;
1116 WARN_ON(!list_empty(&dp
->dl_recall_lru
));
1118 if (clp
->cl_minorversion
) {
1119 dp
->dl_stid
.sc_type
= NFS4_REVOKED_DELEG_STID
;
1120 refcount_inc(&dp
->dl_stid
.sc_count
);
1121 spin_lock(&clp
->cl_lock
);
1122 list_add(&dp
->dl_recall_lru
, &clp
->cl_revoked
);
1123 spin_unlock(&clp
->cl_lock
);
1125 destroy_unhashed_deleg(dp
);
1132 static unsigned int clientid_hashval(u32 id
)
1134 return id
& CLIENT_HASH_MASK
;
1137 static unsigned int clientstr_hashval(struct xdr_netobj name
)
1139 return opaque_hashval(name
.data
, 8) & CLIENT_HASH_MASK
;
1143 * We store the NONE, READ, WRITE, and BOTH bits separately in the
1144 * st_{access,deny}_bmap field of the stateid, in order to track not
1145 * only what share bits are currently in force, but also what
1146 * combinations of share bits previous opens have used. This allows us
1147 * to enforce the recommendation of rfc 3530 14.2.19 that the server
1148 * return an error if the client attempt to downgrade to a combination
1149 * of share bits not explicable by closing some of its previous opens.
1151 * XXX: This enforcement is actually incomplete, since we don't keep
1152 * track of access/deny bit combinations; so, e.g., we allow:
1154 * OPEN allow read, deny write
1155 * OPEN allow both, deny none
1156 * DOWNGRADE allow read, deny none
1158 * which we should reject.
1161 bmap_to_share_mode(unsigned long bmap
) {
1163 unsigned int access
= 0;
1165 for (i
= 1; i
< 4; i
++) {
1166 if (test_bit(i
, &bmap
))
1172 /* set share access for a given stateid */
1174 set_access(u32 access
, struct nfs4_ol_stateid
*stp
)
1176 unsigned char mask
= 1 << access
;
1178 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
1179 stp
->st_access_bmap
|= mask
;
1182 /* clear share access for a given stateid */
1184 clear_access(u32 access
, struct nfs4_ol_stateid
*stp
)
1186 unsigned char mask
= 1 << access
;
1188 WARN_ON_ONCE(access
> NFS4_SHARE_ACCESS_BOTH
);
1189 stp
->st_access_bmap
&= ~mask
;
1192 /* test whether a given stateid has access */
1194 test_access(u32 access
, struct nfs4_ol_stateid
*stp
)
1196 unsigned char mask
= 1 << access
;
1198 return (bool)(stp
->st_access_bmap
& mask
);
1201 /* set share deny for a given stateid */
1203 set_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1205 unsigned char mask
= 1 << deny
;
1207 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
1208 stp
->st_deny_bmap
|= mask
;
1211 /* clear share deny for a given stateid */
1213 clear_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1215 unsigned char mask
= 1 << deny
;
1217 WARN_ON_ONCE(deny
> NFS4_SHARE_DENY_BOTH
);
1218 stp
->st_deny_bmap
&= ~mask
;
1221 /* test whether a given stateid is denying specific access */
1223 test_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1225 unsigned char mask
= 1 << deny
;
1227 return (bool)(stp
->st_deny_bmap
& mask
);
1230 static int nfs4_access_to_omode(u32 access
)
1232 switch (access
& NFS4_SHARE_ACCESS_BOTH
) {
1233 case NFS4_SHARE_ACCESS_READ
:
1235 case NFS4_SHARE_ACCESS_WRITE
:
1237 case NFS4_SHARE_ACCESS_BOTH
:
1245 * A stateid that had a deny mode associated with it is being released
1246 * or downgraded. Recalculate the deny mode on the file.
1249 recalculate_deny_mode(struct nfs4_file
*fp
)
1251 struct nfs4_ol_stateid
*stp
;
1253 spin_lock(&fp
->fi_lock
);
1254 fp
->fi_share_deny
= 0;
1255 list_for_each_entry(stp
, &fp
->fi_stateids
, st_perfile
)
1256 fp
->fi_share_deny
|= bmap_to_share_mode(stp
->st_deny_bmap
);
1257 spin_unlock(&fp
->fi_lock
);
1261 reset_union_bmap_deny(u32 deny
, struct nfs4_ol_stateid
*stp
)
1264 bool change
= false;
1266 for (i
= 1; i
< 4; i
++) {
1267 if ((i
& deny
) != i
) {
1273 /* Recalculate per-file deny mode if there was a change */
1275 recalculate_deny_mode(stp
->st_stid
.sc_file
);
1278 /* release all access and file references for a given stateid */
1280 release_all_access(struct nfs4_ol_stateid
*stp
)
1283 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
1285 if (fp
&& stp
->st_deny_bmap
!= 0)
1286 recalculate_deny_mode(fp
);
1288 for (i
= 1; i
< 4; i
++) {
1289 if (test_access(i
, stp
))
1290 nfs4_file_put_access(stp
->st_stid
.sc_file
, i
);
1291 clear_access(i
, stp
);
1295 static inline void nfs4_free_stateowner(struct nfs4_stateowner
*sop
)
1297 kfree(sop
->so_owner
.data
);
1298 sop
->so_ops
->so_free(sop
);
1301 static void nfs4_put_stateowner(struct nfs4_stateowner
*sop
)
1303 struct nfs4_client
*clp
= sop
->so_client
;
1305 might_lock(&clp
->cl_lock
);
1307 if (!atomic_dec_and_lock(&sop
->so_count
, &clp
->cl_lock
))
1309 sop
->so_ops
->so_unhash(sop
);
1310 spin_unlock(&clp
->cl_lock
);
1311 nfs4_free_stateowner(sop
);
1315 nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid
*stp
)
1317 return list_empty(&stp
->st_perfile
);
1320 static bool unhash_ol_stateid(struct nfs4_ol_stateid
*stp
)
1322 struct nfs4_file
*fp
= stp
->st_stid
.sc_file
;
1324 lockdep_assert_held(&stp
->st_stateowner
->so_client
->cl_lock
);
1326 if (list_empty(&stp
->st_perfile
))
1329 spin_lock(&fp
->fi_lock
);
1330 list_del_init(&stp
->st_perfile
);
1331 spin_unlock(&fp
->fi_lock
);
1332 list_del(&stp
->st_perstateowner
);
1336 static void nfs4_free_ol_stateid(struct nfs4_stid
*stid
)
1338 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1340 put_clnt_odstate(stp
->st_clnt_odstate
);
1341 release_all_access(stp
);
1342 if (stp
->st_stateowner
)
1343 nfs4_put_stateowner(stp
->st_stateowner
);
1344 kmem_cache_free(stateid_slab
, stid
);
1347 static void nfs4_free_lock_stateid(struct nfs4_stid
*stid
)
1349 struct nfs4_ol_stateid
*stp
= openlockstateid(stid
);
1350 struct nfs4_lockowner
*lo
= lockowner(stp
->st_stateowner
);
1351 struct nfsd_file
*nf
;
1353 nf
= find_any_file(stp
->st_stid
.sc_file
);
1355 get_file(nf
->nf_file
);
1356 filp_close(nf
->nf_file
, (fl_owner_t
)lo
);
1359 nfs4_free_ol_stateid(stid
);
1363 * Put the persistent reference to an already unhashed generic stateid, while
1364 * holding the cl_lock. If it's the last reference, then put it onto the
1365 * reaplist for later destruction.
1367 static void put_ol_stateid_locked(struct nfs4_ol_stateid
*stp
,
1368 struct list_head
*reaplist
)
1370 struct nfs4_stid
*s
= &stp
->st_stid
;
1371 struct nfs4_client
*clp
= s
->sc_client
;
1373 lockdep_assert_held(&clp
->cl_lock
);
1375 WARN_ON_ONCE(!list_empty(&stp
->st_locks
));
1377 if (!refcount_dec_and_test(&s
->sc_count
)) {
1378 wake_up_all(&close_wq
);
1382 idr_remove(&clp
->cl_stateids
, s
->sc_stateid
.si_opaque
.so_id
);
1383 list_add(&stp
->st_locks
, reaplist
);
1386 static bool unhash_lock_stateid(struct nfs4_ol_stateid
*stp
)
1388 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1390 if (!unhash_ol_stateid(stp
))
1392 list_del_init(&stp
->st_locks
);
1393 nfs4_unhash_stid(&stp
->st_stid
);
1397 static void release_lock_stateid(struct nfs4_ol_stateid
*stp
)
1399 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
1402 spin_lock(&clp
->cl_lock
);
1403 unhashed
= unhash_lock_stateid(stp
);
1404 spin_unlock(&clp
->cl_lock
);
1406 nfs4_put_stid(&stp
->st_stid
);
1409 static void unhash_lockowner_locked(struct nfs4_lockowner
*lo
)
1411 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
1413 lockdep_assert_held(&clp
->cl_lock
);
1415 list_del_init(&lo
->lo_owner
.so_strhash
);
1419 * Free a list of generic stateids that were collected earlier after being
1423 free_ol_stateid_reaplist(struct list_head
*reaplist
)
1425 struct nfs4_ol_stateid
*stp
;
1426 struct nfs4_file
*fp
;
1430 while (!list_empty(reaplist
)) {
1431 stp
= list_first_entry(reaplist
, struct nfs4_ol_stateid
,
1433 list_del(&stp
->st_locks
);
1434 fp
= stp
->st_stid
.sc_file
;
1435 stp
->st_stid
.sc_free(&stp
->st_stid
);
1441 static void release_open_stateid_locks(struct nfs4_ol_stateid
*open_stp
,
1442 struct list_head
*reaplist
)
1444 struct nfs4_ol_stateid
*stp
;
1446 lockdep_assert_held(&open_stp
->st_stid
.sc_client
->cl_lock
);
1448 while (!list_empty(&open_stp
->st_locks
)) {
1449 stp
= list_entry(open_stp
->st_locks
.next
,
1450 struct nfs4_ol_stateid
, st_locks
);
1451 WARN_ON(!unhash_lock_stateid(stp
));
1452 put_ol_stateid_locked(stp
, reaplist
);
1456 static bool unhash_open_stateid(struct nfs4_ol_stateid
*stp
,
1457 struct list_head
*reaplist
)
1459 lockdep_assert_held(&stp
->st_stid
.sc_client
->cl_lock
);
1461 if (!unhash_ol_stateid(stp
))
1463 release_open_stateid_locks(stp
, reaplist
);
1467 static void release_open_stateid(struct nfs4_ol_stateid
*stp
)
1469 LIST_HEAD(reaplist
);
1471 spin_lock(&stp
->st_stid
.sc_client
->cl_lock
);
1472 if (unhash_open_stateid(stp
, &reaplist
))
1473 put_ol_stateid_locked(stp
, &reaplist
);
1474 spin_unlock(&stp
->st_stid
.sc_client
->cl_lock
);
1475 free_ol_stateid_reaplist(&reaplist
);
1478 static void unhash_openowner_locked(struct nfs4_openowner
*oo
)
1480 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1482 lockdep_assert_held(&clp
->cl_lock
);
1484 list_del_init(&oo
->oo_owner
.so_strhash
);
1485 list_del_init(&oo
->oo_perclient
);
1488 static void release_last_closed_stateid(struct nfs4_openowner
*oo
)
1490 struct nfsd_net
*nn
= net_generic(oo
->oo_owner
.so_client
->net
,
1492 struct nfs4_ol_stateid
*s
;
1494 spin_lock(&nn
->client_lock
);
1495 s
= oo
->oo_last_closed_stid
;
1497 list_del_init(&oo
->oo_close_lru
);
1498 oo
->oo_last_closed_stid
= NULL
;
1500 spin_unlock(&nn
->client_lock
);
1502 nfs4_put_stid(&s
->st_stid
);
1505 static void release_openowner(struct nfs4_openowner
*oo
)
1507 struct nfs4_ol_stateid
*stp
;
1508 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
1509 struct list_head reaplist
;
1511 INIT_LIST_HEAD(&reaplist
);
1513 spin_lock(&clp
->cl_lock
);
1514 unhash_openowner_locked(oo
);
1515 while (!list_empty(&oo
->oo_owner
.so_stateids
)) {
1516 stp
= list_first_entry(&oo
->oo_owner
.so_stateids
,
1517 struct nfs4_ol_stateid
, st_perstateowner
);
1518 if (unhash_open_stateid(stp
, &reaplist
))
1519 put_ol_stateid_locked(stp
, &reaplist
);
1521 spin_unlock(&clp
->cl_lock
);
1522 free_ol_stateid_reaplist(&reaplist
);
1523 release_last_closed_stateid(oo
);
1524 nfs4_put_stateowner(&oo
->oo_owner
);
1528 hash_sessionid(struct nfs4_sessionid
*sessionid
)
1530 struct nfsd4_sessionid
*sid
= (struct nfsd4_sessionid
*)sessionid
;
1532 return sid
->sequence
% SESSION_HASH_SIZE
;
1535 #ifdef CONFIG_SUNRPC_DEBUG
1537 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1539 u32
*ptr
= (u32
*)(&sessionid
->data
[0]);
1540 dprintk("%s: %u:%u:%u:%u\n", fn
, ptr
[0], ptr
[1], ptr
[2], ptr
[3]);
1544 dump_sessionid(const char *fn
, struct nfs4_sessionid
*sessionid
)
1550 * Bump the seqid on cstate->replay_owner, and clear replay_owner if it
1551 * won't be used for replay.
1553 void nfsd4_bump_seqid(struct nfsd4_compound_state
*cstate
, __be32 nfserr
)
1555 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
1557 if (nfserr
== nfserr_replay_me
)
1560 if (!seqid_mutating_err(ntohl(nfserr
))) {
1561 nfsd4_cstate_clear_replay(cstate
);
1566 if (so
->so_is_open_owner
)
1567 release_last_closed_stateid(openowner(so
));
1573 gen_sessionid(struct nfsd4_session
*ses
)
1575 struct nfs4_client
*clp
= ses
->se_client
;
1576 struct nfsd4_sessionid
*sid
;
1578 sid
= (struct nfsd4_sessionid
*)ses
->se_sessionid
.data
;
1579 sid
->clientid
= clp
->cl_clientid
;
1580 sid
->sequence
= current_sessionid
++;
1585 * The protocol defines ca_maxresponssize_cached to include the size of
1586 * the rpc header, but all we need to cache is the data starting after
1587 * the end of the initial SEQUENCE operation--the rest we regenerate
1588 * each time. Therefore we can advertise a ca_maxresponssize_cached
1589 * value that is the number of bytes in our cache plus a few additional
1590 * bytes. In order to stay on the safe side, and not promise more than
1591 * we can cache, those additional bytes must be the minimum possible: 24
1592 * bytes of rpc header (xid through accept state, with AUTH_NULL
1593 * verifier), 12 for the compound header (with zero-length tag), and 44
1594 * for the SEQUENCE op response:
1596 #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44)
1599 free_session_slots(struct nfsd4_session
*ses
)
1603 for (i
= 0; i
< ses
->se_fchannel
.maxreqs
; i
++) {
1604 free_svc_cred(&ses
->se_slots
[i
]->sl_cred
);
1605 kfree(ses
->se_slots
[i
]);
1610 * We don't actually need to cache the rpc and session headers, so we
1611 * can allocate a little less for each slot:
1613 static inline u32
slot_bytes(struct nfsd4_channel_attrs
*ca
)
1617 if (ca
->maxresp_cached
< NFSD_MIN_HDR_SEQ_SZ
)
1620 size
= ca
->maxresp_cached
- NFSD_MIN_HDR_SEQ_SZ
;
1621 return size
+ sizeof(struct nfsd4_slot
);
1625 * XXX: If we run out of reserved DRC memory we could (up to a point)
1626 * re-negotiate active sessions and reduce their slot usage to make
1627 * room for new connections. For now we just fail the create session.
1629 static u32
nfsd4_get_drc_mem(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
1631 u32 slotsize
= slot_bytes(ca
);
1632 u32 num
= ca
->maxreqs
;
1633 unsigned long avail
, total_avail
;
1634 unsigned int scale_factor
;
1636 spin_lock(&nfsd_drc_lock
);
1637 if (nfsd_drc_max_mem
> nfsd_drc_mem_used
)
1638 total_avail
= nfsd_drc_max_mem
- nfsd_drc_mem_used
;
1640 /* We have handed out more space than we chose in
1641 * set_max_drc() to allow. That isn't really a
1642 * problem as long as that doesn't make us think we
1643 * have lots more due to integer overflow.
1646 avail
= min((unsigned long)NFSD_MAX_MEM_PER_SESSION
, total_avail
);
1648 * Never use more than a fraction of the remaining memory,
1649 * unless it's the only way to give this client a slot.
1650 * The chosen fraction is either 1/8 or 1/number of threads,
1651 * whichever is smaller. This ensures there are adequate
1652 * slots to support multiple clients per thread.
1653 * Give the client one slot even if that would require
1654 * over-allocation--it is better than failure.
1656 scale_factor
= max_t(unsigned int, 8, nn
->nfsd_serv
->sv_nrthreads
);
1658 avail
= clamp_t(unsigned long, avail
, slotsize
,
1659 total_avail
/scale_factor
);
1660 num
= min_t(int, num
, avail
/ slotsize
);
1661 num
= max_t(int, num
, 1);
1662 nfsd_drc_mem_used
+= num
* slotsize
;
1663 spin_unlock(&nfsd_drc_lock
);
1668 static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs
*ca
)
1670 int slotsize
= slot_bytes(ca
);
1672 spin_lock(&nfsd_drc_lock
);
1673 nfsd_drc_mem_used
-= slotsize
* ca
->maxreqs
;
1674 spin_unlock(&nfsd_drc_lock
);
1677 static struct nfsd4_session
*alloc_session(struct nfsd4_channel_attrs
*fattrs
,
1678 struct nfsd4_channel_attrs
*battrs
)
1680 int numslots
= fattrs
->maxreqs
;
1681 int slotsize
= slot_bytes(fattrs
);
1682 struct nfsd4_session
*new;
1685 BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION
* sizeof(struct nfsd4_slot
*)
1686 + sizeof(struct nfsd4_session
) > PAGE_SIZE
);
1687 mem
= numslots
* sizeof(struct nfsd4_slot
*);
1689 new = kzalloc(sizeof(*new) + mem
, GFP_KERNEL
);
1692 /* allocate each struct nfsd4_slot and data cache in one piece */
1693 for (i
= 0; i
< numslots
; i
++) {
1694 new->se_slots
[i
] = kzalloc(slotsize
, GFP_KERNEL
);
1695 if (!new->se_slots
[i
])
1699 memcpy(&new->se_fchannel
, fattrs
, sizeof(struct nfsd4_channel_attrs
));
1700 memcpy(&new->se_bchannel
, battrs
, sizeof(struct nfsd4_channel_attrs
));
1705 kfree(new->se_slots
[i
]);
1710 static void free_conn(struct nfsd4_conn
*c
)
1712 svc_xprt_put(c
->cn_xprt
);
1716 static void nfsd4_conn_lost(struct svc_xpt_user
*u
)
1718 struct nfsd4_conn
*c
= container_of(u
, struct nfsd4_conn
, cn_xpt_user
);
1719 struct nfs4_client
*clp
= c
->cn_session
->se_client
;
1721 spin_lock(&clp
->cl_lock
);
1722 if (!list_empty(&c
->cn_persession
)) {
1723 list_del(&c
->cn_persession
);
1726 nfsd4_probe_callback(clp
);
1727 spin_unlock(&clp
->cl_lock
);
1730 static struct nfsd4_conn
*alloc_conn(struct svc_rqst
*rqstp
, u32 flags
)
1732 struct nfsd4_conn
*conn
;
1734 conn
= kmalloc(sizeof(struct nfsd4_conn
), GFP_KERNEL
);
1737 svc_xprt_get(rqstp
->rq_xprt
);
1738 conn
->cn_xprt
= rqstp
->rq_xprt
;
1739 conn
->cn_flags
= flags
;
1740 INIT_LIST_HEAD(&conn
->cn_xpt_user
.list
);
1744 static void __nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1746 conn
->cn_session
= ses
;
1747 list_add(&conn
->cn_persession
, &ses
->se_conns
);
1750 static void nfsd4_hash_conn(struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1752 struct nfs4_client
*clp
= ses
->se_client
;
1754 spin_lock(&clp
->cl_lock
);
1755 __nfsd4_hash_conn(conn
, ses
);
1756 spin_unlock(&clp
->cl_lock
);
1759 static int nfsd4_register_conn(struct nfsd4_conn
*conn
)
1761 conn
->cn_xpt_user
.callback
= nfsd4_conn_lost
;
1762 return register_xpt_user(conn
->cn_xprt
, &conn
->cn_xpt_user
);
1765 static void nfsd4_init_conn(struct svc_rqst
*rqstp
, struct nfsd4_conn
*conn
, struct nfsd4_session
*ses
)
1769 nfsd4_hash_conn(conn
, ses
);
1770 ret
= nfsd4_register_conn(conn
);
1772 /* oops; xprt is already down: */
1773 nfsd4_conn_lost(&conn
->cn_xpt_user
);
1774 /* We may have gained or lost a callback channel: */
1775 nfsd4_probe_callback_sync(ses
->se_client
);
1778 static struct nfsd4_conn
*alloc_conn_from_crses(struct svc_rqst
*rqstp
, struct nfsd4_create_session
*cses
)
1780 u32 dir
= NFS4_CDFC4_FORE
;
1782 if (cses
->flags
& SESSION4_BACK_CHAN
)
1783 dir
|= NFS4_CDFC4_BACK
;
1784 return alloc_conn(rqstp
, dir
);
1787 /* must be called under client_lock */
1788 static void nfsd4_del_conns(struct nfsd4_session
*s
)
1790 struct nfs4_client
*clp
= s
->se_client
;
1791 struct nfsd4_conn
*c
;
1793 spin_lock(&clp
->cl_lock
);
1794 while (!list_empty(&s
->se_conns
)) {
1795 c
= list_first_entry(&s
->se_conns
, struct nfsd4_conn
, cn_persession
);
1796 list_del_init(&c
->cn_persession
);
1797 spin_unlock(&clp
->cl_lock
);
1799 unregister_xpt_user(c
->cn_xprt
, &c
->cn_xpt_user
);
1802 spin_lock(&clp
->cl_lock
);
1804 spin_unlock(&clp
->cl_lock
);
1807 static void __free_session(struct nfsd4_session
*ses
)
1809 free_session_slots(ses
);
1813 static void free_session(struct nfsd4_session
*ses
)
1815 nfsd4_del_conns(ses
);
1816 nfsd4_put_drc_mem(&ses
->se_fchannel
);
1817 __free_session(ses
);
1820 static void init_session(struct svc_rqst
*rqstp
, struct nfsd4_session
*new, struct nfs4_client
*clp
, struct nfsd4_create_session
*cses
)
1823 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
1825 new->se_client
= clp
;
1828 INIT_LIST_HEAD(&new->se_conns
);
1830 new->se_cb_seq_nr
= 1;
1831 new->se_flags
= cses
->flags
;
1832 new->se_cb_prog
= cses
->callback_prog
;
1833 new->se_cb_sec
= cses
->cb_sec
;
1834 atomic_set(&new->se_ref
, 0);
1835 idx
= hash_sessionid(&new->se_sessionid
);
1836 list_add(&new->se_hash
, &nn
->sessionid_hashtbl
[idx
]);
1837 spin_lock(&clp
->cl_lock
);
1838 list_add(&new->se_perclnt
, &clp
->cl_sessions
);
1839 spin_unlock(&clp
->cl_lock
);
1842 struct sockaddr
*sa
= svc_addr(rqstp
);
1844 * This is a little silly; with sessions there's no real
1845 * use for the callback address. Use the peer address
1846 * as a reasonable default for now, but consider fixing
1847 * the rpc client not to require an address in the
1850 rpc_copy_addr((struct sockaddr
*)&clp
->cl_cb_conn
.cb_addr
, sa
);
1851 clp
->cl_cb_conn
.cb_addrlen
= svc_addr_len(sa
);
1855 /* caller must hold client_lock */
1856 static struct nfsd4_session
*
1857 __find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
)
1859 struct nfsd4_session
*elem
;
1861 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
1863 lockdep_assert_held(&nn
->client_lock
);
1865 dump_sessionid(__func__
, sessionid
);
1866 idx
= hash_sessionid(sessionid
);
1867 /* Search in the appropriate list */
1868 list_for_each_entry(elem
, &nn
->sessionid_hashtbl
[idx
], se_hash
) {
1869 if (!memcmp(elem
->se_sessionid
.data
, sessionid
->data
,
1870 NFS4_MAX_SESSIONID_LEN
)) {
1875 dprintk("%s: session not found\n", __func__
);
1879 static struct nfsd4_session
*
1880 find_in_sessionid_hashtbl(struct nfs4_sessionid
*sessionid
, struct net
*net
,
1883 struct nfsd4_session
*session
;
1884 __be32 status
= nfserr_badsession
;
1886 session
= __find_in_sessionid_hashtbl(sessionid
, net
);
1889 status
= nfsd4_get_session_locked(session
);
1897 /* caller must hold client_lock */
1899 unhash_session(struct nfsd4_session
*ses
)
1901 struct nfs4_client
*clp
= ses
->se_client
;
1902 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
1904 lockdep_assert_held(&nn
->client_lock
);
1906 list_del(&ses
->se_hash
);
1907 spin_lock(&ses
->se_client
->cl_lock
);
1908 list_del(&ses
->se_perclnt
);
1909 spin_unlock(&ses
->se_client
->cl_lock
);
1912 /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */
1914 STALE_CLIENTID(clientid_t
*clid
, struct nfsd_net
*nn
)
1917 * We're assuming the clid was not given out from a boot
1918 * precisely 2^32 (about 136 years) before this one. That seems
1919 * a safe assumption:
1921 if (clid
->cl_boot
== (u32
)nn
->boot_time
)
1923 trace_nfsd_clid_stale(clid
);
1928 * XXX Should we use a slab cache ?
1929 * This type of memory management is somewhat inefficient, but we use it
1930 * anyway since SETCLIENTID is not a common operation.
1932 static struct nfs4_client
*alloc_client(struct xdr_netobj name
)
1934 struct nfs4_client
*clp
;
1937 clp
= kmem_cache_zalloc(client_slab
, GFP_KERNEL
);
1940 xdr_netobj_dup(&clp
->cl_name
, &name
, GFP_KERNEL
);
1941 if (clp
->cl_name
.data
== NULL
)
1943 clp
->cl_ownerstr_hashtbl
= kmalloc_array(OWNER_HASH_SIZE
,
1944 sizeof(struct list_head
),
1946 if (!clp
->cl_ownerstr_hashtbl
)
1947 goto err_no_hashtbl
;
1948 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++)
1949 INIT_LIST_HEAD(&clp
->cl_ownerstr_hashtbl
[i
]);
1950 INIT_LIST_HEAD(&clp
->cl_sessions
);
1951 idr_init(&clp
->cl_stateids
);
1952 atomic_set(&clp
->cl_rpc_users
, 0);
1953 clp
->cl_cb_state
= NFSD4_CB_UNKNOWN
;
1954 INIT_LIST_HEAD(&clp
->cl_idhash
);
1955 INIT_LIST_HEAD(&clp
->cl_openowners
);
1956 INIT_LIST_HEAD(&clp
->cl_delegations
);
1957 INIT_LIST_HEAD(&clp
->cl_lru
);
1958 INIT_LIST_HEAD(&clp
->cl_revoked
);
1959 #ifdef CONFIG_NFSD_PNFS
1960 INIT_LIST_HEAD(&clp
->cl_lo_states
);
1962 INIT_LIST_HEAD(&clp
->async_copies
);
1963 spin_lock_init(&clp
->async_lock
);
1964 spin_lock_init(&clp
->cl_lock
);
1965 rpc_init_wait_queue(&clp
->cl_cb_waitq
, "Backchannel slot table");
1968 kfree(clp
->cl_name
.data
);
1970 kmem_cache_free(client_slab
, clp
);
1974 static void __free_client(struct kref
*k
)
1976 struct nfsdfs_client
*c
= container_of(k
, struct nfsdfs_client
, cl_ref
);
1977 struct nfs4_client
*clp
= container_of(c
, struct nfs4_client
, cl_nfsdfs
);
1979 free_svc_cred(&clp
->cl_cred
);
1980 kfree(clp
->cl_ownerstr_hashtbl
);
1981 kfree(clp
->cl_name
.data
);
1982 kfree(clp
->cl_nii_domain
.data
);
1983 kfree(clp
->cl_nii_name
.data
);
1984 idr_destroy(&clp
->cl_stateids
);
1985 kmem_cache_free(client_slab
, clp
);
1988 static void drop_client(struct nfs4_client
*clp
)
1990 kref_put(&clp
->cl_nfsdfs
.cl_ref
, __free_client
);
1994 free_client(struct nfs4_client
*clp
)
1996 while (!list_empty(&clp
->cl_sessions
)) {
1997 struct nfsd4_session
*ses
;
1998 ses
= list_entry(clp
->cl_sessions
.next
, struct nfsd4_session
,
2000 list_del(&ses
->se_perclnt
);
2001 WARN_ON_ONCE(atomic_read(&ses
->se_ref
));
2004 rpc_destroy_wait_queue(&clp
->cl_cb_waitq
);
2005 if (clp
->cl_nfsd_dentry
) {
2006 nfsd_client_rmdir(clp
->cl_nfsd_dentry
);
2007 clp
->cl_nfsd_dentry
= NULL
;
2008 wake_up_all(&expiry_wq
);
2013 /* must be called under the client_lock */
2015 unhash_client_locked(struct nfs4_client
*clp
)
2017 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2018 struct nfsd4_session
*ses
;
2020 lockdep_assert_held(&nn
->client_lock
);
2022 /* Mark the client as expired! */
2024 /* Make it invisible */
2025 if (!list_empty(&clp
->cl_idhash
)) {
2026 list_del_init(&clp
->cl_idhash
);
2027 if (test_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
))
2028 rb_erase(&clp
->cl_namenode
, &nn
->conf_name_tree
);
2030 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
2032 list_del_init(&clp
->cl_lru
);
2033 spin_lock(&clp
->cl_lock
);
2034 list_for_each_entry(ses
, &clp
->cl_sessions
, se_perclnt
)
2035 list_del_init(&ses
->se_hash
);
2036 spin_unlock(&clp
->cl_lock
);
2040 unhash_client(struct nfs4_client
*clp
)
2042 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2044 spin_lock(&nn
->client_lock
);
2045 unhash_client_locked(clp
);
2046 spin_unlock(&nn
->client_lock
);
2049 static __be32
mark_client_expired_locked(struct nfs4_client
*clp
)
2051 if (atomic_read(&clp
->cl_rpc_users
))
2052 return nfserr_jukebox
;
2053 unhash_client_locked(clp
);
2058 __destroy_client(struct nfs4_client
*clp
)
2061 struct nfs4_openowner
*oo
;
2062 struct nfs4_delegation
*dp
;
2063 struct list_head reaplist
;
2065 INIT_LIST_HEAD(&reaplist
);
2066 spin_lock(&state_lock
);
2067 while (!list_empty(&clp
->cl_delegations
)) {
2068 dp
= list_entry(clp
->cl_delegations
.next
, struct nfs4_delegation
, dl_perclnt
);
2069 WARN_ON(!unhash_delegation_locked(dp
));
2070 list_add(&dp
->dl_recall_lru
, &reaplist
);
2072 spin_unlock(&state_lock
);
2073 while (!list_empty(&reaplist
)) {
2074 dp
= list_entry(reaplist
.next
, struct nfs4_delegation
, dl_recall_lru
);
2075 list_del_init(&dp
->dl_recall_lru
);
2076 destroy_unhashed_deleg(dp
);
2078 while (!list_empty(&clp
->cl_revoked
)) {
2079 dp
= list_entry(clp
->cl_revoked
.next
, struct nfs4_delegation
, dl_recall_lru
);
2080 list_del_init(&dp
->dl_recall_lru
);
2081 nfs4_put_stid(&dp
->dl_stid
);
2083 while (!list_empty(&clp
->cl_openowners
)) {
2084 oo
= list_entry(clp
->cl_openowners
.next
, struct nfs4_openowner
, oo_perclient
);
2085 nfs4_get_stateowner(&oo
->oo_owner
);
2086 release_openowner(oo
);
2088 for (i
= 0; i
< OWNER_HASH_SIZE
; i
++) {
2089 struct nfs4_stateowner
*so
, *tmp
;
2091 list_for_each_entry_safe(so
, tmp
, &clp
->cl_ownerstr_hashtbl
[i
],
2093 /* Should be no openowners at this point */
2094 WARN_ON_ONCE(so
->so_is_open_owner
);
2095 remove_blocked_locks(lockowner(so
));
2098 nfsd4_return_all_client_layouts(clp
);
2099 nfsd4_shutdown_copy(clp
);
2100 nfsd4_shutdown_callback(clp
);
2101 if (clp
->cl_cb_conn
.cb_xprt
)
2102 svc_xprt_put(clp
->cl_cb_conn
.cb_xprt
);
2104 wake_up_all(&expiry_wq
);
2108 destroy_client(struct nfs4_client
*clp
)
2111 __destroy_client(clp
);
2114 static void inc_reclaim_complete(struct nfs4_client
*clp
)
2116 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2118 if (!nn
->track_reclaim_completes
)
2120 if (!nfsd4_find_reclaim_client(clp
->cl_name
, nn
))
2122 if (atomic_inc_return(&nn
->nr_reclaim_complete
) ==
2123 nn
->reclaim_str_hashtbl_size
) {
2124 printk(KERN_INFO
"NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n",
2126 nfsd4_end_grace(nn
);
2130 static void expire_client(struct nfs4_client
*clp
)
2133 nfsd4_client_record_remove(clp
);
2134 __destroy_client(clp
);
2137 static void copy_verf(struct nfs4_client
*target
, nfs4_verifier
*source
)
2139 memcpy(target
->cl_verifier
.data
, source
->data
,
2140 sizeof(target
->cl_verifier
.data
));
2143 static void copy_clid(struct nfs4_client
*target
, struct nfs4_client
*source
)
2145 target
->cl_clientid
.cl_boot
= source
->cl_clientid
.cl_boot
;
2146 target
->cl_clientid
.cl_id
= source
->cl_clientid
.cl_id
;
2149 static int copy_cred(struct svc_cred
*target
, struct svc_cred
*source
)
2151 target
->cr_principal
= kstrdup(source
->cr_principal
, GFP_KERNEL
);
2152 target
->cr_raw_principal
= kstrdup(source
->cr_raw_principal
,
2154 target
->cr_targ_princ
= kstrdup(source
->cr_targ_princ
, GFP_KERNEL
);
2155 if ((source
->cr_principal
&& !target
->cr_principal
) ||
2156 (source
->cr_raw_principal
&& !target
->cr_raw_principal
) ||
2157 (source
->cr_targ_princ
&& !target
->cr_targ_princ
))
2160 target
->cr_flavor
= source
->cr_flavor
;
2161 target
->cr_uid
= source
->cr_uid
;
2162 target
->cr_gid
= source
->cr_gid
;
2163 target
->cr_group_info
= source
->cr_group_info
;
2164 get_group_info(target
->cr_group_info
);
2165 target
->cr_gss_mech
= source
->cr_gss_mech
;
2166 if (source
->cr_gss_mech
)
2167 gss_mech_get(source
->cr_gss_mech
);
2172 compare_blob(const struct xdr_netobj
*o1
, const struct xdr_netobj
*o2
)
2174 if (o1
->len
< o2
->len
)
2176 if (o1
->len
> o2
->len
)
2178 return memcmp(o1
->data
, o2
->data
, o1
->len
);
2182 same_verf(nfs4_verifier
*v1
, nfs4_verifier
*v2
)
2184 return 0 == memcmp(v1
->data
, v2
->data
, sizeof(v1
->data
));
2188 same_clid(clientid_t
*cl1
, clientid_t
*cl2
)
2190 return (cl1
->cl_boot
== cl2
->cl_boot
) && (cl1
->cl_id
== cl2
->cl_id
);
2193 static bool groups_equal(struct group_info
*g1
, struct group_info
*g2
)
2197 if (g1
->ngroups
!= g2
->ngroups
)
2199 for (i
=0; i
<g1
->ngroups
; i
++)
2200 if (!gid_eq(g1
->gid
[i
], g2
->gid
[i
]))
2206 * RFC 3530 language requires clid_inuse be returned when the
2207 * "principal" associated with a requests differs from that previously
2208 * used. We use uid, gid's, and gss principal string as our best
2209 * approximation. We also don't want to allow non-gss use of a client
2210 * established using gss: in theory cr_principal should catch that
2211 * change, but in practice cr_principal can be null even in the gss case
2212 * since gssd doesn't always pass down a principal string.
2214 static bool is_gss_cred(struct svc_cred
*cr
)
2216 /* Is cr_flavor one of the gss "pseudoflavors"?: */
2217 return (cr
->cr_flavor
> RPC_AUTH_MAXFLAVOR
);
2222 same_creds(struct svc_cred
*cr1
, struct svc_cred
*cr2
)
2224 if ((is_gss_cred(cr1
) != is_gss_cred(cr2
))
2225 || (!uid_eq(cr1
->cr_uid
, cr2
->cr_uid
))
2226 || (!gid_eq(cr1
->cr_gid
, cr2
->cr_gid
))
2227 || !groups_equal(cr1
->cr_group_info
, cr2
->cr_group_info
))
2229 /* XXX: check that cr_targ_princ fields match ? */
2230 if (cr1
->cr_principal
== cr2
->cr_principal
)
2232 if (!cr1
->cr_principal
|| !cr2
->cr_principal
)
2234 return 0 == strcmp(cr1
->cr_principal
, cr2
->cr_principal
);
2237 static bool svc_rqst_integrity_protected(struct svc_rqst
*rqstp
)
2239 struct svc_cred
*cr
= &rqstp
->rq_cred
;
2242 if (!cr
->cr_gss_mech
)
2244 service
= gss_pseudoflavor_to_service(cr
->cr_gss_mech
, cr
->cr_flavor
);
2245 return service
== RPC_GSS_SVC_INTEGRITY
||
2246 service
== RPC_GSS_SVC_PRIVACY
;
2249 bool nfsd4_mach_creds_match(struct nfs4_client
*cl
, struct svc_rqst
*rqstp
)
2251 struct svc_cred
*cr
= &rqstp
->rq_cred
;
2253 if (!cl
->cl_mach_cred
)
2255 if (cl
->cl_cred
.cr_gss_mech
!= cr
->cr_gss_mech
)
2257 if (!svc_rqst_integrity_protected(rqstp
))
2259 if (cl
->cl_cred
.cr_raw_principal
)
2260 return 0 == strcmp(cl
->cl_cred
.cr_raw_principal
,
2261 cr
->cr_raw_principal
);
2262 if (!cr
->cr_principal
)
2264 return 0 == strcmp(cl
->cl_cred
.cr_principal
, cr
->cr_principal
);
2267 static void gen_confirm(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
2272 * This is opaque to client, so no need to byte-swap. Use
2273 * __force to keep sparse happy
2275 verf
[0] = (__force __be32
)(u32
)ktime_get_real_seconds();
2276 verf
[1] = (__force __be32
)nn
->clverifier_counter
++;
2277 memcpy(clp
->cl_confirm
.data
, verf
, sizeof(clp
->cl_confirm
.data
));
2280 static void gen_clid(struct nfs4_client
*clp
, struct nfsd_net
*nn
)
2282 clp
->cl_clientid
.cl_boot
= (u32
)nn
->boot_time
;
2283 clp
->cl_clientid
.cl_id
= nn
->clientid_counter
++;
2284 gen_confirm(clp
, nn
);
2287 static struct nfs4_stid
*
2288 find_stateid_locked(struct nfs4_client
*cl
, stateid_t
*t
)
2290 struct nfs4_stid
*ret
;
2292 ret
= idr_find(&cl
->cl_stateids
, t
->si_opaque
.so_id
);
2293 if (!ret
|| !ret
->sc_type
)
2298 static struct nfs4_stid
*
2299 find_stateid_by_type(struct nfs4_client
*cl
, stateid_t
*t
, char typemask
)
2301 struct nfs4_stid
*s
;
2303 spin_lock(&cl
->cl_lock
);
2304 s
= find_stateid_locked(cl
, t
);
2306 if (typemask
& s
->sc_type
)
2307 refcount_inc(&s
->sc_count
);
2311 spin_unlock(&cl
->cl_lock
);
2315 static struct nfs4_client
*get_nfsdfs_clp(struct inode
*inode
)
2317 struct nfsdfs_client
*nc
;
2318 nc
= get_nfsdfs_client(inode
);
2321 return container_of(nc
, struct nfs4_client
, cl_nfsdfs
);
2324 static void seq_quote_mem(struct seq_file
*m
, char *data
, int len
)
2326 seq_printf(m
, "\"");
2327 seq_escape_mem_ascii(m
, data
, len
);
2328 seq_printf(m
, "\"");
2331 static int client_info_show(struct seq_file
*m
, void *v
)
2333 struct inode
*inode
= m
->private;
2334 struct nfs4_client
*clp
;
2337 clp
= get_nfsdfs_clp(inode
);
2340 memcpy(&clid
, &clp
->cl_clientid
, sizeof(clid
));
2341 seq_printf(m
, "clientid: 0x%llx\n", clid
);
2342 seq_printf(m
, "address: \"%pISpc\"\n", (struct sockaddr
*)&clp
->cl_addr
);
2343 seq_printf(m
, "name: ");
2344 seq_quote_mem(m
, clp
->cl_name
.data
, clp
->cl_name
.len
);
2345 seq_printf(m
, "\nminor version: %d\n", clp
->cl_minorversion
);
2346 if (clp
->cl_nii_domain
.data
) {
2347 seq_printf(m
, "Implementation domain: ");
2348 seq_quote_mem(m
, clp
->cl_nii_domain
.data
,
2349 clp
->cl_nii_domain
.len
);
2350 seq_printf(m
, "\nImplementation name: ");
2351 seq_quote_mem(m
, clp
->cl_nii_name
.data
, clp
->cl_nii_name
.len
);
2352 seq_printf(m
, "\nImplementation time: [%lld, %ld]\n",
2353 clp
->cl_nii_time
.tv_sec
, clp
->cl_nii_time
.tv_nsec
);
2360 static int client_info_open(struct inode
*inode
, struct file
*file
)
2362 return single_open(file
, client_info_show
, inode
);
2365 static const struct file_operations client_info_fops
= {
2366 .open
= client_info_open
,
2368 .llseek
= seq_lseek
,
2369 .release
= single_release
,
2372 static void *states_start(struct seq_file
*s
, loff_t
*pos
)
2373 __acquires(&clp
->cl_lock
)
2375 struct nfs4_client
*clp
= s
->private;
2376 unsigned long id
= *pos
;
2379 spin_lock(&clp
->cl_lock
);
2380 ret
= idr_get_next_ul(&clp
->cl_stateids
, &id
);
2385 static void *states_next(struct seq_file
*s
, void *v
, loff_t
*pos
)
2387 struct nfs4_client
*clp
= s
->private;
2388 unsigned long id
= *pos
;
2393 ret
= idr_get_next_ul(&clp
->cl_stateids
, &id
);
2398 static void states_stop(struct seq_file
*s
, void *v
)
2399 __releases(&clp
->cl_lock
)
2401 struct nfs4_client
*clp
= s
->private;
2403 spin_unlock(&clp
->cl_lock
);
2406 static void nfs4_show_fname(struct seq_file
*s
, struct nfsd_file
*f
)
2408 seq_printf(s
, "filename: \"%pD2\"", f
->nf_file
);
2411 static void nfs4_show_superblock(struct seq_file
*s
, struct nfsd_file
*f
)
2413 struct inode
*inode
= f
->nf_inode
;
2415 seq_printf(s
, "superblock: \"%02x:%02x:%ld\"",
2416 MAJOR(inode
->i_sb
->s_dev
),
2417 MINOR(inode
->i_sb
->s_dev
),
2421 static void nfs4_show_owner(struct seq_file
*s
, struct nfs4_stateowner
*oo
)
2423 seq_printf(s
, "owner: ");
2424 seq_quote_mem(s
, oo
->so_owner
.data
, oo
->so_owner
.len
);
2427 static void nfs4_show_stateid(struct seq_file
*s
, stateid_t
*stid
)
2429 seq_printf(s
, "0x%.8x", stid
->si_generation
);
2430 seq_printf(s
, "%12phN", &stid
->si_opaque
);
2433 static int nfs4_show_open(struct seq_file
*s
, struct nfs4_stid
*st
)
2435 struct nfs4_ol_stateid
*ols
;
2436 struct nfs4_file
*nf
;
2437 struct nfsd_file
*file
;
2438 struct nfs4_stateowner
*oo
;
2439 unsigned int access
, deny
;
2441 if (st
->sc_type
!= NFS4_OPEN_STID
&& st
->sc_type
!= NFS4_LOCK_STID
)
2442 return 0; /* XXX: or SEQ_SKIP? */
2443 ols
= openlockstateid(st
);
2444 oo
= ols
->st_stateowner
;
2446 file
= find_any_file(nf
);
2448 seq_printf(s
, "- ");
2449 nfs4_show_stateid(s
, &st
->sc_stateid
);
2450 seq_printf(s
, ": { type: open, ");
2452 access
= bmap_to_share_mode(ols
->st_access_bmap
);
2453 deny
= bmap_to_share_mode(ols
->st_deny_bmap
);
2455 seq_printf(s
, "access: %s%s, ",
2456 access
& NFS4_SHARE_ACCESS_READ
? "r" : "-",
2457 access
& NFS4_SHARE_ACCESS_WRITE
? "w" : "-");
2458 seq_printf(s
, "deny: %s%s, ",
2459 deny
& NFS4_SHARE_ACCESS_READ
? "r" : "-",
2460 deny
& NFS4_SHARE_ACCESS_WRITE
? "w" : "-");
2462 nfs4_show_superblock(s
, file
);
2463 seq_printf(s
, ", ");
2464 nfs4_show_fname(s
, file
);
2465 seq_printf(s
, ", ");
2466 nfs4_show_owner(s
, oo
);
2467 seq_printf(s
, " }\n");
2468 nfsd_file_put(file
);
2473 static int nfs4_show_lock(struct seq_file
*s
, struct nfs4_stid
*st
)
2475 struct nfs4_ol_stateid
*ols
;
2476 struct nfs4_file
*nf
;
2477 struct nfsd_file
*file
;
2478 struct nfs4_stateowner
*oo
;
2480 ols
= openlockstateid(st
);
2481 oo
= ols
->st_stateowner
;
2483 file
= find_any_file(nf
);
2485 seq_printf(s
, "- ");
2486 nfs4_show_stateid(s
, &st
->sc_stateid
);
2487 seq_printf(s
, ": { type: lock, ");
2490 * Note: a lock stateid isn't really the same thing as a lock,
2491 * it's the locking state held by one owner on a file, and there
2492 * may be multiple (or no) lock ranges associated with it.
2493 * (Same for the matter is true of open stateids.)
2496 nfs4_show_superblock(s
, file
);
2497 /* XXX: open stateid? */
2498 seq_printf(s
, ", ");
2499 nfs4_show_fname(s
, file
);
2500 seq_printf(s
, ", ");
2501 nfs4_show_owner(s
, oo
);
2502 seq_printf(s
, " }\n");
2503 nfsd_file_put(file
);
2508 static int nfs4_show_deleg(struct seq_file
*s
, struct nfs4_stid
*st
)
2510 struct nfs4_delegation
*ds
;
2511 struct nfs4_file
*nf
;
2512 struct nfsd_file
*file
;
2514 ds
= delegstateid(st
);
2516 file
= nf
->fi_deleg_file
;
2518 seq_printf(s
, "- ");
2519 nfs4_show_stateid(s
, &st
->sc_stateid
);
2520 seq_printf(s
, ": { type: deleg, ");
2522 /* Kinda dead code as long as we only support read delegs: */
2523 seq_printf(s
, "access: %s, ",
2524 ds
->dl_type
== NFS4_OPEN_DELEGATE_READ
? "r" : "w");
2526 /* XXX: lease time, whether it's being recalled. */
2528 nfs4_show_superblock(s
, file
);
2529 seq_printf(s
, ", ");
2530 nfs4_show_fname(s
, file
);
2531 seq_printf(s
, " }\n");
2536 static int nfs4_show_layout(struct seq_file
*s
, struct nfs4_stid
*st
)
2538 struct nfs4_layout_stateid
*ls
;
2539 struct nfsd_file
*file
;
2541 ls
= container_of(st
, struct nfs4_layout_stateid
, ls_stid
);
2544 seq_printf(s
, "- ");
2545 nfs4_show_stateid(s
, &st
->sc_stateid
);
2546 seq_printf(s
, ": { type: layout, ");
2548 /* XXX: What else would be useful? */
2550 nfs4_show_superblock(s
, file
);
2551 seq_printf(s
, ", ");
2552 nfs4_show_fname(s
, file
);
2553 seq_printf(s
, " }\n");
2558 static int states_show(struct seq_file
*s
, void *v
)
2560 struct nfs4_stid
*st
= v
;
2562 switch (st
->sc_type
) {
2563 case NFS4_OPEN_STID
:
2564 return nfs4_show_open(s
, st
);
2565 case NFS4_LOCK_STID
:
2566 return nfs4_show_lock(s
, st
);
2567 case NFS4_DELEG_STID
:
2568 return nfs4_show_deleg(s
, st
);
2569 case NFS4_LAYOUT_STID
:
2570 return nfs4_show_layout(s
, st
);
2572 return 0; /* XXX: or SEQ_SKIP? */
2574 /* XXX: copy stateids? */
2577 static struct seq_operations states_seq_ops
= {
2578 .start
= states_start
,
2579 .next
= states_next
,
2580 .stop
= states_stop
,
2584 static int client_states_open(struct inode
*inode
, struct file
*file
)
2587 struct nfs4_client
*clp
;
2590 clp
= get_nfsdfs_clp(inode
);
2594 ret
= seq_open(file
, &states_seq_ops
);
2597 s
= file
->private_data
;
2602 static int client_opens_release(struct inode
*inode
, struct file
*file
)
2604 struct seq_file
*m
= file
->private_data
;
2605 struct nfs4_client
*clp
= m
->private;
2607 /* XXX: alternatively, we could get/drop in seq start/stop */
2612 static const struct file_operations client_states_fops
= {
2613 .open
= client_states_open
,
2615 .llseek
= seq_lseek
,
2616 .release
= client_opens_release
,
2620 * Normally we refuse to destroy clients that are in use, but here the
2621 * administrator is telling us to just do it. We also want to wait
2622 * so the caller has a guarantee that the client's locks are gone by
2623 * the time the write returns:
2625 static void force_expire_client(struct nfs4_client
*clp
)
2627 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2628 bool already_expired
;
2630 spin_lock(&clp
->cl_lock
);
2632 spin_unlock(&clp
->cl_lock
);
2634 wait_event(expiry_wq
, atomic_read(&clp
->cl_rpc_users
) == 0);
2635 spin_lock(&nn
->client_lock
);
2636 already_expired
= list_empty(&clp
->cl_lru
);
2637 if (!already_expired
)
2638 unhash_client_locked(clp
);
2639 spin_unlock(&nn
->client_lock
);
2641 if (!already_expired
)
2644 wait_event(expiry_wq
, clp
->cl_nfsd_dentry
== NULL
);
2647 static ssize_t
client_ctl_write(struct file
*file
, const char __user
*buf
,
2648 size_t size
, loff_t
*pos
)
2651 struct nfs4_client
*clp
;
2653 data
= simple_transaction_get(file
, buf
, size
);
2655 return PTR_ERR(data
);
2656 if (size
!= 7 || 0 != memcmp(data
, "expire\n", 7))
2658 clp
= get_nfsdfs_clp(file_inode(file
));
2661 force_expire_client(clp
);
2666 static const struct file_operations client_ctl_fops
= {
2667 .write
= client_ctl_write
,
2668 .release
= simple_transaction_release
,
2671 static const struct tree_descr client_files
[] = {
2672 [0] = {"info", &client_info_fops
, S_IRUSR
},
2673 [1] = {"states", &client_states_fops
, S_IRUSR
},
2674 [2] = {"ctl", &client_ctl_fops
, S_IWUSR
},
2678 static struct nfs4_client
*create_client(struct xdr_netobj name
,
2679 struct svc_rqst
*rqstp
, nfs4_verifier
*verf
)
2681 struct nfs4_client
*clp
;
2682 struct sockaddr
*sa
= svc_addr(rqstp
);
2684 struct net
*net
= SVC_NET(rqstp
);
2685 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
2687 clp
= alloc_client(name
);
2691 ret
= copy_cred(&clp
->cl_cred
, &rqstp
->rq_cred
);
2697 kref_init(&clp
->cl_nfsdfs
.cl_ref
);
2698 nfsd4_init_cb(&clp
->cl_cb_null
, clp
, NULL
, NFSPROC4_CLNT_CB_NULL
);
2699 clp
->cl_time
= ktime_get_boottime_seconds();
2700 clear_bit(0, &clp
->cl_cb_slot_busy
);
2701 copy_verf(clp
, verf
);
2702 memcpy(&clp
->cl_addr
, sa
, sizeof(struct sockaddr_storage
));
2703 clp
->cl_cb_session
= NULL
;
2705 clp
->cl_nfsd_dentry
= nfsd_client_mkdir(nn
, &clp
->cl_nfsdfs
,
2706 clp
->cl_clientid
.cl_id
- nn
->clientid_base
,
2708 if (!clp
->cl_nfsd_dentry
) {
2716 add_clp_to_name_tree(struct nfs4_client
*new_clp
, struct rb_root
*root
)
2718 struct rb_node
**new = &(root
->rb_node
), *parent
= NULL
;
2719 struct nfs4_client
*clp
;
2722 clp
= rb_entry(*new, struct nfs4_client
, cl_namenode
);
2725 if (compare_blob(&clp
->cl_name
, &new_clp
->cl_name
) > 0)
2726 new = &((*new)->rb_left
);
2728 new = &((*new)->rb_right
);
2731 rb_link_node(&new_clp
->cl_namenode
, parent
, new);
2732 rb_insert_color(&new_clp
->cl_namenode
, root
);
2735 static struct nfs4_client
*
2736 find_clp_in_name_tree(struct xdr_netobj
*name
, struct rb_root
*root
)
2739 struct rb_node
*node
= root
->rb_node
;
2740 struct nfs4_client
*clp
;
2743 clp
= rb_entry(node
, struct nfs4_client
, cl_namenode
);
2744 cmp
= compare_blob(&clp
->cl_name
, name
);
2746 node
= node
->rb_left
;
2748 node
= node
->rb_right
;
2756 add_to_unconfirmed(struct nfs4_client
*clp
)
2758 unsigned int idhashval
;
2759 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2761 lockdep_assert_held(&nn
->client_lock
);
2763 clear_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2764 add_clp_to_name_tree(clp
, &nn
->unconf_name_tree
);
2765 idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2766 list_add(&clp
->cl_idhash
, &nn
->unconf_id_hashtbl
[idhashval
]);
2767 renew_client_locked(clp
);
2771 move_to_confirmed(struct nfs4_client
*clp
)
2773 unsigned int idhashval
= clientid_hashval(clp
->cl_clientid
.cl_id
);
2774 struct nfsd_net
*nn
= net_generic(clp
->net
, nfsd_net_id
);
2776 lockdep_assert_held(&nn
->client_lock
);
2778 dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp
);
2779 list_move(&clp
->cl_idhash
, &nn
->conf_id_hashtbl
[idhashval
]);
2780 rb_erase(&clp
->cl_namenode
, &nn
->unconf_name_tree
);
2781 add_clp_to_name_tree(clp
, &nn
->conf_name_tree
);
2782 set_bit(NFSD4_CLIENT_CONFIRMED
, &clp
->cl_flags
);
2783 renew_client_locked(clp
);
2786 static struct nfs4_client
*
2787 find_client_in_id_table(struct list_head
*tbl
, clientid_t
*clid
, bool sessions
)
2789 struct nfs4_client
*clp
;
2790 unsigned int idhashval
= clientid_hashval(clid
->cl_id
);
2792 list_for_each_entry(clp
, &tbl
[idhashval
], cl_idhash
) {
2793 if (same_clid(&clp
->cl_clientid
, clid
)) {
2794 if ((bool)clp
->cl_minorversion
!= sessions
)
2796 renew_client_locked(clp
);
2803 static struct nfs4_client
*
2804 find_confirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2806 struct list_head
*tbl
= nn
->conf_id_hashtbl
;
2808 lockdep_assert_held(&nn
->client_lock
);
2809 return find_client_in_id_table(tbl
, clid
, sessions
);
2812 static struct nfs4_client
*
2813 find_unconfirmed_client(clientid_t
*clid
, bool sessions
, struct nfsd_net
*nn
)
2815 struct list_head
*tbl
= nn
->unconf_id_hashtbl
;
2817 lockdep_assert_held(&nn
->client_lock
);
2818 return find_client_in_id_table(tbl
, clid
, sessions
);
2821 static bool clp_used_exchangeid(struct nfs4_client
*clp
)
2823 return clp
->cl_exchange_flags
!= 0;
2826 static struct nfs4_client
*
2827 find_confirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2829 lockdep_assert_held(&nn
->client_lock
);
2830 return find_clp_in_name_tree(name
, &nn
->conf_name_tree
);
2833 static struct nfs4_client
*
2834 find_unconfirmed_client_by_name(struct xdr_netobj
*name
, struct nfsd_net
*nn
)
2836 lockdep_assert_held(&nn
->client_lock
);
2837 return find_clp_in_name_tree(name
, &nn
->unconf_name_tree
);
2841 gen_callback(struct nfs4_client
*clp
, struct nfsd4_setclientid
*se
, struct svc_rqst
*rqstp
)
2843 struct nfs4_cb_conn
*conn
= &clp
->cl_cb_conn
;
2844 struct sockaddr
*sa
= svc_addr(rqstp
);
2845 u32 scopeid
= rpc_get_scope_id(sa
);
2846 unsigned short expected_family
;
2848 /* Currently, we only support tcp and tcp6 for the callback channel */
2849 if (se
->se_callback_netid_len
== 3 &&
2850 !memcmp(se
->se_callback_netid_val
, "tcp", 3))
2851 expected_family
= AF_INET
;
2852 else if (se
->se_callback_netid_len
== 4 &&
2853 !memcmp(se
->se_callback_netid_val
, "tcp6", 4))
2854 expected_family
= AF_INET6
;
2858 conn
->cb_addrlen
= rpc_uaddr2sockaddr(clp
->net
, se
->se_callback_addr_val
,
2859 se
->se_callback_addr_len
,
2860 (struct sockaddr
*)&conn
->cb_addr
,
2861 sizeof(conn
->cb_addr
));
2863 if (!conn
->cb_addrlen
|| conn
->cb_addr
.ss_family
!= expected_family
)
2866 if (conn
->cb_addr
.ss_family
== AF_INET6
)
2867 ((struct sockaddr_in6
*)&conn
->cb_addr
)->sin6_scope_id
= scopeid
;
2869 conn
->cb_prog
= se
->se_callback_prog
;
2870 conn
->cb_ident
= se
->se_callback_ident
;
2871 memcpy(&conn
->cb_saddr
, &rqstp
->rq_daddr
, rqstp
->rq_daddrlen
);
2872 trace_nfsd_cb_args(clp
, conn
);
2875 conn
->cb_addr
.ss_family
= AF_UNSPEC
;
2876 conn
->cb_addrlen
= 0;
2877 trace_nfsd_cb_nodelegs(clp
);
2882 * Cache a reply. nfsd4_check_resp_size() has bounded the cache size.
2885 nfsd4_store_cache_entry(struct nfsd4_compoundres
*resp
)
2887 struct xdr_buf
*buf
= resp
->xdr
.buf
;
2888 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2891 dprintk("--> %s slot %p\n", __func__
, slot
);
2893 slot
->sl_flags
|= NFSD4_SLOT_INITIALIZED
;
2894 slot
->sl_opcnt
= resp
->opcnt
;
2895 slot
->sl_status
= resp
->cstate
.status
;
2896 free_svc_cred(&slot
->sl_cred
);
2897 copy_cred(&slot
->sl_cred
, &resp
->rqstp
->rq_cred
);
2899 if (!nfsd4_cache_this(resp
)) {
2900 slot
->sl_flags
&= ~NFSD4_SLOT_CACHED
;
2903 slot
->sl_flags
|= NFSD4_SLOT_CACHED
;
2905 base
= resp
->cstate
.data_offset
;
2906 slot
->sl_datalen
= buf
->len
- base
;
2907 if (read_bytes_from_xdr_buf(buf
, base
, slot
->sl_data
, slot
->sl_datalen
))
2908 WARN(1, "%s: sessions DRC could not cache compound\n",
2914 * Encode the replay sequence operation from the slot values.
2915 * If cachethis is FALSE encode the uncached rep error on the next
2916 * operation which sets resp->p and increments resp->opcnt for
2917 * nfs4svc_encode_compoundres.
2921 nfsd4_enc_sequence_replay(struct nfsd4_compoundargs
*args
,
2922 struct nfsd4_compoundres
*resp
)
2924 struct nfsd4_op
*op
;
2925 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2927 /* Encode the replayed sequence operation */
2928 op
= &args
->ops
[resp
->opcnt
- 1];
2929 nfsd4_encode_operation(resp
, op
);
2931 if (slot
->sl_flags
& NFSD4_SLOT_CACHED
)
2933 if (args
->opcnt
== 1) {
2935 * The original operation wasn't a solo sequence--we
2936 * always cache those--so this retry must not match the
2939 op
->status
= nfserr_seq_false_retry
;
2941 op
= &args
->ops
[resp
->opcnt
++];
2942 op
->status
= nfserr_retry_uncached_rep
;
2943 nfsd4_encode_operation(resp
, op
);
2949 * The sequence operation is not cached because we can use the slot and
2953 nfsd4_replay_cache_entry(struct nfsd4_compoundres
*resp
,
2954 struct nfsd4_sequence
*seq
)
2956 struct nfsd4_slot
*slot
= resp
->cstate
.slot
;
2957 struct xdr_stream
*xdr
= &resp
->xdr
;
2961 dprintk("--> %s slot %p\n", __func__
, slot
);
2963 status
= nfsd4_enc_sequence_replay(resp
->rqstp
->rq_argp
, resp
);
2967 p
= xdr_reserve_space(xdr
, slot
->sl_datalen
);
2970 return nfserr_serverfault
;
2972 xdr_encode_opaque_fixed(p
, slot
->sl_data
, slot
->sl_datalen
);
2973 xdr_commit_encode(xdr
);
2975 resp
->opcnt
= slot
->sl_opcnt
;
2976 return slot
->sl_status
;
2980 * Set the exchange_id flags returned by the server.
2983 nfsd4_set_ex_flags(struct nfs4_client
*new, struct nfsd4_exchange_id
*clid
)
2985 #ifdef CONFIG_NFSD_PNFS
2986 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_PNFS_MDS
;
2988 new->cl_exchange_flags
|= EXCHGID4_FLAG_USE_NON_PNFS
;
2991 /* Referrals are supported, Migration is not. */
2992 new->cl_exchange_flags
|= EXCHGID4_FLAG_SUPP_MOVED_REFER
;
2994 /* set the wire flags to return to client. */
2995 clid
->flags
= new->cl_exchange_flags
;
2998 static bool client_has_openowners(struct nfs4_client
*clp
)
3000 struct nfs4_openowner
*oo
;
3002 list_for_each_entry(oo
, &clp
->cl_openowners
, oo_perclient
) {
3003 if (!list_empty(&oo
->oo_owner
.so_stateids
))
3009 static bool client_has_state(struct nfs4_client
*clp
)
3011 return client_has_openowners(clp
)
3012 #ifdef CONFIG_NFSD_PNFS
3013 || !list_empty(&clp
->cl_lo_states
)
3015 || !list_empty(&clp
->cl_delegations
)
3016 || !list_empty(&clp
->cl_sessions
)
3017 || !list_empty(&clp
->async_copies
);
3020 static __be32
copy_impl_id(struct nfs4_client
*clp
,
3021 struct nfsd4_exchange_id
*exid
)
3023 if (!exid
->nii_domain
.data
)
3025 xdr_netobj_dup(&clp
->cl_nii_domain
, &exid
->nii_domain
, GFP_KERNEL
);
3026 if (!clp
->cl_nii_domain
.data
)
3027 return nfserr_jukebox
;
3028 xdr_netobj_dup(&clp
->cl_nii_name
, &exid
->nii_name
, GFP_KERNEL
);
3029 if (!clp
->cl_nii_name
.data
)
3030 return nfserr_jukebox
;
3031 clp
->cl_nii_time
= exid
->nii_time
;
3036 nfsd4_exchange_id(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3037 union nfsd4_op_u
*u
)
3039 struct nfsd4_exchange_id
*exid
= &u
->exchange_id
;
3040 struct nfs4_client
*conf
, *new;
3041 struct nfs4_client
*unconf
= NULL
;
3043 char addr_str
[INET6_ADDRSTRLEN
];
3044 nfs4_verifier verf
= exid
->verifier
;
3045 struct sockaddr
*sa
= svc_addr(rqstp
);
3046 bool update
= exid
->flags
& EXCHGID4_FLAG_UPD_CONFIRMED_REC_A
;
3047 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3049 rpc_ntop(sa
, addr_str
, sizeof(addr_str
));
3050 dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p "
3051 "ip_addr=%s flags %x, spa_how %d\n",
3052 __func__
, rqstp
, exid
, exid
->clname
.len
, exid
->clname
.data
,
3053 addr_str
, exid
->flags
, exid
->spa_how
);
3055 if (exid
->flags
& ~EXCHGID4_FLAG_MASK_A
)
3056 return nfserr_inval
;
3058 new = create_client(exid
->clname
, rqstp
, &verf
);
3060 return nfserr_jukebox
;
3061 status
= copy_impl_id(new, exid
);
3065 switch (exid
->spa_how
) {
3067 exid
->spo_must_enforce
[0] = 0;
3068 exid
->spo_must_enforce
[1] = (
3069 1 << (OP_BIND_CONN_TO_SESSION
- 32) |
3070 1 << (OP_EXCHANGE_ID
- 32) |
3071 1 << (OP_CREATE_SESSION
- 32) |
3072 1 << (OP_DESTROY_SESSION
- 32) |
3073 1 << (OP_DESTROY_CLIENTID
- 32));
3075 exid
->spo_must_allow
[0] &= (1 << (OP_CLOSE
) |
3076 1 << (OP_OPEN_DOWNGRADE
) |
3078 1 << (OP_DELEGRETURN
));
3080 exid
->spo_must_allow
[1] &= (
3081 1 << (OP_TEST_STATEID
- 32) |
3082 1 << (OP_FREE_STATEID
- 32));
3083 if (!svc_rqst_integrity_protected(rqstp
)) {
3084 status
= nfserr_inval
;
3088 * Sometimes userspace doesn't give us a principal.
3089 * Which is a bug, really. Anyway, we can't enforce
3090 * MACH_CRED in that case, better to give up now:
3092 if (!new->cl_cred
.cr_principal
&&
3093 !new->cl_cred
.cr_raw_principal
) {
3094 status
= nfserr_serverfault
;
3097 new->cl_mach_cred
= true;
3100 default: /* checked by xdr code */
3104 status
= nfserr_encr_alg_unsupp
;
3108 /* Cases below refer to rfc 5661 section 18.35.4: */
3109 spin_lock(&nn
->client_lock
);
3110 conf
= find_confirmed_client_by_name(&exid
->clname
, nn
);
3112 bool creds_match
= same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
);
3113 bool verfs_match
= same_verf(&verf
, &conf
->cl_verifier
);
3116 if (!clp_used_exchangeid(conf
)) { /* buggy client */
3117 status
= nfserr_inval
;
3120 if (!nfsd4_mach_creds_match(conf
, rqstp
)) {
3121 status
= nfserr_wrong_cred
;
3124 if (!creds_match
) { /* case 9 */
3125 status
= nfserr_perm
;
3128 if (!verfs_match
) { /* case 8 */
3129 status
= nfserr_not_same
;
3133 exid
->flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
3136 if (!creds_match
) { /* case 3 */
3137 if (client_has_state(conf
)) {
3138 status
= nfserr_clid_inuse
;
3143 if (verfs_match
) { /* case 2 */
3144 conf
->cl_exchange_flags
|= EXCHGID4_FLAG_CONFIRMED_R
;
3147 /* case 5, client reboot */
3152 if (update
) { /* case 7 */
3153 status
= nfserr_noent
;
3157 unconf
= find_unconfirmed_client_by_name(&exid
->clname
, nn
);
3158 if (unconf
) /* case 4, possible retry or client restart */
3159 unhash_client_locked(unconf
);
3161 /* case 1 (normal case) */
3164 status
= mark_client_expired_locked(conf
);
3168 new->cl_minorversion
= cstate
->minorversion
;
3169 new->cl_spo_must_allow
.u
.words
[0] = exid
->spo_must_allow
[0];
3170 new->cl_spo_must_allow
.u
.words
[1] = exid
->spo_must_allow
[1];
3172 add_to_unconfirmed(new);
3175 exid
->clientid
.cl_boot
= conf
->cl_clientid
.cl_boot
;
3176 exid
->clientid
.cl_id
= conf
->cl_clientid
.cl_id
;
3178 exid
->seqid
= conf
->cl_cs_slot
.sl_seqid
+ 1;
3179 nfsd4_set_ex_flags(conf
, exid
);
3181 dprintk("nfsd4_exchange_id seqid %d flags %x\n",
3182 conf
->cl_cs_slot
.sl_seqid
, conf
->cl_exchange_flags
);
3186 spin_unlock(&nn
->client_lock
);
3191 expire_client(unconf
);
3196 check_slot_seqid(u32 seqid
, u32 slot_seqid
, int slot_inuse
)
3198 dprintk("%s enter. seqid %d slot_seqid %d\n", __func__
, seqid
,
3201 /* The slot is in use, and no response has been sent. */
3203 if (seqid
== slot_seqid
)
3204 return nfserr_jukebox
;
3206 return nfserr_seq_misordered
;
3208 /* Note unsigned 32-bit arithmetic handles wraparound: */
3209 if (likely(seqid
== slot_seqid
+ 1))
3211 if (seqid
== slot_seqid
)
3212 return nfserr_replay_cache
;
3213 return nfserr_seq_misordered
;
3217 * Cache the create session result into the create session single DRC
3218 * slot cache by saving the xdr structure. sl_seqid has been set.
3219 * Do this for solo or embedded create session operations.
3222 nfsd4_cache_create_session(struct nfsd4_create_session
*cr_ses
,
3223 struct nfsd4_clid_slot
*slot
, __be32 nfserr
)
3225 slot
->sl_status
= nfserr
;
3226 memcpy(&slot
->sl_cr_ses
, cr_ses
, sizeof(*cr_ses
));
3230 nfsd4_replay_create_session(struct nfsd4_create_session
*cr_ses
,
3231 struct nfsd4_clid_slot
*slot
)
3233 memcpy(cr_ses
, &slot
->sl_cr_ses
, sizeof(*cr_ses
));
3234 return slot
->sl_status
;
3237 #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\
3238 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \
3239 1 + /* MIN tag is length with zero, only length */ \
3240 3 + /* version, opcount, opcode */ \
3241 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3242 /* seqid, slotID, slotID, cache */ \
3243 4 ) * sizeof(__be32))
3245 #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\
3246 2 + /* verifier: AUTH_NULL, length 0 */\
3248 1 + /* MIN tag is length with zero, only length */ \
3249 3 + /* opcount, opcode, opstatus*/ \
3250 XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \
3251 /* seqid, slotID, slotID, slotID, status */ \
3252 5 ) * sizeof(__be32))
3254 static __be32
check_forechannel_attrs(struct nfsd4_channel_attrs
*ca
, struct nfsd_net
*nn
)
3256 u32 maxrpc
= nn
->nfsd_serv
->sv_max_mesg
;
3258 if (ca
->maxreq_sz
< NFSD_MIN_REQ_HDR_SEQ_SZ
)
3259 return nfserr_toosmall
;
3260 if (ca
->maxresp_sz
< NFSD_MIN_RESP_HDR_SEQ_SZ
)
3261 return nfserr_toosmall
;
3262 ca
->headerpadsz
= 0;
3263 ca
->maxreq_sz
= min_t(u32
, ca
->maxreq_sz
, maxrpc
);
3264 ca
->maxresp_sz
= min_t(u32
, ca
->maxresp_sz
, maxrpc
);
3265 ca
->maxops
= min_t(u32
, ca
->maxops
, NFSD_MAX_OPS_PER_COMPOUND
);
3266 ca
->maxresp_cached
= min_t(u32
, ca
->maxresp_cached
,
3267 NFSD_SLOT_CACHE_SIZE
+ NFSD_MIN_HDR_SEQ_SZ
);
3268 ca
->maxreqs
= min_t(u32
, ca
->maxreqs
, NFSD_MAX_SLOTS_PER_SESSION
);
3270 * Note decreasing slot size below client's request may make it
3271 * difficult for client to function correctly, whereas
3272 * decreasing the number of slots will (just?) affect
3273 * performance. When short on memory we therefore prefer to
3274 * decrease number of slots instead of their size. Clients that
3275 * request larger slots than they need will get poor results:
3276 * Note that we always allow at least one slot, because our
3277 * accounting is soft and provides no guarantees either way.
3279 ca
->maxreqs
= nfsd4_get_drc_mem(ca
, nn
);
3285 * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now.
3286 * These are based on similar macros in linux/sunrpc/msg_prot.h .
3288 #define RPC_MAX_HEADER_WITH_AUTH_SYS \
3289 (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK))
3291 #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \
3292 (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK))
3294 #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \
3295 RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32))
3296 #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \
3297 RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \
3300 static __be32
check_backchannel_attrs(struct nfsd4_channel_attrs
*ca
)
3302 ca
->headerpadsz
= 0;
3304 if (ca
->maxreq_sz
< NFSD_CB_MAX_REQ_SZ
)
3305 return nfserr_toosmall
;
3306 if (ca
->maxresp_sz
< NFSD_CB_MAX_RESP_SZ
)
3307 return nfserr_toosmall
;
3308 ca
->maxresp_cached
= 0;
3310 return nfserr_toosmall
;
3315 static __be32
nfsd4_check_cb_sec(struct nfsd4_cb_sec
*cbs
)
3317 switch (cbs
->flavor
) {
3323 * GSS case: the spec doesn't allow us to return this
3324 * error. But it also doesn't allow us not to support
3326 * I'd rather this fail hard than return some error the
3327 * client might think it can already handle:
3329 return nfserr_encr_alg_unsupp
;
3334 nfsd4_create_session(struct svc_rqst
*rqstp
,
3335 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
3337 struct nfsd4_create_session
*cr_ses
= &u
->create_session
;
3338 struct sockaddr
*sa
= svc_addr(rqstp
);
3339 struct nfs4_client
*conf
, *unconf
;
3340 struct nfs4_client
*old
= NULL
;
3341 struct nfsd4_session
*new;
3342 struct nfsd4_conn
*conn
;
3343 struct nfsd4_clid_slot
*cs_slot
= NULL
;
3345 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3347 if (cr_ses
->flags
& ~SESSION4_FLAG_MASK_A
)
3348 return nfserr_inval
;
3349 status
= nfsd4_check_cb_sec(&cr_ses
->cb_sec
);
3352 status
= check_forechannel_attrs(&cr_ses
->fore_channel
, nn
);
3355 status
= check_backchannel_attrs(&cr_ses
->back_channel
);
3357 goto out_release_drc_mem
;
3358 status
= nfserr_jukebox
;
3359 new = alloc_session(&cr_ses
->fore_channel
, &cr_ses
->back_channel
);
3361 goto out_release_drc_mem
;
3362 conn
= alloc_conn_from_crses(rqstp
, cr_ses
);
3364 goto out_free_session
;
3366 spin_lock(&nn
->client_lock
);
3367 unconf
= find_unconfirmed_client(&cr_ses
->clientid
, true, nn
);
3368 conf
= find_confirmed_client(&cr_ses
->clientid
, true, nn
);
3369 WARN_ON_ONCE(conf
&& unconf
);
3372 status
= nfserr_wrong_cred
;
3373 if (!nfsd4_mach_creds_match(conf
, rqstp
))
3375 cs_slot
= &conf
->cl_cs_slot
;
3376 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
3378 if (status
== nfserr_replay_cache
)
3379 status
= nfsd4_replay_create_session(cr_ses
, cs_slot
);
3382 } else if (unconf
) {
3383 if (!same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
) ||
3384 !rpc_cmp_addr(sa
, (struct sockaddr
*) &unconf
->cl_addr
)) {
3385 status
= nfserr_clid_inuse
;
3388 status
= nfserr_wrong_cred
;
3389 if (!nfsd4_mach_creds_match(unconf
, rqstp
))
3391 cs_slot
= &unconf
->cl_cs_slot
;
3392 status
= check_slot_seqid(cr_ses
->seqid
, cs_slot
->sl_seqid
, 0);
3394 /* an unconfirmed replay returns misordered */
3395 status
= nfserr_seq_misordered
;
3398 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
3400 status
= mark_client_expired_locked(old
);
3406 move_to_confirmed(unconf
);
3409 status
= nfserr_stale_clientid
;
3413 /* Persistent sessions are not supported */
3414 cr_ses
->flags
&= ~SESSION4_PERSIST
;
3415 /* Upshifting from TCP to RDMA is not supported */
3416 cr_ses
->flags
&= ~SESSION4_RDMA
;
3418 init_session(rqstp
, new, conf
, cr_ses
);
3419 nfsd4_get_session_locked(new);
3421 memcpy(cr_ses
->sessionid
.data
, new->se_sessionid
.data
,
3422 NFS4_MAX_SESSIONID_LEN
);
3423 cs_slot
->sl_seqid
++;
3424 cr_ses
->seqid
= cs_slot
->sl_seqid
;
3426 /* cache solo and embedded create sessions under the client_lock */
3427 nfsd4_cache_create_session(cr_ses
, cs_slot
, status
);
3428 spin_unlock(&nn
->client_lock
);
3429 /* init connection and backchannel */
3430 nfsd4_init_conn(rqstp
, conn
, new);
3431 nfsd4_put_session(new);
3436 spin_unlock(&nn
->client_lock
);
3441 __free_session(new);
3442 out_release_drc_mem
:
3443 nfsd4_put_drc_mem(&cr_ses
->fore_channel
);
3447 static __be32
nfsd4_map_bcts_dir(u32
*dir
)
3450 case NFS4_CDFC4_FORE
:
3451 case NFS4_CDFC4_BACK
:
3453 case NFS4_CDFC4_FORE_OR_BOTH
:
3454 case NFS4_CDFC4_BACK_OR_BOTH
:
3455 *dir
= NFS4_CDFC4_BOTH
;
3458 return nfserr_inval
;
3461 __be32
nfsd4_backchannel_ctl(struct svc_rqst
*rqstp
,
3462 struct nfsd4_compound_state
*cstate
,
3463 union nfsd4_op_u
*u
)
3465 struct nfsd4_backchannel_ctl
*bc
= &u
->backchannel_ctl
;
3466 struct nfsd4_session
*session
= cstate
->session
;
3467 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3470 status
= nfsd4_check_cb_sec(&bc
->bc_cb_sec
);
3473 spin_lock(&nn
->client_lock
);
3474 session
->se_cb_prog
= bc
->bc_cb_program
;
3475 session
->se_cb_sec
= bc
->bc_cb_sec
;
3476 spin_unlock(&nn
->client_lock
);
3478 nfsd4_probe_callback(session
->se_client
);
3483 static struct nfsd4_conn
*__nfsd4_find_conn(struct svc_xprt
*xpt
, struct nfsd4_session
*s
)
3485 struct nfsd4_conn
*c
;
3487 list_for_each_entry(c
, &s
->se_conns
, cn_persession
) {
3488 if (c
->cn_xprt
== xpt
) {
3495 static __be32
nfsd4_match_existing_connection(struct svc_rqst
*rqst
,
3496 struct nfsd4_session
*session
, u32 req
)
3498 struct nfs4_client
*clp
= session
->se_client
;
3499 struct svc_xprt
*xpt
= rqst
->rq_xprt
;
3500 struct nfsd4_conn
*c
;
3503 /* Following the last paragraph of RFC 5661 Section 18.34.3: */
3504 spin_lock(&clp
->cl_lock
);
3505 c
= __nfsd4_find_conn(xpt
, session
);
3507 status
= nfserr_noent
;
3508 else if (req
== c
->cn_flags
)
3510 else if (req
== NFS4_CDFC4_FORE_OR_BOTH
&&
3511 c
->cn_flags
!= NFS4_CDFC4_BACK
)
3513 else if (req
== NFS4_CDFC4_BACK_OR_BOTH
&&
3514 c
->cn_flags
!= NFS4_CDFC4_FORE
)
3517 status
= nfserr_inval
;
3518 spin_unlock(&clp
->cl_lock
);
3522 __be32
nfsd4_bind_conn_to_session(struct svc_rqst
*rqstp
,
3523 struct nfsd4_compound_state
*cstate
,
3524 union nfsd4_op_u
*u
)
3526 struct nfsd4_bind_conn_to_session
*bcts
= &u
->bind_conn_to_session
;
3528 struct nfsd4_conn
*conn
;
3529 struct nfsd4_session
*session
;
3530 struct net
*net
= SVC_NET(rqstp
);
3531 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3533 if (!nfsd4_last_compound_op(rqstp
))
3534 return nfserr_not_only_op
;
3535 spin_lock(&nn
->client_lock
);
3536 session
= find_in_sessionid_hashtbl(&bcts
->sessionid
, net
, &status
);
3537 spin_unlock(&nn
->client_lock
);
3539 goto out_no_session
;
3540 status
= nfserr_wrong_cred
;
3541 if (!nfsd4_mach_creds_match(session
->se_client
, rqstp
))
3543 status
= nfsd4_match_existing_connection(rqstp
, session
, bcts
->dir
);
3544 if (status
== nfs_ok
|| status
== nfserr_inval
)
3546 status
= nfsd4_map_bcts_dir(&bcts
->dir
);
3549 conn
= alloc_conn(rqstp
, bcts
->dir
);
3550 status
= nfserr_jukebox
;
3553 nfsd4_init_conn(rqstp
, conn
, session
);
3556 nfsd4_put_session(session
);
3561 static bool nfsd4_compound_in_session(struct nfsd4_compound_state
*cstate
, struct nfs4_sessionid
*sid
)
3563 if (!cstate
->session
)
3565 return !memcmp(sid
, &cstate
->session
->se_sessionid
, sizeof(*sid
));
3569 nfsd4_destroy_session(struct svc_rqst
*r
, struct nfsd4_compound_state
*cstate
,
3570 union nfsd4_op_u
*u
)
3572 struct nfs4_sessionid
*sessionid
= &u
->destroy_session
.sessionid
;
3573 struct nfsd4_session
*ses
;
3575 int ref_held_by_me
= 0;
3576 struct net
*net
= SVC_NET(r
);
3577 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3579 status
= nfserr_not_only_op
;
3580 if (nfsd4_compound_in_session(cstate
, sessionid
)) {
3581 if (!nfsd4_last_compound_op(r
))
3585 dump_sessionid(__func__
, sessionid
);
3586 spin_lock(&nn
->client_lock
);
3587 ses
= find_in_sessionid_hashtbl(sessionid
, net
, &status
);
3589 goto out_client_lock
;
3590 status
= nfserr_wrong_cred
;
3591 if (!nfsd4_mach_creds_match(ses
->se_client
, r
))
3592 goto out_put_session
;
3593 status
= mark_session_dead_locked(ses
, 1 + ref_held_by_me
);
3595 goto out_put_session
;
3596 unhash_session(ses
);
3597 spin_unlock(&nn
->client_lock
);
3599 nfsd4_probe_callback_sync(ses
->se_client
);
3601 spin_lock(&nn
->client_lock
);
3604 nfsd4_put_session_locked(ses
);
3606 spin_unlock(&nn
->client_lock
);
3611 static __be32
nfsd4_sequence_check_conn(struct nfsd4_conn
*new, struct nfsd4_session
*ses
)
3613 struct nfs4_client
*clp
= ses
->se_client
;
3614 struct nfsd4_conn
*c
;
3615 __be32 status
= nfs_ok
;
3618 spin_lock(&clp
->cl_lock
);
3619 c
= __nfsd4_find_conn(new->cn_xprt
, ses
);
3622 status
= nfserr_conn_not_bound_to_session
;
3623 if (clp
->cl_mach_cred
)
3625 __nfsd4_hash_conn(new, ses
);
3626 spin_unlock(&clp
->cl_lock
);
3627 ret
= nfsd4_register_conn(new);
3629 /* oops; xprt is already down: */
3630 nfsd4_conn_lost(&new->cn_xpt_user
);
3633 spin_unlock(&clp
->cl_lock
);
3638 static bool nfsd4_session_too_many_ops(struct svc_rqst
*rqstp
, struct nfsd4_session
*session
)
3640 struct nfsd4_compoundargs
*args
= rqstp
->rq_argp
;
3642 return args
->opcnt
> session
->se_fchannel
.maxops
;
3645 static bool nfsd4_request_too_big(struct svc_rqst
*rqstp
,
3646 struct nfsd4_session
*session
)
3648 struct xdr_buf
*xb
= &rqstp
->rq_arg
;
3650 return xb
->len
> session
->se_fchannel
.maxreq_sz
;
3653 static bool replay_matches_cache(struct svc_rqst
*rqstp
,
3654 struct nfsd4_sequence
*seq
, struct nfsd4_slot
*slot
)
3656 struct nfsd4_compoundargs
*argp
= rqstp
->rq_argp
;
3658 if ((bool)(slot
->sl_flags
& NFSD4_SLOT_CACHETHIS
) !=
3659 (bool)seq
->cachethis
)
3662 * If there's an error then the reply can have fewer ops than
3665 if (slot
->sl_opcnt
< argp
->opcnt
&& !slot
->sl_status
)
3668 * But if we cached a reply with *more* ops than the call you're
3669 * sending us now, then this new call is clearly not really a
3670 * replay of the old one:
3672 if (slot
->sl_opcnt
> argp
->opcnt
)
3674 /* This is the only check explicitly called by spec: */
3675 if (!same_creds(&rqstp
->rq_cred
, &slot
->sl_cred
))
3678 * There may be more comparisons we could actually do, but the
3679 * spec doesn't require us to catch every case where the calls
3680 * don't match (that would require caching the call as well as
3681 * the reply), so we don't bother.
3687 nfsd4_sequence(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3688 union nfsd4_op_u
*u
)
3690 struct nfsd4_sequence
*seq
= &u
->sequence
;
3691 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
3692 struct xdr_stream
*xdr
= &resp
->xdr
;
3693 struct nfsd4_session
*session
;
3694 struct nfs4_client
*clp
;
3695 struct nfsd4_slot
*slot
;
3696 struct nfsd4_conn
*conn
;
3699 struct net
*net
= SVC_NET(rqstp
);
3700 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
3702 if (resp
->opcnt
!= 1)
3703 return nfserr_sequence_pos
;
3706 * Will be either used or freed by nfsd4_sequence_check_conn
3709 conn
= alloc_conn(rqstp
, NFS4_CDFC4_FORE
);
3711 return nfserr_jukebox
;
3713 spin_lock(&nn
->client_lock
);
3714 session
= find_in_sessionid_hashtbl(&seq
->sessionid
, net
, &status
);
3716 goto out_no_session
;
3717 clp
= session
->se_client
;
3719 status
= nfserr_too_many_ops
;
3720 if (nfsd4_session_too_many_ops(rqstp
, session
))
3721 goto out_put_session
;
3723 status
= nfserr_req_too_big
;
3724 if (nfsd4_request_too_big(rqstp
, session
))
3725 goto out_put_session
;
3727 status
= nfserr_badslot
;
3728 if (seq
->slotid
>= session
->se_fchannel
.maxreqs
)
3729 goto out_put_session
;
3731 slot
= session
->se_slots
[seq
->slotid
];
3732 dprintk("%s: slotid %d\n", __func__
, seq
->slotid
);
3734 /* We do not negotiate the number of slots yet, so set the
3735 * maxslots to the session maxreqs which is used to encode
3736 * sr_highest_slotid and the sr_target_slot id to maxslots */
3737 seq
->maxslots
= session
->se_fchannel
.maxreqs
;
3739 status
= check_slot_seqid(seq
->seqid
, slot
->sl_seqid
,
3740 slot
->sl_flags
& NFSD4_SLOT_INUSE
);
3741 if (status
== nfserr_replay_cache
) {
3742 status
= nfserr_seq_misordered
;
3743 if (!(slot
->sl_flags
& NFSD4_SLOT_INITIALIZED
))
3744 goto out_put_session
;
3745 status
= nfserr_seq_false_retry
;
3746 if (!replay_matches_cache(rqstp
, seq
, slot
))
3747 goto out_put_session
;
3748 cstate
->slot
= slot
;
3749 cstate
->session
= session
;
3751 /* Return the cached reply status and set cstate->status
3752 * for nfsd4_proc_compound processing */
3753 status
= nfsd4_replay_cache_entry(resp
, seq
);
3754 cstate
->status
= nfserr_replay_cache
;
3758 goto out_put_session
;
3760 status
= nfsd4_sequence_check_conn(conn
, session
);
3763 goto out_put_session
;
3765 buflen
= (seq
->cachethis
) ?
3766 session
->se_fchannel
.maxresp_cached
:
3767 session
->se_fchannel
.maxresp_sz
;
3768 status
= (seq
->cachethis
) ? nfserr_rep_too_big_to_cache
:
3770 if (xdr_restrict_buflen(xdr
, buflen
- rqstp
->rq_auth_slack
))
3771 goto out_put_session
;
3772 svc_reserve(rqstp
, buflen
);
3775 /* Success! bump slot seqid */
3776 slot
->sl_seqid
= seq
->seqid
;
3777 slot
->sl_flags
|= NFSD4_SLOT_INUSE
;
3779 slot
->sl_flags
|= NFSD4_SLOT_CACHETHIS
;
3781 slot
->sl_flags
&= ~NFSD4_SLOT_CACHETHIS
;
3783 cstate
->slot
= slot
;
3784 cstate
->session
= session
;
3788 switch (clp
->cl_cb_state
) {
3790 seq
->status_flags
= SEQ4_STATUS_CB_PATH_DOWN
;
3792 case NFSD4_CB_FAULT
:
3793 seq
->status_flags
= SEQ4_STATUS_BACKCHANNEL_FAULT
;
3796 seq
->status_flags
= 0;
3798 if (!list_empty(&clp
->cl_revoked
))
3799 seq
->status_flags
|= SEQ4_STATUS_RECALLABLE_STATE_REVOKED
;
3803 spin_unlock(&nn
->client_lock
);
3806 nfsd4_put_session_locked(session
);
3807 goto out_no_session
;
3811 nfsd4_sequence_done(struct nfsd4_compoundres
*resp
)
3813 struct nfsd4_compound_state
*cs
= &resp
->cstate
;
3815 if (nfsd4_has_session(cs
)) {
3816 if (cs
->status
!= nfserr_replay_cache
) {
3817 nfsd4_store_cache_entry(resp
);
3818 cs
->slot
->sl_flags
&= ~NFSD4_SLOT_INUSE
;
3820 /* Drop session reference that was taken in nfsd4_sequence() */
3821 nfsd4_put_session(cs
->session
);
3823 put_client_renew(cs
->clp
);
3827 nfsd4_destroy_clientid(struct svc_rqst
*rqstp
,
3828 struct nfsd4_compound_state
*cstate
,
3829 union nfsd4_op_u
*u
)
3831 struct nfsd4_destroy_clientid
*dc
= &u
->destroy_clientid
;
3832 struct nfs4_client
*conf
, *unconf
;
3833 struct nfs4_client
*clp
= NULL
;
3835 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3837 spin_lock(&nn
->client_lock
);
3838 unconf
= find_unconfirmed_client(&dc
->clientid
, true, nn
);
3839 conf
= find_confirmed_client(&dc
->clientid
, true, nn
);
3840 WARN_ON_ONCE(conf
&& unconf
);
3843 if (client_has_state(conf
)) {
3844 status
= nfserr_clientid_busy
;
3847 status
= mark_client_expired_locked(conf
);
3854 status
= nfserr_stale_clientid
;
3857 if (!nfsd4_mach_creds_match(clp
, rqstp
)) {
3859 status
= nfserr_wrong_cred
;
3862 unhash_client_locked(clp
);
3864 spin_unlock(&nn
->client_lock
);
3871 nfsd4_reclaim_complete(struct svc_rqst
*rqstp
,
3872 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
3874 struct nfsd4_reclaim_complete
*rc
= &u
->reclaim_complete
;
3877 if (rc
->rca_one_fs
) {
3878 if (!cstate
->current_fh
.fh_dentry
)
3879 return nfserr_nofilehandle
;
3881 * We don't take advantage of the rca_one_fs case.
3882 * That's OK, it's optional, we can safely ignore it.
3887 status
= nfserr_complete_already
;
3888 if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
,
3889 &cstate
->session
->se_client
->cl_flags
))
3892 status
= nfserr_stale_clientid
;
3893 if (is_client_expired(cstate
->session
->se_client
))
3895 * The following error isn't really legal.
3896 * But we only get here if the client just explicitly
3897 * destroyed the client. Surely it no longer cares what
3898 * error it gets back on an operation for the dead
3904 nfsd4_client_record_create(cstate
->session
->se_client
);
3905 inc_reclaim_complete(cstate
->session
->se_client
);
3911 nfsd4_setclientid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
3912 union nfsd4_op_u
*u
)
3914 struct nfsd4_setclientid
*setclid
= &u
->setclientid
;
3915 struct xdr_netobj clname
= setclid
->se_name
;
3916 nfs4_verifier clverifier
= setclid
->se_verf
;
3917 struct nfs4_client
*conf
, *new;
3918 struct nfs4_client
*unconf
= NULL
;
3920 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3922 new = create_client(clname
, rqstp
, &clverifier
);
3924 return nfserr_jukebox
;
3925 /* Cases below refer to rfc 3530 section 14.2.33: */
3926 spin_lock(&nn
->client_lock
);
3927 conf
= find_confirmed_client_by_name(&clname
, nn
);
3928 if (conf
&& client_has_state(conf
)) {
3930 status
= nfserr_clid_inuse
;
3931 if (clp_used_exchangeid(conf
))
3933 if (!same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
)) {
3934 trace_nfsd_clid_inuse_err(conf
);
3938 unconf
= find_unconfirmed_client_by_name(&clname
, nn
);
3940 unhash_client_locked(unconf
);
3941 /* We need to handle only case 1: probable callback update */
3942 if (conf
&& same_verf(&conf
->cl_verifier
, &clverifier
)) {
3943 copy_clid(new, conf
);
3944 gen_confirm(new, nn
);
3946 new->cl_minorversion
= 0;
3947 gen_callback(new, setclid
, rqstp
);
3948 add_to_unconfirmed(new);
3949 setclid
->se_clientid
.cl_boot
= new->cl_clientid
.cl_boot
;
3950 setclid
->se_clientid
.cl_id
= new->cl_clientid
.cl_id
;
3951 memcpy(setclid
->se_confirm
.data
, new->cl_confirm
.data
, sizeof(setclid
->se_confirm
.data
));
3955 spin_unlock(&nn
->client_lock
);
3959 expire_client(unconf
);
3965 nfsd4_setclientid_confirm(struct svc_rqst
*rqstp
,
3966 struct nfsd4_compound_state
*cstate
,
3967 union nfsd4_op_u
*u
)
3969 struct nfsd4_setclientid_confirm
*setclientid_confirm
=
3970 &u
->setclientid_confirm
;
3971 struct nfs4_client
*conf
, *unconf
;
3972 struct nfs4_client
*old
= NULL
;
3973 nfs4_verifier confirm
= setclientid_confirm
->sc_confirm
;
3974 clientid_t
* clid
= &setclientid_confirm
->sc_clientid
;
3976 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
3978 if (STALE_CLIENTID(clid
, nn
))
3979 return nfserr_stale_clientid
;
3981 spin_lock(&nn
->client_lock
);
3982 conf
= find_confirmed_client(clid
, false, nn
);
3983 unconf
= find_unconfirmed_client(clid
, false, nn
);
3985 * We try hard to give out unique clientid's, so if we get an
3986 * attempt to confirm the same clientid with a different cred,
3987 * the client may be buggy; this should never happen.
3989 * Nevertheless, RFC 7530 recommends INUSE for this case:
3991 status
= nfserr_clid_inuse
;
3992 if (unconf
&& !same_creds(&unconf
->cl_cred
, &rqstp
->rq_cred
))
3994 if (conf
&& !same_creds(&conf
->cl_cred
, &rqstp
->rq_cred
))
3996 /* cases below refer to rfc 3530 section 14.2.34: */
3997 if (!unconf
|| !same_verf(&confirm
, &unconf
->cl_confirm
)) {
3998 if (conf
&& same_verf(&confirm
, &conf
->cl_confirm
)) {
3999 /* case 2: probable retransmit */
4001 } else /* case 4: client hasn't noticed we rebooted yet? */
4002 status
= nfserr_stale_clientid
;
4006 if (conf
) { /* case 1: callback update */
4008 unhash_client_locked(old
);
4009 nfsd4_change_callback(conf
, &unconf
->cl_cb_conn
);
4010 } else { /* case 3: normal case; new or rebooted client */
4011 old
= find_confirmed_client_by_name(&unconf
->cl_name
, nn
);
4013 status
= nfserr_clid_inuse
;
4014 if (client_has_state(old
)
4015 && !same_creds(&unconf
->cl_cred
,
4018 status
= mark_client_expired_locked(old
);
4024 move_to_confirmed(unconf
);
4027 get_client_locked(conf
);
4028 spin_unlock(&nn
->client_lock
);
4029 nfsd4_probe_callback(conf
);
4030 spin_lock(&nn
->client_lock
);
4031 put_client_renew_locked(conf
);
4033 spin_unlock(&nn
->client_lock
);
4039 static struct nfs4_file
*nfsd4_alloc_file(void)
4041 return kmem_cache_alloc(file_slab
, GFP_KERNEL
);
4044 /* OPEN Share state helper functions */
4045 static void nfsd4_init_file(struct knfsd_fh
*fh
, unsigned int hashval
,
4046 struct nfs4_file
*fp
)
4048 lockdep_assert_held(&state_lock
);
4050 refcount_set(&fp
->fi_ref
, 1);
4051 spin_lock_init(&fp
->fi_lock
);
4052 INIT_LIST_HEAD(&fp
->fi_stateids
);
4053 INIT_LIST_HEAD(&fp
->fi_delegations
);
4054 INIT_LIST_HEAD(&fp
->fi_clnt_odstate
);
4055 fh_copy_shallow(&fp
->fi_fhandle
, fh
);
4056 fp
->fi_deleg_file
= NULL
;
4057 fp
->fi_had_conflict
= false;
4058 fp
->fi_share_deny
= 0;
4059 memset(fp
->fi_fds
, 0, sizeof(fp
->fi_fds
));
4060 memset(fp
->fi_access
, 0, sizeof(fp
->fi_access
));
4061 #ifdef CONFIG_NFSD_PNFS
4062 INIT_LIST_HEAD(&fp
->fi_lo_states
);
4063 atomic_set(&fp
->fi_lo_recalls
, 0);
4065 hlist_add_head_rcu(&fp
->fi_hash
, &file_hashtbl
[hashval
]);
4069 nfsd4_free_slabs(void)
4071 kmem_cache_destroy(client_slab
);
4072 kmem_cache_destroy(openowner_slab
);
4073 kmem_cache_destroy(lockowner_slab
);
4074 kmem_cache_destroy(file_slab
);
4075 kmem_cache_destroy(stateid_slab
);
4076 kmem_cache_destroy(deleg_slab
);
4077 kmem_cache_destroy(odstate_slab
);
4081 nfsd4_init_slabs(void)
4083 client_slab
= kmem_cache_create("nfsd4_clients",
4084 sizeof(struct nfs4_client
), 0, 0, NULL
);
4085 if (client_slab
== NULL
)
4087 openowner_slab
= kmem_cache_create("nfsd4_openowners",
4088 sizeof(struct nfs4_openowner
), 0, 0, NULL
);
4089 if (openowner_slab
== NULL
)
4090 goto out_free_client_slab
;
4091 lockowner_slab
= kmem_cache_create("nfsd4_lockowners",
4092 sizeof(struct nfs4_lockowner
), 0, 0, NULL
);
4093 if (lockowner_slab
== NULL
)
4094 goto out_free_openowner_slab
;
4095 file_slab
= kmem_cache_create("nfsd4_files",
4096 sizeof(struct nfs4_file
), 0, 0, NULL
);
4097 if (file_slab
== NULL
)
4098 goto out_free_lockowner_slab
;
4099 stateid_slab
= kmem_cache_create("nfsd4_stateids",
4100 sizeof(struct nfs4_ol_stateid
), 0, 0, NULL
);
4101 if (stateid_slab
== NULL
)
4102 goto out_free_file_slab
;
4103 deleg_slab
= kmem_cache_create("nfsd4_delegations",
4104 sizeof(struct nfs4_delegation
), 0, 0, NULL
);
4105 if (deleg_slab
== NULL
)
4106 goto out_free_stateid_slab
;
4107 odstate_slab
= kmem_cache_create("nfsd4_odstate",
4108 sizeof(struct nfs4_clnt_odstate
), 0, 0, NULL
);
4109 if (odstate_slab
== NULL
)
4110 goto out_free_deleg_slab
;
4113 out_free_deleg_slab
:
4114 kmem_cache_destroy(deleg_slab
);
4115 out_free_stateid_slab
:
4116 kmem_cache_destroy(stateid_slab
);
4118 kmem_cache_destroy(file_slab
);
4119 out_free_lockowner_slab
:
4120 kmem_cache_destroy(lockowner_slab
);
4121 out_free_openowner_slab
:
4122 kmem_cache_destroy(openowner_slab
);
4123 out_free_client_slab
:
4124 kmem_cache_destroy(client_slab
);
4129 static void init_nfs4_replay(struct nfs4_replay
*rp
)
4131 rp
->rp_status
= nfserr_serverfault
;
4133 rp
->rp_buf
= rp
->rp_ibuf
;
4134 mutex_init(&rp
->rp_mutex
);
4137 static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state
*cstate
,
4138 struct nfs4_stateowner
*so
)
4140 if (!nfsd4_has_session(cstate
)) {
4141 mutex_lock(&so
->so_replay
.rp_mutex
);
4142 cstate
->replay_owner
= nfs4_get_stateowner(so
);
4146 void nfsd4_cstate_clear_replay(struct nfsd4_compound_state
*cstate
)
4148 struct nfs4_stateowner
*so
= cstate
->replay_owner
;
4151 cstate
->replay_owner
= NULL
;
4152 mutex_unlock(&so
->so_replay
.rp_mutex
);
4153 nfs4_put_stateowner(so
);
4157 static inline void *alloc_stateowner(struct kmem_cache
*slab
, struct xdr_netobj
*owner
, struct nfs4_client
*clp
)
4159 struct nfs4_stateowner
*sop
;
4161 sop
= kmem_cache_alloc(slab
, GFP_KERNEL
);
4165 xdr_netobj_dup(&sop
->so_owner
, owner
, GFP_KERNEL
);
4166 if (!sop
->so_owner
.data
) {
4167 kmem_cache_free(slab
, sop
);
4171 INIT_LIST_HEAD(&sop
->so_stateids
);
4172 sop
->so_client
= clp
;
4173 init_nfs4_replay(&sop
->so_replay
);
4174 atomic_set(&sop
->so_count
, 1);
4178 static void hash_openowner(struct nfs4_openowner
*oo
, struct nfs4_client
*clp
, unsigned int strhashval
)
4180 lockdep_assert_held(&clp
->cl_lock
);
4182 list_add(&oo
->oo_owner
.so_strhash
,
4183 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
4184 list_add(&oo
->oo_perclient
, &clp
->cl_openowners
);
4187 static void nfs4_unhash_openowner(struct nfs4_stateowner
*so
)
4189 unhash_openowner_locked(openowner(so
));
4192 static void nfs4_free_openowner(struct nfs4_stateowner
*so
)
4194 struct nfs4_openowner
*oo
= openowner(so
);
4196 kmem_cache_free(openowner_slab
, oo
);
4199 static const struct nfs4_stateowner_operations openowner_ops
= {
4200 .so_unhash
= nfs4_unhash_openowner
,
4201 .so_free
= nfs4_free_openowner
,
4204 static struct nfs4_ol_stateid
*
4205 nfsd4_find_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
4207 struct nfs4_ol_stateid
*local
, *ret
= NULL
;
4208 struct nfs4_openowner
*oo
= open
->op_openowner
;
4210 lockdep_assert_held(&fp
->fi_lock
);
4212 list_for_each_entry(local
, &fp
->fi_stateids
, st_perfile
) {
4213 /* ignore lock owners */
4214 if (local
->st_stateowner
->so_is_open_owner
== 0)
4216 if (local
->st_stateowner
!= &oo
->oo_owner
)
4218 if (local
->st_stid
.sc_type
== NFS4_OPEN_STID
) {
4220 refcount_inc(&ret
->st_stid
.sc_count
);
4228 nfsd4_verify_open_stid(struct nfs4_stid
*s
)
4230 __be32 ret
= nfs_ok
;
4232 switch (s
->sc_type
) {
4236 case NFS4_CLOSED_STID
:
4237 case NFS4_CLOSED_DELEG_STID
:
4238 ret
= nfserr_bad_stateid
;
4240 case NFS4_REVOKED_DELEG_STID
:
4241 ret
= nfserr_deleg_revoked
;
4246 /* Lock the stateid st_mutex, and deal with races with CLOSE */
4248 nfsd4_lock_ol_stateid(struct nfs4_ol_stateid
*stp
)
4252 mutex_lock_nested(&stp
->st_mutex
, LOCK_STATEID_MUTEX
);
4253 ret
= nfsd4_verify_open_stid(&stp
->st_stid
);
4255 mutex_unlock(&stp
->st_mutex
);
4259 static struct nfs4_ol_stateid
*
4260 nfsd4_find_and_lock_existing_open(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
4262 struct nfs4_ol_stateid
*stp
;
4264 spin_lock(&fp
->fi_lock
);
4265 stp
= nfsd4_find_existing_open(fp
, open
);
4266 spin_unlock(&fp
->fi_lock
);
4267 if (!stp
|| nfsd4_lock_ol_stateid(stp
) == nfs_ok
)
4269 nfs4_put_stid(&stp
->st_stid
);
4274 static struct nfs4_openowner
*
4275 alloc_init_open_stateowner(unsigned int strhashval
, struct nfsd4_open
*open
,
4276 struct nfsd4_compound_state
*cstate
)
4278 struct nfs4_client
*clp
= cstate
->clp
;
4279 struct nfs4_openowner
*oo
, *ret
;
4281 oo
= alloc_stateowner(openowner_slab
, &open
->op_owner
, clp
);
4284 oo
->oo_owner
.so_ops
= &openowner_ops
;
4285 oo
->oo_owner
.so_is_open_owner
= 1;
4286 oo
->oo_owner
.so_seqid
= open
->op_seqid
;
4288 if (nfsd4_has_session(cstate
))
4289 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
4291 oo
->oo_last_closed_stid
= NULL
;
4292 INIT_LIST_HEAD(&oo
->oo_close_lru
);
4293 spin_lock(&clp
->cl_lock
);
4294 ret
= find_openstateowner_str_locked(strhashval
, open
, clp
);
4296 hash_openowner(oo
, clp
, strhashval
);
4299 nfs4_free_stateowner(&oo
->oo_owner
);
4301 spin_unlock(&clp
->cl_lock
);
4305 static struct nfs4_ol_stateid
*
4306 init_open_stateid(struct nfs4_file
*fp
, struct nfsd4_open
*open
)
4309 struct nfs4_openowner
*oo
= open
->op_openowner
;
4310 struct nfs4_ol_stateid
*retstp
= NULL
;
4311 struct nfs4_ol_stateid
*stp
;
4314 /* We are moving these outside of the spinlocks to avoid the warnings */
4315 mutex_init(&stp
->st_mutex
);
4316 mutex_lock_nested(&stp
->st_mutex
, OPEN_STATEID_MUTEX
);
4319 spin_lock(&oo
->oo_owner
.so_client
->cl_lock
);
4320 spin_lock(&fp
->fi_lock
);
4322 retstp
= nfsd4_find_existing_open(fp
, open
);
4326 open
->op_stp
= NULL
;
4327 refcount_inc(&stp
->st_stid
.sc_count
);
4328 stp
->st_stid
.sc_type
= NFS4_OPEN_STID
;
4329 INIT_LIST_HEAD(&stp
->st_locks
);
4330 stp
->st_stateowner
= nfs4_get_stateowner(&oo
->oo_owner
);
4332 stp
->st_stid
.sc_file
= fp
;
4333 stp
->st_access_bmap
= 0;
4334 stp
->st_deny_bmap
= 0;
4335 stp
->st_openstp
= NULL
;
4336 list_add(&stp
->st_perstateowner
, &oo
->oo_owner
.so_stateids
);
4337 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
4340 spin_unlock(&fp
->fi_lock
);
4341 spin_unlock(&oo
->oo_owner
.so_client
->cl_lock
);
4343 /* Handle races with CLOSE */
4344 if (nfsd4_lock_ol_stateid(retstp
) != nfs_ok
) {
4345 nfs4_put_stid(&retstp
->st_stid
);
4348 /* To keep mutex tracking happy */
4349 mutex_unlock(&stp
->st_mutex
);
4356 * In the 4.0 case we need to keep the owners around a little while to handle
4357 * CLOSE replay. We still do need to release any file access that is held by
4358 * them before returning however.
4361 move_to_close_lru(struct nfs4_ol_stateid
*s
, struct net
*net
)
4363 struct nfs4_ol_stateid
*last
;
4364 struct nfs4_openowner
*oo
= openowner(s
->st_stateowner
);
4365 struct nfsd_net
*nn
= net_generic(s
->st_stid
.sc_client
->net
,
4368 dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo
);
4371 * We know that we hold one reference via nfsd4_close, and another
4372 * "persistent" reference for the client. If the refcount is higher
4373 * than 2, then there are still calls in progress that are using this
4374 * stateid. We can't put the sc_file reference until they are finished.
4375 * Wait for the refcount to drop to 2. Since it has been unhashed,
4376 * there should be no danger of the refcount going back up again at
4379 wait_event(close_wq
, refcount_read(&s
->st_stid
.sc_count
) == 2);
4381 release_all_access(s
);
4382 if (s
->st_stid
.sc_file
) {
4383 put_nfs4_file(s
->st_stid
.sc_file
);
4384 s
->st_stid
.sc_file
= NULL
;
4387 spin_lock(&nn
->client_lock
);
4388 last
= oo
->oo_last_closed_stid
;
4389 oo
->oo_last_closed_stid
= s
;
4390 list_move_tail(&oo
->oo_close_lru
, &nn
->close_lru
);
4391 oo
->oo_time
= ktime_get_boottime_seconds();
4392 spin_unlock(&nn
->client_lock
);
4394 nfs4_put_stid(&last
->st_stid
);
4397 /* search file_hashtbl[] for file */
4398 static struct nfs4_file
*
4399 find_file_locked(struct knfsd_fh
*fh
, unsigned int hashval
)
4401 struct nfs4_file
*fp
;
4403 hlist_for_each_entry_rcu(fp
, &file_hashtbl
[hashval
], fi_hash
,
4404 lockdep_is_held(&state_lock
)) {
4405 if (fh_match(&fp
->fi_fhandle
, fh
)) {
4406 if (refcount_inc_not_zero(&fp
->fi_ref
))
4414 find_file(struct knfsd_fh
*fh
)
4416 struct nfs4_file
*fp
;
4417 unsigned int hashval
= file_hashval(fh
);
4420 fp
= find_file_locked(fh
, hashval
);
4425 static struct nfs4_file
*
4426 find_or_add_file(struct nfs4_file
*new, struct knfsd_fh
*fh
)
4428 struct nfs4_file
*fp
;
4429 unsigned int hashval
= file_hashval(fh
);
4432 fp
= find_file_locked(fh
, hashval
);
4437 spin_lock(&state_lock
);
4438 fp
= find_file_locked(fh
, hashval
);
4439 if (likely(fp
== NULL
)) {
4440 nfsd4_init_file(fh
, hashval
, new);
4443 spin_unlock(&state_lock
);
4449 * Called to check deny when READ with all zero stateid or
4450 * WRITE with all zero or all one stateid
4453 nfs4_share_conflict(struct svc_fh
*current_fh
, unsigned int deny_type
)
4455 struct nfs4_file
*fp
;
4456 __be32 ret
= nfs_ok
;
4458 fp
= find_file(¤t_fh
->fh_handle
);
4461 /* Check for conflicting share reservations */
4462 spin_lock(&fp
->fi_lock
);
4463 if (fp
->fi_share_deny
& deny_type
)
4464 ret
= nfserr_locked
;
4465 spin_unlock(&fp
->fi_lock
);
4470 static void nfsd4_cb_recall_prepare(struct nfsd4_callback
*cb
)
4472 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
4473 struct nfsd_net
*nn
= net_generic(dp
->dl_stid
.sc_client
->net
,
4476 block_delegations(&dp
->dl_stid
.sc_file
->fi_fhandle
);
4479 * We can't do this in nfsd_break_deleg_cb because it is
4480 * already holding inode->i_lock.
4482 * If the dl_time != 0, then we know that it has already been
4483 * queued for a lease break. Don't queue it again.
4485 spin_lock(&state_lock
);
4486 if (dp
->dl_time
== 0) {
4487 dp
->dl_time
= ktime_get_boottime_seconds();
4488 list_add_tail(&dp
->dl_recall_lru
, &nn
->del_recall_lru
);
4490 spin_unlock(&state_lock
);
4493 static int nfsd4_cb_recall_done(struct nfsd4_callback
*cb
,
4494 struct rpc_task
*task
)
4496 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
4498 if (dp
->dl_stid
.sc_type
== NFS4_CLOSED_DELEG_STID
)
4501 switch (task
->tk_status
) {
4504 case -NFS4ERR_DELAY
:
4505 rpc_delay(task
, 2 * HZ
);
4508 case -NFS4ERR_BAD_STATEID
:
4510 * Race: client probably got cb_recall before open reply
4511 * granting delegation.
4513 if (dp
->dl_retries
--) {
4514 rpc_delay(task
, 2 * HZ
);
4523 static void nfsd4_cb_recall_release(struct nfsd4_callback
*cb
)
4525 struct nfs4_delegation
*dp
= cb_to_delegation(cb
);
4527 nfs4_put_stid(&dp
->dl_stid
);
4530 static const struct nfsd4_callback_ops nfsd4_cb_recall_ops
= {
4531 .prepare
= nfsd4_cb_recall_prepare
,
4532 .done
= nfsd4_cb_recall_done
,
4533 .release
= nfsd4_cb_recall_release
,
4536 static void nfsd_break_one_deleg(struct nfs4_delegation
*dp
)
4539 * We're assuming the state code never drops its reference
4540 * without first removing the lease. Since we're in this lease
4541 * callback (and since the lease code is serialized by the
4542 * i_lock) we know the server hasn't removed the lease yet, and
4543 * we know it's safe to take a reference.
4545 refcount_inc(&dp
->dl_stid
.sc_count
);
4546 nfsd4_run_cb(&dp
->dl_recall
);
4549 /* Called from break_lease() with i_lock held. */
4551 nfsd_break_deleg_cb(struct file_lock
*fl
)
4554 struct nfs4_delegation
*dp
= (struct nfs4_delegation
*)fl
->fl_owner
;
4555 struct nfs4_file
*fp
= dp
->dl_stid
.sc_file
;
4557 trace_nfsd_deleg_break(&dp
->dl_stid
.sc_stateid
);
4560 * We don't want the locks code to timeout the lease for us;
4561 * we'll remove it ourself if a delegation isn't returned
4564 fl
->fl_break_time
= 0;
4566 spin_lock(&fp
->fi_lock
);
4567 fp
->fi_had_conflict
= true;
4568 nfsd_break_one_deleg(dp
);
4569 spin_unlock(&fp
->fi_lock
);
4573 static bool nfsd_breaker_owns_lease(struct file_lock
*fl
)
4575 struct nfs4_delegation
*dl
= fl
->fl_owner
;
4576 struct svc_rqst
*rqst
;
4577 struct nfs4_client
*clp
;
4581 rqst
= kthread_data(current
);
4582 clp
= *(rqst
->rq_lease_breaker
);
4583 return dl
->dl_stid
.sc_client
== clp
;
4587 nfsd_change_deleg_cb(struct file_lock
*onlist
, int arg
,
4588 struct list_head
*dispose
)
4591 return lease_modify(onlist
, arg
, dispose
);
4596 static const struct lock_manager_operations nfsd_lease_mng_ops
= {
4597 .lm_breaker_owns_lease
= nfsd_breaker_owns_lease
,
4598 .lm_break
= nfsd_break_deleg_cb
,
4599 .lm_change
= nfsd_change_deleg_cb
,
4602 static __be32
nfsd4_check_seqid(struct nfsd4_compound_state
*cstate
, struct nfs4_stateowner
*so
, u32 seqid
)
4604 if (nfsd4_has_session(cstate
))
4606 if (seqid
== so
->so_seqid
- 1)
4607 return nfserr_replay_me
;
4608 if (seqid
== so
->so_seqid
)
4610 return nfserr_bad_seqid
;
4613 static __be32
lookup_clientid(clientid_t
*clid
,
4614 struct nfsd4_compound_state
*cstate
,
4615 struct nfsd_net
*nn
,
4618 struct nfs4_client
*found
;
4621 found
= cstate
->clp
;
4622 if (!same_clid(&found
->cl_clientid
, clid
))
4623 return nfserr_stale_clientid
;
4627 if (STALE_CLIENTID(clid
, nn
))
4628 return nfserr_stale_clientid
;
4631 * For v4.1+ we get the client in the SEQUENCE op. If we don't have one
4632 * cached already then we know this is for is for v4.0 and "sessions"
4635 WARN_ON_ONCE(cstate
->session
);
4636 spin_lock(&nn
->client_lock
);
4637 found
= find_confirmed_client(clid
, sessions
, nn
);
4639 spin_unlock(&nn
->client_lock
);
4640 return nfserr_expired
;
4642 atomic_inc(&found
->cl_rpc_users
);
4643 spin_unlock(&nn
->client_lock
);
4645 /* Cache the nfs4_client in cstate! */
4646 cstate
->clp
= found
;
4651 nfsd4_process_open1(struct nfsd4_compound_state
*cstate
,
4652 struct nfsd4_open
*open
, struct nfsd_net
*nn
)
4654 clientid_t
*clientid
= &open
->op_clientid
;
4655 struct nfs4_client
*clp
= NULL
;
4656 unsigned int strhashval
;
4657 struct nfs4_openowner
*oo
= NULL
;
4660 if (STALE_CLIENTID(&open
->op_clientid
, nn
))
4661 return nfserr_stale_clientid
;
4663 * In case we need it later, after we've already created the
4664 * file and don't want to risk a further failure:
4666 open
->op_file
= nfsd4_alloc_file();
4667 if (open
->op_file
== NULL
)
4668 return nfserr_jukebox
;
4670 status
= lookup_clientid(clientid
, cstate
, nn
, false);
4675 strhashval
= ownerstr_hashval(&open
->op_owner
);
4676 oo
= find_openstateowner_str(strhashval
, open
, clp
);
4677 open
->op_openowner
= oo
;
4681 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
4682 /* Replace unconfirmed owners without checking for replay. */
4683 release_openowner(oo
);
4684 open
->op_openowner
= NULL
;
4687 status
= nfsd4_check_seqid(cstate
, &oo
->oo_owner
, open
->op_seqid
);
4692 oo
= alloc_init_open_stateowner(strhashval
, open
, cstate
);
4694 return nfserr_jukebox
;
4695 open
->op_openowner
= oo
;
4697 open
->op_stp
= nfs4_alloc_open_stateid(clp
);
4699 return nfserr_jukebox
;
4701 if (nfsd4_has_session(cstate
) &&
4702 (cstate
->current_fh
.fh_export
->ex_flags
& NFSEXP_PNFS
)) {
4703 open
->op_odstate
= alloc_clnt_odstate(clp
);
4704 if (!open
->op_odstate
)
4705 return nfserr_jukebox
;
4711 static inline __be32
4712 nfs4_check_delegmode(struct nfs4_delegation
*dp
, int flags
)
4714 if ((flags
& WR_STATE
) && (dp
->dl_type
== NFS4_OPEN_DELEGATE_READ
))
4715 return nfserr_openmode
;
4720 static int share_access_to_flags(u32 share_access
)
4722 return share_access
== NFS4_SHARE_ACCESS_READ
? RD_STATE
: WR_STATE
;
4725 static struct nfs4_delegation
*find_deleg_stateid(struct nfs4_client
*cl
, stateid_t
*s
)
4727 struct nfs4_stid
*ret
;
4729 ret
= find_stateid_by_type(cl
, s
,
4730 NFS4_DELEG_STID
|NFS4_REVOKED_DELEG_STID
);
4733 return delegstateid(ret
);
4736 static bool nfsd4_is_deleg_cur(struct nfsd4_open
*open
)
4738 return open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEGATE_CUR
||
4739 open
->op_claim_type
== NFS4_OPEN_CLAIM_DELEG_CUR_FH
;
4743 nfs4_check_deleg(struct nfs4_client
*cl
, struct nfsd4_open
*open
,
4744 struct nfs4_delegation
**dp
)
4747 __be32 status
= nfserr_bad_stateid
;
4748 struct nfs4_delegation
*deleg
;
4750 deleg
= find_deleg_stateid(cl
, &open
->op_delegate_stateid
);
4753 if (deleg
->dl_stid
.sc_type
== NFS4_REVOKED_DELEG_STID
) {
4754 nfs4_put_stid(&deleg
->dl_stid
);
4755 if (cl
->cl_minorversion
)
4756 status
= nfserr_deleg_revoked
;
4759 flags
= share_access_to_flags(open
->op_share_access
);
4760 status
= nfs4_check_delegmode(deleg
, flags
);
4762 nfs4_put_stid(&deleg
->dl_stid
);
4767 if (!nfsd4_is_deleg_cur(open
))
4771 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
4775 static inline int nfs4_access_to_access(u32 nfs4_access
)
4779 if (nfs4_access
& NFS4_SHARE_ACCESS_READ
)
4780 flags
|= NFSD_MAY_READ
;
4781 if (nfs4_access
& NFS4_SHARE_ACCESS_WRITE
)
4782 flags
|= NFSD_MAY_WRITE
;
4786 static inline __be32
4787 nfsd4_truncate(struct svc_rqst
*rqstp
, struct svc_fh
*fh
,
4788 struct nfsd4_open
*open
)
4790 struct iattr iattr
= {
4791 .ia_valid
= ATTR_SIZE
,
4794 if (!open
->op_truncate
)
4796 if (!(open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
))
4797 return nfserr_inval
;
4798 return nfsd_setattr(rqstp
, fh
, &iattr
, 0, (time64_t
)0);
4801 static __be32
nfs4_get_vfs_file(struct svc_rqst
*rqstp
, struct nfs4_file
*fp
,
4802 struct svc_fh
*cur_fh
, struct nfs4_ol_stateid
*stp
,
4803 struct nfsd4_open
*open
)
4805 struct nfsd_file
*nf
= NULL
;
4807 int oflag
= nfs4_access_to_omode(open
->op_share_access
);
4808 int access
= nfs4_access_to_access(open
->op_share_access
);
4809 unsigned char old_access_bmap
, old_deny_bmap
;
4811 spin_lock(&fp
->fi_lock
);
4814 * Are we trying to set a deny mode that would conflict with
4817 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
4818 if (status
!= nfs_ok
) {
4819 spin_unlock(&fp
->fi_lock
);
4823 /* set access to the file */
4824 status
= nfs4_file_get_access(fp
, open
->op_share_access
);
4825 if (status
!= nfs_ok
) {
4826 spin_unlock(&fp
->fi_lock
);
4830 /* Set access bits in stateid */
4831 old_access_bmap
= stp
->st_access_bmap
;
4832 set_access(open
->op_share_access
, stp
);
4834 /* Set new deny mask */
4835 old_deny_bmap
= stp
->st_deny_bmap
;
4836 set_deny(open
->op_share_deny
, stp
);
4837 fp
->fi_share_deny
|= (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4839 if (!fp
->fi_fds
[oflag
]) {
4840 spin_unlock(&fp
->fi_lock
);
4841 status
= nfsd_file_acquire(rqstp
, cur_fh
, access
, &nf
);
4843 goto out_put_access
;
4844 spin_lock(&fp
->fi_lock
);
4845 if (!fp
->fi_fds
[oflag
]) {
4846 fp
->fi_fds
[oflag
] = nf
;
4850 spin_unlock(&fp
->fi_lock
);
4854 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4856 goto out_put_access
;
4860 stp
->st_access_bmap
= old_access_bmap
;
4861 nfs4_file_put_access(fp
, open
->op_share_access
);
4862 reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap
), stp
);
4867 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
)
4870 unsigned char old_deny_bmap
= stp
->st_deny_bmap
;
4872 if (!test_access(open
->op_share_access
, stp
))
4873 return nfs4_get_vfs_file(rqstp
, fp
, cur_fh
, stp
, open
);
4875 /* test and set deny mode */
4876 spin_lock(&fp
->fi_lock
);
4877 status
= nfs4_file_check_deny(fp
, open
->op_share_deny
);
4878 if (status
== nfs_ok
) {
4879 set_deny(open
->op_share_deny
, stp
);
4880 fp
->fi_share_deny
|=
4881 (open
->op_share_deny
& NFS4_SHARE_DENY_BOTH
);
4883 spin_unlock(&fp
->fi_lock
);
4885 if (status
!= nfs_ok
)
4888 status
= nfsd4_truncate(rqstp
, cur_fh
, open
);
4889 if (status
!= nfs_ok
)
4890 reset_union_bmap_deny(old_deny_bmap
, stp
);
4894 /* Should we give out recallable state?: */
4895 static bool nfsd4_cb_channel_good(struct nfs4_client
*clp
)
4897 if (clp
->cl_cb_state
== NFSD4_CB_UP
)
4900 * In the sessions case, since we don't have to establish a
4901 * separate connection for callbacks, we assume it's OK
4902 * until we hear otherwise:
4904 return clp
->cl_minorversion
&& clp
->cl_cb_state
== NFSD4_CB_UNKNOWN
;
4907 static struct file_lock
*nfs4_alloc_init_lease(struct nfs4_delegation
*dp
,
4910 struct file_lock
*fl
;
4912 fl
= locks_alloc_lock();
4915 fl
->fl_lmops
= &nfsd_lease_mng_ops
;
4916 fl
->fl_flags
= FL_DELEG
;
4917 fl
->fl_type
= flag
== NFS4_OPEN_DELEGATE_READ
? F_RDLCK
: F_WRLCK
;
4918 fl
->fl_end
= OFFSET_MAX
;
4919 fl
->fl_owner
= (fl_owner_t
)dp
;
4920 fl
->fl_pid
= current
->tgid
;
4921 fl
->fl_file
= dp
->dl_stid
.sc_file
->fi_deleg_file
->nf_file
;
4925 static struct nfs4_delegation
*
4926 nfs4_set_delegation(struct nfs4_client
*clp
, struct svc_fh
*fh
,
4927 struct nfs4_file
*fp
, struct nfs4_clnt_odstate
*odstate
)
4930 struct nfs4_delegation
*dp
;
4931 struct nfsd_file
*nf
;
4932 struct file_lock
*fl
;
4935 * The fi_had_conflict and nfs_get_existing_delegation checks
4936 * here are just optimizations; we'll need to recheck them at
4939 if (fp
->fi_had_conflict
)
4940 return ERR_PTR(-EAGAIN
);
4942 nf
= find_readable_file(fp
);
4944 /* We should always have a readable file here */
4946 return ERR_PTR(-EBADF
);
4948 spin_lock(&state_lock
);
4949 spin_lock(&fp
->fi_lock
);
4950 if (nfs4_delegation_exists(clp
, fp
))
4952 else if (!fp
->fi_deleg_file
) {
4953 fp
->fi_deleg_file
= nf
;
4954 /* increment early to prevent fi_deleg_file from being
4956 fp
->fi_delegees
= 1;
4960 spin_unlock(&fp
->fi_lock
);
4961 spin_unlock(&state_lock
);
4965 return ERR_PTR(status
);
4968 dp
= alloc_init_deleg(clp
, fp
, fh
, odstate
);
4972 fl
= nfs4_alloc_init_lease(dp
, NFS4_OPEN_DELEGATE_READ
);
4974 goto out_clnt_odstate
;
4976 status
= vfs_setlease(fp
->fi_deleg_file
->nf_file
, fl
->fl_type
, &fl
, NULL
);
4978 locks_free_lock(fl
);
4980 goto out_clnt_odstate
;
4982 spin_lock(&state_lock
);
4983 spin_lock(&fp
->fi_lock
);
4984 if (fp
->fi_had_conflict
)
4987 status
= hash_delegation_locked(dp
, fp
);
4988 spin_unlock(&fp
->fi_lock
);
4989 spin_unlock(&state_lock
);
4996 vfs_setlease(fp
->fi_deleg_file
->nf_file
, F_UNLCK
, NULL
, (void **)&dp
);
4998 put_clnt_odstate(dp
->dl_clnt_odstate
);
4999 nfs4_put_stid(&dp
->dl_stid
);
5002 return ERR_PTR(status
);
5005 static void nfsd4_open_deleg_none_ext(struct nfsd4_open
*open
, int status
)
5007 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
5008 if (status
== -EAGAIN
)
5009 open
->op_why_no_deleg
= WND4_CONTENTION
;
5011 open
->op_why_no_deleg
= WND4_RESOURCE
;
5012 switch (open
->op_deleg_want
) {
5013 case NFS4_SHARE_WANT_READ_DELEG
:
5014 case NFS4_SHARE_WANT_WRITE_DELEG
:
5015 case NFS4_SHARE_WANT_ANY_DELEG
:
5017 case NFS4_SHARE_WANT_CANCEL
:
5018 open
->op_why_no_deleg
= WND4_CANCELLED
;
5020 case NFS4_SHARE_WANT_NO_DELEG
:
5027 * Attempt to hand out a delegation.
5029 * Note we don't support write delegations, and won't until the vfs has
5030 * proper support for them.
5033 nfs4_open_delegation(struct svc_fh
*fh
, struct nfsd4_open
*open
,
5034 struct nfs4_ol_stateid
*stp
)
5036 struct nfs4_delegation
*dp
;
5037 struct nfs4_openowner
*oo
= openowner(stp
->st_stateowner
);
5038 struct nfs4_client
*clp
= stp
->st_stid
.sc_client
;
5042 cb_up
= nfsd4_cb_channel_good(oo
->oo_owner
.so_client
);
5043 open
->op_recall
= 0;
5044 switch (open
->op_claim_type
) {
5045 case NFS4_OPEN_CLAIM_PREVIOUS
:
5047 open
->op_recall
= 1;
5048 if (open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_READ
)
5051 case NFS4_OPEN_CLAIM_NULL
:
5052 case NFS4_OPEN_CLAIM_FH
:
5054 * Let's not give out any delegations till everyone's
5055 * had the chance to reclaim theirs, *and* until
5056 * NLM locks have all been reclaimed:
5058 if (locks_in_grace(clp
->net
))
5060 if (!cb_up
|| !(oo
->oo_flags
& NFS4_OO_CONFIRMED
))
5063 * Also, if the file was opened for write or
5064 * create, there's a good chance the client's
5065 * about to write to it, resulting in an
5066 * immediate recall (since we don't support
5067 * write delegations):
5069 if (open
->op_share_access
& NFS4_SHARE_ACCESS_WRITE
)
5071 if (open
->op_create
== NFS4_OPEN_CREATE
)
5077 dp
= nfs4_set_delegation(clp
, fh
, stp
->st_stid
.sc_file
, stp
->st_clnt_odstate
);
5081 memcpy(&open
->op_delegate_stateid
, &dp
->dl_stid
.sc_stateid
, sizeof(dp
->dl_stid
.sc_stateid
));
5083 trace_nfsd_deleg_open(&dp
->dl_stid
.sc_stateid
);
5084 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_READ
;
5085 nfs4_put_stid(&dp
->dl_stid
);
5088 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE
;
5089 if (open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
&&
5090 open
->op_delegate_type
!= NFS4_OPEN_DELEGATE_NONE
) {
5091 dprintk("NFSD: WARNING: refusing delegation reclaim\n");
5092 open
->op_recall
= 1;
5095 /* 4.1 client asking for a delegation? */
5096 if (open
->op_deleg_want
)
5097 nfsd4_open_deleg_none_ext(open
, status
);
5101 static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open
*open
,
5102 struct nfs4_delegation
*dp
)
5104 if (open
->op_deleg_want
== NFS4_SHARE_WANT_READ_DELEG
&&
5105 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
5106 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
5107 open
->op_why_no_deleg
= WND4_NOT_SUPP_DOWNGRADE
;
5108 } else if (open
->op_deleg_want
== NFS4_SHARE_WANT_WRITE_DELEG
&&
5109 dp
->dl_type
== NFS4_OPEN_DELEGATE_WRITE
) {
5110 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
5111 open
->op_why_no_deleg
= WND4_NOT_SUPP_UPGRADE
;
5113 /* Otherwise the client must be confused wanting a delegation
5114 * it already has, therefore we don't return
5115 * NFS4_OPEN_DELEGATE_NONE_EXT and reason.
5120 nfsd4_process_open2(struct svc_rqst
*rqstp
, struct svc_fh
*current_fh
, struct nfsd4_open
*open
)
5122 struct nfsd4_compoundres
*resp
= rqstp
->rq_resp
;
5123 struct nfs4_client
*cl
= open
->op_openowner
->oo_owner
.so_client
;
5124 struct nfs4_file
*fp
= NULL
;
5125 struct nfs4_ol_stateid
*stp
= NULL
;
5126 struct nfs4_delegation
*dp
= NULL
;
5128 bool new_stp
= false;
5131 * Lookup file; if found, lookup stateid and check open request,
5132 * and check for delegations in the process of being recalled.
5133 * If not found, create the nfs4_file struct
5135 fp
= find_or_add_file(open
->op_file
, ¤t_fh
->fh_handle
);
5136 if (fp
!= open
->op_file
) {
5137 status
= nfs4_check_deleg(cl
, open
, &dp
);
5140 stp
= nfsd4_find_and_lock_existing_open(fp
, open
);
5142 open
->op_file
= NULL
;
5143 status
= nfserr_bad_stateid
;
5144 if (nfsd4_is_deleg_cur(open
))
5149 stp
= init_open_stateid(fp
, open
);
5155 * OPEN the file, or upgrade an existing OPEN.
5156 * If truncate fails, the OPEN fails.
5158 * stp is already locked.
5161 /* Stateid was found, this is an OPEN upgrade */
5162 status
= nfs4_upgrade_open(rqstp
, fp
, current_fh
, stp
, open
);
5164 mutex_unlock(&stp
->st_mutex
);
5168 status
= nfs4_get_vfs_file(rqstp
, fp
, current_fh
, stp
, open
);
5170 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
5171 release_open_stateid(stp
);
5172 mutex_unlock(&stp
->st_mutex
);
5176 stp
->st_clnt_odstate
= find_or_hash_clnt_odstate(fp
,
5178 if (stp
->st_clnt_odstate
== open
->op_odstate
)
5179 open
->op_odstate
= NULL
;
5182 nfs4_inc_and_copy_stateid(&open
->op_stateid
, &stp
->st_stid
);
5183 mutex_unlock(&stp
->st_mutex
);
5185 if (nfsd4_has_session(&resp
->cstate
)) {
5186 if (open
->op_deleg_want
& NFS4_SHARE_WANT_NO_DELEG
) {
5187 open
->op_delegate_type
= NFS4_OPEN_DELEGATE_NONE_EXT
;
5188 open
->op_why_no_deleg
= WND4_NOT_WANTED
;
5194 * Attempt to hand out a delegation. No error return, because the
5195 * OPEN succeeds even if we fail.
5197 nfs4_open_delegation(current_fh
, open
, stp
);
5200 trace_nfsd_deleg_none(&stp
->st_stid
.sc_stateid
);
5202 /* 4.1 client trying to upgrade/downgrade delegation? */
5203 if (open
->op_delegate_type
== NFS4_OPEN_DELEGATE_NONE
&& dp
&&
5204 open
->op_deleg_want
)
5205 nfsd4_deleg_xgrade_none_ext(open
, dp
);
5209 if (status
== 0 && open
->op_claim_type
== NFS4_OPEN_CLAIM_PREVIOUS
)
5210 open
->op_openowner
->oo_flags
|= NFS4_OO_CONFIRMED
;
5212 * To finish the open response, we just need to set the rflags.
5214 open
->op_rflags
= NFS4_OPEN_RESULT_LOCKTYPE_POSIX
;
5215 if (nfsd4_has_session(&resp
->cstate
))
5216 open
->op_rflags
|= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK
;
5217 else if (!(open
->op_openowner
->oo_flags
& NFS4_OO_CONFIRMED
))
5218 open
->op_rflags
|= NFS4_OPEN_RESULT_CONFIRM
;
5221 nfs4_put_stid(&dp
->dl_stid
);
5223 nfs4_put_stid(&stp
->st_stid
);
5228 void nfsd4_cleanup_open_state(struct nfsd4_compound_state
*cstate
,
5229 struct nfsd4_open
*open
)
5231 if (open
->op_openowner
) {
5232 struct nfs4_stateowner
*so
= &open
->op_openowner
->oo_owner
;
5234 nfsd4_cstate_assign_replay(cstate
, so
);
5235 nfs4_put_stateowner(so
);
5238 kmem_cache_free(file_slab
, open
->op_file
);
5240 nfs4_put_stid(&open
->op_stp
->st_stid
);
5241 if (open
->op_odstate
)
5242 kmem_cache_free(odstate_slab
, open
->op_odstate
);
5246 nfsd4_renew(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5247 union nfsd4_op_u
*u
)
5249 clientid_t
*clid
= &u
->renew
;
5250 struct nfs4_client
*clp
;
5252 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
5254 trace_nfsd_clid_renew(clid
);
5255 status
= lookup_clientid(clid
, cstate
, nn
, false);
5259 status
= nfserr_cb_path_down
;
5260 if (!list_empty(&clp
->cl_delegations
)
5261 && clp
->cl_cb_state
!= NFSD4_CB_UP
)
5269 nfsd4_end_grace(struct nfsd_net
*nn
)
5271 /* do nothing if grace period already ended */
5272 if (nn
->grace_ended
)
5275 trace_nfsd_grace_complete(nn
);
5276 nn
->grace_ended
= true;
5278 * If the server goes down again right now, an NFSv4
5279 * client will still be allowed to reclaim after it comes back up,
5280 * even if it hasn't yet had a chance to reclaim state this time.
5283 nfsd4_record_grace_done(nn
);
5285 * At this point, NFSv4 clients can still reclaim. But if the
5286 * server crashes, any that have not yet reclaimed will be out
5287 * of luck on the next boot.
5289 * (NFSv4.1+ clients are considered to have reclaimed once they
5290 * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to
5291 * have reclaimed after their first OPEN.)
5293 locks_end_grace(&nn
->nfsd4_manager
);
5295 * At this point, and once lockd and/or any other containers
5296 * exit their grace period, further reclaims will fail and
5297 * regular locking can resume.
5302 * If we've waited a lease period but there are still clients trying to
5303 * reclaim, wait a little longer to give them a chance to finish.
5305 static bool clients_still_reclaiming(struct nfsd_net
*nn
)
5307 time64_t double_grace_period_end
= nn
->boot_time
+
5308 2 * nn
->nfsd4_lease
;
5310 if (nn
->track_reclaim_completes
&&
5311 atomic_read(&nn
->nr_reclaim_complete
) ==
5312 nn
->reclaim_str_hashtbl_size
)
5314 if (!nn
->somebody_reclaimed
)
5316 nn
->somebody_reclaimed
= false;
5318 * If we've given them *two* lease times to reclaim, and they're
5319 * still not done, give up:
5321 if (ktime_get_boottime_seconds() > double_grace_period_end
)
5327 nfs4_laundromat(struct nfsd_net
*nn
)
5329 struct nfs4_client
*clp
;
5330 struct nfs4_openowner
*oo
;
5331 struct nfs4_delegation
*dp
;
5332 struct nfs4_ol_stateid
*stp
;
5333 struct nfsd4_blocked_lock
*nbl
;
5334 struct list_head
*pos
, *next
, reaplist
;
5335 time64_t cutoff
= ktime_get_boottime_seconds() - nn
->nfsd4_lease
;
5336 time64_t t
, new_timeo
= nn
->nfsd4_lease
;
5337 struct nfs4_cpntf_state
*cps
;
5338 copy_stateid_t
*cps_t
;
5341 if (clients_still_reclaiming(nn
)) {
5345 nfsd4_end_grace(nn
);
5346 INIT_LIST_HEAD(&reaplist
);
5348 spin_lock(&nn
->s2s_cp_lock
);
5349 idr_for_each_entry(&nn
->s2s_cp_stateids
, cps_t
, i
) {
5350 cps
= container_of(cps_t
, struct nfs4_cpntf_state
, cp_stateid
);
5351 if (cps
->cp_stateid
.sc_type
== NFS4_COPYNOTIFY_STID
&&
5352 cps
->cpntf_time
> cutoff
)
5353 _free_cpntf_state_locked(nn
, cps
);
5355 spin_unlock(&nn
->s2s_cp_lock
);
5357 spin_lock(&nn
->client_lock
);
5358 list_for_each_safe(pos
, next
, &nn
->client_lru
) {
5359 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
5360 if (clp
->cl_time
> cutoff
) {
5361 t
= clp
->cl_time
- cutoff
;
5362 new_timeo
= min(new_timeo
, t
);
5365 if (mark_client_expired_locked(clp
)) {
5366 trace_nfsd_clid_expired(&clp
->cl_clientid
);
5369 list_add(&clp
->cl_lru
, &reaplist
);
5371 spin_unlock(&nn
->client_lock
);
5372 list_for_each_safe(pos
, next
, &reaplist
) {
5373 clp
= list_entry(pos
, struct nfs4_client
, cl_lru
);
5374 trace_nfsd_clid_purged(&clp
->cl_clientid
);
5375 list_del_init(&clp
->cl_lru
);
5378 spin_lock(&state_lock
);
5379 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
5380 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
5381 if (dp
->dl_time
> cutoff
) {
5382 t
= dp
->dl_time
- cutoff
;
5383 new_timeo
= min(new_timeo
, t
);
5386 WARN_ON(!unhash_delegation_locked(dp
));
5387 list_add(&dp
->dl_recall_lru
, &reaplist
);
5389 spin_unlock(&state_lock
);
5390 while (!list_empty(&reaplist
)) {
5391 dp
= list_first_entry(&reaplist
, struct nfs4_delegation
,
5393 list_del_init(&dp
->dl_recall_lru
);
5394 revoke_delegation(dp
);
5397 spin_lock(&nn
->client_lock
);
5398 while (!list_empty(&nn
->close_lru
)) {
5399 oo
= list_first_entry(&nn
->close_lru
, struct nfs4_openowner
,
5401 if (oo
->oo_time
> cutoff
) {
5402 t
= oo
->oo_time
- cutoff
;
5403 new_timeo
= min(new_timeo
, t
);
5406 list_del_init(&oo
->oo_close_lru
);
5407 stp
= oo
->oo_last_closed_stid
;
5408 oo
->oo_last_closed_stid
= NULL
;
5409 spin_unlock(&nn
->client_lock
);
5410 nfs4_put_stid(&stp
->st_stid
);
5411 spin_lock(&nn
->client_lock
);
5413 spin_unlock(&nn
->client_lock
);
5416 * It's possible for a client to try and acquire an already held lock
5417 * that is being held for a long time, and then lose interest in it.
5418 * So, we clean out any un-revisited request after a lease period
5419 * under the assumption that the client is no longer interested.
5421 * RFC5661, sec. 9.6 states that the client must not rely on getting
5422 * notifications and must continue to poll for locks, even when the
5423 * server supports them. Thus this shouldn't lead to clients blocking
5424 * indefinitely once the lock does become free.
5426 BUG_ON(!list_empty(&reaplist
));
5427 spin_lock(&nn
->blocked_locks_lock
);
5428 while (!list_empty(&nn
->blocked_locks_lru
)) {
5429 nbl
= list_first_entry(&nn
->blocked_locks_lru
,
5430 struct nfsd4_blocked_lock
, nbl_lru
);
5431 if (nbl
->nbl_time
> cutoff
) {
5432 t
= nbl
->nbl_time
- cutoff
;
5433 new_timeo
= min(new_timeo
, t
);
5436 list_move(&nbl
->nbl_lru
, &reaplist
);
5437 list_del_init(&nbl
->nbl_list
);
5439 spin_unlock(&nn
->blocked_locks_lock
);
5441 while (!list_empty(&reaplist
)) {
5442 nbl
= list_first_entry(&reaplist
,
5443 struct nfsd4_blocked_lock
, nbl_lru
);
5444 list_del_init(&nbl
->nbl_lru
);
5445 free_blocked_lock(nbl
);
5448 new_timeo
= max_t(time64_t
, new_timeo
, NFSD_LAUNDROMAT_MINTIMEOUT
);
5452 static struct workqueue_struct
*laundry_wq
;
5453 static void laundromat_main(struct work_struct
*);
5456 laundromat_main(struct work_struct
*laundry
)
5459 struct delayed_work
*dwork
= to_delayed_work(laundry
);
5460 struct nfsd_net
*nn
= container_of(dwork
, struct nfsd_net
,
5463 t
= nfs4_laundromat(nn
);
5464 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, t
*HZ
);
5467 static inline __be32
nfs4_check_fh(struct svc_fh
*fhp
, struct nfs4_stid
*stp
)
5469 if (!fh_match(&fhp
->fh_handle
, &stp
->sc_file
->fi_fhandle
))
5470 return nfserr_bad_stateid
;
5475 access_permit_read(struct nfs4_ol_stateid
*stp
)
5477 return test_access(NFS4_SHARE_ACCESS_READ
, stp
) ||
5478 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
) ||
5479 test_access(NFS4_SHARE_ACCESS_WRITE
, stp
);
5483 access_permit_write(struct nfs4_ol_stateid
*stp
)
5485 return test_access(NFS4_SHARE_ACCESS_WRITE
, stp
) ||
5486 test_access(NFS4_SHARE_ACCESS_BOTH
, stp
);
5490 __be32
nfs4_check_openmode(struct nfs4_ol_stateid
*stp
, int flags
)
5492 __be32 status
= nfserr_openmode
;
5494 /* For lock stateid's, we test the parent open, not the lock: */
5495 if (stp
->st_openstp
)
5496 stp
= stp
->st_openstp
;
5497 if ((flags
& WR_STATE
) && !access_permit_write(stp
))
5499 if ((flags
& RD_STATE
) && !access_permit_read(stp
))
5506 static inline __be32
5507 check_special_stateids(struct net
*net
, svc_fh
*current_fh
, stateid_t
*stateid
, int flags
)
5509 if (ONE_STATEID(stateid
) && (flags
& RD_STATE
))
5511 else if (opens_in_grace(net
)) {
5512 /* Answer in remaining cases depends on existence of
5513 * conflicting state; so we must wait out the grace period. */
5514 return nfserr_grace
;
5515 } else if (flags
& WR_STATE
)
5516 return nfs4_share_conflict(current_fh
,
5517 NFS4_SHARE_DENY_WRITE
);
5518 else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */
5519 return nfs4_share_conflict(current_fh
,
5520 NFS4_SHARE_DENY_READ
);
5524 * Allow READ/WRITE during grace period on recovered state only for files
5525 * that are not able to provide mandatory locking.
5528 grace_disallows_io(struct net
*net
, struct inode
*inode
)
5530 return opens_in_grace(net
) && mandatory_lock(inode
);
5533 static __be32
check_stateid_generation(stateid_t
*in
, stateid_t
*ref
, bool has_session
)
5536 * When sessions are used the stateid generation number is ignored
5539 if (has_session
&& in
->si_generation
== 0)
5542 if (in
->si_generation
== ref
->si_generation
)
5545 /* If the client sends us a stateid from the future, it's buggy: */
5546 if (nfsd4_stateid_generation_after(in
, ref
))
5547 return nfserr_bad_stateid
;
5549 * However, we could see a stateid from the past, even from a
5550 * non-buggy client. For example, if the client sends a lock
5551 * while some IO is outstanding, the lock may bump si_generation
5552 * while the IO is still in flight. The client could avoid that
5553 * situation by waiting for responses on all the IO requests,
5554 * but better performance may result in retrying IO that
5555 * receives an old_stateid error if requests are rarely
5556 * reordered in flight:
5558 return nfserr_old_stateid
;
5561 static __be32
nfsd4_stid_check_stateid_generation(stateid_t
*in
, struct nfs4_stid
*s
, bool has_session
)
5565 spin_lock(&s
->sc_lock
);
5566 ret
= nfsd4_verify_open_stid(s
);
5568 ret
= check_stateid_generation(in
, &s
->sc_stateid
, has_session
);
5569 spin_unlock(&s
->sc_lock
);
5573 static __be32
nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid
*ols
)
5575 if (ols
->st_stateowner
->so_is_open_owner
&&
5576 !(openowner(ols
->st_stateowner
)->oo_flags
& NFS4_OO_CONFIRMED
))
5577 return nfserr_bad_stateid
;
5581 static __be32
nfsd4_validate_stateid(struct nfs4_client
*cl
, stateid_t
*stateid
)
5583 struct nfs4_stid
*s
;
5584 __be32 status
= nfserr_bad_stateid
;
5586 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
5587 CLOSE_STATEID(stateid
))
5589 if (!same_clid(&stateid
->si_opaque
.so_clid
, &cl
->cl_clientid
))
5591 spin_lock(&cl
->cl_lock
);
5592 s
= find_stateid_locked(cl
, stateid
);
5595 status
= nfsd4_stid_check_stateid_generation(stateid
, s
, 1);
5598 switch (s
->sc_type
) {
5599 case NFS4_DELEG_STID
:
5602 case NFS4_REVOKED_DELEG_STID
:
5603 status
= nfserr_deleg_revoked
;
5605 case NFS4_OPEN_STID
:
5606 case NFS4_LOCK_STID
:
5607 status
= nfsd4_check_openowner_confirmed(openlockstateid(s
));
5610 printk("unknown stateid type %x\n", s
->sc_type
);
5612 case NFS4_CLOSED_STID
:
5613 case NFS4_CLOSED_DELEG_STID
:
5614 status
= nfserr_bad_stateid
;
5617 spin_unlock(&cl
->cl_lock
);
5622 nfsd4_lookup_stateid(struct nfsd4_compound_state
*cstate
,
5623 stateid_t
*stateid
, unsigned char typemask
,
5624 struct nfs4_stid
**s
, struct nfsd_net
*nn
)
5627 bool return_revoked
= false;
5630 * only return revoked delegations if explicitly asked.
5631 * otherwise we report revoked or bad_stateid status.
5633 if (typemask
& NFS4_REVOKED_DELEG_STID
)
5634 return_revoked
= true;
5635 else if (typemask
& NFS4_DELEG_STID
)
5636 typemask
|= NFS4_REVOKED_DELEG_STID
;
5638 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
) ||
5639 CLOSE_STATEID(stateid
))
5640 return nfserr_bad_stateid
;
5641 status
= lookup_clientid(&stateid
->si_opaque
.so_clid
, cstate
, nn
,
5643 if (status
== nfserr_stale_clientid
) {
5644 if (cstate
->session
)
5645 return nfserr_bad_stateid
;
5646 return nfserr_stale_stateid
;
5650 *s
= find_stateid_by_type(cstate
->clp
, stateid
, typemask
);
5652 return nfserr_bad_stateid
;
5653 if (((*s
)->sc_type
== NFS4_REVOKED_DELEG_STID
) && !return_revoked
) {
5655 if (cstate
->minorversion
)
5656 return nfserr_deleg_revoked
;
5657 return nfserr_bad_stateid
;
5662 static struct nfsd_file
*
5663 nfs4_find_file(struct nfs4_stid
*s
, int flags
)
5668 switch (s
->sc_type
) {
5669 case NFS4_DELEG_STID
:
5670 if (WARN_ON_ONCE(!s
->sc_file
->fi_deleg_file
))
5672 return nfsd_file_get(s
->sc_file
->fi_deleg_file
);
5673 case NFS4_OPEN_STID
:
5674 case NFS4_LOCK_STID
:
5675 if (flags
& RD_STATE
)
5676 return find_readable_file(s
->sc_file
);
5678 return find_writeable_file(s
->sc_file
);
5686 nfs4_check_olstateid(struct nfs4_ol_stateid
*ols
, int flags
)
5690 status
= nfsd4_check_openowner_confirmed(ols
);
5693 return nfs4_check_openmode(ols
, flags
);
5697 nfs4_check_file(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct nfs4_stid
*s
,
5698 struct nfsd_file
**nfp
, int flags
)
5700 int acc
= (flags
& RD_STATE
) ? NFSD_MAY_READ
: NFSD_MAY_WRITE
;
5701 struct nfsd_file
*nf
;
5704 nf
= nfs4_find_file(s
, flags
);
5706 status
= nfsd_permission(rqstp
, fhp
->fh_export
, fhp
->fh_dentry
,
5707 acc
| NFSD_MAY_OWNER_OVERRIDE
);
5713 status
= nfsd_file_acquire(rqstp
, fhp
, acc
, &nf
);
5722 _free_cpntf_state_locked(struct nfsd_net
*nn
, struct nfs4_cpntf_state
*cps
)
5724 WARN_ON_ONCE(cps
->cp_stateid
.sc_type
!= NFS4_COPYNOTIFY_STID
);
5725 if (!refcount_dec_and_test(&cps
->cp_stateid
.sc_count
))
5727 list_del(&cps
->cp_list
);
5728 idr_remove(&nn
->s2s_cp_stateids
,
5729 cps
->cp_stateid
.stid
.si_opaque
.so_id
);
5733 * A READ from an inter server to server COPY will have a
5734 * copy stateid. Look up the copy notify stateid from the
5735 * idr structure and take a reference on it.
5737 __be32
manage_cpntf_state(struct nfsd_net
*nn
, stateid_t
*st
,
5738 struct nfs4_client
*clp
,
5739 struct nfs4_cpntf_state
**cps
)
5741 copy_stateid_t
*cps_t
;
5742 struct nfs4_cpntf_state
*state
= NULL
;
5744 if (st
->si_opaque
.so_clid
.cl_id
!= nn
->s2s_cp_cl_id
)
5745 return nfserr_bad_stateid
;
5746 spin_lock(&nn
->s2s_cp_lock
);
5747 cps_t
= idr_find(&nn
->s2s_cp_stateids
, st
->si_opaque
.so_id
);
5749 state
= container_of(cps_t
, struct nfs4_cpntf_state
,
5751 if (state
->cp_stateid
.sc_type
!= NFS4_COPYNOTIFY_STID
) {
5756 refcount_inc(&state
->cp_stateid
.sc_count
);
5758 _free_cpntf_state_locked(nn
, state
);
5761 spin_unlock(&nn
->s2s_cp_lock
);
5763 return nfserr_bad_stateid
;
5769 static __be32
find_cpntf_state(struct nfsd_net
*nn
, stateid_t
*st
,
5770 struct nfs4_stid
**stid
)
5773 struct nfs4_cpntf_state
*cps
= NULL
;
5774 struct nfsd4_compound_state cstate
;
5776 status
= manage_cpntf_state(nn
, st
, NULL
, &cps
);
5780 cps
->cpntf_time
= ktime_get_boottime_seconds();
5781 memset(&cstate
, 0, sizeof(cstate
));
5782 status
= lookup_clientid(&cps
->cp_p_clid
, &cstate
, nn
, true);
5785 status
= nfsd4_lookup_stateid(&cstate
, &cps
->cp_p_stateid
,
5786 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
5788 put_client_renew(cstate
.clp
);
5790 nfs4_put_cpntf_state(nn
, cps
);
5794 void nfs4_put_cpntf_state(struct nfsd_net
*nn
, struct nfs4_cpntf_state
*cps
)
5796 spin_lock(&nn
->s2s_cp_lock
);
5797 _free_cpntf_state_locked(nn
, cps
);
5798 spin_unlock(&nn
->s2s_cp_lock
);
5802 * Checks for stateid operations
5805 nfs4_preprocess_stateid_op(struct svc_rqst
*rqstp
,
5806 struct nfsd4_compound_state
*cstate
, struct svc_fh
*fhp
,
5807 stateid_t
*stateid
, int flags
, struct nfsd_file
**nfp
,
5808 struct nfs4_stid
**cstid
)
5810 struct inode
*ino
= d_inode(fhp
->fh_dentry
);
5811 struct net
*net
= SVC_NET(rqstp
);
5812 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
5813 struct nfs4_stid
*s
= NULL
;
5819 if (grace_disallows_io(net
, ino
))
5820 return nfserr_grace
;
5822 if (ZERO_STATEID(stateid
) || ONE_STATEID(stateid
)) {
5823 status
= check_special_stateids(net
, fhp
, stateid
, flags
);
5827 status
= nfsd4_lookup_stateid(cstate
, stateid
,
5828 NFS4_DELEG_STID
|NFS4_OPEN_STID
|NFS4_LOCK_STID
,
5830 if (status
== nfserr_bad_stateid
)
5831 status
= find_cpntf_state(nn
, stateid
, &s
);
5834 status
= nfsd4_stid_check_stateid_generation(stateid
, s
,
5835 nfsd4_has_session(cstate
));
5839 switch (s
->sc_type
) {
5840 case NFS4_DELEG_STID
:
5841 status
= nfs4_check_delegmode(delegstateid(s
), flags
);
5843 case NFS4_OPEN_STID
:
5844 case NFS4_LOCK_STID
:
5845 status
= nfs4_check_olstateid(openlockstateid(s
), flags
);
5848 status
= nfserr_bad_stateid
;
5853 status
= nfs4_check_fh(fhp
, s
);
5856 if (status
== nfs_ok
&& nfp
)
5857 status
= nfs4_check_file(rqstp
, fhp
, s
, nfp
, flags
);
5860 if (!status
&& cstid
)
5869 * Test if the stateid is valid
5872 nfsd4_test_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5873 union nfsd4_op_u
*u
)
5875 struct nfsd4_test_stateid
*test_stateid
= &u
->test_stateid
;
5876 struct nfsd4_test_stateid_id
*stateid
;
5877 struct nfs4_client
*cl
= cstate
->session
->se_client
;
5879 list_for_each_entry(stateid
, &test_stateid
->ts_stateid_list
, ts_id_list
)
5880 stateid
->ts_id_status
=
5881 nfsd4_validate_stateid(cl
, &stateid
->ts_id_stateid
);
5887 nfsd4_free_lock_stateid(stateid_t
*stateid
, struct nfs4_stid
*s
)
5889 struct nfs4_ol_stateid
*stp
= openlockstateid(s
);
5892 ret
= nfsd4_lock_ol_stateid(stp
);
5896 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
5900 ret
= nfserr_locks_held
;
5901 if (check_for_locks(stp
->st_stid
.sc_file
,
5902 lockowner(stp
->st_stateowner
)))
5905 release_lock_stateid(stp
);
5909 mutex_unlock(&stp
->st_mutex
);
5916 nfsd4_free_stateid(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
5917 union nfsd4_op_u
*u
)
5919 struct nfsd4_free_stateid
*free_stateid
= &u
->free_stateid
;
5920 stateid_t
*stateid
= &free_stateid
->fr_stateid
;
5921 struct nfs4_stid
*s
;
5922 struct nfs4_delegation
*dp
;
5923 struct nfs4_client
*cl
= cstate
->session
->se_client
;
5924 __be32 ret
= nfserr_bad_stateid
;
5926 spin_lock(&cl
->cl_lock
);
5927 s
= find_stateid_locked(cl
, stateid
);
5930 spin_lock(&s
->sc_lock
);
5931 switch (s
->sc_type
) {
5932 case NFS4_DELEG_STID
:
5933 ret
= nfserr_locks_held
;
5935 case NFS4_OPEN_STID
:
5936 ret
= check_stateid_generation(stateid
, &s
->sc_stateid
, 1);
5939 ret
= nfserr_locks_held
;
5941 case NFS4_LOCK_STID
:
5942 spin_unlock(&s
->sc_lock
);
5943 refcount_inc(&s
->sc_count
);
5944 spin_unlock(&cl
->cl_lock
);
5945 ret
= nfsd4_free_lock_stateid(stateid
, s
);
5947 case NFS4_REVOKED_DELEG_STID
:
5948 spin_unlock(&s
->sc_lock
);
5949 dp
= delegstateid(s
);
5950 list_del_init(&dp
->dl_recall_lru
);
5951 spin_unlock(&cl
->cl_lock
);
5955 /* Default falls through and returns nfserr_bad_stateid */
5957 spin_unlock(&s
->sc_lock
);
5959 spin_unlock(&cl
->cl_lock
);
5967 return (type
== NFS4_READW_LT
|| type
== NFS4_READ_LT
) ?
5968 RD_STATE
: WR_STATE
;
5971 static __be32
nfs4_seqid_op_checks(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
, u32 seqid
, struct nfs4_ol_stateid
*stp
)
5973 struct svc_fh
*current_fh
= &cstate
->current_fh
;
5974 struct nfs4_stateowner
*sop
= stp
->st_stateowner
;
5977 status
= nfsd4_check_seqid(cstate
, sop
, seqid
);
5980 status
= nfsd4_lock_ol_stateid(stp
);
5981 if (status
!= nfs_ok
)
5983 status
= check_stateid_generation(stateid
, &stp
->st_stid
.sc_stateid
, nfsd4_has_session(cstate
));
5984 if (status
== nfs_ok
)
5985 status
= nfs4_check_fh(current_fh
, &stp
->st_stid
);
5986 if (status
!= nfs_ok
)
5987 mutex_unlock(&stp
->st_mutex
);
5992 * Checks for sequence id mutating operations.
5995 nfs4_preprocess_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
5996 stateid_t
*stateid
, char typemask
,
5997 struct nfs4_ol_stateid
**stpp
,
5998 struct nfsd_net
*nn
)
6001 struct nfs4_stid
*s
;
6002 struct nfs4_ol_stateid
*stp
= NULL
;
6004 trace_nfsd_preprocess(seqid
, stateid
);
6007 status
= nfsd4_lookup_stateid(cstate
, stateid
, typemask
, &s
, nn
);
6010 stp
= openlockstateid(s
);
6011 nfsd4_cstate_assign_replay(cstate
, stp
->st_stateowner
);
6013 status
= nfs4_seqid_op_checks(cstate
, stateid
, seqid
, stp
);
6017 nfs4_put_stid(&stp
->st_stid
);
6021 static __be32
nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state
*cstate
, u32 seqid
,
6022 stateid_t
*stateid
, struct nfs4_ol_stateid
**stpp
, struct nfsd_net
*nn
)
6025 struct nfs4_openowner
*oo
;
6026 struct nfs4_ol_stateid
*stp
;
6028 status
= nfs4_preprocess_seqid_op(cstate
, seqid
, stateid
,
6029 NFS4_OPEN_STID
, &stp
, nn
);
6032 oo
= openowner(stp
->st_stateowner
);
6033 if (!(oo
->oo_flags
& NFS4_OO_CONFIRMED
)) {
6034 mutex_unlock(&stp
->st_mutex
);
6035 nfs4_put_stid(&stp
->st_stid
);
6036 return nfserr_bad_stateid
;
6043 nfsd4_open_confirm(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6044 union nfsd4_op_u
*u
)
6046 struct nfsd4_open_confirm
*oc
= &u
->open_confirm
;
6048 struct nfs4_openowner
*oo
;
6049 struct nfs4_ol_stateid
*stp
;
6050 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6052 dprintk("NFSD: nfsd4_open_confirm on file %pd\n",
6053 cstate
->current_fh
.fh_dentry
);
6055 status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0);
6059 status
= nfs4_preprocess_seqid_op(cstate
,
6060 oc
->oc_seqid
, &oc
->oc_req_stateid
,
6061 NFS4_OPEN_STID
, &stp
, nn
);
6064 oo
= openowner(stp
->st_stateowner
);
6065 status
= nfserr_bad_stateid
;
6066 if (oo
->oo_flags
& NFS4_OO_CONFIRMED
) {
6067 mutex_unlock(&stp
->st_mutex
);
6070 oo
->oo_flags
|= NFS4_OO_CONFIRMED
;
6071 nfs4_inc_and_copy_stateid(&oc
->oc_resp_stateid
, &stp
->st_stid
);
6072 mutex_unlock(&stp
->st_mutex
);
6073 trace_nfsd_open_confirm(oc
->oc_seqid
, &stp
->st_stid
.sc_stateid
);
6074 nfsd4_client_record_create(oo
->oo_owner
.so_client
);
6077 nfs4_put_stid(&stp
->st_stid
);
6079 nfsd4_bump_seqid(cstate
, status
);
6083 static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid
*stp
, u32 access
)
6085 if (!test_access(access
, stp
))
6087 nfs4_file_put_access(stp
->st_stid
.sc_file
, access
);
6088 clear_access(access
, stp
);
6091 static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid
*stp
, u32 to_access
)
6093 switch (to_access
) {
6094 case NFS4_SHARE_ACCESS_READ
:
6095 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_WRITE
);
6096 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
6098 case NFS4_SHARE_ACCESS_WRITE
:
6099 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_READ
);
6100 nfs4_stateid_downgrade_bit(stp
, NFS4_SHARE_ACCESS_BOTH
);
6102 case NFS4_SHARE_ACCESS_BOTH
:
6110 nfsd4_open_downgrade(struct svc_rqst
*rqstp
,
6111 struct nfsd4_compound_state
*cstate
, union nfsd4_op_u
*u
)
6113 struct nfsd4_open_downgrade
*od
= &u
->open_downgrade
;
6115 struct nfs4_ol_stateid
*stp
;
6116 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6118 dprintk("NFSD: nfsd4_open_downgrade on file %pd\n",
6119 cstate
->current_fh
.fh_dentry
);
6121 /* We don't yet support WANT bits: */
6122 if (od
->od_deleg_want
)
6123 dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__
,
6126 status
= nfs4_preprocess_confirmed_seqid_op(cstate
, od
->od_seqid
,
6127 &od
->od_stateid
, &stp
, nn
);
6130 status
= nfserr_inval
;
6131 if (!test_access(od
->od_share_access
, stp
)) {
6132 dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n",
6133 stp
->st_access_bmap
, od
->od_share_access
);
6136 if (!test_deny(od
->od_share_deny
, stp
)) {
6137 dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n",
6138 stp
->st_deny_bmap
, od
->od_share_deny
);
6141 nfs4_stateid_downgrade(stp
, od
->od_share_access
);
6142 reset_union_bmap_deny(od
->od_share_deny
, stp
);
6143 nfs4_inc_and_copy_stateid(&od
->od_stateid
, &stp
->st_stid
);
6146 mutex_unlock(&stp
->st_mutex
);
6147 nfs4_put_stid(&stp
->st_stid
);
6149 nfsd4_bump_seqid(cstate
, status
);
6153 static void nfsd4_close_open_stateid(struct nfs4_ol_stateid
*s
)
6155 struct nfs4_client
*clp
= s
->st_stid
.sc_client
;
6157 LIST_HEAD(reaplist
);
6159 spin_lock(&clp
->cl_lock
);
6160 unhashed
= unhash_open_stateid(s
, &reaplist
);
6162 if (clp
->cl_minorversion
) {
6164 put_ol_stateid_locked(s
, &reaplist
);
6165 spin_unlock(&clp
->cl_lock
);
6166 free_ol_stateid_reaplist(&reaplist
);
6168 spin_unlock(&clp
->cl_lock
);
6169 free_ol_stateid_reaplist(&reaplist
);
6171 move_to_close_lru(s
, clp
->net
);
6176 * nfs4_unlock_state() called after encode
6179 nfsd4_close(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6180 union nfsd4_op_u
*u
)
6182 struct nfsd4_close
*close
= &u
->close
;
6184 struct nfs4_ol_stateid
*stp
;
6185 struct net
*net
= SVC_NET(rqstp
);
6186 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6188 dprintk("NFSD: nfsd4_close on file %pd\n",
6189 cstate
->current_fh
.fh_dentry
);
6191 status
= nfs4_preprocess_seqid_op(cstate
, close
->cl_seqid
,
6193 NFS4_OPEN_STID
|NFS4_CLOSED_STID
,
6195 nfsd4_bump_seqid(cstate
, status
);
6199 stp
->st_stid
.sc_type
= NFS4_CLOSED_STID
;
6202 * Technically we don't _really_ have to increment or copy it, since
6203 * it should just be gone after this operation and we clobber the
6204 * copied value below, but we continue to do so here just to ensure
6205 * that racing ops see that there was a state change.
6207 nfs4_inc_and_copy_stateid(&close
->cl_stateid
, &stp
->st_stid
);
6209 nfsd4_close_open_stateid(stp
);
6210 mutex_unlock(&stp
->st_mutex
);
6212 /* v4.1+ suggests that we send a special stateid in here, since the
6213 * clients should just ignore this anyway. Since this is not useful
6214 * for v4.0 clients either, we set it to the special close_stateid
6217 * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5
6219 memcpy(&close
->cl_stateid
, &close_stateid
, sizeof(close
->cl_stateid
));
6221 /* put reference from nfs4_preprocess_seqid_op */
6222 nfs4_put_stid(&stp
->st_stid
);
6228 nfsd4_delegreturn(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6229 union nfsd4_op_u
*u
)
6231 struct nfsd4_delegreturn
*dr
= &u
->delegreturn
;
6232 struct nfs4_delegation
*dp
;
6233 stateid_t
*stateid
= &dr
->dr_stateid
;
6234 struct nfs4_stid
*s
;
6236 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6238 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
6241 status
= nfsd4_lookup_stateid(cstate
, stateid
, NFS4_DELEG_STID
, &s
, nn
);
6244 dp
= delegstateid(s
);
6245 status
= nfsd4_stid_check_stateid_generation(stateid
, &dp
->dl_stid
, nfsd4_has_session(cstate
));
6249 destroy_delegation(dp
);
6251 nfs4_put_stid(&dp
->dl_stid
);
6257 end_offset(u64 start
, u64 len
)
6262 return end
>= start
? end
: NFS4_MAX_UINT64
;
6265 /* last octet in a range */
6267 last_byte_offset(u64 start
, u64 len
)
6273 return end
> start
? end
- 1: NFS4_MAX_UINT64
;
6277 * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that
6278 * we can't properly handle lock requests that go beyond the (2^63 - 1)-th
6279 * byte, because of sign extension problems. Since NFSv4 calls for 64-bit
6280 * locking, this prevents us from being completely protocol-compliant. The
6281 * real solution to this problem is to start using unsigned file offsets in
6282 * the VFS, but this is a very deep change!
6285 nfs4_transform_lock_offset(struct file_lock
*lock
)
6287 if (lock
->fl_start
< 0)
6288 lock
->fl_start
= OFFSET_MAX
;
6289 if (lock
->fl_end
< 0)
6290 lock
->fl_end
= OFFSET_MAX
;
6294 nfsd4_fl_get_owner(fl_owner_t owner
)
6296 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
6298 nfs4_get_stateowner(&lo
->lo_owner
);
6303 nfsd4_fl_put_owner(fl_owner_t owner
)
6305 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)owner
;
6308 nfs4_put_stateowner(&lo
->lo_owner
);
6312 nfsd4_lm_notify(struct file_lock
*fl
)
6314 struct nfs4_lockowner
*lo
= (struct nfs4_lockowner
*)fl
->fl_owner
;
6315 struct net
*net
= lo
->lo_owner
.so_client
->net
;
6316 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6317 struct nfsd4_blocked_lock
*nbl
= container_of(fl
,
6318 struct nfsd4_blocked_lock
, nbl_lock
);
6321 /* An empty list means that something else is going to be using it */
6322 spin_lock(&nn
->blocked_locks_lock
);
6323 if (!list_empty(&nbl
->nbl_list
)) {
6324 list_del_init(&nbl
->nbl_list
);
6325 list_del_init(&nbl
->nbl_lru
);
6328 spin_unlock(&nn
->blocked_locks_lock
);
6331 nfsd4_run_cb(&nbl
->nbl_cb
);
6334 static const struct lock_manager_operations nfsd_posix_mng_ops
= {
6335 .lm_notify
= nfsd4_lm_notify
,
6336 .lm_get_owner
= nfsd4_fl_get_owner
,
6337 .lm_put_owner
= nfsd4_fl_put_owner
,
6341 nfs4_set_lock_denied(struct file_lock
*fl
, struct nfsd4_lock_denied
*deny
)
6343 struct nfs4_lockowner
*lo
;
6345 if (fl
->fl_lmops
== &nfsd_posix_mng_ops
) {
6346 lo
= (struct nfs4_lockowner
*) fl
->fl_owner
;
6347 xdr_netobj_dup(&deny
->ld_owner
, &lo
->lo_owner
.so_owner
,
6349 if (!deny
->ld_owner
.data
)
6350 /* We just don't care that much */
6352 deny
->ld_clientid
= lo
->lo_owner
.so_client
->cl_clientid
;
6355 deny
->ld_owner
.len
= 0;
6356 deny
->ld_owner
.data
= NULL
;
6357 deny
->ld_clientid
.cl_boot
= 0;
6358 deny
->ld_clientid
.cl_id
= 0;
6360 deny
->ld_start
= fl
->fl_start
;
6361 deny
->ld_length
= NFS4_MAX_UINT64
;
6362 if (fl
->fl_end
!= NFS4_MAX_UINT64
)
6363 deny
->ld_length
= fl
->fl_end
- fl
->fl_start
+ 1;
6364 deny
->ld_type
= NFS4_READ_LT
;
6365 if (fl
->fl_type
!= F_RDLCK
)
6366 deny
->ld_type
= NFS4_WRITE_LT
;
6369 static struct nfs4_lockowner
*
6370 find_lockowner_str_locked(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
6372 unsigned int strhashval
= ownerstr_hashval(owner
);
6373 struct nfs4_stateowner
*so
;
6375 lockdep_assert_held(&clp
->cl_lock
);
6377 list_for_each_entry(so
, &clp
->cl_ownerstr_hashtbl
[strhashval
],
6379 if (so
->so_is_open_owner
)
6381 if (same_owner_str(so
, owner
))
6382 return lockowner(nfs4_get_stateowner(so
));
6387 static struct nfs4_lockowner
*
6388 find_lockowner_str(struct nfs4_client
*clp
, struct xdr_netobj
*owner
)
6390 struct nfs4_lockowner
*lo
;
6392 spin_lock(&clp
->cl_lock
);
6393 lo
= find_lockowner_str_locked(clp
, owner
);
6394 spin_unlock(&clp
->cl_lock
);
6398 static void nfs4_unhash_lockowner(struct nfs4_stateowner
*sop
)
6400 unhash_lockowner_locked(lockowner(sop
));
6403 static void nfs4_free_lockowner(struct nfs4_stateowner
*sop
)
6405 struct nfs4_lockowner
*lo
= lockowner(sop
);
6407 kmem_cache_free(lockowner_slab
, lo
);
6410 static const struct nfs4_stateowner_operations lockowner_ops
= {
6411 .so_unhash
= nfs4_unhash_lockowner
,
6412 .so_free
= nfs4_free_lockowner
,
6416 * Alloc a lock owner structure.
6417 * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has
6420 * strhashval = ownerstr_hashval
6422 static struct nfs4_lockowner
*
6423 alloc_init_lock_stateowner(unsigned int strhashval
, struct nfs4_client
*clp
,
6424 struct nfs4_ol_stateid
*open_stp
,
6425 struct nfsd4_lock
*lock
)
6427 struct nfs4_lockowner
*lo
, *ret
;
6429 lo
= alloc_stateowner(lockowner_slab
, &lock
->lk_new_owner
, clp
);
6432 INIT_LIST_HEAD(&lo
->lo_blocked
);
6433 INIT_LIST_HEAD(&lo
->lo_owner
.so_stateids
);
6434 lo
->lo_owner
.so_is_open_owner
= 0;
6435 lo
->lo_owner
.so_seqid
= lock
->lk_new_lock_seqid
;
6436 lo
->lo_owner
.so_ops
= &lockowner_ops
;
6437 spin_lock(&clp
->cl_lock
);
6438 ret
= find_lockowner_str_locked(clp
, &lock
->lk_new_owner
);
6440 list_add(&lo
->lo_owner
.so_strhash
,
6441 &clp
->cl_ownerstr_hashtbl
[strhashval
]);
6444 nfs4_free_stateowner(&lo
->lo_owner
);
6446 spin_unlock(&clp
->cl_lock
);
6450 static struct nfs4_ol_stateid
*
6451 find_lock_stateid(const struct nfs4_lockowner
*lo
,
6452 const struct nfs4_ol_stateid
*ost
)
6454 struct nfs4_ol_stateid
*lst
;
6456 lockdep_assert_held(&ost
->st_stid
.sc_client
->cl_lock
);
6458 /* If ost is not hashed, ost->st_locks will not be valid */
6459 if (!nfs4_ol_stateid_unhashed(ost
))
6460 list_for_each_entry(lst
, &ost
->st_locks
, st_locks
) {
6461 if (lst
->st_stateowner
== &lo
->lo_owner
) {
6462 refcount_inc(&lst
->st_stid
.sc_count
);
6469 static struct nfs4_ol_stateid
*
6470 init_lock_stateid(struct nfs4_ol_stateid
*stp
, struct nfs4_lockowner
*lo
,
6471 struct nfs4_file
*fp
, struct inode
*inode
,
6472 struct nfs4_ol_stateid
*open_stp
)
6474 struct nfs4_client
*clp
= lo
->lo_owner
.so_client
;
6475 struct nfs4_ol_stateid
*retstp
;
6477 mutex_init(&stp
->st_mutex
);
6478 mutex_lock_nested(&stp
->st_mutex
, OPEN_STATEID_MUTEX
);
6480 spin_lock(&clp
->cl_lock
);
6481 if (nfs4_ol_stateid_unhashed(open_stp
))
6483 retstp
= find_lock_stateid(lo
, open_stp
);
6486 refcount_inc(&stp
->st_stid
.sc_count
);
6487 stp
->st_stid
.sc_type
= NFS4_LOCK_STID
;
6488 stp
->st_stateowner
= nfs4_get_stateowner(&lo
->lo_owner
);
6490 stp
->st_stid
.sc_file
= fp
;
6491 stp
->st_access_bmap
= 0;
6492 stp
->st_deny_bmap
= open_stp
->st_deny_bmap
;
6493 stp
->st_openstp
= open_stp
;
6494 spin_lock(&fp
->fi_lock
);
6495 list_add(&stp
->st_locks
, &open_stp
->st_locks
);
6496 list_add(&stp
->st_perstateowner
, &lo
->lo_owner
.so_stateids
);
6497 list_add(&stp
->st_perfile
, &fp
->fi_stateids
);
6498 spin_unlock(&fp
->fi_lock
);
6499 spin_unlock(&clp
->cl_lock
);
6502 spin_unlock(&clp
->cl_lock
);
6503 if (nfsd4_lock_ol_stateid(retstp
) != nfs_ok
) {
6504 nfs4_put_stid(&retstp
->st_stid
);
6507 /* To keep mutex tracking happy */
6508 mutex_unlock(&stp
->st_mutex
);
6511 spin_unlock(&clp
->cl_lock
);
6512 mutex_unlock(&stp
->st_mutex
);
6516 static struct nfs4_ol_stateid
*
6517 find_or_create_lock_stateid(struct nfs4_lockowner
*lo
, struct nfs4_file
*fi
,
6518 struct inode
*inode
, struct nfs4_ol_stateid
*ost
,
6521 struct nfs4_stid
*ns
= NULL
;
6522 struct nfs4_ol_stateid
*lst
;
6523 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
6524 struct nfs4_client
*clp
= oo
->oo_owner
.so_client
;
6527 spin_lock(&clp
->cl_lock
);
6528 lst
= find_lock_stateid(lo
, ost
);
6529 spin_unlock(&clp
->cl_lock
);
6531 if (nfsd4_lock_ol_stateid(lst
) == nfs_ok
)
6533 nfs4_put_stid(&lst
->st_stid
);
6535 ns
= nfs4_alloc_stid(clp
, stateid_slab
, nfs4_free_lock_stateid
);
6539 lst
= init_lock_stateid(openlockstateid(ns
), lo
, fi
, inode
, ost
);
6540 if (lst
== openlockstateid(ns
))
6549 check_lock_length(u64 offset
, u64 length
)
6551 return ((length
== 0) || ((length
!= NFS4_MAX_UINT64
) &&
6552 (length
> ~offset
)));
6555 static void get_lock_access(struct nfs4_ol_stateid
*lock_stp
, u32 access
)
6557 struct nfs4_file
*fp
= lock_stp
->st_stid
.sc_file
;
6559 lockdep_assert_held(&fp
->fi_lock
);
6561 if (test_access(access
, lock_stp
))
6563 __nfs4_file_get_access(fp
, access
);
6564 set_access(access
, lock_stp
);
6568 lookup_or_create_lock_state(struct nfsd4_compound_state
*cstate
,
6569 struct nfs4_ol_stateid
*ost
,
6570 struct nfsd4_lock
*lock
,
6571 struct nfs4_ol_stateid
**plst
, bool *new)
6574 struct nfs4_file
*fi
= ost
->st_stid
.sc_file
;
6575 struct nfs4_openowner
*oo
= openowner(ost
->st_stateowner
);
6576 struct nfs4_client
*cl
= oo
->oo_owner
.so_client
;
6577 struct inode
*inode
= d_inode(cstate
->current_fh
.fh_dentry
);
6578 struct nfs4_lockowner
*lo
;
6579 struct nfs4_ol_stateid
*lst
;
6580 unsigned int strhashval
;
6582 lo
= find_lockowner_str(cl
, &lock
->lk_new_owner
);
6584 strhashval
= ownerstr_hashval(&lock
->lk_new_owner
);
6585 lo
= alloc_init_lock_stateowner(strhashval
, cl
, ost
, lock
);
6587 return nfserr_jukebox
;
6589 /* with an existing lockowner, seqids must be the same */
6590 status
= nfserr_bad_seqid
;
6591 if (!cstate
->minorversion
&&
6592 lock
->lk_new_lock_seqid
!= lo
->lo_owner
.so_seqid
)
6596 lst
= find_or_create_lock_stateid(lo
, fi
, inode
, ost
, new);
6598 status
= nfserr_jukebox
;
6605 nfs4_put_stateowner(&lo
->lo_owner
);
6613 nfsd4_lock(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6614 union nfsd4_op_u
*u
)
6616 struct nfsd4_lock
*lock
= &u
->lock
;
6617 struct nfs4_openowner
*open_sop
= NULL
;
6618 struct nfs4_lockowner
*lock_sop
= NULL
;
6619 struct nfs4_ol_stateid
*lock_stp
= NULL
;
6620 struct nfs4_ol_stateid
*open_stp
= NULL
;
6621 struct nfs4_file
*fp
;
6622 struct nfsd_file
*nf
= NULL
;
6623 struct nfsd4_blocked_lock
*nbl
= NULL
;
6624 struct file_lock
*file_lock
= NULL
;
6625 struct file_lock
*conflock
= NULL
;
6630 unsigned char fl_type
;
6631 unsigned int fl_flags
= FL_POSIX
;
6632 struct net
*net
= SVC_NET(rqstp
);
6633 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
6635 dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n",
6636 (long long) lock
->lk_offset
,
6637 (long long) lock
->lk_length
);
6639 if (check_lock_length(lock
->lk_offset
, lock
->lk_length
))
6640 return nfserr_inval
;
6642 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
,
6643 S_IFREG
, NFSD_MAY_LOCK
))) {
6644 dprintk("NFSD: nfsd4_lock: permission denied!\n");
6648 if (lock
->lk_is_new
) {
6649 if (nfsd4_has_session(cstate
))
6650 /* See rfc 5661 18.10.3: given clientid is ignored: */
6651 memcpy(&lock
->lk_new_clientid
,
6652 &cstate
->session
->se_client
->cl_clientid
,
6653 sizeof(clientid_t
));
6655 status
= nfserr_stale_clientid
;
6656 if (STALE_CLIENTID(&lock
->lk_new_clientid
, nn
))
6659 /* validate and update open stateid and open seqid */
6660 status
= nfs4_preprocess_confirmed_seqid_op(cstate
,
6661 lock
->lk_new_open_seqid
,
6662 &lock
->lk_new_open_stateid
,
6666 mutex_unlock(&open_stp
->st_mutex
);
6667 open_sop
= openowner(open_stp
->st_stateowner
);
6668 status
= nfserr_bad_stateid
;
6669 if (!same_clid(&open_sop
->oo_owner
.so_client
->cl_clientid
,
6670 &lock
->lk_new_clientid
))
6672 status
= lookup_or_create_lock_state(cstate
, open_stp
, lock
,
6675 status
= nfs4_preprocess_seqid_op(cstate
,
6676 lock
->lk_old_lock_seqid
,
6677 &lock
->lk_old_lock_stateid
,
6678 NFS4_LOCK_STID
, &lock_stp
, nn
);
6682 lock_sop
= lockowner(lock_stp
->st_stateowner
);
6684 lkflg
= setlkflg(lock
->lk_type
);
6685 status
= nfs4_check_openmode(lock_stp
, lkflg
);
6689 status
= nfserr_grace
;
6690 if (locks_in_grace(net
) && !lock
->lk_reclaim
)
6692 status
= nfserr_no_grace
;
6693 if (!locks_in_grace(net
) && lock
->lk_reclaim
)
6696 fp
= lock_stp
->st_stid
.sc_file
;
6697 switch (lock
->lk_type
) {
6699 if (nfsd4_has_session(cstate
))
6700 fl_flags
|= FL_SLEEP
;
6703 spin_lock(&fp
->fi_lock
);
6704 nf
= find_readable_file_locked(fp
);
6706 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_READ
);
6707 spin_unlock(&fp
->fi_lock
);
6710 case NFS4_WRITEW_LT
:
6711 if (nfsd4_has_session(cstate
))
6712 fl_flags
|= FL_SLEEP
;
6715 spin_lock(&fp
->fi_lock
);
6716 nf
= find_writeable_file_locked(fp
);
6718 get_lock_access(lock_stp
, NFS4_SHARE_ACCESS_WRITE
);
6719 spin_unlock(&fp
->fi_lock
);
6723 status
= nfserr_inval
;
6728 status
= nfserr_openmode
;
6732 nbl
= find_or_allocate_block(lock_sop
, &fp
->fi_fhandle
, nn
);
6734 dprintk("NFSD: %s: unable to allocate block!\n", __func__
);
6735 status
= nfserr_jukebox
;
6739 file_lock
= &nbl
->nbl_lock
;
6740 file_lock
->fl_type
= fl_type
;
6741 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(&lock_sop
->lo_owner
));
6742 file_lock
->fl_pid
= current
->tgid
;
6743 file_lock
->fl_file
= nf
->nf_file
;
6744 file_lock
->fl_flags
= fl_flags
;
6745 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
6746 file_lock
->fl_start
= lock
->lk_offset
;
6747 file_lock
->fl_end
= last_byte_offset(lock
->lk_offset
, lock
->lk_length
);
6748 nfs4_transform_lock_offset(file_lock
);
6750 conflock
= locks_alloc_lock();
6752 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6753 status
= nfserr_jukebox
;
6757 if (fl_flags
& FL_SLEEP
) {
6758 nbl
->nbl_time
= ktime_get_boottime_seconds();
6759 spin_lock(&nn
->blocked_locks_lock
);
6760 list_add_tail(&nbl
->nbl_list
, &lock_sop
->lo_blocked
);
6761 list_add_tail(&nbl
->nbl_lru
, &nn
->blocked_locks_lru
);
6762 spin_unlock(&nn
->blocked_locks_lock
);
6765 err
= vfs_lock_file(nf
->nf_file
, F_SETLK
, file_lock
, conflock
);
6767 case 0: /* success! */
6768 nfs4_inc_and_copy_stateid(&lock
->lk_resp_stateid
, &lock_stp
->st_stid
);
6770 if (lock
->lk_reclaim
)
6771 nn
->somebody_reclaimed
= true;
6773 case FILE_LOCK_DEFERRED
:
6776 case -EAGAIN
: /* conflock holds conflicting lock */
6777 status
= nfserr_denied
;
6778 dprintk("NFSD: nfsd4_lock: conflicting lock found!\n");
6779 nfs4_set_lock_denied(conflock
, &lock
->lk_denied
);
6782 status
= nfserr_deadlock
;
6785 dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err
);
6786 status
= nfserrno(err
);
6791 /* dequeue it if we queued it before */
6792 if (fl_flags
& FL_SLEEP
) {
6793 spin_lock(&nn
->blocked_locks_lock
);
6794 list_del_init(&nbl
->nbl_list
);
6795 list_del_init(&nbl
->nbl_lru
);
6796 spin_unlock(&nn
->blocked_locks_lock
);
6798 free_blocked_lock(nbl
);
6803 /* Bump seqid manually if the 4.0 replay owner is openowner */
6804 if (cstate
->replay_owner
&&
6805 cstate
->replay_owner
!= &lock_sop
->lo_owner
&&
6806 seqid_mutating_err(ntohl(status
)))
6807 lock_sop
->lo_owner
.so_seqid
++;
6810 * If this is a new, never-before-used stateid, and we are
6811 * returning an error, then just go ahead and release it.
6814 release_lock_stateid(lock_stp
);
6816 mutex_unlock(&lock_stp
->st_mutex
);
6818 nfs4_put_stid(&lock_stp
->st_stid
);
6821 nfs4_put_stid(&open_stp
->st_stid
);
6822 nfsd4_bump_seqid(cstate
, status
);
6824 locks_free_lock(conflock
);
6829 * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN,
6830 * so we do a temporary open here just to get an open file to pass to
6831 * vfs_test_lock. (Arguably perhaps test_lock should be done with an
6834 static __be32
nfsd_test_lock(struct svc_rqst
*rqstp
, struct svc_fh
*fhp
, struct file_lock
*lock
)
6836 struct nfsd_file
*nf
;
6837 __be32 err
= nfsd_file_acquire(rqstp
, fhp
, NFSD_MAY_READ
, &nf
);
6839 err
= nfserrno(vfs_test_lock(nf
->nf_file
, lock
));
6849 nfsd4_lockt(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6850 union nfsd4_op_u
*u
)
6852 struct nfsd4_lockt
*lockt
= &u
->lockt
;
6853 struct file_lock
*file_lock
= NULL
;
6854 struct nfs4_lockowner
*lo
= NULL
;
6856 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6858 if (locks_in_grace(SVC_NET(rqstp
)))
6859 return nfserr_grace
;
6861 if (check_lock_length(lockt
->lt_offset
, lockt
->lt_length
))
6862 return nfserr_inval
;
6864 if (!nfsd4_has_session(cstate
)) {
6865 status
= lookup_clientid(&lockt
->lt_clientid
, cstate
, nn
,
6871 if ((status
= fh_verify(rqstp
, &cstate
->current_fh
, S_IFREG
, 0)))
6874 file_lock
= locks_alloc_lock();
6876 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6877 status
= nfserr_jukebox
;
6881 switch (lockt
->lt_type
) {
6884 file_lock
->fl_type
= F_RDLCK
;
6887 case NFS4_WRITEW_LT
:
6888 file_lock
->fl_type
= F_WRLCK
;
6891 dprintk("NFSD: nfs4_lockt: bad lock type!\n");
6892 status
= nfserr_inval
;
6896 lo
= find_lockowner_str(cstate
->clp
, &lockt
->lt_owner
);
6898 file_lock
->fl_owner
= (fl_owner_t
)lo
;
6899 file_lock
->fl_pid
= current
->tgid
;
6900 file_lock
->fl_flags
= FL_POSIX
;
6902 file_lock
->fl_start
= lockt
->lt_offset
;
6903 file_lock
->fl_end
= last_byte_offset(lockt
->lt_offset
, lockt
->lt_length
);
6905 nfs4_transform_lock_offset(file_lock
);
6907 status
= nfsd_test_lock(rqstp
, &cstate
->current_fh
, file_lock
);
6911 if (file_lock
->fl_type
!= F_UNLCK
) {
6912 status
= nfserr_denied
;
6913 nfs4_set_lock_denied(file_lock
, &lockt
->lt_denied
);
6917 nfs4_put_stateowner(&lo
->lo_owner
);
6919 locks_free_lock(file_lock
);
6924 nfsd4_locku(struct svc_rqst
*rqstp
, struct nfsd4_compound_state
*cstate
,
6925 union nfsd4_op_u
*u
)
6927 struct nfsd4_locku
*locku
= &u
->locku
;
6928 struct nfs4_ol_stateid
*stp
;
6929 struct nfsd_file
*nf
= NULL
;
6930 struct file_lock
*file_lock
= NULL
;
6933 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
6935 dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n",
6936 (long long) locku
->lu_offset
,
6937 (long long) locku
->lu_length
);
6939 if (check_lock_length(locku
->lu_offset
, locku
->lu_length
))
6940 return nfserr_inval
;
6942 status
= nfs4_preprocess_seqid_op(cstate
, locku
->lu_seqid
,
6943 &locku
->lu_stateid
, NFS4_LOCK_STID
,
6947 nf
= find_any_file(stp
->st_stid
.sc_file
);
6949 status
= nfserr_lock_range
;
6952 file_lock
= locks_alloc_lock();
6954 dprintk("NFSD: %s: unable to allocate lock!\n", __func__
);
6955 status
= nfserr_jukebox
;
6959 file_lock
->fl_type
= F_UNLCK
;
6960 file_lock
->fl_owner
= (fl_owner_t
)lockowner(nfs4_get_stateowner(stp
->st_stateowner
));
6961 file_lock
->fl_pid
= current
->tgid
;
6962 file_lock
->fl_file
= nf
->nf_file
;
6963 file_lock
->fl_flags
= FL_POSIX
;
6964 file_lock
->fl_lmops
= &nfsd_posix_mng_ops
;
6965 file_lock
->fl_start
= locku
->lu_offset
;
6967 file_lock
->fl_end
= last_byte_offset(locku
->lu_offset
,
6969 nfs4_transform_lock_offset(file_lock
);
6971 err
= vfs_lock_file(nf
->nf_file
, F_SETLK
, file_lock
, NULL
);
6973 dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n");
6976 nfs4_inc_and_copy_stateid(&locku
->lu_stateid
, &stp
->st_stid
);
6980 mutex_unlock(&stp
->st_mutex
);
6981 nfs4_put_stid(&stp
->st_stid
);
6983 nfsd4_bump_seqid(cstate
, status
);
6985 locks_free_lock(file_lock
);
6989 status
= nfserrno(err
);
6995 * true: locks held by lockowner
6996 * false: no locks held by lockowner
6999 check_for_locks(struct nfs4_file
*fp
, struct nfs4_lockowner
*lowner
)
7001 struct file_lock
*fl
;
7003 struct nfsd_file
*nf
= find_any_file(fp
);
7004 struct inode
*inode
;
7005 struct file_lock_context
*flctx
;
7008 /* Any valid lock stateid should have some sort of access */
7013 inode
= locks_inode(nf
->nf_file
);
7014 flctx
= inode
->i_flctx
;
7016 if (flctx
&& !list_empty_careful(&flctx
->flc_posix
)) {
7017 spin_lock(&flctx
->flc_lock
);
7018 list_for_each_entry(fl
, &flctx
->flc_posix
, fl_list
) {
7019 if (fl
->fl_owner
== (fl_owner_t
)lowner
) {
7024 spin_unlock(&flctx
->flc_lock
);
7031 nfsd4_release_lockowner(struct svc_rqst
*rqstp
,
7032 struct nfsd4_compound_state
*cstate
,
7033 union nfsd4_op_u
*u
)
7035 struct nfsd4_release_lockowner
*rlockowner
= &u
->release_lockowner
;
7036 clientid_t
*clid
= &rlockowner
->rl_clientid
;
7037 struct nfs4_stateowner
*sop
;
7038 struct nfs4_lockowner
*lo
= NULL
;
7039 struct nfs4_ol_stateid
*stp
;
7040 struct xdr_netobj
*owner
= &rlockowner
->rl_owner
;
7041 unsigned int hashval
= ownerstr_hashval(owner
);
7043 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
7044 struct nfs4_client
*clp
;
7045 LIST_HEAD (reaplist
);
7047 dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n",
7048 clid
->cl_boot
, clid
->cl_id
);
7050 status
= lookup_clientid(clid
, cstate
, nn
, false);
7055 /* Find the matching lock stateowner */
7056 spin_lock(&clp
->cl_lock
);
7057 list_for_each_entry(sop
, &clp
->cl_ownerstr_hashtbl
[hashval
],
7060 if (sop
->so_is_open_owner
|| !same_owner_str(sop
, owner
))
7063 /* see if there are still any locks associated with it */
7064 lo
= lockowner(sop
);
7065 list_for_each_entry(stp
, &sop
->so_stateids
, st_perstateowner
) {
7066 if (check_for_locks(stp
->st_stid
.sc_file
, lo
)) {
7067 status
= nfserr_locks_held
;
7068 spin_unlock(&clp
->cl_lock
);
7073 nfs4_get_stateowner(sop
);
7077 spin_unlock(&clp
->cl_lock
);
7081 unhash_lockowner_locked(lo
);
7082 while (!list_empty(&lo
->lo_owner
.so_stateids
)) {
7083 stp
= list_first_entry(&lo
->lo_owner
.so_stateids
,
7084 struct nfs4_ol_stateid
,
7086 WARN_ON(!unhash_lock_stateid(stp
));
7087 put_ol_stateid_locked(stp
, &reaplist
);
7089 spin_unlock(&clp
->cl_lock
);
7090 free_ol_stateid_reaplist(&reaplist
);
7091 remove_blocked_locks(lo
);
7092 nfs4_put_stateowner(&lo
->lo_owner
);
7097 static inline struct nfs4_client_reclaim
*
7100 return kmalloc(sizeof(struct nfs4_client_reclaim
), GFP_KERNEL
);
7104 nfs4_has_reclaimed_state(struct xdr_netobj name
, struct nfsd_net
*nn
)
7106 struct nfs4_client_reclaim
*crp
;
7108 crp
= nfsd4_find_reclaim_client(name
, nn
);
7109 return (crp
&& crp
->cr_clp
);
7113 * failure => all reset bets are off, nfserr_no_grace...
7115 * The caller is responsible for freeing name.data if NULL is returned (it
7116 * will be freed in nfs4_remove_reclaim_record in the normal case).
7118 struct nfs4_client_reclaim
*
7119 nfs4_client_to_reclaim(struct xdr_netobj name
, struct xdr_netobj princhash
,
7120 struct nfsd_net
*nn
)
7122 unsigned int strhashval
;
7123 struct nfs4_client_reclaim
*crp
;
7125 trace_nfsd_clid_reclaim(nn
, name
.len
, name
.data
);
7126 crp
= alloc_reclaim();
7128 strhashval
= clientstr_hashval(name
);
7129 INIT_LIST_HEAD(&crp
->cr_strhash
);
7130 list_add(&crp
->cr_strhash
, &nn
->reclaim_str_hashtbl
[strhashval
]);
7131 crp
->cr_name
.data
= name
.data
;
7132 crp
->cr_name
.len
= name
.len
;
7133 crp
->cr_princhash
.data
= princhash
.data
;
7134 crp
->cr_princhash
.len
= princhash
.len
;
7136 nn
->reclaim_str_hashtbl_size
++;
7142 nfs4_remove_reclaim_record(struct nfs4_client_reclaim
*crp
, struct nfsd_net
*nn
)
7144 list_del(&crp
->cr_strhash
);
7145 kfree(crp
->cr_name
.data
);
7146 kfree(crp
->cr_princhash
.data
);
7148 nn
->reclaim_str_hashtbl_size
--;
7152 nfs4_release_reclaim(struct nfsd_net
*nn
)
7154 struct nfs4_client_reclaim
*crp
= NULL
;
7157 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7158 while (!list_empty(&nn
->reclaim_str_hashtbl
[i
])) {
7159 crp
= list_entry(nn
->reclaim_str_hashtbl
[i
].next
,
7160 struct nfs4_client_reclaim
, cr_strhash
);
7161 nfs4_remove_reclaim_record(crp
, nn
);
7164 WARN_ON_ONCE(nn
->reclaim_str_hashtbl_size
);
7168 * called from OPEN, CLAIM_PREVIOUS with a new clientid. */
7169 struct nfs4_client_reclaim
*
7170 nfsd4_find_reclaim_client(struct xdr_netobj name
, struct nfsd_net
*nn
)
7172 unsigned int strhashval
;
7173 struct nfs4_client_reclaim
*crp
= NULL
;
7175 trace_nfsd_clid_find(nn
, name
.len
, name
.data
);
7177 strhashval
= clientstr_hashval(name
);
7178 list_for_each_entry(crp
, &nn
->reclaim_str_hashtbl
[strhashval
], cr_strhash
) {
7179 if (compare_blob(&crp
->cr_name
, &name
) == 0) {
7187 * Called from OPEN. Look for clientid in reclaim list.
7190 nfs4_check_open_reclaim(clientid_t
*clid
,
7191 struct nfsd4_compound_state
*cstate
,
7192 struct nfsd_net
*nn
)
7196 /* find clientid in conf_id_hashtbl */
7197 status
= lookup_clientid(clid
, cstate
, nn
, false);
7199 return nfserr_reclaim_bad
;
7201 if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE
, &cstate
->clp
->cl_flags
))
7202 return nfserr_no_grace
;
7204 if (nfsd4_client_record_check(cstate
->clp
))
7205 return nfserr_reclaim_bad
;
7210 #ifdef CONFIG_NFSD_FAULT_INJECTION
7212 put_client(struct nfs4_client
*clp
)
7214 atomic_dec(&clp
->cl_rpc_users
);
7217 static struct nfs4_client
*
7218 nfsd_find_client(struct sockaddr_storage
*addr
, size_t addr_size
)
7220 struct nfs4_client
*clp
;
7221 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7224 if (!nfsd_netns_ready(nn
))
7227 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
7228 if (memcmp(&clp
->cl_addr
, addr
, addr_size
) == 0)
7235 nfsd_inject_print_clients(void)
7237 struct nfs4_client
*clp
;
7239 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7241 char buf
[INET6_ADDRSTRLEN
];
7243 if (!nfsd_netns_ready(nn
))
7246 spin_lock(&nn
->client_lock
);
7247 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
7248 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
7249 pr_info("NFS Client: %s\n", buf
);
7252 spin_unlock(&nn
->client_lock
);
7258 nfsd_inject_forget_client(struct sockaddr_storage
*addr
, size_t addr_size
)
7261 struct nfs4_client
*clp
;
7262 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7265 if (!nfsd_netns_ready(nn
))
7268 spin_lock(&nn
->client_lock
);
7269 clp
= nfsd_find_client(addr
, addr_size
);
7271 if (mark_client_expired_locked(clp
) == nfs_ok
)
7276 spin_unlock(&nn
->client_lock
);
7285 nfsd_inject_forget_clients(u64 max
)
7288 struct nfs4_client
*clp
, *next
;
7289 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7291 LIST_HEAD(reaplist
);
7293 if (!nfsd_netns_ready(nn
))
7296 spin_lock(&nn
->client_lock
);
7297 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
7298 if (mark_client_expired_locked(clp
) == nfs_ok
) {
7299 list_add(&clp
->cl_lru
, &reaplist
);
7300 if (max
!= 0 && ++count
>= max
)
7304 spin_unlock(&nn
->client_lock
);
7306 list_for_each_entry_safe(clp
, next
, &reaplist
, cl_lru
)
7312 static void nfsd_print_count(struct nfs4_client
*clp
, unsigned int count
,
7315 char buf
[INET6_ADDRSTRLEN
];
7316 rpc_ntop((struct sockaddr
*)&clp
->cl_addr
, buf
, sizeof(buf
));
7317 printk(KERN_INFO
"NFS Client: %s has %u %s\n", buf
, count
, type
);
7321 nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid
*lst
,
7322 struct list_head
*collect
)
7324 struct nfs4_client
*clp
= lst
->st_stid
.sc_client
;
7325 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7331 lockdep_assert_held(&nn
->client_lock
);
7332 atomic_inc(&clp
->cl_rpc_users
);
7333 list_add(&lst
->st_locks
, collect
);
7336 static u64
nfsd_foreach_client_lock(struct nfs4_client
*clp
, u64 max
,
7337 struct list_head
*collect
,
7338 bool (*func
)(struct nfs4_ol_stateid
*))
7340 struct nfs4_openowner
*oop
;
7341 struct nfs4_ol_stateid
*stp
, *st_next
;
7342 struct nfs4_ol_stateid
*lst
, *lst_next
;
7345 spin_lock(&clp
->cl_lock
);
7346 list_for_each_entry(oop
, &clp
->cl_openowners
, oo_perclient
) {
7347 list_for_each_entry_safe(stp
, st_next
,
7348 &oop
->oo_owner
.so_stateids
, st_perstateowner
) {
7349 list_for_each_entry_safe(lst
, lst_next
,
7350 &stp
->st_locks
, st_locks
) {
7353 nfsd_inject_add_lock_to_list(lst
,
7358 * Despite the fact that these functions deal
7359 * with 64-bit integers for "count", we must
7360 * ensure that it doesn't blow up the
7361 * clp->cl_rpc_users. Throw a warning if we
7362 * start to approach INT_MAX here.
7364 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
7371 spin_unlock(&clp
->cl_lock
);
7377 nfsd_collect_client_locks(struct nfs4_client
*clp
, struct list_head
*collect
,
7380 return nfsd_foreach_client_lock(clp
, max
, collect
, unhash_lock_stateid
);
7384 nfsd_print_client_locks(struct nfs4_client
*clp
)
7386 u64 count
= nfsd_foreach_client_lock(clp
, 0, NULL
, NULL
);
7387 nfsd_print_count(clp
, count
, "locked files");
7392 nfsd_inject_print_locks(void)
7394 struct nfs4_client
*clp
;
7396 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7399 if (!nfsd_netns_ready(nn
))
7402 spin_lock(&nn
->client_lock
);
7403 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
7404 count
+= nfsd_print_client_locks(clp
);
7405 spin_unlock(&nn
->client_lock
);
7411 nfsd_reap_locks(struct list_head
*reaplist
)
7413 struct nfs4_client
*clp
;
7414 struct nfs4_ol_stateid
*stp
, *next
;
7416 list_for_each_entry_safe(stp
, next
, reaplist
, st_locks
) {
7417 list_del_init(&stp
->st_locks
);
7418 clp
= stp
->st_stid
.sc_client
;
7419 nfs4_put_stid(&stp
->st_stid
);
7425 nfsd_inject_forget_client_locks(struct sockaddr_storage
*addr
, size_t addr_size
)
7427 unsigned int count
= 0;
7428 struct nfs4_client
*clp
;
7429 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7431 LIST_HEAD(reaplist
);
7433 if (!nfsd_netns_ready(nn
))
7436 spin_lock(&nn
->client_lock
);
7437 clp
= nfsd_find_client(addr
, addr_size
);
7439 count
= nfsd_collect_client_locks(clp
, &reaplist
, 0);
7440 spin_unlock(&nn
->client_lock
);
7441 nfsd_reap_locks(&reaplist
);
7446 nfsd_inject_forget_locks(u64 max
)
7449 struct nfs4_client
*clp
;
7450 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7452 LIST_HEAD(reaplist
);
7454 if (!nfsd_netns_ready(nn
))
7457 spin_lock(&nn
->client_lock
);
7458 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
7459 count
+= nfsd_collect_client_locks(clp
, &reaplist
, max
- count
);
7460 if (max
!= 0 && count
>= max
)
7463 spin_unlock(&nn
->client_lock
);
7464 nfsd_reap_locks(&reaplist
);
7469 nfsd_foreach_client_openowner(struct nfs4_client
*clp
, u64 max
,
7470 struct list_head
*collect
,
7471 void (*func
)(struct nfs4_openowner
*))
7473 struct nfs4_openowner
*oop
, *next
;
7474 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7478 lockdep_assert_held(&nn
->client_lock
);
7480 spin_lock(&clp
->cl_lock
);
7481 list_for_each_entry_safe(oop
, next
, &clp
->cl_openowners
, oo_perclient
) {
7485 atomic_inc(&clp
->cl_rpc_users
);
7486 list_add(&oop
->oo_perclient
, collect
);
7491 * Despite the fact that these functions deal with
7492 * 64-bit integers for "count", we must ensure that
7493 * it doesn't blow up the clp->cl_rpc_users. Throw a
7494 * warning if we start to approach INT_MAX here.
7496 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
7500 spin_unlock(&clp
->cl_lock
);
7506 nfsd_print_client_openowners(struct nfs4_client
*clp
)
7508 u64 count
= nfsd_foreach_client_openowner(clp
, 0, NULL
, NULL
);
7510 nfsd_print_count(clp
, count
, "openowners");
7515 nfsd_collect_client_openowners(struct nfs4_client
*clp
,
7516 struct list_head
*collect
, u64 max
)
7518 return nfsd_foreach_client_openowner(clp
, max
, collect
,
7519 unhash_openowner_locked
);
7523 nfsd_inject_print_openowners(void)
7525 struct nfs4_client
*clp
;
7527 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7530 if (!nfsd_netns_ready(nn
))
7533 spin_lock(&nn
->client_lock
);
7534 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
7535 count
+= nfsd_print_client_openowners(clp
);
7536 spin_unlock(&nn
->client_lock
);
7542 nfsd_reap_openowners(struct list_head
*reaplist
)
7544 struct nfs4_client
*clp
;
7545 struct nfs4_openowner
*oop
, *next
;
7547 list_for_each_entry_safe(oop
, next
, reaplist
, oo_perclient
) {
7548 list_del_init(&oop
->oo_perclient
);
7549 clp
= oop
->oo_owner
.so_client
;
7550 release_openowner(oop
);
7556 nfsd_inject_forget_client_openowners(struct sockaddr_storage
*addr
,
7559 unsigned int count
= 0;
7560 struct nfs4_client
*clp
;
7561 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7563 LIST_HEAD(reaplist
);
7565 if (!nfsd_netns_ready(nn
))
7568 spin_lock(&nn
->client_lock
);
7569 clp
= nfsd_find_client(addr
, addr_size
);
7571 count
= nfsd_collect_client_openowners(clp
, &reaplist
, 0);
7572 spin_unlock(&nn
->client_lock
);
7573 nfsd_reap_openowners(&reaplist
);
7578 nfsd_inject_forget_openowners(u64 max
)
7581 struct nfs4_client
*clp
;
7582 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7584 LIST_HEAD(reaplist
);
7586 if (!nfsd_netns_ready(nn
))
7589 spin_lock(&nn
->client_lock
);
7590 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
7591 count
+= nfsd_collect_client_openowners(clp
, &reaplist
,
7593 if (max
!= 0 && count
>= max
)
7596 spin_unlock(&nn
->client_lock
);
7597 nfsd_reap_openowners(&reaplist
);
7601 static u64
nfsd_find_all_delegations(struct nfs4_client
*clp
, u64 max
,
7602 struct list_head
*victims
)
7604 struct nfs4_delegation
*dp
, *next
;
7605 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7609 lockdep_assert_held(&nn
->client_lock
);
7611 spin_lock(&state_lock
);
7612 list_for_each_entry_safe(dp
, next
, &clp
->cl_delegations
, dl_perclnt
) {
7615 * It's not safe to mess with delegations that have a
7616 * non-zero dl_time. They might have already been broken
7617 * and could be processed by the laundromat outside of
7618 * the state_lock. Just leave them be.
7620 if (dp
->dl_time
!= 0)
7623 atomic_inc(&clp
->cl_rpc_users
);
7624 WARN_ON(!unhash_delegation_locked(dp
));
7625 list_add(&dp
->dl_recall_lru
, victims
);
7629 * Despite the fact that these functions deal with
7630 * 64-bit integers for "count", we must ensure that
7631 * it doesn't blow up the clp->cl_rpc_users. Throw a
7632 * warning if we start to approach INT_MAX here.
7634 WARN_ON_ONCE(count
== (INT_MAX
/ 2));
7638 spin_unlock(&state_lock
);
7643 nfsd_print_client_delegations(struct nfs4_client
*clp
)
7645 u64 count
= nfsd_find_all_delegations(clp
, 0, NULL
);
7647 nfsd_print_count(clp
, count
, "delegations");
7652 nfsd_inject_print_delegations(void)
7654 struct nfs4_client
*clp
;
7656 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7659 if (!nfsd_netns_ready(nn
))
7662 spin_lock(&nn
->client_lock
);
7663 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
)
7664 count
+= nfsd_print_client_delegations(clp
);
7665 spin_unlock(&nn
->client_lock
);
7671 nfsd_forget_delegations(struct list_head
*reaplist
)
7673 struct nfs4_client
*clp
;
7674 struct nfs4_delegation
*dp
, *next
;
7676 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
7677 list_del_init(&dp
->dl_recall_lru
);
7678 clp
= dp
->dl_stid
.sc_client
;
7679 revoke_delegation(dp
);
7685 nfsd_inject_forget_client_delegations(struct sockaddr_storage
*addr
,
7689 struct nfs4_client
*clp
;
7690 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7692 LIST_HEAD(reaplist
);
7694 if (!nfsd_netns_ready(nn
))
7697 spin_lock(&nn
->client_lock
);
7698 clp
= nfsd_find_client(addr
, addr_size
);
7700 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
7701 spin_unlock(&nn
->client_lock
);
7703 nfsd_forget_delegations(&reaplist
);
7708 nfsd_inject_forget_delegations(u64 max
)
7711 struct nfs4_client
*clp
;
7712 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7714 LIST_HEAD(reaplist
);
7716 if (!nfsd_netns_ready(nn
))
7719 spin_lock(&nn
->client_lock
);
7720 list_for_each_entry(clp
, &nn
->client_lru
, cl_lru
) {
7721 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
7722 if (max
!= 0 && count
>= max
)
7725 spin_unlock(&nn
->client_lock
);
7726 nfsd_forget_delegations(&reaplist
);
7731 nfsd_recall_delegations(struct list_head
*reaplist
)
7733 struct nfs4_client
*clp
;
7734 struct nfs4_delegation
*dp
, *next
;
7736 list_for_each_entry_safe(dp
, next
, reaplist
, dl_recall_lru
) {
7737 list_del_init(&dp
->dl_recall_lru
);
7738 clp
= dp
->dl_stid
.sc_client
;
7740 trace_nfsd_deleg_recall(&dp
->dl_stid
.sc_stateid
);
7743 * We skipped all entries that had a zero dl_time before,
7744 * so we can now reset the dl_time back to 0. If a delegation
7745 * break comes in now, then it won't make any difference since
7746 * we're recalling it either way.
7748 spin_lock(&state_lock
);
7750 spin_unlock(&state_lock
);
7751 nfsd_break_one_deleg(dp
);
7757 nfsd_inject_recall_client_delegations(struct sockaddr_storage
*addr
,
7761 struct nfs4_client
*clp
;
7762 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7764 LIST_HEAD(reaplist
);
7766 if (!nfsd_netns_ready(nn
))
7769 spin_lock(&nn
->client_lock
);
7770 clp
= nfsd_find_client(addr
, addr_size
);
7772 count
= nfsd_find_all_delegations(clp
, 0, &reaplist
);
7773 spin_unlock(&nn
->client_lock
);
7775 nfsd_recall_delegations(&reaplist
);
7780 nfsd_inject_recall_delegations(u64 max
)
7783 struct nfs4_client
*clp
, *next
;
7784 struct nfsd_net
*nn
= net_generic(current
->nsproxy
->net_ns
,
7786 LIST_HEAD(reaplist
);
7788 if (!nfsd_netns_ready(nn
))
7791 spin_lock(&nn
->client_lock
);
7792 list_for_each_entry_safe(clp
, next
, &nn
->client_lru
, cl_lru
) {
7793 count
+= nfsd_find_all_delegations(clp
, max
- count
, &reaplist
);
7794 if (max
!= 0 && ++count
>= max
)
7797 spin_unlock(&nn
->client_lock
);
7798 nfsd_recall_delegations(&reaplist
);
7801 #endif /* CONFIG_NFSD_FAULT_INJECTION */
7804 * Since the lifetime of a delegation isn't limited to that of an open, a
7805 * client may quite reasonably hang on to a delegation as long as it has
7806 * the inode cached. This becomes an obvious problem the first time a
7807 * client's inode cache approaches the size of the server's total memory.
7809 * For now we avoid this problem by imposing a hard limit on the number
7810 * of delegations, which varies according to the server's memory size.
7813 set_max_delegations(void)
7816 * Allow at most 4 delegations per megabyte of RAM. Quick
7817 * estimates suggest that in the worst case (where every delegation
7818 * is for a different inode), a delegation could take about 1.5K,
7819 * giving a worst case usage of about 6% of memory.
7821 max_delegations
= nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT
);
7824 static int nfs4_state_create_net(struct net
*net
)
7826 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7829 nn
->conf_id_hashtbl
= kmalloc_array(CLIENT_HASH_SIZE
,
7830 sizeof(struct list_head
),
7832 if (!nn
->conf_id_hashtbl
)
7834 nn
->unconf_id_hashtbl
= kmalloc_array(CLIENT_HASH_SIZE
,
7835 sizeof(struct list_head
),
7837 if (!nn
->unconf_id_hashtbl
)
7839 nn
->sessionid_hashtbl
= kmalloc_array(SESSION_HASH_SIZE
,
7840 sizeof(struct list_head
),
7842 if (!nn
->sessionid_hashtbl
)
7845 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7846 INIT_LIST_HEAD(&nn
->conf_id_hashtbl
[i
]);
7847 INIT_LIST_HEAD(&nn
->unconf_id_hashtbl
[i
]);
7849 for (i
= 0; i
< SESSION_HASH_SIZE
; i
++)
7850 INIT_LIST_HEAD(&nn
->sessionid_hashtbl
[i
]);
7851 nn
->conf_name_tree
= RB_ROOT
;
7852 nn
->unconf_name_tree
= RB_ROOT
;
7853 nn
->boot_time
= ktime_get_real_seconds();
7854 nn
->grace_ended
= false;
7855 nn
->nfsd4_manager
.block_opens
= true;
7856 INIT_LIST_HEAD(&nn
->nfsd4_manager
.list
);
7857 INIT_LIST_HEAD(&nn
->client_lru
);
7858 INIT_LIST_HEAD(&nn
->close_lru
);
7859 INIT_LIST_HEAD(&nn
->del_recall_lru
);
7860 spin_lock_init(&nn
->client_lock
);
7861 spin_lock_init(&nn
->s2s_cp_lock
);
7862 idr_init(&nn
->s2s_cp_stateids
);
7864 spin_lock_init(&nn
->blocked_locks_lock
);
7865 INIT_LIST_HEAD(&nn
->blocked_locks_lru
);
7867 INIT_DELAYED_WORK(&nn
->laundromat_work
, laundromat_main
);
7873 kfree(nn
->unconf_id_hashtbl
);
7875 kfree(nn
->conf_id_hashtbl
);
7881 nfs4_state_destroy_net(struct net
*net
)
7884 struct nfs4_client
*clp
= NULL
;
7885 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7887 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7888 while (!list_empty(&nn
->conf_id_hashtbl
[i
])) {
7889 clp
= list_entry(nn
->conf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
7890 destroy_client(clp
);
7894 WARN_ON(!list_empty(&nn
->blocked_locks_lru
));
7896 for (i
= 0; i
< CLIENT_HASH_SIZE
; i
++) {
7897 while (!list_empty(&nn
->unconf_id_hashtbl
[i
])) {
7898 clp
= list_entry(nn
->unconf_id_hashtbl
[i
].next
, struct nfs4_client
, cl_idhash
);
7899 destroy_client(clp
);
7903 kfree(nn
->sessionid_hashtbl
);
7904 kfree(nn
->unconf_id_hashtbl
);
7905 kfree(nn
->conf_id_hashtbl
);
7910 nfs4_state_start_net(struct net
*net
)
7912 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7915 ret
= get_nfsdfs(net
);
7918 ret
= nfs4_state_create_net(net
);
7920 mntput(nn
->nfsd_mnt
);
7923 locks_start_grace(net
, &nn
->nfsd4_manager
);
7924 nfsd4_client_tracking_init(net
);
7925 if (nn
->track_reclaim_completes
&& nn
->reclaim_str_hashtbl_size
== 0)
7927 printk(KERN_INFO
"NFSD: starting %lld-second grace period (net %x)\n",
7928 nn
->nfsd4_grace
, net
->ns
.inum
);
7929 trace_nfsd_grace_start(nn
);
7930 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_grace
* HZ
);
7934 printk(KERN_INFO
"NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n",
7936 queue_delayed_work(laundry_wq
, &nn
->laundromat_work
, nn
->nfsd4_lease
* HZ
);
7937 nfsd4_end_grace(nn
);
7941 /* initialization to perform when the nfsd service is started: */
7944 nfs4_state_start(void)
7948 laundry_wq
= alloc_workqueue("%s", WQ_UNBOUND
, 0, "nfsd4");
7949 if (laundry_wq
== NULL
) {
7953 ret
= nfsd4_create_callback_queue();
7955 goto out_free_laundry
;
7957 set_max_delegations();
7961 destroy_workqueue(laundry_wq
);
7967 nfs4_state_shutdown_net(struct net
*net
)
7969 struct nfs4_delegation
*dp
= NULL
;
7970 struct list_head
*pos
, *next
, reaplist
;
7971 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
7973 cancel_delayed_work_sync(&nn
->laundromat_work
);
7974 locks_end_grace(&nn
->nfsd4_manager
);
7976 INIT_LIST_HEAD(&reaplist
);
7977 spin_lock(&state_lock
);
7978 list_for_each_safe(pos
, next
, &nn
->del_recall_lru
) {
7979 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
7980 WARN_ON(!unhash_delegation_locked(dp
));
7981 list_add(&dp
->dl_recall_lru
, &reaplist
);
7983 spin_unlock(&state_lock
);
7984 list_for_each_safe(pos
, next
, &reaplist
) {
7985 dp
= list_entry (pos
, struct nfs4_delegation
, dl_recall_lru
);
7986 list_del_init(&dp
->dl_recall_lru
);
7987 destroy_unhashed_deleg(dp
);
7990 nfsd4_client_tracking_exit(net
);
7991 nfs4_state_destroy_net(net
);
7992 mntput(nn
->nfsd_mnt
);
7996 nfs4_state_shutdown(void)
7998 destroy_workqueue(laundry_wq
);
7999 nfsd4_destroy_callback_queue();
8003 get_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
8005 if (HAS_CSTATE_FLAG(cstate
, CURRENT_STATE_ID_FLAG
) &&
8006 CURRENT_STATEID(stateid
))
8007 memcpy(stateid
, &cstate
->current_stateid
, sizeof(stateid_t
));
8011 put_stateid(struct nfsd4_compound_state
*cstate
, stateid_t
*stateid
)
8013 if (cstate
->minorversion
) {
8014 memcpy(&cstate
->current_stateid
, stateid
, sizeof(stateid_t
));
8015 SET_CSTATE_FLAG(cstate
, CURRENT_STATE_ID_FLAG
);
8020 clear_current_stateid(struct nfsd4_compound_state
*cstate
)
8022 CLEAR_CSTATE_FLAG(cstate
, CURRENT_STATE_ID_FLAG
);
8026 * functions to set current state id
8029 nfsd4_set_opendowngradestateid(struct nfsd4_compound_state
*cstate
,
8030 union nfsd4_op_u
*u
)
8032 put_stateid(cstate
, &u
->open_downgrade
.od_stateid
);
8036 nfsd4_set_openstateid(struct nfsd4_compound_state
*cstate
,
8037 union nfsd4_op_u
*u
)
8039 put_stateid(cstate
, &u
->open
.op_stateid
);
8043 nfsd4_set_closestateid(struct nfsd4_compound_state
*cstate
,
8044 union nfsd4_op_u
*u
)
8046 put_stateid(cstate
, &u
->close
.cl_stateid
);
8050 nfsd4_set_lockstateid(struct nfsd4_compound_state
*cstate
,
8051 union nfsd4_op_u
*u
)
8053 put_stateid(cstate
, &u
->lock
.lk_resp_stateid
);
8057 * functions to consume current state id
8061 nfsd4_get_opendowngradestateid(struct nfsd4_compound_state
*cstate
,
8062 union nfsd4_op_u
*u
)
8064 get_stateid(cstate
, &u
->open_downgrade
.od_stateid
);
8068 nfsd4_get_delegreturnstateid(struct nfsd4_compound_state
*cstate
,
8069 union nfsd4_op_u
*u
)
8071 get_stateid(cstate
, &u
->delegreturn
.dr_stateid
);
8075 nfsd4_get_freestateid(struct nfsd4_compound_state
*cstate
,
8076 union nfsd4_op_u
*u
)
8078 get_stateid(cstate
, &u
->free_stateid
.fr_stateid
);
8082 nfsd4_get_setattrstateid(struct nfsd4_compound_state
*cstate
,
8083 union nfsd4_op_u
*u
)
8085 get_stateid(cstate
, &u
->setattr
.sa_stateid
);
8089 nfsd4_get_closestateid(struct nfsd4_compound_state
*cstate
,
8090 union nfsd4_op_u
*u
)
8092 get_stateid(cstate
, &u
->close
.cl_stateid
);
8096 nfsd4_get_lockustateid(struct nfsd4_compound_state
*cstate
,
8097 union nfsd4_op_u
*u
)
8099 get_stateid(cstate
, &u
->locku
.lu_stateid
);
8103 nfsd4_get_readstateid(struct nfsd4_compound_state
*cstate
,
8104 union nfsd4_op_u
*u
)
8106 get_stateid(cstate
, &u
->read
.rd_stateid
);
8110 nfsd4_get_writestateid(struct nfsd4_compound_state
*cstate
,
8111 union nfsd4_op_u
*u
)
8113 get_stateid(cstate
, &u
->write
.wr_stateid
);