4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ofd/ofd_dlm.c
34 * This file contains OBD Filter Device (OFD) LDLM-related code which is just
35 * intent handling for glimpse lock.
37 * Author: Andreas Dilger <andreas.dilger@intel.com>
38 * Author: Jinshan Xiong <jinshan.xiong@intel.com>
39 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
40 * Author: Mikhail Pershin <mike.pershin@intel.com>
43 #define DEBUG_SUBSYSTEM S_FILTER
45 #include "ofd_internal.h"
47 struct ofd_intent_args {
48 struct list_head gl_list;
55 * OFD interval callback.
57 * The interval_callback_t is part of interval_iterate_reverse() and is called
58 * for each interval in tree. The OFD interval callback searches for locks
59 * covering extents beyond the given args->size. This is used to decide if the
60 * size is too small and needs to be updated. Note that we are only interested
61 * in growing the size, as truncate is the only operation which can shrink it,
62 * and it is handled differently. This is why we only look at locks beyond the
65 * It finds the highest lock (by starting point) in this interval, and adds it
66 * to the list of locks to glimpse. We must glimpse a list of locks - rather
67 * than only the highest lock on the file - because lockahead creates extent
68 * locks in advance of IO, and so breaks the assumption that the holder of the
69 * highest lock knows the current file size.
71 * This assumption is normally true because locks which are created as part of
72 * IO - rather than in advance of it - are guaranteed to be 'active', i.e.,
73 * involved in IO, and the holder of the highest 'active' lock always knows the
74 * current file size, because the size is either not changing or the holder of
75 * that lock is responsible for updating it.
77 * So we need only glimpse until we find the first client with an 'active'
80 * Unfortunately, there is no way to know if a manually requested/speculative
81 * lock is 'active' from the server side. So when we see a potentially
82 * speculative lock, we must send a glimpse for that lock unless we have
83 * already sent a glimpse to the holder of that lock.
85 * However, *all* non-speculative locks are active. So we can stop glimpsing
86 * as soon as we find a non-speculative lock. Currently, all speculative PW
87 * locks have LDLM_FL_NO_EXPANSION set, and we use this to identify them. This
88 * is enforced by an assertion in osc_lock_init, which references this comment.
90 * If that ever changes, we will either need to find a new way to identify
91 * active locks or we will need to consider all PW locks (we will still only
92 * glimpse one per client).
94 * Note that it is safe to glimpse only the 'top' lock from each interval
95 * because ofd_intent_cb is only called for PW extent locks, and for PW locks,
96 * there is only one lock per interval.
98 * \param[in] n interval node
99 * \param[in,out] args intent arguments, gl work list for identified locks
101 * \retval INTERVAL_ITER_STOP if the interval is lower than
102 * file size, caller stops execution
103 * \retval INTERVAL_ITER_CONT if callback finished successfully
104 * and caller may continue execution
106 static enum interval_iter ofd_intent_cb(struct interval_node *n, void *args)
108 struct ldlm_interval *node = (struct ldlm_interval *)n;
109 struct ofd_intent_args *arg = args;
110 __u64 size = arg->size;
111 struct ldlm_lock *victim_lock = NULL;
112 struct ldlm_lock *lck;
113 struct ldlm_glimpse_work *gl_work = NULL;
116 /* If the interval is lower than the current file size, just break. */
117 if (interval_high(n) <= size)
118 GOTO(out, rc = INTERVAL_ITER_STOP);
120 /* Find the 'victim' lock from this interval */
121 list_for_each_entry(lck, &node->li_group, l_sl_policy) {
123 victim_lock = LDLM_LOCK_GET(lck);
125 /* the same policy group - every lock has the
126 * same extent, so needn't do it any more */
130 /* l_export can be null in race with eviction - In that case, we will
131 * not find any locks in this interval */
133 GOTO(out, rc = INTERVAL_ITER_CONT);
136 * This check is for lock taken in ofd_destroy_by_fid() that does
137 * not have l_glimpse_ast set. So the logic is: if there is a lock
138 * with no l_glimpse_ast set, this object is being destroyed already.
139 * Hence, if you are grabbing DLM locks on the server, always set
140 * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
142 if (victim_lock->l_glimpse_ast == NULL) {
143 LDLM_DEBUG(victim_lock, "no l_glimpse_ast");
144 arg->no_glimpse_ast = true;
145 GOTO(out_release, rc = INTERVAL_ITER_STOP);
148 /* If NO_EXPANSION is not set, this is an active lock, and we don't need
149 * to glimpse any further once we've glimpsed the client holding this
150 * lock. So set us up to stop. See comment above this function. */
151 if (!(victim_lock->l_flags & LDLM_FL_NO_EXPANSION))
152 rc = INTERVAL_ITER_STOP;
154 rc = INTERVAL_ITER_CONT;
156 /* Check to see if we're already set up to send a glimpse to this
157 * client; if so, don't add this lock to the glimpse list - We need
158 * only glimpse each client once. (And if we know that client holds
159 * an active lock, we can stop glimpsing. So keep the rc set in the
161 list_for_each_entry(gl_work, &arg->gl_list, gl_list) {
162 if (gl_work->gl_lock->l_export == victim_lock->l_export)
163 GOTO(out_release, rc);
166 if (!OBD_FAIL_CHECK(OBD_FAIL_OST_GL_WORK_ALLOC))
167 OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem,
171 arg->error = -ENOMEM;
172 GOTO(out_release, rc = INTERVAL_ITER_STOP);
175 /* Populate the gl_work structure. */
176 gl_work->gl_lock = victim_lock;
177 list_add_tail(&gl_work->gl_list, &arg->gl_list);
178 /* There is actually no need for a glimpse descriptor when glimpsing
180 gl_work->gl_desc = NULL;
181 /* This tells ldlm_work_gl_ast_lock this was allocated from a slab and
182 * must be freed in a slab-aware manner. */
183 gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
188 /* If the victim doesn't go on the glimpse list, we must release it */
189 LDLM_LOCK_RELEASE(victim_lock);
195 * OFD lock intent policy
197 * This defines ldlm_namespace::ns_policy interface for OFD.
198 * Intent policy is called when lock has an intent, for OFD that
199 * means glimpse lock and policy fills Lock Value Block (LVB).
201 * If already granted lock is found it will be placed in \a lockp and
202 * returned back to caller function.
204 * \param[in] ns namespace
205 * \param[in,out] lockp pointer to the lock
206 * \param[in] req_cookie incoming request
207 * \param[in] mode LDLM mode
208 * \param[in] flags LDLM flags
209 * \param[in] data opaque data, not used in OFD policy
211 * \retval ELDLM_LOCK_REPLACED if already granted lock was found
212 * and placed in \a lockp
213 * \retval ELDLM_LOCK_ABORTED in other cases except error
214 * \retval negative errno on error
216 int ofd_intent_policy(struct ldlm_namespace *ns, struct ldlm_lock **lockp,
217 void *req_cookie, enum ldlm_mode mode, __u64 flags,
220 struct ptlrpc_request *req = req_cookie;
221 struct ldlm_lock *lock = *lockp;
222 struct ldlm_resource *res = lock->l_resource;
223 ldlm_processing_policy policy;
224 struct ost_lvb *res_lvb, *reply_lvb;
225 struct ldlm_reply *rep;
228 struct ldlm_interval_tree *tree;
229 struct ofd_intent_args arg;
231 [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
232 [DLM_LOCKREPLY_OFF] = sizeof(*rep),
233 [DLM_REPLY_REC_OFF] = sizeof(*reply_lvb)
235 struct ldlm_glimpse_work *pos, *tmp;
238 /* update stats for intent in intent policy */
239 if (ptlrpc_req2svc(req)->srv_stats != NULL)
240 lprocfs_counter_incr(ptlrpc_req2svc(req)->srv_stats,
241 PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE);
243 INIT_LIST_HEAD(&arg.gl_list);
244 arg.no_glimpse_ast = false;
246 lock->l_lvb_type = LVB_T_OST;
247 policy = ldlm_get_processing_policy(res);
248 LASSERT(policy != NULL);
249 LASSERT(req != NULL);
251 rc = lustre_pack_reply(req, 3, repsize, NULL);
253 RETURN(req->rq_status = rc);
255 rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
256 LASSERT(rep != NULL);
258 reply_lvb = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
260 LASSERT(reply_lvb != NULL);
262 /* Call the extent policy function to see if our request can be
263 * granted, or is blocked.
264 * If the OST lock has LDLM_FL_HAS_INTENT set, it means a glimpse
265 * lock, and should not be granted if the lock will be blocked.
268 if (flags & LDLM_FL_BLOCK_NOWAIT) {
269 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_AGL_DELAY, 5);
271 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_AGL_NOLOCK))
272 RETURN(ELDLM_LOCK_ABORTED);
275 LASSERT(ns == ldlm_res_to_ns(res));
278 /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
279 * lock was found by ldlm_handle_enqueue(); if so no need to grant
281 if (flags & LDLM_FL_RESENT) {
282 rc = LDLM_ITER_CONTINUE;
285 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
286 check_res_locked(res);
289 /* The lock met with no resistance; we're finished. */
290 if (rc == LDLM_ITER_CONTINUE) {
291 if (OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2)) {
292 ldlm_resource_unlink_lock(lock);
293 err = ELDLM_LOCK_ABORTED;
295 err = ELDLM_LOCK_REPLACED;
299 } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
300 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
301 * callback for glimpse size. The real size user will trigger
302 * the glimpse callback when necessary. */
304 RETURN(ELDLM_LOCK_ABORTED);
307 /* Do not grant any lock, but instead send GL callbacks. The extent
308 * policy nicely created a list of all PW locks for us. We will choose
309 * the highest of those which are larger than the size in the LVB, if
310 * any, and perform a glimpse callback. */
311 res_lvb = res->lr_lvb_data;
312 LASSERT(res_lvb != NULL);
313 *reply_lvb = *res_lvb;
316 * ->ns_lock guarantees that no new locks are granted, and,
317 * therefore, that res->lr_lvb_data cannot increase beyond the
318 * end of already granted lock. As a result, it is safe to
319 * check against "stale" reply_lvb->lvb_size value without
322 arg.size = reply_lvb->lvb_size;
324 /* Check for PW locks beyond the size in the LVB, build the list
325 * of locks to glimpse (arg.gl_list) */
326 for (idx = 0; idx < LCK_MODE_NUM; idx++) {
327 tree = &res->lr_itree[idx];
328 if (tree->lit_mode == LCK_PR)
331 interval_iterate_reverse(tree->lit_root, ofd_intent_cb, &arg);
334 GOTO(out, rc = arg.error);
339 /* There were no PW locks beyond the size in the LVB; finished. */
340 if (list_empty(&arg.gl_list))
341 RETURN(ELDLM_LOCK_ABORTED);
343 if (arg.no_glimpse_ast) {
344 /* We are racing with unlink(); just return -ENOENT */
345 rep->lock_policy_res1 = ptlrpc_status_hton(-ENOENT);
346 GOTO(out, ELDLM_LOCK_ABORTED);
349 /* this will update the LVB */
350 ldlm_glimpse_locks(res, &arg.gl_list);
353 *reply_lvb = *res_lvb;
357 /* If the list is not empty, we failed to glimpse some locks and
358 * must clean up. Usually due to a race with unlink.*/
359 list_for_each_entry_safe(pos, tmp, &arg.gl_list, gl_list) {
360 list_del(&pos->gl_list);
361 LDLM_LOCK_RELEASE(pos->gl_lock);
362 OBD_SLAB_FREE_PTR(pos, ldlm_glimpse_work_kmem);
365 RETURN(rc < 0 ? rc : ELDLM_LOCK_ABORTED);