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