2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
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>
15 #include <linux/nfs4.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_xdr.h>
20 #include "delegation.h"
22 static struct nfs_delegation *nfs_alloc_delegation(void)
24 return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
27 static void nfs_free_delegation(struct nfs_delegation *delegation)
30 put_rpccred(delegation->cred);
34 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
36 struct inode *inode = state->inode;
40 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
41 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
43 if ((struct nfs_open_context *)fl->fl_file->private_data != ctx)
45 status = nfs4_lock_delegation_recall(state, fl);
50 printk(KERN_ERR "%s: unhandled error %d.\n",
51 __FUNCTION__, status);
52 case -NFS4ERR_EXPIRED:
53 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
54 case -NFS4ERR_STALE_CLIENTID:
55 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs4_state);
64 static void nfs_delegation_claim_opens(struct inode *inode)
66 struct nfs_inode *nfsi = NFS_I(inode);
67 struct nfs_open_context *ctx;
68 struct nfs4_state *state;
72 spin_lock(&inode->i_lock);
73 list_for_each_entry(ctx, &nfsi->open_files, list) {
77 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
79 get_nfs_open_context(ctx);
80 spin_unlock(&inode->i_lock);
81 err = nfs4_open_delegation_recall(ctx->dentry, state);
83 err = nfs_delegation_claim_locks(ctx, state);
84 put_nfs_open_context(ctx);
89 spin_unlock(&inode->i_lock);
93 * Set up a delegation on an inode
95 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
97 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
99 if (delegation == NULL)
101 memcpy(delegation->stateid.data, res->delegation.data,
102 sizeof(delegation->stateid.data));
103 delegation->type = res->delegation_type;
104 delegation->maxsize = res->maxsize;
106 delegation->cred = get_rpccred(cred);
107 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
108 NFS_I(inode)->delegation_state = delegation->type;
113 * Set up a delegation on an inode
115 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
117 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
118 struct nfs_inode *nfsi = NFS_I(inode);
119 struct nfs_delegation *delegation;
122 /* Ensure we first revalidate the attributes and page cache! */
123 if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
124 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
126 delegation = nfs_alloc_delegation();
127 if (delegation == NULL)
129 memcpy(delegation->stateid.data, res->delegation.data,
130 sizeof(delegation->stateid.data));
131 delegation->type = res->delegation_type;
132 delegation->maxsize = res->maxsize;
133 delegation->cred = get_rpccred(cred);
134 delegation->inode = inode;
136 spin_lock(&clp->cl_lock);
137 if (nfsi->delegation == NULL) {
138 list_add(&delegation->super_list, &clp->cl_delegations);
139 nfsi->delegation = delegation;
140 nfsi->delegation_state = delegation->type;
143 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
144 sizeof(delegation->stateid)) != 0 ||
145 delegation->type != nfsi->delegation->type) {
146 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
147 __FUNCTION__, NIPQUAD(clp->cl_addr));
151 spin_unlock(&clp->cl_lock);
152 if (delegation != NULL)
157 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
161 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
163 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
164 nfs_free_delegation(delegation);
168 /* Sync all data to disk upon delegation return */
169 static void nfs_msync_inode(struct inode *inode)
171 filemap_fdatawrite(inode->i_mapping);
173 filemap_fdatawait(inode->i_mapping);
177 * Basic procedure for returning a delegation to the server
179 int __nfs_inode_return_delegation(struct inode *inode)
181 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
182 struct nfs_inode *nfsi = NFS_I(inode);
183 struct nfs_delegation *delegation;
186 nfs_msync_inode(inode);
187 down_read(&clp->cl_sem);
188 /* Guard against new delegated open calls */
189 down_write(&nfsi->rwsem);
190 spin_lock(&clp->cl_lock);
191 delegation = nfsi->delegation;
192 if (delegation != NULL) {
193 list_del_init(&delegation->super_list);
194 nfsi->delegation = NULL;
195 nfsi->delegation_state = 0;
197 spin_unlock(&clp->cl_lock);
198 nfs_delegation_claim_opens(inode);
199 up_write(&nfsi->rwsem);
200 up_read(&clp->cl_sem);
201 nfs_msync_inode(inode);
203 if (delegation != NULL)
204 res = nfs_do_return_delegation(inode, delegation);
209 * Return all delegations associated to a super block
211 void nfs_return_all_delegations(struct super_block *sb)
213 struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
214 struct nfs_delegation *delegation;
220 spin_lock(&clp->cl_lock);
221 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
222 if (delegation->inode->i_sb != sb)
224 inode = igrab(delegation->inode);
227 spin_unlock(&clp->cl_lock);
228 nfs_inode_return_delegation(inode);
232 spin_unlock(&clp->cl_lock);
236 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
238 void nfs_handle_cb_pathdown(struct nfs4_client *clp)
240 struct nfs_delegation *delegation;
246 spin_lock(&clp->cl_lock);
247 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
248 inode = igrab(delegation->inode);
251 spin_unlock(&clp->cl_lock);
252 nfs_inode_return_delegation(inode);
256 spin_unlock(&clp->cl_lock);
259 struct recall_threadargs {
261 struct nfs4_client *clp;
262 const nfs4_stateid *stateid;
264 struct completion started;
268 static int recall_thread(void *data)
270 struct recall_threadargs *args = (struct recall_threadargs *)data;
271 struct inode *inode = igrab(args->inode);
272 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
273 struct nfs_inode *nfsi = NFS_I(inode);
274 struct nfs_delegation *delegation;
276 daemonize("nfsv4-delegreturn");
278 nfs_msync_inode(inode);
279 down_read(&clp->cl_sem);
280 down_write(&nfsi->rwsem);
281 spin_lock(&clp->cl_lock);
282 delegation = nfsi->delegation;
283 if (delegation != NULL && memcmp(delegation->stateid.data,
285 sizeof(delegation->stateid.data)) == 0) {
286 list_del_init(&delegation->super_list);
287 nfsi->delegation = NULL;
288 nfsi->delegation_state = 0;
292 args->result = -ENOENT;
294 spin_unlock(&clp->cl_lock);
295 complete(&args->started);
296 nfs_delegation_claim_opens(inode);
297 up_write(&nfsi->rwsem);
298 up_read(&clp->cl_sem);
299 nfs_msync_inode(inode);
301 if (delegation != NULL)
302 nfs_do_return_delegation(inode, delegation);
304 module_put_and_exit(0);
308 * Asynchronous delegation recall!
310 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
312 struct recall_threadargs data = {
318 init_completion(&data.started);
319 __module_get(THIS_MODULE);
320 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
323 wait_for_completion(&data.started);
326 module_put(THIS_MODULE);
331 * Retrieve the inode associated with a delegation
333 struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
335 struct nfs_delegation *delegation;
336 struct inode *res = NULL;
337 spin_lock(&clp->cl_lock);
338 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
339 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
340 res = igrab(delegation->inode);
344 spin_unlock(&clp->cl_lock);
349 * Mark all delegations as needing to be reclaimed
351 void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
353 struct nfs_delegation *delegation;
354 spin_lock(&clp->cl_lock);
355 list_for_each_entry(delegation, &clp->cl_delegations, super_list)
356 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
357 spin_unlock(&clp->cl_lock);
361 * Reap all unclaimed delegations after reboot recovery is done
363 void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
365 struct nfs_delegation *delegation, *n;
367 spin_lock(&clp->cl_lock);
368 list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
369 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
371 list_move(&delegation->super_list, &head);
372 NFS_I(delegation->inode)->delegation = NULL;
373 NFS_I(delegation->inode)->delegation_state = 0;
375 spin_unlock(&clp->cl_lock);
376 while(!list_empty(&head)) {
377 delegation = list_entry(head.next, struct nfs_delegation, super_list);
378 list_del(&delegation->super_list);
379 nfs_free_delegation(delegation);