IPVS: Change of socket usage to enable name space exit.
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / delegation.c
blobbbbc6bf5cb2e42a8b1440c31eafa2e0f2c0c5a77
1 /*
2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
8 */
9 #include <linux/completion.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
16 #include <linux/nfs4.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_xdr.h>
20 #include "nfs4_fs.h"
21 #include "delegation.h"
22 #include "internal.h"
24 static void nfs_do_free_delegation(struct nfs_delegation *delegation)
26 kfree(delegation);
29 static void nfs_free_delegation_callback(struct rcu_head *head)
31 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
33 nfs_do_free_delegation(delegation);
36 static void nfs_free_delegation(struct nfs_delegation *delegation)
38 if (delegation->cred) {
39 put_rpccred(delegation->cred);
40 delegation->cred = NULL;
42 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
45 /**
46 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
47 * @delegation: delegation to process
50 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
52 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
55 /**
56 * nfs_have_delegation - check if inode has a delegation
57 * @inode: inode to check
58 * @flags: delegation types to check for
60 * Returns one if inode has the indicated delegation, otherwise zero.
62 int nfs_have_delegation(struct inode *inode, fmode_t flags)
64 struct nfs_delegation *delegation;
65 int ret = 0;
67 flags &= FMODE_READ|FMODE_WRITE;
68 rcu_read_lock();
69 delegation = rcu_dereference(NFS_I(inode)->delegation);
70 if (delegation != NULL && (delegation->type & flags) == flags) {
71 nfs_mark_delegation_referenced(delegation);
72 ret = 1;
74 rcu_read_unlock();
75 return ret;
78 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
80 struct inode *inode = state->inode;
81 struct file_lock *fl;
82 int status = 0;
84 if (inode->i_flock == NULL)
85 goto out;
87 /* Protect inode->i_flock using the file locks lock */
88 lock_flocks();
89 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
90 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
91 continue;
92 if (nfs_file_open_context(fl->fl_file) != ctx)
93 continue;
94 unlock_flocks();
95 status = nfs4_lock_delegation_recall(state, fl);
96 if (status < 0)
97 goto out;
98 lock_flocks();
100 unlock_flocks();
101 out:
102 return status;
105 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
107 struct nfs_inode *nfsi = NFS_I(inode);
108 struct nfs_open_context *ctx;
109 struct nfs4_state *state;
110 int err;
112 again:
113 spin_lock(&inode->i_lock);
114 list_for_each_entry(ctx, &nfsi->open_files, list) {
115 state = ctx->state;
116 if (state == NULL)
117 continue;
118 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
119 continue;
120 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
121 continue;
122 get_nfs_open_context(ctx);
123 spin_unlock(&inode->i_lock);
124 err = nfs4_open_delegation_recall(ctx, state, stateid);
125 if (err >= 0)
126 err = nfs_delegation_claim_locks(ctx, state);
127 put_nfs_open_context(ctx);
128 if (err != 0)
129 return err;
130 goto again;
132 spin_unlock(&inode->i_lock);
133 return 0;
137 * nfs_inode_reclaim_delegation - process a delegation reclaim request
138 * @inode: inode to process
139 * @cred: credential to use for request
140 * @res: new delegation state from server
143 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
144 struct nfs_openres *res)
146 struct nfs_delegation *delegation;
147 struct rpc_cred *oldcred = NULL;
149 rcu_read_lock();
150 delegation = rcu_dereference(NFS_I(inode)->delegation);
151 if (delegation != NULL) {
152 spin_lock(&delegation->lock);
153 if (delegation->inode != NULL) {
154 memcpy(delegation->stateid.data, res->delegation.data,
155 sizeof(delegation->stateid.data));
156 delegation->type = res->delegation_type;
157 delegation->maxsize = res->maxsize;
158 oldcred = delegation->cred;
159 delegation->cred = get_rpccred(cred);
160 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
161 &delegation->flags);
162 NFS_I(inode)->delegation_state = delegation->type;
163 spin_unlock(&delegation->lock);
164 put_rpccred(oldcred);
165 rcu_read_unlock();
166 } else {
167 /* We appear to have raced with a delegation return. */
168 spin_unlock(&delegation->lock);
169 rcu_read_unlock();
170 nfs_inode_set_delegation(inode, cred, res);
172 } else {
173 rcu_read_unlock();
177 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
179 int res = 0;
181 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
182 nfs_free_delegation(delegation);
183 return res;
186 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
188 struct inode *inode = NULL;
190 spin_lock(&delegation->lock);
191 if (delegation->inode != NULL)
192 inode = igrab(delegation->inode);
193 spin_unlock(&delegation->lock);
194 return inode;
197 static struct nfs_delegation *
198 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
199 struct nfs_server *server)
201 struct nfs_delegation *delegation =
202 rcu_dereference_protected(nfsi->delegation,
203 lockdep_is_held(&server->nfs_client->cl_lock));
205 if (delegation == NULL)
206 goto nomatch;
208 spin_lock(&delegation->lock);
209 list_del_rcu(&delegation->super_list);
210 delegation->inode = NULL;
211 nfsi->delegation_state = 0;
212 rcu_assign_pointer(nfsi->delegation, NULL);
213 spin_unlock(&delegation->lock);
214 return delegation;
215 nomatch:
216 return NULL;
219 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
220 struct nfs_server *server)
222 struct nfs_client *clp = server->nfs_client;
223 struct nfs_delegation *delegation;
225 spin_lock(&clp->cl_lock);
226 delegation = nfs_detach_delegation_locked(nfsi, server);
227 spin_unlock(&clp->cl_lock);
228 return delegation;
232 * nfs_inode_set_delegation - set up a delegation on an inode
233 * @inode: inode to which delegation applies
234 * @cred: cred to use for subsequent delegation processing
235 * @res: new delegation state from server
237 * Returns zero on success, or a negative errno value.
239 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
241 struct nfs_server *server = NFS_SERVER(inode);
242 struct nfs_client *clp = server->nfs_client;
243 struct nfs_inode *nfsi = NFS_I(inode);
244 struct nfs_delegation *delegation, *old_delegation;
245 struct nfs_delegation *freeme = NULL;
246 int status = 0;
248 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
249 if (delegation == NULL)
250 return -ENOMEM;
251 memcpy(delegation->stateid.data, res->delegation.data,
252 sizeof(delegation->stateid.data));
253 delegation->type = res->delegation_type;
254 delegation->maxsize = res->maxsize;
255 delegation->change_attr = nfsi->change_attr;
256 delegation->cred = get_rpccred(cred);
257 delegation->inode = inode;
258 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
259 spin_lock_init(&delegation->lock);
261 spin_lock(&clp->cl_lock);
262 old_delegation = rcu_dereference_protected(nfsi->delegation,
263 lockdep_is_held(&clp->cl_lock));
264 if (old_delegation != NULL) {
265 if (memcmp(&delegation->stateid, &old_delegation->stateid,
266 sizeof(old_delegation->stateid)) == 0 &&
267 delegation->type == old_delegation->type) {
268 goto out;
271 * Deal with broken servers that hand out two
272 * delegations for the same file.
274 dfprintk(FILE, "%s: server %s handed out "
275 "a duplicate delegation!\n",
276 __func__, clp->cl_hostname);
277 if (delegation->type <= old_delegation->type) {
278 freeme = delegation;
279 delegation = NULL;
280 goto out;
282 freeme = nfs_detach_delegation_locked(nfsi, server);
284 list_add_rcu(&delegation->super_list, &server->delegations);
285 nfsi->delegation_state = delegation->type;
286 rcu_assign_pointer(nfsi->delegation, delegation);
287 delegation = NULL;
289 /* Ensure we revalidate the attributes and page cache! */
290 spin_lock(&inode->i_lock);
291 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
292 spin_unlock(&inode->i_lock);
294 out:
295 spin_unlock(&clp->cl_lock);
296 if (delegation != NULL)
297 nfs_free_delegation(delegation);
298 if (freeme != NULL)
299 nfs_do_return_delegation(inode, freeme, 0);
300 return status;
304 * Basic procedure for returning a delegation to the server
306 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
308 struct nfs_inode *nfsi = NFS_I(inode);
309 int err;
312 * Guard against new delegated open/lock/unlock calls and against
313 * state recovery
315 down_write(&nfsi->rwsem);
316 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
317 up_write(&nfsi->rwsem);
318 if (err)
319 goto out;
321 err = nfs_do_return_delegation(inode, delegation, issync);
322 out:
323 return err;
327 * nfs_client_return_marked_delegations - return previously marked delegations
328 * @clp: nfs_client to process
330 * Returns zero on success, or a negative errno value.
332 int nfs_client_return_marked_delegations(struct nfs_client *clp)
334 struct nfs_delegation *delegation;
335 struct nfs_server *server;
336 struct inode *inode;
337 int err = 0;
339 restart:
340 rcu_read_lock();
341 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
342 list_for_each_entry_rcu(delegation, &server->delegations,
343 super_list) {
344 if (!test_and_clear_bit(NFS_DELEGATION_RETURN,
345 &delegation->flags))
346 continue;
347 inode = nfs_delegation_grab_inode(delegation);
348 if (inode == NULL)
349 continue;
350 delegation = nfs_detach_delegation(NFS_I(inode),
351 server);
352 rcu_read_unlock();
354 if (delegation != NULL) {
355 filemap_flush(inode->i_mapping);
356 err = __nfs_inode_return_delegation(inode,
357 delegation, 0);
359 iput(inode);
360 if (!err)
361 goto restart;
362 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
363 return err;
366 rcu_read_unlock();
367 return 0;
371 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
372 * @inode: inode to process
374 * Does not protect against delegation reclaims, therefore really only safe
375 * to be called from nfs4_clear_inode().
377 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
379 struct nfs_server *server = NFS_SERVER(inode);
380 struct nfs_inode *nfsi = NFS_I(inode);
381 struct nfs_delegation *delegation;
383 if (rcu_access_pointer(nfsi->delegation) != NULL) {
384 delegation = nfs_detach_delegation(nfsi, server);
385 if (delegation != NULL)
386 nfs_do_return_delegation(inode, delegation, 0);
391 * nfs_inode_return_delegation - synchronously return a delegation
392 * @inode: inode to process
394 * Returns zero on success, or a negative errno value.
396 int nfs_inode_return_delegation(struct inode *inode)
398 struct nfs_server *server = NFS_SERVER(inode);
399 struct nfs_inode *nfsi = NFS_I(inode);
400 struct nfs_delegation *delegation;
401 int err = 0;
403 if (rcu_access_pointer(nfsi->delegation) != NULL) {
404 delegation = nfs_detach_delegation(nfsi, server);
405 if (delegation != NULL) {
406 nfs_wb_all(inode);
407 err = __nfs_inode_return_delegation(inode, delegation, 1);
410 return err;
413 static void nfs_mark_return_delegation(struct nfs_delegation *delegation)
415 struct nfs_client *clp = NFS_SERVER(delegation->inode)->nfs_client;
417 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
418 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
422 * nfs_super_return_all_delegations - return delegations for one superblock
423 * @sb: sb to process
426 void nfs_super_return_all_delegations(struct super_block *sb)
428 struct nfs_server *server = NFS_SB(sb);
429 struct nfs_client *clp = server->nfs_client;
430 struct nfs_delegation *delegation;
432 if (clp == NULL)
433 return;
435 rcu_read_lock();
436 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
437 spin_lock(&delegation->lock);
438 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
439 spin_unlock(&delegation->lock);
441 rcu_read_unlock();
443 if (nfs_client_return_marked_delegations(clp) != 0)
444 nfs4_schedule_state_manager(clp);
447 static void nfs_mark_return_all_delegation_types(struct nfs_server *server,
448 fmode_t flags)
450 struct nfs_delegation *delegation;
452 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
453 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
454 continue;
455 if (delegation->type & flags)
456 nfs_mark_return_delegation(delegation);
460 static void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp,
461 fmode_t flags)
463 struct nfs_server *server;
465 rcu_read_lock();
466 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
467 nfs_mark_return_all_delegation_types(server, flags);
468 rcu_read_unlock();
471 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
473 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
476 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
478 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
479 nfs4_schedule_state_manager(clp);
483 * nfs_expire_all_delegation_types
484 * @clp: client to process
485 * @flags: delegation types to expire
488 void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
490 nfs_client_mark_return_all_delegation_types(clp, flags);
491 nfs_delegation_run_state_manager(clp);
495 * nfs_expire_all_delegations
496 * @clp: client to process
499 void nfs_expire_all_delegations(struct nfs_client *clp)
501 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
505 * nfs_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
506 * @clp: client to process
509 void nfs_handle_cb_pathdown(struct nfs_client *clp)
511 if (clp == NULL)
512 return;
513 nfs_client_mark_return_all_delegations(clp);
516 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
518 struct nfs_delegation *delegation;
520 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
521 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
522 continue;
523 nfs_mark_return_delegation(delegation);
528 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
529 * @clp: nfs_client to process
532 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
534 struct nfs_server *server;
536 rcu_read_lock();
537 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
538 nfs_mark_return_unreferenced_delegations(server);
539 rcu_read_unlock();
541 nfs_delegation_run_state_manager(clp);
545 * nfs_async_inode_return_delegation - asynchronously return a delegation
546 * @inode: inode to process
547 * @stateid: state ID information from CB_RECALL arguments
549 * Returns zero on success, or a negative errno value.
551 int nfs_async_inode_return_delegation(struct inode *inode,
552 const nfs4_stateid *stateid)
554 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
555 struct nfs_delegation *delegation;
557 rcu_read_lock();
558 delegation = rcu_dereference(NFS_I(inode)->delegation);
560 if (!clp->cl_mvops->validate_stateid(delegation, stateid)) {
561 rcu_read_unlock();
562 return -ENOENT;
564 nfs_mark_return_delegation(delegation);
565 rcu_read_unlock();
567 nfs_delegation_run_state_manager(clp);
568 return 0;
571 static struct inode *
572 nfs_delegation_find_inode_server(struct nfs_server *server,
573 const struct nfs_fh *fhandle)
575 struct nfs_delegation *delegation;
576 struct inode *res = NULL;
578 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
579 spin_lock(&delegation->lock);
580 if (delegation->inode != NULL &&
581 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
582 res = igrab(delegation->inode);
584 spin_unlock(&delegation->lock);
585 if (res != NULL)
586 break;
588 return res;
592 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
593 * @clp: client state handle
594 * @fhandle: filehandle from a delegation recall
596 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
597 * cannot be found.
599 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
600 const struct nfs_fh *fhandle)
602 struct nfs_server *server;
603 struct inode *res = NULL;
605 rcu_read_lock();
606 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
607 res = nfs_delegation_find_inode_server(server, fhandle);
608 if (res != NULL)
609 break;
611 rcu_read_unlock();
612 return res;
615 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
617 struct nfs_delegation *delegation;
619 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
620 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
624 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
625 * @clp: nfs_client to process
628 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
630 struct nfs_server *server;
632 rcu_read_lock();
633 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
634 nfs_delegation_mark_reclaim_server(server);
635 rcu_read_unlock();
639 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
640 * @clp: nfs_client to process
643 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
645 struct nfs_delegation *delegation;
646 struct nfs_server *server;
647 struct inode *inode;
649 restart:
650 rcu_read_lock();
651 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
652 list_for_each_entry_rcu(delegation, &server->delegations,
653 super_list) {
654 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
655 &delegation->flags) == 0)
656 continue;
657 inode = nfs_delegation_grab_inode(delegation);
658 if (inode == NULL)
659 continue;
660 delegation = nfs_detach_delegation(NFS_I(inode),
661 server);
662 rcu_read_unlock();
664 if (delegation != NULL)
665 nfs_free_delegation(delegation);
666 iput(inode);
667 goto restart;
670 rcu_read_unlock();
674 * nfs_delegations_present - check for existence of delegations
675 * @clp: client state handle
677 * Returns one if there are any nfs_delegation structures attached
678 * to this nfs_client.
680 int nfs_delegations_present(struct nfs_client *clp)
682 struct nfs_server *server;
683 int ret = 0;
685 rcu_read_lock();
686 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
687 if (!list_empty(&server->delegations)) {
688 ret = 1;
689 break;
691 rcu_read_unlock();
692 return ret;
696 * nfs4_copy_delegation_stateid - Copy inode's state ID information
697 * @dst: stateid data structure to fill in
698 * @inode: inode to check
700 * Returns one and fills in "dst->data" * if inode had a delegation,
701 * otherwise zero is returned.
703 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
705 struct nfs_inode *nfsi = NFS_I(inode);
706 struct nfs_delegation *delegation;
707 int ret = 0;
709 rcu_read_lock();
710 delegation = rcu_dereference(nfsi->delegation);
711 if (delegation != NULL) {
712 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
713 ret = 1;
715 rcu_read_unlock();
716 return ret;