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_free_delegation(struct nfs_delegation *delegation)
26 put_rpccred(delegation->cred);
30 static void nfs_free_delegation_callback(struct rcu_head *head)
32 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
34 nfs_free_delegation(delegation);
37 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
39 struct inode *inode = state->inode;
43 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
44 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
46 if ((struct nfs_open_context *)fl->fl_file->private_data != ctx)
48 status = nfs4_lock_delegation_recall(state, fl);
53 printk(KERN_ERR "%s: unhandled error %d.\n",
54 __FUNCTION__, status);
55 case -NFS4ERR_EXPIRED:
56 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
57 case -NFS4ERR_STALE_CLIENTID:
58 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
67 static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
69 struct nfs_inode *nfsi = NFS_I(inode);
70 struct nfs_open_context *ctx;
71 struct nfs4_state *state;
75 spin_lock(&inode->i_lock);
76 list_for_each_entry(ctx, &nfsi->open_files, list) {
80 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
82 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
84 get_nfs_open_context(ctx);
85 spin_unlock(&inode->i_lock);
86 err = nfs4_open_delegation_recall(ctx, state, stateid);
88 err = nfs_delegation_claim_locks(ctx, state);
89 put_nfs_open_context(ctx);
94 spin_unlock(&inode->i_lock);
98 * Set up a delegation on an inode
100 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
102 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
104 if (delegation == NULL)
106 memcpy(delegation->stateid.data, res->delegation.data,
107 sizeof(delegation->stateid.data));
108 delegation->type = res->delegation_type;
109 delegation->maxsize = res->maxsize;
111 delegation->cred = get_rpccred(cred);
112 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
113 NFS_I(inode)->delegation_state = delegation->type;
118 * Set up a delegation on an inode
120 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
122 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
123 struct nfs_inode *nfsi = NFS_I(inode);
124 struct nfs_delegation *delegation;
127 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
128 if (delegation == NULL)
130 memcpy(delegation->stateid.data, res->delegation.data,
131 sizeof(delegation->stateid.data));
132 delegation->type = res->delegation_type;
133 delegation->maxsize = res->maxsize;
134 delegation->change_attr = nfsi->change_attr;
135 delegation->cred = get_rpccred(cred);
136 delegation->inode = inode;
138 spin_lock(&clp->cl_lock);
139 if (rcu_dereference(nfsi->delegation) == NULL) {
140 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
141 nfsi->delegation_state = delegation->type;
142 rcu_assign_pointer(nfsi->delegation, delegation);
145 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
146 sizeof(delegation->stateid)) != 0 ||
147 delegation->type != nfsi->delegation->type) {
148 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
149 __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
154 /* Ensure we revalidate the attributes and page cache! */
155 spin_lock(&inode->i_lock);
156 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
157 spin_unlock(&inode->i_lock);
159 spin_unlock(&clp->cl_lock);
164 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
168 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
169 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
173 /* Sync all data to disk upon delegation return */
174 static void nfs_msync_inode(struct inode *inode)
176 filemap_fdatawrite(inode->i_mapping);
178 filemap_fdatawait(inode->i_mapping);
182 * Basic procedure for returning a delegation to the server
184 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
186 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
187 struct nfs_inode *nfsi = NFS_I(inode);
189 nfs_msync_inode(inode);
190 down_read(&clp->cl_sem);
191 /* Guard against new delegated open calls */
192 down_write(&nfsi->rwsem);
193 nfs_delegation_claim_opens(inode, &delegation->stateid);
194 up_write(&nfsi->rwsem);
195 up_read(&clp->cl_sem);
196 nfs_msync_inode(inode);
198 return nfs_do_return_delegation(inode, delegation);
201 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
203 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
205 if (delegation == NULL)
207 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
208 sizeof(delegation->stateid.data)) != 0)
210 list_del_rcu(&delegation->super_list);
211 nfsi->delegation_state = 0;
212 rcu_assign_pointer(nfsi->delegation, NULL);
218 int nfs_inode_return_delegation(struct inode *inode)
220 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
221 struct nfs_inode *nfsi = NFS_I(inode);
222 struct nfs_delegation *delegation;
225 if (rcu_dereference(nfsi->delegation) != NULL) {
226 spin_lock(&clp->cl_lock);
227 delegation = nfs_detach_delegation_locked(nfsi, NULL);
228 spin_unlock(&clp->cl_lock);
229 if (delegation != NULL)
230 err = __nfs_inode_return_delegation(inode, delegation);
236 * Return all delegations associated to a super block
238 void nfs_return_all_delegations(struct super_block *sb)
240 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
241 struct nfs_delegation *delegation;
248 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
249 if (delegation->inode->i_sb != sb)
251 inode = igrab(delegation->inode);
254 spin_lock(&clp->cl_lock);
255 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
256 spin_unlock(&clp->cl_lock);
258 if (delegation != NULL)
259 __nfs_inode_return_delegation(inode, delegation);
266 static int nfs_do_expire_all_delegations(void *ptr)
268 struct nfs_client *clp = ptr;
269 struct nfs_delegation *delegation;
272 allow_signal(SIGKILL);
274 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
276 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
279 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
280 inode = igrab(delegation->inode);
283 spin_lock(&clp->cl_lock);
284 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
285 spin_unlock(&clp->cl_lock);
288 __nfs_inode_return_delegation(inode, delegation);
295 module_put_and_exit(0);
298 void nfs_expire_all_delegations(struct nfs_client *clp)
300 struct task_struct *task;
302 __module_get(THIS_MODULE);
303 atomic_inc(&clp->cl_count);
304 task = kthread_run(nfs_do_expire_all_delegations, clp,
305 "%u.%u.%u.%u-delegreturn",
306 NIPQUAD(clp->cl_addr.sin_addr));
310 module_put(THIS_MODULE);
314 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
316 void nfs_handle_cb_pathdown(struct nfs_client *clp)
318 struct nfs_delegation *delegation;
325 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
326 inode = igrab(delegation->inode);
329 spin_lock(&clp->cl_lock);
330 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
331 spin_unlock(&clp->cl_lock);
333 if (delegation != NULL)
334 __nfs_inode_return_delegation(inode, delegation);
341 struct recall_threadargs {
343 struct nfs_client *clp;
344 const nfs4_stateid *stateid;
346 struct completion started;
350 static int recall_thread(void *data)
352 struct recall_threadargs *args = (struct recall_threadargs *)data;
353 struct inode *inode = igrab(args->inode);
354 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
355 struct nfs_inode *nfsi = NFS_I(inode);
356 struct nfs_delegation *delegation;
358 daemonize("nfsv4-delegreturn");
360 nfs_msync_inode(inode);
361 down_read(&clp->cl_sem);
362 down_write(&nfsi->rwsem);
363 spin_lock(&clp->cl_lock);
364 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
365 if (delegation != NULL)
368 args->result = -ENOENT;
369 spin_unlock(&clp->cl_lock);
370 complete(&args->started);
371 nfs_delegation_claim_opens(inode, args->stateid);
372 up_write(&nfsi->rwsem);
373 up_read(&clp->cl_sem);
374 nfs_msync_inode(inode);
376 if (delegation != NULL)
377 nfs_do_return_delegation(inode, delegation);
379 module_put_and_exit(0);
383 * Asynchronous delegation recall!
385 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
387 struct recall_threadargs data = {
393 init_completion(&data.started);
394 __module_get(THIS_MODULE);
395 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
398 wait_for_completion(&data.started);
401 module_put(THIS_MODULE);
406 * Retrieve the inode associated with a delegation
408 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
410 struct nfs_delegation *delegation;
411 struct inode *res = NULL;
413 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
414 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
415 res = igrab(delegation->inode);
424 * Mark all delegations as needing to be reclaimed
426 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
428 struct nfs_delegation *delegation;
430 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
431 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
436 * Reap all unclaimed delegations after reboot recovery is done
438 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
440 struct nfs_delegation *delegation;
443 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
444 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
446 spin_lock(&clp->cl_lock);
447 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
448 spin_unlock(&clp->cl_lock);
450 if (delegation != NULL)
451 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
457 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
459 struct nfs_inode *nfsi = NFS_I(inode);
460 struct nfs_delegation *delegation;
464 delegation = rcu_dereference(nfsi->delegation);
465 if (delegation != NULL) {
466 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));