+EXPORT_SYMBOL(ldlm_lock_prolong_one);
+
+static enum interval_iter ldlm_resource_prolong_cb(struct interval_node *n,
+ void *data)
+{
+ struct ldlm_prolong_args *arg = data;
+ struct ldlm_interval *node = to_ldlm_interval(n);
+ struct ldlm_lock *lock;
+
+ ENTRY;
+
+ LASSERT(!list_empty(&node->li_group));
+
+ list_for_each_entry(lock, &node->li_group, l_sl_policy) {
+ ldlm_lock_prolong_one(lock, arg);
+ }
+
+ RETURN(INTERVAL_ITER_CONT);
+}
+
+/**
+ * Walk through granted tree and prolong locks if they overlaps extent.
+ *
+ * \param[in] arg prolong args
+ */
+void ldlm_resource_prolong(struct ldlm_prolong_args *arg)
+{
+ struct ldlm_interval_tree *tree;
+ struct ldlm_resource *res;
+ struct interval_node_extent ex = { .start = arg->lpa_extent.start,
+ .end = arg->lpa_extent.end };
+ int idx;
+
+ ENTRY;
+
+ res = ldlm_resource_get(arg->lpa_export->exp_obd->obd_namespace, NULL,
+ &arg->lpa_resid, LDLM_EXTENT, 0);
+ if (IS_ERR(res)) {
+ CDEBUG(D_DLMTRACE, "Failed to get resource for resid %llu/%llu\n",
+ arg->lpa_resid.name[0], arg->lpa_resid.name[1]);
+ RETURN_EXIT;
+ }
+
+ lock_res(res);
+ for (idx = 0; idx < LCK_MODE_NUM; idx++) {
+ tree = &res->lr_itree[idx];
+ if (tree->lit_root == NULL) /* empty tree, skipped */
+ continue;
+
+ /* There is no possibility to check for the groupID
+ * so all the group locks are considered as valid
+ * here, especially because the client is supposed
+ * to check it has such a lock before sending an RPC.
+ */
+ if (!(tree->lit_mode & arg->lpa_mode))
+ continue;
+
+ interval_search(tree->lit_root, &ex,
+ ldlm_resource_prolong_cb, arg);
+ }
+
+ unlock_res(res);
+ ldlm_resource_putref(res);
+
+ EXIT;
+}
+EXPORT_SYMBOL(ldlm_resource_prolong);
+
+/**
+ * Process a granting attempt for extent lock.
+ * Must be called with ns lock held.
+ *
+ * This function looks for any conflicts for \a lock in the granted or
+ * waiting queues. The lock is granted if no conflicts are found in
+ * either queue.
+ */
+int ldlm_process_extent_lock(struct ldlm_lock *lock, __u64 *flags,
+ enum ldlm_process_intention intention,
+ enum ldlm_error *err, struct list_head *work_list)
+{
+ struct ldlm_resource *res = lock->l_resource;
+ struct list_head rpc_list;
+ int rc, rc2;
+ int contended_locks = 0;
+ ENTRY;
+
+ LASSERT(lock->l_granted_mode != lock->l_req_mode);
+ LASSERT(list_empty(&res->lr_converting));
+ LASSERT(!(*flags & LDLM_FL_DENY_ON_CONTENTION) ||
+ !ldlm_is_ast_discard_data(lock));
+ INIT_LIST_HEAD(&rpc_list);
+ check_res_locked(res);
+ *err = ELDLM_OK;
+
+ if (intention == LDLM_PROCESS_RESCAN) {
+ /* Careful observers will note that we don't handle -EWOULDBLOCK
+ * here, but it's ok for a non-obvious reason -- compat_queue
+ * can only return -EWOULDBLOCK if (flags & BLOCK_NOWAIT |
+ * SPECULATIVE). flags should always be zero here, and if that
+ * ever stops being true, we want to find out. */
+ LASSERT(*flags == 0);
+ rc = ldlm_extent_compat_queue(&res->lr_granted, lock, flags,
+ err, NULL, &contended_locks);
+ if (rc == 1) {
+ rc = ldlm_extent_compat_queue(&res->lr_waiting, lock,
+ flags, err, NULL,
+ &contended_locks);
+ }
+ if (rc == 0)
+ RETURN(LDLM_ITER_STOP);
+
+ ldlm_resource_unlink_lock(lock);
+
+ if (!OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_EVICT_RACE))
+ ldlm_extent_policy(res, lock, flags);
+ ldlm_grant_lock(lock, work_list);
+ RETURN(LDLM_ITER_CONTINUE);
+ }
+
+ LASSERT((intention == LDLM_PROCESS_ENQUEUE && work_list == NULL) ||
+ (intention == LDLM_PROCESS_RECOVERY && work_list != NULL));
+ restart:
+ contended_locks = 0;
+ rc = ldlm_extent_compat_queue(&res->lr_granted, lock, flags, err,
+ &rpc_list, &contended_locks);
+ if (rc < 0)
+ GOTO(out_rpc_list, rc);
+
+ rc2 = 0;
+ if (rc != 2) {
+ rc2 = ldlm_extent_compat_queue(&res->lr_waiting, lock,
+ flags, err, &rpc_list,
+ &contended_locks);
+ if (rc2 < 0)
+ GOTO(out_rpc_list, rc = rc2);
+ }
+
+ if (rc + rc2 != 2) {
+ /* Adding LDLM_FL_NO_TIMEOUT flag to granted lock to force
+ * client to wait for the lock endlessly once the lock is
+ * enqueued -bzzz */
+ rc = ldlm_handle_conflict_lock(lock, flags, &rpc_list,
+ LDLM_FL_NO_TIMEOUT);
+ if (rc == -ERESTART)
+ GOTO(restart, rc);
+ *err = rc;
+ } else {
+ ldlm_extent_policy(res, lock, flags);
+ ldlm_resource_unlink_lock(lock);
+ ldlm_grant_lock(lock, work_list);
+ rc = 0;
+ }
+
+out_rpc_list:
+ if (!list_empty(&rpc_list)) {
+ LASSERT(!ldlm_is_ast_discard_data(lock));
+ ldlm_discard_bl_list(&rpc_list);
+ }
+ RETURN(rc);
+}
+#endif /* HAVE_SERVER_SUPPORT */
+
+struct ldlm_kms_shift_args {
+ __u64 old_kms;
+ __u64 kms;
+ bool complete;
+};
+
+/* Callback for interval_iterate functions, used by ldlm_extent_shift_Kms */
+static enum interval_iter ldlm_kms_shift_cb(struct interval_node *n,
+ void *args)
+{
+ struct ldlm_kms_shift_args *arg = args;
+ struct ldlm_interval *node = to_ldlm_interval(n);
+ struct ldlm_lock *tmplock;
+ struct ldlm_lock *lock = NULL;
+
+ ENTRY;
+
+ /* Since all locks in an interval have the same extent, we can just
+ * use the first lock without kms_ignore set. */
+ list_for_each_entry(tmplock, &node->li_group, l_sl_policy) {
+ if (ldlm_is_kms_ignore(tmplock))
+ continue;
+
+ lock = tmplock;
+
+ break;
+ }
+
+ /* No locks in this interval without kms_ignore set */
+ if (!lock)
+ RETURN(INTERVAL_ITER_CONT);
+
+ /* If we find a lock with a greater or equal kms, we are not the
+ * highest lock (or we share that distinction with another lock), and
+ * don't need to update KMS. Return old_kms and stop looking. */
+ if (lock->l_policy_data.l_extent.end >= arg->old_kms) {
+ arg->kms = arg->old_kms;
+ arg->complete = true;
+ RETURN(INTERVAL_ITER_STOP);
+ }
+
+ if (lock->l_policy_data.l_extent.end + 1 > arg->kms)
+ arg->kms = lock->l_policy_data.l_extent.end + 1;
+
+ /* Since interval_iterate_reverse starts with the highest lock and
+ * works down, for PW locks, we only need to check if we should update
+ * the kms, then stop walking the tree. PR locks are not exclusive, so
+ * the highest start does not imply the highest end and we must
+ * continue. (Only one group lock is allowed per resource, so this is
+ * irrelevant for group locks.)*/
+ if (lock->l_granted_mode == LCK_PW)
+ RETURN(INTERVAL_ITER_STOP);
+ else
+ RETURN(INTERVAL_ITER_CONT);
+}
+
+/* When a lock is cancelled by a client, the KMS may undergo change if this
+ * is the "highest lock". This function returns the new KMS value, updating
+ * it only if we were the highest lock.
+ *
+ * Caller must hold lr_lock already.
+ *
+ * NB: A lock on [x,y] protects a KMS of up to y + 1 bytes! */
+__u64 ldlm_extent_shift_kms(struct ldlm_lock *lock, __u64 old_kms)
+{
+ struct ldlm_resource *res = lock->l_resource;
+ struct ldlm_interval_tree *tree;
+ struct ldlm_kms_shift_args args;
+ int idx = 0;
+
+ ENTRY;
+
+ args.old_kms = old_kms;
+ args.kms = 0;
+ args.complete = false;
+
+ /* don't let another thread in ldlm_extent_shift_kms race in
+ * just after we finish and take our lock into account in its
+ * calculation of the kms */
+ ldlm_set_kms_ignore(lock);
+
+ /* We iterate over the lock trees, looking for the largest kms smaller
+ * than the current one. */
+ for (idx = 0; idx < LCK_MODE_NUM; idx++) {
+ tree = &res->lr_itree[idx];
+
+ /* If our already known kms is >= than the highest 'end' in
+ * this tree, we don't need to check this tree, because
+ * the kms from a tree can be lower than in_max_high (due to
+ * kms_ignore), but it can never be higher. */
+ if (!tree->lit_root || args.kms >= tree->lit_root->in_max_high)
+ continue;
+
+ interval_iterate_reverse(tree->lit_root, ldlm_kms_shift_cb,
+ &args);
+
+ /* this tells us we're not the highest lock, so we don't need
+ * to check the remaining trees */
+ if (args.complete)
+ break;
+ }
+
+ LASSERTF(args.kms <= args.old_kms, "kms %llu old_kms %llu\n", args.kms,
+ args.old_kms);
+
+ RETURN(args.kms);
+}
+EXPORT_SYMBOL(ldlm_extent_shift_kms);
+
+struct kmem_cache *ldlm_interval_slab;
+struct ldlm_interval *ldlm_interval_alloc(struct ldlm_lock *lock)
+{
+ struct ldlm_interval *node;
+ ENTRY;
+
+ LASSERT(lock->l_resource->lr_type == LDLM_EXTENT);
+ OBD_SLAB_ALLOC_PTR_GFP(node, ldlm_interval_slab, GFP_NOFS);
+ if (node == NULL)
+ RETURN(NULL);
+
+ INIT_LIST_HEAD(&node->li_group);
+ ldlm_interval_attach(node, lock);
+ RETURN(node);
+}
+
+void ldlm_interval_free(struct ldlm_interval *node)
+{
+ if (node) {
+ LASSERT(list_empty(&node->li_group));
+ LASSERT(!interval_is_intree(&node->li_node));
+ OBD_SLAB_FREE(node, ldlm_interval_slab, sizeof(*node));
+ }
+}
+
+/* interval tree, for LDLM_EXTENT. */
+void ldlm_interval_attach(struct ldlm_interval *n,
+ struct ldlm_lock *l)
+{
+ LASSERT(l->l_tree_node == NULL);
+ LASSERT(l->l_resource->lr_type == LDLM_EXTENT);
+
+ list_add_tail(&l->l_sl_policy, &n->li_group);
+ l->l_tree_node = n;
+}
+
+struct ldlm_interval *ldlm_interval_detach(struct ldlm_lock *l)
+{
+ struct ldlm_interval *n = l->l_tree_node;
+
+ if (n == NULL)
+ return NULL;
+
+ LASSERT(!list_empty(&n->li_group));
+ l->l_tree_node = NULL;
+ list_del_init(&l->l_sl_policy);
+
+ return list_empty(&n->li_group) ? n : NULL;
+}
+
+static inline int ldlm_mode_to_index(enum ldlm_mode mode)
+{
+ int index;
+
+ LASSERT(mode != 0);
+ LASSERT(is_power_of_2(mode));
+ for (index = -1; mode != 0; index++, mode >>= 1)
+ /* do nothing */;
+ LASSERT(index < LCK_MODE_NUM);
+ return index;
+}
+
+/** Add newly granted lock into interval tree for the resource. */
+void ldlm_extent_add_lock(struct ldlm_resource *res,
+ struct ldlm_lock *lock)
+{
+ struct interval_node *found, **root;
+ struct ldlm_interval *node;
+ struct ldlm_extent *extent;
+ int idx, rc;
+
+ LASSERT(lock->l_granted_mode == lock->l_req_mode);
+
+ node = lock->l_tree_node;
+ LASSERT(node != NULL);
+ LASSERT(!interval_is_intree(&node->li_node));
+
+ idx = ldlm_mode_to_index(lock->l_granted_mode);
+ LASSERT(lock->l_granted_mode == 1 << idx);
+ LASSERT(lock->l_granted_mode == res->lr_itree[idx].lit_mode);
+
+ /* node extent initialize */
+ extent = &lock->l_policy_data.l_extent;
+
+ rc = interval_set(&node->li_node, extent->start, extent->end);
+ LASSERT(!rc);
+
+ root = &res->lr_itree[idx].lit_root;
+ found = interval_insert(&node->li_node, root);
+ if (found) { /* The policy group found. */
+ struct ldlm_interval *tmp = ldlm_interval_detach(lock);
+ LASSERT(tmp != NULL);
+ ldlm_interval_free(tmp);
+ ldlm_interval_attach(to_ldlm_interval(found), lock);
+ }
+ res->lr_itree[idx].lit_size++;
+
+ /* even though we use interval tree to manage the extent lock, we also
+ * add the locks into grant list, for debug purpose, .. */
+ ldlm_resource_add_lock(res, &res->lr_granted, lock);
+
+ if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GRANT_CHECK)) {
+ struct ldlm_lock *lck;
+
+ list_for_each_entry_reverse(lck, &res->lr_granted,
+ l_res_link) {
+ if (lck == lock)
+ continue;
+ if (lockmode_compat(lck->l_granted_mode,
+ lock->l_granted_mode))
+ continue;
+ if (ldlm_extent_overlap(&lck->l_req_extent,
+ &lock->l_req_extent)) {
+ CDEBUG(D_ERROR, "granting conflicting lock %p "
+ "%p\n", lck, lock);
+ ldlm_resource_dump(D_ERROR, res);
+ LBUG();
+ }
+ }
+ }
+}
+
+/** Remove cancelled lock from resource interval tree. */
+void ldlm_extent_unlink_lock(struct ldlm_lock *lock)
+{
+ struct ldlm_resource *res = lock->l_resource;
+ struct ldlm_interval *node = lock->l_tree_node;
+ struct ldlm_interval_tree *tree;
+ int idx;
+
+ if (!node || !interval_is_intree(&node->li_node)) /* duplicate unlink */
+ return;
+
+ idx = ldlm_mode_to_index(lock->l_granted_mode);
+ LASSERT(lock->l_granted_mode == 1 << idx);
+ tree = &res->lr_itree[idx];
+
+ LASSERT(tree->lit_root != NULL); /* assure the tree is not null */
+
+ tree->lit_size--;
+ node = ldlm_interval_detach(lock);
+ if (node) {
+ interval_erase(&node->li_node, &tree->lit_root);
+ ldlm_interval_free(node);
+ }
+}
+
+void ldlm_extent_policy_wire_to_local(const union ldlm_wire_policy_data *wpolicy,
+ union ldlm_policy_data *lpolicy)
+{
+ lpolicy->l_extent.start = wpolicy->l_extent.start;
+ lpolicy->l_extent.end = wpolicy->l_extent.end;
+ lpolicy->l_extent.gid = wpolicy->l_extent.gid;
+}
+
+void ldlm_extent_policy_local_to_wire(const union ldlm_policy_data *lpolicy,
+ union ldlm_wire_policy_data *wpolicy)
+{
+ memset(wpolicy, 0, sizeof(*wpolicy));
+ wpolicy->l_extent.start = lpolicy->l_extent.start;
+ wpolicy->l_extent.end = lpolicy->l_extent.end;
+ wpolicy->l_extent.gid = lpolicy->l_extent.gid;
+}
+