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 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
48 struct inode
*inode
= state
->inode
;
52 for (fl
= inode
->i_flock
; fl
!= 0; fl
= fl
->fl_next
) {
53 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
55 if (nfs_file_open_context(fl
->fl_file
) != ctx
)
57 status
= nfs4_lock_delegation_recall(state
, fl
);
62 printk(KERN_ERR
"%s: unhandled error %d.\n",
63 __FUNCTION__
, status
);
64 case -NFS4ERR_EXPIRED
:
65 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
66 case -NFS4ERR_STALE_CLIENTID
:
67 nfs4_schedule_state_recovery(NFS_SERVER(inode
)->nfs_client
);
76 static void nfs_delegation_claim_opens(struct inode
*inode
, const nfs4_stateid
*stateid
)
78 struct nfs_inode
*nfsi
= NFS_I(inode
);
79 struct nfs_open_context
*ctx
;
80 struct nfs4_state
*state
;
84 spin_lock(&inode
->i_lock
);
85 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
89 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
91 if (memcmp(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
)) != 0)
93 get_nfs_open_context(ctx
);
94 spin_unlock(&inode
->i_lock
);
95 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
);
97 err
= nfs_delegation_claim_locks(ctx
, state
);
98 put_nfs_open_context(ctx
);
103 spin_unlock(&inode
->i_lock
);
107 * Set up a delegation on an inode
109 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
111 struct nfs_delegation
*delegation
= NFS_I(inode
)->delegation
;
112 struct rpc_cred
*oldcred
;
114 if (delegation
== NULL
)
116 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
117 sizeof(delegation
->stateid
.data
));
118 delegation
->type
= res
->delegation_type
;
119 delegation
->maxsize
= res
->maxsize
;
120 oldcred
= delegation
->cred
;
121 delegation
->cred
= get_rpccred(cred
);
122 delegation
->flags
&= ~NFS_DELEGATION_NEED_RECLAIM
;
123 NFS_I(inode
)->delegation_state
= delegation
->type
;
125 put_rpccred(oldcred
);
128 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
132 res
= nfs4_proc_delegreturn(inode
, delegation
->cred
, &delegation
->stateid
, issync
);
133 nfs_free_delegation(delegation
);
137 static struct nfs_delegation
*nfs_detach_delegation_locked(struct nfs_inode
*nfsi
, const nfs4_stateid
*stateid
)
139 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
141 if (delegation
== NULL
)
143 if (stateid
!= NULL
&& memcmp(delegation
->stateid
.data
, stateid
->data
,
144 sizeof(delegation
->stateid
.data
)) != 0)
146 list_del_rcu(&delegation
->super_list
);
147 nfsi
->delegation_state
= 0;
148 rcu_assign_pointer(nfsi
->delegation
, NULL
);
155 * Set up a delegation on an inode
157 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
159 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
160 struct nfs_inode
*nfsi
= NFS_I(inode
);
161 struct nfs_delegation
*delegation
;
162 struct nfs_delegation
*freeme
= NULL
;
165 delegation
= kmalloc(sizeof(*delegation
), GFP_KERNEL
);
166 if (delegation
== NULL
)
168 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
169 sizeof(delegation
->stateid
.data
));
170 delegation
->type
= res
->delegation_type
;
171 delegation
->maxsize
= res
->maxsize
;
172 delegation
->change_attr
= nfsi
->change_attr
;
173 delegation
->cred
= get_rpccred(cred
);
174 delegation
->inode
= inode
;
176 spin_lock(&clp
->cl_lock
);
177 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
178 if (memcmp(&delegation
->stateid
, &nfsi
->delegation
->stateid
,
179 sizeof(delegation
->stateid
)) == 0 &&
180 delegation
->type
== nfsi
->delegation
->type
) {
184 * Deal with broken servers that hand out two
185 * delegations for the same file.
187 dfprintk(FILE, "%s: server %s handed out "
188 "a duplicate delegation!\n",
189 __FUNCTION__
, clp
->cl_hostname
);
190 if (delegation
->type
<= nfsi
->delegation
->type
) {
195 freeme
= nfs_detach_delegation_locked(nfsi
, NULL
);
197 list_add_rcu(&delegation
->super_list
, &clp
->cl_delegations
);
198 nfsi
->delegation_state
= delegation
->type
;
199 rcu_assign_pointer(nfsi
->delegation
, delegation
);
202 /* Ensure we revalidate the attributes and page cache! */
203 spin_lock(&inode
->i_lock
);
204 nfsi
->cache_validity
|= NFS_INO_REVAL_FORCED
;
205 spin_unlock(&inode
->i_lock
);
208 spin_unlock(&clp
->cl_lock
);
209 if (delegation
!= NULL
)
210 nfs_free_delegation(delegation
);
212 nfs_do_return_delegation(inode
, freeme
, 0);
216 /* Sync all data to disk upon delegation return */
217 static void nfs_msync_inode(struct inode
*inode
)
219 filemap_fdatawrite(inode
->i_mapping
);
221 filemap_fdatawait(inode
->i_mapping
);
225 * Basic procedure for returning a delegation to the server
227 static int __nfs_inode_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
)
229 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
230 struct nfs_inode
*nfsi
= NFS_I(inode
);
232 nfs_msync_inode(inode
);
233 down_read(&clp
->cl_sem
);
234 /* Guard against new delegated open calls */
235 down_write(&nfsi
->rwsem
);
236 nfs_delegation_claim_opens(inode
, &delegation
->stateid
);
237 up_write(&nfsi
->rwsem
);
238 up_read(&clp
->cl_sem
);
239 nfs_msync_inode(inode
);
241 return nfs_do_return_delegation(inode
, delegation
, 1);
245 * This function returns the delegation without reclaiming opens
246 * or protecting against delegation reclaims.
247 * It is therefore really only safe to be called from
250 void nfs_inode_return_delegation_noreclaim(struct inode
*inode
)
252 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
253 struct nfs_inode
*nfsi
= NFS_I(inode
);
254 struct nfs_delegation
*delegation
;
256 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
257 spin_lock(&clp
->cl_lock
);
258 delegation
= nfs_detach_delegation_locked(nfsi
, NULL
);
259 spin_unlock(&clp
->cl_lock
);
260 if (delegation
!= NULL
)
261 nfs_do_return_delegation(inode
, delegation
, 0);
265 int nfs_inode_return_delegation(struct inode
*inode
)
267 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
268 struct nfs_inode
*nfsi
= NFS_I(inode
);
269 struct nfs_delegation
*delegation
;
272 if (rcu_dereference(nfsi
->delegation
) != NULL
) {
273 spin_lock(&clp
->cl_lock
);
274 delegation
= nfs_detach_delegation_locked(nfsi
, NULL
);
275 spin_unlock(&clp
->cl_lock
);
276 if (delegation
!= NULL
)
277 err
= __nfs_inode_return_delegation(inode
, delegation
);
283 * Return all delegations associated to a super block
285 void nfs_return_all_delegations(struct super_block
*sb
)
287 struct nfs_client
*clp
= NFS_SB(sb
)->nfs_client
;
288 struct nfs_delegation
*delegation
;
295 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
296 if (delegation
->inode
->i_sb
!= sb
)
298 inode
= igrab(delegation
->inode
);
301 spin_lock(&clp
->cl_lock
);
302 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
303 spin_unlock(&clp
->cl_lock
);
305 if (delegation
!= NULL
)
306 __nfs_inode_return_delegation(inode
, delegation
);
313 static int nfs_do_expire_all_delegations(void *ptr
)
315 struct nfs_client
*clp
= ptr
;
316 struct nfs_delegation
*delegation
;
319 allow_signal(SIGKILL
);
321 if (test_bit(NFS4CLNT_STATE_RECOVER
, &clp
->cl_state
) != 0)
323 if (test_bit(NFS4CLNT_LEASE_EXPIRED
, &clp
->cl_state
) == 0)
326 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
327 inode
= igrab(delegation
->inode
);
330 spin_lock(&clp
->cl_lock
);
331 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
332 spin_unlock(&clp
->cl_lock
);
335 __nfs_inode_return_delegation(inode
, delegation
);
342 module_put_and_exit(0);
345 void nfs_expire_all_delegations(struct nfs_client
*clp
)
347 struct task_struct
*task
;
349 __module_get(THIS_MODULE
);
350 atomic_inc(&clp
->cl_count
);
351 task
= kthread_run(nfs_do_expire_all_delegations
, clp
,
353 rpc_peeraddr2str(clp
->cl_rpcclient
,
358 module_put(THIS_MODULE
);
362 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
364 void nfs_handle_cb_pathdown(struct nfs_client
*clp
)
366 struct nfs_delegation
*delegation
;
373 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
374 inode
= igrab(delegation
->inode
);
377 spin_lock(&clp
->cl_lock
);
378 delegation
= nfs_detach_delegation_locked(NFS_I(inode
), NULL
);
379 spin_unlock(&clp
->cl_lock
);
381 if (delegation
!= NULL
)
382 __nfs_inode_return_delegation(inode
, delegation
);
389 struct recall_threadargs
{
391 struct nfs_client
*clp
;
392 const nfs4_stateid
*stateid
;
394 struct completion started
;
398 static int recall_thread(void *data
)
400 struct recall_threadargs
*args
= (struct recall_threadargs
*)data
;
401 struct inode
*inode
= igrab(args
->inode
);
402 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
403 struct nfs_inode
*nfsi
= NFS_I(inode
);
404 struct nfs_delegation
*delegation
;
406 daemonize("nfsv4-delegreturn");
408 nfs_msync_inode(inode
);
409 down_read(&clp
->cl_sem
);
410 down_write(&nfsi
->rwsem
);
411 spin_lock(&clp
->cl_lock
);
412 delegation
= nfs_detach_delegation_locked(nfsi
, args
->stateid
);
413 if (delegation
!= NULL
)
416 args
->result
= -ENOENT
;
417 spin_unlock(&clp
->cl_lock
);
418 complete(&args
->started
);
419 nfs_delegation_claim_opens(inode
, args
->stateid
);
420 up_write(&nfsi
->rwsem
);
421 up_read(&clp
->cl_sem
);
422 nfs_msync_inode(inode
);
424 if (delegation
!= NULL
)
425 nfs_do_return_delegation(inode
, delegation
, 1);
427 module_put_and_exit(0);
431 * Asynchronous delegation recall!
433 int nfs_async_inode_return_delegation(struct inode
*inode
, const nfs4_stateid
*stateid
)
435 struct recall_threadargs data
= {
441 init_completion(&data
.started
);
442 __module_get(THIS_MODULE
);
443 status
= kernel_thread(recall_thread
, &data
, CLONE_KERNEL
);
446 wait_for_completion(&data
.started
);
449 module_put(THIS_MODULE
);
454 * Retrieve the inode associated with a delegation
456 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
, const struct nfs_fh
*fhandle
)
458 struct nfs_delegation
*delegation
;
459 struct inode
*res
= NULL
;
461 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
462 if (nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
463 res
= igrab(delegation
->inode
);
472 * Mark all delegations as needing to be reclaimed
474 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
476 struct nfs_delegation
*delegation
;
478 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
)
479 delegation
->flags
|= NFS_DELEGATION_NEED_RECLAIM
;
484 * Reap all unclaimed delegations after reboot recovery is done
486 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
488 struct nfs_delegation
*delegation
;
491 list_for_each_entry_rcu(delegation
, &clp
->cl_delegations
, super_list
) {
492 if ((delegation
->flags
& NFS_DELEGATION_NEED_RECLAIM
) == 0)
494 spin_lock(&clp
->cl_lock
);
495 delegation
= nfs_detach_delegation_locked(NFS_I(delegation
->inode
), NULL
);
496 spin_unlock(&clp
->cl_lock
);
498 if (delegation
!= NULL
)
499 nfs_free_delegation(delegation
);
505 int nfs4_copy_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
507 struct nfs_inode
*nfsi
= NFS_I(inode
);
508 struct nfs_delegation
*delegation
;
512 delegation
= rcu_dereference(nfsi
->delegation
);
513 if (delegation
!= NULL
) {
514 memcpy(dst
->data
, delegation
->stateid
.data
, sizeof(dst
->data
));