Whamcloud - gitweb
LU-12017 ldlm: DoM truncate deadlock
[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  * It should iterate through all waiting locks on a given resource queue and
63  * attempt to grant them. An optimization is to check only heads waitintg
64  * locks for each inodebit type.
65  *
66  * Must be called with resource lock held.
67  */
68 int ldlm_reprocess_inodebits_queue(struct ldlm_resource *res,
69                                    struct list_head *queue,
70                                    struct list_head *work_list,
71                                    enum ldlm_process_intention intention,
72                                    struct ldlm_lock *hint)
73 {
74         __u64 flags;
75         int rc = LDLM_ITER_CONTINUE;
76         enum ldlm_error err;
77         struct list_head bl_ast_list = LIST_HEAD_INIT(bl_ast_list);
78         struct ldlm_ibits_queues *queues = res->lr_ibits_queues;
79         int i;
80
81         ENTRY;
82
83         check_res_locked(res);
84
85         LASSERT(res->lr_type == LDLM_IBITS);
86         LASSERT(intention == LDLM_PROCESS_RESCAN ||
87                 intention == LDLM_PROCESS_RECOVERY);
88
89         if (intention == LDLM_PROCESS_RECOVERY)
90                 return ldlm_reprocess_queue(res, queue, work_list, intention,
91                                             NULL);
92
93 restart:
94         CDEBUG(D_DLMTRACE, "--- Reprocess resource "DLDLMRES" (%p)\n",
95                PLDLMRES(res), res);
96
97         for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
98                 struct list_head rpc_list = LIST_HEAD_INIT(rpc_list);
99                 struct list_head *head = &queues->liq_waiting[i];
100                 struct ldlm_lock *pending;
101                 struct ldlm_ibits_node *node;
102
103                 if (list_empty(head))
104                         continue;
105                 if (hint && !(hint->l_policy_data.l_inodebits.bits & (1 << i)))
106                         continue;
107
108                 node = list_entry(head->next, struct ldlm_ibits_node,
109                                   lin_link[i]);
110
111                 pending = node->lock;
112                 LDLM_DEBUG(pending, "Reprocessing lock from queue %d", i);
113
114                 flags = 0;
115                 rc = ldlm_process_inodebits_lock(pending, &flags, intention,
116                                                  &err, &rpc_list);
117                 if (ldlm_is_granted(pending)) {
118                         list_splice(&rpc_list, work_list);
119                         /* Try to grant more locks from current queue */
120                         i--;
121                 } else {
122                         list_splice(&rpc_list, &bl_ast_list);
123                 }
124         }
125
126         if (!list_empty(&bl_ast_list)) {
127                 unlock_res(res);
128
129                 rc = ldlm_run_ast_work(ldlm_res_to_ns(res), &bl_ast_list,
130                                        LDLM_WORK_BL_AST);
131
132                 lock_res(res);
133                 if (rc == -ERESTART)
134                         GOTO(restart, rc);
135         }
136
137         if (!list_empty(&bl_ast_list))
138                 ldlm_discard_bl_list(&bl_ast_list);
139
140         RETURN(rc);
141 }
142
143 /**
144  * Determine if the lock is compatible with all locks on the queue.
145  *
146  * If \a work_list is provided, conflicting locks are linked there.
147  * If \a work_list is not provided, we exit this function on first conflict.
148  *
149  * \retval 0 if there are conflicting locks in the \a queue
150  * \retval 1 if the lock is compatible to all locks in \a queue
151  *
152  * IBITS locks in granted queue are organized in bunches of
153  * same-mode/same-bits locks called "skip lists". The First lock in the
154  * bunch contains a pointer to the end of the bunch.  This allows us to
155  * skip an entire bunch when iterating the list in search for conflicting
156  * locks if first lock of the bunch is not conflicting with us.
157  */
158 static int
159 ldlm_inodebits_compat_queue(struct list_head *queue, struct ldlm_lock *req,
160                             struct list_head *work_list)
161 {
162         struct list_head *tmp;
163         struct ldlm_lock *lock;
164         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
165         __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
166         int compat = 1;
167
168         ENTRY;
169
170         /* There is no sense in lock with no bits set. Also such a lock
171          * would be compatible with any other bit lock.
172          * Meanwhile that can be true if there were just try_bits and all
173          * are failed, so just exit gracefully and let the caller to care.
174          */
175         if ((req_bits | *try_bits) == 0)
176                 RETURN(0);
177
178         list_for_each(tmp, queue) {
179                 struct list_head *mode_tail;
180
181                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
182
183                 /* We stop walking the queue if we hit ourselves so we don't
184                  * take conflicting locks enqueued after us into account,
185                  * or we'd wait forever. */
186                 if (req == lock)
187                         RETURN(compat);
188
189                 /* last lock in mode group */
190                 LASSERT(lock->l_sl_mode.prev != NULL);
191                 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
192                                         l_sl_mode)->l_res_link;
193
194                 /* if request lock is not COS_INCOMPAT and COS is disabled,
195                  * they are compatible, IOW this request is from a local
196                  * transaction on a DNE system. */
197                 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
198                     !ldlm_is_cos_enabled(req)) {
199                         /* jump to last lock in mode group */
200                         tmp = mode_tail;
201                         continue;
202                 }
203
204                 /* locks' mode are compatible, bits don't matter */
205                 if (lockmode_compat(lock->l_req_mode, req->l_req_mode)) {
206                         /* jump to last lock in mode group */
207                         tmp = mode_tail;
208                         continue;
209                 }
210
211                 for (;;) {
212                         struct list_head *head;
213
214                         /* Advance loop cursor to last lock in policy group. */
215                         tmp = &list_entry(lock->l_sl_policy.prev,
216                                           struct ldlm_lock,
217                                           l_sl_policy)->l_res_link;
218
219                         /* New lock's try_bits are filtered out by ibits
220                          * of all locks in both granted and waiting queues.
221                          */
222                         *try_bits &= ~(lock->l_policy_data.l_inodebits.bits |
223                                 lock->l_policy_data.l_inodebits.try_bits);
224
225                         if ((req_bits | *try_bits) == 0)
226                                 RETURN(0);
227
228                         /* The new lock ibits is more preferable than try_bits
229                          * of waiting locks so drop conflicting try_bits in
230                          * the waiting queue.
231                          * Notice that try_bits of granted locks must be zero.
232                          */
233                         lock->l_policy_data.l_inodebits.try_bits &= ~req_bits;
234
235                         /* Locks with overlapping bits conflict. */
236                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
237                                 /* COS lock mode has a special compatibility
238                                  * requirement: it is only compatible with
239                                  * locks from the same client. */
240                                 if (lock->l_req_mode == LCK_COS &&
241                                     !ldlm_is_cos_incompat(req) &&
242                                     ldlm_is_cos_enabled(req) &&
243                                     lock->l_client_cookie == req->l_client_cookie)
244                                         goto not_conflicting;
245
246                                 /* Found a conflicting policy group. */
247                                 if (!work_list)
248                                         RETURN(0);
249
250                                 compat = 0;
251
252                                 /* Add locks of the policy group to @work_list
253                                  * as blocking locks for @req */
254                                 if (lock->l_blocking_ast)
255                                         ldlm_add_ast_work_item(lock, req,
256                                                                work_list);
257                                 head = &lock->l_sl_policy;
258                                 list_for_each_entry(lock, head, l_sl_policy)
259                                         if (lock->l_blocking_ast)
260                                                 ldlm_add_ast_work_item(lock,
261                                                                 req, work_list);
262                         }
263 not_conflicting:
264                         if (tmp == mode_tail)
265                                 break;
266
267                         tmp = tmp->next;
268                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
269                 } /* Loop over policy groups within one mode group. */
270         } /* Loop over mode groups within @queue. */
271
272         RETURN(compat);
273 }
274
275 /**
276  * Process a granting attempt for IBITS lock.
277  * Must be called with ns lock held
278  *
279  * This function looks for any conflicts for \a lock in the granted or
280  * waiting queues. The lock is granted if no conflicts are found in
281  * either queue.
282  */
283 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *flags,
284                                 enum ldlm_process_intention intention,
285                                 enum ldlm_error *err,
286                                 struct list_head *work_list)
287 {
288         struct ldlm_resource *res = lock->l_resource;
289         struct list_head *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
290                                                         NULL : work_list;
291         int rc;
292
293         ENTRY;
294
295         LASSERT(!ldlm_is_granted(lock));
296         check_res_locked(res);
297
298         if (intention == LDLM_PROCESS_RESCAN) {
299                 struct list_head *bl_list;
300
301                 if (*flags & LDLM_FL_BLOCK_NOWAIT) {
302                         bl_list = NULL;
303                         *err = ELDLM_LOCK_WOULDBLOCK;
304                 } else {
305                         bl_list = work_list;
306                         *err = ELDLM_LOCK_ABORTED;
307                 }
308
309                 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
310
311                 /* It is possible that some of granted locks was not canceled
312                  * but converted and is kept in granted queue. So there is
313                  * a window where lock with 'ast_sent' might become granted
314                  * again. Meanwhile a new lock may appear in that window and
315                  * conflicts with the converted lock so the following scenario
316                  * is possible:
317                  *
318                  * 1) lock1 conflicts with lock2
319                  * 2) bl_ast was sent for lock2
320                  * 3) lock3 comes and conflicts with lock2 too
321                  * 4) no bl_ast sent because lock2->l_bl_ast_sent is 1
322                  * 5) lock2 was converted for lock1 but not for lock3
323                  * 6) lock1 granted, lock3 still is waiting for lock2, but
324                  *    there will never be another bl_ast for that
325                  *
326                  * To avoid this scenario the work_list is used below to collect
327                  * any blocked locks from granted queue during every reprocess
328                  * and bl_ast will be sent if needed.
329                  */
330                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
331                                                  bl_list);
332                 if (!rc)
333                         RETURN(LDLM_ITER_STOP);
334                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock, NULL);
335                 if (!rc)
336                         RETURN(LDLM_ITER_STOP);
337
338                 /* grant also try_bits if any */
339                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
340                         lock->l_policy_data.l_inodebits.bits |=
341                                 lock->l_policy_data.l_inodebits.try_bits;
342                         lock->l_policy_data.l_inodebits.try_bits = 0;
343                         *flags |= LDLM_FL_LOCK_CHANGED;
344                 }
345                 ldlm_resource_unlink_lock(lock);
346                 ldlm_grant_lock(lock, grant_work);
347
348                 *err = ELDLM_OK;
349                 RETURN(LDLM_ITER_CONTINUE);
350         }
351
352         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock, work_list);
353         rc += ldlm_inodebits_compat_queue(&res->lr_waiting, lock, work_list);
354
355         if (rc != 2) {
356                 /* if there were only bits to try and all are conflicting */
357                 if ((lock->l_policy_data.l_inodebits.bits |
358                      lock->l_policy_data.l_inodebits.try_bits) == 0) {
359                         *err = ELDLM_LOCK_WOULDBLOCK;
360                 } else {
361                         *err = ELDLM_OK;
362                 }
363         } else {
364                 /* grant also all remaining try_bits */
365                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
366                         lock->l_policy_data.l_inodebits.bits |=
367                                 lock->l_policy_data.l_inodebits.try_bits;
368                         lock->l_policy_data.l_inodebits.try_bits = 0;
369                         *flags |= LDLM_FL_LOCK_CHANGED;
370                 }
371                 LASSERT(lock->l_policy_data.l_inodebits.bits);
372                 ldlm_resource_unlink_lock(lock);
373                 ldlm_grant_lock(lock, grant_work);
374                 *err = ELDLM_OK;
375         }
376
377         RETURN(LDLM_ITER_CONTINUE);
378 }
379 #endif /* HAVE_SERVER_SUPPORT */
380
381 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
382                                      union ldlm_policy_data *lpolicy)
383 {
384         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
385         lpolicy->l_inodebits.try_bits = wpolicy->l_inodebits.try_bits;
386 }
387
388 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
389                                      union ldlm_wire_policy_data *wpolicy)
390 {
391         memset(wpolicy, 0, sizeof(*wpolicy));
392         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
393         wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
394 }
395
396 /**
397  * Attempt to convert already granted IBITS lock with several bits set to
398  * a lock with less bits (downgrade).
399  *
400  * Such lock conversion is used to keep lock with non-blocking bits instead of
401  * cancelling it, introduced for better support of DoM files.
402  */
403 int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
404 {
405         ENTRY;
406
407         check_res_locked(lock->l_resource);
408
409         /* Just return if there are no conflicting bits */
410         if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
411                 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
412                           lock->l_policy_data.l_inodebits.bits, to_drop);
413                 /* nothing to do */
414                 RETURN(0);
415         }
416
417         /* remove lock from a skiplist and put in the new place
418          * according with new inodebits */
419         ldlm_resource_unlink_lock(lock);
420         lock->l_policy_data.l_inodebits.bits &= ~to_drop;
421         ldlm_grant_lock_with_skiplist(lock);
422         RETURN(0);
423 }
424 EXPORT_SYMBOL(ldlm_inodebits_drop);
425
426 /* convert single lock */
427 int ldlm_cli_dropbits(struct ldlm_lock *lock, __u64 drop_bits)
428 {
429         struct lustre_handle lockh;
430         __u32 flags = 0;
431         int rc;
432
433         ENTRY;
434
435         LASSERT(drop_bits);
436         LASSERT(!lock->l_readers && !lock->l_writers);
437
438         LDLM_DEBUG(lock, "client lock convert START");
439
440         ldlm_lock2handle(lock, &lockh);
441         lock_res_and_lock(lock);
442         /* check if all bits are blocked */
443         if (!(lock->l_policy_data.l_inodebits.bits & ~drop_bits)) {
444                 unlock_res_and_lock(lock);
445                 /* return error to continue with cancel */
446                 GOTO(exit, rc = -EINVAL);
447         }
448
449         /* check if no common bits, consider this as successful convert */
450         if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
451                 unlock_res_and_lock(lock);
452                 GOTO(exit, rc = 0);
453         }
454
455         /* check if there is race with cancel */
456         if (ldlm_is_canceling(lock) || ldlm_is_cancel(lock)) {
457                 unlock_res_and_lock(lock);
458                 GOTO(exit, rc = -EINVAL);
459         }
460
461         /* clear cbpending flag early, it is safe to match lock right after
462          * client convert because it is downgrade always.
463          */
464         ldlm_clear_cbpending(lock);
465         ldlm_clear_bl_ast(lock);
466
467         /* If lock is being converted already, check drop bits first */
468         if (ldlm_is_converting(lock)) {
469                 /* raced lock convert, lock inodebits are remaining bits
470                  * so check if they are conflicting with new convert or not.
471                  */
472                 if (!(lock->l_policy_data.l_inodebits.bits & drop_bits)) {
473                         unlock_res_and_lock(lock);
474                         GOTO(exit, rc = 0);
475                 }
476                 /* Otherwise drop new conflicting bits in new convert */
477         }
478         ldlm_set_converting(lock);
479         /* from all bits of blocking lock leave only conflicting */
480         drop_bits &= lock->l_policy_data.l_inodebits.bits;
481         /* save them in cancel_bits, so l_blocking_ast will know
482          * which bits from the current lock were dropped. */
483         lock->l_policy_data.l_inodebits.cancel_bits = drop_bits;
484         /* Finally clear these bits in lock ibits */
485         ldlm_inodebits_drop(lock, drop_bits);
486         unlock_res_and_lock(lock);
487         /* Finally call cancel callback for remaining bits only.
488          * It is important to have converting flag during that
489          * so blocking_ast callback can distinguish convert from
490          * cancels.
491          */
492         if (lock->l_blocking_ast)
493                 lock->l_blocking_ast(lock, NULL, lock->l_ast_data,
494                                      LDLM_CB_CANCELING);
495
496         /* now notify server about convert */
497         rc = ldlm_cli_convert(lock, &flags);
498         if (rc) {
499                 lock_res_and_lock(lock);
500                 if (ldlm_is_converting(lock)) {
501                         ldlm_clear_converting(lock);
502                         ldlm_set_cbpending(lock);
503                         ldlm_set_bl_ast(lock);
504                 }
505                 unlock_res_and_lock(lock);
506                 GOTO(exit, rc);
507         }
508         EXIT;
509 exit:
510         LDLM_DEBUG(lock, "client lock convert END");
511         return rc;
512 }
513
514
515 int ldlm_inodebits_alloc_lock(struct ldlm_lock *lock)
516 {
517         if (ldlm_is_ns_srv(lock)) {
518                 int i;
519
520                 OBD_SLAB_ALLOC_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
521                 if (lock->l_ibits_node == NULL)
522                         return -ENOMEM;
523                 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
524                         INIT_LIST_HEAD(&lock->l_ibits_node->lin_link[i]);
525                 lock->l_ibits_node->lock = lock;
526         } else {
527                 lock->l_ibits_node = NULL;
528         }
529         return 0;
530 }
531
532 void ldlm_inodebits_add_lock(struct ldlm_resource *res, struct list_head *head,
533                              struct ldlm_lock *lock)
534 {
535         int i;
536
537         if (!ldlm_is_ns_srv(lock))
538                 return;
539
540         if (head == &res->lr_waiting) {
541                 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
542                         if (lock->l_policy_data.l_inodebits.bits & (1 << i))
543                                 list_add_tail(&lock->l_ibits_node->lin_link[i],
544                                         &res->lr_ibits_queues->liq_waiting[i]);
545                 }
546         } else if (head == &res->lr_granted && lock->l_ibits_node != NULL) {
547                 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
548                         LASSERT(list_empty(&lock->l_ibits_node->lin_link[i]));
549                 OBD_SLAB_FREE_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
550                 lock->l_ibits_node = NULL;
551         }
552 }
553
554 void ldlm_inodebits_unlink_lock(struct ldlm_lock *lock)
555 {
556         int i;
557
558         ldlm_unlink_lock_skiplist(lock);
559         if (!ldlm_is_ns_srv(lock))
560                 return;
561
562         for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
563                 list_del_init(&lock->l_ibits_node->lin_link[i]);
564 }