Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros v2
[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, int *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                                 rc = lodname2mdt_index(
364                                         lustre_cfg_string(lcfg, 0), &mdt_index);
365                                 if (rc != 0)
366                                         GOTO(out, rc);
367                         }
368                         rc = lod_add_device(env, lod, arg1, index, gen,
369                                             mdt_index, LUSTRE_OSC_NAME, 1);
370                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
371                         mdt_index = index;
372                         rc = lod_add_device(env, lod, arg1, index, gen,
373                                             mdt_index, LUSTRE_MDC_NAME, 1);
374                         if (rc == 0)
375                                 rc = lod_seq_init_cli(env, lod, arg1,
376                                                       mdt_index);
377                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
378                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
379                         mdt_index = 0;
380                         rc = lod_add_device(env, lod, arg1, index, gen,
381                                             mdt_index, LUSTRE_OSC_NAME, 0);
382                 } else {
383                         rc = lod_del_device(env, lod,
384                                             &lod->lod_ost_descs,
385                                             arg1, index, gen);
386                 }
387
388                 break;
389         }
390
391         case LCFG_PARAM: {
392                 struct lprocfs_static_vars  v = { 0 };
393                 struct obd_device         *obd = lod2obd(lod);
394
395                 lprocfs_lod_init_vars(&v);
396
397                 rc = class_process_proc_param(PARAM_LOV, v.obd_vars, lcfg, obd);
398                 if (rc > 0)
399                         rc = 0;
400                 GOTO(out, rc);
401         }
402         case LCFG_CLEANUP:
403         case LCFG_PRE_CLEANUP: {
404                 lu_dev_del_linkage(dev->ld_site, dev);
405                 lod_cleanup_desc_tgts(env, lod, &lod->lod_mdt_descs, lcfg);
406                 lod_cleanup_desc_tgts(env, lod, &lod->lod_ost_descs, lcfg);
407
408                 lod_seq_fini_cli(lod);
409
410                 if (lcfg->lcfg_command == LCFG_PRE_CLEANUP)
411                         break;
412                 /*
413                  * do cleanup on underlying storage only when
414                  * all OSPs are cleaned up, as they use that OSD as well
415                  */
416                 next = &lod->lod_child->dd_lu_dev;
417                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
418                 if (rc)
419                         CERROR("%s: can't process %u: %d\n",
420                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
421
422                 rc = obd_disconnect(lod->lod_child_exp);
423                 if (rc)
424                         CERROR("error in disconnect from storage: %d\n", rc);
425                 break;
426         }
427         default:
428                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
429                       lcfg->lcfg_command);
430                rc = -EINVAL;
431                break;
432         }
433
434 out:
435         RETURN(rc);
436 }
437
438 static int lod_recovery_complete(const struct lu_env *env,
439                                  struct lu_device *dev)
440 {
441         struct lod_device   *lod = lu2lod_dev(dev);
442         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
443         int                  i, rc;
444         ENTRY;
445
446         LASSERT(lod->lod_recovery_completed == 0);
447         lod->lod_recovery_completed = 1;
448
449         rc = next->ld_ops->ldo_recovery_complete(env, next);
450
451         lod_getref(&lod->lod_ost_descs);
452         if (lod->lod_osts_size > 0) {
453                 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
454                         struct lod_tgt_desc *tgt;
455                         tgt = OST_TGT(lod, i);
456                         LASSERT(tgt && tgt->ltd_tgt);
457                         next = &tgt->ltd_ost->dd_lu_dev;
458                         rc = next->ld_ops->ldo_recovery_complete(env, next);
459                         if (rc)
460                                 CERROR("%s: can't complete recovery on #%d:"
461                                         "%d\n", lod2obd(lod)->obd_name, i, rc);
462                 }
463         }
464         lod_putref(lod, &lod->lod_ost_descs);
465         RETURN(rc);
466 }
467
468 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
469                        struct lu_device *cdev)
470 {
471         struct lod_device   *lod = lu2lod_dev(cdev);
472         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
473         int                  rc;
474         ENTRY;
475
476         rc = next->ld_ops->ldo_prepare(env, pdev, next);
477         if (rc != 0) {
478                 CERROR("%s: prepare bottom error: rc = %d\n",
479                        lod2obd(lod)->obd_name, rc);
480                 RETURN(rc);
481         }
482
483         lod->lod_initialized = 1;
484
485         RETURN(rc);
486 }
487
488 const struct lu_device_operations lod_lu_ops = {
489         .ldo_object_alloc       = lod_object_alloc,
490         .ldo_process_config     = lod_process_config,
491         .ldo_recovery_complete  = lod_recovery_complete,
492         .ldo_prepare            = lod_prepare,
493 };
494
495 static int lod_root_get(const struct lu_env *env,
496                         struct dt_device *dev, struct lu_fid *f)
497 {
498         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
499 }
500
501 static int lod_statfs(const struct lu_env *env,
502                       struct dt_device *dev, struct obd_statfs *sfs)
503 {
504         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
505 }
506
507 static struct thandle *lod_trans_create(const struct lu_env *env,
508                                         struct dt_device *dev)
509 {
510         struct thandle *th;
511
512         th = dt_trans_create(env, dt2lod_dev(dev)->lod_child);
513         if (IS_ERR(th))
514                 return th;
515
516         CFS_INIT_LIST_HEAD(&th->th_remote_update_list);
517         return th;
518 }
519
520 static int lod_remote_sync(const struct lu_env *env, struct dt_device *dev,
521                            struct thandle *th)
522 {
523         struct update_request *update;
524         int    rc = 0;
525         ENTRY;
526
527         if (cfs_list_empty(&th->th_remote_update_list))
528                 RETURN(0);
529
530         cfs_list_for_each_entry(update, &th->th_remote_update_list,
531                                 ur_list) {
532                 /* In DNE phase I, there should be only one OSP
533                  * here, so we will do send/receive one by one,
534                  * instead of sending them parallel, will fix this
535                  * in Phase II */
536                 th->th_current_request = update;
537                 rc = dt_trans_start(env, update->ur_dt, th);
538                 if (rc != 0) {
539                         /* FIXME how to revert the partial results
540                          * once error happened? Resolved by 2 Phase commit */
541                         update->ur_rc = rc;
542                         break;
543                 }
544         }
545
546         RETURN(rc);
547 }
548
549 static int lod_trans_start(const struct lu_env *env, struct dt_device *dev,
550                            struct thandle *th)
551 {
552         struct lod_device *lod = dt2lod_dev((struct dt_device *) dev);
553         int rc;
554
555         rc = lod_remote_sync(env, dev, th);
556         if (rc)
557                 return rc;
558
559         return dt_trans_start(env, lod->lod_child, th);
560 }
561
562 static int lod_trans_stop(const struct lu_env *env, struct thandle *th)
563 {
564         struct update_request *update;
565         struct update_request *tmp;
566         int rc = 0;
567         int rc2 = 0;
568
569         cfs_list_for_each_entry_safe(update, tmp,
570                                      &th->th_remote_update_list,
571                                      ur_list) {
572                 th->th_current_request = update;
573                 rc2 = dt_trans_stop(env, update->ur_dt, th);
574                 if (unlikely(rc2 != 0 && rc == 0))
575                         rc = rc2;
576         }
577
578         rc2 = dt_trans_stop(env, th->th_dev, th);
579
580         return rc2 != 0 ? rc2 : rc;
581 }
582
583 static void lod_conf_get(const struct lu_env *env,
584                          const struct dt_device *dev,
585                          struct dt_device_param *param)
586 {
587         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
588 }
589
590 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
591 {
592         struct lod_device   *lod = dt2lod_dev(dev);
593         struct lod_ost_desc *ost;
594         int                  rc = 0, i;
595         ENTRY;
596
597         lod_getref(&lod->lod_ost_descs);
598         lod_foreach_ost(lod, i) {
599                 ost = OST_TGT(lod, i);
600                 LASSERT(ost && ost->ltd_ost);
601                 rc = dt_sync(env, ost->ltd_ost);
602                 if (rc) {
603                         CERROR("%s: can't sync %u: %d\n",
604                                lod2obd(lod)->obd_name, i, rc);
605                         break;
606                 }
607         }
608         lod_putref(lod, &lod->lod_ost_descs);
609         if (rc == 0)
610                 rc = dt_sync(env, lod->lod_child);
611
612         RETURN(rc);
613 }
614
615 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
616 {
617         return dt_ro(env, dt2lod_dev(dev)->lod_child);
618 }
619
620 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
621 {
622         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
623 }
624
625 static int lod_init_capa_ctxt(const struct lu_env *env, struct dt_device *dev,
626                               int mode, unsigned long timeout,
627                               __u32 alg, struct lustre_capa_key *keys)
628 {
629         struct dt_device *next = dt2lod_dev(dev)->lod_child;
630         return dt_init_capa_ctxt(env, next, mode, timeout, alg, keys);
631 }
632
633 static const struct dt_device_operations lod_dt_ops = {
634         .dt_root_get         = lod_root_get,
635         .dt_statfs           = lod_statfs,
636         .dt_trans_create     = lod_trans_create,
637         .dt_trans_start      = lod_trans_start,
638         .dt_trans_stop       = lod_trans_stop,
639         .dt_conf_get         = lod_conf_get,
640         .dt_sync             = lod_sync,
641         .dt_ro               = lod_ro,
642         .dt_commit_async     = lod_commit_async,
643         .dt_init_capa_ctxt   = lod_init_capa_ctxt,
644 };
645
646 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
647                               struct lustre_cfg *cfg)
648 {
649         struct obd_connect_data *data = NULL;
650         struct obd_device       *obd;
651         char                    *nextdev = NULL, *p, *s;
652         int                      rc, len = 0;
653         ENTRY;
654
655         LASSERT(cfg);
656         LASSERT(lod->lod_child_exp == NULL);
657
658         /* compatibility hack: we still use old config logs
659          * which specify LOV, but we need to learn underlying
660          * OSD device, which is supposed to be:
661          *  <fsname>-MDTxxxx-osd
662          *
663          * 2.x MGS generates lines like the following:
664          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
665          * 1.8 MGS generates lines like the following:
666          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
667          *
668          * we use "-MDT" to differentiate 2.x from 1.8 */
669
670         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
671                 len = strlen(p) + 1;
672                 OBD_ALLOC(nextdev, len);
673                 if (nextdev == NULL)
674                         GOTO(out, rc = -ENOMEM);
675
676                 strcpy(nextdev, p);
677                 s = strstr(nextdev, "-mdtlov");
678                 if (unlikely(s == NULL)) {
679                         CERROR("unable to parse device name %s\n",
680                                lustre_cfg_string(cfg, 0));
681                         GOTO(out, rc = -EINVAL);
682                 }
683
684                 if (strstr(nextdev, "-MDT")) {
685                         /* 2.x config */
686                         strcpy(s, "-osd");
687                 } else {
688                         /* 1.8 config */
689                         strcpy(s, "-MDT0000-osd");
690                 }
691         } else {
692                 CERROR("unable to parse device name %s\n",
693                        lustre_cfg_string(cfg, 0));
694                 GOTO(out, rc = -EINVAL);
695         }
696
697         OBD_ALLOC_PTR(data);
698         if (data == NULL)
699                 GOTO(out, rc = -ENOMEM);
700
701         obd = class_name2obd(nextdev);
702         if (obd == NULL) {
703                 CERROR("can not locate next device: %s\n", nextdev);
704                 GOTO(out, rc = -ENOTCONN);
705         }
706
707         data->ocd_connect_flags = OBD_CONNECT_VERSION;
708         data->ocd_version = LUSTRE_VERSION_CODE;
709
710         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
711                          data, NULL);
712         if (rc) {
713                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
714                 GOTO(out, rc);
715         }
716
717         lod->lod_dt_dev.dd_lu_dev.ld_site =
718                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
719         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
720         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
721
722 out:
723         if (data)
724                 OBD_FREE_PTR(data);
725         if (nextdev)
726                 OBD_FREE(nextdev, len);
727         RETURN(rc);
728 }
729
730 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
731 {
732         mutex_init(&ltd->ltd_mutex);
733         init_rwsem(&ltd->ltd_rw_sem);
734
735         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
736          * added to the LOD, see lod_add_device() */
737         ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
738         if (ltd->ltd_tgt_bitmap == NULL)
739                 RETURN(-ENOMEM);
740
741         ltd->ltd_tgts_size  = 32;
742         ltd->ltd_tgtnr      = 0;
743
744         ltd->ltd_death_row = 0;
745         ltd->ltd_refcount  = 0;
746         return 0;
747 }
748
749 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
750                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
751 {
752         struct dt_device_param ddp;
753         struct obd_device     *obd;
754         int                    rc;
755         ENTRY;
756
757         obd = class_name2obd(lustre_cfg_string(cfg, 0));
758         if (obd == NULL) {
759                 CERROR("Cannot find obd with name %s\n",
760                        lustre_cfg_string(cfg, 0));
761                 RETURN(-ENODEV);
762         }
763
764         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
765         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
766         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
767         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
768
769         rc = lod_connect_to_osd(env, lod, cfg);
770         if (rc)
771                 RETURN(rc);
772
773         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
774         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
775
776         /* setup obd to be used with old lov code */
777         rc = lod_pools_init(lod, cfg);
778         if (rc)
779                 GOTO(out_disconnect, rc);
780
781         rc = lod_procfs_init(lod);
782         if (rc)
783                 GOTO(out_pools, rc);
784
785         spin_lock_init(&lod->lod_desc_lock);
786         spin_lock_init(&lod->lod_connects_lock);
787         lod_tgt_desc_init(&lod->lod_mdt_descs);
788         lod_tgt_desc_init(&lod->lod_ost_descs);
789
790         RETURN(0);
791
792 out_pools:
793         lod_pools_fini(lod);
794 out_disconnect:
795         obd_disconnect(lod->lod_child_exp);
796         RETURN(rc);
797 }
798
799 static struct lu_device *lod_device_free(const struct lu_env *env,
800                                          struct lu_device *lu)
801 {
802         struct lod_device *lod = lu2lod_dev(lu);
803         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
804         ENTRY;
805
806         LASSERT(cfs_atomic_read(&lu->ld_ref) == 0);
807         dt_device_fini(&lod->lod_dt_dev);
808         OBD_FREE_PTR(lod);
809         RETURN(next);
810 }
811
812 static struct lu_device *lod_device_alloc(const struct lu_env *env,
813                                           struct lu_device_type *type,
814                                           struct lustre_cfg *lcfg)
815 {
816         struct lod_device *lod;
817         struct lu_device  *lu_dev;
818
819         OBD_ALLOC_PTR(lod);
820         if (lod == NULL) {
821                 lu_dev = ERR_PTR(-ENOMEM);
822         } else {
823                 int rc;
824
825                 lu_dev = lod2lu_dev(lod);
826                 dt_device_init(&lod->lod_dt_dev, type);
827                 rc = lod_init0(env, lod, type, lcfg);
828                 if (rc != 0) {
829                         lod_device_free(env, lu_dev);
830                         lu_dev = ERR_PTR(rc);
831                 }
832         }
833
834         return lu_dev;
835 }
836
837 static struct lu_device *lod_device_fini(const struct lu_env *env,
838                                          struct lu_device *d)
839 {
840         struct lod_device *lod = lu2lod_dev(d);
841         int                rc;
842         ENTRY;
843
844         lod_pools_fini(lod);
845
846         lod_procfs_fini(lod);
847
848         rc = lod_fini_tgt(lod, &lod->lod_ost_descs);
849         if (rc)
850                 CERROR("%s:can not fini ost descs %d\n",
851                         lod2obd(lod)->obd_name, rc);
852
853         rc = lod_fini_tgt(lod, &lod->lod_mdt_descs);
854         if (rc)
855                 CERROR("%s:can not fini mdt descs %d\n",
856                         lod2obd(lod)->obd_name, rc);
857
858         RETURN(NULL);
859 }
860
861 /*
862  * we use exports to track all LOD users
863  */
864 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
865                            struct obd_device *obd, struct obd_uuid *cluuid,
866                            struct obd_connect_data *data, void *localdata)
867 {
868         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
869         struct lustre_handle  conn;
870         int                   rc;
871         ENTRY;
872
873         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
874
875         rc = class_connect(&conn, obd, cluuid);
876         if (rc)
877                 RETURN(rc);
878
879         *exp = class_conn2export(&conn);
880
881         spin_lock(&lod->lod_connects_lock);
882         lod->lod_connects++;
883         /* at the moment we expect the only user */
884         LASSERT(lod->lod_connects == 1);
885         spin_unlock(&lod->lod_connects_lock);
886
887         RETURN(0);
888 }
889
890 /*
891  * once last export (we don't count self-export) disappeared
892  * lod can be released
893  */
894 static int lod_obd_disconnect(struct obd_export *exp)
895 {
896         struct obd_device *obd = exp->exp_obd;
897         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
898         int                rc, release = 0;
899         ENTRY;
900
901         /* Only disconnect the underlying layers on the final disconnect. */
902         spin_lock(&lod->lod_connects_lock);
903         lod->lod_connects--;
904         if (lod->lod_connects != 0) {
905                 /* why should there be more than 1 connect? */
906                 spin_unlock(&lod->lod_connects_lock);
907                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
908                        lod->lod_connects);
909                 goto out;
910         }
911         spin_unlock(&lod->lod_connects_lock);
912
913         /* the last user of lod has gone, let's release the device */
914         release = 1;
915
916 out:
917         rc = class_disconnect(exp); /* bz 9811 */
918
919         if (rc == 0 && release)
920                 class_manual_cleanup(obd);
921         RETURN(rc);
922 }
923
924 LU_KEY_INIT(lod, struct lod_thread_info);
925
926 static void lod_key_fini(const struct lu_context *ctx,
927                 struct lu_context_key *key, void *data)
928 {
929         struct lod_thread_info *info = data;
930         /* allocated in lod_get_lov_ea
931          * XXX: this is overload, a tread may have such store but used only
932          * once. Probably better would be pool of such stores per LOD.
933          */
934         if (info->lti_ea_store) {
935                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
936                 info->lti_ea_store = NULL;
937                 info->lti_ea_store_size = 0;
938         }
939         OBD_FREE_PTR(info);
940 }
941
942 /* context key: lod_thread_key */
943 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
944
945 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
946
947 static struct lu_device_type_operations lod_device_type_ops = {
948         .ldto_init           = lod_type_init,
949         .ldto_fini           = lod_type_fini,
950
951         .ldto_start          = lod_type_start,
952         .ldto_stop           = lod_type_stop,
953
954         .ldto_device_alloc   = lod_device_alloc,
955         .ldto_device_free    = lod_device_free,
956
957         .ldto_device_fini    = lod_device_fini
958 };
959
960 static struct lu_device_type lod_device_type = {
961         .ldt_tags     = LU_DEVICE_DT,
962         .ldt_name     = LUSTRE_LOD_NAME,
963         .ldt_ops      = &lod_device_type_ops,
964         .ldt_ctx_tags = LCT_MD_THREAD,
965 };
966
967 static int lod_obd_health_check(const struct lu_env *env,
968                 struct obd_device *obd)
969 {
970         struct lod_device   *d = lu2lod_dev(obd->obd_lu_dev);
971         struct lod_ost_desc *ost;
972         int                  i, rc = 1;
973         ENTRY;
974
975         LASSERT(d);
976         lod_getref(&d->lod_ost_descs);
977         lod_foreach_ost(d, i) {
978                 ost = OST_TGT(d, i);
979                 LASSERT(ost && ost->ltd_ost);
980                 rc = obd_health_check(env, ost->ltd_exp->exp_obd);
981                 /* one healthy device is enough */
982                 if (rc == 0)
983                         break;
984         }
985         lod_putref(d, &d->lod_ost_descs);
986         RETURN(rc);
987 }
988
989 static struct obd_ops lod_obd_device_ops = {
990         .o_owner        = THIS_MODULE,
991         .o_connect      = lod_obd_connect,
992         .o_disconnect   = lod_obd_disconnect,
993         .o_health_check = lod_obd_health_check,
994         .o_pool_new     = lod_pool_new,
995         .o_pool_rem     = lod_pool_remove,
996         .o_pool_add     = lod_pool_add,
997         .o_pool_del     = lod_pool_del,
998 };
999
1000 static int __init lod_mod_init(void)
1001 {
1002         struct lprocfs_static_vars  lvars = { 0 };
1003         cfs_proc_dir_entry_t       *lov_proc_dir;
1004         int                         rc;
1005
1006         rc = lu_kmem_init(lod_caches);
1007         if (rc)
1008                 return rc;
1009
1010         lprocfs_lod_init_vars(&lvars);
1011
1012         rc = class_register_type(&lod_obd_device_ops, NULL, lvars.module_vars,
1013                                  LUSTRE_LOD_NAME, &lod_device_type);
1014         if (rc) {
1015                 lu_kmem_fini(lod_caches);
1016                 return rc;
1017         }
1018
1019         /* create "lov" entry in procfs for compatibility purposes */
1020         lov_proc_dir = lprocfs_srch(proc_lustre_root, "lov");
1021         if (lov_proc_dir == NULL) {
1022                 lov_proc_dir = lprocfs_register("lov", proc_lustre_root,
1023                                                 NULL, NULL);
1024                 if (IS_ERR(lov_proc_dir))
1025                         CERROR("lod: can't create compat entry \"lov\": %d\n",
1026                                (int)PTR_ERR(lov_proc_dir));
1027         }
1028
1029         return rc;
1030 }
1031
1032 static void __exit lod_mod_exit(void)
1033 {
1034
1035         lprocfs_try_remove_proc_entry("lov", proc_lustre_root);
1036
1037         class_unregister_type(LUSTRE_LOD_NAME);
1038         lu_kmem_fini(lod_caches);
1039 }
1040
1041 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
1042 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
1043 MODULE_LICENSE("GPL");
1044
1045 module_init(lod_mod_init);
1046 module_exit(lod_mod_exit);
1047