2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
9 #include <linux/completion.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/spinlock.h>
15 #include <linux/nfs4.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_xdr.h>
20 #include "delegation.h"
23 static void nfs_do_free_delegation(struct nfs_delegation
*delegation
)
28 static void nfs_free_delegation_callback(struct rcu_head
*head
)
30 struct nfs_delegation
*delegation
= container_of(head
, struct nfs_delegation
, rcu
);
32 nfs_do_free_delegation(delegation
);
35 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
37 struct rpc_cred
*cred
;
39 cred
= rcu_dereference(delegation
->cred
);
40 rcu_assign_pointer(delegation
->cred
, NULL
);
41 call_rcu(&delegation
->rcu
, nfs_free_delegation_callback
);
46 void nfs_mark_delegation_referenced(struct nfs_delegation
*delegation
)
48 set_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
);
51 int nfs_have_delegation(struct inode
*inode
, fmode_t flags
)
53 struct nfs_delegation
*delegation
;
56 flags
&= FMODE_READ
|FMODE_WRITE
;
58 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
59 if (delegation
!= NULL
&& (delegation
->type
& flags
) == flags
) {
60 nfs_mark_delegation_referenced(delegation
);
67 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
69 struct inode
*inode
= state
->inode
;
73 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
74 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
76 if (nfs_file_open_context(fl
->fl_file
) != ctx
)
78 status
= nfs4_lock_delegation_recall(state
, fl
);
85 static void nfs_delegation_claim_opens(struct inode
*inode
, const nfs4_stateid
*stateid
)
87 struct nfs_inode
*nfsi
= NFS_I(inode
);
88 struct nfs_open_context
*ctx
;
89 struct nfs4_state
*state
;
93 spin_lock(&inode
->i_lock
);
94 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
98 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
100 if (memcmp(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
)) != 0)
102 get_nfs_open_context(ctx
);
103 spin_unlock(&inode
->i_lock
);
104 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
);
106 err
= nfs_delegation_claim_locks(ctx
, state
);
107 put_nfs_open_context(ctx
);
112 spin_unlock(&inode
->i_lock
);
116 * Set up a delegation on an inode
118 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
120 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
121 struct rpc_cred
*oldcred
;
123 if (delegation
== NULL
)
125 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
126 sizeof(delegation
->stateid
.data
));
127 delegation
->type
= res
->delegation_type
;
128 delegation
->maxsize
= res
->maxsize
;
129 oldcred
= delegation
->cred
;
130 delegation
->cred
= get_rpccred(cred
);
131 clear_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
132 NFS_I(inode
)->delegation_state
= delegation
->type
;
134 put_rpccred(oldcred
);
137 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
141 res
= nfs4_proc_delegreturn(inode
, delegation
->cred
, &delegation
->stateid
, issync
);
142 nfs_free_delegation(delegation
);
146 static struct inode
*nfs_delegation_grab_inode(struct nfs_delegation
*delegation
)
148 struct inode
*inode
= NULL
;
150 spin_lock(&delegation
->lock
);
151 if (delegation
->inode
!= NULL
)
152 inode
= igrab(delegation
->inode
);
153 spin_unlock(&delegation
->lock
);
157 static struct nfs_delegation
*nfs_detach_delegation_locked(struct nfs_inode
*nfsi
, const nfs4_stateid
*stateid
)
159 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
161 if (delegation
== NULL
)
163 spin_lock(&delegation
->lock
);
164 if (stateid
!= NULL
&& memcmp(delegation
->stateid
.data
, stateid
->data
,
165 sizeof(delegation
->stateid
.data
)) != 0)
167 list_del_rcu(&delegation
->super_list
);
168 delegation
->inode
= NULL
;
169 nfsi
->delegation_state
= 0;
170 rcu_assign_pointer(nfsi
->delegation
, NULL
);
171 spin_unlock(&delegation
->lock
);
174 spin_unlock(&delegation
->lock
);
180 * Set up a delegation on an inode
182 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
184 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
185 struct nfs_inode
*nfsi
= NFS_I(inode
);
186 struct nfs_delegation
*delegation
;
187 struct nfs_delegation
*freeme
= NULL
;
190 delegation
= kmalloc(sizeof(*delegation
), GFP_KERNEL
);
191 if (delegation
== NULL
)
193 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
194 sizeof(delegation
->stateid
.data
));
195 delegation
->type
= res
->delegation_type
;
196 delegation
->maxsize
= res
->maxsize
;
197 delegation
->change_attr
= nfsi
->change_attr
;
198 delegation
->cred
= get_rpccred(cred
);
199 delegation
->inode
= inode
;
200 delegation
->flags
= 1<<NFS_DELEGATION_REFERENCED
;
201 spin_lock_init(&delegation
->lock
);
203 spin_lock(&clp
->cl_lock
);
204 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
205 if (memcmp(&delegation
->stateid
, &nfsi
->delegation
->stateid
,
206 sizeof(delegation
->stateid
)) == 0 &&
207 delegation
->type
== nfsi
->delegation
->type
) {
211 * Deal with broken servers that hand out two
212 * delegations for the same file.
214 dfprintk(FILE, "%s: server %s handed out "
215 "a duplicate delegation!\n",
216 __func__
, clp
->cl_hostname
);
217 if (delegation
->type
<= nfsi
->delegation
->type
) {
222 freeme
= nfs_detach_delegation_locked(nfsi
, NULL
);
224 list_add_rcu(&delegation
->super_list
, &clp
->cl_delegations
);
225 nfsi
->delegation_state
= delegation
->type
;
226 rcu_assign_pointer(nfsi
->delegation
, delegation
);
229 /* Ensure we revalidate the attributes and page cache! */
230 spin_lock(&inode
->i_lock
);
231 nfsi
->cache_validity
|= NFS_INO_REVAL_FORCED
;
232 spin_unlock(&inode
->i_lock
);
235 spin_unlock(&clp
->cl_lock
);
236 if (delegation
!= NULL
)
237 nfs_free_delegation(delegation
);
239 nfs_do_return_delegation(inode
, freeme
, 0);
243 /* Sync all data to disk upon delegation return */
244 static void nfs_msync_inode(struct inode
*inode
)
246 filemap_fdatawrite(inode
->i_mapping
);
248 filemap_fdatawait(inode
->i_mapping
);
252 * Basic procedure for returning a delegation to the server
254 static int __nfs_inode_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
)
256 struct nfs_inode
*nfsi
= NFS_I(inode
);
258 nfs_msync_inode(inode
);
259 /* Guard against new delegated open calls */
260 down_write(&nfsi
->rwsem
);
261 nfs_delegation_claim_opens(inode
, &delegation
->stateid
);
262 up_write(&nfsi
->rwsem
);
263 nfs_msync_inode(inode
);
265 return nfs_do_return_delegation(inode
, delegation
, 1);
269 * Return all delegations that have been marked for return
271 void nfs_client_return_marked_delegations(struct nfs_client
*clp
)
273 struct nfs_delegation
*delegation
;
278 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
279 if (!test_and_clear_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
))
281 inode
= nfs_delegation_grab_inode(delegation
);
284 spin_lock(&clp
->cl_lock
);
285 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
286 spin_unlock(&clp
->cl_lock
);
288 if (delegation
!= NULL
)
289 __nfs_inode_return_delegation(inode
, delegation
);
297 * This function returns the delegation without reclaiming opens
298 * or protecting against delegation reclaims.
299 * It is therefore really only safe to be called from
302 void nfs_inode_return_delegation_noreclaim(struct inode
*inode
)
304 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
305 struct nfs_inode
*nfsi
= NFS_I(inode
);
306 struct nfs_delegation
*delegation
;
308 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
309 spin_lock(&clp
->cl_lock
);
310 delegation
= nfs_detach_delegation_locked(nfsi
, NULL
);
311 spin_unlock(&clp
->cl_lock
);
312 if (delegation
!= NULL
)
313 nfs_do_return_delegation(inode
, delegation
, 0);
317 int nfs_inode_return_delegation(struct inode
*inode
)
319 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
320 struct nfs_inode
*nfsi
= NFS_I(inode
);
321 struct nfs_delegation
*delegation
;
324 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
325 spin_lock(&clp
->cl_lock
);
326 delegation
= nfs_detach_delegation_locked(nfsi
, NULL
);
327 spin_unlock(&clp
->cl_lock
);
328 if (delegation
!= NULL
)
329 err
= __nfs_inode_return_delegation(inode
, delegation
);
334 static void nfs_mark_return_delegation(struct nfs_client
*clp
, struct nfs_delegation
*delegation
)
336 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
337 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
341 * Return all delegations associated to a super block
343 void nfs_super_return_all_delegations(struct super_block
*sb
)
345 struct nfs_client
*clp
= NFS_SB(sb
)->nfs_client
;
346 struct nfs_delegation
*delegation
;
351 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
352 spin_lock(&delegation
->lock
);
353 if (delegation
->inode
!= NULL
&& delegation
->inode
->i_sb
== sb
)
354 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
355 spin_unlock(&delegation
->lock
);
358 nfs_client_return_marked_delegations(clp
);
361 static void nfs_client_mark_return_all_delegations(struct nfs_client
*clp
)
363 struct nfs_delegation
*delegation
;
366 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
367 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
368 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
373 static void nfs_delegation_run_state_manager(struct nfs_client
*clp
)
375 if (test_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
))
376 nfs4_schedule_state_manager(clp
);
379 void nfs_expire_all_delegations(struct nfs_client
*clp
)
381 nfs_client_mark_return_all_delegations(clp
);
382 nfs_delegation_run_state_manager(clp
);
386 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
388 void nfs_handle_cb_pathdown(struct nfs_client
*clp
)
392 nfs_client_mark_return_all_delegations(clp
);
395 static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client
*clp
)
397 struct nfs_delegation
*delegation
;
400 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
401 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
))
403 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
404 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
409 void nfs_expire_unreferenced_delegations(struct nfs_client
*clp
)
411 nfs_client_mark_return_unreferenced_delegations(clp
);
412 nfs_delegation_run_state_manager(clp
);
416 * Asynchronous delegation recall!
418 int nfs_async_inode_return_delegation(struct inode
*inode
, const nfs4_stateid
*stateid
)
420 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
421 struct nfs_delegation
*delegation
;
424 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
425 if (delegation
== NULL
|| memcmp(delegation
->stateid
.data
, stateid
->data
,
426 sizeof(delegation
->stateid
.data
)) != 0) {
430 nfs_mark_return_delegation(clp
, delegation
);
432 nfs_delegation_run_state_manager(clp
);
437 * Retrieve the inode associated with a delegation
439 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
, const struct nfs_fh
*fhandle
)
441 struct nfs_delegation
*delegation
;
442 struct inode
*res
= NULL
;
444 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
445 spin_lock(&delegation
->lock
);
446 if (delegation
->inode
!= NULL
&&
447 nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
448 res
= igrab(delegation
->inode
);
450 spin_unlock(&delegation
->lock
);
459 * Mark all delegations as needing to be reclaimed
461 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
463 struct nfs_delegation
*delegation
;
465 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
)
466 set_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
471 * Reap all unclaimed delegations after reboot recovery is done
473 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
475 struct nfs_delegation
*delegation
;
479 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
480 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
) == 0)
482 inode
= nfs_delegation_grab_inode(delegation
);
485 spin_lock(&clp
->cl_lock
);
486 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
487 spin_unlock(&clp
->cl_lock
);
489 if (delegation
!= NULL
)
490 nfs_free_delegation(delegation
);
497 int nfs4_copy_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
499 struct nfs_inode
*nfsi
= NFS_I(inode
);
500 struct nfs_delegation
*delegation
;
504 delegation
= rcu_dereference(nfsi
->delegation
);
505 if (delegation
!= NULL
) {
506 memcpy(dst
->data
, delegation
->stateid
.data
, sizeof(dst
->data
));