Whamcloud - gitweb
LU-13086 tests: restore compatibility with mpich
[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, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * Implementation of cl_device and cl_device_type for LOV layer.
32  *
33  *   Author: Nikita Danilov <nikita.danilov@sun.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_LOV
37
38 /* class_name2obd() */
39 #include <obd_class.h>
40
41 #include "lov_cl_internal.h"
42
43 struct kmem_cache *lov_lock_kmem;
44 struct kmem_cache *lov_object_kmem;
45 struct kmem_cache *lov_thread_kmem;
46 struct kmem_cache *lov_session_kmem;
47
48 struct kmem_cache *lovsub_object_kmem;
49
50 struct lu_kmem_descr lov_caches[] = {
51         {
52                 .ckd_cache = &lov_lock_kmem,
53                 .ckd_name  = "lov_lock_kmem",
54                 .ckd_size  = sizeof(struct lov_lock)
55         },
56         {
57                 .ckd_cache = &lov_object_kmem,
58                 .ckd_name  = "lov_object_kmem",
59                 .ckd_size  = sizeof(struct lov_object)
60         },
61         {
62                 .ckd_cache = &lov_thread_kmem,
63                 .ckd_name  = "lov_thread_kmem",
64                 .ckd_size  = sizeof(struct lov_thread_info)
65         },
66         {
67                 .ckd_cache = &lov_session_kmem,
68                 .ckd_name  = "lov_session_kmem",
69                 .ckd_size  = sizeof(struct lov_session)
70         },
71         {
72                 .ckd_cache = &lovsub_object_kmem,
73                 .ckd_name  = "lovsub_object_kmem",
74                 .ckd_size  = sizeof(struct lovsub_object)
75         },
76         {
77                 .ckd_cache = NULL
78         }
79 };
80
81 /*****************************************************************************
82  *
83  * Lov device and device type functions.
84  *
85  */
86
87 static void *lov_key_init(const struct lu_context *ctx,
88                           struct lu_context_key *key)
89 {
90         struct lov_thread_info *info;
91
92         OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, GFP_NOFS);
93         if (!info)
94                 info = ERR_PTR(-ENOMEM);
95         return info;
96 }
97
98 static void lov_key_fini(const struct lu_context *ctx,
99                          struct lu_context_key *key, void *data)
100 {
101         struct lov_thread_info *info = data;
102         OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
103 }
104
105 struct lu_context_key lov_key = {
106         .lct_tags = LCT_CL_THREAD,
107         .lct_init = lov_key_init,
108         .lct_fini = lov_key_fini
109 };
110
111 static void *lov_session_key_init(const struct lu_context *ctx,
112                                   struct lu_context_key *key)
113 {
114         struct lov_session *info;
115
116         OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, GFP_NOFS);
117         if (!info)
118                 info = ERR_PTR(-ENOMEM);
119         return info;
120 }
121
122 static void lov_session_key_fini(const struct lu_context *ctx,
123                                  struct lu_context_key *key, void *data)
124 {
125         struct lov_session *info = data;
126
127         OBD_SLAB_FREE_PTR(info, lov_session_kmem);
128 }
129
130 struct lu_context_key lov_session_key = {
131         .lct_tags = LCT_SESSION,
132         .lct_init = lov_session_key_init,
133         .lct_fini = lov_session_key_fini
134 };
135
136 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
137 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
138
139
140 static int lov_mdc_dev_init(const struct lu_env *env, struct lov_device *ld,
141                             struct lu_device *mdc_dev, __u32 idx, __u32 nr)
142 {
143         struct cl_device *cl;
144
145         ENTRY;
146         cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
147                            mdc_dev);
148         if (IS_ERR(cl))
149                 RETURN(PTR_ERR(cl));
150
151         ld->ld_md_tgts[nr].ldm_mdc = cl;
152         ld->ld_md_tgts[nr].ldm_idx = idx;
153         RETURN(0);
154 }
155
156 static struct lu_device *lov_device_fini(const struct lu_env *env,
157                                          struct lu_device *d)
158 {
159         struct lov_device *ld = lu2lov_dev(d);
160         int i;
161
162         LASSERT(ld->ld_lov != NULL);
163
164         if (ld->ld_lmv) {
165                 class_decref(ld->ld_lmv, "lov", d);
166                 ld->ld_lmv = NULL;
167         }
168
169         if (ld->ld_md_tgts) {
170                 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
171                         if (!ld->ld_md_tgts[i].ldm_mdc)
172                                 continue;
173
174                         cl_stack_fini(env, ld->ld_md_tgts[i].ldm_mdc);
175                         ld->ld_md_tgts[i].ldm_mdc = NULL;
176                         ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc = NULL;
177                 }
178         }
179
180         if (ld->ld_target) {
181                 lov_foreach_target(ld, i) {
182                         struct lovsub_device *lsd;
183
184                         lsd = ld->ld_target[i];
185                         if (lsd) {
186                                 cl_stack_fini(env, lovsub2cl_dev(lsd));
187                                 ld->ld_target[i] = NULL;
188                         }
189                 }
190         }
191         RETURN(NULL);
192 }
193
194 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
195                            const char *name, struct lu_device *next)
196 {
197         struct lov_device *ld = lu2lov_dev(d);
198         int i;
199         int rc = 0;
200
201         /* check all added already MDC subdevices and initialize them */
202         for (i = 0; i < ld->ld_md_tgts_nr; i++) {
203                 struct obd_device *mdc;
204                 __u32 idx;
205
206                 mdc = ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc;
207                 idx = ld->ld_lov->lov_mdc_tgts[i].lmtd_index;
208
209                 if (!mdc)
210                         continue;
211
212                 rc = lov_mdc_dev_init(env, ld, mdc->obd_lu_dev, idx, i);
213                 if (rc) {
214                         CERROR("%s: failed to add MDC %s as target: rc = %d\n",
215                                d->ld_obd->obd_name,
216                                obd_uuid2str(&mdc->obd_uuid), rc);
217                         GOTO(out_err, rc);
218                 }
219         }
220
221         if (!ld->ld_target)
222                 RETURN(0);
223
224         lov_foreach_target(ld, i) {
225                 struct lovsub_device *lsd;
226                 struct cl_device *cl;
227                 struct lov_tgt_desc *desc;
228
229                 desc = ld->ld_lov->lov_tgts[i];
230                 if (!desc)
231                         continue;
232
233                 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
234                                    desc->ltd_obd->obd_lu_dev);
235                 if (IS_ERR(cl))
236                         GOTO(out_err, rc = PTR_ERR(cl));
237
238                 lsd = cl2lovsub_dev(cl);
239                 ld->ld_target[i] = lsd;
240         }
241         ld->ld_flags |= LOV_DEV_INITIALIZED;
242         RETURN(0);
243
244 out_err:
245         lu_device_fini(d);
246         RETURN(rc);
247 }
248
249 /* Free the lov specific data created for the back end lu_device. */
250 static struct lu_device *lov_device_free(const struct lu_env *env,
251                                          struct lu_device *d)
252 {
253         struct lov_device *ld = lu2lov_dev(d);
254         const int nr = ld->ld_target_nr;
255
256         lu_site_fini(&ld->ld_site);
257
258         cl_device_fini(lu2cl_dev(d));
259         if (ld->ld_target) {
260                 OBD_FREE_PTR_ARRAY(ld->ld_target, nr);
261                 ld->ld_target = NULL;
262         }
263         if (ld->ld_md_tgts) {
264                 OBD_FREE_PTR_ARRAY(ld->ld_md_tgts, LOV_MDC_TGT_MAX);
265                 ld->ld_md_tgts = NULL;
266         }
267         /* free array of MDCs */
268         if (ld->ld_lov->lov_mdc_tgts) {
269                 OBD_FREE_PTR_ARRAY(ld->ld_lov->lov_mdc_tgts, LOV_MDC_TGT_MAX);
270                 ld->ld_lov->lov_mdc_tgts = NULL;
271         }
272
273         OBD_FREE_PTR(ld);
274         return NULL;
275 }
276
277 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
278                               __u32 index)
279 {
280         struct lov_device *ld = lu2lov_dev(dev);
281
282         ENTRY;
283
284         if (ld->ld_target[index]) {
285                 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
286                 ld->ld_target[index] = NULL;
287         }
288         EXIT;
289 }
290
291 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
292 {
293         int result;
294         __u32 tgt_size;
295         __u32 sub_size;
296
297         ENTRY;
298         result = 0;
299         tgt_size = dev->ld_lov->lov_tgt_size;
300         sub_size = dev->ld_target_nr;
301         if (sub_size < tgt_size) {
302                 struct lovsub_device **newd;
303                 const size_t sz = sizeof(newd[0]);
304
305                 OBD_ALLOC_PTR_ARRAY(newd, tgt_size);
306                 if (newd) {
307                         if (sub_size > 0) {
308                                 memcpy(newd, dev->ld_target, sub_size * sz);
309                                 OBD_FREE(dev->ld_target, sub_size * sz);
310                         }
311
312                         dev->ld_target = newd;
313                         dev->ld_target_nr = tgt_size;
314                 } else {
315                         result = -ENOMEM;
316                 }
317         }
318
319         RETURN(result);
320 }
321
322 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
323                              __u32 index)
324 {
325         struct obd_device    *obd = dev->ld_obd;
326         struct lov_device    *ld  = lu2lov_dev(dev);
327         struct lov_tgt_desc  *tgt;
328         struct lovsub_device *lsd;
329         struct cl_device     *cl;
330         int rc;
331
332         ENTRY;
333
334         lov_tgts_getref(obd);
335
336         tgt = obd->u.lov.lov_tgts[index];
337         LASSERT(tgt != NULL);
338         LASSERT(tgt->ltd_obd != NULL);
339
340         if (!tgt->ltd_obd->obd_set_up) {
341                 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
342                 RETURN(-EINVAL);
343         }
344
345         rc = lov_expand_targets(env, ld);
346         if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
347                 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
348                                    tgt->ltd_obd->obd_lu_dev);
349                 if (!IS_ERR(cl)) {
350                         lsd = cl2lovsub_dev(cl);
351                         ld->ld_target[index] = lsd;
352                 } else {
353                         CERROR("add failed (%d), deleting %s\n", rc,
354                                obd_uuid2str(&tgt->ltd_uuid));
355                         lov_cl_del_target(env, dev, index);
356                         rc = PTR_ERR(cl);
357                 }
358         }
359
360         lov_tgts_putref(obd);
361
362         RETURN(rc);
363 }
364
365 /**
366  * Add new MDC target device in LOV.
367  *
368  * This function is part of the configuration log processing. It adds new MDC
369  * device to the MDC device array indexed by their indexes.
370  *
371  * \param[in] env       execution environment
372  * \param[in] d         LU device of LOV device
373  * \param[in] mdc       MDC device to add
374  * \param[in] idx       MDC device index
375  *
376  * \retval              0 if successful
377  * \retval              negative value on error
378  */
379 static int lov_add_mdc_target(const struct lu_env *env, struct lu_device *d,
380                               struct obd_device *mdc, __u32 idx)
381 {
382         struct lov_device *ld = lu2lov_dev(d);
383         struct obd_device *lov_obd = d->ld_obd;
384         struct obd_device *lmv_obd;
385         int next;
386         int rc = 0;
387
388         ENTRY;
389
390         LASSERT(mdc != NULL);
391         if (ld->ld_md_tgts_nr == LOV_MDC_TGT_MAX) {
392                 /*
393                  * If the maximum value of LOV_MDC_TGT_MAX will become too
394                  * small then all MD target handling must be rewritten in LOD
395                  * manner, check lod_add_device() and related functionality.
396                  */
397                 CERROR("%s: cannot serve more than %d MDC devices\n",
398                        lov_obd->obd_name, LOV_MDC_TGT_MAX);
399                 RETURN(-ERANGE);
400         }
401
402         /*
403          * grab FLD from lmv, do that here, when first MDC is added
404          * to be sure LMV is set up and can be found
405          */
406         if (!ld->ld_lmv) {
407                 next = 0;
408                 while ((lmv_obd = class_devices_in_group(&lov_obd->obd_uuid,
409                                                          &next)) != NULL) {
410                         if ((strncmp(lmv_obd->obd_type->typ_name,
411                                      LUSTRE_LMV_NAME,
412                                      strlen(LUSTRE_LMV_NAME)) == 0))
413                                 break;
414                 }
415                 if (!lmv_obd) {
416                         CERROR("%s: cannot find LMV OBD by UUID (%s)\n",
417                                lov_obd->obd_name,
418                                obd_uuid2str(&lmv_obd->obd_uuid));
419                         RETURN(-ENODEV);
420                 }
421                 spin_lock(&lmv_obd->obd_dev_lock);
422                 class_incref(lmv_obd, "lov", ld);
423                 spin_unlock(&lmv_obd->obd_dev_lock);
424                 ld->ld_lmv = lmv_obd;
425         }
426
427         LASSERT(lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_mdc ==
428                 NULL);
429
430         if (ld->ld_flags & LOV_DEV_INITIALIZED) {
431                 rc = lov_mdc_dev_init(env, ld, mdc->obd_lu_dev, idx,
432                                       ld->ld_md_tgts_nr);
433                 if (rc) {
434                         CERROR("%s: failed to add MDC %s as target: rc = %d\n",
435                                lov_obd->obd_name, obd_uuid2str(&mdc->obd_uuid),
436                                rc);
437                         RETURN(rc);
438                 }
439         }
440
441         lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_mdc = mdc;
442         lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_index = idx;
443         ld->ld_md_tgts_nr++;
444
445         RETURN(rc);
446 }
447
448 static int lov_process_config(const struct lu_env *env,
449                               struct lu_device *d, struct lustre_cfg *cfg)
450 {
451         struct obd_device *obd = d->ld_obd;
452         int cmd;
453         int rc;
454         int gen;
455         u32 index;
456
457         lov_tgts_getref(obd);
458
459         cmd = cfg->lcfg_command;
460
461         rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
462         if (rc < 0)
463                 GOTO(out, rc);
464
465         switch (cmd) {
466         case LCFG_LOV_ADD_OBD:
467         case LCFG_LOV_ADD_INA:
468                 rc = lov_cl_add_target(env, d, index);
469                 if (rc != 0)
470                         lov_del_target(d->ld_obd, index, NULL, 0);
471                 break;
472         case LCFG_LOV_DEL_OBD:
473                 lov_cl_del_target(env, d, index);
474                 break;
475         case LCFG_ADD_MDC:
476         {
477                 struct obd_device *mdc;
478                 struct obd_uuid tgt_uuid;
479
480                 /*
481                  * modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
482                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID
483                  */
484                 if (LUSTRE_CFG_BUFLEN(cfg, 1) > sizeof(tgt_uuid.uuid))
485                         GOTO(out, rc = -EINVAL);
486
487                 obd_str2uuid(&tgt_uuid, lustre_cfg_buf(cfg, 1));
488
489                 rc = kstrtou32(lustre_cfg_buf(cfg, 2), 10, &index);
490                 if (rc)
491                         GOTO(out, rc);
492
493                 mdc = class_find_client_obd(&tgt_uuid, LUSTRE_MDC_NAME,
494                                             &obd->obd_uuid);
495                 if (!mdc)
496                         GOTO(out, rc = -ENODEV);
497                 rc = lov_add_mdc_target(env, d, mdc, index);
498                 break;
499         }
500         }
501 out:
502         lov_tgts_putref(obd);
503         RETURN(rc);
504 }
505
506 static const struct lu_device_operations lov_lu_ops = {
507         .ldo_object_alloc      = lov_object_alloc,
508         .ldo_process_config    = lov_process_config,
509 };
510
511 static struct lu_device *lov_device_alloc(const struct lu_env *env,
512                                           struct lu_device_type *t,
513                                           struct lustre_cfg *cfg)
514 {
515         struct lu_device *d;
516         struct lov_device *ld;
517         struct obd_device *obd;
518         int rc;
519
520         OBD_ALLOC_PTR(ld);
521         if (!ld)
522                 RETURN(ERR_PTR(-ENOMEM));
523
524         cl_device_init(&ld->ld_cl, t);
525         d = lov2lu_dev(ld);
526         d->ld_ops = &lov_lu_ops;
527
528         /* setup the LOV OBD */
529         obd = class_name2obd(lustre_cfg_string(cfg, 0));
530         LASSERT(obd != NULL);
531         rc = lov_setup(obd, cfg);
532         if (rc)
533                 GOTO(out, rc);
534
535         /* Alloc MDC devices array */
536         /* XXX: need dynamic allocation at some moment */
537         OBD_ALLOC_PTR_ARRAY(ld->ld_md_tgts, LOV_MDC_TGT_MAX);
538         if (!ld->ld_md_tgts)
539                 GOTO(out, rc = -ENOMEM);
540
541         ld->ld_md_tgts_nr = 0;
542
543         ld->ld_lov = &obd->u.lov;
544         OBD_ALLOC_PTR_ARRAY(ld->ld_lov->lov_mdc_tgts, LOV_MDC_TGT_MAX);
545         if (!ld->ld_lov->lov_mdc_tgts)
546                 GOTO(out_md_tgts, rc = -ENOMEM);
547
548         rc = lu_site_init(&ld->ld_site, d);
549         if (rc != 0)
550                 GOTO(out_mdc_tgts, rc);
551
552         rc = lu_site_init_finish(&ld->ld_site);
553         if (rc != 0)
554                 GOTO(out_site, rc);
555
556         RETURN(d);
557 out_site:
558         lu_site_fini(&ld->ld_site);
559 out_mdc_tgts:
560         OBD_FREE_PTR_ARRAY(ld->ld_lov->lov_mdc_tgts, LOV_MDC_TGT_MAX);
561         ld->ld_lov->lov_mdc_tgts = NULL;
562 out_md_tgts:
563         OBD_FREE_PTR_ARRAY(ld->ld_md_tgts, LOV_MDC_TGT_MAX);
564         ld->ld_md_tgts = NULL;
565 out:
566         OBD_FREE_PTR(ld);
567
568         return ERR_PTR(rc);
569 }
570
571 static const struct lu_device_type_operations lov_device_type_ops = {
572         .ldto_init = lov_type_init,
573         .ldto_fini = lov_type_fini,
574
575         .ldto_start = lov_type_start,
576         .ldto_stop  = lov_type_stop,
577
578         .ldto_device_alloc = lov_device_alloc,
579         .ldto_device_free  = lov_device_free,
580
581         .ldto_device_init    = lov_device_init,
582         .ldto_device_fini    = lov_device_fini
583 };
584
585 struct lu_device_type lov_device_type = {
586         .ldt_tags     = LU_DEVICE_CL,
587         .ldt_name     = LUSTRE_LOV_NAME,
588         .ldt_ops      = &lov_device_type_ops,
589         .ldt_ctx_tags = LCT_CL_THREAD
590 };
591
592 /** @} lov */