NFSv4: Support recalling delegations by stateid
[safe/jmp/linux-2.6] / fs / nfs / delegation.c
1 /*
2  * linux/fs/nfs/delegation.c
3  *
4  * Copyright (C) 2004 Trond Myklebust
5  *
6  * NFS file delegation management
7  *
8  */
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>
14
15 #include <linux/nfs4.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_xdr.h>
18
19 #include "nfs4_fs.h"
20 #include "delegation.h"
21 #include "internal.h"
22
23 static void nfs_free_delegation(struct nfs_delegation *delegation)
24 {
25         if (delegation->cred)
26                 put_rpccred(delegation->cred);
27         kfree(delegation);
28 }
29
30 static 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:
51                                 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
52                                 goto out_err;
53                 }
54         }
55         return 0;
56 out_err:
57         return status;
58 }
59
60 static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
61 {
62         struct nfs_inode *nfsi = NFS_I(inode);
63         struct nfs_open_context *ctx;
64         struct nfs4_state *state;
65         int err;
66
67 again:
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;
75                 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
76                         continue;
77                 get_nfs_open_context(ctx);
78                 spin_unlock(&inode->i_lock);
79                 err = nfs4_open_delegation_recall(ctx, state);
80                 if (err >= 0)
81                         err = nfs_delegation_claim_locks(ctx, state);
82                 put_nfs_open_context(ctx);
83                 if (err != 0)
84                         return;
85                 goto again;
86         }
87         spin_unlock(&inode->i_lock);
88 }
89
90 /*
91  * Set up a delegation on an inode
92  */
93 void 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  */
113 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
114 {
115         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
116         struct nfs_inode *nfsi = NFS_I(inode);
117         struct nfs_delegation *delegation;
118         int status = 0;
119
120         /* 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
124         delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
125         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;
131         delegation->change_attr = nfsi->change_attr;
132         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",
146                                         __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
147                         status = -EIO;
148                 }
149         }
150         spin_unlock(&clp->cl_lock);
151         kfree(delegation);
152         return status;
153 }
154
155 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
156 {
157         int res = 0;
158
159         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 */
165 static 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  */
175 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
176 {
177         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
178         struct nfs_inode *nfsi = NFS_I(inode);
179
180         nfs_msync_inode(inode);
181         down_read(&clp->cl_sem);
182         /* Guard against new delegated open calls */
183         down_write(&nfsi->rwsem);
184         nfs_delegation_claim_opens(inode, &delegation->stateid);
185         up_write(&nfsi->rwsem);
186         up_read(&clp->cl_sem);
187         nfs_msync_inode(inode);
188
189         return nfs_do_return_delegation(inode, delegation);
190 }
191
192 static 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;
205 nomatch:
206         return NULL;
207 }
208
209 int 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;
224 }
225
226 /*
227  * Return all delegations associated to a super block
228  */
229 void nfs_return_all_delegations(struct super_block *sb)
230 {
231         struct nfs_client *clp = NFS_SB(sb)->nfs_client;
232         struct nfs_delegation *delegation;
233         struct inode *inode;
234
235         if (clp == NULL)
236                 return;
237 restart:
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;
245                 nfs_detach_delegation_locked(NFS_I(inode), NULL);
246                 spin_unlock(&clp->cl_lock);
247                 __nfs_inode_return_delegation(inode, delegation);
248                 iput(inode);
249                 goto restart;
250         }
251         spin_unlock(&clp->cl_lock);
252 }
253
254 static int nfs_do_expire_all_delegations(void *ptr)
255 {
256         struct nfs_client *clp = ptr;
257         struct nfs_delegation *delegation;
258         struct inode *inode;
259
260         allow_signal(SIGKILL);
261 restart:
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;
271                 nfs_detach_delegation_locked(NFS_I(inode), NULL);
272                 spin_unlock(&clp->cl_lock);
273                 __nfs_inode_return_delegation(inode, delegation);
274                 iput(inode);
275                 goto restart;
276         }
277 out:
278         spin_unlock(&clp->cl_lock);
279         nfs_put_client(clp);
280         module_put_and_exit(0);
281 }
282
283 void nfs_expire_all_delegations(struct nfs_client *clp)
284 {
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",
291                         NIPQUAD(clp->cl_addr.sin_addr));
292         if (!IS_ERR(task))
293                 return;
294         nfs_put_client(clp);
295         module_put(THIS_MODULE);
296 }
297
298 /*
299  * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
300  */
301 void nfs_handle_cb_pathdown(struct nfs_client *clp)
302 {
303         struct nfs_delegation *delegation;
304         struct inode *inode;
305
306         if (clp == NULL)
307                 return;
308 restart:
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;
314                 nfs_detach_delegation_locked(NFS_I(inode), NULL);
315                 spin_unlock(&clp->cl_lock);
316                 __nfs_inode_return_delegation(inode, delegation);
317                 iput(inode);
318                 goto restart;
319         }
320         spin_unlock(&clp->cl_lock);
321 }
322
323 struct recall_threadargs {
324         struct inode *inode;
325         struct nfs_client *clp;
326         const nfs4_stateid *stateid;
327
328         struct completion started;
329         int result;
330 };
331
332 static int recall_thread(void *data)
333 {
334         struct recall_threadargs *args = (struct recall_threadargs *)data;
335         struct inode *inode = igrab(args->inode);
336         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
337         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);
346         delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
347         if (delegation != NULL)
348                 args->result = 0;
349         else
350                 args->result = -ENOENT;
351         spin_unlock(&clp->cl_lock);
352         complete(&args->started);
353         nfs_delegation_claim_opens(inode, args->stateid);
354         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  */
367 int 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;
382 out_module_put:
383         module_put(THIS_MODULE);
384         return status;
385 }
386
387 /*
388  * Retrieve the inode associated with a delegation
389  */
390 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
391 {
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  */
408 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
409 {
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  */
420 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
421 {
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 }
439
440 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
441 {
442         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
443         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 }