Whamcloud - gitweb
2755d7e1ed1a1587f02ef03ada1be1ce629d22d3
[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                 victim_lock = LDLM_LOCK_GET(lck);
123
124                 /* the same policy group - every lock has the
125                  * same extent, so needn't do it any more */
126                 break;
127         }
128
129         /* l_export can be null in race with eviction - In that case, we will
130          * not find any locks in this interval */
131         if (!victim_lock)
132                 GOTO(out, rc = INTERVAL_ITER_CONT);
133
134         /*
135          * This check is for lock taken in ofd_destroy_by_fid() that does
136          * not have l_glimpse_ast set. So the logic is: if there is a lock
137          * with no l_glimpse_ast set, this object is being destroyed already.
138          * Hence, if you are grabbing DLM locks on the server, always set
139          * non-NULL glimpse_ast (e.g., ldlm_request.c::ldlm_glimpse_ast()).
140          */
141         if (victim_lock->l_glimpse_ast == NULL) {
142                 LDLM_DEBUG(victim_lock, "no l_glimpse_ast");
143                 arg->no_glimpse_ast = true;
144                 GOTO(out_release, rc = INTERVAL_ITER_STOP);
145         }
146
147         /* If NO_EXPANSION is not set, this is an active lock, and we don't need
148          * to glimpse any further once we've glimpsed the client holding this
149          * lock.  So set us up to stop.  See comment above this function. */
150         if (!(victim_lock->l_flags & LDLM_FL_NO_EXPANSION))
151                 rc = INTERVAL_ITER_STOP;
152         else
153                 rc = INTERVAL_ITER_CONT;
154
155         /* Check to see if we're already set up to send a glimpse to this
156          * client; if so, don't add this lock to the glimpse list - We need
157          * only glimpse each client once. (And if we know that client holds
158          * an active lock, we can stop glimpsing.  So keep the rc set in the
159          * check above.) */
160         list_for_each_entry(gl_work, &arg->gl_list, gl_list) {
161                 if (gl_work->gl_lock->l_export == victim_lock->l_export)
162                         GOTO(out_release, rc);
163         }
164
165         if (!OBD_FAIL_CHECK(OBD_FAIL_OST_GL_WORK_ALLOC))
166                 OBD_SLAB_ALLOC_PTR_GFP(gl_work, ldlm_glimpse_work_kmem,
167                                        GFP_ATOMIC);
168
169         if (!gl_work) {
170                 arg->error = -ENOMEM;
171                 GOTO(out_release, rc = INTERVAL_ITER_STOP);
172         }
173
174         /* Populate the gl_work structure. */
175         gl_work->gl_lock = victim_lock;
176         list_add_tail(&gl_work->gl_list, &arg->gl_list);
177         /* There is actually no need for a glimpse descriptor when glimpsing
178          * extent locks */
179         gl_work->gl_desc = NULL;
180         /* This tells ldlm_work_gl_ast_lock this was allocated from a slab and
181          * must be freed in a slab-aware manner. */
182         gl_work->gl_flags = LDLM_GL_WORK_SLAB_ALLOCATED;
183
184         GOTO(out, rc);
185
186 out_release:
187         /* If the victim doesn't go on the glimpse list, we must release it */
188         LDLM_LOCK_RELEASE(victim_lock);
189
190 out:
191         return rc;
192 }
193 /**
194  * OFD lock intent policy
195  *
196  * This defines ldlm_namespace::ns_policy interface for OFD.
197  * Intent policy is called when lock has an intent, for OFD that
198  * means glimpse lock and policy fills Lock Value Block (LVB).
199  *
200  * If already granted lock is found it will be placed in \a lockp and
201  * returned back to caller function.
202  *
203  * \param[in] ns         namespace
204  * \param[in,out] lockp  pointer to the lock
205  * \param[in] req_cookie incoming request
206  * \param[in] mode       LDLM mode
207  * \param[in] flags      LDLM flags
208  * \param[in] data       opaque data, not used in OFD policy
209  *
210  * \retval              ELDLM_LOCK_REPLACED if already granted lock was found
211  *                      and placed in \a lockp
212  * \retval              ELDLM_LOCK_ABORTED in other cases except error
213  * \retval              negative errno on error
214  */
215 int ofd_intent_policy(struct ldlm_namespace *ns, struct ldlm_lock **lockp,
216                       void *req_cookie, enum ldlm_mode mode, __u64 flags,
217                       void *data)
218 {
219         struct ptlrpc_request *req = req_cookie;
220         struct ldlm_lock *lock = *lockp;
221         struct ldlm_resource *res = lock->l_resource;
222         ldlm_processing_policy policy;
223         struct ost_lvb *res_lvb, *reply_lvb;
224         struct ldlm_reply *rep;
225         enum ldlm_error err;
226         int idx, rc;
227         struct ldlm_interval_tree *tree;
228         struct ofd_intent_args arg;
229         __u32 repsize[3] = {
230                 [MSG_PTLRPC_BODY_OFF] = sizeof(struct ptlrpc_body),
231                 [DLM_LOCKREPLY_OFF]   = sizeof(*rep),
232                 [DLM_REPLY_REC_OFF]   = sizeof(*reply_lvb)
233         };
234         struct ldlm_glimpse_work *pos, *tmp;
235         ENTRY;
236
237         /* update stats for intent in intent policy */
238         if (ptlrpc_req2svc(req)->srv_stats != NULL)
239                 lprocfs_counter_incr(ptlrpc_req2svc(req)->srv_stats,
240                                      PTLRPC_LAST_CNTR + LDLM_GLIMPSE_ENQUEUE);
241
242         INIT_LIST_HEAD(&arg.gl_list);
243         arg.no_glimpse_ast = false;
244         arg.error = 0;
245         lock->l_lvb_type = LVB_T_OST;
246         policy = ldlm_get_processing_policy(res);
247         LASSERT(policy != NULL);
248         LASSERT(req != NULL);
249
250         rc = lustre_pack_reply(req, 3, repsize, NULL);
251         if (rc)
252                 RETURN(req->rq_status = rc);
253
254         rep = lustre_msg_buf(req->rq_repmsg, DLM_LOCKREPLY_OFF, sizeof(*rep));
255         LASSERT(rep != NULL);
256
257         reply_lvb = lustre_msg_buf(req->rq_repmsg, DLM_REPLY_REC_OFF,
258                                    sizeof(*reply_lvb));
259         LASSERT(reply_lvb != NULL);
260
261         /* Call the extent policy function to see if our request can be
262          * granted, or is blocked.
263          * If the OST lock has LDLM_FL_HAS_INTENT set, it means a glimpse
264          * lock, and should not be granted if the lock will be blocked.
265          */
266
267         if (flags & LDLM_FL_BLOCK_NOWAIT) {
268                 OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_AGL_DELAY, 5);
269
270                 if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_AGL_NOLOCK))
271                         RETURN(ELDLM_LOCK_ABORTED);
272         }
273
274         LASSERT(ns == ldlm_res_to_ns(res));
275         lock_res(res);
276
277         /* Check if this is a resend case (MSG_RESENT is set on RPC) and a
278          * lock was found by ldlm_handle_enqueue(); if so no need to grant
279          * it again. */
280         if (flags & LDLM_FL_RESENT) {
281                 rc = LDLM_ITER_CONTINUE;
282         } else {
283                 __u64 tmpflags = 0;
284                 rc = policy(lock, &tmpflags, LDLM_PROCESS_RESCAN, &err, NULL);
285                 check_res_locked(res);
286         }
287
288         /* The lock met with no resistance; we're finished. */
289         if (rc == LDLM_ITER_CONTINUE) {
290                 if (OBD_FAIL_TIMEOUT(OBD_FAIL_LDLM_GLIMPSE, 2)) {
291                         ldlm_resource_unlink_lock(lock);
292                         err = ELDLM_LOCK_ABORTED;
293                 } else {
294                         err = ELDLM_LOCK_REPLACED;
295                 }
296                 unlock_res(res);
297                 RETURN(err);
298         } else if (flags & LDLM_FL_BLOCK_NOWAIT) {
299                 /* LDLM_FL_BLOCK_NOWAIT means it is for AGL. Do not send glimpse
300                  * callback for glimpse size. The real size user will trigger
301                  * the glimpse callback when necessary. */
302                 unlock_res(res);
303                 RETURN(ELDLM_LOCK_ABORTED);
304         }
305
306         /* Do not grant any lock, but instead send GL callbacks.  The extent
307          * policy nicely created a list of all PW locks for us.  We will choose
308          * the highest of those which are larger than the size in the LVB, if
309          * any, and perform a glimpse callback. */
310         res_lvb = res->lr_lvb_data;
311         LASSERT(res_lvb != NULL);
312         *reply_lvb = *res_lvb;
313
314         /*
315          * ->ns_lock guarantees that no new locks are granted, and,
316          *  therefore, that res->lr_lvb_data cannot increase beyond the
317          *  end of already granted lock. As a result, it is safe to
318          *  check against "stale" reply_lvb->lvb_size value without
319          *  res->lr_lvb_sem.
320          */
321         arg.size = reply_lvb->lvb_size;
322
323         /* Check for PW locks beyond the size in the LVB, build the list
324          * of locks to glimpse (arg.gl_list) */
325         for (idx = 0; idx < LCK_MODE_NUM; idx++) {
326                 tree = &res->lr_itree[idx];
327                 if (tree->lit_mode == LCK_PR)
328                         continue;
329
330                 interval_iterate_reverse(tree->lit_root, ofd_intent_cb, &arg);
331                 if (arg.error) {
332                         unlock_res(res);
333                         GOTO(out, rc = arg.error);
334                 }
335         }
336         unlock_res(res);
337
338         /* There were no PW locks beyond the size in the LVB; finished. */
339         if (list_empty(&arg.gl_list))
340                 RETURN(ELDLM_LOCK_ABORTED);
341
342         if (arg.no_glimpse_ast) {
343                 /* We are racing with unlink(); just return -ENOENT */
344                 rep->lock_policy_res1 = ptlrpc_status_hton(-ENOENT);
345                 GOTO(out, ELDLM_LOCK_ABORTED);
346         }
347
348         /* this will update the LVB */
349         ldlm_glimpse_locks(res, &arg.gl_list);
350
351         lock_res(res);
352         *reply_lvb = *res_lvb;
353         unlock_res(res);
354
355 out:
356         /* If the list is not empty, we failed to glimpse some locks and
357          * must clean up.  Usually due to a race with unlink.*/
358         list_for_each_entry_safe(pos, tmp, &arg.gl_list, gl_list) {
359                 list_del(&pos->gl_list);
360                 LDLM_LOCK_RELEASE(pos->gl_lock);
361                 OBD_SLAB_FREE_PTR(pos, ldlm_glimpse_work_kmem);
362         }
363
364         RETURN(rc < 0 ? rc : ELDLM_LOCK_ABORTED);
365 }
366