Whamcloud - gitweb
LU-3285 mdc: add IO stats in mdc
[fs/lustre-release.git] / lustre / ofd / ofd_dlm.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) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ofd/ofd_dlm.c
33  *
34  * This file contains OBD Filter Device (OFD) LDLM-related code which is just
35  * intent handling for glimpse lock.
36  *
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>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include "ofd_internal.h"
46
47 struct ofd_intent_args {
48         struct list_head        gl_list;
49         __u64                    size;
50         bool                    no_glimpse_ast;
51         int                     error;
52 };
53
54 /**
55  * OFD interval callback.
56  *
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
63  * current size.
64  *
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.
70  *
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.
76  *
77  * So we need only glimpse until we find the first client with an 'active'
78  * lock.
79  *
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.
84  *
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.
89  *
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).
93  *
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.
97  *
98  * \param[in] n         interval node
99  * \param[in,out] args  intent arguments, gl work list for identified locks
100  *
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
105  */
106 static enum interval_iter ofd_intent_cb(struct interval_node *n, void *args)
107 {
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;
114         int rc = 0;
115
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);
119
120         /* Find the 'victim' lock from this interval */
121         list_for_each_entry(lck, &node->li_group, l_sl_policy) {
122
123                 victim_lock = LDLM_LOCK_GET(lck);
124
125                 /* the same policy group - every lock has the
126                  * same extent, so needn't do it any more */
127                 break;
128         }
129
130         /* l_export can be null in race with eviction - In that case, we will
131          * not find any locks in this interval */
132         if (!victim_lock)
133                 GOTO(out, rc = INTERVAL_ITER_CONT);
134
135         /*
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()).
141          */
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);
146         }
147
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;
153         else
154                 rc = INTERVAL_ITER_CONT;
155
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
160          * check above.) */
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);
164         }
165
166         if (!OBD_FAIL_CHECK(OBD_FAIL_OST_GL_WORK_ALLOC))
167                 OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem,
168                                        GFP_ATOMIC);
169
170         if (!gl_work) {
171                 arg->error = -ENOMEM;
172                 GOTO(out_release, rc = INTERVAL_ITER_STOP);
173         }
174
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
179          * extent locks */
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;
184
185         GOTO(out, rc);
186
187 out_release:
188         /* If the victim doesn't go on the glimpse list, we must release it */
189         LDLM_LOCK_RELEASE(victim_lock);
190
191 out:
192         return rc;
193 }
194 /**
195  * OFD lock intent policy
196  *
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).
200  *
201  * If already granted lock is found it will be placed in \a lockp and
202  * returned back to caller function.
203  *
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
210  *
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
215  */
216 int ofd_intent_policy(struct ldlm_namespace *ns, struct ldlm_lock **lockp,
217                       void *req_cookie, enum ldlm_mode mode, __u64 flags,
218                       void *data)
219 {
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;
226         enum ldlm_error err;
227         int idx, rc;
228         struct ldlm_interval_tree *tree;
229         struct ofd_intent_args arg;
230         __u32 repsize[3] = {
231                 [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
232                 [DLM_LOCKREPLY_OFF]   = sizeof(*rep),
233                 [DLM_REPLY_REC_OFF]   = sizeof(*reply_lvb)
234         };
235         struct ldlm_glimpse_work *pos, *tmp;
236         ENTRY;
237
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);
242
243         INIT_LIST_HEAD(&arg.gl_list);
244         arg.no_glimpse_ast = false;
245         arg.error = 0;
246         lock->l_lvb_type = LVB_T_OST;
247         policy = ldlm_get_processing_policy(res);
248         LASSERT(policy != NULL);
249         LASSERT(req != NULL);
250
251         rc = lustre_pack_reply(req, 3, repsize, NULL);
252         if (rc)
253                 RETURN(req->rq_status = rc);
254
255         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
256         LASSERT(rep != NULL);
257
258         reply_lvb = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
259                                    sizeof(*reply_lvb));
260         LASSERT(reply_lvb != NULL);
261
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.
266          */
267
268         if (flags & LDLM_FL_BLOCK_NOWAIT) {
269                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_AGL_DELAY, 5);
270
271                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_AGL_NOLOCK))
272                         RETURN(ELDLM_LOCK_ABORTED);
273         }
274
275         LASSERT(ns == ldlm_res_to_ns(res));
276         lock_res(res);
277
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
280          * it again. */
281         if (flags & LDLM_FL_RESENT) {
282                 rc = LDLM_ITER_CONTINUE;
283         } else {
284                 __u64 tmpflags = 0;
285                 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
286                 check_res_locked(res);
287         }
288
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;
294                 } else {
295                         err = ELDLM_LOCK_REPLACED;
296                 }
297                 unlock_res(res);
298                 RETURN(err);
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. */
303                 unlock_res(res);
304                 RETURN(ELDLM_LOCK_ABORTED);
305         }
306
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;
314
315         /*
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
320          *  res->lr_lvb_sem.
321          */
322         arg.size = reply_lvb->lvb_size;
323
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)
329                         continue;
330
331                 interval_iterate_reverse(tree->lit_root, ofd_intent_cb, &arg);
332                 if (arg.error) {
333                         unlock_res(res);
334                         GOTO(out, rc = arg.error);
335                 }
336         }
337         unlock_res(res);
338
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);
342
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);
347         }
348
349         /* this will update the LVB */
350         ldlm_glimpse_locks(res, &arg.gl_list);
351
352         lock_res(res);
353         *reply_lvb = *res_lvb;
354         unlock_res(res);
355
356 out:
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);
363         }
364
365         RETURN(rc < 0 ? rc : ELDLM_LOCK_ABORTED);
366 }
367