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