Whamcloud - gitweb
a338c4d13bbef68a85a40718e80bed281438468e
[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         LIST_HEAD(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                 LIST_HEAD(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 & BIT(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                             __u64 *ldlm_flags, struct list_head *work_list)
161 {
162         enum ldlm_mode req_mode = req->l_req_mode;
163         struct list_head *tmp;
164         struct ldlm_lock *lock;
165         __u64 req_bits = req->l_policy_data.l_inodebits.bits;
166         __u64 *try_bits = &req->l_policy_data.l_inodebits.try_bits;
167         int compat = 1;
168
169         ENTRY;
170
171         lockmode_verify(req_mode);
172
173         /* There is no sense in lock with no bits set. Also such a lock
174          * would be compatible with any other bit lock.
175          * Meanwhile that can be true if there were just try_bits and all
176          * are failed, so just exit gracefully and let the caller to care.
177          */
178         if ((req_bits | *try_bits) == 0)
179                 RETURN(0);
180
181         /* Group lock could be only DOM */
182         if (unlikely(req_mode == LCK_GROUP &&
183                      (req_bits | *try_bits) != MDS_INODELOCK_DOM))
184                 RETURN(-EPROTO);
185
186         list_for_each(tmp, queue) {
187                 struct list_head *mode_tail;
188
189                 lock = list_entry(tmp, struct ldlm_lock, l_res_link);
190
191                 /* We stop walking the queue if we hit ourselves so we don't
192                  * take conflicting locks enqueued after us into account,
193                  * or we'd wait forever. */
194                 if (req == lock)
195                         RETURN(compat);
196
197                 /* last lock in mode group */
198                 LASSERT(lock->l_sl_mode.prev != NULL);
199                 mode_tail = &list_entry(lock->l_sl_mode.prev, struct ldlm_lock,
200                                         l_sl_mode)->l_res_link;
201
202                 /* if request lock is not COS_INCOMPAT and COS is disabled,
203                  * they are compatible, IOW this request is from a local
204                  * transaction on a DNE system. */
205                 if (lock->l_req_mode == LCK_COS && !ldlm_is_cos_incompat(req) &&
206                     !ldlm_is_cos_enabled(req)) {
207                         /* jump to last lock in mode group */
208                         tmp = mode_tail;
209                         continue;
210                 }
211
212                 if (lockmode_compat(lock->l_req_mode, req_mode)) {
213                         /* non group locks are compatible, bits don't matter */
214                         if (likely(req_mode != LCK_GROUP)) {
215                                 /* jump to last lock in mode group */
216                                 tmp = mode_tail;
217                                 continue;
218                         }
219
220                         if (req->l_policy_data.l_inodebits.li_gid ==
221                             lock->l_policy_data.l_inodebits.li_gid) {
222                                 if (ldlm_is_granted(lock))
223                                         RETURN(2);
224
225                                 if (*ldlm_flags & LDLM_FL_BLOCK_NOWAIT)
226                                         RETURN(-EWOULDBLOCK);
227
228                                 /* Place the same group together */
229                                 ldlm_resource_insert_lock_after(lock, req);
230                                 RETURN(0);
231                         }
232                 }
233
234                 /* GROUP locks are placed to a head of the waiting list, but
235                  * grouped by gid. */
236                 if (unlikely(req_mode == LCK_GROUP && !ldlm_is_granted(lock))) {
237                         compat = 0;
238                         if (lock->l_req_mode != LCK_GROUP) {
239                                 /* Already not a GROUP lock, insert before. */
240                                 ldlm_resource_insert_lock_before(lock, req);
241                                 break;
242                         }
243                         /* Still GROUP but a different gid(the same gid would
244                          * be handled above). Keep searching for the same gid */
245                         LASSERT(req->l_policy_data.l_inodebits.li_gid !=
246                                 lock->l_policy_data.l_inodebits.li_gid);
247                         continue;
248                 }
249
250                 for (;;) {
251                         struct list_head *head;
252
253                         /* Advance loop cursor to last lock in policy group. */
254                         tmp = &list_entry(lock->l_sl_policy.prev,
255                                           struct ldlm_lock,
256                                           l_sl_policy)->l_res_link;
257
258                         /* New lock's try_bits are filtered out by ibits
259                          * of all locks in both granted and waiting queues.
260                          */
261                         *try_bits &= ~(lock->l_policy_data.l_inodebits.bits |
262                                 lock->l_policy_data.l_inodebits.try_bits);
263
264                         if ((req_bits | *try_bits) == 0)
265                                 RETURN(0);
266
267                         /* The new lock ibits is more preferable than try_bits
268                          * of waiting locks so drop conflicting try_bits in
269                          * the waiting queue.
270                          * Notice that try_bits of granted locks must be zero.
271                          */
272                         lock->l_policy_data.l_inodebits.try_bits &= ~req_bits;
273
274                         /* Locks with overlapping bits conflict. */
275                         if (lock->l_policy_data.l_inodebits.bits & req_bits) {
276                                 /* COS lock mode has a special compatibility
277                                  * requirement: it is only compatible with
278                                  * locks from the same client. */
279                                 if (lock->l_req_mode == LCK_COS &&
280                                     !ldlm_is_cos_incompat(req) &&
281                                     ldlm_is_cos_enabled(req) &&
282                                     lock->l_client_cookie == req->l_client_cookie)
283                                         goto skip_work_list;
284
285                                 compat = 0;
286
287                                 if (unlikely(lock->l_req_mode == LCK_GROUP)) {
288                                         LASSERT(ldlm_has_dom(lock));
289
290                                         if (*ldlm_flags & LDLM_FL_BLOCK_NOWAIT)
291                                                 RETURN(-EWOULDBLOCK);
292
293                                         /* Local combined DOM lock came across
294                                          * GROUP DOM lock, it makes the thread
295                                          * to be blocked for a long time, not
296                                          * allowed, the trybits to be used
297                                          * instead.
298                                          */
299                                         if (!req->l_export &&
300                                             (req_bits & MDS_INODELOCK_DOM) &&
301                                             (req_bits & ~MDS_INODELOCK_DOM))
302                                                 LBUG();
303
304                                         goto skip_work_list;
305                                 }
306
307                                 /* Found a conflicting policy group. */
308                                 if (!work_list)
309                                         RETURN(0);
310
311                                 /* Add locks of the policy group to @work_list
312                                  * as blocking locks for @req */
313                                 if (lock->l_blocking_ast)
314                                         ldlm_add_ast_work_item(lock, req,
315                                                                work_list);
316                                 head = &lock->l_sl_policy;
317                                 list_for_each_entry(lock, head, l_sl_policy)
318                                         if (lock->l_blocking_ast)
319                                                 ldlm_add_ast_work_item(lock,
320                                                                 req, work_list);
321                         }
322 skip_work_list:
323                         if (tmp == mode_tail)
324                                 break;
325
326                         tmp = tmp->next;
327                         lock = list_entry(tmp, struct ldlm_lock, l_res_link);
328                 } /* Loop over policy groups within one mode group. */
329         } /* Loop over mode groups within @queue. */
330
331         RETURN(compat);
332 }
333
334 /**
335  * Process a granting attempt for IBITS lock.
336  * Must be called with ns lock held
337  *
338  * This function looks for any conflicts for \a lock in the granted or
339  * waiting queues. The lock is granted if no conflicts are found in
340  * either queue.
341  */
342 int ldlm_process_inodebits_lock(struct ldlm_lock *lock, __u64 *ldlm_flags,
343                                 enum ldlm_process_intention intention,
344                                 enum ldlm_error *err,
345                                 struct list_head *work_list)
346 {
347         struct ldlm_resource *res = lock->l_resource;
348         struct list_head *grant_work = intention == LDLM_PROCESS_ENQUEUE ?
349                                                         NULL : work_list;
350         int rc, rc2 = 0;
351         ENTRY;
352
353         *err = ELDLM_LOCK_ABORTED;
354         LASSERT(!ldlm_is_granted(lock));
355         check_res_locked(res);
356
357         if (intention == LDLM_PROCESS_RESCAN) {
358                 struct list_head *bl_list =
359                         *ldlm_flags & LDLM_FL_BLOCK_NOWAIT ? NULL : work_list;
360
361                 LASSERT(lock->l_policy_data.l_inodebits.bits != 0);
362
363                 /* It is possible that some of granted locks was not canceled
364                  * but converted and is kept in granted queue. So there is
365                  * a window where lock with 'ast_sent' might become granted
366                  * again. Meanwhile a new lock may appear in that window and
367                  * conflicts with the converted lock so the following scenario
368                  * is possible:
369                  *
370                  * 1) lock1 conflicts with lock2
371                  * 2) bl_ast was sent for lock2
372                  * 3) lock3 comes and conflicts with lock2 too
373                  * 4) no bl_ast sent because lock2->l_bl_ast_sent is 1
374                  * 5) lock2 was converted for lock1 but not for lock3
375                  * 6) lock1 granted, lock3 still is waiting for lock2, but
376                  *    there will never be another bl_ast for that
377                  *
378                  * To avoid this scenario the work_list is used below to collect
379                  * any blocked locks from granted queue during every reprocess
380                  * and bl_ast will be sent if needed.
381                  */
382                 *ldlm_flags = 0;
383                 rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
384                                                  ldlm_flags, bl_list);
385                 if (!rc)
386                         RETURN(LDLM_ITER_STOP);
387                 rc = ldlm_inodebits_compat_queue(&res->lr_waiting, lock,
388                                                  ldlm_flags, NULL);
389                 if (!rc)
390                         RETURN(LDLM_ITER_STOP);
391
392                 /* grant also try_bits if any */
393                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
394                         lock->l_policy_data.l_inodebits.bits |=
395                                 lock->l_policy_data.l_inodebits.try_bits;
396                         lock->l_policy_data.l_inodebits.try_bits = 0;
397                         *ldlm_flags |= LDLM_FL_LOCK_CHANGED;
398                 }
399                 ldlm_resource_unlink_lock(lock);
400                 ldlm_grant_lock(lock, grant_work);
401
402                 *err = ELDLM_OK;
403                 RETURN(LDLM_ITER_CONTINUE);
404         }
405
406         rc = ldlm_inodebits_compat_queue(&res->lr_granted, lock,
407                                          ldlm_flags, work_list);
408         if (rc < 0)
409                 GOTO(out, *err = rc);
410
411         if (rc != 2) {
412                 rc2 = ldlm_inodebits_compat_queue(&res->lr_waiting, lock,
413                                                   ldlm_flags, work_list);
414                 if (rc2 < 0)
415                         GOTO(out, *err = rc = rc2);
416         }
417
418         if (rc + rc2 != 2) {
419                 /* if there were only bits to try and all are conflicting */
420                 if ((lock->l_policy_data.l_inodebits.bits |
421                      lock->l_policy_data.l_inodebits.try_bits)) {
422                         /* There is no sense to set LDLM_FL_NO_TIMEOUT to
423                          * @ldlm_flags for DOM lock while they are enqueued
424                          * through intents, i.e. @lock here is local which does
425                          * not timeout. */
426                         *err = ELDLM_OK;
427                 }
428         } else {
429                 /* grant also all remaining try_bits */
430                 if (lock->l_policy_data.l_inodebits.try_bits != 0) {
431                         lock->l_policy_data.l_inodebits.bits |=
432                                 lock->l_policy_data.l_inodebits.try_bits;
433                         lock->l_policy_data.l_inodebits.try_bits = 0;
434                         *ldlm_flags |= LDLM_FL_LOCK_CHANGED;
435                 }
436                 LASSERT(lock->l_policy_data.l_inodebits.bits);
437                 ldlm_resource_unlink_lock(lock);
438                 ldlm_grant_lock(lock, grant_work);
439                 *err = ELDLM_OK;
440         }
441
442         RETURN(LDLM_ITER_CONTINUE);
443 out:
444         return rc;
445 }
446 #endif /* HAVE_SERVER_SUPPORT */
447
448 void ldlm_ibits_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
449                                      union ldlm_policy_data *lpolicy)
450 {
451         lpolicy->l_inodebits.bits = wpolicy->l_inodebits.bits;
452         lpolicy->l_inodebits.li_gid = wpolicy->l_inodebits.li_gid;
453         /**
454          * try_bits are to be handled outside of generic write_to_local due
455          * to different behavior on a server and client.
456          */
457 }
458
459 void ldlm_ibits_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
460                                      union ldlm_wire_policy_data *wpolicy)
461 {
462         memset(wpolicy, 0, sizeof(*wpolicy));
463         wpolicy->l_inodebits.bits = lpolicy->l_inodebits.bits;
464         wpolicy->l_inodebits.try_bits = lpolicy->l_inodebits.try_bits;
465         wpolicy->l_inodebits.li_gid = lpolicy->l_inodebits.li_gid;
466 }
467
468 /**
469  * Attempt to convert already granted IBITS lock with several bits set to
470  * a lock with less bits (downgrade).
471  *
472  * Such lock conversion is used to keep lock with non-blocking bits instead of
473  * cancelling it, introduced for better support of DoM files.
474  */
475 int ldlm_inodebits_drop(struct ldlm_lock *lock, __u64 to_drop)
476 {
477         ENTRY;
478
479         check_res_locked(lock->l_resource);
480
481         /* Just return if there are no conflicting bits */
482         if ((lock->l_policy_data.l_inodebits.bits & to_drop) == 0) {
483                 LDLM_WARN(lock, "try to drop unset bits %#llx/%#llx",
484                           lock->l_policy_data.l_inodebits.bits, to_drop);
485                 /* nothing to do */
486                 RETURN(0);
487         }
488
489         /* remove lock from a skiplist and put in the new place
490          * according with new inodebits */
491         ldlm_resource_unlink_lock(lock);
492         lock->l_policy_data.l_inodebits.bits &= ~to_drop;
493         ldlm_grant_lock_with_skiplist(lock);
494         RETURN(0);
495 }
496 EXPORT_SYMBOL(ldlm_inodebits_drop);
497
498 /* convert single lock */
499 int ldlm_cli_inodebits_convert(struct ldlm_lock *lock,
500                                enum ldlm_cancel_flags cancel_flags)
501 {
502         struct ldlm_namespace *ns = ldlm_lock_to_ns(lock);
503         struct ldlm_lock_desc ld = { { 0 } };
504         __u64 drop_bits, new_bits;
505         __u32 flags = 0;
506         int rc;
507
508         ENTRY;
509
510         check_res_locked(lock->l_resource);
511
512         /* Lock is being converted already */
513         if (ldlm_is_converting(lock)) {
514                 if (!(cancel_flags & LCF_ASYNC)) {
515                         unlock_res_and_lock(lock);
516                         wait_event_idle(lock->l_waitq,
517                                         is_lock_converted(lock));
518                         lock_res_and_lock(lock);
519                 }
520                 RETURN(0);
521         }
522
523         /* lru_cancel may happen in parallel and call ldlm_cli_cancel_list()
524          * independently.
525          */
526         if (ldlm_is_canceling(lock))
527                 RETURN(-EINVAL);
528
529         /* no need in only local convert */
530         if (lock->l_flags & (LDLM_FL_LOCAL_ONLY | LDLM_FL_CANCEL_ON_BLOCK))
531                 RETURN(-EINVAL);
532
533         drop_bits = lock->l_policy_data.l_inodebits.cancel_bits;
534         /* no cancel bits - means that caller needs full cancel */
535         if (drop_bits == 0)
536                 RETURN(-EINVAL);
537
538         new_bits = lock->l_policy_data.l_inodebits.bits & ~drop_bits;
539         /* check if all lock bits are dropped, proceed with cancel */
540         if (!new_bits)
541                 RETURN(-EINVAL);
542
543         /* check if no dropped bits, consider this as successful convert */
544         if (lock->l_policy_data.l_inodebits.bits == new_bits)
545                 RETURN(0);
546
547         ldlm_set_converting(lock);
548         /* Finally call cancel callback for remaining bits only.
549          * It is important to have converting flag during that
550          * so blocking_ast callback can distinguish convert from
551          * cancels.
552          */
553         ld.l_policy_data.l_inodebits.cancel_bits = drop_bits;
554         unlock_res_and_lock(lock);
555         lock->l_blocking_ast(lock, &ld, lock->l_ast_data, LDLM_CB_CANCELING);
556         /* now notify server about convert */
557         rc = ldlm_cli_convert_req(lock, &flags, new_bits);
558         lock_res_and_lock(lock);
559         if (rc)
560                 GOTO(full_cancel, rc);
561
562         /* Finally clear these bits in lock ibits */
563         ldlm_inodebits_drop(lock, drop_bits);
564
565         /* Being locked again check if lock was canceled, it is important
566          * to do and don't drop cbpending below
567          */
568         if (ldlm_is_canceling(lock))
569                 GOTO(full_cancel, rc = -EINVAL);
570
571         /* also check again if more bits to be cancelled appeared */
572         if (drop_bits != lock->l_policy_data.l_inodebits.cancel_bits)
573                 GOTO(clear_converting, rc = -EAGAIN);
574
575         /* clear cbpending flag early, it is safe to match lock right after
576          * client convert because it is downgrade always.
577          */
578         ldlm_clear_cbpending(lock);
579         ldlm_clear_bl_ast(lock);
580         spin_lock(&ns->ns_lock);
581         if (list_empty(&lock->l_lru))
582                 ldlm_lock_add_to_lru_nolock(lock);
583         spin_unlock(&ns->ns_lock);
584
585         /* the job is done, zero the cancel_bits. If more conflicts appear,
586          * it will result in another cycle of ldlm_cli_inodebits_convert().
587          */
588 full_cancel:
589         lock->l_policy_data.l_inodebits.cancel_bits = 0;
590 clear_converting:
591         ldlm_clear_converting(lock);
592         RETURN(rc);
593 }
594
595 int ldlm_inodebits_alloc_lock(struct ldlm_lock *lock)
596 {
597         if (ldlm_is_ns_srv(lock)) {
598                 int i;
599
600                 OBD_SLAB_ALLOC_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
601                 if (lock->l_ibits_node == NULL)
602                         return -ENOMEM;
603                 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
604                         INIT_LIST_HEAD(&lock->l_ibits_node->lin_link[i]);
605                 lock->l_ibits_node->lock = lock;
606         } else {
607                 lock->l_ibits_node = NULL;
608         }
609         return 0;
610 }
611
612 void ldlm_inodebits_add_lock(struct ldlm_resource *res, struct list_head *head,
613                              struct ldlm_lock *lock, bool tail)
614 {
615         int i;
616
617         if (!ldlm_is_ns_srv(lock))
618                 return;
619
620         if (head == &res->lr_waiting) {
621                 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
622                         if (!(lock->l_policy_data.l_inodebits.bits & BIT(i)))
623                                 continue;
624                         if (tail)
625                                 list_add_tail(&lock->l_ibits_node->lin_link[i],
626                                          &res->lr_ibits_queues->liq_waiting[i]);
627                         else
628                                 list_add(&lock->l_ibits_node->lin_link[i],
629                                          &res->lr_ibits_queues->liq_waiting[i]);
630                 }
631         } else if (head == &res->lr_granted && lock->l_ibits_node != NULL) {
632                 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
633                         LASSERT(list_empty(&lock->l_ibits_node->lin_link[i]));
634                 OBD_SLAB_FREE_PTR(lock->l_ibits_node, ldlm_inodebits_slab);
635                 lock->l_ibits_node = NULL;
636         } else if (head != &res->lr_granted) {
637                 /* we are inserting in a middle of a list, after @head */
638                 struct ldlm_lock *orig = list_entry(head, struct ldlm_lock,
639                                                     l_res_link);
640                 LASSERT(orig->l_policy_data.l_inodebits.bits ==
641                         lock->l_policy_data.l_inodebits.bits);
642                 /* The is no a use case to insert before with exactly matched
643                  * set of bits */
644                 LASSERT(tail == false);
645
646                 for (i = 0; i < MDS_INODELOCK_NUMBITS; i++) {
647                         if (!(lock->l_policy_data.l_inodebits.bits & (1 << i)))
648                                 continue;
649                         list_add(&lock->l_ibits_node->lin_link[i],
650                                  &orig->l_ibits_node->lin_link[i]);
651                 }
652         }
653 }
654
655 void ldlm_inodebits_unlink_lock(struct ldlm_lock *lock)
656 {
657         int i;
658
659         ldlm_unlink_lock_skiplist(lock);
660         if (!ldlm_is_ns_srv(lock))
661                 return;
662
663         for (i = 0; i < MDS_INODELOCK_NUMBITS; i++)
664                 list_del_init(&lock->l_ibits_node->lin_link[i]);
665 }