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