4 * Client-side XDR for NFSv4.
6 * Copyright (c) 2002 The Regents of the University of Michigan.
9 * Kendrick Smith <kmsmith@umich.edu>
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 * 3. Neither the name of the University nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
31 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Implementation of the NFSv4 state model. For the time being,
37 * this is minimal, but will be made much more complex in a
41 #include <linux/kernel.h>
42 #include <linux/slab.h>
44 #include <linux/nfs_fs.h>
45 #include <linux/nfs_idmap.h>
46 #include <linux/kthread.h>
47 #include <linux/module.h>
48 #include <linux/random.h>
49 #include <linux/ratelimit.h>
50 #include <linux/workqueue.h>
51 #include <linux/bitops.h>
52 #include <linux/jiffies.h>
56 #include "delegation.h"
60 #define NFSDBG_FACILITY NFSDBG_STATE
62 #define OPENOWNER_POOL_SIZE 8
64 const nfs4_stateid zero_stateid
;
66 static LIST_HEAD(nfs4_clientid_list
);
68 int nfs4_init_clientid(struct nfs_client
*clp
, struct rpc_cred
*cred
)
70 struct nfs4_setclientid_res clid
= {
71 .clientid
= clp
->cl_clientid
,
72 .confirm
= clp
->cl_confirm
,
77 if (test_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
))
79 port
= nfs_callback_tcpport
;
80 if (clp
->cl_addr
.ss_family
== AF_INET6
)
81 port
= nfs_callback_tcpport6
;
83 status
= nfs4_proc_setclientid(clp
, NFS4_CALLBACK
, port
, cred
, &clid
);
86 clp
->cl_clientid
= clid
.clientid
;
87 clp
->cl_confirm
= clid
.confirm
;
88 set_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
90 status
= nfs4_proc_setclientid_confirm(clp
, &clid
, cred
);
93 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
94 nfs4_schedule_state_renewal(clp
);
99 struct rpc_cred
*nfs4_get_machine_cred_locked(struct nfs_client
*clp
)
101 struct rpc_cred
*cred
= NULL
;
103 if (clp
->cl_machine_cred
!= NULL
)
104 cred
= get_rpccred(clp
->cl_machine_cred
);
108 static void nfs4_clear_machine_cred(struct nfs_client
*clp
)
110 struct rpc_cred
*cred
;
112 spin_lock(&clp
->cl_lock
);
113 cred
= clp
->cl_machine_cred
;
114 clp
->cl_machine_cred
= NULL
;
115 spin_unlock(&clp
->cl_lock
);
120 static struct rpc_cred
*
121 nfs4_get_renew_cred_server_locked(struct nfs_server
*server
)
123 struct rpc_cred
*cred
= NULL
;
124 struct nfs4_state_owner
*sp
;
127 for (pos
= rb_first(&server
->state_owners
);
129 pos
= rb_next(pos
)) {
130 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
131 if (list_empty(&sp
->so_states
))
133 cred
= get_rpccred(sp
->so_cred
);
140 * nfs4_get_renew_cred_locked - Acquire credential for a renew operation
141 * @clp: client state handle
143 * Returns an rpc_cred with reference count bumped, or NULL.
144 * Caller must hold clp->cl_lock.
146 struct rpc_cred
*nfs4_get_renew_cred_locked(struct nfs_client
*clp
)
148 struct rpc_cred
*cred
= NULL
;
149 struct nfs_server
*server
;
151 /* Use machine credentials if available */
152 cred
= nfs4_get_machine_cred_locked(clp
);
157 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
158 cred
= nfs4_get_renew_cred_server_locked(server
);
168 #if defined(CONFIG_NFS_V4_1)
170 static int nfs41_setup_state_renewal(struct nfs_client
*clp
)
173 struct nfs_fsinfo fsinfo
;
175 if (!test_bit(NFS_CS_CHECK_LEASE_TIME
, &clp
->cl_res_state
)) {
176 nfs4_schedule_state_renewal(clp
);
180 status
= nfs4_proc_get_lease_time(clp
, &fsinfo
);
182 /* Update lease time and schedule renewal */
183 spin_lock(&clp
->cl_lock
);
184 clp
->cl_lease_time
= fsinfo
.lease_time
* HZ
;
185 clp
->cl_last_renewal
= jiffies
;
186 spin_unlock(&clp
->cl_lock
);
188 nfs4_schedule_state_renewal(clp
);
195 * Back channel returns NFS4ERR_DELAY for new requests when
196 * NFS4_SESSION_DRAINING is set so there is no work to be done when draining
199 static void nfs4_end_drain_session(struct nfs_client
*clp
)
201 struct nfs4_session
*ses
= clp
->cl_session
;
202 struct nfs4_slot_table
*tbl
;
207 tbl
= &ses
->fc_slot_table
;
208 if (test_and_clear_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
)) {
209 spin_lock(&tbl
->slot_tbl_lock
);
210 max_slots
= tbl
->max_slots
;
211 while (max_slots
--) {
212 if (rpc_wake_up_first(&tbl
->slot_tbl_waitq
,
213 nfs4_set_task_privileged
,
217 spin_unlock(&tbl
->slot_tbl_lock
);
221 static int nfs4_wait_on_slot_tbl(struct nfs4_slot_table
*tbl
)
223 spin_lock(&tbl
->slot_tbl_lock
);
224 if (tbl
->highest_used_slotid
!= NFS4_NO_SLOT
) {
225 INIT_COMPLETION(tbl
->complete
);
226 spin_unlock(&tbl
->slot_tbl_lock
);
227 return wait_for_completion_interruptible(&tbl
->complete
);
229 spin_unlock(&tbl
->slot_tbl_lock
);
233 static int nfs4_begin_drain_session(struct nfs_client
*clp
)
235 struct nfs4_session
*ses
= clp
->cl_session
;
238 set_bit(NFS4_SESSION_DRAINING
, &ses
->session_state
);
240 ret
= nfs4_wait_on_slot_tbl(&ses
->bc_slot_table
);
244 return nfs4_wait_on_slot_tbl(&ses
->fc_slot_table
);
247 int nfs41_init_clientid(struct nfs_client
*clp
, struct rpc_cred
*cred
)
251 if (test_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
))
253 nfs4_begin_drain_session(clp
);
254 status
= nfs4_proc_exchange_id(clp
, cred
);
257 set_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
259 status
= nfs4_proc_create_session(clp
, cred
);
262 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
263 nfs41_setup_state_renewal(clp
);
264 nfs_mark_client_ready(clp
, NFS_CS_READY
);
269 struct rpc_cred
*nfs4_get_exchange_id_cred(struct nfs_client
*clp
)
271 struct rpc_cred
*cred
;
273 spin_lock(&clp
->cl_lock
);
274 cred
= nfs4_get_machine_cred_locked(clp
);
275 spin_unlock(&clp
->cl_lock
);
279 #endif /* CONFIG_NFS_V4_1 */
281 static struct rpc_cred
*
282 nfs4_get_setclientid_cred_server(struct nfs_server
*server
)
284 struct nfs_client
*clp
= server
->nfs_client
;
285 struct rpc_cred
*cred
= NULL
;
286 struct nfs4_state_owner
*sp
;
289 spin_lock(&clp
->cl_lock
);
290 pos
= rb_first(&server
->state_owners
);
292 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
293 cred
= get_rpccred(sp
->so_cred
);
295 spin_unlock(&clp
->cl_lock
);
300 * nfs4_get_setclientid_cred - Acquire credential for a setclientid operation
301 * @clp: client state handle
303 * Returns an rpc_cred with reference count bumped, or NULL.
305 struct rpc_cred
*nfs4_get_setclientid_cred(struct nfs_client
*clp
)
307 struct nfs_server
*server
;
308 struct rpc_cred
*cred
;
310 spin_lock(&clp
->cl_lock
);
311 cred
= nfs4_get_machine_cred_locked(clp
);
312 spin_unlock(&clp
->cl_lock
);
317 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
318 cred
= nfs4_get_setclientid_cred_server(server
);
328 static struct nfs4_state_owner
*
329 nfs4_find_state_owner_locked(struct nfs_server
*server
, struct rpc_cred
*cred
)
331 struct rb_node
**p
= &server
->state_owners
.rb_node
,
333 struct nfs4_state_owner
*sp
;
337 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
339 if (cred
< sp
->so_cred
)
340 p
= &parent
->rb_left
;
341 else if (cred
> sp
->so_cred
)
342 p
= &parent
->rb_right
;
344 if (!list_empty(&sp
->so_lru
))
345 list_del_init(&sp
->so_lru
);
346 atomic_inc(&sp
->so_count
);
353 static struct nfs4_state_owner
*
354 nfs4_insert_state_owner_locked(struct nfs4_state_owner
*new)
356 struct nfs_server
*server
= new->so_server
;
357 struct rb_node
**p
= &server
->state_owners
.rb_node
,
359 struct nfs4_state_owner
*sp
;
364 sp
= rb_entry(parent
, struct nfs4_state_owner
, so_server_node
);
366 if (new->so_cred
< sp
->so_cred
)
367 p
= &parent
->rb_left
;
368 else if (new->so_cred
> sp
->so_cred
)
369 p
= &parent
->rb_right
;
371 if (!list_empty(&sp
->so_lru
))
372 list_del_init(&sp
->so_lru
);
373 atomic_inc(&sp
->so_count
);
377 err
= ida_get_new(&server
->openowner_id
, &new->so_seqid
.owner_id
);
380 rb_link_node(&new->so_server_node
, parent
, p
);
381 rb_insert_color(&new->so_server_node
, &server
->state_owners
);
386 nfs4_remove_state_owner_locked(struct nfs4_state_owner
*sp
)
388 struct nfs_server
*server
= sp
->so_server
;
390 if (!RB_EMPTY_NODE(&sp
->so_server_node
))
391 rb_erase(&sp
->so_server_node
, &server
->state_owners
);
392 ida_remove(&server
->openowner_id
, sp
->so_seqid
.owner_id
);
396 nfs4_init_seqid_counter(struct nfs_seqid_counter
*sc
)
398 sc
->create_time
= ktime_get();
401 spin_lock_init(&sc
->lock
);
402 INIT_LIST_HEAD(&sc
->list
);
403 rpc_init_wait_queue(&sc
->wait
, "Seqid_waitqueue");
407 nfs4_destroy_seqid_counter(struct nfs_seqid_counter
*sc
)
409 rpc_destroy_wait_queue(&sc
->wait
);
413 * nfs4_alloc_state_owner(): this is called on the OPEN or CREATE path to
414 * create a new state_owner.
417 static struct nfs4_state_owner
*
418 nfs4_alloc_state_owner(struct nfs_server
*server
,
419 struct rpc_cred
*cred
,
422 struct nfs4_state_owner
*sp
;
424 sp
= kzalloc(sizeof(*sp
), gfp_flags
);
427 sp
->so_server
= server
;
428 sp
->so_cred
= get_rpccred(cred
);
429 spin_lock_init(&sp
->so_lock
);
430 INIT_LIST_HEAD(&sp
->so_states
);
431 nfs4_init_seqid_counter(&sp
->so_seqid
);
432 atomic_set(&sp
->so_count
, 1);
433 INIT_LIST_HEAD(&sp
->so_lru
);
438 nfs4_drop_state_owner(struct nfs4_state_owner
*sp
)
440 struct rb_node
*rb_node
= &sp
->so_server_node
;
442 if (!RB_EMPTY_NODE(rb_node
)) {
443 struct nfs_server
*server
= sp
->so_server
;
444 struct nfs_client
*clp
= server
->nfs_client
;
446 spin_lock(&clp
->cl_lock
);
447 if (!RB_EMPTY_NODE(rb_node
)) {
448 rb_erase(rb_node
, &server
->state_owners
);
449 RB_CLEAR_NODE(rb_node
);
451 spin_unlock(&clp
->cl_lock
);
455 static void nfs4_free_state_owner(struct nfs4_state_owner
*sp
)
457 nfs4_destroy_seqid_counter(&sp
->so_seqid
);
458 put_rpccred(sp
->so_cred
);
462 static void nfs4_gc_state_owners(struct nfs_server
*server
)
464 struct nfs_client
*clp
= server
->nfs_client
;
465 struct nfs4_state_owner
*sp
, *tmp
;
466 unsigned long time_min
, time_max
;
469 spin_lock(&clp
->cl_lock
);
471 time_min
= (long)time_max
- (long)clp
->cl_lease_time
;
472 list_for_each_entry_safe(sp
, tmp
, &server
->state_owners_lru
, so_lru
) {
473 /* NB: LRU is sorted so that oldest is at the head */
474 if (time_in_range(sp
->so_expires
, time_min
, time_max
))
476 list_move(&sp
->so_lru
, &doomed
);
477 nfs4_remove_state_owner_locked(sp
);
479 spin_unlock(&clp
->cl_lock
);
481 list_for_each_entry_safe(sp
, tmp
, &doomed
, so_lru
) {
482 list_del(&sp
->so_lru
);
483 nfs4_free_state_owner(sp
);
488 * nfs4_get_state_owner - Look up a state owner given a credential
489 * @server: nfs_server to search
490 * @cred: RPC credential to match
492 * Returns a pointer to an instantiated nfs4_state_owner struct, or NULL.
494 struct nfs4_state_owner
*nfs4_get_state_owner(struct nfs_server
*server
,
495 struct rpc_cred
*cred
,
498 struct nfs_client
*clp
= server
->nfs_client
;
499 struct nfs4_state_owner
*sp
, *new;
501 spin_lock(&clp
->cl_lock
);
502 sp
= nfs4_find_state_owner_locked(server
, cred
);
503 spin_unlock(&clp
->cl_lock
);
506 new = nfs4_alloc_state_owner(server
, cred
, gfp_flags
);
510 if (ida_pre_get(&server
->openowner_id
, gfp_flags
) == 0)
512 spin_lock(&clp
->cl_lock
);
513 sp
= nfs4_insert_state_owner_locked(new);
514 spin_unlock(&clp
->cl_lock
);
515 } while (sp
== ERR_PTR(-EAGAIN
));
517 nfs4_free_state_owner(new);
519 nfs4_gc_state_owners(server
);
524 * nfs4_put_state_owner - Release a nfs4_state_owner
525 * @sp: state owner data to release
527 * Note that we keep released state owners on an LRU
529 * This caches valid state owners so that they can be
530 * reused, to avoid the OPEN_CONFIRM on minor version 0.
531 * It also pins the uniquifier of dropped state owners for
532 * a while, to ensure that those state owner names are
535 void nfs4_put_state_owner(struct nfs4_state_owner
*sp
)
537 struct nfs_server
*server
= sp
->so_server
;
538 struct nfs_client
*clp
= server
->nfs_client
;
540 if (!atomic_dec_and_lock(&sp
->so_count
, &clp
->cl_lock
))
543 sp
->so_expires
= jiffies
;
544 list_add_tail(&sp
->so_lru
, &server
->state_owners_lru
);
545 spin_unlock(&clp
->cl_lock
);
549 * nfs4_purge_state_owners - Release all cached state owners
550 * @server: nfs_server with cached state owners to release
552 * Called at umount time. Remaining state owners will be on
553 * the LRU with ref count of zero.
555 void nfs4_purge_state_owners(struct nfs_server
*server
)
557 struct nfs_client
*clp
= server
->nfs_client
;
558 struct nfs4_state_owner
*sp
, *tmp
;
561 spin_lock(&clp
->cl_lock
);
562 list_for_each_entry_safe(sp
, tmp
, &server
->state_owners_lru
, so_lru
) {
563 list_move(&sp
->so_lru
, &doomed
);
564 nfs4_remove_state_owner_locked(sp
);
566 spin_unlock(&clp
->cl_lock
);
568 list_for_each_entry_safe(sp
, tmp
, &doomed
, so_lru
) {
569 list_del(&sp
->so_lru
);
570 nfs4_free_state_owner(sp
);
574 static struct nfs4_state
*
575 nfs4_alloc_open_state(void)
577 struct nfs4_state
*state
;
579 state
= kzalloc(sizeof(*state
), GFP_NOFS
);
582 atomic_set(&state
->count
, 1);
583 INIT_LIST_HEAD(&state
->lock_states
);
584 spin_lock_init(&state
->state_lock
);
585 seqlock_init(&state
->seqlock
);
590 nfs4_state_set_mode_locked(struct nfs4_state
*state
, fmode_t fmode
)
592 if (state
->state
== fmode
)
594 /* NB! List reordering - see the reclaim code for why. */
595 if ((fmode
& FMODE_WRITE
) != (state
->state
& FMODE_WRITE
)) {
596 if (fmode
& FMODE_WRITE
)
597 list_move(&state
->open_states
, &state
->owner
->so_states
);
599 list_move_tail(&state
->open_states
, &state
->owner
->so_states
);
601 state
->state
= fmode
;
604 static struct nfs4_state
*
605 __nfs4_find_state_byowner(struct inode
*inode
, struct nfs4_state_owner
*owner
)
607 struct nfs_inode
*nfsi
= NFS_I(inode
);
608 struct nfs4_state
*state
;
610 list_for_each_entry(state
, &nfsi
->open_states
, inode_states
) {
611 if (state
->owner
!= owner
)
613 if (atomic_inc_not_zero(&state
->count
))
620 nfs4_free_open_state(struct nfs4_state
*state
)
626 nfs4_get_open_state(struct inode
*inode
, struct nfs4_state_owner
*owner
)
628 struct nfs4_state
*state
, *new;
629 struct nfs_inode
*nfsi
= NFS_I(inode
);
631 spin_lock(&inode
->i_lock
);
632 state
= __nfs4_find_state_byowner(inode
, owner
);
633 spin_unlock(&inode
->i_lock
);
636 new = nfs4_alloc_open_state();
637 spin_lock(&owner
->so_lock
);
638 spin_lock(&inode
->i_lock
);
639 state
= __nfs4_find_state_byowner(inode
, owner
);
640 if (state
== NULL
&& new != NULL
) {
642 state
->owner
= owner
;
643 atomic_inc(&owner
->so_count
);
644 list_add(&state
->inode_states
, &nfsi
->open_states
);
646 state
->inode
= inode
;
647 spin_unlock(&inode
->i_lock
);
648 /* Note: The reclaim code dictates that we add stateless
649 * and read-only stateids to the end of the list */
650 list_add_tail(&state
->open_states
, &owner
->so_states
);
651 spin_unlock(&owner
->so_lock
);
653 spin_unlock(&inode
->i_lock
);
654 spin_unlock(&owner
->so_lock
);
656 nfs4_free_open_state(new);
662 void nfs4_put_open_state(struct nfs4_state
*state
)
664 struct inode
*inode
= state
->inode
;
665 struct nfs4_state_owner
*owner
= state
->owner
;
667 if (!atomic_dec_and_lock(&state
->count
, &owner
->so_lock
))
669 spin_lock(&inode
->i_lock
);
670 list_del(&state
->inode_states
);
671 list_del(&state
->open_states
);
672 spin_unlock(&inode
->i_lock
);
673 spin_unlock(&owner
->so_lock
);
675 nfs4_free_open_state(state
);
676 nfs4_put_state_owner(owner
);
680 * Close the current file.
682 static void __nfs4_close(struct nfs4_state
*state
,
683 fmode_t fmode
, gfp_t gfp_mask
, int wait
)
685 struct nfs4_state_owner
*owner
= state
->owner
;
689 atomic_inc(&owner
->so_count
);
690 /* Protect against nfs4_find_state() */
691 spin_lock(&owner
->so_lock
);
692 switch (fmode
& (FMODE_READ
| FMODE_WRITE
)) {
699 case FMODE_READ
|FMODE_WRITE
:
702 newstate
= FMODE_READ
|FMODE_WRITE
;
703 if (state
->n_rdwr
== 0) {
704 if (state
->n_rdonly
== 0) {
705 newstate
&= ~FMODE_READ
;
706 call_close
|= test_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
707 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
709 if (state
->n_wronly
== 0) {
710 newstate
&= ~FMODE_WRITE
;
711 call_close
|= test_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
712 call_close
|= test_bit(NFS_O_RDWR_STATE
, &state
->flags
);
715 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
717 nfs4_state_set_mode_locked(state
, newstate
);
718 spin_unlock(&owner
->so_lock
);
721 nfs4_put_open_state(state
);
722 nfs4_put_state_owner(owner
);
724 bool roc
= pnfs_roc(state
->inode
);
726 nfs4_do_close(state
, gfp_mask
, wait
, roc
);
730 void nfs4_close_state(struct nfs4_state
*state
, fmode_t fmode
)
732 __nfs4_close(state
, fmode
, GFP_NOFS
, 0);
735 void nfs4_close_sync(struct nfs4_state
*state
, fmode_t fmode
)
737 __nfs4_close(state
, fmode
, GFP_KERNEL
, 1);
741 * Search the state->lock_states for an existing lock_owner
742 * that is compatible with current->files
744 static struct nfs4_lock_state
*
745 __nfs4_find_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
, pid_t fl_pid
, unsigned int type
)
747 struct nfs4_lock_state
*pos
;
748 list_for_each_entry(pos
, &state
->lock_states
, ls_locks
) {
749 if (type
!= NFS4_ANY_LOCK_TYPE
&& pos
->ls_owner
.lo_type
!= type
)
751 switch (pos
->ls_owner
.lo_type
) {
752 case NFS4_POSIX_LOCK_TYPE
:
753 if (pos
->ls_owner
.lo_u
.posix_owner
!= fl_owner
)
756 case NFS4_FLOCK_LOCK_TYPE
:
757 if (pos
->ls_owner
.lo_u
.flock_owner
!= fl_pid
)
760 atomic_inc(&pos
->ls_count
);
767 * Return a compatible lock_state. If no initialized lock_state structure
768 * exists, return an uninitialized one.
771 static struct nfs4_lock_state
*nfs4_alloc_lock_state(struct nfs4_state
*state
, fl_owner_t fl_owner
, pid_t fl_pid
, unsigned int type
)
773 struct nfs4_lock_state
*lsp
;
774 struct nfs_server
*server
= state
->owner
->so_server
;
776 lsp
= kzalloc(sizeof(*lsp
), GFP_NOFS
);
779 nfs4_init_seqid_counter(&lsp
->ls_seqid
);
780 atomic_set(&lsp
->ls_count
, 1);
781 lsp
->ls_state
= state
;
782 lsp
->ls_owner
.lo_type
= type
;
783 switch (lsp
->ls_owner
.lo_type
) {
784 case NFS4_FLOCK_LOCK_TYPE
:
785 lsp
->ls_owner
.lo_u
.flock_owner
= fl_pid
;
787 case NFS4_POSIX_LOCK_TYPE
:
788 lsp
->ls_owner
.lo_u
.posix_owner
= fl_owner
;
793 lsp
->ls_seqid
.owner_id
= ida_simple_get(&server
->lockowner_id
, 0, 0, GFP_NOFS
);
794 if (lsp
->ls_seqid
.owner_id
< 0)
796 INIT_LIST_HEAD(&lsp
->ls_locks
);
803 void nfs4_free_lock_state(struct nfs_server
*server
, struct nfs4_lock_state
*lsp
)
805 ida_simple_remove(&server
->lockowner_id
, lsp
->ls_seqid
.owner_id
);
806 nfs4_destroy_seqid_counter(&lsp
->ls_seqid
);
811 * Return a compatible lock_state. If no initialized lock_state structure
812 * exists, return an uninitialized one.
815 static struct nfs4_lock_state
*nfs4_get_lock_state(struct nfs4_state
*state
, fl_owner_t owner
, pid_t pid
, unsigned int type
)
817 struct nfs4_lock_state
*lsp
, *new = NULL
;
820 spin_lock(&state
->state_lock
);
821 lsp
= __nfs4_find_lock_state(state
, owner
, pid
, type
);
825 list_add(&new->ls_locks
, &state
->lock_states
);
826 set_bit(LK_STATE_IN_USE
, &state
->flags
);
831 spin_unlock(&state
->state_lock
);
832 new = nfs4_alloc_lock_state(state
, owner
, pid
, type
);
836 spin_unlock(&state
->state_lock
);
838 nfs4_free_lock_state(state
->owner
->so_server
, new);
843 * Release reference to lock_state, and free it if we see that
844 * it is no longer in use
846 void nfs4_put_lock_state(struct nfs4_lock_state
*lsp
)
848 struct nfs4_state
*state
;
852 state
= lsp
->ls_state
;
853 if (!atomic_dec_and_lock(&lsp
->ls_count
, &state
->state_lock
))
855 list_del(&lsp
->ls_locks
);
856 if (list_empty(&state
->lock_states
))
857 clear_bit(LK_STATE_IN_USE
, &state
->flags
);
858 spin_unlock(&state
->state_lock
);
859 if (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) {
860 if (nfs4_release_lockowner(lsp
) == 0)
863 nfs4_free_lock_state(lsp
->ls_state
->owner
->so_server
, lsp
);
866 static void nfs4_fl_copy_lock(struct file_lock
*dst
, struct file_lock
*src
)
868 struct nfs4_lock_state
*lsp
= src
->fl_u
.nfs4_fl
.owner
;
870 dst
->fl_u
.nfs4_fl
.owner
= lsp
;
871 atomic_inc(&lsp
->ls_count
);
874 static void nfs4_fl_release_lock(struct file_lock
*fl
)
876 nfs4_put_lock_state(fl
->fl_u
.nfs4_fl
.owner
);
879 static const struct file_lock_operations nfs4_fl_lock_ops
= {
880 .fl_copy_lock
= nfs4_fl_copy_lock
,
881 .fl_release_private
= nfs4_fl_release_lock
,
884 int nfs4_set_lock_state(struct nfs4_state
*state
, struct file_lock
*fl
)
886 struct nfs4_lock_state
*lsp
;
888 if (fl
->fl_ops
!= NULL
)
890 if (fl
->fl_flags
& FL_POSIX
)
891 lsp
= nfs4_get_lock_state(state
, fl
->fl_owner
, 0, NFS4_POSIX_LOCK_TYPE
);
892 else if (fl
->fl_flags
& FL_FLOCK
)
893 lsp
= nfs4_get_lock_state(state
, NULL
, fl
->fl_pid
,
894 NFS4_FLOCK_LOCK_TYPE
);
899 fl
->fl_u
.nfs4_fl
.owner
= lsp
;
900 fl
->fl_ops
= &nfs4_fl_lock_ops
;
904 static bool nfs4_copy_lock_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
,
905 fl_owner_t fl_owner
, pid_t fl_pid
)
907 struct nfs4_lock_state
*lsp
;
910 if (test_bit(LK_STATE_IN_USE
, &state
->flags
) == 0)
913 spin_lock(&state
->state_lock
);
914 lsp
= __nfs4_find_lock_state(state
, fl_owner
, fl_pid
, NFS4_ANY_LOCK_TYPE
);
915 if (lsp
!= NULL
&& (lsp
->ls_flags
& NFS_LOCK_INITIALIZED
) != 0) {
916 nfs4_stateid_copy(dst
, &lsp
->ls_stateid
);
919 spin_unlock(&state
->state_lock
);
920 nfs4_put_lock_state(lsp
);
925 static void nfs4_copy_open_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
)
930 seq
= read_seqbegin(&state
->seqlock
);
931 nfs4_stateid_copy(dst
, &state
->stateid
);
932 } while (read_seqretry(&state
->seqlock
, seq
));
936 * Byte-range lock aware utility to initialize the stateid of read/write
939 void nfs4_select_rw_stateid(nfs4_stateid
*dst
, struct nfs4_state
*state
,
940 fmode_t fmode
, fl_owner_t fl_owner
, pid_t fl_pid
)
942 if (nfs4_copy_delegation_stateid(dst
, state
->inode
, fmode
))
944 if (nfs4_copy_lock_stateid(dst
, state
, fl_owner
, fl_pid
))
946 nfs4_copy_open_stateid(dst
, state
);
949 struct nfs_seqid
*nfs_alloc_seqid(struct nfs_seqid_counter
*counter
, gfp_t gfp_mask
)
951 struct nfs_seqid
*new;
953 new = kmalloc(sizeof(*new), gfp_mask
);
955 new->sequence
= counter
;
956 INIT_LIST_HEAD(&new->list
);
962 void nfs_release_seqid(struct nfs_seqid
*seqid
)
964 struct nfs_seqid_counter
*sequence
;
966 if (list_empty(&seqid
->list
))
968 sequence
= seqid
->sequence
;
969 spin_lock(&sequence
->lock
);
970 list_del_init(&seqid
->list
);
971 if (!list_empty(&sequence
->list
)) {
972 struct nfs_seqid
*next
;
974 next
= list_first_entry(&sequence
->list
,
975 struct nfs_seqid
, list
);
976 rpc_wake_up_queued_task(&sequence
->wait
, next
->task
);
978 spin_unlock(&sequence
->lock
);
981 void nfs_free_seqid(struct nfs_seqid
*seqid
)
983 nfs_release_seqid(seqid
);
988 * Increment the seqid if the OPEN/OPEN_DOWNGRADE/CLOSE succeeded, or
989 * failed with a seqid incrementing error -
990 * see comments nfs_fs.h:seqid_mutating_error()
992 static void nfs_increment_seqid(int status
, struct nfs_seqid
*seqid
)
994 BUG_ON(list_first_entry(&seqid
->sequence
->list
, struct nfs_seqid
, list
) != seqid
);
998 case -NFS4ERR_BAD_SEQID
:
999 if (seqid
->sequence
->flags
& NFS_SEQID_CONFIRMED
)
1001 pr_warn_ratelimited("NFS: v4 server returned a bad"
1002 " sequence-id error on an"
1003 " unconfirmed sequence %p!\n",
1005 case -NFS4ERR_STALE_CLIENTID
:
1006 case -NFS4ERR_STALE_STATEID
:
1007 case -NFS4ERR_BAD_STATEID
:
1008 case -NFS4ERR_BADXDR
:
1009 case -NFS4ERR_RESOURCE
:
1010 case -NFS4ERR_NOFILEHANDLE
:
1011 /* Non-seqid mutating errors */
1015 * Note: no locking needed as we are guaranteed to be first
1016 * on the sequence list
1018 seqid
->sequence
->counter
++;
1021 void nfs_increment_open_seqid(int status
, struct nfs_seqid
*seqid
)
1023 struct nfs4_state_owner
*sp
= container_of(seqid
->sequence
,
1024 struct nfs4_state_owner
, so_seqid
);
1025 struct nfs_server
*server
= sp
->so_server
;
1027 if (status
== -NFS4ERR_BAD_SEQID
)
1028 nfs4_drop_state_owner(sp
);
1029 if (!nfs4_has_session(server
->nfs_client
))
1030 nfs_increment_seqid(status
, seqid
);
1034 * Increment the seqid if the LOCK/LOCKU succeeded, or
1035 * failed with a seqid incrementing error -
1036 * see comments nfs_fs.h:seqid_mutating_error()
1038 void nfs_increment_lock_seqid(int status
, struct nfs_seqid
*seqid
)
1040 nfs_increment_seqid(status
, seqid
);
1043 int nfs_wait_on_sequence(struct nfs_seqid
*seqid
, struct rpc_task
*task
)
1045 struct nfs_seqid_counter
*sequence
= seqid
->sequence
;
1048 spin_lock(&sequence
->lock
);
1050 if (list_empty(&seqid
->list
))
1051 list_add_tail(&seqid
->list
, &sequence
->list
);
1052 if (list_first_entry(&sequence
->list
, struct nfs_seqid
, list
) == seqid
)
1054 rpc_sleep_on(&sequence
->wait
, task
, NULL
);
1057 spin_unlock(&sequence
->lock
);
1061 static int nfs4_run_state_manager(void *);
1063 static void nfs4_clear_state_manager_bit(struct nfs_client
*clp
)
1065 smp_mb__before_clear_bit();
1066 clear_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
);
1067 smp_mb__after_clear_bit();
1068 wake_up_bit(&clp
->cl_state
, NFS4CLNT_MANAGER_RUNNING
);
1069 rpc_wake_up(&clp
->cl_rpcwaitq
);
1073 * Schedule the nfs_client asynchronous state management routine
1075 void nfs4_schedule_state_manager(struct nfs_client
*clp
)
1077 struct task_struct
*task
;
1078 char buf
[INET6_ADDRSTRLEN
+ sizeof("-manager") + 1];
1080 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
1082 __module_get(THIS_MODULE
);
1083 atomic_inc(&clp
->cl_count
);
1085 /* The rcu_read_lock() is not strictly necessary, as the state
1086 * manager is the only thread that ever changes the rpc_xprt
1087 * after it's initialized. At this point, we're single threaded. */
1089 snprintf(buf
, sizeof(buf
), "%s-manager",
1090 rpc_peeraddr2str(clp
->cl_rpcclient
, RPC_DISPLAY_ADDR
));
1092 task
= kthread_run(nfs4_run_state_manager
, clp
, buf
);
1094 printk(KERN_ERR
"%s: kthread_run: %ld\n",
1095 __func__
, PTR_ERR(task
));
1096 nfs4_clear_state_manager_bit(clp
);
1097 nfs_put_client(clp
);
1098 module_put(THIS_MODULE
);
1103 * Schedule a lease recovery attempt
1105 void nfs4_schedule_lease_recovery(struct nfs_client
*clp
)
1109 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1110 set_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1111 dprintk("%s: scheduling lease recovery for server %s\n", __func__
,
1113 nfs4_schedule_state_manager(clp
);
1115 EXPORT_SYMBOL_GPL(nfs4_schedule_lease_recovery
);
1118 * nfs40_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
1119 * @clp: client to process
1121 * Set the NFS4CLNT_LEASE_EXPIRED state in order to force a
1122 * resend of the SETCLIENTID and hence re-establish the
1123 * callback channel. Then return all existing delegations.
1125 static void nfs40_handle_cb_pathdown(struct nfs_client
*clp
)
1127 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1128 nfs_expire_all_delegations(clp
);
1129 dprintk("%s: handling CB_PATHDOWN recovery for server %s\n", __func__
,
1133 void nfs4_schedule_path_down_recovery(struct nfs_client
*clp
)
1135 nfs40_handle_cb_pathdown(clp
);
1136 nfs4_schedule_state_manager(clp
);
1139 static int nfs4_state_mark_reclaim_reboot(struct nfs_client
*clp
, struct nfs4_state
*state
)
1142 set_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1143 /* Don't recover state that expired before the reboot */
1144 if (test_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
)) {
1145 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1148 set_bit(NFS_OWNER_RECLAIM_REBOOT
, &state
->owner
->so_flags
);
1149 set_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
);
1153 static int nfs4_state_mark_reclaim_nograce(struct nfs_client
*clp
, struct nfs4_state
*state
)
1155 set_bit(NFS_STATE_RECLAIM_NOGRACE
, &state
->flags
);
1156 clear_bit(NFS_STATE_RECLAIM_REBOOT
, &state
->flags
);
1157 set_bit(NFS_OWNER_RECLAIM_NOGRACE
, &state
->owner
->so_flags
);
1158 set_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
);
1162 void nfs4_schedule_stateid_recovery(const struct nfs_server
*server
, struct nfs4_state
*state
)
1164 struct nfs_client
*clp
= server
->nfs_client
;
1166 nfs4_state_mark_reclaim_nograce(clp
, state
);
1167 dprintk("%s: scheduling stateid recovery for server %s\n", __func__
,
1169 nfs4_schedule_state_manager(clp
);
1171 EXPORT_SYMBOL_GPL(nfs4_schedule_stateid_recovery
);
1173 void nfs_inode_find_state_and_recover(struct inode
*inode
,
1174 const nfs4_stateid
*stateid
)
1176 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
1177 struct nfs_inode
*nfsi
= NFS_I(inode
);
1178 struct nfs_open_context
*ctx
;
1179 struct nfs4_state
*state
;
1182 spin_lock(&inode
->i_lock
);
1183 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
1187 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
1189 if (!nfs4_stateid_match(&state
->stateid
, stateid
))
1191 nfs4_state_mark_reclaim_nograce(clp
, state
);
1194 spin_unlock(&inode
->i_lock
);
1196 nfs4_schedule_state_manager(clp
);
1200 static int nfs4_reclaim_locks(struct nfs4_state
*state
, const struct nfs4_state_recovery_ops
*ops
)
1202 struct inode
*inode
= state
->inode
;
1203 struct nfs_inode
*nfsi
= NFS_I(inode
);
1204 struct file_lock
*fl
;
1207 if (inode
->i_flock
== NULL
)
1210 /* Guard against delegation returns and new lock/unlock calls */
1211 down_write(&nfsi
->rwsem
);
1212 /* Protect inode->i_flock using the BKL */
1214 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
1215 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
1217 if (nfs_file_open_context(fl
->fl_file
)->state
!= state
)
1220 status
= ops
->recover_lock(state
, fl
);
1225 case -NFS4ERR_ADMIN_REVOKED
:
1226 case -NFS4ERR_STALE_STATEID
:
1227 case -NFS4ERR_BAD_STATEID
:
1228 case -NFS4ERR_EXPIRED
:
1229 case -NFS4ERR_NO_GRACE
:
1230 case -NFS4ERR_STALE_CLIENTID
:
1231 case -NFS4ERR_BADSESSION
:
1232 case -NFS4ERR_BADSLOT
:
1233 case -NFS4ERR_BAD_HIGH_SLOT
:
1234 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1237 printk(KERN_ERR
"NFS: %s: unhandled error %d. "
1238 "Zeroing state\n", __func__
, status
);
1240 case -NFS4ERR_DENIED
:
1241 case -NFS4ERR_RECLAIM_BAD
:
1242 case -NFS4ERR_RECLAIM_CONFLICT
:
1243 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
1250 up_write(&nfsi
->rwsem
);
1254 static int nfs4_reclaim_open_state(struct nfs4_state_owner
*sp
, const struct nfs4_state_recovery_ops
*ops
)
1256 struct nfs4_state
*state
;
1257 struct nfs4_lock_state
*lock
;
1260 /* Note: we rely on the sp->so_states list being ordered
1261 * so that we always reclaim open(O_RDWR) and/or open(O_WRITE)
1263 * This is needed to ensure that the server won't give us any
1264 * read delegations that we have to return if, say, we are
1265 * recovering after a network partition or a reboot from a
1266 * server that doesn't support a grace period.
1269 spin_lock(&sp
->so_lock
);
1270 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1271 if (!test_and_clear_bit(ops
->state_flag_bit
, &state
->flags
))
1273 if (state
->state
== 0)
1275 atomic_inc(&state
->count
);
1276 spin_unlock(&sp
->so_lock
);
1277 status
= ops
->recover_open(sp
, state
);
1279 status
= nfs4_reclaim_locks(state
, ops
);
1281 spin_lock(&state
->state_lock
);
1282 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1283 if (!(lock
->ls_flags
& NFS_LOCK_INITIALIZED
))
1284 pr_warn_ratelimited("NFS: "
1286 "failed!\n", __func__
);
1288 spin_unlock(&state
->state_lock
);
1289 nfs4_put_open_state(state
);
1295 printk(KERN_ERR
"NFS: %s: unhandled error %d. "
1296 "Zeroing state\n", __func__
, status
);
1301 * Open state on this file cannot be recovered
1302 * All we can do is revert to using the zero stateid.
1304 memset(&state
->stateid
, 0,
1305 sizeof(state
->stateid
));
1306 /* Mark the file as being 'closed' */
1311 * User RPCSEC_GSS context has expired.
1312 * We cannot recover this stateid now, so
1313 * skip it and allow recovery thread to
1317 case -NFS4ERR_ADMIN_REVOKED
:
1318 case -NFS4ERR_STALE_STATEID
:
1319 case -NFS4ERR_BAD_STATEID
:
1320 case -NFS4ERR_RECLAIM_BAD
:
1321 case -NFS4ERR_RECLAIM_CONFLICT
:
1322 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1324 case -NFS4ERR_EXPIRED
:
1325 case -NFS4ERR_NO_GRACE
:
1326 nfs4_state_mark_reclaim_nograce(sp
->so_server
->nfs_client
, state
);
1327 case -NFS4ERR_STALE_CLIENTID
:
1328 case -NFS4ERR_BADSESSION
:
1329 case -NFS4ERR_BADSLOT
:
1330 case -NFS4ERR_BAD_HIGH_SLOT
:
1331 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1334 nfs4_put_open_state(state
);
1337 spin_unlock(&sp
->so_lock
);
1340 nfs4_put_open_state(state
);
1344 static void nfs4_clear_open_state(struct nfs4_state
*state
)
1346 struct nfs4_lock_state
*lock
;
1348 clear_bit(NFS_DELEGATED_STATE
, &state
->flags
);
1349 clear_bit(NFS_O_RDONLY_STATE
, &state
->flags
);
1350 clear_bit(NFS_O_WRONLY_STATE
, &state
->flags
);
1351 clear_bit(NFS_O_RDWR_STATE
, &state
->flags
);
1352 spin_lock(&state
->state_lock
);
1353 list_for_each_entry(lock
, &state
->lock_states
, ls_locks
) {
1354 lock
->ls_seqid
.flags
= 0;
1355 lock
->ls_flags
&= ~NFS_LOCK_INITIALIZED
;
1357 spin_unlock(&state
->state_lock
);
1360 static void nfs4_reset_seqids(struct nfs_server
*server
,
1361 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1363 struct nfs_client
*clp
= server
->nfs_client
;
1364 struct nfs4_state_owner
*sp
;
1365 struct rb_node
*pos
;
1366 struct nfs4_state
*state
;
1368 spin_lock(&clp
->cl_lock
);
1369 for (pos
= rb_first(&server
->state_owners
);
1371 pos
= rb_next(pos
)) {
1372 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1373 sp
->so_seqid
.flags
= 0;
1374 spin_lock(&sp
->so_lock
);
1375 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1376 if (mark_reclaim(clp
, state
))
1377 nfs4_clear_open_state(state
);
1379 spin_unlock(&sp
->so_lock
);
1381 spin_unlock(&clp
->cl_lock
);
1384 static void nfs4_state_mark_reclaim_helper(struct nfs_client
*clp
,
1385 int (*mark_reclaim
)(struct nfs_client
*clp
, struct nfs4_state
*state
))
1387 struct nfs_server
*server
;
1390 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1391 nfs4_reset_seqids(server
, mark_reclaim
);
1395 static void nfs4_state_start_reclaim_reboot(struct nfs_client
*clp
)
1397 /* Mark all delegations for reclaim */
1398 nfs_delegation_mark_reclaim(clp
);
1399 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_reboot
);
1402 static void nfs4_reclaim_complete(struct nfs_client
*clp
,
1403 const struct nfs4_state_recovery_ops
*ops
)
1405 /* Notify the server we're done reclaiming our state */
1406 if (ops
->reclaim_complete
)
1407 (void)ops
->reclaim_complete(clp
);
1410 static void nfs4_clear_reclaim_server(struct nfs_server
*server
)
1412 struct nfs_client
*clp
= server
->nfs_client
;
1413 struct nfs4_state_owner
*sp
;
1414 struct rb_node
*pos
;
1415 struct nfs4_state
*state
;
1417 spin_lock(&clp
->cl_lock
);
1418 for (pos
= rb_first(&server
->state_owners
);
1420 pos
= rb_next(pos
)) {
1421 sp
= rb_entry(pos
, struct nfs4_state_owner
, so_server_node
);
1422 spin_lock(&sp
->so_lock
);
1423 list_for_each_entry(state
, &sp
->so_states
, open_states
) {
1424 if (!test_and_clear_bit(NFS_STATE_RECLAIM_REBOOT
,
1427 nfs4_state_mark_reclaim_nograce(clp
, state
);
1429 spin_unlock(&sp
->so_lock
);
1431 spin_unlock(&clp
->cl_lock
);
1434 static int nfs4_state_clear_reclaim_reboot(struct nfs_client
*clp
)
1436 struct nfs_server
*server
;
1438 if (!test_and_clear_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
))
1442 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1443 nfs4_clear_reclaim_server(server
);
1446 nfs_delegation_reap_unclaimed(clp
);
1450 static void nfs4_state_end_reclaim_reboot(struct nfs_client
*clp
)
1452 if (!nfs4_state_clear_reclaim_reboot(clp
))
1454 nfs4_reclaim_complete(clp
, clp
->cl_mvops
->reboot_recovery_ops
);
1457 static void nfs_delegation_clear_all(struct nfs_client
*clp
)
1459 nfs_delegation_mark_reclaim(clp
);
1460 nfs_delegation_reap_unclaimed(clp
);
1463 static void nfs4_state_start_reclaim_nograce(struct nfs_client
*clp
)
1465 nfs_delegation_clear_all(clp
);
1466 nfs4_state_mark_reclaim_helper(clp
, nfs4_state_mark_reclaim_nograce
);
1469 static void nfs4_warn_keyexpired(const char *s
)
1471 printk_ratelimited(KERN_WARNING
"Error: state manager"
1472 " encountered RPCSEC_GSS session"
1473 " expired against NFSv4 server %s.\n",
1477 static int nfs4_recovery_handle_error(struct nfs_client
*clp
, int error
)
1482 case -NFS4ERR_CB_PATH_DOWN
:
1483 nfs40_handle_cb_pathdown(clp
);
1485 case -NFS4ERR_NO_GRACE
:
1486 nfs4_state_end_reclaim_reboot(clp
);
1488 case -NFS4ERR_STALE_CLIENTID
:
1489 case -NFS4ERR_LEASE_MOVED
:
1490 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1491 nfs4_state_clear_reclaim_reboot(clp
);
1492 nfs4_state_start_reclaim_reboot(clp
);
1494 case -NFS4ERR_EXPIRED
:
1495 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1496 nfs4_state_start_reclaim_nograce(clp
);
1498 case -NFS4ERR_BADSESSION
:
1499 case -NFS4ERR_BADSLOT
:
1500 case -NFS4ERR_BAD_HIGH_SLOT
:
1501 case -NFS4ERR_DEADSESSION
:
1502 case -NFS4ERR_SEQ_FALSE_RETRY
:
1503 case -NFS4ERR_SEQ_MISORDERED
:
1504 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
1505 /* Zero session reset errors */
1507 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1508 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
1511 /* Nothing we can do */
1512 nfs4_warn_keyexpired(clp
->cl_hostname
);
1515 dprintk("%s: failed to handle error %d for server %s\n",
1516 __func__
, error
, clp
->cl_hostname
);
1519 dprintk("%s: handled error %d for server %s\n", __func__
, error
,
1524 static int nfs4_do_reclaim(struct nfs_client
*clp
, const struct nfs4_state_recovery_ops
*ops
)
1526 struct nfs4_state_owner
*sp
;
1527 struct nfs_server
*server
;
1528 struct rb_node
*pos
;
1533 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1534 nfs4_purge_state_owners(server
);
1535 spin_lock(&clp
->cl_lock
);
1536 for (pos
= rb_first(&server
->state_owners
);
1538 pos
= rb_next(pos
)) {
1540 struct nfs4_state_owner
, so_server_node
);
1541 if (!test_and_clear_bit(ops
->owner_flag_bit
,
1544 atomic_inc(&sp
->so_count
);
1545 spin_unlock(&clp
->cl_lock
);
1548 status
= nfs4_reclaim_open_state(sp
, ops
);
1550 set_bit(ops
->owner_flag_bit
, &sp
->so_flags
);
1551 nfs4_put_state_owner(sp
);
1552 return nfs4_recovery_handle_error(clp
, status
);
1555 nfs4_put_state_owner(sp
);
1558 spin_unlock(&clp
->cl_lock
);
1564 static int nfs4_check_lease(struct nfs_client
*clp
)
1566 struct rpc_cred
*cred
;
1567 const struct nfs4_state_maintenance_ops
*ops
=
1568 clp
->cl_mvops
->state_renewal_ops
;
1571 /* Is the client already known to have an expired lease? */
1572 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1574 spin_lock(&clp
->cl_lock
);
1575 cred
= ops
->get_state_renewal_cred_locked(clp
);
1576 spin_unlock(&clp
->cl_lock
);
1578 cred
= nfs4_get_setclientid_cred(clp
);
1583 status
= ops
->renew_lease(clp
, cred
);
1586 return nfs4_recovery_handle_error(clp
, status
);
1589 /* Set NFS4CLNT_LEASE_EXPIRED for all v4.0 errors and for recoverable errors
1590 * on EXCHANGE_ID for v4.1
1592 static int nfs4_handle_reclaim_lease_error(struct nfs_client
*clp
, int status
)
1595 case -NFS4ERR_SEQ_MISORDERED
:
1596 if (test_and_set_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
))
1597 return -ESERVERFAULT
;
1598 /* Lease confirmation error: retry after purging the lease */
1600 case -NFS4ERR_CLID_INUSE
:
1601 case -NFS4ERR_STALE_CLIENTID
:
1602 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
1605 if (clp
->cl_machine_cred
== NULL
)
1607 /* Handle case where the user hasn't set up machine creds */
1608 nfs4_clear_machine_cred(clp
);
1609 case -NFS4ERR_DELAY
:
1615 case -NFS4ERR_MINOR_VERS_MISMATCH
:
1616 if (clp
->cl_cons_state
== NFS_CS_SESSION_INITING
)
1617 nfs_mark_client_ready(clp
, -EPROTONOSUPPORT
);
1618 dprintk("%s: exit with error %d for server %s\n",
1619 __func__
, -EPROTONOSUPPORT
, clp
->cl_hostname
);
1620 return -EPROTONOSUPPORT
;
1622 nfs4_warn_keyexpired(clp
->cl_hostname
);
1623 case -NFS4ERR_NOT_SAME
: /* FixMe: implement recovery
1624 * in nfs4_exchange_id */
1626 dprintk("%s: exit with error %d for server %s\n", __func__
,
1627 status
, clp
->cl_hostname
);
1630 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1631 dprintk("%s: handled error %d for server %s\n", __func__
, status
,
1636 static int nfs4_reclaim_lease(struct nfs_client
*clp
)
1638 struct rpc_cred
*cred
;
1639 const struct nfs4_state_recovery_ops
*ops
=
1640 clp
->cl_mvops
->reboot_recovery_ops
;
1643 cred
= ops
->get_clid_cred(clp
);
1646 status
= ops
->establish_clid(clp
, cred
);
1649 return nfs4_handle_reclaim_lease_error(clp
, status
);
1653 #ifdef CONFIG_NFS_V4_1
1654 void nfs4_schedule_session_recovery(struct nfs4_session
*session
, int err
)
1656 struct nfs_client
*clp
= session
->clp
;
1660 set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
1662 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION
:
1663 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
1665 nfs4_schedule_lease_recovery(clp
);
1667 EXPORT_SYMBOL_GPL(nfs4_schedule_session_recovery
);
1669 void nfs41_handle_recall_slot(struct nfs_client
*clp
)
1671 set_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
);
1672 dprintk("%s: scheduling slot recall for server %s\n", __func__
,
1674 nfs4_schedule_state_manager(clp
);
1677 static void nfs4_reset_all_state(struct nfs_client
*clp
)
1679 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
1680 set_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
);
1681 clear_bit(NFS4CLNT_LEASE_CONFIRM
, &clp
->cl_state
);
1682 nfs4_state_start_reclaim_nograce(clp
);
1683 dprintk("%s: scheduling reset of all state for server %s!\n",
1684 __func__
, clp
->cl_hostname
);
1685 nfs4_schedule_state_manager(clp
);
1689 static void nfs41_handle_server_reboot(struct nfs_client
*clp
)
1691 if (test_and_set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0) {
1692 nfs4_state_start_reclaim_reboot(clp
);
1693 dprintk("%s: server %s rebooted!\n", __func__
,
1695 nfs4_schedule_state_manager(clp
);
1699 static void nfs41_handle_state_revoked(struct nfs_client
*clp
)
1701 nfs4_reset_all_state(clp
);
1702 dprintk("%s: state revoked on server %s\n", __func__
, clp
->cl_hostname
);
1705 static void nfs41_handle_recallable_state_revoked(struct nfs_client
*clp
)
1707 /* This will need to handle layouts too */
1708 nfs_expire_all_delegations(clp
);
1709 dprintk("%s: Recallable state revoked on server %s!\n", __func__
,
1713 static void nfs41_handle_backchannel_fault(struct nfs_client
*clp
)
1715 nfs_expire_all_delegations(clp
);
1716 if (test_and_set_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
) == 0)
1717 nfs4_schedule_state_manager(clp
);
1718 dprintk("%s: server %s declared a backchannel fault\n", __func__
,
1722 static void nfs41_handle_cb_path_down(struct nfs_client
*clp
)
1724 if (test_and_set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
,
1725 &clp
->cl_state
) == 0)
1726 nfs4_schedule_state_manager(clp
);
1729 void nfs41_handle_sequence_flag_errors(struct nfs_client
*clp
, u32 flags
)
1734 dprintk("%s: \"%s\" (client ID %llx) flags=0x%08x\n",
1735 __func__
, clp
->cl_hostname
, clp
->cl_clientid
, flags
);
1737 if (flags
& SEQ4_STATUS_RESTART_RECLAIM_NEEDED
)
1738 nfs41_handle_server_reboot(clp
);
1739 if (flags
& (SEQ4_STATUS_EXPIRED_ALL_STATE_REVOKED
|
1740 SEQ4_STATUS_EXPIRED_SOME_STATE_REVOKED
|
1741 SEQ4_STATUS_ADMIN_STATE_REVOKED
|
1742 SEQ4_STATUS_LEASE_MOVED
))
1743 nfs41_handle_state_revoked(clp
);
1744 if (flags
& SEQ4_STATUS_RECALLABLE_STATE_REVOKED
)
1745 nfs41_handle_recallable_state_revoked(clp
);
1746 if (flags
& SEQ4_STATUS_BACKCHANNEL_FAULT
)
1747 nfs41_handle_backchannel_fault(clp
);
1748 else if (flags
& (SEQ4_STATUS_CB_PATH_DOWN
|
1749 SEQ4_STATUS_CB_PATH_DOWN_SESSION
))
1750 nfs41_handle_cb_path_down(clp
);
1753 static int nfs4_reset_session(struct nfs_client
*clp
)
1755 struct rpc_cred
*cred
;
1758 nfs4_begin_drain_session(clp
);
1759 cred
= nfs4_get_exchange_id_cred(clp
);
1760 status
= nfs4_proc_destroy_session(clp
->cl_session
, cred
);
1761 if (status
&& status
!= -NFS4ERR_BADSESSION
&&
1762 status
!= -NFS4ERR_DEADSESSION
) {
1763 status
= nfs4_recovery_handle_error(clp
, status
);
1767 memset(clp
->cl_session
->sess_id
.data
, 0, NFS4_MAX_SESSIONID_LEN
);
1768 status
= nfs4_proc_create_session(clp
, cred
);
1770 dprintk("%s: session reset failed with status %d for server %s!\n",
1771 __func__
, status
, clp
->cl_hostname
);
1772 status
= nfs4_handle_reclaim_lease_error(clp
, status
);
1775 clear_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
);
1776 /* create_session negotiated new slot table */
1777 clear_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
);
1778 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
1779 dprintk("%s: session reset was successful for server %s!\n",
1780 __func__
, clp
->cl_hostname
);
1782 /* Let the state manager reestablish state */
1783 if (!test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1784 nfs41_setup_state_renewal(clp
);
1791 static int nfs4_recall_slot(struct nfs_client
*clp
)
1793 struct nfs4_slot_table
*fc_tbl
= &clp
->cl_session
->fc_slot_table
;
1794 struct nfs4_channel_attrs
*fc_attrs
= &clp
->cl_session
->fc_attrs
;
1795 struct nfs4_slot
*new, *old
;
1798 nfs4_begin_drain_session(clp
);
1799 new = kmalloc(fc_tbl
->target_max_slots
* sizeof(struct nfs4_slot
),
1804 spin_lock(&fc_tbl
->slot_tbl_lock
);
1805 for (i
= 0; i
< fc_tbl
->target_max_slots
; i
++)
1806 new[i
].seq_nr
= fc_tbl
->slots
[i
].seq_nr
;
1807 old
= fc_tbl
->slots
;
1808 fc_tbl
->slots
= new;
1809 fc_tbl
->max_slots
= fc_tbl
->target_max_slots
;
1810 fc_tbl
->target_max_slots
= 0;
1811 fc_attrs
->max_reqs
= fc_tbl
->max_slots
;
1812 spin_unlock(&fc_tbl
->slot_tbl_lock
);
1815 nfs4_end_drain_session(clp
);
1819 static int nfs4_bind_conn_to_session(struct nfs_client
*clp
)
1821 struct rpc_cred
*cred
;
1824 nfs4_begin_drain_session(clp
);
1825 cred
= nfs4_get_exchange_id_cred(clp
);
1826 ret
= nfs4_proc_bind_conn_to_session(clp
, cred
);
1829 clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
1832 dprintk("%s: bind_conn_to_session was successful for server %s!\n",
1833 __func__
, clp
->cl_hostname
);
1835 case -NFS4ERR_DELAY
:
1837 set_bit(NFS4CLNT_BIND_CONN_TO_SESSION
, &clp
->cl_state
);
1840 return nfs4_recovery_handle_error(clp
, ret
);
1844 #else /* CONFIG_NFS_V4_1 */
1845 static int nfs4_reset_session(struct nfs_client
*clp
) { return 0; }
1846 static int nfs4_end_drain_session(struct nfs_client
*clp
) { return 0; }
1847 static int nfs4_recall_slot(struct nfs_client
*clp
) { return 0; }
1849 static int nfs4_bind_conn_to_session(struct nfs_client
*clp
)
1853 #endif /* CONFIG_NFS_V4_1 */
1855 static void nfs4_state_manager(struct nfs_client
*clp
)
1859 /* Ensure exclusive access to NFSv4 state */
1861 if (test_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
)) {
1862 status
= nfs4_reclaim_lease(clp
);
1865 clear_bit(NFS4CLNT_PURGE_STATE
, &clp
->cl_state
);
1866 set_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
);
1869 if (test_and_clear_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
)) {
1870 /* We're going to have to re-establish a clientid */
1871 status
= nfs4_reclaim_lease(clp
);
1874 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1876 clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
);
1878 if (test_and_clear_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH
,
1880 nfs4_state_start_reclaim_nograce(clp
);
1882 set_bit(NFS4CLNT_RECLAIM_REBOOT
,
1885 pnfs_destroy_all_layouts(clp
);
1888 if (test_and_clear_bit(NFS4CLNT_CHECK_LEASE
, &clp
->cl_state
)) {
1889 status
= nfs4_check_lease(clp
);
1892 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1896 /* Initialize or reset the session */
1897 if (test_and_clear_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
)
1898 && nfs4_has_session(clp
)) {
1899 status
= nfs4_reset_session(clp
);
1900 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
))
1906 /* Send BIND_CONN_TO_SESSION */
1907 if (test_and_clear_bit(NFS4CLNT_BIND_CONN_TO_SESSION
,
1908 &clp
->cl_state
) && nfs4_has_session(clp
)) {
1909 status
= nfs4_bind_conn_to_session(clp
);
1915 /* First recover reboot state... */
1916 if (test_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
)) {
1917 status
= nfs4_do_reclaim(clp
,
1918 clp
->cl_mvops
->reboot_recovery_ops
);
1919 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) ||
1920 test_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
))
1922 nfs4_state_end_reclaim_reboot(clp
);
1923 if (test_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
))
1929 /* Now recover expired state... */
1930 if (test_and_clear_bit(NFS4CLNT_RECLAIM_NOGRACE
, &clp
->cl_state
)) {
1931 status
= nfs4_do_reclaim(clp
,
1932 clp
->cl_mvops
->nograce_recovery_ops
);
1933 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) ||
1934 test_bit(NFS4CLNT_SESSION_RESET
, &clp
->cl_state
) ||
1935 test_bit(NFS4CLNT_RECLAIM_REBOOT
, &clp
->cl_state
))
1941 nfs4_end_drain_session(clp
);
1942 if (test_and_clear_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
)) {
1943 nfs_client_return_marked_delegations(clp
);
1946 /* Recall session slots */
1947 if (test_and_clear_bit(NFS4CLNT_RECALL_SLOT
, &clp
->cl_state
)
1948 && nfs4_has_session(clp
)) {
1949 status
= nfs4_recall_slot(clp
);
1956 nfs4_clear_state_manager_bit(clp
);
1957 /* Did we race with an attempt to give us more work? */
1958 if (clp
->cl_state
== 0)
1960 if (test_and_set_bit(NFS4CLNT_MANAGER_RUNNING
, &clp
->cl_state
) != 0)
1962 } while (atomic_read(&clp
->cl_count
) > 1);
1965 pr_warn_ratelimited("NFS: state manager failed on NFSv4 server %s"
1966 " with error %d\n", clp
->cl_hostname
, -status
);
1967 nfs4_end_drain_session(clp
);
1968 nfs4_clear_state_manager_bit(clp
);
1971 static int nfs4_run_state_manager(void *ptr
)
1973 struct nfs_client
*clp
= ptr
;
1975 allow_signal(SIGKILL
);
1976 nfs4_state_manager(clp
);
1977 nfs_put_client(clp
);
1978 module_put_and_exit(0);