NFSv4: Move error handling out of the delegation generic code
[linux-2.6/mini2440.git] / fs / nfs / delegation.c
blobd4f669f0683efdde7d0bf626919bceda01354bfc
1 /*
2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
8 */
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>
19 #include "nfs4_fs.h"
20 #include "delegation.h"
21 #include "internal.h"
23 static void nfs_do_free_delegation(struct nfs_delegation *delegation)
25 kfree(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);
42 if (cred)
43 put_rpccred(cred);
46 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
48 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
51 int nfs_have_delegation(struct inode *inode, fmode_t flags)
53 struct nfs_delegation *delegation;
54 int ret = 0;
56 flags &= FMODE_READ|FMODE_WRITE;
57 rcu_read_lock();
58 delegation = rcu_dereference(NFS_I(inode)->delegation);
59 if (delegation != NULL && (delegation->type & flags) == flags) {
60 nfs_mark_delegation_referenced(delegation);
61 ret = 1;
63 rcu_read_unlock();
64 return ret;
67 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
69 struct inode *inode = state->inode;
70 struct file_lock *fl;
71 int status = 0;
73 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
74 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
75 continue;
76 if (nfs_file_open_context(fl->fl_file) != ctx)
77 continue;
78 status = nfs4_lock_delegation_recall(state, fl);
79 if (status < 0)
80 break;
82 return status;
85 static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
87 struct nfs_inode *nfsi = NFS_I(inode);
88 struct nfs_open_context *ctx;
89 struct nfs4_state *state;
90 int err;
92 again:
93 spin_lock(&inode->i_lock);
94 list_for_each_entry(ctx, &nfsi->open_files, list) {
95 state = ctx->state;
96 if (state == NULL)
97 continue;
98 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
99 continue;
100 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
101 continue;
102 get_nfs_open_context(ctx);
103 spin_unlock(&inode->i_lock);
104 err = nfs4_open_delegation_recall(ctx, state, stateid);
105 if (err >= 0)
106 err = nfs_delegation_claim_locks(ctx, state);
107 put_nfs_open_context(ctx);
108 if (err != 0)
109 return;
110 goto again;
112 spin_unlock(&inode->i_lock);
116 * Set up a delegation on an inode
118 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
120 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
121 struct rpc_cred *oldcred;
123 if (delegation == NULL)
124 return;
125 memcpy(delegation->stateid.data, res->delegation.data,
126 sizeof(delegation->stateid.data));
127 delegation->type = res->delegation_type;
128 delegation->maxsize = res->maxsize;
129 oldcred = delegation->cred;
130 delegation->cred = get_rpccred(cred);
131 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
132 NFS_I(inode)->delegation_state = delegation->type;
133 smp_wmb();
134 put_rpccred(oldcred);
137 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
139 int res = 0;
141 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
142 nfs_free_delegation(delegation);
143 return res;
146 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
148 struct inode *inode = NULL;
150 spin_lock(&delegation->lock);
151 if (delegation->inode != NULL)
152 inode = igrab(delegation->inode);
153 spin_unlock(&delegation->lock);
154 return inode;
157 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
159 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
161 if (delegation == NULL)
162 goto nomatch;
163 spin_lock(&delegation->lock);
164 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
165 sizeof(delegation->stateid.data)) != 0)
166 goto nomatch_unlock;
167 list_del_rcu(&delegation->super_list);
168 delegation->inode = NULL;
169 nfsi->delegation_state = 0;
170 rcu_assign_pointer(nfsi->delegation, NULL);
171 spin_unlock(&delegation->lock);
172 return delegation;
173 nomatch_unlock:
174 spin_unlock(&delegation->lock);
175 nomatch:
176 return NULL;
180 * Set up a delegation on an inode
182 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
184 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
185 struct nfs_inode *nfsi = NFS_I(inode);
186 struct nfs_delegation *delegation;
187 struct nfs_delegation *freeme = NULL;
188 int status = 0;
190 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
191 if (delegation == NULL)
192 return -ENOMEM;
193 memcpy(delegation->stateid.data, res->delegation.data,
194 sizeof(delegation->stateid.data));
195 delegation->type = res->delegation_type;
196 delegation->maxsize = res->maxsize;
197 delegation->change_attr = nfsi->change_attr;
198 delegation->cred = get_rpccred(cred);
199 delegation->inode = inode;
200 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
201 spin_lock_init(&delegation->lock);
203 spin_lock(&clp->cl_lock);
204 if (rcu_dereference(nfsi->delegation) != NULL) {
205 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
206 sizeof(delegation->stateid)) == 0 &&
207 delegation->type == nfsi->delegation->type) {
208 goto out;
211 * Deal with broken servers that hand out two
212 * delegations for the same file.
214 dfprintk(FILE, "%s: server %s handed out "
215 "a duplicate delegation!\n",
216 __func__, clp->cl_hostname);
217 if (delegation->type <= nfsi->delegation->type) {
218 freeme = delegation;
219 delegation = NULL;
220 goto out;
222 freeme = nfs_detach_delegation_locked(nfsi, NULL);
224 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
225 nfsi->delegation_state = delegation->type;
226 rcu_assign_pointer(nfsi->delegation, delegation);
227 delegation = NULL;
229 /* Ensure we revalidate the attributes and page cache! */
230 spin_lock(&inode->i_lock);
231 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
232 spin_unlock(&inode->i_lock);
234 out:
235 spin_unlock(&clp->cl_lock);
236 if (delegation != NULL)
237 nfs_free_delegation(delegation);
238 if (freeme != NULL)
239 nfs_do_return_delegation(inode, freeme, 0);
240 return status;
243 /* Sync all data to disk upon delegation return */
244 static void nfs_msync_inode(struct inode *inode)
246 filemap_fdatawrite(inode->i_mapping);
247 nfs_wb_all(inode);
248 filemap_fdatawait(inode->i_mapping);
252 * Basic procedure for returning a delegation to the server
254 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
256 struct nfs_inode *nfsi = NFS_I(inode);
258 nfs_msync_inode(inode);
259 /* Guard against new delegated open calls */
260 down_write(&nfsi->rwsem);
261 nfs_delegation_claim_opens(inode, &delegation->stateid);
262 up_write(&nfsi->rwsem);
263 nfs_msync_inode(inode);
265 return nfs_do_return_delegation(inode, delegation, 1);
269 * Return all delegations that have been marked for return
271 void nfs_client_return_marked_delegations(struct nfs_client *clp)
273 struct nfs_delegation *delegation;
274 struct inode *inode;
276 restart:
277 rcu_read_lock();
278 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
279 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
280 continue;
281 inode = nfs_delegation_grab_inode(delegation);
282 if (inode == NULL)
283 continue;
284 spin_lock(&clp->cl_lock);
285 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
286 spin_unlock(&clp->cl_lock);
287 rcu_read_unlock();
288 if (delegation != NULL)
289 __nfs_inode_return_delegation(inode, delegation);
290 iput(inode);
291 goto restart;
293 rcu_read_unlock();
297 * This function returns the delegation without reclaiming opens
298 * or protecting against delegation reclaims.
299 * It is therefore really only safe to be called from
300 * nfs4_clear_inode()
302 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
304 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
305 struct nfs_inode *nfsi = NFS_I(inode);
306 struct nfs_delegation *delegation;
308 if (rcu_dereference(nfsi->delegation) != NULL) {
309 spin_lock(&clp->cl_lock);
310 delegation = nfs_detach_delegation_locked(nfsi, NULL);
311 spin_unlock(&clp->cl_lock);
312 if (delegation != NULL)
313 nfs_do_return_delegation(inode, delegation, 0);
317 int nfs_inode_return_delegation(struct inode *inode)
319 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
320 struct nfs_inode *nfsi = NFS_I(inode);
321 struct nfs_delegation *delegation;
322 int err = 0;
324 if (rcu_dereference(nfsi->delegation) != NULL) {
325 spin_lock(&clp->cl_lock);
326 delegation = nfs_detach_delegation_locked(nfsi, NULL);
327 spin_unlock(&clp->cl_lock);
328 if (delegation != NULL)
329 err = __nfs_inode_return_delegation(inode, delegation);
331 return err;
334 static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
336 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
337 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
341 * Return all delegations associated to a super block
343 void nfs_super_return_all_delegations(struct super_block *sb)
345 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
346 struct nfs_delegation *delegation;
348 if (clp == NULL)
349 return;
350 rcu_read_lock();
351 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
352 spin_lock(&delegation->lock);
353 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
354 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
355 spin_unlock(&delegation->lock);
357 rcu_read_unlock();
358 nfs_client_return_marked_delegations(clp);
361 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
363 struct nfs_delegation *delegation;
365 rcu_read_lock();
366 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
367 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
368 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
370 rcu_read_unlock();
373 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
375 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
376 nfs4_schedule_state_manager(clp);
379 void nfs_expire_all_delegations(struct nfs_client *clp)
381 nfs_client_mark_return_all_delegations(clp);
382 nfs_delegation_run_state_manager(clp);
386 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
388 void nfs_handle_cb_pathdown(struct nfs_client *clp)
390 if (clp == NULL)
391 return;
392 nfs_client_mark_return_all_delegations(clp);
395 static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
397 struct nfs_delegation *delegation;
399 rcu_read_lock();
400 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
401 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
402 continue;
403 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
404 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
406 rcu_read_unlock();
409 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
411 nfs_client_mark_return_unreferenced_delegations(clp);
412 nfs_delegation_run_state_manager(clp);
416 * Asynchronous delegation recall!
418 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
420 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
421 struct nfs_delegation *delegation;
423 rcu_read_lock();
424 delegation = rcu_dereference(NFS_I(inode)->delegation);
425 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
426 sizeof(delegation->stateid.data)) != 0) {
427 rcu_read_unlock();
428 return -ENOENT;
430 nfs_mark_return_delegation(clp, delegation);
431 rcu_read_unlock();
432 nfs_delegation_run_state_manager(clp);
433 return 0;
437 * Retrieve the inode associated with a delegation
439 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
441 struct nfs_delegation *delegation;
442 struct inode *res = NULL;
443 rcu_read_lock();
444 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
445 spin_lock(&delegation->lock);
446 if (delegation->inode != NULL &&
447 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
448 res = igrab(delegation->inode);
450 spin_unlock(&delegation->lock);
451 if (res != NULL)
452 break;
454 rcu_read_unlock();
455 return res;
459 * Mark all delegations as needing to be reclaimed
461 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
463 struct nfs_delegation *delegation;
464 rcu_read_lock();
465 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
466 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
467 rcu_read_unlock();
471 * Reap all unclaimed delegations after reboot recovery is done
473 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
475 struct nfs_delegation *delegation;
476 struct inode *inode;
477 restart:
478 rcu_read_lock();
479 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
480 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
481 continue;
482 inode = nfs_delegation_grab_inode(delegation);
483 if (inode == NULL)
484 continue;
485 spin_lock(&clp->cl_lock);
486 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
487 spin_unlock(&clp->cl_lock);
488 rcu_read_unlock();
489 if (delegation != NULL)
490 nfs_free_delegation(delegation);
491 iput(inode);
492 goto restart;
494 rcu_read_unlock();
497 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
499 struct nfs_inode *nfsi = NFS_I(inode);
500 struct nfs_delegation *delegation;
501 int ret = 0;
503 rcu_read_lock();
504 delegation = rcu_dereference(nfsi->delegation);
505 if (delegation != NULL) {
506 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
507 ret = 1;
509 rcu_read_unlock();
510 return ret;