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 = NFS_I(inode)->delegation;
133 struct rpc_cred *oldcred;
135 if (delegation == NULL)
137 memcpy(delegation->stateid.data, res->delegation.data,
138 sizeof(delegation->stateid.data));
139 delegation->type = res->delegation_type;
140 delegation->maxsize = res->maxsize;
141 oldcred = delegation->cred;
142 delegation->cred = get_rpccred(cred);
143 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
144 NFS_I(inode)->delegation_state = delegation->type;
146 put_rpccred(oldcred);
149 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
153 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
154 nfs_free_delegation(delegation);
158 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
160 struct inode *inode = NULL;
162 spin_lock(&delegation->lock);
163 if (delegation->inode != NULL)
164 inode = igrab(delegation->inode);
165 spin_unlock(&delegation->lock);
169 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
171 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
173 if (delegation == NULL)
175 spin_lock(&delegation->lock);
176 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
177 sizeof(delegation->stateid.data)) != 0)
179 list_del_rcu(&delegation->super_list);
180 delegation->inode = NULL;
181 nfsi->delegation_state = 0;
182 rcu_assign_pointer(nfsi->delegation, NULL);
183 spin_unlock(&delegation->lock);
186 spin_unlock(&delegation->lock);
192 * Set up a delegation on an inode
194 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
196 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
197 struct nfs_inode *nfsi = NFS_I(inode);
198 struct nfs_delegation *delegation;
199 struct nfs_delegation *freeme = NULL;
202 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
203 if (delegation == NULL)
205 memcpy(delegation->stateid.data, res->delegation.data,
206 sizeof(delegation->stateid.data));
207 delegation->type = res->delegation_type;
208 delegation->maxsize = res->maxsize;
209 delegation->change_attr = nfsi->change_attr;
210 delegation->cred = get_rpccred(cred);
211 delegation->inode = inode;
212 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
213 spin_lock_init(&delegation->lock);
215 spin_lock(&clp->cl_lock);
216 if (rcu_dereference(nfsi->delegation) != NULL) {
217 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
218 sizeof(delegation->stateid)) == 0 &&
219 delegation->type == nfsi->delegation->type) {
223 * Deal with broken servers that hand out two
224 * delegations for the same file.
226 dfprintk(FILE, "%s: server %s handed out "
227 "a duplicate delegation!\n",
228 __func__, clp->cl_hostname);
229 if (delegation->type <= nfsi->delegation->type) {
234 freeme = nfs_detach_delegation_locked(nfsi, NULL);
236 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
237 nfsi->delegation_state = delegation->type;
238 rcu_assign_pointer(nfsi->delegation, delegation);
241 /* Ensure we revalidate the attributes and page cache! */
242 spin_lock(&inode->i_lock);
243 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
244 spin_unlock(&inode->i_lock);
247 spin_unlock(&clp->cl_lock);
248 if (delegation != NULL)
249 nfs_free_delegation(delegation);
251 nfs_do_return_delegation(inode, freeme, 0);
255 /* Sync all data to disk upon delegation return */
256 static void nfs_msync_inode(struct inode *inode)
258 filemap_fdatawrite(inode->i_mapping);
260 filemap_fdatawait(inode->i_mapping);
264 * Basic procedure for returning a delegation to the server
266 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
268 struct nfs_inode *nfsi = NFS_I(inode);
272 * Guard against new delegated open/lock/unlock calls and against
275 down_write(&nfsi->rwsem);
276 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
277 up_write(&nfsi->rwsem);
281 err = nfs_do_return_delegation(inode, delegation, issync);
287 * Return all delegations that have been marked for return
289 int nfs_client_return_marked_delegations(struct nfs_client *clp)
291 struct nfs_delegation *delegation;
297 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
298 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
300 inode = nfs_delegation_grab_inode(delegation);
303 spin_lock(&clp->cl_lock);
304 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
305 spin_unlock(&clp->cl_lock);
307 if (delegation != NULL) {
308 filemap_flush(inode->i_mapping);
309 err = __nfs_inode_return_delegation(inode, delegation, 0);
314 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
322 * This function returns the delegation without reclaiming opens
323 * or protecting against delegation reclaims.
324 * It is therefore really only safe to be called from
327 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
329 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
330 struct nfs_inode *nfsi = NFS_I(inode);
331 struct nfs_delegation *delegation;
333 if (rcu_dereference(nfsi->delegation) != NULL) {
334 spin_lock(&clp->cl_lock);
335 delegation = nfs_detach_delegation_locked(nfsi, NULL);
336 spin_unlock(&clp->cl_lock);
337 if (delegation != NULL)
338 nfs_do_return_delegation(inode, delegation, 0);
342 int nfs_inode_return_delegation(struct inode *inode)
344 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
345 struct nfs_inode *nfsi = NFS_I(inode);
346 struct nfs_delegation *delegation;
349 if (rcu_dereference(nfsi->delegation) != NULL) {
350 spin_lock(&clp->cl_lock);
351 delegation = nfs_detach_delegation_locked(nfsi, NULL);
352 spin_unlock(&clp->cl_lock);
353 if (delegation != NULL) {
354 nfs_msync_inode(inode);
355 err = __nfs_inode_return_delegation(inode, delegation, 1);
361 static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
363 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
364 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
368 * Return all delegations associated to a super block
370 void nfs_super_return_all_delegations(struct super_block *sb)
372 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
373 struct nfs_delegation *delegation;
378 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
379 spin_lock(&delegation->lock);
380 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
381 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
382 spin_unlock(&delegation->lock);
385 if (nfs_client_return_marked_delegations(clp) != 0)
386 nfs4_schedule_state_manager(clp);
390 void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
392 struct nfs_delegation *delegation;
395 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
396 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
398 if (delegation->type & flags)
399 nfs_mark_return_delegation(clp, delegation);
404 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
406 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
409 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
411 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
412 nfs4_schedule_state_manager(clp);
415 void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
417 nfs_client_mark_return_all_delegation_types(clp, flags);
418 nfs_delegation_run_state_manager(clp);
421 void nfs_expire_all_delegations(struct nfs_client *clp)
423 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
427 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
429 void nfs_handle_cb_pathdown(struct nfs_client *clp)
433 nfs_client_mark_return_all_delegations(clp);
436 static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
438 struct nfs_delegation *delegation;
441 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
442 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
444 nfs_mark_return_delegation(clp, delegation);
449 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
451 nfs_client_mark_return_unreferenced_delegations(clp);
452 nfs_delegation_run_state_manager(clp);
456 * Asynchronous delegation recall!
458 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid,
459 int (*validate_stateid)(struct nfs_delegation *delegation,
460 const nfs4_stateid *stateid))
462 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
463 struct nfs_delegation *delegation;
466 delegation = rcu_dereference(NFS_I(inode)->delegation);
468 if (!validate_stateid(delegation, stateid)) {
473 nfs_mark_return_delegation(clp, delegation);
475 nfs_delegation_run_state_manager(clp);
480 * Retrieve the inode associated with a delegation
482 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
484 struct nfs_delegation *delegation;
485 struct inode *res = NULL;
487 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
488 spin_lock(&delegation->lock);
489 if (delegation->inode != NULL &&
490 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
491 res = igrab(delegation->inode);
493 spin_unlock(&delegation->lock);
502 * Mark all delegations as needing to be reclaimed
504 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
506 struct nfs_delegation *delegation;
508 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
509 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
514 * Reap all unclaimed delegations after reboot recovery is done
516 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
518 struct nfs_delegation *delegation;
522 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
523 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
525 inode = nfs_delegation_grab_inode(delegation);
528 spin_lock(&clp->cl_lock);
529 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
530 spin_unlock(&clp->cl_lock);
532 if (delegation != NULL)
533 nfs_free_delegation(delegation);
540 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
542 struct nfs_inode *nfsi = NFS_I(inode);
543 struct nfs_delegation *delegation;
547 delegation = rcu_dereference(nfsi->delegation);
548 if (delegation != NULL) {
549 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));