]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/fs/nfs/delegation.c | |
3 | * | |
4 | * Copyright (C) 2004 Trond Myklebust | |
5 | * | |
6 | * NFS file delegation management | |
7 | * | |
8 | */ | |
1da177e4 | 9 | #include <linux/completion.h> |
58d9714a | 10 | #include <linux/kthread.h> |
1da177e4 LT |
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 | ||
4ce79717 | 19 | #include "nfs4_fs.h" |
1da177e4 | 20 | #include "delegation.h" |
24c8dbbb | 21 | #include "internal.h" |
1da177e4 | 22 | |
905f8d16 | 23 | static void nfs_do_free_delegation(struct nfs_delegation *delegation) |
1da177e4 | 24 | { |
1da177e4 LT |
25 | kfree(delegation); |
26 | } | |
27 | ||
8383e460 TM |
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 | ||
905f8d16 TM |
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); | |
8383e460 TM |
44 | } |
45 | ||
888e694c TM |
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 != 0; fl = fl->fl_next) { | |
53 | if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK))) | |
54 | continue; | |
55 | if ((struct nfs_open_context *)fl->fl_file->private_data != 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 | __FUNCTION__, status); | |
64 | case -NFS4ERR_EXPIRED: | |
65 | /* kill_proc(fl->fl_pid, SIGLOST, 1); */ | |
66 | case -NFS4ERR_STALE_CLIENTID: | |
7539bbab | 67 | nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client); |
888e694c TM |
68 | goto out_err; |
69 | } | |
70 | } | |
71 | return 0; | |
72 | out_err: | |
73 | return status; | |
74 | } | |
75 | ||
90163027 | 76 | static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid) |
1da177e4 LT |
77 | { |
78 | struct nfs_inode *nfsi = NFS_I(inode); | |
79 | struct nfs_open_context *ctx; | |
80 | struct nfs4_state *state; | |
888e694c | 81 | int err; |
1da177e4 LT |
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; | |
90163027 TM |
91 | if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0) |
92 | continue; | |
1da177e4 LT |
93 | get_nfs_open_context(ctx); |
94 | spin_unlock(&inode->i_lock); | |
13437e12 | 95 | err = nfs4_open_delegation_recall(ctx, state, stateid); |
888e694c TM |
96 | if (err >= 0) |
97 | err = nfs_delegation_claim_locks(ctx, state); | |
1da177e4 | 98 | put_nfs_open_context(ctx); |
888e694c TM |
99 | if (err != 0) |
100 | return; | |
1da177e4 LT |
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 | ||
113 | if (delegation == NULL) | |
114 | return; | |
115 | memcpy(delegation->stateid.data, res->delegation.data, | |
116 | sizeof(delegation->stateid.data)); | |
117 | delegation->type = res->delegation_type; | |
118 | delegation->maxsize = res->maxsize; | |
119 | put_rpccred(cred); | |
120 | delegation->cred = get_rpccred(cred); | |
121 | delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM; | |
122 | NFS_I(inode)->delegation_state = delegation->type; | |
123 | smp_wmb(); | |
124 | } | |
125 | ||
126 | /* | |
127 | * Set up a delegation on an inode | |
128 | */ | |
129 | int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) | |
130 | { | |
7539bbab | 131 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
1da177e4 LT |
132 | struct nfs_inode *nfsi = NFS_I(inode); |
133 | struct nfs_delegation *delegation; | |
134 | int status = 0; | |
135 | ||
f52720ca | 136 | delegation = kmalloc(sizeof(*delegation), GFP_KERNEL); |
1da177e4 LT |
137 | if (delegation == NULL) |
138 | return -ENOMEM; | |
139 | memcpy(delegation->stateid.data, res->delegation.data, | |
140 | sizeof(delegation->stateid.data)); | |
141 | delegation->type = res->delegation_type; | |
142 | delegation->maxsize = res->maxsize; | |
beb2a5ec | 143 | delegation->change_attr = nfsi->change_attr; |
1da177e4 LT |
144 | delegation->cred = get_rpccred(cred); |
145 | delegation->inode = inode; | |
146 | ||
147 | spin_lock(&clp->cl_lock); | |
8383e460 TM |
148 | if (rcu_dereference(nfsi->delegation) == NULL) { |
149 | list_add_rcu(&delegation->super_list, &clp->cl_delegations); | |
1da177e4 | 150 | nfsi->delegation_state = delegation->type; |
8383e460 | 151 | rcu_assign_pointer(nfsi->delegation, delegation); |
1da177e4 LT |
152 | delegation = NULL; |
153 | } else { | |
154 | if (memcmp(&delegation->stateid, &nfsi->delegation->stateid, | |
155 | sizeof(delegation->stateid)) != 0 || | |
156 | delegation->type != nfsi->delegation->type) { | |
157 | printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n", | |
24c8dbbb | 158 | __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr)); |
1da177e4 LT |
159 | status = -EIO; |
160 | } | |
161 | } | |
412c77ce TM |
162 | |
163 | /* Ensure we revalidate the attributes and page cache! */ | |
164 | spin_lock(&inode->i_lock); | |
165 | nfsi->cache_validity |= NFS_INO_REVAL_FORCED; | |
166 | spin_unlock(&inode->i_lock); | |
167 | ||
1da177e4 | 168 | spin_unlock(&clp->cl_lock); |
f99d49ad | 169 | kfree(delegation); |
1da177e4 LT |
170 | return status; |
171 | } | |
172 | ||
173 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation) | |
174 | { | |
175 | int res = 0; | |
176 | ||
1da177e4 | 177 | res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid); |
905f8d16 | 178 | nfs_free_delegation(delegation); |
1da177e4 LT |
179 | return res; |
180 | } | |
181 | ||
182 | /* Sync all data to disk upon delegation return */ | |
183 | static void nfs_msync_inode(struct inode *inode) | |
184 | { | |
185 | filemap_fdatawrite(inode->i_mapping); | |
186 | nfs_wb_all(inode); | |
187 | filemap_fdatawait(inode->i_mapping); | |
188 | } | |
189 | ||
190 | /* | |
191 | * Basic procedure for returning a delegation to the server | |
192 | */ | |
90163027 | 193 | static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation) |
1da177e4 | 194 | { |
7539bbab | 195 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
1da177e4 | 196 | struct nfs_inode *nfsi = NFS_I(inode); |
1da177e4 LT |
197 | |
198 | nfs_msync_inode(inode); | |
199 | down_read(&clp->cl_sem); | |
200 | /* Guard against new delegated open calls */ | |
201 | down_write(&nfsi->rwsem); | |
90163027 | 202 | nfs_delegation_claim_opens(inode, &delegation->stateid); |
1da177e4 LT |
203 | up_write(&nfsi->rwsem); |
204 | up_read(&clp->cl_sem); | |
205 | nfs_msync_inode(inode); | |
206 | ||
90163027 TM |
207 | return nfs_do_return_delegation(inode, delegation); |
208 | } | |
209 | ||
210 | static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid) | |
211 | { | |
8383e460 | 212 | struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); |
90163027 TM |
213 | |
214 | if (delegation == NULL) | |
215 | goto nomatch; | |
216 | if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data, | |
217 | sizeof(delegation->stateid.data)) != 0) | |
218 | goto nomatch; | |
8383e460 | 219 | list_del_rcu(&delegation->super_list); |
90163027 | 220 | nfsi->delegation_state = 0; |
8383e460 | 221 | rcu_assign_pointer(nfsi->delegation, NULL); |
90163027 TM |
222 | return delegation; |
223 | nomatch: | |
224 | return NULL; | |
225 | } | |
226 | ||
227 | int nfs_inode_return_delegation(struct inode *inode) | |
228 | { | |
229 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; | |
230 | struct nfs_inode *nfsi = NFS_I(inode); | |
231 | struct nfs_delegation *delegation; | |
232 | int err = 0; | |
233 | ||
8383e460 | 234 | if (rcu_dereference(nfsi->delegation) != NULL) { |
90163027 TM |
235 | spin_lock(&clp->cl_lock); |
236 | delegation = nfs_detach_delegation_locked(nfsi, NULL); | |
237 | spin_unlock(&clp->cl_lock); | |
238 | if (delegation != NULL) | |
239 | err = __nfs_inode_return_delegation(inode, delegation); | |
240 | } | |
241 | return err; | |
1da177e4 LT |
242 | } |
243 | ||
244 | /* | |
245 | * Return all delegations associated to a super block | |
246 | */ | |
247 | void nfs_return_all_delegations(struct super_block *sb) | |
248 | { | |
7539bbab | 249 | struct nfs_client *clp = NFS_SB(sb)->nfs_client; |
1da177e4 LT |
250 | struct nfs_delegation *delegation; |
251 | struct inode *inode; | |
252 | ||
253 | if (clp == NULL) | |
254 | return; | |
255 | restart: | |
8383e460 TM |
256 | rcu_read_lock(); |
257 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { | |
1da177e4 LT |
258 | if (delegation->inode->i_sb != sb) |
259 | continue; | |
260 | inode = igrab(delegation->inode); | |
261 | if (inode == NULL) | |
262 | continue; | |
8383e460 TM |
263 | spin_lock(&clp->cl_lock); |
264 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); | |
1da177e4 | 265 | spin_unlock(&clp->cl_lock); |
8383e460 TM |
266 | rcu_read_unlock(); |
267 | if (delegation != NULL) | |
268 | __nfs_inode_return_delegation(inode, delegation); | |
1da177e4 LT |
269 | iput(inode); |
270 | goto restart; | |
271 | } | |
8383e460 | 272 | rcu_read_unlock(); |
1da177e4 LT |
273 | } |
274 | ||
10afec90 | 275 | static int nfs_do_expire_all_delegations(void *ptr) |
58d9714a | 276 | { |
adfa6f98 | 277 | struct nfs_client *clp = ptr; |
58d9714a TM |
278 | struct nfs_delegation *delegation; |
279 | struct inode *inode; | |
58d9714a TM |
280 | |
281 | allow_signal(SIGKILL); | |
282 | restart: | |
58d9714a TM |
283 | if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0) |
284 | goto out; | |
285 | if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0) | |
286 | goto out; | |
8383e460 TM |
287 | rcu_read_lock(); |
288 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { | |
58d9714a TM |
289 | inode = igrab(delegation->inode); |
290 | if (inode == NULL) | |
291 | continue; | |
8383e460 TM |
292 | spin_lock(&clp->cl_lock); |
293 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); | |
58d9714a | 294 | spin_unlock(&clp->cl_lock); |
8383e460 TM |
295 | rcu_read_unlock(); |
296 | if (delegation) | |
297 | __nfs_inode_return_delegation(inode, delegation); | |
58d9714a | 298 | iput(inode); |
26c78e15 | 299 | goto restart; |
58d9714a | 300 | } |
8383e460 | 301 | rcu_read_unlock(); |
58d9714a | 302 | out: |
24c8dbbb | 303 | nfs_put_client(clp); |
58d9714a TM |
304 | module_put_and_exit(0); |
305 | } | |
306 | ||
adfa6f98 | 307 | void nfs_expire_all_delegations(struct nfs_client *clp) |
58d9714a TM |
308 | { |
309 | struct task_struct *task; | |
310 | ||
311 | __module_get(THIS_MODULE); | |
312 | atomic_inc(&clp->cl_count); | |
313 | task = kthread_run(nfs_do_expire_all_delegations, clp, | |
314 | "%u.%u.%u.%u-delegreturn", | |
24c8dbbb | 315 | NIPQUAD(clp->cl_addr.sin_addr)); |
58d9714a TM |
316 | if (!IS_ERR(task)) |
317 | return; | |
24c8dbbb | 318 | nfs_put_client(clp); |
58d9714a TM |
319 | module_put(THIS_MODULE); |
320 | } | |
321 | ||
1da177e4 LT |
322 | /* |
323 | * Return all delegations following an NFS4ERR_CB_PATH_DOWN error. | |
324 | */ | |
adfa6f98 | 325 | void nfs_handle_cb_pathdown(struct nfs_client *clp) |
1da177e4 LT |
326 | { |
327 | struct nfs_delegation *delegation; | |
328 | struct inode *inode; | |
329 | ||
330 | if (clp == NULL) | |
331 | return; | |
332 | restart: | |
8383e460 TM |
333 | rcu_read_lock(); |
334 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { | |
1da177e4 LT |
335 | inode = igrab(delegation->inode); |
336 | if (inode == NULL) | |
337 | continue; | |
8383e460 TM |
338 | spin_lock(&clp->cl_lock); |
339 | delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL); | |
1da177e4 | 340 | spin_unlock(&clp->cl_lock); |
8383e460 TM |
341 | rcu_read_unlock(); |
342 | if (delegation != NULL) | |
343 | __nfs_inode_return_delegation(inode, delegation); | |
1da177e4 LT |
344 | iput(inode); |
345 | goto restart; | |
346 | } | |
8383e460 | 347 | rcu_read_unlock(); |
1da177e4 LT |
348 | } |
349 | ||
350 | struct recall_threadargs { | |
351 | struct inode *inode; | |
adfa6f98 | 352 | struct nfs_client *clp; |
1da177e4 LT |
353 | const nfs4_stateid *stateid; |
354 | ||
355 | struct completion started; | |
356 | int result; | |
357 | }; | |
358 | ||
359 | static int recall_thread(void *data) | |
360 | { | |
361 | struct recall_threadargs *args = (struct recall_threadargs *)data; | |
362 | struct inode *inode = igrab(args->inode); | |
7539bbab | 363 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
1da177e4 LT |
364 | struct nfs_inode *nfsi = NFS_I(inode); |
365 | struct nfs_delegation *delegation; | |
366 | ||
367 | daemonize("nfsv4-delegreturn"); | |
368 | ||
369 | nfs_msync_inode(inode); | |
370 | down_read(&clp->cl_sem); | |
371 | down_write(&nfsi->rwsem); | |
372 | spin_lock(&clp->cl_lock); | |
90163027 TM |
373 | delegation = nfs_detach_delegation_locked(nfsi, args->stateid); |
374 | if (delegation != NULL) | |
1da177e4 | 375 | args->result = 0; |
90163027 | 376 | else |
1da177e4 | 377 | args->result = -ENOENT; |
1da177e4 LT |
378 | spin_unlock(&clp->cl_lock); |
379 | complete(&args->started); | |
90163027 | 380 | nfs_delegation_claim_opens(inode, args->stateid); |
1da177e4 LT |
381 | up_write(&nfsi->rwsem); |
382 | up_read(&clp->cl_sem); | |
383 | nfs_msync_inode(inode); | |
384 | ||
385 | if (delegation != NULL) | |
386 | nfs_do_return_delegation(inode, delegation); | |
387 | iput(inode); | |
388 | module_put_and_exit(0); | |
389 | } | |
390 | ||
391 | /* | |
392 | * Asynchronous delegation recall! | |
393 | */ | |
394 | int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid) | |
395 | { | |
396 | struct recall_threadargs data = { | |
397 | .inode = inode, | |
398 | .stateid = stateid, | |
399 | }; | |
400 | int status; | |
401 | ||
402 | init_completion(&data.started); | |
403 | __module_get(THIS_MODULE); | |
404 | status = kernel_thread(recall_thread, &data, CLONE_KERNEL); | |
405 | if (status < 0) | |
406 | goto out_module_put; | |
407 | wait_for_completion(&data.started); | |
408 | return data.result; | |
409 | out_module_put: | |
410 | module_put(THIS_MODULE); | |
411 | return status; | |
412 | } | |
413 | ||
414 | /* | |
415 | * Retrieve the inode associated with a delegation | |
416 | */ | |
adfa6f98 | 417 | struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle) |
1da177e4 LT |
418 | { |
419 | struct nfs_delegation *delegation; | |
420 | struct inode *res = NULL; | |
8383e460 TM |
421 | rcu_read_lock(); |
422 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { | |
1da177e4 LT |
423 | if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { |
424 | res = igrab(delegation->inode); | |
425 | break; | |
426 | } | |
427 | } | |
8383e460 | 428 | rcu_read_unlock(); |
1da177e4 LT |
429 | return res; |
430 | } | |
431 | ||
432 | /* | |
433 | * Mark all delegations as needing to be reclaimed | |
434 | */ | |
adfa6f98 | 435 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) |
1da177e4 LT |
436 | { |
437 | struct nfs_delegation *delegation; | |
8383e460 TM |
438 | rcu_read_lock(); |
439 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) | |
1da177e4 | 440 | delegation->flags |= NFS_DELEGATION_NEED_RECLAIM; |
8383e460 | 441 | rcu_read_unlock(); |
1da177e4 LT |
442 | } |
443 | ||
444 | /* | |
445 | * Reap all unclaimed delegations after reboot recovery is done | |
446 | */ | |
adfa6f98 | 447 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) |
1da177e4 | 448 | { |
8383e460 TM |
449 | struct nfs_delegation *delegation; |
450 | restart: | |
451 | rcu_read_lock(); | |
452 | list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) { | |
1da177e4 LT |
453 | if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0) |
454 | continue; | |
8383e460 TM |
455 | spin_lock(&clp->cl_lock); |
456 | delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL); | |
457 | spin_unlock(&clp->cl_lock); | |
458 | rcu_read_unlock(); | |
459 | if (delegation != NULL) | |
905f8d16 | 460 | nfs_free_delegation(delegation); |
8383e460 | 461 | goto restart; |
1da177e4 | 462 | } |
8383e460 | 463 | rcu_read_unlock(); |
1da177e4 | 464 | } |
3e4f6290 TM |
465 | |
466 | int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode) | |
467 | { | |
3e4f6290 TM |
468 | struct nfs_inode *nfsi = NFS_I(inode); |
469 | struct nfs_delegation *delegation; | |
8383e460 | 470 | int ret = 0; |
3e4f6290 | 471 | |
8383e460 TM |
472 | rcu_read_lock(); |
473 | delegation = rcu_dereference(nfsi->delegation); | |
3e4f6290 TM |
474 | if (delegation != NULL) { |
475 | memcpy(dst->data, delegation->stateid.data, sizeof(dst->data)); | |
8383e460 | 476 | ret = 1; |
3e4f6290 | 477 | } |
8383e460 TM |
478 | rcu_read_unlock(); |
479 | return ret; | |
3e4f6290 | 480 | } |