NFSv4: Remove nfs_client->cl_sem
[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_do_free_delegation(struct nfs_delegation *delegation)
24 {
25         kfree(delegation);
26 }
27
28 static void nfs_free_delegation_callback(struct rcu_head *head)
29 {
30         struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
31
32         nfs_do_free_delegation(delegation);
33 }
34
35 static void nfs_free_delegation(struct nfs_delegation *delegation)
36 {
37         struct rpc_cred *cred;
38
39         cred = rcu_dereference(delegation->cred);
40         rcu_assign_pointer(delegation->cred, NULL);
41         call_rcu(&delegation->rcu, nfs_free_delegation_callback);
42         if (cred)
43                 put_rpccred(cred);
44 }
45
46 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
47 {
48         struct inode *inode = state->inode;
49         struct file_lock *fl;
50         int status;
51
52         for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
53                 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
54                         continue;
55                 if (nfs_file_open_context(fl->fl_file) != ctx)
56                         continue;
57                 status = nfs4_lock_delegation_recall(state, fl);
58                 if (status >= 0)
59                         continue;
60                 switch (status) {
61                         default:
62                                 printk(KERN_ERR "%s: unhandled error %d.\n",
63                                                 __func__, status);
64                         case -NFS4ERR_EXPIRED:
65                                 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
66                         case -NFS4ERR_STALE_CLIENTID:
67                                 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
68                                 goto out_err;
69                 }
70         }
71         return 0;
72 out_err:
73         return status;
74 }
75
76 static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
77 {
78         struct nfs_inode *nfsi = NFS_I(inode);
79         struct nfs_open_context *ctx;
80         struct nfs4_state *state;
81         int err;
82
83 again:
84         spin_lock(&inode->i_lock);
85         list_for_each_entry(ctx, &nfsi->open_files, list) {
86                 state = ctx->state;
87                 if (state == NULL)
88                         continue;
89                 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
90                         continue;
91                 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
92                         continue;
93                 get_nfs_open_context(ctx);
94                 spin_unlock(&inode->i_lock);
95                 err = nfs4_open_delegation_recall(ctx, state, stateid);
96                 if (err >= 0)
97                         err = nfs_delegation_claim_locks(ctx, state);
98                 put_nfs_open_context(ctx);
99                 if (err != 0)
100                         return;
101                 goto again;
102         }
103         spin_unlock(&inode->i_lock);
104 }
105
106 /*
107  * Set up a delegation on an inode
108  */
109 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
110 {
111         struct nfs_delegation *delegation = NFS_I(inode)->delegation;
112         struct rpc_cred *oldcred;
113
114         if (delegation == NULL)
115                 return;
116         memcpy(delegation->stateid.data, res->delegation.data,
117                         sizeof(delegation->stateid.data));
118         delegation->type = res->delegation_type;
119         delegation->maxsize = res->maxsize;
120         oldcred = delegation->cred;
121         delegation->cred = get_rpccred(cred);
122         clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
123         NFS_I(inode)->delegation_state = delegation->type;
124         smp_wmb();
125         put_rpccred(oldcred);
126 }
127
128 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
129 {
130         int res = 0;
131
132         res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
133         nfs_free_delegation(delegation);
134         return res;
135 }
136
137 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
138 {
139         struct inode *inode = NULL;
140
141         spin_lock(&delegation->lock);
142         if (delegation->inode != NULL)
143                 inode = igrab(delegation->inode);
144         spin_unlock(&delegation->lock);
145         return inode;
146 }
147
148 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
149 {
150         struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
151
152         if (delegation == NULL)
153                 goto nomatch;
154         spin_lock(&delegation->lock);
155         if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
156                                 sizeof(delegation->stateid.data)) != 0)
157                 goto nomatch_unlock;
158         list_del_rcu(&delegation->super_list);
159         delegation->inode = NULL;
160         nfsi->delegation_state = 0;
161         rcu_assign_pointer(nfsi->delegation, NULL);
162         spin_unlock(&delegation->lock);
163         return delegation;
164 nomatch_unlock:
165         spin_unlock(&delegation->lock);
166 nomatch:
167         return NULL;
168 }
169
170 /*
171  * Set up a delegation on an inode
172  */
173 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
174 {
175         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
176         struct nfs_inode *nfsi = NFS_I(inode);
177         struct nfs_delegation *delegation;
178         struct nfs_delegation *freeme = NULL;
179         int status = 0;
180
181         delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
182         if (delegation == NULL)
183                 return -ENOMEM;
184         memcpy(delegation->stateid.data, res->delegation.data,
185                         sizeof(delegation->stateid.data));
186         delegation->type = res->delegation_type;
187         delegation->maxsize = res->maxsize;
188         delegation->change_attr = nfsi->change_attr;
189         delegation->cred = get_rpccred(cred);
190         delegation->inode = inode;
191         spin_lock_init(&delegation->lock);
192
193         spin_lock(&clp->cl_lock);
194         if (rcu_dereference(nfsi->delegation) != NULL) {
195                 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
196                                         sizeof(delegation->stateid)) == 0 &&
197                                 delegation->type == nfsi->delegation->type) {
198                         goto out;
199                 }
200                 /*
201                  * Deal with broken servers that hand out two
202                  * delegations for the same file.
203                  */
204                 dfprintk(FILE, "%s: server %s handed out "
205                                 "a duplicate delegation!\n",
206                                 __func__, clp->cl_hostname);
207                 if (delegation->type <= nfsi->delegation->type) {
208                         freeme = delegation;
209                         delegation = NULL;
210                         goto out;
211                 }
212                 freeme = nfs_detach_delegation_locked(nfsi, NULL);
213         }
214         list_add_rcu(&delegation->super_list, &clp->cl_delegations);
215         nfsi->delegation_state = delegation->type;
216         rcu_assign_pointer(nfsi->delegation, delegation);
217         delegation = NULL;
218
219         /* Ensure we revalidate the attributes and page cache! */
220         spin_lock(&inode->i_lock);
221         nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
222         spin_unlock(&inode->i_lock);
223
224 out:
225         spin_unlock(&clp->cl_lock);
226         if (delegation != NULL)
227                 nfs_free_delegation(delegation);
228         if (freeme != NULL)
229                 nfs_do_return_delegation(inode, freeme, 0);
230         return status;
231 }
232
233 /* Sync all data to disk upon delegation return */
234 static void nfs_msync_inode(struct inode *inode)
235 {
236         filemap_fdatawrite(inode->i_mapping);
237         nfs_wb_all(inode);
238         filemap_fdatawait(inode->i_mapping);
239 }
240
241 /*
242  * Basic procedure for returning a delegation to the server
243  */
244 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
245 {
246         struct nfs_inode *nfsi = NFS_I(inode);
247
248         nfs_msync_inode(inode);
249         /* Guard against new delegated open calls */
250         down_write(&nfsi->rwsem);
251         nfs_delegation_claim_opens(inode, &delegation->stateid);
252         up_write(&nfsi->rwsem);
253         nfs_msync_inode(inode);
254
255         return nfs_do_return_delegation(inode, delegation, 1);
256 }
257
258 /*
259  * This function returns the delegation without reclaiming opens
260  * or protecting against delegation reclaims.
261  * It is therefore really only safe to be called from
262  * nfs4_clear_inode()
263  */
264 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
265 {
266         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
267         struct nfs_inode *nfsi = NFS_I(inode);
268         struct nfs_delegation *delegation;
269
270         if (rcu_dereference(nfsi->delegation) != NULL) {
271                 spin_lock(&clp->cl_lock);
272                 delegation = nfs_detach_delegation_locked(nfsi, NULL);
273                 spin_unlock(&clp->cl_lock);
274                 if (delegation != NULL)
275                         nfs_do_return_delegation(inode, delegation, 0);
276         }
277 }
278
279 int nfs_inode_return_delegation(struct inode *inode)
280 {
281         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
282         struct nfs_inode *nfsi = NFS_I(inode);
283         struct nfs_delegation *delegation;
284         int err = 0;
285
286         if (rcu_dereference(nfsi->delegation) != NULL) {
287                 spin_lock(&clp->cl_lock);
288                 delegation = nfs_detach_delegation_locked(nfsi, NULL);
289                 spin_unlock(&clp->cl_lock);
290                 if (delegation != NULL)
291                         err = __nfs_inode_return_delegation(inode, delegation);
292         }
293         return err;
294 }
295
296 /*
297  * Return all delegations associated to a super block
298  */
299 void nfs_return_all_delegations(struct super_block *sb)
300 {
301         struct nfs_client *clp = NFS_SB(sb)->nfs_client;
302         struct nfs_delegation *delegation;
303         struct inode *inode;
304
305         if (clp == NULL)
306                 return;
307 restart:
308         rcu_read_lock();
309         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
310                 inode = NULL;
311                 spin_lock(&delegation->lock);
312                 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
313                         inode = igrab(delegation->inode);
314                 spin_unlock(&delegation->lock);
315                 if (inode == NULL)
316                         continue;
317                 spin_lock(&clp->cl_lock);
318                 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
319                 spin_unlock(&clp->cl_lock);
320                 rcu_read_unlock();
321                 if (delegation != NULL)
322                         __nfs_inode_return_delegation(inode, delegation);
323                 iput(inode);
324                 goto restart;
325         }
326         rcu_read_unlock();
327 }
328
329 static int nfs_do_expire_all_delegations(void *ptr)
330 {
331         struct nfs_client *clp = ptr;
332         struct nfs_delegation *delegation;
333         struct inode *inode;
334
335         allow_signal(SIGKILL);
336 restart:
337         if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
338                 goto out;
339         if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
340                 goto out;
341         rcu_read_lock();
342         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
343                 inode = nfs_delegation_grab_inode(delegation);
344                 if (inode == NULL)
345                         continue;
346                 spin_lock(&clp->cl_lock);
347                 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
348                 spin_unlock(&clp->cl_lock);
349                 rcu_read_unlock();
350                 if (delegation)
351                         __nfs_inode_return_delegation(inode, delegation);
352                 iput(inode);
353                 goto restart;
354         }
355         rcu_read_unlock();
356 out:
357         nfs_put_client(clp);
358         module_put_and_exit(0);
359 }
360
361 void nfs_expire_all_delegations(struct nfs_client *clp)
362 {
363         struct task_struct *task;
364
365         __module_get(THIS_MODULE);
366         atomic_inc(&clp->cl_count);
367         task = kthread_run(nfs_do_expire_all_delegations, clp,
368                                 "%s-delegreturn",
369                                 rpc_peeraddr2str(clp->cl_rpcclient,
370                                                         RPC_DISPLAY_ADDR));
371         if (!IS_ERR(task))
372                 return;
373         nfs_put_client(clp);
374         module_put(THIS_MODULE);
375 }
376
377 /*
378  * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
379  */
380 void nfs_handle_cb_pathdown(struct nfs_client *clp)
381 {
382         struct nfs_delegation *delegation;
383         struct inode *inode;
384
385         if (clp == NULL)
386                 return;
387 restart:
388         rcu_read_lock();
389         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
390                 inode = nfs_delegation_grab_inode(delegation);
391                 if (inode == NULL)
392                         continue;
393                 spin_lock(&clp->cl_lock);
394                 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
395                 spin_unlock(&clp->cl_lock);
396                 rcu_read_unlock();
397                 if (delegation != NULL)
398                         __nfs_inode_return_delegation(inode, delegation);
399                 iput(inode);
400                 goto restart;
401         }
402         rcu_read_unlock();
403 }
404
405 struct recall_threadargs {
406         struct inode *inode;
407         struct nfs_client *clp;
408         const nfs4_stateid *stateid;
409
410         struct completion started;
411         int result;
412 };
413
414 static int recall_thread(void *data)
415 {
416         struct recall_threadargs *args = (struct recall_threadargs *)data;
417         struct inode *inode = igrab(args->inode);
418         struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
419         struct nfs_inode *nfsi = NFS_I(inode);
420         struct nfs_delegation *delegation;
421
422         daemonize("nfsv4-delegreturn");
423
424         nfs_msync_inode(inode);
425         down_write(&nfsi->rwsem);
426         spin_lock(&clp->cl_lock);
427         delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
428         if (delegation != NULL)
429                 args->result = 0;
430         else
431                 args->result = -ENOENT;
432         spin_unlock(&clp->cl_lock);
433         complete(&args->started);
434         nfs_delegation_claim_opens(inode, args->stateid);
435         up_write(&nfsi->rwsem);
436         nfs_msync_inode(inode);
437
438         if (delegation != NULL)
439                 nfs_do_return_delegation(inode, delegation, 1);
440         iput(inode);
441         module_put_and_exit(0);
442 }
443
444 /*
445  * Asynchronous delegation recall!
446  */
447 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
448 {
449         struct recall_threadargs data = {
450                 .inode = inode,
451                 .stateid = stateid,
452         };
453         int status;
454
455         init_completion(&data.started);
456         __module_get(THIS_MODULE);
457         status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
458         if (status < 0)
459                 goto out_module_put;
460         wait_for_completion(&data.started);
461         return data.result;
462 out_module_put:
463         module_put(THIS_MODULE);
464         return status;
465 }
466
467 /*
468  * Retrieve the inode associated with a delegation
469  */
470 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
471 {
472         struct nfs_delegation *delegation;
473         struct inode *res = NULL;
474         rcu_read_lock();
475         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
476                 spin_lock(&delegation->lock);
477                 if (delegation->inode != NULL &&
478                     nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
479                         res = igrab(delegation->inode);
480                 }
481                 spin_unlock(&delegation->lock);
482                 if (res != NULL)
483                         break;
484         }
485         rcu_read_unlock();
486         return res;
487 }
488
489 /*
490  * Mark all delegations as needing to be reclaimed
491  */
492 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
493 {
494         struct nfs_delegation *delegation;
495         rcu_read_lock();
496         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
497                 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
498         rcu_read_unlock();
499 }
500
501 /*
502  * Reap all unclaimed delegations after reboot recovery is done
503  */
504 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
505 {
506         struct nfs_delegation *delegation;
507         struct inode *inode;
508 restart:
509         rcu_read_lock();
510         list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
511                 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
512                         continue;
513                 inode = nfs_delegation_grab_inode(delegation);
514                 if (inode == NULL)
515                         continue;
516                 spin_lock(&clp->cl_lock);
517                 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
518                 spin_unlock(&clp->cl_lock);
519                 rcu_read_unlock();
520                 if (delegation != NULL)
521                         nfs_free_delegation(delegation);
522                 iput(inode);
523                 goto restart;
524         }
525         rcu_read_unlock();
526 }
527
528 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
529 {
530         struct nfs_inode *nfsi = NFS_I(inode);
531         struct nfs_delegation *delegation;
532         int ret = 0;
533
534         rcu_read_lock();
535         delegation = rcu_dereference(nfsi->delegation);
536         if (delegation != NULL) {
537                 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
538                 ret = 1;
539         }
540         rcu_read_unlock();
541         return ret;
542 }