Whamcloud - gitweb
LU-10269 ldlm: fix the issues introduced by try bits
[fs/lustre-release.git] / lustre / ldlm / ldlm_inodebits.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ldlm/ldlm_inodebits.c
33  *
34  * Author: Peter Braam <braam@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  */
37
38 /**
39  * This file contains implementation of IBITS lock type
40  *
41  * IBITS lock type contains a bit mask determining various properties of an
42  * object. The meanings of specific bits are specific to the caller and are
43  * opaque to LDLM code.
44  *
45  * Locks with intersecting bitmasks and conflicting lock modes (e.g.  LCK_PW)
46  * are considered conflicting.  See the lock mode compatibility matrix
47  * in lustre_dlm.h.
48  */
49
50 #define DEBUG_SUBSYSTEM S_LDLM
51
52 #include <lustre_dlm.h>
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <obd_class.h>
56
57 #include "ldlm_internal.h"
58
59 #ifdef HAVE_SERVER_SUPPORT
60 /**
61  * Determine if the lock is compatible with all locks on the queue.
62  *
63  * If \a work_list is provided, conflicting locks are linked there.
64  * If \a work_list is not provided, we exit this function on first conflict.
65  *
66  * \retval 0 if there are conflicting locks in the \a queue
67  * \retval 1 if the lock is compatible to all locks in \a queue
68  *
69  * IBITS locks in granted queue are organized in bunches of
70  * same-mode/same-bits locks called "skip lists". The First lock in the
71  * bunch contains a pointer to the end of the bunch.  This allows us to
72  * skip an entire bunch when iterating the list in search for conflicting
73  * locks if first lock of the bunch is not conflicting with us.
74  */
75 static int
76 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
77                             struct list_head *work_list)
78 {
79         struct list_head *tmp;
80         struct ldlm_lock *lock;
81         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
82         __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
83         int compat = 1;
84
85         ENTRY;
86
87         /* There is no sense in lock with no bits set. Also such a lock
88          * would be compatible with any other bit lock.
89          * Meanwhile that can be true if there were just try_bits and all
90          * are failed, so just exit gracefully and let the caller to care.
91          */
92         if ((req_bits | *try_bits) == 0)
93                 RETURN(0);
94
95         list_for_each(tmp, queue) {
96                 struct list_head *mode_tail;
97
98                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
99
100                 /* We stop walking the queue if we hit ourselves so we don't
101                  * take conflicting locks enqueued after us into account,
102                  * or we'd wait forever. */
103                 if (req == lock)
104                         RETURN(compat);
105
106                 /* last lock in mode group */
107                 LASSERT(lock->l_sl_mode.prev != NULL);
108                 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
109                                         l_sl_mode)->l_res_link;
110
111                 /* if request lock is not COS_INCOMPAT and COS is disabled,
112                  * they are compatible, IOW this request is from a local
113                  * transaction on a DNE system. */
114                 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
115                     !ldlm_is_cos_enabled(req)) {
116                         /* jump to last lock in mode group */
117                         tmp = mode_tail;
118                         continue;
119                 }
120
121                 /* locks' mode are compatible, bits don't matter */
122                 if (lockmode_compat(lock->l_req_mode, req->l_req_mode)) {
123                         /* jump to last lock in mode group */
124                         tmp = mode_tail;
125                         continue;
126                 }
127
128                 for (;;) {
129                         struct list_head *head;
130
131                         /* Advance loop cursor to last lock in policy group. */
132                         tmp = &list_entry(lock->l_sl_policy.prev,
133                                           struct ldlm_lock,
134                                           l_sl_policy)->l_res_link;
135
136                         /* no locks with trybits in either queues */
137                         LASSERT(lock->l_policy_data.l_inodebits.try_bits == 0);
138
139                         /* New lock's try_bits are filtered out by ibits
140                          * of all locks in both granted and waiting queues.
141                          */
142                         *try_bits &= ~lock->l_policy_data.l_inodebits.bits;
143
144                         if ((req_bits | *try_bits) == 0)
145                                 RETURN(0);
146
147                         /* Locks with overlapping bits conflict. */
148                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
149                                 /* COS lock mode has a special compatibility
150                                  * requirement: it is only compatible with
151                                  * locks from the same client. */
152                                 if (lock->l_req_mode == LCK_COS &&
153                                     !ldlm_is_cos_incompat(req) &&
154                                     ldlm_is_cos_enabled(req) &&
155                                     lock->l_client_cookie == req->l_client_cookie)
156                                         goto not_conflicting;
157
158                                 /* The conflicting lock will keep only mandatory
159                                  * ibits and zero trybits in waiting queue.
160                                  * All actual trybits are cleared.
161                                  */
162                                 *try_bits = 0;
163
164                                 /* Found a conflicting policy group. */
165                                 if (!work_list)
166                                         RETURN(0);
167
168                                 compat = 0;
169
170                                 /* Add locks of the policy group to @work_list
171                                  * as blocking locks for @req */
172                                 if (lock->l_blocking_ast)
173                                         ldlm_add_ast_work_item(lock, req,
174                                                                work_list);
175                                 head = &lock->l_sl_policy;
176                                 list_for_each_entry(lock, head, l_sl_policy)
177                                         if (lock->l_blocking_ast)
178                                                 ldlm_add_ast_work_item(lock,
179                                                                 req, work_list);
180                         }
181 not_conflicting:
182                         if (tmp == mode_tail)
183                                 break;
184
185                         tmp = tmp->next;
186                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
187                 } /* Loop over policy groups within one mode group. */
188         } /* Loop over mode groups within @queue. */
189
190         RETURN(compat);
191 }
192
193 /**
194  * Process a granting attempt for IBITS lock.
195  * Must be called with ns lock held
196  *
197  * This function looks for any conflicts for \a lock in the granted or
198  * waiting queues. The lock is granted if no conflicts are found in
199  * either queue.
200  */
201 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
202                                 enum ldlm_process_intention intention,
203                                 enum ldlm_error *err,
204                                 struct list_head *work_list)
205 {
206         struct ldlm_resource *res = lock->l_resource;
207         struct list_head rpc_list;
208         int rc;
209
210         ENTRY;
211
212         LASSERT(lock->l_granted_mode != lock->l_req_mode);
213         LASSERT(list_empty(&res->lr_converting));
214         INIT_LIST_HEAD(&rpc_list);
215         check_res_locked(res);
216
217         if (intention == LDLM_PROCESS_RESCAN) {
218                 *err = ELDLM_LOCK_ABORTED;
219
220                 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
221
222                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, NULL);
223                 if (!rc)
224                         RETURN(LDLM_ITER_STOP);
225                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
226                 if (!rc)
227                         RETURN(LDLM_ITER_STOP);
228
229                 /* grant also try_bits if any */
230                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
231                         lock->l_policy_data.l_inodebits.bits |=
232                                 lock->l_policy_data.l_inodebits.try_bits;
233                         lock->l_policy_data.l_inodebits.try_bits = 0;
234                         *flags |= LDLM_FL_LOCK_CHANGED;
235                 }
236                 ldlm_resource_unlink_lock(lock);
237                 ldlm_grant_lock(lock, work_list);
238
239                 *err = ELDLM_OK;
240                 RETURN(LDLM_ITER_CONTINUE);
241         }
242
243         LASSERT((intention == LDLM_PROCESS_ENQUEUE && work_list == NULL) ||
244                 (intention == LDLM_PROCESS_RECOVERY && work_list != NULL));
245 restart:
246         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, &rpc_list);
247         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, &rpc_list);
248
249         if (rc != 2) {
250                 /* if there were only bits to try and all are conflicting */
251                 if ((lock->l_policy_data.l_inodebits.bits |
252                      lock->l_policy_data.l_inodebits.try_bits) == 0) {
253                         rc = ELDLM_LOCK_WOULDBLOCK;
254                 } else {
255                         rc = ldlm_handle_conflict_lock(lock, flags,
256                                                        &rpc_list, 0);
257                         if (rc == -ERESTART)
258                                 GOTO(restart, rc);
259                 }
260                 *err = rc;
261         } else {
262                 /* grant also all remaining try_bits */
263                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
264                         lock->l_policy_data.l_inodebits.bits |=
265                                 lock->l_policy_data.l_inodebits.try_bits;
266                         lock->l_policy_data.l_inodebits.try_bits = 0;
267                         *flags |= LDLM_FL_LOCK_CHANGED;
268                 }
269                 LASSERT(lock->l_policy_data.l_inodebits.bits);
270                 ldlm_resource_unlink_lock(lock);
271                 ldlm_grant_lock(lock, work_list);
272                 rc = 0;
273         }
274
275         if (!list_empty(&rpc_list))
276                 ldlm_discard_bl_list(&rpc_list);
277
278         RETURN(rc);
279 }
280 #endif /* HAVE_SERVER_SUPPORT */
281
282 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
283                                      union ldlm_policy_data *lpolicy)
284 {
285         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
286         lpolicy->l_inodebits.try_bits = wpolicy->l_inodebits.try_bits;
287 }
288
289 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
290                                      union ldlm_wire_policy_data *wpolicy)
291 {
292         memset(wpolicy, 0, sizeof(*wpolicy));
293         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
294         wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
295 }
296
297 int ldlm_inodebits_drop(struct ldlm_lock *lock,  __u64 to_drop)
298 {
299         ENTRY;
300
301         check_res_locked(lock->l_resource);
302
303         /* Just return if there are no conflicting bits */
304         if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
305                 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx\n",
306                           lock->l_policy_data.l_inodebits.bits, to_drop);
307                 /* nothing to do */
308                 RETURN(0);
309         }
310
311         /* remove lock from a skiplist and put in the new place
312          * according with new inodebits */
313         ldlm_resource_unlink_lock(lock);
314         lock->l_policy_data.l_inodebits.bits &= ~to_drop;
315         ldlm_grant_lock_with_skiplist(lock);
316         RETURN(0);
317 }
318 EXPORT_SYMBOL(ldlm_inodebits_drop);