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 != NULL; 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",
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 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
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 inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
139 struct inode *inode = NULL;
141 spin_lock(&delegation->lock);
142 if (delegation->inode != NULL)
143 inode = igrab(delegation->inode);
144 spin_unlock(&delegation->lock);
148 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
150 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
152 if (delegation == NULL)
154 spin_lock(&delegation->lock);
155 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
156 sizeof(delegation->stateid.data)) != 0)
158 list_del_rcu(&delegation->super_list);
159 delegation->inode = NULL;
160 nfsi->delegation_state = 0;
161 rcu_assign_pointer(nfsi->delegation, NULL);
162 spin_unlock(&delegation->lock);
165 spin_unlock(&delegation->lock);
171 * Set up a delegation on an inode
173 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
175 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
176 struct nfs_inode *nfsi = NFS_I(inode);
177 struct nfs_delegation *delegation;
178 struct nfs_delegation *freeme = NULL;
181 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
182 if (delegation == NULL)
184 memcpy(delegation->stateid.data, res->delegation.data,
185 sizeof(delegation->stateid.data));
186 delegation->type = res->delegation_type;
187 delegation->maxsize = res->maxsize;
188 delegation->change_attr = nfsi->change_attr;
189 delegation->cred = get_rpccred(cred);
190 delegation->inode = inode;
191 spin_lock_init(&delegation->lock);
193 spin_lock(&clp->cl_lock);
194 if (rcu_dereference(nfsi->delegation) != NULL) {
195 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
196 sizeof(delegation->stateid)) == 0 &&
197 delegation->type == nfsi->delegation->type) {
201 * Deal with broken servers that hand out two
202 * delegations for the same file.
204 dfprintk(FILE, "%s: server %s handed out "
205 "a duplicate delegation!\n",
206 __func__, clp->cl_hostname);
207 if (delegation->type <= nfsi->delegation->type) {
212 freeme = nfs_detach_delegation_locked(nfsi, NULL);
214 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
215 nfsi->delegation_state = delegation->type;
216 rcu_assign_pointer(nfsi->delegation, delegation);
219 /* Ensure we revalidate the attributes and page cache! */
220 spin_lock(&inode->i_lock);
221 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
222 spin_unlock(&inode->i_lock);
225 spin_unlock(&clp->cl_lock);
226 if (delegation != NULL)
227 nfs_free_delegation(delegation);
229 nfs_do_return_delegation(inode, freeme, 0);
233 /* Sync all data to disk upon delegation return */
234 static void nfs_msync_inode(struct inode *inode)
236 filemap_fdatawrite(inode->i_mapping);
238 filemap_fdatawait(inode->i_mapping);
242 * Basic procedure for returning a delegation to the server
244 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
246 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
247 struct nfs_inode *nfsi = NFS_I(inode);
249 nfs_msync_inode(inode);
250 down_read(&clp->cl_sem);
251 /* Guard against new delegated open calls */
252 down_write(&nfsi->rwsem);
253 nfs_delegation_claim_opens(inode, &delegation->stateid);
254 up_write(&nfsi->rwsem);
255 up_read(&clp->cl_sem);
256 nfs_msync_inode(inode);
258 return nfs_do_return_delegation(inode, delegation, 1);
262 * This function returns the delegation without reclaiming opens
263 * or protecting against delegation reclaims.
264 * It is therefore really only safe to be called from
267 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
269 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
270 struct nfs_inode *nfsi = NFS_I(inode);
271 struct nfs_delegation *delegation;
273 if (rcu_dereference(nfsi->delegation) != NULL) {
274 spin_lock(&clp->cl_lock);
275 delegation = nfs_detach_delegation_locked(nfsi, NULL);
276 spin_unlock(&clp->cl_lock);
277 if (delegation != NULL)
278 nfs_do_return_delegation(inode, delegation, 0);
282 int nfs_inode_return_delegation(struct inode *inode)
284 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
285 struct nfs_inode *nfsi = NFS_I(inode);
286 struct nfs_delegation *delegation;
289 if (rcu_dereference(nfsi->delegation) != NULL) {
290 spin_lock(&clp->cl_lock);
291 delegation = nfs_detach_delegation_locked(nfsi, NULL);
292 spin_unlock(&clp->cl_lock);
293 if (delegation != NULL)
294 err = __nfs_inode_return_delegation(inode, delegation);
300 * Return all delegations associated to a super block
302 void nfs_return_all_delegations(struct super_block *sb)
304 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
305 struct nfs_delegation *delegation;
312 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
314 spin_lock(&delegation->lock);
315 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
316 inode = igrab(delegation->inode);
317 spin_unlock(&delegation->lock);
320 spin_lock(&clp->cl_lock);
321 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
322 spin_unlock(&clp->cl_lock);
324 if (delegation != NULL)
325 __nfs_inode_return_delegation(inode, delegation);
332 static int nfs_do_expire_all_delegations(void *ptr)
334 struct nfs_client *clp = ptr;
335 struct nfs_delegation *delegation;
338 allow_signal(SIGKILL);
340 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
342 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
345 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
346 inode = nfs_delegation_grab_inode(delegation);
349 spin_lock(&clp->cl_lock);
350 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
351 spin_unlock(&clp->cl_lock);
354 __nfs_inode_return_delegation(inode, delegation);
361 module_put_and_exit(0);
364 void nfs_expire_all_delegations(struct nfs_client *clp)
366 struct task_struct *task;
368 __module_get(THIS_MODULE);
369 atomic_inc(&clp->cl_count);
370 task = kthread_run(nfs_do_expire_all_delegations, clp,
372 rpc_peeraddr2str(clp->cl_rpcclient,
377 module_put(THIS_MODULE);
381 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
383 void nfs_handle_cb_pathdown(struct nfs_client *clp)
385 struct nfs_delegation *delegation;
392 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
393 inode = nfs_delegation_grab_inode(delegation);
396 spin_lock(&clp->cl_lock);
397 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
398 spin_unlock(&clp->cl_lock);
400 if (delegation != NULL)
401 __nfs_inode_return_delegation(inode, delegation);
408 struct recall_threadargs {
410 struct nfs_client *clp;
411 const nfs4_stateid *stateid;
413 struct completion started;
417 static int recall_thread(void *data)
419 struct recall_threadargs *args = (struct recall_threadargs *)data;
420 struct inode *inode = igrab(args->inode);
421 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
422 struct nfs_inode *nfsi = NFS_I(inode);
423 struct nfs_delegation *delegation;
425 daemonize("nfsv4-delegreturn");
427 nfs_msync_inode(inode);
428 down_read(&clp->cl_sem);
429 down_write(&nfsi->rwsem);
430 spin_lock(&clp->cl_lock);
431 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
432 if (delegation != NULL)
435 args->result = -ENOENT;
436 spin_unlock(&clp->cl_lock);
437 complete(&args->started);
438 nfs_delegation_claim_opens(inode, args->stateid);
439 up_write(&nfsi->rwsem);
440 up_read(&clp->cl_sem);
441 nfs_msync_inode(inode);
443 if (delegation != NULL)
444 nfs_do_return_delegation(inode, delegation, 1);
446 module_put_and_exit(0);
450 * Asynchronous delegation recall!
452 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
454 struct recall_threadargs data = {
460 init_completion(&data.started);
461 __module_get(THIS_MODULE);
462 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
465 wait_for_completion(&data.started);
468 module_put(THIS_MODULE);
473 * Retrieve the inode associated with a delegation
475 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
477 struct nfs_delegation *delegation;
478 struct inode *res = NULL;
480 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
481 spin_lock(&delegation->lock);
482 if (delegation->inode != NULL &&
483 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
484 res = igrab(delegation->inode);
486 spin_unlock(&delegation->lock);
495 * Mark all delegations as needing to be reclaimed
497 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
499 struct nfs_delegation *delegation;
501 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
502 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
507 * Reap all unclaimed delegations after reboot recovery is done
509 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
511 struct nfs_delegation *delegation;
515 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
516 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
518 inode = nfs_delegation_grab_inode(delegation);
521 spin_lock(&clp->cl_lock);
522 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
523 spin_unlock(&clp->cl_lock);
525 if (delegation != NULL)
526 nfs_free_delegation(delegation);
533 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
535 struct nfs_inode *nfsi = NFS_I(inode);
536 struct nfs_delegation *delegation;
540 delegation = rcu_dereference(nfsi->delegation);
541 if (delegation != NULL) {
542 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));