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/spinlock.h>
15 #include <linux/nfs4.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_xdr.h>
20 #include "delegation.h"
23 static void nfs_free_delegation(struct nfs_delegation *delegation)
26 put_rpccred(delegation->cred);
30 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
32 struct inode *inode = state->inode;
36 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
37 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
39 if ((struct nfs_open_context *)fl->fl_file->private_data != ctx)
41 status = nfs4_lock_delegation_recall(state, fl);
46 printk(KERN_ERR "%s: unhandled error %d.\n",
47 __FUNCTION__, status);
48 case -NFS4ERR_EXPIRED:
49 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
50 case -NFS4ERR_STALE_CLIENTID:
51 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
60 static void nfs_delegation_claim_opens(struct inode *inode)
62 struct nfs_inode *nfsi = NFS_I(inode);
63 struct nfs_open_context *ctx;
64 struct nfs4_state *state;
68 spin_lock(&inode->i_lock);
69 list_for_each_entry(ctx, &nfsi->open_files, list) {
73 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
75 get_nfs_open_context(ctx);
76 spin_unlock(&inode->i_lock);
77 err = nfs4_open_delegation_recall(ctx->dentry, state);
79 err = nfs_delegation_claim_locks(ctx, state);
80 put_nfs_open_context(ctx);
85 spin_unlock(&inode->i_lock);
89 * Set up a delegation on an inode
91 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
93 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
95 if (delegation == NULL)
97 memcpy(delegation->stateid.data, res->delegation.data,
98 sizeof(delegation->stateid.data));
99 delegation->type = res->delegation_type;
100 delegation->maxsize = res->maxsize;
102 delegation->cred = get_rpccred(cred);
103 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
104 NFS_I(inode)->delegation_state = delegation->type;
109 * Set up a delegation on an inode
111 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
113 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
114 struct nfs_inode *nfsi = NFS_I(inode);
115 struct nfs_delegation *delegation;
118 /* Ensure we first revalidate the attributes and page cache! */
119 if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
120 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
122 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
123 if (delegation == NULL)
125 memcpy(delegation->stateid.data, res->delegation.data,
126 sizeof(delegation->stateid.data));
127 delegation->type = res->delegation_type;
128 delegation->maxsize = res->maxsize;
129 delegation->change_attr = nfsi->change_attr;
130 delegation->cred = get_rpccred(cred);
131 delegation->inode = inode;
133 spin_lock(&clp->cl_lock);
134 if (nfsi->delegation == NULL) {
135 list_add(&delegation->super_list, &clp->cl_delegations);
136 nfsi->delegation = delegation;
137 nfsi->delegation_state = delegation->type;
140 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
141 sizeof(delegation->stateid)) != 0 ||
142 delegation->type != nfsi->delegation->type) {
143 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
144 __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
148 spin_unlock(&clp->cl_lock);
153 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
157 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
158 nfs_free_delegation(delegation);
162 /* Sync all data to disk upon delegation return */
163 static void nfs_msync_inode(struct inode *inode)
165 filemap_fdatawrite(inode->i_mapping);
167 filemap_fdatawait(inode->i_mapping);
171 * Basic procedure for returning a delegation to the server
173 int __nfs_inode_return_delegation(struct inode *inode)
175 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
176 struct nfs_inode *nfsi = NFS_I(inode);
177 struct nfs_delegation *delegation;
180 nfs_msync_inode(inode);
181 down_read(&clp->cl_sem);
182 /* Guard against new delegated open calls */
183 down_write(&nfsi->rwsem);
184 spin_lock(&clp->cl_lock);
185 delegation = nfsi->delegation;
186 if (delegation != NULL) {
187 list_del_init(&delegation->super_list);
188 nfsi->delegation = NULL;
189 nfsi->delegation_state = 0;
191 spin_unlock(&clp->cl_lock);
192 nfs_delegation_claim_opens(inode);
193 up_write(&nfsi->rwsem);
194 up_read(&clp->cl_sem);
195 nfs_msync_inode(inode);
197 if (delegation != NULL)
198 res = nfs_do_return_delegation(inode, delegation);
203 * Return all delegations associated to a super block
205 void nfs_return_all_delegations(struct super_block *sb)
207 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
208 struct nfs_delegation *delegation;
214 spin_lock(&clp->cl_lock);
215 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
216 if (delegation->inode->i_sb != sb)
218 inode = igrab(delegation->inode);
221 spin_unlock(&clp->cl_lock);
222 nfs_inode_return_delegation(inode);
226 spin_unlock(&clp->cl_lock);
229 static int nfs_do_expire_all_delegations(void *ptr)
231 struct nfs_client *clp = ptr;
232 struct nfs_delegation *delegation;
235 allow_signal(SIGKILL);
237 spin_lock(&clp->cl_lock);
238 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
240 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
242 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
243 inode = igrab(delegation->inode);
246 spin_unlock(&clp->cl_lock);
247 nfs_inode_return_delegation(inode);
252 spin_unlock(&clp->cl_lock);
254 module_put_and_exit(0);
257 void nfs_expire_all_delegations(struct nfs_client *clp)
259 struct task_struct *task;
261 __module_get(THIS_MODULE);
262 atomic_inc(&clp->cl_count);
263 task = kthread_run(nfs_do_expire_all_delegations, clp,
264 "%u.%u.%u.%u-delegreturn",
265 NIPQUAD(clp->cl_addr.sin_addr));
269 module_put(THIS_MODULE);
273 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
275 void nfs_handle_cb_pathdown(struct nfs_client *clp)
277 struct nfs_delegation *delegation;
283 spin_lock(&clp->cl_lock);
284 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
285 inode = igrab(delegation->inode);
288 spin_unlock(&clp->cl_lock);
289 nfs_inode_return_delegation(inode);
293 spin_unlock(&clp->cl_lock);
296 struct recall_threadargs {
298 struct nfs_client *clp;
299 const nfs4_stateid *stateid;
301 struct completion started;
305 static int recall_thread(void *data)
307 struct recall_threadargs *args = (struct recall_threadargs *)data;
308 struct inode *inode = igrab(args->inode);
309 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
310 struct nfs_inode *nfsi = NFS_I(inode);
311 struct nfs_delegation *delegation;
313 daemonize("nfsv4-delegreturn");
315 nfs_msync_inode(inode);
316 down_read(&clp->cl_sem);
317 down_write(&nfsi->rwsem);
318 spin_lock(&clp->cl_lock);
319 delegation = nfsi->delegation;
320 if (delegation != NULL && memcmp(delegation->stateid.data,
322 sizeof(delegation->stateid.data)) == 0) {
323 list_del_init(&delegation->super_list);
324 nfsi->delegation = NULL;
325 nfsi->delegation_state = 0;
329 args->result = -ENOENT;
331 spin_unlock(&clp->cl_lock);
332 complete(&args->started);
333 nfs_delegation_claim_opens(inode);
334 up_write(&nfsi->rwsem);
335 up_read(&clp->cl_sem);
336 nfs_msync_inode(inode);
338 if (delegation != NULL)
339 nfs_do_return_delegation(inode, delegation);
341 module_put_and_exit(0);
345 * Asynchronous delegation recall!
347 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
349 struct recall_threadargs data = {
355 init_completion(&data.started);
356 __module_get(THIS_MODULE);
357 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
360 wait_for_completion(&data.started);
363 module_put(THIS_MODULE);
368 * Retrieve the inode associated with a delegation
370 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
372 struct nfs_delegation *delegation;
373 struct inode *res = NULL;
374 spin_lock(&clp->cl_lock);
375 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
376 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
377 res = igrab(delegation->inode);
381 spin_unlock(&clp->cl_lock);
386 * Mark all delegations as needing to be reclaimed
388 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
390 struct nfs_delegation *delegation;
391 spin_lock(&clp->cl_lock);
392 list_for_each_entry(delegation, &clp->cl_delegations, super_list)
393 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
394 spin_unlock(&clp->cl_lock);
398 * Reap all unclaimed delegations after reboot recovery is done
400 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
402 struct nfs_delegation *delegation, *n;
404 spin_lock(&clp->cl_lock);
405 list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
406 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
408 list_move(&delegation->super_list, &head);
409 NFS_I(delegation->inode)->delegation = NULL;
410 NFS_I(delegation->inode)->delegation_state = 0;
412 spin_unlock(&clp->cl_lock);
413 while(!list_empty(&head)) {
414 delegation = list_entry(head.next, struct nfs_delegation, super_list);
415 list_del(&delegation->super_list);
416 nfs_free_delegation(delegation);
420 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
422 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
423 struct nfs_inode *nfsi = NFS_I(inode);
424 struct nfs_delegation *delegation;
427 if (nfsi->delegation_state == 0)
429 spin_lock(&clp->cl_lock);
430 delegation = nfsi->delegation;
431 if (delegation != NULL) {
432 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
435 spin_unlock(&clp->cl_lock);