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/slab.h>
14 #include <linux/smp_lock.h>
15 #include <linux/spinlock.h>
17 #include <linux/nfs4.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_xdr.h>
22 #include "delegation.h"
25 static void nfs_do_free_delegation(struct nfs_delegation *delegation)
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_do_free_delegation(delegation);
37 static void nfs_free_delegation(struct nfs_delegation *delegation)
39 struct rpc_cred *cred;
41 cred = rcu_dereference(delegation->cred);
42 rcu_assign_pointer(delegation->cred, NULL);
43 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
48 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
50 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
53 int nfs_have_delegation(struct inode *inode, fmode_t flags)
55 struct nfs_delegation *delegation;
58 flags &= FMODE_READ|FMODE_WRITE;
60 delegation = rcu_dereference(NFS_I(inode)->delegation);
61 if (delegation != NULL && (delegation->type & flags) == flags) {
62 nfs_mark_delegation_referenced(delegation);
69 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
71 struct inode *inode = state->inode;
75 if (inode->i_flock == NULL)
78 /* Protect inode->i_flock using the BKL */
80 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
81 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
83 if (nfs_file_open_context(fl->fl_file) != ctx)
86 status = nfs4_lock_delegation_recall(state, fl);
96 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
98 struct nfs_inode *nfsi = NFS_I(inode);
99 struct nfs_open_context *ctx;
100 struct nfs4_state *state;
104 spin_lock(&inode->i_lock);
105 list_for_each_entry(ctx, &nfsi->open_files, list) {
109 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
111 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
113 get_nfs_open_context(ctx);
114 spin_unlock(&inode->i_lock);
115 err = nfs4_open_delegation_recall(ctx, state, stateid);
117 err = nfs_delegation_claim_locks(ctx, state);
118 put_nfs_open_context(ctx);
123 spin_unlock(&inode->i_lock);
128 * Set up a delegation on an inode
130 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
132 struct nfs_delegation *delegation;
133 struct rpc_cred *oldcred = NULL;
136 delegation = rcu_dereference(NFS_I(inode)->delegation);
137 if (delegation != NULL) {
138 spin_lock(&delegation->lock);
139 if (delegation->inode != NULL) {
140 memcpy(delegation->stateid.data, res->delegation.data,
141 sizeof(delegation->stateid.data));
142 delegation->type = res->delegation_type;
143 delegation->maxsize = res->maxsize;
144 oldcred = delegation->cred;
145 delegation->cred = get_rpccred(cred);
146 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
148 NFS_I(inode)->delegation_state = delegation->type;
149 spin_unlock(&delegation->lock);
150 put_rpccred(oldcred);
153 /* We appear to have raced with a delegation return. */
154 spin_unlock(&delegation->lock);
156 nfs_inode_set_delegation(inode, cred, res);
163 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
167 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
168 nfs_free_delegation(delegation);
172 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
174 struct inode *inode = NULL;
176 spin_lock(&delegation->lock);
177 if (delegation->inode != NULL)
178 inode = igrab(delegation->inode);
179 spin_unlock(&delegation->lock);
183 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
185 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
187 if (delegation == NULL)
189 spin_lock(&delegation->lock);
190 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
191 sizeof(delegation->stateid.data)) != 0)
193 list_del_rcu(&delegation->super_list);
194 delegation->inode = NULL;
195 nfsi->delegation_state = 0;
196 rcu_assign_pointer(nfsi->delegation, NULL);
197 spin_unlock(&delegation->lock);
200 spin_unlock(&delegation->lock);
206 * Set up a delegation on an inode
208 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
210 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
211 struct nfs_inode *nfsi = NFS_I(inode);
212 struct nfs_delegation *delegation;
213 struct nfs_delegation *freeme = NULL;
216 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
217 if (delegation == NULL)
219 memcpy(delegation->stateid.data, res->delegation.data,
220 sizeof(delegation->stateid.data));
221 delegation->type = res->delegation_type;
222 delegation->maxsize = res->maxsize;
223 delegation->change_attr = nfsi->change_attr;
224 delegation->cred = get_rpccred(cred);
225 delegation->inode = inode;
226 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
227 spin_lock_init(&delegation->lock);
229 spin_lock(&clp->cl_lock);
230 if (rcu_dereference(nfsi->delegation) != NULL) {
231 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
232 sizeof(delegation->stateid)) == 0 &&
233 delegation->type == nfsi->delegation->type) {
237 * Deal with broken servers that hand out two
238 * delegations for the same file.
240 dfprintk(FILE, "%s: server %s handed out "
241 "a duplicate delegation!\n",
242 __func__, clp->cl_hostname);
243 if (delegation->type <= nfsi->delegation->type) {
248 freeme = nfs_detach_delegation_locked(nfsi, NULL);
250 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
251 nfsi->delegation_state = delegation->type;
252 rcu_assign_pointer(nfsi->delegation, delegation);
255 /* Ensure we revalidate the attributes and page cache! */
256 spin_lock(&inode->i_lock);
257 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
258 spin_unlock(&inode->i_lock);
261 spin_unlock(&clp->cl_lock);
262 if (delegation != NULL)
263 nfs_free_delegation(delegation);
265 nfs_do_return_delegation(inode, freeme, 0);
269 /* Sync all data to disk upon delegation return */
270 static void nfs_msync_inode(struct inode *inode)
272 filemap_fdatawrite(inode->i_mapping);
274 filemap_fdatawait(inode->i_mapping);
278 * Basic procedure for returning a delegation to the server
280 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
282 struct nfs_inode *nfsi = NFS_I(inode);
286 * Guard against new delegated open/lock/unlock calls and against
289 down_write(&nfsi->rwsem);
290 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
291 up_write(&nfsi->rwsem);
295 err = nfs_do_return_delegation(inode, delegation, issync);
301 * Return all delegations that have been marked for return
303 int nfs_client_return_marked_delegations(struct nfs_client *clp)
305 struct nfs_delegation *delegation;
311 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
312 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
314 inode = nfs_delegation_grab_inode(delegation);
317 spin_lock(&clp->cl_lock);
318 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
319 spin_unlock(&clp->cl_lock);
321 if (delegation != NULL) {
322 filemap_flush(inode->i_mapping);
323 err = __nfs_inode_return_delegation(inode, delegation, 0);
328 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
336 * This function returns the delegation without reclaiming opens
337 * or protecting against delegation reclaims.
338 * It is therefore really only safe to be called from
341 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
343 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
344 struct nfs_inode *nfsi = NFS_I(inode);
345 struct nfs_delegation *delegation;
347 if (rcu_dereference(nfsi->delegation) != NULL) {
348 spin_lock(&clp->cl_lock);
349 delegation = nfs_detach_delegation_locked(nfsi, NULL);
350 spin_unlock(&clp->cl_lock);
351 if (delegation != NULL)
352 nfs_do_return_delegation(inode, delegation, 0);
356 int nfs_inode_return_delegation(struct inode *inode)
358 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
359 struct nfs_inode *nfsi = NFS_I(inode);
360 struct nfs_delegation *delegation;
363 if (rcu_dereference(nfsi->delegation) != NULL) {
364 spin_lock(&clp->cl_lock);
365 delegation = nfs_detach_delegation_locked(nfsi, NULL);
366 spin_unlock(&clp->cl_lock);
367 if (delegation != NULL) {
368 nfs_msync_inode(inode);
369 err = __nfs_inode_return_delegation(inode, delegation, 1);
375 static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
377 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
378 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
382 * Return all delegations associated to a super block
384 void nfs_super_return_all_delegations(struct super_block *sb)
386 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
387 struct nfs_delegation *delegation;
392 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
393 spin_lock(&delegation->lock);
394 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
395 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
396 spin_unlock(&delegation->lock);
399 if (nfs_client_return_marked_delegations(clp) != 0)
400 nfs4_schedule_state_manager(clp);
404 void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
406 struct nfs_delegation *delegation;
409 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
410 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
412 if (delegation->type & flags)
413 nfs_mark_return_delegation(clp, delegation);
418 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
420 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
423 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
425 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
426 nfs4_schedule_state_manager(clp);
429 void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
431 nfs_client_mark_return_all_delegation_types(clp, flags);
432 nfs_delegation_run_state_manager(clp);
435 void nfs_expire_all_delegations(struct nfs_client *clp)
437 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
441 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
443 void nfs_handle_cb_pathdown(struct nfs_client *clp)
447 nfs_client_mark_return_all_delegations(clp);
450 static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
452 struct nfs_delegation *delegation;
455 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
456 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
458 nfs_mark_return_delegation(clp, delegation);
463 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
465 nfs_client_mark_return_unreferenced_delegations(clp);
466 nfs_delegation_run_state_manager(clp);
470 * Asynchronous delegation recall!
472 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid,
473 int (*validate_stateid)(struct nfs_delegation *delegation,
474 const nfs4_stateid *stateid))
476 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
477 struct nfs_delegation *delegation;
480 delegation = rcu_dereference(NFS_I(inode)->delegation);
482 if (!validate_stateid(delegation, stateid)) {
487 nfs_mark_return_delegation(clp, delegation);
489 nfs_delegation_run_state_manager(clp);
494 * Retrieve the inode associated with a delegation
496 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
498 struct nfs_delegation *delegation;
499 struct inode *res = NULL;
501 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
502 spin_lock(&delegation->lock);
503 if (delegation->inode != NULL &&
504 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
505 res = igrab(delegation->inode);
507 spin_unlock(&delegation->lock);
516 * Mark all delegations as needing to be reclaimed
518 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
520 struct nfs_delegation *delegation;
522 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
523 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
528 * Reap all unclaimed delegations after reboot recovery is done
530 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
532 struct nfs_delegation *delegation;
536 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
537 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
539 inode = nfs_delegation_grab_inode(delegation);
542 spin_lock(&clp->cl_lock);
543 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
544 spin_unlock(&clp->cl_lock);
546 if (delegation != NULL)
547 nfs_free_delegation(delegation);
554 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
556 struct nfs_inode *nfsi = NFS_I(inode);
557 struct nfs_delegation *delegation;
561 delegation = rcu_dereference(nfsi->delegation);
562 if (delegation != NULL) {
563 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));