blob: 44135af9894cc44bf733546d18513ce360c86fda [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/delegation.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFS file delegation management
7 *
8 */
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>
14
15#include <linux/nfs4.h>
16#include <linux/nfs_fs.h>
17#include <linux/nfs_xdr.h>
18
Trond Myklebust4ce79712005-06-22 17:16:21 +000019#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "delegation.h"
21
22static struct nfs_delegation *nfs_alloc_delegation(void)
23{
24 return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
25}
26
27static void nfs_free_delegation(struct nfs_delegation *delegation)
28{
29 if (delegation->cred)
30 put_rpccred(delegation->cred);
31 kfree(delegation);
32}
33
34static void nfs_delegation_claim_opens(struct inode *inode)
35{
36 struct nfs_inode *nfsi = NFS_I(inode);
37 struct nfs_open_context *ctx;
38 struct nfs4_state *state;
39
40again:
41 spin_lock(&inode->i_lock);
42 list_for_each_entry(ctx, &nfsi->open_files, list) {
43 state = ctx->state;
44 if (state == NULL)
45 continue;
46 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
47 continue;
48 get_nfs_open_context(ctx);
49 spin_unlock(&inode->i_lock);
50 if (nfs4_open_delegation_recall(ctx->dentry, state) < 0)
51 return;
52 put_nfs_open_context(ctx);
53 goto again;
54 }
55 spin_unlock(&inode->i_lock);
56}
57
58/*
59 * Set up a delegation on an inode
60 */
61void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
62{
63 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
64
65 if (delegation == NULL)
66 return;
67 memcpy(delegation->stateid.data, res->delegation.data,
68 sizeof(delegation->stateid.data));
69 delegation->type = res->delegation_type;
70 delegation->maxsize = res->maxsize;
71 put_rpccred(cred);
72 delegation->cred = get_rpccred(cred);
73 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
74 NFS_I(inode)->delegation_state = delegation->type;
75 smp_wmb();
76}
77
78/*
79 * Set up a delegation on an inode
80 */
81int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
82{
83 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
84 struct nfs_inode *nfsi = NFS_I(inode);
85 struct nfs_delegation *delegation;
86 int status = 0;
87
Trond Myklebustb3c52da2005-10-17 06:02:00 -040088 /* 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);
91
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 delegation = nfs_alloc_delegation();
93 if (delegation == NULL)
94 return -ENOMEM;
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;
101
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;
107 delegation = NULL;
108 } else {
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));
114 status = -EIO;
115 }
116 }
117 spin_unlock(&clp->cl_lock);
118 if (delegation != NULL)
119 kfree(delegation);
120 return status;
121}
122
123static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
124{
125 int res = 0;
126
127 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
128
129 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
130 nfs_free_delegation(delegation);
131 return res;
132}
133
134/* Sync all data to disk upon delegation return */
135static void nfs_msync_inode(struct inode *inode)
136{
137 filemap_fdatawrite(inode->i_mapping);
138 nfs_wb_all(inode);
139 filemap_fdatawait(inode->i_mapping);
140}
141
142/*
143 * Basic procedure for returning a delegation to the server
144 */
Trond Myklebustcae7a072005-10-18 14:20:19 -0700145int __nfs_inode_return_delegation(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146{
147 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
148 struct nfs_inode *nfsi = NFS_I(inode);
149 struct nfs_delegation *delegation;
150 int res = 0;
151
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;
162 }
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);
168
169 if (delegation != NULL)
170 res = nfs_do_return_delegation(inode, delegation);
171 return res;
172}
173
174/*
175 * Return all delegations associated to a super block
176 */
177void nfs_return_all_delegations(struct super_block *sb)
178{
179 struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
180 struct nfs_delegation *delegation;
181 struct inode *inode;
182
183 if (clp == NULL)
184 return;
185restart:
186 spin_lock(&clp->cl_lock);
187 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
188 if (delegation->inode->i_sb != sb)
189 continue;
190 inode = igrab(delegation->inode);
191 if (inode == NULL)
192 continue;
193 spin_unlock(&clp->cl_lock);
194 nfs_inode_return_delegation(inode);
195 iput(inode);
196 goto restart;
197 }
198 spin_unlock(&clp->cl_lock);
199}
200
201/*
202 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
203 */
204void nfs_handle_cb_pathdown(struct nfs4_client *clp)
205{
206 struct nfs_delegation *delegation;
207 struct inode *inode;
208
209 if (clp == NULL)
210 return;
211restart:
212 spin_lock(&clp->cl_lock);
213 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
214 inode = igrab(delegation->inode);
215 if (inode == NULL)
216 continue;
217 spin_unlock(&clp->cl_lock);
218 nfs_inode_return_delegation(inode);
219 iput(inode);
220 goto restart;
221 }
222 spin_unlock(&clp->cl_lock);
223}
224
225struct recall_threadargs {
226 struct inode *inode;
227 struct nfs4_client *clp;
228 const nfs4_stateid *stateid;
229
230 struct completion started;
231 int result;
232};
233
234static int recall_thread(void *data)
235{
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;
241
242 daemonize("nfsv4-delegreturn");
243
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,
250 args->stateid->data,
251 sizeof(delegation->stateid.data)) == 0) {
252 list_del_init(&delegation->super_list);
253 nfsi->delegation = NULL;
254 nfsi->delegation_state = 0;
255 args->result = 0;
256 } else {
257 delegation = NULL;
258 args->result = -ENOENT;
259 }
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);
266
267 if (delegation != NULL)
268 nfs_do_return_delegation(inode, delegation);
269 iput(inode);
270 module_put_and_exit(0);
271}
272
273/*
274 * Asynchronous delegation recall!
275 */
276int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
277{
278 struct recall_threadargs data = {
279 .inode = inode,
280 .stateid = stateid,
281 };
282 int status;
283
284 init_completion(&data.started);
285 __module_get(THIS_MODULE);
286 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
287 if (status < 0)
288 goto out_module_put;
289 wait_for_completion(&data.started);
290 return data.result;
291out_module_put:
292 module_put(THIS_MODULE);
293 return status;
294}
295
296/*
297 * Retrieve the inode associated with a delegation
298 */
299struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
300{
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);
307 break;
308 }
309 }
310 spin_unlock(&clp->cl_lock);
311 return res;
312}
313
314/*
315 * Mark all delegations as needing to be reclaimed
316 */
317void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
318{
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);
324}
325
326/*
327 * Reap all unclaimed delegations after reboot recovery is done
328 */
329void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
330{
331 struct nfs_delegation *delegation, *n;
332 LIST_HEAD(head);
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)
336 continue;
337 list_move(&delegation->super_list, &head);
338 NFS_I(delegation->inode)->delegation = NULL;
339 NFS_I(delegation->inode)->delegation_state = 0;
340 }
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);
346 }
347}