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