blob: 93a9f4bd9bd0cd8276ccbecb76a7637d8d61fc98 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/delegation.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFS file delegation management
7 *
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/completion.h>
Trond Myklebust58d97142006-01-03 09:55:24 +010010#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/spinlock.h>
14
15#include <linux/nfs4.h>
16#include <linux/nfs_fs.h>
17#include <linux/nfs_xdr.h>
18
Trond Myklebust4ce79712005-06-22 17:16:21 +000019#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040021#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023static void nfs_free_delegation(struct nfs_delegation *delegation)
24{
25 if (delegation->cred)
26 put_rpccred(delegation->cred);
27 kfree(delegation);
28}
29
Trond Myklebust888e6942005-11-04 15:38:11 -050030static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
31{
32 struct inode *inode = state->inode;
33 struct file_lock *fl;
34 int status;
35
36 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
37 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
38 continue;
39 if ((struct nfs_open_context *)fl->fl_file->private_data != ctx)
40 continue;
41 status = nfs4_lock_delegation_recall(state, fl);
42 if (status >= 0)
43 continue;
44 switch (status) {
45 default:
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:
David Howells7539bba2006-08-22 20:06:09 -040051 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
Trond Myklebust888e6942005-11-04 15:38:11 -050052 goto out_err;
53 }
54 }
55 return 0;
56out_err:
57 return status;
58}
59
Trond Myklebust90163022007-07-05 14:55:18 -040060static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
62 struct nfs_inode *nfsi = NFS_I(inode);
63 struct nfs_open_context *ctx;
64 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -050065 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67again:
68 spin_lock(&inode->i_lock);
69 list_for_each_entry(ctx, &nfsi->open_files, list) {
70 state = ctx->state;
71 if (state == NULL)
72 continue;
73 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
74 continue;
Trond Myklebust90163022007-07-05 14:55:18 -040075 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
76 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 get_nfs_open_context(ctx);
78 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -040079 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -050080 if (err >= 0)
81 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -050083 if (err != 0)
84 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 goto again;
86 }
87 spin_unlock(&inode->i_lock);
88}
89
90/*
91 * Set up a delegation on an inode
92 */
93void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
94{
95 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
96
97 if (delegation == NULL)
98 return;
99 memcpy(delegation->stateid.data, res->delegation.data,
100 sizeof(delegation->stateid.data));
101 delegation->type = res->delegation_type;
102 delegation->maxsize = res->maxsize;
103 put_rpccred(cred);
104 delegation->cred = get_rpccred(cred);
105 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
106 NFS_I(inode)->delegation_state = delegation->type;
107 smp_wmb();
108}
109
110/*
111 * Set up a delegation on an inode
112 */
113int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
114{
David Howells7539bba2006-08-22 20:06:09 -0400115 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct nfs_inode *nfsi = NFS_I(inode);
117 struct nfs_delegation *delegation;
118 int status = 0;
119
Trond Myklebustb3c52da2005-10-17 06:02:00 -0400120 /* Ensure we first revalidate the attributes and page cache! */
121 if ((nfsi->cache_validity & (NFS_INO_REVAL_PAGECACHE|NFS_INO_INVALID_ATTR)))
122 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
123
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700124 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 if (delegation == NULL)
126 return -ENOMEM;
127 memcpy(delegation->stateid.data, res->delegation.data,
128 sizeof(delegation->stateid.data));
129 delegation->type = res->delegation_type;
130 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100131 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 delegation->cred = get_rpccred(cred);
133 delegation->inode = inode;
134
135 spin_lock(&clp->cl_lock);
136 if (nfsi->delegation == NULL) {
137 list_add(&delegation->super_list, &clp->cl_delegations);
138 nfsi->delegation = delegation;
139 nfsi->delegation_state = delegation->type;
140 delegation = NULL;
141 } else {
142 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
143 sizeof(delegation->stateid)) != 0 ||
144 delegation->type != nfsi->delegation->type) {
145 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
David Howells24c8dbb2006-08-22 20:06:10 -0400146 __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 status = -EIO;
148 }
149 }
150 spin_unlock(&clp->cl_lock);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800151 kfree(delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return status;
153}
154
155static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
156{
157 int res = 0;
158
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
160 nfs_free_delegation(delegation);
161 return res;
162}
163
164/* Sync all data to disk upon delegation return */
165static void nfs_msync_inode(struct inode *inode)
166{
167 filemap_fdatawrite(inode->i_mapping);
168 nfs_wb_all(inode);
169 filemap_fdatawait(inode->i_mapping);
170}
171
172/*
173 * Basic procedure for returning a delegation to the server
174 */
Trond Myklebust90163022007-07-05 14:55:18 -0400175static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
David Howells7539bba2006-08-22 20:06:09 -0400177 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179
180 nfs_msync_inode(inode);
181 down_read(&clp->cl_sem);
182 /* Guard against new delegated open calls */
183 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400184 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 up_write(&nfsi->rwsem);
186 up_read(&clp->cl_sem);
187 nfs_msync_inode(inode);
188
Trond Myklebust90163022007-07-05 14:55:18 -0400189 return nfs_do_return_delegation(inode, delegation);
190}
191
192static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
193{
194 struct nfs_delegation *delegation = nfsi->delegation;
195
196 if (delegation == NULL)
197 goto nomatch;
198 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
199 sizeof(delegation->stateid.data)) != 0)
200 goto nomatch;
201 list_del_init(&delegation->super_list);
202 nfsi->delegation = NULL;
203 nfsi->delegation_state = 0;
204 return delegation;
205nomatch:
206 return NULL;
207}
208
209int nfs_inode_return_delegation(struct inode *inode)
210{
211 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
212 struct nfs_inode *nfsi = NFS_I(inode);
213 struct nfs_delegation *delegation;
214 int err = 0;
215
216 if (nfsi->delegation_state != 0) {
217 spin_lock(&clp->cl_lock);
218 delegation = nfs_detach_delegation_locked(nfsi, NULL);
219 spin_unlock(&clp->cl_lock);
220 if (delegation != NULL)
221 err = __nfs_inode_return_delegation(inode, delegation);
222 }
223 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/*
227 * Return all delegations associated to a super block
228 */
229void nfs_return_all_delegations(struct super_block *sb)
230{
David Howells7539bba2006-08-22 20:06:09 -0400231 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct nfs_delegation *delegation;
233 struct inode *inode;
234
235 if (clp == NULL)
236 return;
237restart:
238 spin_lock(&clp->cl_lock);
239 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
240 if (delegation->inode->i_sb != sb)
241 continue;
242 inode = igrab(delegation->inode);
243 if (inode == NULL)
244 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400245 nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_unlock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400247 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 iput(inode);
249 goto restart;
250 }
251 spin_unlock(&clp->cl_lock);
252}
253
Trond Myklebust10afec902007-05-14 17:16:04 -0400254static int nfs_do_expire_all_delegations(void *ptr)
Trond Myklebust58d97142006-01-03 09:55:24 +0100255{
David Howellsadfa6f92006-08-22 20:06:08 -0400256 struct nfs_client *clp = ptr;
Trond Myklebust58d97142006-01-03 09:55:24 +0100257 struct nfs_delegation *delegation;
258 struct inode *inode;
Trond Myklebust58d97142006-01-03 09:55:24 +0100259
260 allow_signal(SIGKILL);
261restart:
262 spin_lock(&clp->cl_lock);
263 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
264 goto out;
265 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
266 goto out;
267 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
268 inode = igrab(delegation->inode);
269 if (inode == NULL)
270 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400271 nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust58d97142006-01-03 09:55:24 +0100272 spin_unlock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400273 __nfs_inode_return_delegation(inode, delegation);
Trond Myklebust58d97142006-01-03 09:55:24 +0100274 iput(inode);
Trond Myklebust26c78e12006-01-03 09:55:58 +0100275 goto restart;
Trond Myklebust58d97142006-01-03 09:55:24 +0100276 }
277out:
278 spin_unlock(&clp->cl_lock);
David Howells24c8dbb2006-08-22 20:06:10 -0400279 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100280 module_put_and_exit(0);
281}
282
David Howellsadfa6f92006-08-22 20:06:08 -0400283void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100284{
285 struct task_struct *task;
286
287 __module_get(THIS_MODULE);
288 atomic_inc(&clp->cl_count);
289 task = kthread_run(nfs_do_expire_all_delegations, clp,
290 "%u.%u.%u.%u-delegreturn",
David Howells24c8dbb2006-08-22 20:06:10 -0400291 NIPQUAD(clp->cl_addr.sin_addr));
Trond Myklebust58d97142006-01-03 09:55:24 +0100292 if (!IS_ERR(task))
293 return;
David Howells24c8dbb2006-08-22 20:06:10 -0400294 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100295 module_put(THIS_MODULE);
296}
297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298/*
299 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
300 */
David Howellsadfa6f92006-08-22 20:06:08 -0400301void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 struct nfs_delegation *delegation;
304 struct inode *inode;
305
306 if (clp == NULL)
307 return;
308restart:
309 spin_lock(&clp->cl_lock);
310 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
311 inode = igrab(delegation->inode);
312 if (inode == NULL)
313 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400314 nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 spin_unlock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400316 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 iput(inode);
318 goto restart;
319 }
320 spin_unlock(&clp->cl_lock);
321}
322
323struct recall_threadargs {
324 struct inode *inode;
David Howellsadfa6f92006-08-22 20:06:08 -0400325 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 const nfs4_stateid *stateid;
327
328 struct completion started;
329 int result;
330};
331
332static int recall_thread(void *data)
333{
334 struct recall_threadargs *args = (struct recall_threadargs *)data;
335 struct inode *inode = igrab(args->inode);
David Howells7539bba2006-08-22 20:06:09 -0400336 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct nfs_inode *nfsi = NFS_I(inode);
338 struct nfs_delegation *delegation;
339
340 daemonize("nfsv4-delegreturn");
341
342 nfs_msync_inode(inode);
343 down_read(&clp->cl_sem);
344 down_write(&nfsi->rwsem);
345 spin_lock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400346 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
347 if (delegation != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 args->result = 0;
Trond Myklebust90163022007-07-05 14:55:18 -0400349 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 args->result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 spin_unlock(&clp->cl_lock);
352 complete(&args->started);
Trond Myklebust90163022007-07-05 14:55:18 -0400353 nfs_delegation_claim_opens(inode, args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 up_write(&nfsi->rwsem);
355 up_read(&clp->cl_sem);
356 nfs_msync_inode(inode);
357
358 if (delegation != NULL)
359 nfs_do_return_delegation(inode, delegation);
360 iput(inode);
361 module_put_and_exit(0);
362}
363
364/*
365 * Asynchronous delegation recall!
366 */
367int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
368{
369 struct recall_threadargs data = {
370 .inode = inode,
371 .stateid = stateid,
372 };
373 int status;
374
375 init_completion(&data.started);
376 __module_get(THIS_MODULE);
377 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
378 if (status < 0)
379 goto out_module_put;
380 wait_for_completion(&data.started);
381 return data.result;
382out_module_put:
383 module_put(THIS_MODULE);
384 return status;
385}
386
387/*
388 * Retrieve the inode associated with a delegation
389 */
David Howellsadfa6f92006-08-22 20:06:08 -0400390struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
392 struct nfs_delegation *delegation;
393 struct inode *res = NULL;
394 spin_lock(&clp->cl_lock);
395 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
396 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
397 res = igrab(delegation->inode);
398 break;
399 }
400 }
401 spin_unlock(&clp->cl_lock);
402 return res;
403}
404
405/*
406 * Mark all delegations as needing to be reclaimed
407 */
David Howellsadfa6f92006-08-22 20:06:08 -0400408void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409{
410 struct nfs_delegation *delegation;
411 spin_lock(&clp->cl_lock);
412 list_for_each_entry(delegation, &clp->cl_delegations, super_list)
413 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
414 spin_unlock(&clp->cl_lock);
415}
416
417/*
418 * Reap all unclaimed delegations after reboot recovery is done
419 */
David Howellsadfa6f92006-08-22 20:06:08 -0400420void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct nfs_delegation *delegation, *n;
423 LIST_HEAD(head);
424 spin_lock(&clp->cl_lock);
425 list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
426 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
427 continue;
428 list_move(&delegation->super_list, &head);
429 NFS_I(delegation->inode)->delegation = NULL;
430 NFS_I(delegation->inode)->delegation_state = 0;
431 }
432 spin_unlock(&clp->cl_lock);
433 while(!list_empty(&head)) {
434 delegation = list_entry(head.next, struct nfs_delegation, super_list);
435 list_del(&delegation->super_list);
436 nfs_free_delegation(delegation);
437 }
438}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500439
440int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
441{
David Howells7539bba2006-08-22 20:06:09 -0400442 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500443 struct nfs_inode *nfsi = NFS_I(inode);
444 struct nfs_delegation *delegation;
445 int res = 0;
446
447 if (nfsi->delegation_state == 0)
448 return 0;
449 spin_lock(&clp->cl_lock);
450 delegation = nfsi->delegation;
451 if (delegation != NULL) {
452 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
453 res = 1;
454 }
455 spin_unlock(&clp->cl_lock);
456 return res;
457}