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