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