+/* The purpose of this function is to return:
+ * - the maximum extent
+ * - containing the requested extent
+ * - and not overlapping existing conflicting extents outside the requested one
+ *
+ * Use interval tree to expand the lock extent for granted lock.
+ */
+static void ldlm_extent_internal_policy_granted(struct ldlm_lock *req,
+ struct ldlm_extent *new_ex)
+{
+ struct ldlm_resource *res = req->l_resource;
+ ldlm_mode_t req_mode = req->l_req_mode;
+ __u64 req_start = req->l_req_extent.start;
+ __u64 req_end = req->l_req_extent.end;
+ struct ldlm_interval_tree *tree;
+ struct interval_node_extent limiter = { new_ex->start, new_ex->end };
+ int conflicting = 0;
+ int idx;
+ ENTRY;
+
+ lockmode_verify(req_mode);
+
+ /* using interval tree to handle the ldlm extent granted locks */
+ for (idx = 0; idx < LCK_MODE_NUM; idx++) {
+ struct interval_node_extent ext = { req_start, req_end };
+
+ tree = &res->lr_itree[idx];
+ if (lockmode_compat(tree->lit_mode, req_mode))
+ continue;
+
+ conflicting += tree->lit_size;
+ if (conflicting > 4)
+ limiter.start = req_start;
+
+ if (interval_is_overlapped(tree->lit_root, &ext))
+ CDEBUG(D_INFO,
+ "req_mode = %d, tree->lit_mode = %d, "
+ "tree->lit_size = %d\n",
+ req_mode, tree->lit_mode, tree->lit_size);
+ interval_expand(tree->lit_root, &ext, &limiter);
+ limiter.start = max(limiter.start, ext.start);
+ limiter.end = min(limiter.end, ext.end);
+ if (limiter.start == req_start && limiter.end == req_end)
+ break;
+ }
+
+ new_ex->start = limiter.start;
+ new_ex->end = limiter.end;
+ LASSERT(new_ex->start <= req_start);
+ LASSERT(new_ex->end >= req_end);
+
+ ldlm_extent_internal_policy_fixup(req, new_ex, conflicting);
+ EXIT;
+}
+
+/* The purpose of this function is to return:
+ * - the maximum extent
+ * - containing the requested extent
+ * - and not overlapping existing conflicting extents outside the requested one
+ */
+static void
+ldlm_extent_internal_policy_waiting(struct ldlm_lock *req,
+ struct ldlm_extent *new_ex)
+{
+ cfs_list_t *tmp;
+ struct ldlm_resource *res = req->l_resource;
+ ldlm_mode_t req_mode = req->l_req_mode;
+ __u64 req_start = req->l_req_extent.start;
+ __u64 req_end = req->l_req_extent.end;
+ int conflicting = 0;
+ ENTRY;
+
+ lockmode_verify(req_mode);
+
+ /* for waiting locks */
+ cfs_list_for_each(tmp, &res->lr_waiting) {
+ struct ldlm_lock *lock;
+ struct ldlm_extent *l_extent;
+
+ lock = cfs_list_entry(tmp, struct ldlm_lock, l_res_link);
+ l_extent = &lock->l_policy_data.l_extent;
+
+ /* We already hit the minimum requested size, search no more */
+ if (new_ex->start == req_start && new_ex->end == req_end) {
+ EXIT;
+ return;
+ }
+
+ /* Don't conflict with ourselves */
+ if (req == lock)
+ continue;
+
+ /* Locks are compatible, overlap doesn't matter */
+ /* Until bug 20 is fixed, try to avoid granting overlapping
+ * locks on one client (they take a long time to cancel) */
+ if (lockmode_compat(lock->l_req_mode, req_mode) &&
+ lock->l_export != req->l_export)
+ continue;
+
+ /* If this is a high-traffic lock, don't grow downwards at all
+ * or grow upwards too much */
+ ++conflicting;
+ if (conflicting > 4)
+ new_ex->start = req_start;
+
+ /* If lock doesn't overlap new_ex, skip it. */
+ if (!ldlm_extent_overlap(l_extent, new_ex))
+ continue;
+
+ /* Locks conflicting in requested extents and we can't satisfy
+ * both locks, so ignore it. Either we will ping-pong this
+ * extent (we would regardless of what extent we granted) or
+ * lock is unused and it shouldn't limit our extent growth. */
+ if (ldlm_extent_overlap(&lock->l_req_extent,&req->l_req_extent))
+ continue;
+
+ /* We grow extents downwards only as far as they don't overlap
+ * with already-granted locks, on the assumption that clients
+ * will be writing beyond the initial requested end and would
+ * then need to enqueue a new lock beyond previous request.
+ * l_req_extent->end strictly < req_start, checked above. */
+ if (l_extent->start < req_start && new_ex->start != req_start) {
+ if (l_extent->end >= req_start)
+ new_ex->start = req_start;
+ else
+ new_ex->start = min(l_extent->end+1, req_start);
+ }
+
+ /* If we need to cancel this lock anyways because our request
+ * overlaps the granted lock, we grow up to its requested
+ * extent start instead of limiting this extent, assuming that
+ * clients are writing forwards and the lock had over grown
+ * its extent downwards before we enqueued our request. */
+ if (l_extent->end > req_end) {
+ if (l_extent->start <= req_end)
+ new_ex->end = max(lock->l_req_extent.start - 1,
+ req_end);
+ else
+ new_ex->end = max(l_extent->start - 1, req_end);
+ }
+ }
+
+ ldlm_extent_internal_policy_fixup(req, new_ex, conflicting);
+ EXIT;
+}
+
+
+/* In order to determine the largest possible extent we can grant, we need
+ * to scan all of the queues. */
+static void ldlm_extent_policy(struct ldlm_resource *res,
+ struct ldlm_lock *lock, int *flags)
+{
+ struct ldlm_extent new_ex = { .start = 0, .end = OBD_OBJECT_EOF };
+
+ if (lock->l_export == NULL)
+ /*
+ * this is local lock taken by server (e.g., as a part of
+ * OST-side locking, or unlink handling). Expansion doesn't
+ * make a lot of sense for local locks, because they are
+ * dropped immediately on operation completion and would only
+ * conflict with other threads.
+ */
+ return;
+
+ if (lock->l_policy_data.l_extent.start == 0 &&
+ lock->l_policy_data.l_extent.end == OBD_OBJECT_EOF)
+ /* fast-path whole file locks */
+ return;
+
+ ldlm_extent_internal_policy_granted(lock, &new_ex);
+ ldlm_extent_internal_policy_waiting(lock, &new_ex);
+
+ if (new_ex.start != lock->l_policy_data.l_extent.start ||
+ new_ex.end != lock->l_policy_data.l_extent.end) {
+ *flags |= LDLM_FL_LOCK_CHANGED;
+ lock->l_policy_data.l_extent.start = new_ex.start;
+ lock->l_policy_data.l_extent.end = new_ex.end;
+ }
+}