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