2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
9 #include <linux/config.h>
10 #include <linux/completion.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"
22 static struct nfs_delegation *nfs_alloc_delegation(void)
24 return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
27 static void nfs_free_delegation(struct nfs_delegation *delegation)
30 put_rpccred(delegation->cred);
34 static void nfs_delegation_claim_opens(struct inode *inode)
36 struct nfs_inode *nfsi = NFS_I(inode);
37 struct nfs_open_context *ctx;
38 struct nfs4_state *state;
41 spin_lock(&inode->i_lock);
42 list_for_each_entry(ctx, &nfsi->open_files, list) {
46 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
48 get_nfs_open_context(ctx);
49 spin_unlock(&inode->i_lock);
50 if (nfs4_open_delegation_recall(ctx->dentry, state) < 0)
52 put_nfs_open_context(ctx);
55 spin_unlock(&inode->i_lock);
59 * Set up a delegation on an inode
61 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
63 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
65 if (delegation == NULL)
67 memcpy(delegation->stateid.data, res->delegation.data,
68 sizeof(delegation->stateid.data));
69 delegation->type = res->delegation_type;
70 delegation->maxsize = res->maxsize;
72 delegation->cred = get_rpccred(cred);
73 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
74 NFS_I(inode)->delegation_state = delegation->type;
79 * Set up a delegation on an inode
81 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
83 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
84 struct nfs_inode *nfsi = NFS_I(inode);
85 struct nfs_delegation *delegation;
88 /* Ensure we first revalidate the attributes and page cache! */
89 if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
90 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
92 delegation = nfs_alloc_delegation();
93 if (delegation == NULL)
95 memcpy(delegation->stateid.data, res->delegation.data,
96 sizeof(delegation->stateid.data));
97 delegation->type = res->delegation_type;
98 delegation->maxsize = res->maxsize;
99 delegation->cred = get_rpccred(cred);
100 delegation->inode = inode;
102 spin_lock(&clp->cl_lock);
103 if (nfsi->delegation == NULL) {
104 list_add(&delegation->super_list, &clp->cl_delegations);
105 nfsi->delegation = delegation;
106 nfsi->delegation_state = delegation->type;
109 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
110 sizeof(delegation->stateid)) != 0 ||
111 delegation->type != nfsi->delegation->type) {
112 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
113 __FUNCTION__, NIPQUAD(clp->cl_addr));
117 spin_unlock(&clp->cl_lock);
118 if (delegation != NULL)
123 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
127 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
129 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
130 nfs_free_delegation(delegation);
134 /* Sync all data to disk upon delegation return */
135 static void nfs_msync_inode(struct inode *inode)
137 filemap_fdatawrite(inode->i_mapping);
139 filemap_fdatawait(inode->i_mapping);
143 * Basic procedure for returning a delegation to the server
145 int __nfs_inode_return_delegation(struct inode *inode)
147 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
148 struct nfs_inode *nfsi = NFS_I(inode);
149 struct nfs_delegation *delegation;
152 nfs_msync_inode(inode);
153 down_read(&clp->cl_sem);
154 /* Guard against new delegated open calls */
155 down_write(&nfsi->rwsem);
156 spin_lock(&clp->cl_lock);
157 delegation = nfsi->delegation;
158 if (delegation != NULL) {
159 list_del_init(&delegation->super_list);
160 nfsi->delegation = NULL;
161 nfsi->delegation_state = 0;
163 spin_unlock(&clp->cl_lock);
164 nfs_delegation_claim_opens(inode);
165 up_write(&nfsi->rwsem);
166 up_read(&clp->cl_sem);
167 nfs_msync_inode(inode);
169 if (delegation != NULL)
170 res = nfs_do_return_delegation(inode, delegation);
175 * Return all delegations associated to a super block
177 void nfs_return_all_delegations(struct super_block *sb)
179 struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
180 struct nfs_delegation *delegation;
186 spin_lock(&clp->cl_lock);
187 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
188 if (delegation->inode->i_sb != sb)
190 inode = igrab(delegation->inode);
193 spin_unlock(&clp->cl_lock);
194 nfs_inode_return_delegation(inode);
198 spin_unlock(&clp->cl_lock);
202 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
204 void nfs_handle_cb_pathdown(struct nfs4_client *clp)
206 struct nfs_delegation *delegation;
212 spin_lock(&clp->cl_lock);
213 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
214 inode = igrab(delegation->inode);
217 spin_unlock(&clp->cl_lock);
218 nfs_inode_return_delegation(inode);
222 spin_unlock(&clp->cl_lock);
225 struct recall_threadargs {
227 struct nfs4_client *clp;
228 const nfs4_stateid *stateid;
230 struct completion started;
234 static int recall_thread(void *data)
236 struct recall_threadargs *args = (struct recall_threadargs *)data;
237 struct inode *inode = igrab(args->inode);
238 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
239 struct nfs_inode *nfsi = NFS_I(inode);
240 struct nfs_delegation *delegation;
242 daemonize("nfsv4-delegreturn");
244 nfs_msync_inode(inode);
245 down_read(&clp->cl_sem);
246 down_write(&nfsi->rwsem);
247 spin_lock(&clp->cl_lock);
248 delegation = nfsi->delegation;
249 if (delegation != NULL && memcmp(delegation->stateid.data,
251 sizeof(delegation->stateid.data)) == 0) {
252 list_del_init(&delegation->super_list);
253 nfsi->delegation = NULL;
254 nfsi->delegation_state = 0;
258 args->result = -ENOENT;
260 spin_unlock(&clp->cl_lock);
261 complete(&args->started);
262 nfs_delegation_claim_opens(inode);
263 up_write(&nfsi->rwsem);
264 up_read(&clp->cl_sem);
265 nfs_msync_inode(inode);
267 if (delegation != NULL)
268 nfs_do_return_delegation(inode, delegation);
270 module_put_and_exit(0);
274 * Asynchronous delegation recall!
276 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
278 struct recall_threadargs data = {
284 init_completion(&data.started);
285 __module_get(THIS_MODULE);
286 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
289 wait_for_completion(&data.started);
292 module_put(THIS_MODULE);
297 * Retrieve the inode associated with a delegation
299 struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
301 struct nfs_delegation *delegation;
302 struct inode *res = NULL;
303 spin_lock(&clp->cl_lock);
304 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
305 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
306 res = igrab(delegation->inode);
310 spin_unlock(&clp->cl_lock);
315 * Mark all delegations as needing to be reclaimed
317 void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
319 struct nfs_delegation *delegation;
320 spin_lock(&clp->cl_lock);
321 list_for_each_entry(delegation, &clp->cl_delegations, super_list)
322 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
323 spin_unlock(&clp->cl_lock);
327 * Reap all unclaimed delegations after reboot recovery is done
329 void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
331 struct nfs_delegation *delegation, *n;
333 spin_lock(&clp->cl_lock);
334 list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
335 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
337 list_move(&delegation->super_list, &head);
338 NFS_I(delegation->inode)->delegation = NULL;
339 NFS_I(delegation->inode)->delegation_state = 0;
341 spin_unlock(&clp->cl_lock);
342 while(!list_empty(&head)) {
343 delegation = list_entry(head.next, struct nfs_delegation, super_list);
344 list_del(&delegation->super_list);
345 nfs_free_delegation(delegation);