Whamcloud - gitweb
LU-3259 clio: cl_lock simplification
[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, 2013, 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 struct kmem_cache *lov_req_kmem;
53
54 struct kmem_cache *lovsub_lock_kmem;
55 struct kmem_cache *lovsub_object_kmem;
56 struct kmem_cache *lovsub_req_kmem;
57
58 struct kmem_cache *lov_lock_link_kmem;
59
60 /** Lock class of lov_device::ld_mutex. */
61 struct lock_class_key cl_lov_device_mutex_class;
62
63 struct lu_kmem_descr lov_caches[] = {
64         {
65                 .ckd_cache = &lov_lock_kmem,
66                 .ckd_name  = "lov_lock_kmem",
67                 .ckd_size  = sizeof (struct lov_lock)
68         },
69         {
70                 .ckd_cache = &lov_object_kmem,
71                 .ckd_name  = "lov_object_kmem",
72                 .ckd_size  = sizeof (struct lov_object)
73         },
74         {
75                 .ckd_cache = &lov_thread_kmem,
76                 .ckd_name  = "lov_thread_kmem",
77                 .ckd_size  = sizeof (struct lov_thread_info)
78         },
79         {
80                 .ckd_cache = &lov_session_kmem,
81                 .ckd_name  = "lov_session_kmem",
82                 .ckd_size  = sizeof (struct lov_session)
83         },
84         {
85                 .ckd_cache = &lov_req_kmem,
86                 .ckd_name  = "lov_req_kmem",
87                 .ckd_size  = sizeof (struct lov_req)
88         },
89         {
90                 .ckd_cache = &lovsub_lock_kmem,
91                 .ckd_name  = "lovsub_lock_kmem",
92                 .ckd_size  = sizeof (struct lovsub_lock)
93         },
94         {
95                 .ckd_cache = &lovsub_object_kmem,
96                 .ckd_name  = "lovsub_object_kmem",
97                 .ckd_size  = sizeof (struct lovsub_object)
98         },
99         {
100                 .ckd_cache = &lovsub_req_kmem,
101                 .ckd_name  = "lovsub_req_kmem",
102                 .ckd_size  = sizeof (struct lovsub_req)
103         },
104         {
105                 .ckd_cache = &lov_lock_link_kmem,
106                 .ckd_name  = "lov_lock_link_kmem",
107                 .ckd_size  = sizeof (struct lov_lock_link)
108         },
109         {
110                 .ckd_cache = NULL
111         }
112 };
113
114 /*****************************************************************************
115  *
116  * Lov transfer operations.
117  *
118  */
119
120 static void lov_req_completion(const struct lu_env *env,
121                                const struct cl_req_slice *slice, int ioret)
122 {
123         struct lov_req *lr;
124
125         ENTRY;
126         lr = cl2lov_req(slice);
127         OBD_SLAB_FREE_PTR(lr, lov_req_kmem);
128         EXIT;
129 }
130
131 static const struct cl_req_operations lov_req_ops = {
132         .cro_completion = lov_req_completion
133 };
134
135 /*****************************************************************************
136  *
137  * Lov device and device type functions.
138  *
139  */
140
141 static void *lov_key_init(const struct lu_context *ctx,
142                           struct lu_context_key *key)
143 {
144         struct lov_thread_info *info;
145
146         OBD_SLAB_ALLOC_PTR_GFP(info, lov_thread_kmem, GFP_NOFS);
147         if (info == NULL)
148                 info = ERR_PTR(-ENOMEM);
149         return info;
150 }
151
152 static void lov_key_fini(const struct lu_context *ctx,
153                          struct lu_context_key *key, void *data)
154 {
155         struct lov_thread_info *info = data;
156         OBD_SLAB_FREE_PTR(info, lov_thread_kmem);
157 }
158
159 struct lu_context_key lov_key = {
160         .lct_tags = LCT_CL_THREAD,
161         .lct_init = lov_key_init,
162         .lct_fini = lov_key_fini
163 };
164
165 static void *lov_session_key_init(const struct lu_context *ctx,
166                                   struct lu_context_key *key)
167 {
168         struct lov_session *info;
169
170         OBD_SLAB_ALLOC_PTR_GFP(info, lov_session_kmem, GFP_NOFS);
171         if (info == NULL)
172                 info = ERR_PTR(-ENOMEM);
173         return info;
174 }
175
176 static void lov_session_key_fini(const struct lu_context *ctx,
177                                  struct lu_context_key *key, void *data)
178 {
179         struct lov_session *info = data;
180         OBD_SLAB_FREE_PTR(info, lov_session_kmem);
181 }
182
183 struct lu_context_key lov_session_key = {
184         .lct_tags = LCT_SESSION,
185         .lct_init = lov_session_key_init,
186         .lct_fini = lov_session_key_fini
187 };
188
189 /* type constructor/destructor: lov_type_{init,fini,start,stop}() */
190 LU_TYPE_INIT_FINI(lov, &lov_key, &lov_session_key);
191
192 static struct lu_device *lov_device_fini(const struct lu_env *env,
193                                          struct lu_device *d)
194 {
195         int i;
196         struct lov_device *ld = lu2lov_dev(d);
197
198         LASSERT(ld->ld_lov != NULL);
199         if (ld->ld_target == NULL)
200                 RETURN(NULL);
201
202         lov_foreach_target(ld, i) {
203                 struct lovsub_device *lsd;
204
205                 lsd = ld->ld_target[i];
206                 if (lsd != NULL) {
207                         cl_stack_fini(env, lovsub2cl_dev(lsd));
208                         ld->ld_target[i] = NULL;
209                 }
210         }
211         RETURN(NULL);
212 }
213
214 static int lov_device_init(const struct lu_env *env, struct lu_device *d,
215                            const char *name, struct lu_device *next)
216 {
217         struct lov_device *ld = lu2lov_dev(d);
218         int i;
219         int rc = 0;
220
221         LASSERT(d->ld_site != NULL);
222         if (ld->ld_target == NULL)
223                 RETURN(rc);
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 == NULL)
232                         continue;
233
234                 cl = cl_type_setup(env, d->ld_site, &lovsub_device_type,
235                                    desc->ltd_obd->obd_lu_dev);
236                 if (IS_ERR(cl)) {
237                         rc = PTR_ERR(cl);
238                         break;
239                 }
240                 lsd = cl2lovsub_dev(cl);
241                 lsd->acid_idx = i;
242                 lsd->acid_super = ld;
243                 ld->ld_target[i] = lsd;
244         }
245
246         if (rc)
247                 lov_device_fini(env, d);
248         else
249                 ld->ld_flags |= LOV_DEV_INITIALIZED;
250
251         RETURN(rc);
252 }
253
254 static int lov_req_init(const struct lu_env *env, struct cl_device *dev,
255                         struct cl_req *req)
256 {
257         struct lov_req *lr;
258         int result;
259
260         ENTRY;
261         OBD_SLAB_ALLOC_PTR_GFP(lr, lov_req_kmem, GFP_NOFS);
262         if (lr != NULL) {
263                 cl_req_slice_add(req, &lr->lr_cl, dev, &lov_req_ops);
264                 result = 0;
265         } else
266                 result = -ENOMEM;
267         RETURN(result);
268 }
269
270 static const struct cl_device_operations lov_cl_ops = {
271         .cdo_req_init = lov_req_init
272 };
273
274 static void lov_emerg_free(struct lov_device_emerg **emrg, int nr)
275 {
276         int i;
277
278         for (i = 0; i < nr; ++i) {
279                 struct lov_device_emerg *em;
280
281                 em = emrg[i];
282                 if (em != NULL) {
283                         LASSERT(em->emrg_page_list.pl_nr == 0);
284                         if (em->emrg_env != NULL)
285                                 cl_env_put(em->emrg_env, &em->emrg_refcheck);
286                         OBD_FREE_PTR(em);
287                 }
288         }
289         OBD_FREE(emrg, nr * sizeof emrg[0]);
290 }
291
292 static struct lu_device *lov_device_free(const struct lu_env *env,
293                                          struct lu_device *d)
294 {
295         struct lov_device *ld = lu2lov_dev(d);
296         const int          nr = ld->ld_target_nr;
297
298         cl_device_fini(lu2cl_dev(d));
299         if (ld->ld_target != NULL)
300                 OBD_FREE(ld->ld_target, nr * sizeof ld->ld_target[0]);
301         if (ld->ld_emrg != NULL)
302                 lov_emerg_free(ld->ld_emrg, nr);
303         OBD_FREE_PTR(ld);
304         return NULL;
305 }
306
307 static void lov_cl_del_target(const struct lu_env *env, struct lu_device *dev,
308                               __u32 index)
309 {
310         struct lov_device *ld = lu2lov_dev(dev);
311         ENTRY;
312
313         if (ld->ld_target[index] != NULL) {
314                 cl_stack_fini(env, lovsub2cl_dev(ld->ld_target[index]));
315                 ld->ld_target[index] = NULL;
316         }
317         EXIT;
318 }
319
320 static struct lov_device_emerg **lov_emerg_alloc(int nr)
321 {
322         struct lov_device_emerg **emerg;
323         int i;
324         int result;
325
326         OBD_ALLOC(emerg, nr * sizeof emerg[0]);
327         if (emerg == NULL)
328                 return ERR_PTR(-ENOMEM);
329         for (result = i = 0; i < nr && result == 0; i++) {
330                 struct lov_device_emerg *em;
331
332                 OBD_ALLOC_PTR(em);
333                 if (em != NULL) {
334                         emerg[i] = em;
335                         cl_page_list_init(&em->emrg_page_list);
336                         em->emrg_env = cl_env_alloc(&em->emrg_refcheck,
337                                                     LCT_REMEMBER|LCT_NOREF);
338                         if (!IS_ERR(em->emrg_env))
339                                 em->emrg_env->le_ctx.lc_cookie = 0x2;
340                         else {
341                                 result = PTR_ERR(em->emrg_env);
342                                 em->emrg_env = NULL;
343                         }
344                 } else
345                         result = -ENOMEM;
346         }
347         if (result != 0) {
348                 lov_emerg_free(emerg, nr);
349                 emerg = ERR_PTR(result);
350         }
351         return emerg;
352 }
353
354 static int lov_expand_targets(const struct lu_env *env, struct lov_device *dev)
355 {
356         int   result;
357         __u32 tgt_size;
358         __u32 sub_size;
359
360         ENTRY;
361         result = 0;
362         tgt_size = dev->ld_lov->lov_tgt_size;
363         sub_size = dev->ld_target_nr;
364         if (sub_size < tgt_size) {
365                 struct lovsub_device    **newd;
366                 struct lov_device_emerg **emerg;
367                 const size_t              sz   = sizeof newd[0];
368
369                 emerg = lov_emerg_alloc(tgt_size);
370                 if (IS_ERR(emerg))
371                         RETURN(PTR_ERR(emerg));
372
373                 OBD_ALLOC(newd, tgt_size * sz);
374                 if (newd != NULL) {
375                         mutex_lock(&dev->ld_mutex);
376                         if (sub_size > 0) {
377                                 memcpy(newd, dev->ld_target, sub_size * sz);
378                                 OBD_FREE(dev->ld_target, sub_size * sz);
379                         }
380                         dev->ld_target    = newd;
381                         dev->ld_target_nr = tgt_size;
382
383                         if (dev->ld_emrg != NULL)
384                                 lov_emerg_free(dev->ld_emrg, sub_size);
385                         dev->ld_emrg = emerg;
386                         mutex_unlock(&dev->ld_mutex);
387                 } else {
388                         lov_emerg_free(emerg, tgt_size);
389                         result = -ENOMEM;
390                 }
391         }
392         RETURN(result);
393 }
394
395 static int lov_cl_add_target(const struct lu_env *env, struct lu_device *dev,
396                              __u32 index)
397 {
398         struct obd_device    *obd = dev->ld_obd;
399         struct lov_device    *ld  = lu2lov_dev(dev);
400         struct lov_tgt_desc  *tgt;
401         struct lovsub_device *lsd;
402         struct cl_device     *cl;
403         int rc;
404         ENTRY;
405
406         obd_getref(obd);
407
408         tgt = obd->u.lov.lov_tgts[index];
409         LASSERT(tgt != NULL);
410         LASSERT(tgt->ltd_obd != NULL);
411
412         if (!tgt->ltd_obd->obd_set_up) {
413                 CERROR("Target %s not set up\n", obd_uuid2str(&tgt->ltd_uuid));
414                 RETURN(-EINVAL);
415         }
416
417         rc = lov_expand_targets(env, ld);
418         if (rc == 0 && ld->ld_flags & LOV_DEV_INITIALIZED) {
419                 LASSERT(dev->ld_site != NULL);
420
421                 cl = cl_type_setup(env, dev->ld_site, &lovsub_device_type,
422                                    tgt->ltd_obd->obd_lu_dev);
423                 if (!IS_ERR(cl)) {
424                         lsd = cl2lovsub_dev(cl);
425                         lsd->acid_idx = index;
426                         lsd->acid_super = ld;
427                         ld->ld_target[index] = lsd;
428                 } else {
429                         CERROR("add failed (%d), deleting %s\n", rc,
430                                obd_uuid2str(&tgt->ltd_uuid));
431                         lov_cl_del_target(env, dev, index);
432                         rc = PTR_ERR(cl);
433                 }
434         }
435         obd_putref(obd);
436         RETURN(rc);
437 }
438
439 static int lov_process_config(const struct lu_env *env,
440                               struct lu_device *d, struct lustre_cfg *cfg)
441 {
442         struct obd_device *obd = d->ld_obd;
443         int cmd;
444         int rc;
445         int gen;
446         __u32 index;
447
448         obd_getref(obd);
449
450         cmd = cfg->lcfg_command;
451         rc = lov_process_config_base(d->ld_obd, cfg, &index, &gen);
452         if (rc == 0) {
453                 switch(cmd) {
454                 case LCFG_LOV_ADD_OBD:
455                 case LCFG_LOV_ADD_INA:
456                         rc = lov_cl_add_target(env, d, index);
457                         if (rc != 0)
458                                 lov_del_target(d->ld_obd, index, 0, 0);
459                         break;
460                 case LCFG_LOV_DEL_OBD:
461                         lov_cl_del_target(env, d, index);
462                         break;
463                 }
464         }
465         obd_putref(obd);
466         RETURN(rc);
467 }
468
469 static const struct lu_device_operations lov_lu_ops = {
470         .ldo_object_alloc      = lov_object_alloc,
471         .ldo_process_config    = lov_process_config,
472 };
473
474 static struct lu_device *lov_device_alloc(const struct lu_env *env,
475                                           struct lu_device_type *t,
476                                           struct lustre_cfg *cfg)
477 {
478         struct lu_device *d;
479         struct lov_device *ld;
480         struct obd_device *obd;
481         int rc;
482
483         OBD_ALLOC_PTR(ld);
484         if (ld == NULL)
485                 RETURN(ERR_PTR(-ENOMEM));
486
487         cl_device_init(&ld->ld_cl, t);
488         d = lov2lu_dev(ld);
489         d->ld_ops        = &lov_lu_ops;
490         ld->ld_cl.cd_ops = &lov_cl_ops;
491
492         mutex_init(&ld->ld_mutex);
493         lockdep_set_class(&ld->ld_mutex, &cl_lov_device_mutex_class);
494
495         /* setup the LOV OBD */
496         obd = class_name2obd(lustre_cfg_string(cfg, 0));
497         LASSERT(obd != NULL);
498         rc = lov_setup(obd, cfg);
499         if (rc) {
500                 lov_device_free(env, d);
501                 RETURN(ERR_PTR(rc));
502         }
503
504         ld->ld_lov = &obd->u.lov;
505         RETURN(d);
506 }
507
508 static const struct lu_device_type_operations lov_device_type_ops = {
509         .ldto_init = lov_type_init,
510         .ldto_fini = lov_type_fini,
511
512         .ldto_start = lov_type_start,
513         .ldto_stop  = lov_type_stop,
514
515         .ldto_device_alloc = lov_device_alloc,
516         .ldto_device_free  = lov_device_free,
517
518         .ldto_device_init    = lov_device_init,
519         .ldto_device_fini    = lov_device_fini
520 };
521
522 struct lu_device_type lov_device_type = {
523         .ldt_tags     = LU_DEVICE_CL,
524         .ldt_name     = LUSTRE_LOV_NAME,
525         .ldt_ops      = &lov_device_type_ops,
526         .ldt_ctx_tags = LCT_CL_THREAD
527 };
528 EXPORT_SYMBOL(lov_device_type);
529
530 /** @} lov */