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/smp_lock.h>
14 #include <linux/spinlock.h>
16 #include <linux/nfs4.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_xdr.h>
21 #include "delegation.h"
24 static void nfs_do_free_delegation(struct nfs_delegation *delegation)
29 static void nfs_free_delegation_callback(struct rcu_head *head)
31 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
33 nfs_do_free_delegation(delegation);
36 static void nfs_free_delegation(struct nfs_delegation *delegation)
38 struct rpc_cred *cred;
40 cred = rcu_dereference(delegation->cred);
41 rcu_assign_pointer(delegation->cred, NULL);
42 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
47 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
49 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
52 int nfs_have_delegation(struct inode *inode, fmode_t flags)
54 struct nfs_delegation *delegation;
57 flags &= FMODE_READ|FMODE_WRITE;
59 delegation = rcu_dereference(NFS_I(inode)->delegation);
60 if (delegation != NULL && (delegation->type & flags) == flags) {
61 nfs_mark_delegation_referenced(delegation);
68 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
70 struct inode *inode = state->inode;
74 if (inode->i_flock == NULL)
77 /* Protect inode->i_flock using the BKL */
79 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
80 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
82 if (nfs_file_open_context(fl->fl_file) != ctx)
85 status = nfs4_lock_delegation_recall(state, fl);
95 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
97 struct nfs_inode *nfsi = NFS_I(inode);
98 struct nfs_open_context *ctx;
99 struct nfs4_state *state;
103 spin_lock(&inode->i_lock);
104 list_for_each_entry(ctx, &nfsi->open_files, list) {
108 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
110 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
112 get_nfs_open_context(ctx);
113 spin_unlock(&inode->i_lock);
114 err = nfs4_open_delegation_recall(ctx, state, stateid);
116 err = nfs_delegation_claim_locks(ctx, state);
117 put_nfs_open_context(ctx);
122 spin_unlock(&inode->i_lock);
127 * Set up a delegation on an inode
129 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
131 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
132 struct rpc_cred *oldcred;
134 if (delegation == NULL)
136 memcpy(delegation->stateid.data, res->delegation.data,
137 sizeof(delegation->stateid.data));
138 delegation->type = res->delegation_type;
139 delegation->maxsize = res->maxsize;
140 oldcred = delegation->cred;
141 delegation->cred = get_rpccred(cred);
142 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
143 NFS_I(inode)->delegation_state = delegation->type;
145 put_rpccred(oldcred);
148 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
152 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
153 nfs_free_delegation(delegation);
157 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
159 struct inode *inode = NULL;
161 spin_lock(&delegation->lock);
162 if (delegation->inode != NULL)
163 inode = igrab(delegation->inode);
164 spin_unlock(&delegation->lock);
168 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
170 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
172 if (delegation == NULL)
174 spin_lock(&delegation->lock);
175 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
176 sizeof(delegation->stateid.data)) != 0)
178 list_del_rcu(&delegation->super_list);
179 delegation->inode = NULL;
180 nfsi->delegation_state = 0;
181 rcu_assign_pointer(nfsi->delegation, NULL);
182 spin_unlock(&delegation->lock);
185 spin_unlock(&delegation->lock);
191 * Set up a delegation on an inode
193 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
195 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
196 struct nfs_inode *nfsi = NFS_I(inode);
197 struct nfs_delegation *delegation;
198 struct nfs_delegation *freeme = NULL;
201 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
202 if (delegation == NULL)
204 memcpy(delegation->stateid.data, res->delegation.data,
205 sizeof(delegation->stateid.data));
206 delegation->type = res->delegation_type;
207 delegation->maxsize = res->maxsize;
208 delegation->change_attr = nfsi->change_attr;
209 delegation->cred = get_rpccred(cred);
210 delegation->inode = inode;
211 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
212 spin_lock_init(&delegation->lock);
214 spin_lock(&clp->cl_lock);
215 if (rcu_dereference(nfsi->delegation) != NULL) {
216 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
217 sizeof(delegation->stateid)) == 0 &&
218 delegation->type == nfsi->delegation->type) {
222 * Deal with broken servers that hand out two
223 * delegations for the same file.
225 dfprintk(FILE, "%s: server %s handed out "
226 "a duplicate delegation!\n",
227 __func__, clp->cl_hostname);
228 if (delegation->type <= nfsi->delegation->type) {
233 freeme = nfs_detach_delegation_locked(nfsi, NULL);
235 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
236 nfsi->delegation_state = delegation->type;
237 rcu_assign_pointer(nfsi->delegation, delegation);
240 /* Ensure we revalidate the attributes and page cache! */
241 spin_lock(&inode->i_lock);
242 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
243 spin_unlock(&inode->i_lock);
246 spin_unlock(&clp->cl_lock);
247 if (delegation != NULL)
248 nfs_free_delegation(delegation);
250 nfs_do_return_delegation(inode, freeme, 0);
254 /* Sync all data to disk upon delegation return */
255 static void nfs_msync_inode(struct inode *inode)
257 filemap_fdatawrite(inode->i_mapping);
259 filemap_fdatawait(inode->i_mapping);
263 * Basic procedure for returning a delegation to the server
265 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
267 struct nfs_inode *nfsi = NFS_I(inode);
271 * Guard against new delegated open/lock/unlock calls and against
274 down_write(&nfsi->rwsem);
275 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
276 up_write(&nfsi->rwsem);
280 err = nfs_do_return_delegation(inode, delegation, issync);
286 * Return all delegations that have been marked for return
288 int nfs_client_return_marked_delegations(struct nfs_client *clp)
290 struct nfs_delegation *delegation;
296 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
297 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
299 inode = nfs_delegation_grab_inode(delegation);
302 spin_lock(&clp->cl_lock);
303 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
304 spin_unlock(&clp->cl_lock);
306 if (delegation != NULL) {
307 filemap_flush(inode->i_mapping);
308 err = __nfs_inode_return_delegation(inode, delegation, 0);
313 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
321 * This function returns the delegation without reclaiming opens
322 * or protecting against delegation reclaims.
323 * It is therefore really only safe to be called from
326 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
328 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
329 struct nfs_inode *nfsi = NFS_I(inode);
330 struct nfs_delegation *delegation;
332 if (rcu_dereference(nfsi->delegation) != NULL) {
333 spin_lock(&clp->cl_lock);
334 delegation = nfs_detach_delegation_locked(nfsi, NULL);
335 spin_unlock(&clp->cl_lock);
336 if (delegation != NULL)
337 nfs_do_return_delegation(inode, delegation, 0);
341 int nfs_inode_return_delegation(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;
348 if (rcu_dereference(nfsi->delegation) != NULL) {
349 spin_lock(&clp->cl_lock);
350 delegation = nfs_detach_delegation_locked(nfsi, NULL);
351 spin_unlock(&clp->cl_lock);
352 if (delegation != NULL) {
353 nfs_msync_inode(inode);
354 err = __nfs_inode_return_delegation(inode, delegation, 1);
360 static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
362 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
363 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
367 * Return all delegations associated to a super block
369 void nfs_super_return_all_delegations(struct super_block *sb)
371 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
372 struct nfs_delegation *delegation;
377 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
378 spin_lock(&delegation->lock);
379 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
380 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
381 spin_unlock(&delegation->lock);
384 if (nfs_client_return_marked_delegations(clp) != 0)
385 nfs4_schedule_state_manager(clp);
389 void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
391 struct nfs_delegation *delegation;
394 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
395 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
397 if (delegation->type & flags)
398 nfs_mark_return_delegation(clp, delegation);
403 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
405 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
408 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
410 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
411 nfs4_schedule_state_manager(clp);
414 void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
416 nfs_client_mark_return_all_delegation_types(clp, flags);
417 nfs_delegation_run_state_manager(clp);
420 void nfs_expire_all_delegations(struct nfs_client *clp)
422 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
426 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
428 void nfs_handle_cb_pathdown(struct nfs_client *clp)
432 nfs_client_mark_return_all_delegations(clp);
435 static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
437 struct nfs_delegation *delegation;
440 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
441 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
443 nfs_mark_return_delegation(clp, delegation);
448 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
450 nfs_client_mark_return_unreferenced_delegations(clp);
451 nfs_delegation_run_state_manager(clp);
455 * Asynchronous delegation recall!
457 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid,
458 int (*validate_stateid)(struct nfs_delegation *delegation,
459 const nfs4_stateid *stateid))
461 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
462 struct nfs_delegation *delegation;
465 delegation = rcu_dereference(NFS_I(inode)->delegation);
467 if (!validate_stateid(delegation, stateid)) {
472 nfs_mark_return_delegation(clp, delegation);
474 nfs_delegation_run_state_manager(clp);
479 * Retrieve the inode associated with a delegation
481 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
483 struct nfs_delegation *delegation;
484 struct inode *res = NULL;
486 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
487 spin_lock(&delegation->lock);
488 if (delegation->inode != NULL &&
489 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
490 res = igrab(delegation->inode);
492 spin_unlock(&delegation->lock);
501 * Mark all delegations as needing to be reclaimed
503 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
505 struct nfs_delegation *delegation;
507 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
508 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
513 * Reap all unclaimed delegations after reboot recovery is done
515 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
517 struct nfs_delegation *delegation;
521 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
522 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
524 inode = nfs_delegation_grab_inode(delegation);
527 spin_lock(&clp->cl_lock);
528 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
529 spin_unlock(&clp->cl_lock);
531 if (delegation != NULL)
532 nfs_free_delegation(delegation);
539 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
541 struct nfs_inode *nfsi = NFS_I(inode);
542 struct nfs_delegation *delegation;
546 delegation = rcu_dereference(nfsi->delegation);
547 if (delegation != NULL) {
548 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));