Whamcloud - gitweb
LU-2955 tests: make replay-ost-single/8b SLOW for ZFS
[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) 2012, Intel Corporation.
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_fid.h>
48 #include <lustre_param.h>
49 #include <lustre_update.h>
50
51 #include "lod_internal.h"
52
53 /**
54  * Lookup MDT/OST index \a tgt by FID \a fid.
55  *
56  * \param lod LOD to be lookup at.
57  * \param fid FID of object to find MDT/OST.
58  * \param tgt MDT/OST index to return.
59  * \param flags indidcate the FID is on MDS or OST.
60  **/
61 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
62                    const struct lu_fid *fid, __u32 *tgt, int flags)
63 {
64         struct lu_seq_range     range;
65         struct lu_server_fld    *server_fld;
66         int rc = 0;
67         ENTRY;
68
69         LASSERTF(fid_is_sane(fid), "Invalid FID "DFID"\n", PFID(fid));
70         if (fid_is_idif(fid)) {
71                 *tgt = fid_idif_ost_idx(fid);
72                 RETURN(rc);
73         }
74
75         if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
76                 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
77                 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
78                 RETURN(rc);
79         }
80
81         server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
82         range.lsr_flags = flags;
83         rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
84         if (rc) {
85                 CERROR("%s: Can't find tgt by seq "LPX64", rc %d\n",
86                        lod2obd(lod)->obd_name, fid_seq(fid), rc);
87                 RETURN(rc);
88         }
89
90         *tgt = range.lsr_index;
91
92         CDEBUG(D_INFO, "LOD: got tgt %x for sequence: "
93                LPX64"\n", *tgt, fid_seq(fid));
94
95         RETURN(rc);
96 }
97
98 extern struct lu_object_operations lod_lu_obj_ops;
99 extern struct lu_object_operations lod_lu_robj_ops;
100 extern struct dt_object_operations lod_obj_ops;
101 extern struct dt_lock_operations   lod_lock_ops;
102
103 /* Slab for OSD object allocation */
104 cfs_mem_cache_t *lod_object_kmem;
105
106 static struct lu_kmem_descr lod_caches[] = {
107         {
108                 .ckd_cache = &lod_object_kmem,
109                 .ckd_name  = "lod_obj",
110                 .ckd_size  = sizeof(struct lod_object)
111         },
112         {
113                 .ckd_cache = NULL
114         }
115 };
116
117 static struct lu_device *lod_device_fini(const struct lu_env *env,
118                                          struct lu_device *d);
119
120 struct lu_object *lod_object_alloc(const struct lu_env *env,
121                                    const struct lu_object_header *hdr,
122                                    struct lu_device *dev)
123 {
124         struct lod_object       *lod_obj;
125         struct lu_object        *lu_obj;
126         const struct lu_fid     *fid = &hdr->loh_fid;
127         mdsno_t                 mds;
128         int                     rc = 0;
129         ENTRY;
130
131         OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, CFS_ALLOC_IO);
132         if (lod_obj == NULL)
133                 RETURN(ERR_PTR(-ENOMEM));
134
135         rc = lod_fld_lookup(env, lu2lod_dev(dev), fid, &mds, LU_SEQ_RANGE_MDT);
136         if (rc) {
137                 OBD_SLAB_FREE_PTR(lod_obj, lod_object_kmem);
138                 RETURN(ERR_PTR(rc));
139         }
140
141         lod_obj->ldo_mds_num = mds;
142         lu_obj = lod2lu_obj(lod_obj);
143         dt_object_init(&lod_obj->ldo_obj, NULL, dev);
144         lod_obj->ldo_obj.do_ops = &lod_obj_ops;
145         lod_obj->ldo_obj.do_lock_ops = &lod_lock_ops;
146         if (likely(mds == lu_site2seq(dev->ld_site)->ss_node_id))
147                 lu_obj->lo_ops = &lod_lu_obj_ops;
148         else
149                 lu_obj->lo_ops = &lod_lu_robj_ops;
150         RETURN(lu_obj);
151 }
152
153 static int lod_cleanup_desc_tgts(const struct lu_env *env,
154                                  struct lod_device *lod,
155                                  struct lod_tgt_descs *ltd,
156                                  struct lustre_cfg *lcfg)
157 {
158         struct lu_device  *next;
159         int rc = 0;
160         int i;
161
162         lod_getref(ltd);
163         if (ltd->ltd_tgts_size <= 0) {
164                 lod_putref(lod, ltd);
165                 return 0;
166         }
167         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
168                 struct lod_tgt_desc *tgt;
169                 int rc1;
170
171                 tgt = LTD_TGT(ltd, i);
172                 LASSERT(tgt && tgt->ltd_tgt);
173                 next = &tgt->ltd_tgt->dd_lu_dev;
174                 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
175                 if (rc1) {
176                         CERROR("%s: error cleaning up LOD index %u: cmd %#x"
177                                ": rc = %d\n", lod2obd(lod)->obd_name, i,
178                                lcfg->lcfg_command, rc1);
179                         rc = rc1;
180                 }
181         }
182         lod_putref(lod, ltd);
183         return rc;
184 }
185
186 static int lodname2mdt_index(char *lodname, int *index)
187 {
188         char *ptr, *tmp;
189
190         /* The lodname suppose to be fsname-MDTxxxx-mdtlov */
191         ptr = strrchr(lodname, '-');
192         if (ptr == NULL) {
193                 CERROR("invalid MDT index in '%s'\n", lodname);
194                 return -EINVAL;
195         }
196
197         if (strncmp(ptr, "-mdtlov", 7) != 0) {
198                 CERROR("invalid MDT index in '%s'\n", lodname);
199                 return -EINVAL;
200         }
201
202         if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
203                 CERROR("invalid MDT index in '%s'\n", lodname);
204                 return -EINVAL;
205         }
206
207         if (strncmp(ptr - 8, "-MDT", 4) != 0) {
208                 CERROR("invalid MDT index in '%s'\n", lodname);
209                 return -EINVAL;
210         }
211
212         *index = simple_strtol(ptr - 4, &tmp, 16);
213         if (*tmp != '-' || *index > INT_MAX || *index < 0) {
214                 CERROR("invalid MDT index in '%s'\n", lodname);
215                 return -EINVAL;
216         }
217         return 0;
218 }
219
220 /*
221  * Init client sequence manager which is used by local MDS to talk to sequence
222  * controller on remote node.
223  */
224 static int lod_seq_init_cli(const struct lu_env *env,
225                             struct lod_device *lod,
226                             char *tgtuuid, int index)
227 {
228         struct seq_server_site  *ss;
229         struct obd_device       *osp;
230         int                     rc;
231         char                    *prefix;
232         struct obd_uuid         obd_uuid;
233         ENTRY;
234
235         ss = lu_site2seq(lod2lu_dev(lod)->ld_site);
236         LASSERT(ss != NULL);
237
238         /* check if this is adding the first MDC and controller is not yet
239          * initialized. */
240         if (index != 0 || ss->ss_client_seq)
241                 RETURN(0);
242
243         obd_str2uuid(&obd_uuid, tgtuuid);
244         osp = class_find_client_obd(&obd_uuid, LUSTRE_OSP_NAME,
245                                    &lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_uuid);
246         if (osp == NULL) {
247                 CERROR("%s: can't find %s device\n",
248                         lod->lod_dt_dev.dd_lu_dev.ld_obd->obd_name,
249                         tgtuuid);
250                 RETURN(-EINVAL);
251         }
252
253         if (!osp->obd_set_up) {
254                 CERROR("target %s not set up\n", osp->obd_name);
255                 rc = -EINVAL;
256         }
257
258         LASSERT(ss->ss_control_exp);
259         OBD_ALLOC_PTR(ss->ss_client_seq);
260         if (ss->ss_client_seq == NULL)
261                 RETURN(-ENOMEM);
262
263         OBD_ALLOC(prefix, MAX_OBD_NAME + 5);
264         if (!prefix) {
265                 OBD_FREE_PTR(ss->ss_client_seq);
266                 ss->ss_client_seq = NULL;
267                 RETURN(-ENOMEM);
268         }
269
270         snprintf(prefix, MAX_OBD_NAME + 5, "ctl-%s", osp->obd_name);
271         rc = seq_client_init(ss->ss_client_seq, ss->ss_control_exp,
272                              LUSTRE_SEQ_METADATA, prefix, NULL);
273         OBD_FREE(prefix, MAX_OBD_NAME + 5);
274         if (rc) {
275                 OBD_FREE_PTR(ss->ss_client_seq);
276                 ss->ss_client_seq = NULL;
277                 RETURN(rc);
278         }
279
280         LASSERT(ss->ss_server_seq != NULL);
281         rc = seq_server_set_cli(ss->ss_server_seq, ss->ss_client_seq,
282                                 env);
283
284         RETURN(rc);
285 }
286
287 static void lod_seq_fini_cli(struct lod_device *lod)
288 {
289         struct seq_server_site *ss;
290
291         ENTRY;
292
293         ss = lu_site2seq(lod2lu_dev(lod)->ld_site);
294         if (ss == NULL) {
295                 EXIT;
296                 return;
297         }
298
299         if (ss->ss_server_seq)
300                 seq_server_set_cli(ss->ss_server_seq,
301                            NULL, NULL);
302
303         if (ss->ss_control_exp) {
304                 class_export_put(ss->ss_control_exp);
305                 ss->ss_control_exp = NULL;
306         }
307
308         EXIT;
309         return;
310 }
311
312 /**
313  * Procss config log on LOD
314  * \param env environment info
315  * \param dev lod device
316  * \param lcfg config log
317  *
318  * Add osc config log,
319  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
320  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
321  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
322  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
323  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
324  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
325  *
326  * Add mdc config log
327  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
328  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
329  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
330  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
331  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
332  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
333  **/
334 static int lod_process_config(const struct lu_env *env,
335                               struct lu_device *dev,
336                               struct lustre_cfg *lcfg)
337 {
338         struct lod_device *lod = lu2lod_dev(dev);
339         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
340         char              *arg1;
341         int                rc = 0;
342         ENTRY;
343
344         switch(lcfg->lcfg_command) {
345         case LCFG_LOV_DEL_OBD:
346         case LCFG_LOV_ADD_INA:
347         case LCFG_LOV_ADD_OBD:
348         case LCFG_ADD_MDC: {
349                 __u32 index;
350                 __u32 mdt_index;
351                 int gen;
352                 /* lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
353                  * modify_mdc_tgts add  0:lustre-MDT0001
354                  *                    1:lustre-MDT0001-mdc0002
355                  *                    2:2  3:1*/
356                 arg1 = lustre_cfg_string(lcfg, 1);
357
358                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
359                         GOTO(out, rc = -EINVAL);
360                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
361                         GOTO(out, rc = -EINVAL);
362
363                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
364                         char *mdt;
365                         mdt = strstr(lustre_cfg_string(lcfg, 0), "-MDT");
366                         /* 1.8 configs don't have "-MDT0000" at the end */
367                         if (mdt == NULL) {
368                                 mdt_index = 0;
369                         } else {
370                                 rc = lodname2mdt_index(
371                                         lustre_cfg_string(lcfg, 0), &mdt_index);
372                                 if (rc != 0)
373                                         GOTO(out, rc);
374                         }
375                         rc = lod_add_device(env, lod, arg1, index, gen,
376                                             mdt_index, LUSTRE_OSC_NAME, 1);
377                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
378                         mdt_index = index;
379                         rc = lod_add_device(env, lod, arg1, index, gen,
380                                             mdt_index, LUSTRE_MDC_NAME, 1);
381                         if (rc == 0)
382                                 rc = lod_seq_init_cli(env, lod, arg1,
383                                                       mdt_index);
384                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
385                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
386                         mdt_index = 0;
387                         rc = lod_add_device(env, lod, arg1, index, gen,
388                                             mdt_index, LUSTRE_OSC_NAME, 0);
389                 } else {
390                         rc = lod_del_device(env, lod,
391                                             &lod->lod_ost_descs,
392                                             arg1, index, gen);
393                 }
394
395                 break;
396         }
397
398         case LCFG_PARAM: {
399                 struct lprocfs_static_vars  v = { 0 };
400                 struct obd_device         *obd = lod2obd(lod);
401
402                 lprocfs_lod_init_vars(&v);
403
404                 rc = class_process_proc_param(PARAM_LOV, v.obd_vars, lcfg, obd);
405                 if (rc > 0)
406                         rc = 0;
407                 GOTO(out, rc);
408         }
409         case LCFG_CLEANUP:
410         case LCFG_PRE_CLEANUP: {
411                 lu_dev_del_linkage(dev->ld_site, dev);
412                 lod_cleanup_desc_tgts(env, lod, &lod->lod_mdt_descs, lcfg);
413                 lod_cleanup_desc_tgts(env, lod, &lod->lod_ost_descs, lcfg);
414
415                 lod_seq_fini_cli(lod);
416
417                 if (lcfg->lcfg_command == LCFG_PRE_CLEANUP)
418                         break;
419                 /*
420                  * do cleanup on underlying storage only when
421                  * all OSPs are cleaned up, as they use that OSD as well
422                  */
423                 next = &lod->lod_child->dd_lu_dev;
424                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
425                 if (rc)
426                         CERROR("%s: can't process %u: %d\n",
427                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
428
429                 rc = obd_disconnect(lod->lod_child_exp);
430                 if (rc)
431                         CERROR("error in disconnect from storage: %d\n", rc);
432                 break;
433         }
434         default:
435                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
436                       lcfg->lcfg_command);
437                rc = -EINVAL;
438                break;
439         }
440
441 out:
442         RETURN(rc);
443 }
444
445 static int lod_recovery_complete(const struct lu_env *env,
446                                  struct lu_device *dev)
447 {
448         struct lod_device   *lod = lu2lod_dev(dev);
449         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
450         int                  i, rc;
451         ENTRY;
452
453         LASSERT(lod->lod_recovery_completed == 0);
454         lod->lod_recovery_completed = 1;
455
456         rc = next->ld_ops->ldo_recovery_complete(env, next);
457
458         lod_getref(&lod->lod_ost_descs);
459         if (lod->lod_osts_size > 0) {
460                 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
461                         struct lod_tgt_desc *tgt;
462                         tgt = OST_TGT(lod, i);
463                         LASSERT(tgt && tgt->ltd_tgt);
464                         next = &tgt->ltd_ost->dd_lu_dev;
465                         rc = next->ld_ops->ldo_recovery_complete(env, next);
466                         if (rc)
467                                 CERROR("%s: can't complete recovery on #%d:"
468                                         "%d\n", lod2obd(lod)->obd_name, i, rc);
469                 }
470         }
471         lod_putref(lod, &lod->lod_ost_descs);
472         RETURN(rc);
473 }
474
475 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
476                        struct lu_device *cdev)
477 {
478         struct lod_device   *lod = lu2lod_dev(cdev);
479         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
480         int                  rc;
481         ENTRY;
482
483         rc = next->ld_ops->ldo_prepare(env, pdev, next);
484         if (rc != 0) {
485                 CERROR("%s: prepare bottom error: rc = %d\n",
486                        lod2obd(lod)->obd_name, rc);
487                 RETURN(rc);
488         }
489
490         lod->lod_initialized = 1;
491
492         RETURN(rc);
493 }
494
495 const struct lu_device_operations lod_lu_ops = {
496         .ldo_object_alloc       = lod_object_alloc,
497         .ldo_process_config     = lod_process_config,
498         .ldo_recovery_complete  = lod_recovery_complete,
499         .ldo_prepare            = lod_prepare,
500 };
501
502 static int lod_root_get(const struct lu_env *env,
503                         struct dt_device *dev, struct lu_fid *f)
504 {
505         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
506 }
507
508 static int lod_statfs(const struct lu_env *env,
509                       struct dt_device *dev, struct obd_statfs *sfs)
510 {
511         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
512 }
513
514 static struct thandle *lod_trans_create(const struct lu_env *env,
515                                         struct dt_device *dev)
516 {
517         struct thandle *th;
518
519         th = dt_trans_create(env, dt2lod_dev(dev)->lod_child);
520         if (IS_ERR(th))
521                 return th;
522
523         CFS_INIT_LIST_HEAD(&th->th_remote_update_list);
524         return th;
525 }
526
527 static int lod_remote_sync(const struct lu_env *env, struct dt_device *dev,
528                            struct thandle *th)
529 {
530         struct update_request *update;
531         int    rc = 0;
532         ENTRY;
533
534         if (cfs_list_empty(&th->th_remote_update_list))
535                 RETURN(0);
536
537         cfs_list_for_each_entry(update, &th->th_remote_update_list,
538                                 ur_list) {
539                 /* In DNE phase I, there should be only one OSP
540                  * here, so we will do send/receive one by one,
541                  * instead of sending them parallel, will fix this
542                  * in Phase II */
543                 th->th_current_request = update;
544                 rc = dt_trans_start(env, update->ur_dt, th);
545                 if (rc != 0) {
546                         /* FIXME how to revert the partial results
547                          * once error happened? Resolved by 2 Phase commit */
548                         update->ur_rc = rc;
549                         break;
550                 }
551         }
552
553         RETURN(rc);
554 }
555
556 static int lod_trans_start(const struct lu_env *env, struct dt_device *dev,
557                            struct thandle *th)
558 {
559         struct lod_device *lod = dt2lod_dev((struct dt_device *) dev);
560         int rc;
561
562         rc = lod_remote_sync(env, dev, th);
563         if (rc)
564                 return rc;
565
566         return dt_trans_start(env, lod->lod_child, th);
567 }
568
569 static int lod_trans_stop(const struct lu_env *env, struct thandle *th)
570 {
571         struct update_request *update;
572         struct update_request *tmp;
573         int rc = 0;
574         int rc2 = 0;
575
576         cfs_list_for_each_entry_safe(update, tmp,
577                                      &th->th_remote_update_list,
578                                      ur_list) {
579                 th->th_current_request = update;
580                 rc2 = dt_trans_stop(env, update->ur_dt, th);
581                 if (unlikely(rc2 != 0 && rc == 0))
582                         rc = rc2;
583         }
584
585         rc2 = dt_trans_stop(env, th->th_dev, th);
586
587         return rc2 != 0 ? rc2 : rc;
588 }
589
590 static void lod_conf_get(const struct lu_env *env,
591                          const struct dt_device *dev,
592                          struct dt_device_param *param)
593 {
594         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
595 }
596
597 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
598 {
599         struct lod_device   *lod = dt2lod_dev(dev);
600         struct lod_ost_desc *ost;
601         int                  rc = 0, i;
602         ENTRY;
603
604         lod_getref(&lod->lod_ost_descs);
605         lod_foreach_ost(lod, i) {
606                 ost = OST_TGT(lod, i);
607                 LASSERT(ost && ost->ltd_ost);
608                 rc = dt_sync(env, ost->ltd_ost);
609                 if (rc) {
610                         CERROR("%s: can't sync %u: %d\n",
611                                lod2obd(lod)->obd_name, i, rc);
612                         break;
613                 }
614         }
615         lod_putref(lod, &lod->lod_ost_descs);
616         if (rc == 0)
617                 rc = dt_sync(env, lod->lod_child);
618
619         RETURN(rc);
620 }
621
622 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
623 {
624         return dt_ro(env, dt2lod_dev(dev)->lod_child);
625 }
626
627 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
628 {
629         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
630 }
631
632 static int lod_init_capa_ctxt(const struct lu_env *env, struct dt_device *dev,
633                               int mode, unsigned long timeout,
634                               __u32 alg, struct lustre_capa_key *keys)
635 {
636         struct dt_device *next = dt2lod_dev(dev)->lod_child;
637         return dt_init_capa_ctxt(env, next, mode, timeout, alg, keys);
638 }
639
640 static const struct dt_device_operations lod_dt_ops = {
641         .dt_root_get         = lod_root_get,
642         .dt_statfs           = lod_statfs,
643         .dt_trans_create     = lod_trans_create,
644         .dt_trans_start      = lod_trans_start,
645         .dt_trans_stop       = lod_trans_stop,
646         .dt_conf_get         = lod_conf_get,
647         .dt_sync             = lod_sync,
648         .dt_ro               = lod_ro,
649         .dt_commit_async     = lod_commit_async,
650         .dt_init_capa_ctxt   = lod_init_capa_ctxt,
651 };
652
653 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
654                               struct lustre_cfg *cfg)
655 {
656         struct obd_connect_data *data = NULL;
657         struct obd_device       *obd;
658         char                    *nextdev = NULL, *p, *s;
659         int                      rc, len = 0;
660         ENTRY;
661
662         LASSERT(cfg);
663         LASSERT(lod->lod_child_exp == NULL);
664
665         /* compatibility hack: we still use old config logs
666          * which specify LOV, but we need to learn underlying
667          * OSD device, which is supposed to be:
668          *  <fsname>-MDTxxxx-osd
669          *
670          * 2.x MGS generates lines like the following:
671          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
672          * 1.8 MGS generates lines like the following:
673          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
674          *
675          * we use "-MDT" to differentiate 2.x from 1.8 */
676
677         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
678                 len = strlen(p) + 1;
679                 OBD_ALLOC(nextdev, len);
680                 if (nextdev == NULL)
681                         GOTO(out, rc = -ENOMEM);
682
683                 strcpy(nextdev, p);
684                 s = strstr(nextdev, "-mdtlov");
685                 if (unlikely(s == NULL)) {
686                         CERROR("unable to parse device name %s\n",
687                                lustre_cfg_string(cfg, 0));
688                         GOTO(out, rc = -EINVAL);
689                 }
690
691                 if (strstr(nextdev, "-MDT")) {
692                         /* 2.x config */
693                         strcpy(s, "-osd");
694                 } else {
695                         /* 1.8 config */
696                         strcpy(s, "-MDT0000-osd");
697                 }
698         } else {
699                 CERROR("unable to parse device name %s\n",
700                        lustre_cfg_string(cfg, 0));
701                 GOTO(out, rc = -EINVAL);
702         }
703
704         OBD_ALLOC_PTR(data);
705         if (data == NULL)
706                 GOTO(out, rc = -ENOMEM);
707
708         obd = class_name2obd(nextdev);
709         if (obd == NULL) {
710                 CERROR("can not locate next device: %s\n", nextdev);
711                 GOTO(out, rc = -ENOTCONN);
712         }
713
714         data->ocd_connect_flags = OBD_CONNECT_VERSION;
715         data->ocd_version = LUSTRE_VERSION_CODE;
716
717         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
718                          data, NULL);
719         if (rc) {
720                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
721                 GOTO(out, rc);
722         }
723
724         lod->lod_dt_dev.dd_lu_dev.ld_site =
725                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
726         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
727         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
728
729 out:
730         if (data)
731                 OBD_FREE_PTR(data);
732         if (nextdev)
733                 OBD_FREE(nextdev, len);
734         RETURN(rc);
735 }
736
737 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
738 {
739         mutex_init(&ltd->ltd_mutex);
740         init_rwsem(&ltd->ltd_rw_sem);
741
742         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
743          * added to the LOD, see lod_add_device() */
744         ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
745         if (ltd->ltd_tgt_bitmap == NULL)
746                 RETURN(-ENOMEM);
747
748         ltd->ltd_tgts_size  = 32;
749         ltd->ltd_tgtnr      = 0;
750
751         ltd->ltd_death_row = 0;
752         ltd->ltd_refcount  = 0;
753         return 0;
754 }
755
756 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
757                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
758 {
759         struct dt_device_param ddp;
760         struct obd_device     *obd;
761         int                    rc;
762         ENTRY;
763
764         obd = class_name2obd(lustre_cfg_string(cfg, 0));
765         if (obd == NULL) {
766                 CERROR("Cannot find obd with name %s\n",
767                        lustre_cfg_string(cfg, 0));
768                 RETURN(-ENODEV);
769         }
770
771         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
772         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
773         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
774         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
775
776         rc = lod_connect_to_osd(env, lod, cfg);
777         if (rc)
778                 RETURN(rc);
779
780         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
781         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
782
783         /* setup obd to be used with old lov code */
784         rc = lod_pools_init(lod, cfg);
785         if (rc)
786                 GOTO(out_disconnect, rc);
787
788         rc = lod_procfs_init(lod);
789         if (rc)
790                 GOTO(out_pools, rc);
791
792         spin_lock_init(&lod->lod_desc_lock);
793         spin_lock_init(&lod->lod_connects_lock);
794         lod_tgt_desc_init(&lod->lod_mdt_descs);
795         lod_tgt_desc_init(&lod->lod_ost_descs);
796
797         RETURN(0);
798
799 out_pools:
800         lod_pools_fini(lod);
801 out_disconnect:
802         obd_disconnect(lod->lod_child_exp);
803         RETURN(rc);
804 }
805
806 static struct lu_device *lod_device_free(const struct lu_env *env,
807                                          struct lu_device *lu)
808 {
809         struct lod_device *lod = lu2lod_dev(lu);
810         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
811         ENTRY;
812
813         LASSERT(cfs_atomic_read(&lu->ld_ref) == 0);
814         dt_device_fini(&lod->lod_dt_dev);
815         OBD_FREE_PTR(lod);
816         RETURN(next);
817 }
818
819 static struct lu_device *lod_device_alloc(const struct lu_env *env,
820                                           struct lu_device_type *type,
821                                           struct lustre_cfg *lcfg)
822 {
823         struct lod_device *lod;
824         struct lu_device  *lu_dev;
825
826         OBD_ALLOC_PTR(lod);
827         if (lod == NULL) {
828                 lu_dev = ERR_PTR(-ENOMEM);
829         } else {
830                 int rc;
831
832                 lu_dev = lod2lu_dev(lod);
833                 dt_device_init(&lod->lod_dt_dev, type);
834                 rc = lod_init0(env, lod, type, lcfg);
835                 if (rc != 0) {
836                         lod_device_free(env, lu_dev);
837                         lu_dev = ERR_PTR(rc);
838                 }
839         }
840
841         return lu_dev;
842 }
843
844 static struct lu_device *lod_device_fini(const struct lu_env *env,
845                                          struct lu_device *d)
846 {
847         struct lod_device *lod = lu2lod_dev(d);
848         int                rc;
849         ENTRY;
850
851         lod_pools_fini(lod);
852
853         lod_procfs_fini(lod);
854
855         rc = lod_fini_tgt(lod, &lod->lod_ost_descs);
856         if (rc)
857                 CERROR("%s:can not fini ost descs %d\n",
858                         lod2obd(lod)->obd_name, rc);
859
860         rc = lod_fini_tgt(lod, &lod->lod_mdt_descs);
861         if (rc)
862                 CERROR("%s:can not fini mdt descs %d\n",
863                         lod2obd(lod)->obd_name, rc);
864
865         RETURN(NULL);
866 }
867
868 /*
869  * we use exports to track all LOD users
870  */
871 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
872                            struct obd_device *obd, struct obd_uuid *cluuid,
873                            struct obd_connect_data *data, void *localdata)
874 {
875         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
876         struct lustre_handle  conn;
877         int                   rc;
878         ENTRY;
879
880         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
881
882         rc = class_connect(&conn, obd, cluuid);
883         if (rc)
884                 RETURN(rc);
885
886         *exp = class_conn2export(&conn);
887
888         spin_lock(&lod->lod_connects_lock);
889         lod->lod_connects++;
890         /* at the moment we expect the only user */
891         LASSERT(lod->lod_connects == 1);
892         spin_unlock(&lod->lod_connects_lock);
893
894         RETURN(0);
895 }
896
897 /*
898  * once last export (we don't count self-export) disappeared
899  * lod can be released
900  */
901 static int lod_obd_disconnect(struct obd_export *exp)
902 {
903         struct obd_device *obd = exp->exp_obd;
904         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
905         int                rc, release = 0;
906         ENTRY;
907
908         /* Only disconnect the underlying layers on the final disconnect. */
909         spin_lock(&lod->lod_connects_lock);
910         lod->lod_connects--;
911         if (lod->lod_connects != 0) {
912                 /* why should there be more than 1 connect? */
913                 spin_unlock(&lod->lod_connects_lock);
914                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
915                        lod->lod_connects);
916                 goto out;
917         }
918         spin_unlock(&lod->lod_connects_lock);
919
920         /* the last user of lod has gone, let's release the device */
921         release = 1;
922
923 out:
924         rc = class_disconnect(exp); /* bz 9811 */
925
926         if (rc == 0 && release)
927                 class_manual_cleanup(obd);
928         RETURN(rc);
929 }
930
931 LU_KEY_INIT(lod, struct lod_thread_info);
932
933 static void lod_key_fini(const struct lu_context *ctx,
934                 struct lu_context_key *key, void *data)
935 {
936         struct lod_thread_info *info = data;
937         /* allocated in lod_get_lov_ea
938          * XXX: this is overload, a tread may have such store but used only
939          * once. Probably better would be pool of such stores per LOD.
940          */
941         if (info->lti_ea_store) {
942                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
943                 info->lti_ea_store = NULL;
944                 info->lti_ea_store_size = 0;
945         }
946         OBD_FREE_PTR(info);
947 }
948
949 /* context key: lod_thread_key */
950 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
951
952 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
953
954 static struct lu_device_type_operations lod_device_type_ops = {
955         .ldto_init           = lod_type_init,
956         .ldto_fini           = lod_type_fini,
957
958         .ldto_start          = lod_type_start,
959         .ldto_stop           = lod_type_stop,
960
961         .ldto_device_alloc   = lod_device_alloc,
962         .ldto_device_free    = lod_device_free,
963
964         .ldto_device_fini    = lod_device_fini
965 };
966
967 static struct lu_device_type lod_device_type = {
968         .ldt_tags     = LU_DEVICE_DT,
969         .ldt_name     = LUSTRE_LOD_NAME,
970         .ldt_ops      = &lod_device_type_ops,
971         .ldt_ctx_tags = LCT_MD_THREAD,
972 };
973
974 static int lod_obd_health_check(const struct lu_env *env,
975                 struct obd_device *obd)
976 {
977         struct lod_device   *d = lu2lod_dev(obd->obd_lu_dev);
978         struct lod_ost_desc *ost;
979         int                  i, rc = 1;
980         ENTRY;
981
982         LASSERT(d);
983         lod_getref(&d->lod_ost_descs);
984         lod_foreach_ost(d, i) {
985                 ost = OST_TGT(d, i);
986                 LASSERT(ost && ost->ltd_ost);
987                 rc = obd_health_check(env, ost->ltd_exp->exp_obd);
988                 /* one healthy device is enough */
989                 if (rc == 0)
990                         break;
991         }
992         lod_putref(d, &d->lod_ost_descs);
993         RETURN(rc);
994 }
995
996 static struct obd_ops lod_obd_device_ops = {
997         .o_owner        = THIS_MODULE,
998         .o_connect      = lod_obd_connect,
999         .o_disconnect   = lod_obd_disconnect,
1000         .o_health_check = lod_obd_health_check,
1001         .o_pool_new     = lod_pool_new,
1002         .o_pool_rem     = lod_pool_remove,
1003         .o_pool_add     = lod_pool_add,
1004         .o_pool_del     = lod_pool_del,
1005 };
1006
1007 static int __init lod_mod_init(void)
1008 {
1009         struct lprocfs_static_vars  lvars = { 0 };
1010         cfs_proc_dir_entry_t       *lov_proc_dir;
1011         int                         rc;
1012
1013         rc = lu_kmem_init(lod_caches);
1014         if (rc)
1015                 return rc;
1016
1017         lprocfs_lod_init_vars(&lvars);
1018
1019         rc = class_register_type(&lod_obd_device_ops, NULL, lvars.module_vars,
1020                                  LUSTRE_LOD_NAME, &lod_device_type);
1021         if (rc) {
1022                 lu_kmem_fini(lod_caches);
1023                 return rc;
1024         }
1025
1026         /* create "lov" entry in procfs for compatibility purposes */
1027         lov_proc_dir = lprocfs_srch(proc_lustre_root, "lov");
1028         if (lov_proc_dir == NULL) {
1029                 lov_proc_dir = lprocfs_register("lov", proc_lustre_root,
1030                                                 NULL, NULL);
1031                 if (IS_ERR(lov_proc_dir))
1032                         CERROR("lod: can't create compat entry \"lov\": %d\n",
1033                                (int)PTR_ERR(lov_proc_dir));
1034         }
1035
1036         return rc;
1037 }
1038
1039 static void __exit lod_mod_exit(void)
1040 {
1041
1042         lprocfs_try_remove_proc_entry("lov", proc_lustre_root);
1043
1044         class_unregister_type(LUSTRE_LOD_NAME);
1045         lu_kmem_fini(lod_caches);
1046 }
1047
1048 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
1049 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
1050 MODULE_LICENSE("GPL");
1051
1052 module_init(lod_mod_init);
1053 module_exit(lod_mod_exit);
1054