Whamcloud - gitweb
new llog_ldlm process and mgc_enqueue
[fs/lustre-release.git] / lustre / ldlm / ldlm_plain.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002, 2003 Cluster File Systems, Inc.
5  *   Author: Peter Braam <braam@clusterfs.com>
6  *   Author: Phil Schwan <phil@clusterfs.com>
7  *
8  *   This file is part of the Lustre file system, http://www.lustre.org
9  *   Lustre is a trademark of Cluster File Systems, Inc.
10  *
11  *   You may have signed or agreed to another license before downloading
12  *   this software.  If so, you are bound by the terms and conditions
13  *   of that agreement, and the following does not apply to you.  See the
14  *   LICENSE file included with this distribution for more information.
15  *
16  *   If you did not agree to a different license, then this copy of Lustre
17  *   is open source software; you can redistribute it and/or modify it
18  *   under the terms of version 2 of the GNU General Public License as
19  *   published by the Free Software Foundation.
20  *
21  *   In either case, Lustre is distributed in the hope that it will be
22  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
23  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  *   license text for more details.
25  */
26
27 #define DEBUG_SUBSYSTEM S_LDLM
28
29 #ifdef __KERNEL__
30 #include <linux/lustre_dlm.h>
31 #include <linux/obd_support.h>
32 #include <linux/lustre_lib.h>
33 #else
34 #include <liblustre.h>
35 #endif
36
37 #include "ldlm_internal.h"
38
39 static inline int
40 ldlm_plain_compat_queue(struct list_head *queue, struct ldlm_lock *req,
41                         int send_cbs)
42 {
43         struct list_head *tmp;
44         struct ldlm_lock *lock;
45         ldlm_mode_t req_mode = req->l_req_mode;
46         int compat = 1;
47         ENTRY;
48
49         lockmode_verify(req_mode);
50
51         list_for_each(tmp, queue) {
52                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
53
54                 if (req == lock)
55                         RETURN(compat);
56
57                 if (lockmode_compat(lock->l_req_mode, req_mode))
58                         continue;
59
60                 if (!send_cbs)
61                         RETURN(0);
62
63                 compat = 0;
64                 if (lock->l_blocking_ast)
65                         ldlm_add_ast_work_item(lock, req, NULL, 0);
66         }
67
68         RETURN(compat);
69 }
70
71 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
72  *   - blocking ASTs have already been sent
73  *   - the caller has already initialized req->lr_tmp
74  *   - must call this function with the ns lock held
75  *
76  * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
77  *   - blocking ASTs have not been sent
78  *   - the caller has NOT initialized req->lr_tmp, so we must
79  *   - must call this function with the ns lock held once */
80 int ldlm_process_plain_lock(struct ldlm_lock *lock, int *flags, int first_enq,
81                             ldlm_error_t *err)
82 {
83         struct ldlm_resource *res = lock->l_resource;
84         struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
85         int rc;
86         ENTRY;
87
88         LASSERT(list_empty(&res->lr_converting));
89
90         if (!first_enq) {
91                 LASSERT(res->lr_tmp != NULL);
92                 rc = ldlm_plain_compat_queue(&res->lr_granted, lock, 0);
93                 if (!rc)
94                         RETURN(LDLM_ITER_STOP);
95                 rc = ldlm_plain_compat_queue(&res->lr_waiting, lock, 0);
96                 if (!rc)
97                         RETURN(LDLM_ITER_STOP);
98
99                 ldlm_resource_unlink_lock(lock);
100                 ldlm_grant_lock(lock, NULL, 0, 1);
101                 RETURN(LDLM_ITER_CONTINUE);
102         }
103
104  restart:
105         LASSERT(res->lr_tmp == NULL);
106         res->lr_tmp = &rpc_list;
107         rc = ldlm_plain_compat_queue(&res->lr_granted, lock, 1);
108         rc += ldlm_plain_compat_queue(&res->lr_waiting, lock, 1);
109         res->lr_tmp = NULL;
110
111         if (rc != 2) {
112                 /* If either of the compat_queue()s returned 0, then we
113                  * have ASTs to send and must go onto the waiting list.
114                  *
115                  * bug 2322: we used to unlink and re-add here, which was a
116                  * terrible folly -- if we goto restart, we could get
117                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
118                 if (list_empty(&lock->l_res_link))
119                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
120                 l_unlock(&res->lr_namespace->ns_lock);
121                 rc = ldlm_run_ast_work(res->lr_namespace, &rpc_list);
122                 l_lock(&res->lr_namespace->ns_lock);
123                 if (rc == -ERESTART)
124                         GOTO(restart, -ERESTART);
125                 *flags |= LDLM_FL_BLOCK_GRANTED;
126         } else {
127                 ldlm_resource_unlink_lock(lock);
128                 ldlm_grant_lock(lock, NULL, 0, 0);
129         }
130         RETURN(0);
131 }