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