Whamcloud - gitweb
LU-1303 lod: introduce lod device
[fs/lustre-release.git] / lustre / lod / lod_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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2012, Intel, Inc.
27  *
28  */
29 /*
30  * This file is part of Lustre, http://www.lustre.org/
31  * Lustre is a trademark of Sun Microsystems, Inc.
32  *
33  * lustre/lod/lod_dev.c
34  *
35  * Lustre Logical Object Device
36  *
37  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
38  * Author: Mikhail Pershin <mike.pershin@intel.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_MDS
45
46 #include <obd_class.h>
47 #include <lustre_param.h>
48
49 #include "lod_internal.h"
50
51 /* Slab for OSD object allocation */
52 cfs_mem_cache_t *lod_object_kmem;
53
54 static struct lu_kmem_descr lod_caches[] = {
55         {
56                 .ckd_cache = &lod_object_kmem,
57                 .ckd_name  = "lod_obj",
58                 .ckd_size  = sizeof(struct lod_object)
59         },
60         {
61                 .ckd_cache = NULL
62         }
63 };
64
65 static int lod_process_config(const struct lu_env *env,
66                               struct lu_device *dev,
67                               struct lustre_cfg *lcfg)
68 {
69         struct lod_device *lod = lu2lod_dev(dev);
70         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
71         char              *arg1;
72         int                rc, i;
73         ENTRY;
74
75         switch(lcfg->lcfg_command) {
76
77         case LCFG_LOV_DEL_OBD:
78         case LCFG_LOV_ADD_INA:
79         case LCFG_LOV_ADD_OBD: {
80                 __u32 index;
81                 int gen;
82                 /* lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1 */
83                 arg1 = lustre_cfg_string(lcfg, 1);
84
85                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
86                         GOTO(out, rc = -EINVAL);
87                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
88                         GOTO(out, rc = -EINVAL);
89
90                 rc = -EINVAL;
91                 break;
92         }
93
94         case LCFG_PARAM: {
95                 struct lprocfs_static_vars  v = { 0 };
96                 struct obd_device         *obd = lod2obd(lod);
97
98                 rc = class_process_proc_param(PARAM_LOV, v.obd_vars, lcfg, obd);
99                 if (rc > 0)
100                         rc = 0;
101                 GOTO(out, rc);
102          }
103
104         case LCFG_CLEANUP:
105                 lu_dev_del_linkage(dev->ld_site, dev);
106                 lod_getref(lod);
107                 lod_foreach_ost(lod, i) {
108                         struct lod_ost_desc *ost;
109                         ost = OST_TGT(lod, i);
110                         LASSERT(ost && ost->ltd_ost);
111                         next = &ost->ltd_ost->dd_lu_dev;
112                         rc = next->ld_ops->ldo_process_config(env, next, lcfg);
113                         if (rc)
114                                 CERROR("%s: can't process %u: %d\n",
115                                        lod2obd(lod)->obd_name,
116                                        lcfg->lcfg_command, rc);
117                 }
118                 lod_putref(lod);
119
120                 /*
121                  * do cleanup on underlying storage only when
122                  * all OSPs are cleaned up, as they use that OSD as well
123                  */
124                 next = &lod->lod_child->dd_lu_dev;
125                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
126                 if (rc)
127                         CERROR("%s: can't process %u: %d\n",
128                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
129                 break;
130
131         default:
132                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
133                       lcfg->lcfg_command);
134                rc = -EINVAL;
135                break;
136         }
137
138 out:
139         RETURN(rc);
140 }
141
142 static int lod_recovery_complete(const struct lu_env *env,
143                                  struct lu_device *dev)
144 {
145         struct lod_device   *lod = lu2lod_dev(dev);
146         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
147         struct lod_ost_desc *ost;
148         int                  i, rc;
149         ENTRY;
150
151         LASSERT(lod->lod_recovery_completed == 0);
152         lod->lod_recovery_completed = 1;
153
154         rc = next->ld_ops->ldo_recovery_complete(env, next);
155
156         lod_getref(lod);
157         lod_foreach_ost(lod, i) {
158                 ost = OST_TGT(lod, i);
159                 LASSERT(ost && ost->ltd_ost);
160                 next = &ost->ltd_ost->dd_lu_dev;
161                 rc = next->ld_ops->ldo_recovery_complete(env, next);
162                 if (rc)
163                         CERROR("%s: can't complete recovery on #%d: %d\n",
164                                lod2obd(lod)->obd_name, i, rc);
165         }
166         lod_putref(lod);
167
168         RETURN(rc);
169 }
170
171 const struct lu_device_operations lod_lu_ops = {
172         .ldo_process_config     = lod_process_config,
173         .ldo_recovery_complete  = lod_recovery_complete,
174 };
175
176 static int lod_root_get(const struct lu_env *env,
177                         struct dt_device *dev, struct lu_fid *f)
178 {
179         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
180 }
181
182 static int lod_statfs(const struct lu_env *env,
183                       struct dt_device *dev, struct obd_statfs *sfs)
184 {
185         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
186 }
187
188 static struct thandle *lod_trans_create(const struct lu_env *env,
189                                         struct dt_device *dev)
190 {
191         return dt_trans_create(env, dt2lod_dev(dev)->lod_child);
192 }
193
194 static int lod_trans_start(const struct lu_env *env, struct dt_device *dev,
195                            struct thandle *th)
196 {
197         return dt_trans_start(env, dt2lod_dev(dev)->lod_child, th);
198 }
199
200 static int lod_trans_stop(const struct lu_env *env, struct thandle *th)
201 {
202         /* XXX: we don't know next device, will be fixed with DNE */
203         return dt_trans_stop(env, th->th_dev, th);
204 }
205
206 static void lod_conf_get(const struct lu_env *env,
207                          const struct dt_device *dev,
208                          struct dt_device_param *param)
209 {
210         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
211 }
212
213 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
214 {
215         struct lod_device   *lod = dt2lod_dev(dev);
216         struct lod_ost_desc *ost;
217         int                  rc = 0, i;
218         ENTRY;
219
220         lod_getref(lod);
221         lod_foreach_ost(lod, i) {
222                 ost = OST_TGT(lod, i);
223                 LASSERT(ost && ost->ltd_ost);
224                 rc = dt_sync(env, ost->ltd_ost);
225                 if (rc) {
226                         CERROR("%s: can't sync %u: %d\n",
227                                lod2obd(lod)->obd_name, i, rc);
228                         break;
229                 }
230         }
231         lod_putref(lod);
232         if (rc == 0)
233                 rc = dt_sync(env, lod->lod_child);
234
235         RETURN(rc);
236 }
237
238 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
239 {
240         return dt_ro(env, dt2lod_dev(dev)->lod_child);
241 }
242
243 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
244 {
245         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
246 }
247
248 static int lod_init_capa_ctxt(const struct lu_env *env, struct dt_device *dev,
249                               int mode, unsigned long timeout,
250                               __u32 alg, struct lustre_capa_key *keys)
251 {
252         struct dt_device *next = dt2lod_dev(dev)->lod_child;
253         return dt_init_capa_ctxt(env, next, mode, timeout, alg, keys);
254 }
255
256 static const struct dt_device_operations lod_dt_ops = {
257         .dt_root_get         = lod_root_get,
258         .dt_statfs           = lod_statfs,
259         .dt_trans_create     = lod_trans_create,
260         .dt_trans_start      = lod_trans_start,
261         .dt_trans_stop       = lod_trans_stop,
262         .dt_conf_get         = lod_conf_get,
263         .dt_sync             = lod_sync,
264         .dt_ro               = lod_ro,
265         .dt_commit_async     = lod_commit_async,
266         .dt_init_capa_ctxt   = lod_init_capa_ctxt,
267 };
268
269 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
270                               struct lustre_cfg *cfg)
271 {
272         struct obd_connect_data *data = NULL;
273         struct obd_device       *obd;
274         char                    *nextdev = NULL, *p, *s;
275         int                      rc, len = 0;
276         ENTRY;
277
278         LASSERT(cfg);
279         LASSERT(lod->lod_child_exp == NULL);
280
281         /* compatibility hack: we still use old config logs
282          * which specify LOV, but we need to learn underlying
283          * OSD device, which is supposed to be:
284          *  <fsname>-MDTxxxx-osd
285          *
286          * 2.x MGS generates lines like the following:
287          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
288          * 1.8 MGS generates lines like the following:
289          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
290          *
291          * we use "-MDT" to differentiate 2.x from 1.8 */
292
293         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
294                 len = strlen(p) + 1;
295                 OBD_ALLOC(nextdev, len);
296                 if (nextdev == NULL)
297                         GOTO(out, rc = -ENOMEM);
298
299                 strcpy(nextdev, p);
300                 s = strstr(nextdev, "-mdtlov");
301                 if (unlikely(s == NULL)) {
302                         CERROR("unable to parse device name %s\n",
303                                lustre_cfg_string(cfg, 0));
304                         GOTO(out, rc = -EINVAL);
305                 }
306
307                 if (strstr(nextdev, "-MDT")) {
308                         /* 2.x config */
309                         strcpy(s, "-osd");
310                 } else {
311                         /* 1.8 config */
312                         strcpy(s, "-MDT0000-osd");
313                 }
314         } else {
315                 CERROR("unable to parse device name %s\n",
316                        lustre_cfg_string(cfg, 0));
317                 GOTO(out, rc = -EINVAL);
318         }
319
320         OBD_ALLOC_PTR(data);
321         if (data == NULL)
322                 GOTO(out, rc = -ENOMEM);
323
324         obd = class_name2obd(nextdev);
325         if (obd == NULL) {
326                 CERROR("can not locate next device: %s\n", nextdev);
327                 GOTO(out, rc = -ENOTCONN);
328         }
329
330         data->ocd_connect_flags = OBD_CONNECT_VERSION;
331         data->ocd_version = LUSTRE_VERSION_CODE;
332
333         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
334                          data, NULL);
335         if (rc) {
336                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
337                 GOTO(out, rc);
338         }
339
340         lod->lod_dt_dev.dd_lu_dev.ld_site =
341                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
342         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
343         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
344
345 out:
346         if (data)
347                 OBD_FREE_PTR(data);
348         if (nextdev)
349                 OBD_FREE(nextdev, len);
350         RETURN(rc);
351 }
352
353 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
354                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
355 {
356         struct dt_device_param ddp;
357         struct proc_dir_entry *lov_proc_dir;
358         struct obd_device     *obd;
359         int                    rc;
360         ENTRY;
361
362         obd = class_name2obd(lustre_cfg_string(cfg, 0));
363         if (obd == NULL) {
364                 CERROR("Cannot find obd with name %s\n",
365                        lustre_cfg_string(cfg, 0));
366                 RETURN(-ENODEV);
367         }
368
369         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
370         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
371
372         rc = lod_connect_to_osd(env, lod, cfg);
373         if (rc)
374                 RETURN(rc);
375
376         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
377         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
378
379         /* for compatibility we link old procfs's OSC entries to osp ones */
380         lov_proc_dir = lprocfs_srch(proc_lustre_root, "lov");
381         if (lov_proc_dir) {
382                 cfs_proc_dir_entry_t *symlink = NULL;
383                 char *name;
384                 OBD_ALLOC(name, strlen(obd->obd_name) + 1);
385                 if (name) {
386                         strcpy(name, obd->obd_name);
387                         if (strstr(name, "lov"))
388                                 symlink = lprocfs_add_symlink(name,
389                                                 lov_proc_dir,
390                                                 "../lod/%s",
391                                                 obd->obd_name);
392                         OBD_FREE(name, strlen(obd->obd_name) + 1);
393                         lod->lod_symlink = symlink;
394                 }
395         }
396
397         cfs_mutex_init(&lod->lod_mutex);
398         cfs_init_rwsem(&lod->lod_rw_sem);
399         cfs_spin_lock_init(&lod->lod_desc_lock);
400
401         RETURN(0);
402
403         obd_disconnect(lod->lod_child_exp);
404         RETURN(rc);
405 }
406
407 static struct lu_device *lod_device_free(const struct lu_env *env,
408                                          struct lu_device *lu)
409 {
410         struct lod_device *lod = lu2lod_dev(lu);
411         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
412         ENTRY;
413
414         LASSERT(cfs_atomic_read(&lu->ld_ref) == 0);
415         dt_device_fini(&lod->lod_dt_dev);
416         OBD_FREE_PTR(lod);
417         RETURN(next);
418 }
419
420 static struct lu_device *lod_device_alloc(const struct lu_env *env,
421                                           struct lu_device_type *type,
422                                           struct lustre_cfg *lcfg)
423 {
424         struct lod_device *lod;
425         struct lu_device  *lu_dev;
426
427         OBD_ALLOC_PTR(lod);
428         if (lod == NULL) {
429                 lu_dev = ERR_PTR(-ENOMEM);
430         } else {
431                 int rc;
432
433                 lu_dev = lod2lu_dev(lod);
434                 dt_device_init(&lod->lod_dt_dev, type);
435                 rc = lod_init0(env, lod, type, lcfg);
436                 if (rc != 0) {
437                         lod_device_free(env, lu_dev);
438                         lu_dev = ERR_PTR(rc);
439                 }
440         }
441
442         return lu_dev;
443 }
444
445 static struct lu_device *lod_device_fini(const struct lu_env *env,
446                                          struct lu_device *d)
447 {
448         struct lod_device *lod = lu2lod_dev(d);
449         int                rc;
450         ENTRY;
451
452         if (lod->lod_symlink)
453                 lprocfs_remove(&lod->lod_symlink);
454
455         rc = obd_disconnect(lod->lod_child_exp);
456         if (rc)
457                 CERROR("error in disconnect from storage: %d\n", rc);
458
459         RETURN(NULL);
460 }
461
462 /*
463  * we use exports to track all LOD users
464  */
465 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
466                            struct obd_device *obd, struct obd_uuid *cluuid,
467                            struct obd_connect_data *data, void *localdata)
468 {
469         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
470         struct lustre_handle  conn;
471         int                   rc;
472         ENTRY;
473
474         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
475
476         rc = class_connect(&conn, obd, cluuid);
477         if (rc)
478                 RETURN(rc);
479
480         *exp = class_conn2export(&conn);
481
482         cfs_mutex_lock(&lod->lod_mutex);
483         lod->lod_connects++;
484         /* at the moment we expect the only user */
485         LASSERT(lod->lod_connects == 1);
486         cfs_mutex_unlock(&lod->lod_mutex);
487
488         RETURN(0);
489 }
490
491 /*
492  * once last export (we don't count self-export) disappeared
493  * lod can be released
494  */
495 static int lod_obd_disconnect(struct obd_export *exp)
496 {
497         struct obd_device *obd = exp->exp_obd;
498         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
499         int                rc, release = 0;
500         ENTRY;
501
502         /* Only disconnect the underlying layers on the final disconnect. */
503         cfs_mutex_lock(&lod->lod_mutex);
504         lod->lod_connects--;
505         if (lod->lod_connects != 0) {
506                 /* why should there be more than 1 connect? */
507                 cfs_mutex_unlock(&lod->lod_mutex);
508                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
509                        lod->lod_connects);
510                 goto out;
511         }
512         cfs_mutex_unlock(&lod->lod_mutex);
513
514         /* the last user of lod has gone, let's release the device */
515         release = 1;
516
517 out:
518         rc = class_disconnect(exp); /* bz 9811 */
519
520         if (rc == 0 && release)
521                 class_manual_cleanup(obd);
522         RETURN(rc);
523 }
524
525 LU_KEY_INIT(lod, struct lod_thread_info);
526
527 static void lod_key_fini(const struct lu_context *ctx,
528                 struct lu_context_key *key, void *data)
529 {
530         struct lod_thread_info *info = data;
531         /* allocated in lod_get_lov_ea
532          * XXX: this is overload, a tread may have such store but used only
533          * once. Probably better would be pool of such stores per LOD.
534          */
535         if (info->lti_ea_store) {
536                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
537                 info->lti_ea_store = NULL;
538                 info->lti_ea_store_size = 0;
539         }
540         OBD_FREE_PTR(info);
541 }
542
543 /* context key: lod_thread_key */
544 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
545
546 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
547
548 static struct lu_device_type_operations lod_device_type_ops = {
549         .ldto_init           = lod_type_init,
550         .ldto_fini           = lod_type_fini,
551
552         .ldto_start          = lod_type_start,
553         .ldto_stop           = lod_type_stop,
554
555         .ldto_device_alloc   = lod_device_alloc,
556         .ldto_device_free    = lod_device_free,
557
558         .ldto_device_fini    = lod_device_fini
559 };
560
561 static struct lu_device_type lod_device_type = {
562         .ldt_tags     = LU_DEVICE_DT,
563         .ldt_name     = LUSTRE_LOD_NAME,
564         .ldt_ops      = &lod_device_type_ops,
565         .ldt_ctx_tags = LCT_MD_THREAD,
566 };
567
568 static int lod_obd_health_check(const struct lu_env *env,
569                 struct obd_device *obd)
570 {
571         struct lod_device   *d = lu2lod_dev(obd->obd_lu_dev);
572         struct lod_ost_desc *ost;
573         int                  i, rc = 1;
574         ENTRY;
575
576         LASSERT(d);
577         lod_getref(d);
578         cfs_foreach_bit(d->lod_ost_bitmap, i) {
579                 ost = OST_TGT(d, i);
580                 LASSERT(ost && ost->ltd_ost);
581                 rc = obd_health_check(env, ost->ltd_exp->exp_obd);
582                 /* one healthy device is enough */
583                 if (rc == 0)
584                         break;
585         }
586         lod_putref(d);
587         RETURN(rc);
588 }
589
590 static struct obd_ops lod_obd_device_ops = {
591         .o_owner        = THIS_MODULE,
592         .o_connect      = lod_obd_connect,
593         .o_disconnect   = lod_obd_disconnect,
594         .o_health_check = lod_obd_health_check,
595 };
596
597 static int __init lod_mod_init(void)
598 {
599         struct lprocfs_static_vars  lvars = { 0 };
600         cfs_proc_dir_entry_t       *lov_proc_dir;
601         int                         rc;
602
603         rc = lu_kmem_init(lod_caches);
604         if (rc)
605                 return rc;
606
607         rc = class_register_type(&lod_obd_device_ops, NULL, lvars.module_vars,
608                                  LUSTRE_LOD_NAME, &lod_device_type);
609         if (rc) {
610                 lu_kmem_fini(lod_caches);
611                 return rc;
612         }
613
614         /* create "lov" entry in procfs for compatibility purposes */
615         lov_proc_dir = lprocfs_srch(proc_lustre_root, "lov");
616         if (lov_proc_dir == NULL) {
617                 lov_proc_dir = lprocfs_register("lov", proc_lustre_root,
618                                                 NULL, NULL);
619                 if (IS_ERR(lov_proc_dir))
620                         CERROR("lod: can't create compat entry \"lov\": %d\n",
621                                (int)PTR_ERR(lov_proc_dir));
622         }
623
624         return rc;
625 }
626
627 static void __exit lod_mod_exit(void)
628 {
629
630         lprocfs_try_remove_proc_entry("lov", proc_lustre_root);
631
632         class_unregister_type(LUSTRE_LOD_NAME);
633         lu_kmem_fini(lod_caches);
634 }
635
636 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
637 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
638 MODULE_LICENSE("GPL");
639
640 module_init(lod_mod_init);
641 module_exit(lod_mod_exit);
642