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