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