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/kthread.h>
12 #include <linux/module.h>
13 #include <linux/sched.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"
23 static struct nfs_delegation *nfs_alloc_delegation(void)
25 return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
28 static void nfs_free_delegation(struct nfs_delegation *delegation)
31 put_rpccred(delegation->cred);
35 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
37 struct inode *inode = state->inode;
41 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
42 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
44 if ((struct nfs_open_context *)fl->fl_file->private_data != ctx)
46 status = nfs4_lock_delegation_recall(state, fl);
51 printk(KERN_ERR "%s: unhandled error %d.\n",
52 __FUNCTION__, status);
53 case -NFS4ERR_EXPIRED:
54 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
55 case -NFS4ERR_STALE_CLIENTID:
56 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs4_state);
65 static void nfs_delegation_claim_opens(struct inode *inode)
67 struct nfs_inode *nfsi = NFS_I(inode);
68 struct nfs_open_context *ctx;
69 struct nfs4_state *state;
73 spin_lock(&inode->i_lock);
74 list_for_each_entry(ctx, &nfsi->open_files, list) {
78 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
80 get_nfs_open_context(ctx);
81 spin_unlock(&inode->i_lock);
82 err = nfs4_open_delegation_recall(ctx->dentry, state);
84 err = nfs_delegation_claim_locks(ctx, state);
85 put_nfs_open_context(ctx);
90 spin_unlock(&inode->i_lock);
94 * Set up a delegation on an inode
96 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
98 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
100 if (delegation == NULL)
102 memcpy(delegation->stateid.data, res->delegation.data,
103 sizeof(delegation->stateid.data));
104 delegation->type = res->delegation_type;
105 delegation->maxsize = res->maxsize;
107 delegation->cred = get_rpccred(cred);
108 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
109 NFS_I(inode)->delegation_state = delegation->type;
114 * Set up a delegation on an inode
116 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
118 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
119 struct nfs_inode *nfsi = NFS_I(inode);
120 struct nfs_delegation *delegation;
123 /* Ensure we first revalidate the attributes and page cache! */
124 if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
125 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
127 delegation = nfs_alloc_delegation();
128 if (delegation == NULL)
130 memcpy(delegation->stateid.data, res->delegation.data,
131 sizeof(delegation->stateid.data));
132 delegation->type = res->delegation_type;
133 delegation->maxsize = res->maxsize;
134 delegation->change_attr = nfsi->change_attr;
135 delegation->cred = get_rpccred(cred);
136 delegation->inode = inode;
138 spin_lock(&clp->cl_lock);
139 if (nfsi->delegation == NULL) {
140 list_add(&delegation->super_list, &clp->cl_delegations);
141 nfsi->delegation = delegation;
142 nfsi->delegation_state = delegation->type;
145 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
146 sizeof(delegation->stateid)) != 0 ||
147 delegation->type != nfsi->delegation->type) {
148 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
149 __FUNCTION__, NIPQUAD(clp->cl_addr));
153 spin_unlock(&clp->cl_lock);
158 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
162 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
163 nfs_free_delegation(delegation);
167 /* Sync all data to disk upon delegation return */
168 static void nfs_msync_inode(struct inode *inode)
170 filemap_fdatawrite(inode->i_mapping);
172 filemap_fdatawait(inode->i_mapping);
176 * Basic procedure for returning a delegation to the server
178 int __nfs_inode_return_delegation(struct inode *inode)
180 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
181 struct nfs_inode *nfsi = NFS_I(inode);
182 struct nfs_delegation *delegation;
185 nfs_msync_inode(inode);
186 down_read(&clp->cl_sem);
187 /* Guard against new delegated open calls */
188 down_write(&nfsi->rwsem);
189 spin_lock(&clp->cl_lock);
190 delegation = nfsi->delegation;
191 if (delegation != NULL) {
192 list_del_init(&delegation->super_list);
193 nfsi->delegation = NULL;
194 nfsi->delegation_state = 0;
196 spin_unlock(&clp->cl_lock);
197 nfs_delegation_claim_opens(inode);
198 up_write(&nfsi->rwsem);
199 up_read(&clp->cl_sem);
200 nfs_msync_inode(inode);
202 if (delegation != NULL)
203 res = nfs_do_return_delegation(inode, delegation);
208 * Return all delegations associated to a super block
210 void nfs_return_all_delegations(struct super_block *sb)
212 struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
213 struct nfs_delegation *delegation;
219 spin_lock(&clp->cl_lock);
220 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
221 if (delegation->inode->i_sb != sb)
223 inode = igrab(delegation->inode);
226 spin_unlock(&clp->cl_lock);
227 nfs_inode_return_delegation(inode);
231 spin_unlock(&clp->cl_lock);
234 int nfs_do_expire_all_delegations(void *ptr)
236 struct nfs4_client *clp = ptr;
237 struct nfs_delegation *delegation;
241 allow_signal(SIGKILL);
243 spin_lock(&clp->cl_lock);
244 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
246 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
248 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
249 inode = igrab(delegation->inode);
252 spin_unlock(&clp->cl_lock);
253 err = nfs_inode_return_delegation(inode);
259 spin_unlock(&clp->cl_lock);
260 nfs4_put_client(clp);
261 module_put_and_exit(0);
264 void nfs_expire_all_delegations(struct nfs4_client *clp)
266 struct task_struct *task;
268 __module_get(THIS_MODULE);
269 atomic_inc(&clp->cl_count);
270 task = kthread_run(nfs_do_expire_all_delegations, clp,
271 "%u.%u.%u.%u-delegreturn",
272 NIPQUAD(clp->cl_addr));
275 nfs4_put_client(clp);
276 module_put(THIS_MODULE);
280 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
282 void nfs_handle_cb_pathdown(struct nfs4_client *clp)
284 struct nfs_delegation *delegation;
290 spin_lock(&clp->cl_lock);
291 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
292 inode = igrab(delegation->inode);
295 spin_unlock(&clp->cl_lock);
296 nfs_inode_return_delegation(inode);
300 spin_unlock(&clp->cl_lock);
303 struct recall_threadargs {
305 struct nfs4_client *clp;
306 const nfs4_stateid *stateid;
308 struct completion started;
312 static int recall_thread(void *data)
314 struct recall_threadargs *args = (struct recall_threadargs *)data;
315 struct inode *inode = igrab(args->inode);
316 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
317 struct nfs_inode *nfsi = NFS_I(inode);
318 struct nfs_delegation *delegation;
320 daemonize("nfsv4-delegreturn");
322 nfs_msync_inode(inode);
323 down_read(&clp->cl_sem);
324 down_write(&nfsi->rwsem);
325 spin_lock(&clp->cl_lock);
326 delegation = nfsi->delegation;
327 if (delegation != NULL && memcmp(delegation->stateid.data,
329 sizeof(delegation->stateid.data)) == 0) {
330 list_del_init(&delegation->super_list);
331 nfsi->delegation = NULL;
332 nfsi->delegation_state = 0;
336 args->result = -ENOENT;
338 spin_unlock(&clp->cl_lock);
339 complete(&args->started);
340 nfs_delegation_claim_opens(inode);
341 up_write(&nfsi->rwsem);
342 up_read(&clp->cl_sem);
343 nfs_msync_inode(inode);
345 if (delegation != NULL)
346 nfs_do_return_delegation(inode, delegation);
348 module_put_and_exit(0);
352 * Asynchronous delegation recall!
354 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
356 struct recall_threadargs data = {
362 init_completion(&data.started);
363 __module_get(THIS_MODULE);
364 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
367 wait_for_completion(&data.started);
370 module_put(THIS_MODULE);
375 * Retrieve the inode associated with a delegation
377 struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
379 struct nfs_delegation *delegation;
380 struct inode *res = NULL;
381 spin_lock(&clp->cl_lock);
382 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
383 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
384 res = igrab(delegation->inode);
388 spin_unlock(&clp->cl_lock);
393 * Mark all delegations as needing to be reclaimed
395 void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
397 struct nfs_delegation *delegation;
398 spin_lock(&clp->cl_lock);
399 list_for_each_entry(delegation, &clp->cl_delegations, super_list)
400 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
401 spin_unlock(&clp->cl_lock);
405 * Reap all unclaimed delegations after reboot recovery is done
407 void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
409 struct nfs_delegation *delegation, *n;
411 spin_lock(&clp->cl_lock);
412 list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
413 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
415 list_move(&delegation->super_list, &head);
416 NFS_I(delegation->inode)->delegation = NULL;
417 NFS_I(delegation->inode)->delegation_state = 0;
419 spin_unlock(&clp->cl_lock);
420 while(!list_empty(&head)) {
421 delegation = list_entry(head.next, struct nfs_delegation, super_list);
422 list_del(&delegation->super_list);
423 nfs_free_delegation(delegation);