Whamcloud - gitweb
LU-6635 lfsck: block replacing the OST-object for test
[fs/lustre-release.git] / lustre / lov / lov_dev.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) 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  * Implementation of cl_device and cl_device_type for LOV layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 /* class_name2obd() */
40 #include <obd_class.h>
41
42 #include "lov_cl_internal.h"
43
44 struct kmem_cache *lov_lock_kmem;
45 struct kmem_cache *lov_object_kmem;
46 struct kmem_cache *lov_thread_kmem;
47 struct kmem_cache *lov_session_kmem;
48
49 struct kmem_cache *lovsub_lock_kmem;
50 struct kmem_cache *lovsub_object_kmem;
51
52 struct lu_kmem_descr lov_caches[] = {
53         {
54                 .ckd_cache = &lov_lock_kmem,
55                 .ckd_name  = "lov_lock_kmem",
56                 .ckd_size  = sizeof (struct lov_lock)
57         },
58         {
59                 .ckd_cache = &lov_object_kmem,
60                 .ckd_name  = "lov_object_kmem",
61                 .ckd_size  = sizeof (struct lov_object)
62         },
63         {
64                 .ckd_cache = &lov_thread_kmem,
65                 .ckd_name  = "lov_thread_kmem",
66                 .ckd_size  = sizeof (struct lov_thread_info)
67         },
68         {
69                 .ckd_cache = &lov_session_kmem,
70                 .ckd_name  = "lov_session_kmem",
71                 .ckd_size  = sizeof (struct lov_session)
72         },
73         {
74                 .ckd_cache = &lovsub_lock_kmem,
75                 .ckd_name  = "lovsub_lock_kmem",
76                 .ckd_size  = sizeof (struct lovsub_lock)
77         },
78         {
79                 .ckd_cache = &lovsub_object_kmem,
80                 .ckd_name  = "lovsub_object_kmem",
81                 .ckd_size  = sizeof (struct lovsub_object)
82         },
83         {
84                 .ckd_cache = NULL
85         }
86 };
87
88 /*****************************************************************************
89  *
90  * Lov device and device type functions.
91  *
92  */
93
94 static void *lov_key_init(const struct lu_context *ctx,
95                           struct lu_context_key *key)
96 {
97         struct lov_thread_info *info;
98
99         OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, GFP_NOFS);
100         if (info == NULL)
101                 info = ERR_PTR(-ENOMEM);
102         return info;
103 }
104
105 static void lov_key_fini(const struct lu_context *ctx,
106                          struct lu_context_key *key, void *data)
107 {
108         struct lov_thread_info *info = data;
109         OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
110 }
111
112 struct lu_context_key lov_key = {
113         .lct_tags = LCT_CL_THREAD,
114         .lct_init = lov_key_init,
115         .lct_fini = lov_key_fini
116 };
117
118 static void *lov_session_key_init(const struct lu_context *ctx,
119                                   struct lu_context_key *key)
120 {
121         struct lov_session *info;
122
123         OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, GFP_NOFS);
124         if (info == NULL)
125                 info = ERR_PTR(-ENOMEM);
126         return info;
127 }
128
129 static void lov_session_key_fini(const struct lu_context *ctx,
130                                  struct lu_context_key *key, void *data)
131 {
132         struct lov_session *info = data;
133         OBD_SLAB_FREE_PTR(info, lov_session_kmem);
134 }
135
136 struct lu_context_key lov_session_key = {
137         .lct_tags = LCT_SESSION,
138         .lct_init = lov_session_key_init,
139         .lct_fini = lov_session_key_fini
140 };
141
142 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
143 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
144
145 static struct lu_device *lov_device_fini(const struct lu_env *env,
146                                          struct lu_device *d)
147 {
148         int i;
149         struct lov_device *ld = lu2lov_dev(d);
150
151         LASSERT(ld->ld_lov != NULL);
152         if (ld->ld_target == NULL)
153                 RETURN(NULL);
154
155         lov_foreach_target(ld, i) {
156                 struct lovsub_device *lsd;
157
158                 lsd = ld->ld_target[i];
159                 if (lsd != NULL) {
160                         cl_stack_fini(env, lovsub2cl_dev(lsd));
161                         ld->ld_target[i] = NULL;
162                 }
163         }
164         RETURN(NULL);
165 }
166
167 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
168                            const char *name, struct lu_device *next)
169 {
170         struct lov_device *ld = lu2lov_dev(d);
171         int i;
172         int rc = 0;
173
174         LASSERT(d->ld_site != NULL);
175         if (ld->ld_target == NULL)
176                 RETURN(rc);
177
178         lov_foreach_target(ld, i) {
179                 struct lovsub_device *lsd;
180                 struct cl_device     *cl;
181                 struct lov_tgt_desc  *desc;
182
183                 desc = ld->ld_lov->lov_tgts[i];
184                 if (desc == NULL)
185                         continue;
186
187                 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
188                                    desc->ltd_obd->obd_lu_dev);
189                 if (IS_ERR(cl)) {
190                         rc = PTR_ERR(cl);
191                         break;
192                 }
193                 lsd = cl2lovsub_dev(cl);
194                 ld->ld_target[i] = lsd;
195         }
196
197         if (rc)
198                 lov_device_fini(env, d);
199         else
200                 ld->ld_flags |= LOV_DEV_INITIALIZED;
201
202         RETURN(rc);
203 }
204
205 /* Free the lov specific data created for the back end lu_device. */
206 static struct lu_device *lov_device_free(const struct lu_env *env,
207                                          struct lu_device *d)
208 {
209         struct lov_device *ld = lu2lov_dev(d);
210         const int nr = ld->ld_target_nr;
211
212         cl_device_fini(lu2cl_dev(d));
213         if (ld->ld_target != NULL)
214                 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
215
216         OBD_FREE_PTR(ld);
217         return NULL;
218 }
219
220 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
221                               __u32 index)
222 {
223         struct lov_device *ld = lu2lov_dev(dev);
224         ENTRY;
225
226         if (ld->ld_target[index] != NULL) {
227                 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
228                 ld->ld_target[index] = NULL;
229         }
230         EXIT;
231 }
232
233 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
234 {
235         int result;
236         __u32 tgt_size;
237         __u32 sub_size;
238
239         ENTRY;
240         result = 0;
241         tgt_size = dev->ld_lov->lov_tgt_size;
242         sub_size = dev->ld_target_nr;
243         if (sub_size < tgt_size) {
244                 struct lovsub_device **newd;
245                 const size_t sz = sizeof(newd[0]);
246
247                 OBD_ALLOC(newd, tgt_size * sz);
248                 if (newd != NULL) {
249                         if (sub_size > 0) {
250                                 memcpy(newd, dev->ld_target, sub_size * sz);
251                                 OBD_FREE(dev->ld_target, sub_size * sz);
252                         }
253
254                         dev->ld_target = newd;
255                         dev->ld_target_nr = tgt_size;
256                 } else {
257                         result = -ENOMEM;
258                 }
259         }
260
261         RETURN(result);
262 }
263
264 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
265                              __u32 index)
266 {
267         struct obd_device    *obd = dev->ld_obd;
268         struct lov_device    *ld  = lu2lov_dev(dev);
269         struct lov_tgt_desc  *tgt;
270         struct lovsub_device *lsd;
271         struct cl_device     *cl;
272         int rc;
273         ENTRY;
274
275         obd_getref(obd);
276
277         tgt = obd->u.lov.lov_tgts[index];
278         LASSERT(tgt != NULL);
279         LASSERT(tgt->ltd_obd != NULL);
280
281         if (!tgt->ltd_obd->obd_set_up) {
282                 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
283                 RETURN(-EINVAL);
284         }
285
286         rc = lov_expand_targets(env, ld);
287         if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
288                 LASSERT(dev->ld_site != NULL);
289
290                 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
291                                    tgt->ltd_obd->obd_lu_dev);
292                 if (!IS_ERR(cl)) {
293                         lsd = cl2lovsub_dev(cl);
294                         ld->ld_target[index] = lsd;
295                 } else {
296                         CERROR("add failed (%d), deleting %s\n", rc,
297                                obd_uuid2str(&tgt->ltd_uuid));
298                         lov_cl_del_target(env, dev, index);
299                         rc = PTR_ERR(cl);
300                 }
301         }
302         obd_putref(obd);
303         RETURN(rc);
304 }
305
306 static int lov_process_config(const struct lu_env *env,
307                               struct lu_device *d, struct lustre_cfg *cfg)
308 {
309         struct obd_device *obd = d->ld_obd;
310         int cmd;
311         int rc;
312         int gen;
313         __u32 index;
314
315         obd_getref(obd);
316
317         cmd = cfg->lcfg_command;
318         rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
319         if (rc == 0) {
320                 switch(cmd) {
321                 case LCFG_LOV_ADD_OBD:
322                 case LCFG_LOV_ADD_INA:
323                         rc = lov_cl_add_target(env, d, index);
324                         if (rc != 0)
325                                 lov_del_target(d->ld_obd, index, NULL, 0);
326                         break;
327                 case LCFG_LOV_DEL_OBD:
328                         lov_cl_del_target(env, d, index);
329                         break;
330                 }
331         }
332         obd_putref(obd);
333         RETURN(rc);
334 }
335
336 static const struct lu_device_operations lov_lu_ops = {
337         .ldo_object_alloc      = lov_object_alloc,
338         .ldo_process_config    = lov_process_config,
339 };
340
341 static struct lu_device *lov_device_alloc(const struct lu_env *env,
342                                           struct lu_device_type *t,
343                                           struct lustre_cfg *cfg)
344 {
345         struct lu_device *d;
346         struct lov_device *ld;
347         struct obd_device *obd;
348         int rc;
349
350         OBD_ALLOC_PTR(ld);
351         if (ld == NULL)
352                 RETURN(ERR_PTR(-ENOMEM));
353
354         cl_device_init(&ld->ld_cl, t);
355         d = lov2lu_dev(ld);
356         d->ld_ops = &lov_lu_ops;
357
358         /* setup the LOV OBD */
359         obd = class_name2obd(lustre_cfg_string(cfg, 0));
360         LASSERT(obd != NULL);
361         rc = lov_setup(obd, cfg);
362         if (rc) {
363                 lov_device_free(env, d);
364                 RETURN(ERR_PTR(rc));
365         }
366
367         ld->ld_lov = &obd->u.lov;
368         RETURN(d);
369 }
370
371 static const struct lu_device_type_operations lov_device_type_ops = {
372         .ldto_init = lov_type_init,
373         .ldto_fini = lov_type_fini,
374
375         .ldto_start = lov_type_start,
376         .ldto_stop  = lov_type_stop,
377
378         .ldto_device_alloc = lov_device_alloc,
379         .ldto_device_free  = lov_device_free,
380
381         .ldto_device_init    = lov_device_init,
382         .ldto_device_fini    = lov_device_fini
383 };
384
385 struct lu_device_type lov_device_type = {
386         .ldt_tags     = LU_DEVICE_CL,
387         .ldt_name     = LUSTRE_LOV_NAME,
388         .ldt_ops      = &lov_device_type_ops,
389         .ldt_ctx_tags = LCT_CL_THREAD
390 };
391
392 /** @} lov */