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