GNU Linux-libre 4.14.324-gnu1
[releases.git] / drivers / staging / lustre / 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, 2012, 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 information(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_stripe);
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
93         subenv = lov_sublock_env_get(env, parent, lls);
94         if (!IS_ERR(subenv)) {
95                 result = cl_lock_init(subenv->lse_env, &lls->sub_lock,
96                                       subenv->lse_io);
97         } else {
98                 /* error occurs. */
99                 result = PTR_ERR(subenv);
100         }
101         return result;
102 }
103
104 /**
105  * Creates sub-locks for a given lov_lock for the first time.
106  *
107  * Goes through all sub-objects of top-object, and creates sub-locks on every
108  * sub-object intersecting with top-lock extent. This is complicated by the
109  * fact that top-lock (that is being created) can be accessed concurrently
110  * through already created sub-locks (possibly shared with other top-locks).
111  */
112 static struct lov_lock *lov_lock_sub_init(const struct lu_env *env,
113                                           const struct cl_object *obj,
114                                           struct cl_lock *lock)
115 {
116         int result = 0;
117         int i;
118         int nr;
119         u64 start;
120         u64 end;
121         u64 file_start;
122         u64 file_end;
123
124         struct lov_object       *loo    = cl2lov(obj);
125         struct lov_layout_raid0 *r0     = lov_r0(loo);
126         struct lov_lock         *lovlck;
127
128         CDEBUG(D_INODE, "%p: lock/io FID " DFID "/" DFID ", lock/io clobj %p/%p\n",
129                loo, PFID(lu_object_fid(lov2lu(loo))),
130                PFID(lu_object_fid(&obj->co_lu)),
131                lov2cl(loo), obj);
132
133         file_start = cl_offset(lov2cl(loo), lock->cll_descr.cld_start);
134         file_end   = cl_offset(lov2cl(loo), lock->cll_descr.cld_end + 1) - 1;
135
136         for (i = 0, nr = 0; i < r0->lo_nr; i++) {
137                 /*
138                  * XXX for wide striping smarter algorithm is desirable,
139                  * breaking out of the loop, early.
140                  */
141                 if (likely(r0->lo_sub[i]) && /* spare layout */
142                     lov_stripe_intersects(loo->lo_lsm, i,
143                                           file_start, file_end, &start, &end))
144                         nr++;
145         }
146         LASSERT(nr > 0);
147         lovlck = libcfs_kvzalloc(offsetof(struct lov_lock, lls_sub[nr]),
148                                  GFP_NOFS);
149         if (!lovlck)
150                 return ERR_PTR(-ENOMEM);
151
152         lovlck->lls_nr = nr;
153         for (i = 0, nr = 0; i < r0->lo_nr; ++i) {
154                 if (likely(r0->lo_sub[i]) &&
155                     lov_stripe_intersects(loo->lo_lsm, i,
156                                           file_start, file_end, &start, &end)) {
157                         struct lov_lock_sub *lls = &lovlck->lls_sub[nr];
158                         struct cl_lock_descr *descr;
159
160                         descr = &lls->sub_lock.cll_descr;
161
162                         LASSERT(!descr->cld_obj);
163                         descr->cld_obj   = lovsub2cl(r0->lo_sub[i]);
164                         descr->cld_start = cl_index(descr->cld_obj, start);
165                         descr->cld_end   = cl_index(descr->cld_obj, end);
166                         descr->cld_mode  = lock->cll_descr.cld_mode;
167                         descr->cld_gid   = lock->cll_descr.cld_gid;
168                         descr->cld_enq_flags = lock->cll_descr.cld_enq_flags;
169                         lls->sub_stripe = i;
170
171                         /* initialize sub lock */
172                         result = lov_sublock_init(env, lock, lls);
173                         if (result < 0)
174                                 break;
175
176                         lls->sub_initialized = 1;
177                         nr++;
178                 }
179         }
180         LASSERT(ergo(result == 0, nr == lovlck->lls_nr));
181
182         if (result != 0) {
183                 for (i = 0; i < nr; ++i) {
184                         if (!lovlck->lls_sub[i].sub_initialized)
185                                 break;
186
187                         cl_lock_fini(env, &lovlck->lls_sub[i].sub_lock);
188                 }
189                 kvfree(lovlck);
190                 lovlck = ERR_PTR(result);
191         }
192
193         return lovlck;
194 }
195
196 static void lov_lock_fini(const struct lu_env *env,
197                           struct cl_lock_slice *slice)
198 {
199         struct lov_lock *lovlck;
200         int i;
201
202         lovlck = cl2lov_lock(slice);
203         for (i = 0; i < lovlck->lls_nr; ++i) {
204                 LASSERT(!lovlck->lls_sub[i].sub_is_enqueued);
205                 if (lovlck->lls_sub[i].sub_initialized)
206                         cl_lock_fini(env, &lovlck->lls_sub[i].sub_lock);
207         }
208         kvfree(lovlck);
209 }
210
211 /**
212  * Implementation of cl_lock_operations::clo_enqueue() for lov layer. This
213  * function is rather subtle, as it enqueues top-lock (i.e., advances top-lock
214  * state machine from CLS_QUEUING to CLS_ENQUEUED states) by juggling sub-lock
215  * state machines in the face of sub-locks sharing (by multiple top-locks),
216  * and concurrent sub-lock cancellations.
217  */
218 static int lov_lock_enqueue(const struct lu_env *env,
219                             const struct cl_lock_slice *slice,
220                             struct cl_io *io, struct cl_sync_io *anchor)
221 {
222         struct cl_lock *lock = slice->cls_lock;
223         struct lov_lock *lovlck = cl2lov_lock(slice);
224         int i;
225         int rc = 0;
226
227         for (i = 0; i < lovlck->lls_nr; ++i) {
228                 struct lov_lock_sub  *lls = &lovlck->lls_sub[i];
229                 struct lov_sublock_env *subenv;
230
231                 subenv = lov_sublock_env_get(env, lock, lls);
232                 if (IS_ERR(subenv)) {
233                         rc = PTR_ERR(subenv);
234                         break;
235                 }
236                 rc = cl_lock_enqueue(subenv->lse_env, subenv->lse_io,
237                                      &lls->sub_lock, anchor);
238                 if (rc != 0)
239                         break;
240
241                 lls->sub_is_enqueued = 1;
242         }
243         return rc;
244 }
245
246 static void lov_lock_cancel(const struct lu_env *env,
247                             const struct cl_lock_slice *slice)
248 {
249         struct cl_lock *lock = slice->cls_lock;
250         struct lov_lock *lovlck = cl2lov_lock(slice);
251         int i;
252
253         for (i = 0; i < lovlck->lls_nr; ++i) {
254                 struct lov_lock_sub *lls = &lovlck->lls_sub[i];
255                 struct cl_lock *sublock = &lls->sub_lock;
256                 struct lov_sublock_env *subenv;
257
258                 if (!lls->sub_is_enqueued)
259                         continue;
260
261                 lls->sub_is_enqueued = 0;
262                 subenv = lov_sublock_env_get(env, lock, lls);
263                 if (!IS_ERR(subenv)) {
264                         cl_lock_cancel(subenv->lse_env, sublock);
265                 } else {
266                         CL_LOCK_DEBUG(D_ERROR, env, slice->cls_lock,
267                                       "%s fails with %ld.\n",
268                                       __func__, PTR_ERR(subenv));
269                 }
270         }
271 }
272
273 static int lov_lock_print(const struct lu_env *env, void *cookie,
274                           lu_printer_t p, const struct cl_lock_slice *slice)
275 {
276         struct lov_lock *lck = cl2lov_lock(slice);
277         int           i;
278
279         (*p)(env, cookie, "%d\n", lck->lls_nr);
280         for (i = 0; i < lck->lls_nr; ++i) {
281                 struct lov_lock_sub *sub;
282
283                 sub = &lck->lls_sub[i];
284                 (*p)(env, cookie, "    %d %x: ", i, sub->sub_is_enqueued);
285                 cl_lock_print(env, cookie, p, &sub->sub_lock);
286         }
287         return 0;
288 }
289
290 static const struct cl_lock_operations lov_lock_ops = {
291         .clo_fini      = lov_lock_fini,
292         .clo_enqueue   = lov_lock_enqueue,
293         .clo_cancel    = lov_lock_cancel,
294         .clo_print     = lov_lock_print
295 };
296
297 int lov_lock_init_raid0(const struct lu_env *env, struct cl_object *obj,
298                         struct cl_lock *lock, const struct cl_io *io)
299 {
300         struct lov_lock *lck;
301         int result = 0;
302
303         lck = lov_lock_sub_init(env, obj, lock);
304         if (!IS_ERR(lck))
305                 cl_lock_slice_add(lock, &lck->lls_cl, obj, &lov_lock_ops);
306         else
307                 result = PTR_ERR(lck);
308         return result;
309 }
310
311 static void lov_empty_lock_fini(const struct lu_env *env,
312                                 struct cl_lock_slice *slice)
313 {
314         struct lov_lock *lck = cl2lov_lock(slice);
315
316         kmem_cache_free(lov_lock_kmem, lck);
317 }
318
319 static int lov_empty_lock_print(const struct lu_env *env, void *cookie,
320                                 lu_printer_t p,
321                                 const struct cl_lock_slice *slice)
322 {
323         (*p)(env, cookie, "empty\n");
324         return 0;
325 }
326
327 /* XXX: more methods will be added later. */
328 static const struct cl_lock_operations lov_empty_lock_ops = {
329         .clo_fini  = lov_empty_lock_fini,
330         .clo_print = lov_empty_lock_print
331 };
332
333 int lov_lock_init_empty(const struct lu_env *env, struct cl_object *obj,
334                         struct cl_lock *lock, const struct cl_io *io)
335 {
336         struct lov_lock *lck;
337         int result = -ENOMEM;
338
339         lck = kmem_cache_zalloc(lov_lock_kmem, GFP_NOFS);
340         if (lck) {
341                 cl_lock_slice_add(lock, &lck->lls_cl, obj, &lov_empty_lock_ops);
342                 result = 0;
343         }
344         return result;
345 }
346
347 /** @} lov */