Whamcloud - gitweb
LU-8998 clio: Client side implementation for PFL
[fs/lustre-release.git] / lustre / lov / lov_lock.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) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2016, 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  * Implementation of cl_lock for LOV layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #include "lov_cl_internal.h"
40
41 /** \addtogroup lov
42  *  @{
43  */
44
45 /*****************************************************************************
46  *
47  * Lov lock operations.
48  *
49  */
50
51 static struct lov_sublock_env *lov_sublock_env_get(const struct lu_env *env,
52                                                    const struct cl_lock *parent,
53                                                    struct lov_lock_sub *lls)
54 {
55         struct lov_sublock_env *subenv;
56         struct lov_io          *lio    = lov_env_io(env);
57         struct cl_io           *io     = lio->lis_cl.cis_io;
58         struct lov_io_sub      *sub;
59
60         subenv = &lov_env_session(env)->ls_subenv;
61
62         /*
63          * FIXME: We tend to use the subio's env & io to call the sublock
64          * lock operations because osc lock sometimes stores some control
65          * variables in thread's IO infomation(Now only lockless information).
66          * However, if the lock's host(object) is different from the object
67          * for current IO, we have no way to get the subenv and subio because
68          * they are not initialized at all. As a temp fix, in this case,
69          * we still borrow the parent's env to call sublock operations.
70          */
71         if (!io || !cl_object_same(io->ci_obj, parent->cll_descr.cld_obj)) {
72                 subenv->lse_env = env;
73                 subenv->lse_io = io;
74         } else {
75                 sub = lov_sub_get(env, lio, lls->sub_index);
76                 if (!IS_ERR(sub)) {
77                         subenv->lse_env = sub->sub_env;
78                         subenv->lse_io  = &sub->sub_io;
79                 } else {
80                         subenv = (void *)sub;
81                 }
82         }
83         return subenv;
84 }
85
86 static int lov_sublock_init(const struct lu_env *env,
87                             const struct cl_lock *parent,
88                             struct lov_lock_sub *lls)
89 {
90         struct lov_sublock_env *subenv;
91         int result;
92         ENTRY;
93
94         subenv = lov_sublock_env_get(env, parent, lls);
95         if (!IS_ERR(subenv)) {
96                 result = cl_lock_init(subenv->lse_env, &lls->sub_lock,
97                                       subenv->lse_io);
98         } else {
99                 /* error occurs. */
100                 result = PTR_ERR(subenv);
101         }
102         RETURN(result);
103 }
104
105 /**
106  * Creates sub-locks for a given lov_lock for the first time.
107  *
108  * Goes through all sub-objects of top-object, and creates sub-locks on every
109  * sub-object intersecting with top-lock extent. This is complicated by the
110  * fact that top-lock (that is being created) can be accessed concurrently
111  * through already created sub-locks (possibly shared with other top-locks).
112  */
113 static struct lov_lock *lov_lock_sub_init(const struct lu_env *env,
114                                           const struct cl_object *obj,
115                                           struct cl_lock *lock)
116 {
117         struct lov_object *lov = cl2lov(obj);
118         struct lov_lock *lovlck;
119         struct lu_extent ext;
120         loff_t start;
121         loff_t end;
122         int result = 0;
123         int i;
124         int index;
125         int nr;
126
127         ENTRY;
128
129         ext.e_start = cl_offset(obj, lock->cll_descr.cld_start);
130         if (lock->cll_descr.cld_end == CL_PAGE_EOF)
131                 ext.e_end = OBD_OBJECT_EOF;
132         else
133                 ext.e_end  = cl_offset(obj, lock->cll_descr.cld_end + 1);
134
135         nr = 0;
136         for (index = lov_lsm_entry(lov->lo_lsm, ext.e_start);
137              index != -1 && index < lov->lo_lsm->lsm_entry_count; index++) {
138                 struct lov_layout_raid0 *r0 = lov_r0(lov, index);
139
140                 /* assume lsm entries are sorted. */
141                 if (!lu_extent_is_overlapped(&ext,
142                                              &lov_lse(lov, index)->lsme_extent))
143                         break;
144
145                 for (i = 0; i < r0->lo_nr; i++) {
146                         if (likely(r0->lo_sub[i] != NULL) && /* spare layout */
147                             lov_stripe_intersects(lov->lo_lsm, index, i,
148                                                   &ext, &start, &end))
149                                 nr++;
150                 }
151         }
152         if (nr == 0)
153                 RETURN(ERR_PTR(-EINVAL));
154
155         OBD_ALLOC_LARGE(lovlck, offsetof(struct lov_lock, lls_sub[nr]));
156         if (lovlck == NULL)
157                 RETURN(ERR_PTR(-ENOMEM));
158
159         lovlck->lls_nr = nr;
160         nr = 0;
161         for (index = lov_lsm_entry(lov->lo_lsm, ext.e_start);
162              index < lov->lo_lsm->lsm_entry_count; index++) {
163                 struct lov_layout_raid0 *r0 = lov_r0(lov, index);
164
165                 /* assume lsm entries are sorted. */
166                 if (!lu_extent_is_overlapped(&ext,
167                                              &lov_lse(lov, index)->lsme_extent))
168                         break;
169                 for (i = 0; i < r0->lo_nr; ++i) {
170                         struct lov_lock_sub *lls = &lovlck->lls_sub[nr];
171                         struct cl_lock_descr *descr = &lls->sub_lock.cll_descr;
172
173                         if (unlikely(r0->lo_sub[i] == NULL) ||
174                             !lov_stripe_intersects(lov->lo_lsm, index, i,
175                                                    &ext, &start, &end))
176                                 continue;
177
178                         LASSERT(descr->cld_obj == NULL);
179                         descr->cld_obj   = lovsub2cl(r0->lo_sub[i]);
180                         descr->cld_start = cl_index(descr->cld_obj, start);
181                         descr->cld_end   = cl_index(descr->cld_obj, end);
182                         descr->cld_mode  = lock->cll_descr.cld_mode;
183                         descr->cld_gid   = lock->cll_descr.cld_gid;
184                         descr->cld_enq_flags = lock->cll_descr.cld_enq_flags;
185
186                         lls->sub_index = lov_comp_index(index, i);
187
188                         /* initialize sub lock */
189                         result = lov_sublock_init(env, lock, lls);
190                         if (result < 0)
191                                 break;
192
193                         lls->sub_initialized = 1;
194                         nr++;
195                 }
196         }
197         LASSERT(ergo(result == 0, nr == lovlck->lls_nr));
198
199         if (result != 0) {
200                 for (i = 0; i < nr; ++i) {
201                         if (!lovlck->lls_sub[i].sub_initialized)
202                                 break;
203
204                         cl_lock_fini(env, &lovlck->lls_sub[i].sub_lock);
205                 }
206
207                 OBD_FREE_LARGE(lovlck,
208                                 offsetof(struct lov_lock, lls_sub[nr]));
209                 lovlck = ERR_PTR(result);
210         }
211
212         RETURN(lovlck);
213 }
214
215 static void lov_lock_fini(const struct lu_env *env,
216                           struct cl_lock_slice *slice)
217 {
218         struct lov_lock *lovlck;
219         int i;
220
221         ENTRY;
222         lovlck = cl2lov_lock(slice);
223         for (i = 0; i < lovlck->lls_nr; ++i) {
224                 LASSERT(!lovlck->lls_sub[i].sub_is_enqueued);
225                 if (lovlck->lls_sub[i].sub_initialized)
226                         cl_lock_fini(env, &lovlck->lls_sub[i].sub_lock);
227         }
228         OBD_FREE_LARGE(lovlck,
229                        offsetof(struct lov_lock, lls_sub[lovlck->lls_nr]));
230         EXIT;
231 }
232
233 /**
234  * Implementation of cl_lock_operations::clo_enqueue() for lov layer. This
235  * function is rather subtle, as it enqueues top-lock (i.e., advances top-lock
236  * state machine from CLS_QUEUING to CLS_ENQUEUED states) by juggling sub-lock
237  * state machines in the face of sub-locks sharing (by multiple top-locks),
238  * and concurrent sub-lock cancellations.
239  */
240 static int lov_lock_enqueue(const struct lu_env *env,
241                             const struct cl_lock_slice *slice,
242                             struct cl_io *io, struct cl_sync_io *anchor)
243 {
244         struct cl_lock          *lock   = slice->cls_lock;
245         struct lov_lock         *lovlck = cl2lov_lock(slice);
246         int                     i;
247         int                     rc      = 0;
248
249         ENTRY;
250
251         for (i = 0; i < lovlck->lls_nr; ++i) {
252                 struct lov_lock_sub     *lls = &lovlck->lls_sub[i];
253                 struct lov_sublock_env  *subenv;
254
255                 subenv = lov_sublock_env_get(env, lock, lls);
256                 if (IS_ERR(subenv)) {
257                         rc = PTR_ERR(subenv);
258                         break;
259                 }
260
261                 rc = cl_lock_enqueue(subenv->lse_env, subenv->lse_io,
262                                      &lls->sub_lock, anchor);
263                 if (rc != 0)
264                         break;
265
266                 lls->sub_is_enqueued = 1;
267         }
268         RETURN(rc);
269 }
270
271 static void lov_lock_cancel(const struct lu_env *env,
272                             const struct cl_lock_slice *slice)
273 {
274         struct cl_lock  *lock   = slice->cls_lock;
275         struct lov_lock *lovlck = cl2lov_lock(slice);
276         int i;
277
278         ENTRY;
279
280         for (i = 0; i < lovlck->lls_nr; ++i) {
281                 struct lov_lock_sub     *lls = &lovlck->lls_sub[i];
282                 struct cl_lock          *sublock = &lls->sub_lock;
283                 struct lov_sublock_env  *subenv;
284
285                 if (!lls->sub_is_enqueued)
286                         continue;
287
288                 lls->sub_is_enqueued = 0;
289                 subenv = lov_sublock_env_get(env, lock, lls);
290                 if (!IS_ERR(subenv)) {
291                         cl_lock_cancel(subenv->lse_env, sublock);
292                 } else {
293                         CL_LOCK_DEBUG(D_ERROR, env, slice->cls_lock,
294                                       "lov_lock_cancel fails with %ld.\n",
295                                       PTR_ERR(subenv));
296                 }
297         }
298 }
299
300 static int lov_lock_print(const struct lu_env *env, void *cookie,
301                           lu_printer_t p, const struct cl_lock_slice *slice)
302 {
303         struct lov_lock *lck = cl2lov_lock(slice);
304         int              i;
305
306         (*p)(env, cookie, "%d\n", lck->lls_nr);
307         for (i = 0; i < lck->lls_nr; ++i) {
308                 struct lov_lock_sub *sub;
309
310                 sub = &lck->lls_sub[i];
311                 (*p)(env, cookie, "    %d %x: ", i, sub->sub_is_enqueued);
312                 cl_lock_print(env, cookie, p, &sub->sub_lock);
313         }
314         return 0;
315 }
316
317 static const struct cl_lock_operations lov_lock_ops = {
318         .clo_fini      = lov_lock_fini,
319         .clo_enqueue   = lov_lock_enqueue,
320         .clo_cancel    = lov_lock_cancel,
321         .clo_print     = lov_lock_print
322 };
323
324 int lov_lock_init_composite(const struct lu_env *env, struct cl_object *obj,
325                             struct cl_lock *lock, const struct cl_io *io)
326 {
327         struct lov_lock *lck;
328         int result = 0;
329
330         ENTRY;
331         lck = lov_lock_sub_init(env, obj, lock);
332         if (!IS_ERR(lck))
333                 cl_lock_slice_add(lock, &lck->lls_cl, obj, &lov_lock_ops);
334         else
335                 result = PTR_ERR(lck);
336         RETURN(result);
337 }
338
339 static void lov_empty_lock_fini(const struct lu_env *env,
340                                 struct cl_lock_slice *slice)
341 {
342         struct lov_lock *lck = cl2lov_lock(slice);
343         OBD_SLAB_FREE_PTR(lck, lov_lock_kmem);
344 }
345
346 static int lov_empty_lock_print(const struct lu_env *env, void *cookie,
347                         lu_printer_t p, const struct cl_lock_slice *slice)
348 {
349         (*p)(env, cookie, "empty\n");
350         return 0;
351 }
352
353 /* XXX: more methods will be added later. */
354 static const struct cl_lock_operations lov_empty_lock_ops = {
355         .clo_fini  = lov_empty_lock_fini,
356         .clo_print = lov_empty_lock_print
357 };
358
359 int lov_lock_init_empty(const struct lu_env *env, struct cl_object *obj,
360                         struct cl_lock *lock, const struct cl_io *io)
361 {
362         struct lov_lock *lck;
363         int result = -ENOMEM;
364
365         ENTRY;
366         OBD_SLAB_ALLOC_PTR_GFP(lck, lov_lock_kmem, GFP_NOFS);
367         if (lck != NULL) {
368                 cl_lock_slice_add(lock, &lck->lls_cl, obj, &lov_empty_lock_ops);
369                 result = 0;
370         }
371         RETURN(result);
372 }
373
374 /** @} lov */