Whamcloud - gitweb
LU-8047 llite: optimizations for not granted lock processing
[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, 2017, 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 /**
62  * Determine if the lock is compatible with all locks on the queue.
63  *
64  * If \a work_list is provided, conflicting locks are linked there.
65  * If \a work_list is not provided, we exit this function on first conflict.
66  *
67  * \retval 0 if there are conflicting locks in the \a queue
68  * \retval 1 if the lock is compatible to all locks in \a queue
69  *
70  * IBITS locks in granted queue are organized in bunches of
71  * same-mode/same-bits locks called "skip lists". The First lock in the
72  * bunch contains a pointer to the end of the bunch.  This allows us to
73  * skip an entire bunch when iterating the list in search for conflicting
74  * locks if first lock of the bunch is not conflicting with us.
75  */
76 static int
77 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
78                             struct list_head *work_list)
79 {
80         struct list_head *tmp;
81         struct ldlm_lock *lock;
82         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
83         __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
84         int compat = 1;
85
86         ENTRY;
87
88         /* There is no sense in lock with no bits set. Also such a lock
89          * would be compatible with any other bit lock.
90          * Meanwhile that can be true if there were just try_bits and all
91          * are failed, so just exit gracefully and let the caller to care.
92          */
93         if ((req_bits | *try_bits) == 0)
94                 RETURN(0);
95
96         list_for_each(tmp, queue) {
97                 struct list_head *mode_tail;
98
99                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
100
101                 /* We stop walking the queue if we hit ourselves so we don't
102                  * take conflicting locks enqueued after us into account,
103                  * or we'd wait forever. */
104                 if (req == lock)
105                         RETURN(compat);
106
107                 /* last lock in mode group */
108                 LASSERT(lock->l_sl_mode.prev != NULL);
109                 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
110                                         l_sl_mode)->l_res_link;
111
112                 /* if request lock is not COS_INCOMPAT and COS is disabled,
113                  * they are compatible, IOW this request is from a local
114                  * transaction on a DNE system. */
115                 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
116                     !ldlm_is_cos_enabled(req)) {
117                         /* jump to last lock in mode group */
118                         tmp = mode_tail;
119                         continue;
120                 }
121
122                 /* locks' mode are compatible, bits don't matter */
123                 if (lockmode_compat(lock->l_req_mode, req->l_req_mode)) {
124                         /* jump to last lock in mode group */
125                         tmp = mode_tail;
126                         continue;
127                 }
128
129                 for (;;) {
130                         struct list_head *head;
131
132                         /* Advance loop cursor to last lock in policy group. */
133                         tmp = &list_entry(lock->l_sl_policy.prev,
134                                           struct ldlm_lock,
135                                           l_sl_policy)->l_res_link;
136
137                         /* New lock's try_bits are filtered out by ibits
138                          * of all locks in both granted and waiting queues.
139                          */
140                         *try_bits &= ~(lock->l_policy_data.l_inodebits.bits |
141                                 lock->l_policy_data.l_inodebits.try_bits);
142
143                         if ((req_bits | *try_bits) == 0)
144                                 RETURN(0);
145
146                         /* The new lock ibits is more preferable than try_bits
147                          * of waiting locks so drop conflicting try_bits in
148                          * the waiting queue.
149                          * Notice that try_bits of granted locks must be zero.
150                          */
151                         lock->l_policy_data.l_inodebits.try_bits &= ~req_bits;
152
153                         /* Locks with overlapping bits conflict. */
154                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
155                                 /* COS lock mode has a special compatibility
156                                  * requirement: it is only compatible with
157                                  * locks from the same client. */
158                                 if (lock->l_req_mode == LCK_COS &&
159                                     !ldlm_is_cos_incompat(req) &&
160                                     ldlm_is_cos_enabled(req) &&
161                                     lock->l_client_cookie == req->l_client_cookie)
162                                         goto not_conflicting;
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 *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
208                                                         NULL : work_list;
209         int rc;
210
211         ENTRY;
212
213         LASSERT(!ldlm_is_granted(lock));
214         check_res_locked(res);
215
216         if (intention == LDLM_PROCESS_RESCAN) {
217                 struct list_head *bl_list;
218
219                 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
220                         bl_list = NULL;
221                         *err = ELDLM_LOCK_WOULDBLOCK;
222                 } else {
223                         bl_list = work_list;
224                         *err = ELDLM_LOCK_ABORTED;
225                 }
226
227                 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
228
229                 /* It is possible that some of granted locks was not canceled
230                  * but converted and is kept in granted queue. So there is
231                  * a window where lock with 'ast_sent' might become granted
232                  * again. Meanwhile a new lock may appear in that window and
233                  * conflicts with the converted lock so the following scenario
234                  * is possible:
235                  *
236                  * 1) lock1 conflicts with lock2
237                  * 2) bl_ast was sent for lock2
238                  * 3) lock3 comes and conflicts with lock2 too
239                  * 4) no bl_ast sent because lock2->l_bl_ast_sent is 1
240                  * 5) lock2 was converted for lock1 but not for lock3
241                  * 6) lock1 granted, lock3 still is waiting for lock2, but
242                  *    there will never be another bl_ast for that
243                  *
244                  * To avoid this scenario the work_list is used below to collect
245                  * any blocked locks from granted queue during every reprocess
246                  * and bl_ast will be sent if needed.
247                  */
248                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
249                                                  bl_list);
250                 if (!rc)
251                         RETURN(LDLM_ITER_STOP);
252                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
253                 if (!rc)
254                         RETURN(LDLM_ITER_STOP);
255
256                 /* grant also try_bits if any */
257                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
258                         lock->l_policy_data.l_inodebits.bits |=
259                                 lock->l_policy_data.l_inodebits.try_bits;
260                         lock->l_policy_data.l_inodebits.try_bits = 0;
261                         *flags |= LDLM_FL_LOCK_CHANGED;
262                 }
263                 ldlm_resource_unlink_lock(lock);
264                 ldlm_grant_lock(lock, grant_work);
265
266                 *err = ELDLM_OK;
267                 RETURN(LDLM_ITER_CONTINUE);
268         }
269
270         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, work_list);
271         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, work_list);
272
273         if (rc != 2) {
274                 /* if there were only bits to try and all are conflicting */
275                 if ((lock->l_policy_data.l_inodebits.bits |
276                      lock->l_policy_data.l_inodebits.try_bits) == 0) {
277                         *err = ELDLM_LOCK_WOULDBLOCK;
278                 } else {
279                         *err = ELDLM_OK;
280                 }
281         } else {
282                 /* grant also all remaining try_bits */
283                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
284                         lock->l_policy_data.l_inodebits.bits |=
285                                 lock->l_policy_data.l_inodebits.try_bits;
286                         lock->l_policy_data.l_inodebits.try_bits = 0;
287                         *flags |= LDLM_FL_LOCK_CHANGED;
288                 }
289                 LASSERT(lock->l_policy_data.l_inodebits.bits);
290                 ldlm_resource_unlink_lock(lock);
291                 ldlm_grant_lock(lock, grant_work);
292                 *err = ELDLM_OK;
293         }
294
295         RETURN(LDLM_ITER_CONTINUE);
296 }
297 #endif /* HAVE_SERVER_SUPPORT */
298
299 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
300                                      union ldlm_policy_data *lpolicy)
301 {
302         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
303         lpolicy->l_inodebits.try_bits = wpolicy->l_inodebits.try_bits;
304 }
305
306 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
307                                      union ldlm_wire_policy_data *wpolicy)
308 {
309         memset(wpolicy, 0, sizeof(*wpolicy));
310         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
311         wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
312 }
313
314 /**
315  * Attempt to convert already granted IBITS lock with several bits set to
316  * a lock with less bits (downgrade).
317  *
318  * Such lock conversion is used to keep lock with non-blocking bits instead of
319  * cancelling it, introduced for better support of DoM files.
320  */
321 int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
322 {
323         ENTRY;
324
325         check_res_locked(lock->l_resource);
326
327         /* Just return if there are no conflicting bits */
328         if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
329                 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
330                           lock->l_policy_data.l_inodebits.bits, to_drop);
331                 /* nothing to do */
332                 RETURN(0);
333         }
334
335         /* remove lock from a skiplist and put in the new place
336          * according with new inodebits */
337         ldlm_resource_unlink_lock(lock);
338         lock->l_policy_data.l_inodebits.bits &= ~to_drop;
339         ldlm_grant_lock_with_skiplist(lock);
340         RETURN(0);
341 }
342 EXPORT_SYMBOL(ldlm_inodebits_drop);
343
344 /* convert single lock */
345 int ldlm_cli_dropbits(struct ldlm_lock *lock, __u64 drop_bits)
346 {
347         struct lustre_handle lockh;
348         __u32 flags = 0;
349         int rc;
350
351         ENTRY;
352
353         LASSERT(drop_bits);
354         LASSERT(!lock->l_readers && !lock->l_writers);
355
356         LDLM_DEBUG(lock, "client lock convert START");
357
358         ldlm_lock2handle(lock, &lockh);
359         lock_res_and_lock(lock);
360         /* check if all bits are blocked */
361         if (!(lock->l_policy_data.l_inodebits.bits & ~drop_bits)) {
362                 unlock_res_and_lock(lock);
363                 /* return error to continue with cancel */
364                 GOTO(exit, rc = -EINVAL);
365         }
366
367         /* check if no common bits, consider this as successful convert */
368         if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
369                 unlock_res_and_lock(lock);
370                 GOTO(exit, rc = 0);
371         }
372
373         /* check if there is race with cancel */
374         if (ldlm_is_canceling(lock) || ldlm_is_cancel(lock)) {
375                 unlock_res_and_lock(lock);
376                 GOTO(exit, rc = -EINVAL);
377         }
378
379         /* clear cbpending flag early, it is safe to match lock right after
380          * client convert because it is downgrade always.
381          */
382         ldlm_clear_cbpending(lock);
383         ldlm_clear_bl_ast(lock);
384
385         /* If lock is being converted already, check drop bits first */
386         if (ldlm_is_converting(lock)) {
387                 /* raced lock convert, lock inodebits are remaining bits
388                  * so check if they are conflicting with new convert or not.
389                  */
390                 if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
391                         unlock_res_and_lock(lock);
392                         GOTO(exit, rc = 0);
393                 }
394                 /* Otherwise drop new conflicting bits in new convert */
395         }
396         ldlm_set_converting(lock);
397         /* from all bits of blocking lock leave only conflicting */
398         drop_bits &= lock->l_policy_data.l_inodebits.bits;
399         /* save them in cancel_bits, so l_blocking_ast will know
400          * which bits from the current lock were dropped. */
401         lock->l_policy_data.l_inodebits.cancel_bits = drop_bits;
402         /* Finally clear these bits in lock ibits */
403         ldlm_inodebits_drop(lock, drop_bits);
404         unlock_res_and_lock(lock);
405         /* Finally call cancel callback for remaining bits only.
406          * It is important to have converting flag during that
407          * so blocking_ast callback can distinguish convert from
408          * cancels.
409          */
410         if (lock->l_blocking_ast)
411                 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
412                                      LDLM_CB_CANCELING);
413
414         /* now notify server about convert */
415         rc = ldlm_cli_convert(lock, &flags);
416         if (rc) {
417                 lock_res_and_lock(lock);
418                 if (ldlm_is_converting(lock)) {
419                         ldlm_clear_converting(lock);
420                         ldlm_set_cbpending(lock);
421                         ldlm_set_bl_ast(lock);
422                 }
423                 unlock_res_and_lock(lock);
424                 GOTO(exit, rc);
425         }
426         EXIT;
427 exit:
428         LDLM_DEBUG(lock, "client lock convert END");
429         return rc;
430 }