Whamcloud - gitweb
LU-571 ldlm: Remove parallel AST limitation
[fs/lustre-release.git] / lustre / ldlm / ldlm_inodebits.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011 Whamcloud, Inc.
33  *
34  */
35 /*
36  * This file is part of Lustre, http://www.lustre.org/
37  * Lustre is a trademark of Sun Microsystems, Inc.
38  *
39  * lustre/ldlm/ldlm_inodebits.c
40  *
41  * Author: Peter Braam <braam@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LDLM
46 #ifndef __KERNEL__
47 # include <liblustre.h>
48 #endif
49
50 #include <lustre_dlm.h>
51 #include <obd_support.h>
52 #include <lustre_lib.h>
53
54 #include "ldlm_internal.h"
55
56 /* Determine if the lock is compatible with all locks on the queue. */
57 static int
58 ldlm_inodebits_compat_queue(cfs_list_t *queue, struct ldlm_lock *req,
59                             cfs_list_t *work_list)
60 {
61         cfs_list_t *tmp;
62         struct ldlm_lock *lock;
63         ldlm_mode_t req_mode = req->l_req_mode;
64         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
65         int compat = 1;
66         ENTRY;
67
68         LASSERT(req_bits); /* There is no sense in lock with no bits set,
69                               I think. Also such a lock would be compatible
70                                with any other bit lock */
71
72         cfs_list_for_each(tmp, queue) {
73                 cfs_list_t *mode_tail;
74
75                 lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
76
77                 if (req == lock)
78                         RETURN(compat);
79
80                 /* last lock in mode group */
81                 LASSERT(lock->l_sl_mode.prev != NULL);
82                 mode_tail = &cfs_list_entry(lock->l_sl_mode.prev,
83                                             struct ldlm_lock,
84                                             l_sl_mode)->l_res_link;
85
86                 /* locks are compatible, bits don't matter */
87                 if (lockmode_compat(lock->l_req_mode, req_mode)) {
88                         /* jump to last lock in mode group */
89                         tmp = mode_tail;
90                         continue;
91                 }
92
93                 for (;;) {
94                         cfs_list_t *head;
95
96                         /* last lock in policy group */
97                         tmp = &cfs_list_entry(lock->l_sl_policy.prev,
98                                               struct ldlm_lock,
99                                               l_sl_policy)->l_res_link;
100
101                         /* locks with bits overlapped are conflicting locks */
102                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
103                                 /* COS lock from the same client is
104                                    not conflicting */
105                                 if (lock->l_req_mode == LCK_COS &&
106                                     lock->l_client_cookie == req->l_client_cookie)
107                                         goto not_conflicting;
108                                 /* conflicting policy */
109                                 if (!work_list)
110                                         RETURN(0);
111
112                                 compat = 0;
113
114                                 /* add locks of the policy group to
115                                  * @work_list as blocking locks for
116                                  * @req */
117                                 if (lock->l_blocking_ast)
118                                         ldlm_add_ast_work_item(lock, req,
119                                                                work_list);
120                                 head = &lock->l_sl_policy;
121                                 cfs_list_for_each_entry(lock, head, l_sl_policy)
122                                         if (lock->l_blocking_ast)
123                                                 ldlm_add_ast_work_item(lock, req,
124                                                                        work_list);
125                         }
126                 not_conflicting:
127                         if (tmp == mode_tail)
128                                 break;
129
130                         tmp = tmp->next;
131                         lock = cfs_list_entry(tmp, struct ldlm_lock,
132                                               l_res_link);
133                 } /* loop over policy groups within one mode group */
134         } /* loop over mode groups within @queue */
135
136         RETURN(compat);
137 }
138
139 /* If first_enq is 0 (ie, called from ldlm_reprocess_queue):
140   *   - blocking ASTs have already been sent
141   *   - must call this function with the ns lock held
142   *
143   * If first_enq is 1 (ie, called from ldlm_lock_enqueue):
144   *   - blocking ASTs have not been sent
145   *   - must call this function with the ns lock held once */
146 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, int *flags,
147                                 int first_enq, ldlm_error_t *err,
148                                 cfs_list_t *work_list)
149 {
150         struct ldlm_resource *res = lock->l_resource;
151         CFS_LIST_HEAD(rpc_list);
152         int rc;
153         ENTRY;
154
155         LASSERT(cfs_list_empty(&res->lr_converting));
156         check_res_locked(res);
157
158         if (!first_enq) {
159                 LASSERT(work_list != NULL);
160                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, NULL);
161                 if (!rc)
162                         RETURN(LDLM_ITER_STOP);
163                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
164                 if (!rc)
165                         RETURN(LDLM_ITER_STOP);
166
167                 ldlm_resource_unlink_lock(lock);
168                 ldlm_grant_lock(lock, work_list);
169                 RETURN(LDLM_ITER_CONTINUE);
170         }
171
172  restart:
173         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, &rpc_list);
174         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, &rpc_list);
175
176         if (rc != 2) {
177                 /* If either of the compat_queue()s returned 0, then we
178                  * have ASTs to send and must go onto the waiting list.
179                  *
180                  * bug 2322: we used to unlink and re-add here, which was a
181                  * terrible folly -- if we goto restart, we could get
182                  * re-ordered!  Causes deadlock, because ASTs aren't sent! */
183                 if (cfs_list_empty(&lock->l_res_link))
184                         ldlm_resource_add_lock(res, &res->lr_waiting, lock);
185                 unlock_res(res);
186                 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &rpc_list,
187                                        LDLM_WORK_BL_AST);
188                 lock_res(res);
189                 if (rc == -ERESTART)
190                         GOTO(restart, -ERESTART);
191                 *flags |= LDLM_FL_BLOCK_GRANTED;
192         } else {
193                 ldlm_resource_unlink_lock(lock);
194                 ldlm_grant_lock(lock, NULL);
195         }
196         RETURN(0);
197 }
198
199 void ldlm_ibits_policy_wire_to_local(const ldlm_wire_policy_data_t *wpolicy,
200                                      ldlm_policy_data_t *lpolicy)
201 {
202         memset(lpolicy, 0, sizeof(*lpolicy));
203         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
204 }
205
206 void ldlm_ibits_policy_local_to_wire(const ldlm_policy_data_t *lpolicy,
207                                      ldlm_wire_policy_data_t *wpolicy)
208 {
209         memset(wpolicy, 0, sizeof(*wpolicy));
210         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
211 }