Whamcloud - gitweb
LU-3285 lov: add MDT target to the LOV device
[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, 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_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
146 static int lov_mdc_dev_init(const struct lu_env *env, struct lov_device *ld,
147                             struct lu_device *mdc_dev, __u32 idx, __u32 nr)
148 {
149         struct cl_device *cl;
150
151         ENTRY;
152         cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
153                            mdc_dev);
154         if (IS_ERR(cl))
155                 RETURN(PTR_ERR(cl));
156
157         ld->ld_md_tgts[nr].ldm_mdc = cl;
158         ld->ld_md_tgts[nr].ldm_idx = idx;
159         RETURN(0);
160 }
161
162 static struct lu_device *lov_device_fini(const struct lu_env *env,
163                                          struct lu_device *d)
164 {
165         struct lov_device *ld = lu2lov_dev(d);
166         int i;
167
168         LASSERT(ld->ld_lov != NULL);
169
170         if (ld->ld_lmv != NULL) {
171                 class_decref(ld->ld_lmv, "lov", d);
172                 ld->ld_lmv = NULL;
173         }
174
175         if (ld->ld_md_tgts != NULL) {
176                 for (i = 0; i < ld->ld_md_tgts_nr; i++) {
177                         if (ld->ld_md_tgts[i].ldm_mdc == NULL)
178                                 continue;
179
180                         cl_stack_fini(env, ld->ld_md_tgts[i].ldm_mdc);
181                         ld->ld_md_tgts[i].ldm_mdc = NULL;
182                         ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc = NULL;
183                 }
184         }
185
186         if (ld->ld_target != NULL) {
187                 lov_foreach_target(ld, i) {
188                         struct lovsub_device *lsd;
189
190                         lsd = ld->ld_target[i];
191                         if (lsd != NULL) {
192                                 cl_stack_fini(env, lovsub2cl_dev(lsd));
193                                 ld->ld_target[i] = NULL;
194                         }
195                 }
196         }
197         RETURN(NULL);
198 }
199
200 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
201                            const char *name, struct lu_device *next)
202 {
203         struct lov_device *ld = lu2lov_dev(d);
204         int i;
205         int rc = 0;
206
207         /* check all added already MDC subdevices and initialize them */
208         for (i = 0; i < ld->ld_md_tgts_nr; i++) {
209                 struct obd_device *mdc;
210                 __u32 idx;
211
212                 mdc = ld->ld_lov->lov_mdc_tgts[i].lmtd_mdc;
213                 idx = ld->ld_lov->lov_mdc_tgts[i].lmtd_index;
214
215                 if (mdc == NULL)
216                         continue;
217
218                 rc = lov_mdc_dev_init(env, ld, mdc->obd_lu_dev, idx, i);
219                 if (rc) {
220                         CERROR("%s: failed to add MDC %s as target: rc = %d\n",
221                                d->ld_obd->obd_name,
222                                obd_uuid2str(&mdc->obd_uuid), rc);
223                         GOTO(out_err, rc);
224                 }
225         }
226
227         if (ld->ld_target == NULL)
228                 RETURN(0);
229
230         lov_foreach_target(ld, i) {
231                 struct lovsub_device *lsd;
232                 struct cl_device *cl;
233                 struct lov_tgt_desc *desc;
234
235                 desc = ld->ld_lov->lov_tgts[i];
236                 if (desc == NULL)
237                         continue;
238
239                 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
240                                    desc->ltd_obd->obd_lu_dev);
241                 if (IS_ERR(cl))
242                         GOTO(out_err, rc = PTR_ERR(cl));
243
244                 lsd = cl2lovsub_dev(cl);
245                 ld->ld_target[i] = lsd;
246         }
247         ld->ld_flags |= LOV_DEV_INITIALIZED;
248         RETURN(0);
249
250 out_err:
251         lu_device_fini(d);
252         RETURN(rc);
253 }
254
255 /* Free the lov specific data created for the back end lu_device. */
256 static struct lu_device *lov_device_free(const struct lu_env *env,
257                                          struct lu_device *d)
258 {
259         struct lov_device *ld = lu2lov_dev(d);
260         const int nr = ld->ld_target_nr;
261
262         lu_site_fini(&ld->ld_site);
263
264         cl_device_fini(lu2cl_dev(d));
265         if (ld->ld_target) {
266                 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
267                 ld->ld_target = NULL;
268         }
269         if (ld->ld_md_tgts) {
270                 OBD_FREE(ld->ld_md_tgts,
271                          sizeof(*ld->ld_md_tgts) * LOV_MDC_TGT_MAX);
272                 ld->ld_md_tgts = NULL;
273         }
274         /* free array of MDCs */
275         if (ld->ld_lov->lov_mdc_tgts) {
276                 OBD_FREE(ld->ld_lov->lov_mdc_tgts,
277                          sizeof(*ld->ld_lov->lov_mdc_tgts) * LOV_MDC_TGT_MAX);
278                 ld->ld_lov->lov_mdc_tgts = NULL;
279         }
280
281         OBD_FREE_PTR(ld);
282         return NULL;
283 }
284
285 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
286                               __u32 index)
287 {
288         struct lov_device *ld = lu2lov_dev(dev);
289         ENTRY;
290
291         if (ld->ld_target[index] != NULL) {
292                 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
293                 ld->ld_target[index] = NULL;
294         }
295         EXIT;
296 }
297
298 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
299 {
300         int result;
301         __u32 tgt_size;
302         __u32 sub_size;
303
304         ENTRY;
305         result = 0;
306         tgt_size = dev->ld_lov->lov_tgt_size;
307         sub_size = dev->ld_target_nr;
308         if (sub_size < tgt_size) {
309                 struct lovsub_device **newd;
310                 const size_t sz = sizeof(newd[0]);
311
312                 OBD_ALLOC(newd, tgt_size * sz);
313                 if (newd != NULL) {
314                         if (sub_size > 0) {
315                                 memcpy(newd, dev->ld_target, sub_size * sz);
316                                 OBD_FREE(dev->ld_target, sub_size * sz);
317                         }
318
319                         dev->ld_target = newd;
320                         dev->ld_target_nr = tgt_size;
321                 } else {
322                         result = -ENOMEM;
323                 }
324         }
325
326         RETURN(result);
327 }
328
329 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
330                              __u32 index)
331 {
332         struct obd_device    *obd = dev->ld_obd;
333         struct lov_device    *ld  = lu2lov_dev(dev);
334         struct lov_tgt_desc  *tgt;
335         struct lovsub_device *lsd;
336         struct cl_device     *cl;
337         int rc;
338         ENTRY;
339
340         obd_getref(obd);
341
342         tgt = obd->u.lov.lov_tgts[index];
343         LASSERT(tgt != NULL);
344         LASSERT(tgt->ltd_obd != NULL);
345
346         if (!tgt->ltd_obd->obd_set_up) {
347                 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
348                 RETURN(-EINVAL);
349         }
350
351         rc = lov_expand_targets(env, ld);
352         if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
353                 cl = cl_type_setup(env, &ld->ld_site, &lovsub_device_type,
354                                    tgt->ltd_obd->obd_lu_dev);
355                 if (!IS_ERR(cl)) {
356                         lsd = cl2lovsub_dev(cl);
357                         ld->ld_target[index] = lsd;
358                 } else {
359                         CERROR("add failed (%d), deleting %s\n", rc,
360                                obd_uuid2str(&tgt->ltd_uuid));
361                         lov_cl_del_target(env, dev, index);
362                         rc = PTR_ERR(cl);
363                 }
364         }
365         obd_putref(obd);
366         RETURN(rc);
367 }
368
369 /**
370  * Add new MDC target device in LOV.
371  *
372  * This function is part of the configuration log processing. It adds new MDC
373  * device to the MDC device array indexed by their indexes.
374  *
375  * \param[in] env       execution environment
376  * \param[in] d         LU device of LOV device
377  * \param[in] mdc       MDC device to add
378  * \param[in] idx       MDC device index
379  *
380  * \retval              0 if successful
381  * \retval              negative value on error
382  */
383 static int lov_add_mdc_target(const struct lu_env *env, struct lu_device *d,
384                               struct obd_device *mdc, __u32 idx)
385 {
386         struct lov_device *ld = lu2lov_dev(d);
387         struct obd_device *lov_obd = d->ld_obd;
388         struct obd_device *lmv_obd;
389         int next;
390         int rc = 0;
391
392         ENTRY;
393
394         LASSERT(mdc != NULL);
395         if (ld->ld_md_tgts_nr == LOV_MDC_TGT_MAX) {
396                 /* If the maximum value of LOV_MDC_TGT_MAX will become too
397                  * small then all MD target handling must be rewritten in LOD
398                  * manner, check lod_add_device() and related functionality.
399                  */
400                 CERROR("%s: cannot serve more than %d MDC devices\n",
401                        lov_obd->obd_name, LOV_MDC_TGT_MAX);
402                 RETURN(-ERANGE);
403         }
404
405         /* grab FLD from lmv, do that here, when first MDC is added
406          * to be sure LMV is set up and can be found */
407         if (ld->ld_lmv == NULL) {
408                 next = 0;
409                 while ((lmv_obd = class_devices_in_group(&lov_obd->obd_uuid,
410                                                          &next)) != NULL) {
411                         if ((strncmp(lmv_obd->obd_type->typ_name,
412                                      LUSTRE_LMV_NAME,
413                                      strlen(LUSTRE_LMV_NAME)) == 0))
414                                 break;
415                 }
416                 if (lmv_obd == NULL) {
417                         CERROR("%s: cannot find LMV OBD by UUID (%s)\n",
418                                lov_obd->obd_name,
419                                obd_uuid2str(&lmv_obd->obd_uuid));
420                         RETURN(-ENODEV);
421                 }
422                 spin_lock(&lmv_obd->obd_dev_lock);
423                 class_incref(lmv_obd, "lov", ld);
424                 spin_unlock(&lmv_obd->obd_dev_lock);
425                 ld->ld_lmv = lmv_obd;
426         }
427
428         LASSERT(lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_mdc ==
429                 NULL);
430
431         if (ld->ld_flags & LOV_DEV_INITIALIZED) {
432                 rc = lov_mdc_dev_init(env, ld, mdc->obd_lu_dev, idx,
433                                       ld->ld_md_tgts_nr);
434                 if (rc) {
435                         CERROR("%s: failed to add MDC %s as target: rc = %d\n",
436                                lov_obd->obd_name, obd_uuid2str(&mdc->obd_uuid),
437                                rc);
438                         RETURN(rc);
439                 }
440         }
441
442         lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_mdc = mdc;
443         lov_obd->u.lov.lov_mdc_tgts[ld->ld_md_tgts_nr].lmtd_index = idx;
444         ld->ld_md_tgts_nr++;
445
446         RETURN(rc);
447 }
448
449 static int lov_process_config(const struct lu_env *env,
450                               struct lu_device *d, struct lustre_cfg *cfg)
451 {
452         struct obd_device *obd = d->ld_obd;
453         int cmd;
454         int rc;
455         int gen;
456         __u32 index;
457
458         obd_getref(obd);
459
460         cmd = cfg->lcfg_command;
461
462         rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
463         if (rc < 0)
464                 GOTO(out, rc);
465
466         switch (cmd) {
467         case LCFG_LOV_ADD_OBD:
468         case LCFG_LOV_ADD_INA:
469                 rc = lov_cl_add_target(env, d, index);
470                 if (rc != 0)
471                         lov_del_target(d->ld_obd, index, NULL, 0);
472                 break;
473         case LCFG_LOV_DEL_OBD:
474                 lov_cl_del_target(env, d, index);
475                 break;
476         case LCFG_ADD_MDC:
477         {
478                 struct obd_device       *mdc;
479                 struct obd_uuid          tgt_uuid;
480
481                 /* modify_mdc_tgts add 0:lustre-clilmv  1:lustre-MDT0000_UUID
482                  * 2:0  3:1  4:lustre-MDT0000-mdc_UUID */
483                 if (LUSTRE_CFG_BUFLEN(cfg, 1) > sizeof(tgt_uuid.uuid))
484                         GOTO(out, rc = -EINVAL);
485
486                 obd_str2uuid(&tgt_uuid, lustre_cfg_buf(cfg, 1));
487
488                 if (sscanf(lustre_cfg_buf(cfg, 2), "%d", &index) != 1)
489                         GOTO(out, rc = -EINVAL);
490
491                 mdc = class_find_client_obd(&tgt_uuid, LUSTRE_MDC_NAME,
492                                             &obd->obd_uuid);
493                 if (mdc == NULL)
494                         GOTO(out, rc = -ENODEV);
495                 rc = lov_add_mdc_target(env, d, mdc, index);
496                 break;
497         }
498         }
499 out:
500         obd_putref(obd);
501         RETURN(rc);
502 }
503
504 static const struct lu_device_operations lov_lu_ops = {
505         .ldo_object_alloc      = lov_object_alloc,
506         .ldo_process_config    = lov_process_config,
507 };
508
509 static struct lu_device *lov_device_alloc(const struct lu_env *env,
510                                           struct lu_device_type *t,
511                                           struct lustre_cfg *cfg)
512 {
513         struct lu_device *d;
514         struct lov_device *ld;
515         struct obd_device *obd;
516         int rc;
517
518         OBD_ALLOC_PTR(ld);
519         if (ld == NULL)
520                 RETURN(ERR_PTR(-ENOMEM));
521
522         cl_device_init(&ld->ld_cl, t);
523         d = lov2lu_dev(ld);
524         d->ld_ops = &lov_lu_ops;
525
526         /* setup the LOV OBD */
527         obd = class_name2obd(lustre_cfg_string(cfg, 0));
528         LASSERT(obd != NULL);
529         rc = lov_setup(obd, cfg);
530         if (rc)
531                 GOTO(out, rc);
532
533         /* Alloc MDC devices array */
534         /* XXX: need dynamic allocation at some moment */
535         OBD_ALLOC(ld->ld_md_tgts, sizeof(*ld->ld_md_tgts) * LOV_MDC_TGT_MAX);
536         if (ld->ld_md_tgts == NULL)
537                 GOTO(out, rc = -ENOMEM);
538
539         ld->ld_md_tgts_nr = 0;
540
541         ld->ld_lov = &obd->u.lov;
542         OBD_ALLOC(ld->ld_lov->lov_mdc_tgts,
543                   sizeof(*ld->ld_lov->lov_mdc_tgts) * LOV_MDC_TGT_MAX);
544         if (ld->ld_lov->lov_mdc_tgts == NULL)
545                 GOTO(out_md_tgts, rc = -ENOMEM);
546
547         rc = lu_site_init(&ld->ld_site, d);
548         if (rc != 0)
549                 GOTO(out_mdc_tgts, rc);
550
551         rc = lu_site_init_finish(&ld->ld_site);
552         if (rc != 0)
553                 GOTO(out_site, rc);
554
555         RETURN(d);
556 out_site:
557         lu_site_fini(&ld->ld_site);
558 out_mdc_tgts:
559         OBD_FREE(ld->ld_lov->lov_mdc_tgts,
560                  sizeof(*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(ld->ld_md_tgts, sizeof(*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 */