Whamcloud - gitweb
0a77113a734b7480a89e5ad91652f833a4453005
[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  * Procss config log on LOD
215  * \param env environment info
216  * \param dev lod device
217  * \param lcfg config log
218  *
219  * Add osc config log,
220  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
221  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
222  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
223  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
224  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
225  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
226  *
227  * Add mdc config log
228  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
229  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
230  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
231  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
232  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
233  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
234  **/
235 static int lod_process_config(const struct lu_env *env,
236                               struct lu_device *dev,
237                               struct lustre_cfg *lcfg)
238 {
239         struct lod_device *lod = lu2lod_dev(dev);
240         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
241         char              *arg1;
242         int                rc = 0;
243         ENTRY;
244
245         switch(lcfg->lcfg_command) {
246         case LCFG_LOV_DEL_OBD:
247         case LCFG_LOV_ADD_INA:
248         case LCFG_LOV_ADD_OBD:
249         case LCFG_ADD_MDC: {
250                 __u32 index;
251                 __u32 mdt_index;
252                 int gen;
253                 /* lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
254                  * modify_mdc_tgts add  0:lustre-MDT0001
255                  *                    1:lustre-MDT0001-mdc0002
256                  *                    2:2  3:1*/
257                 arg1 = lustre_cfg_string(lcfg, 1);
258
259                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
260                         GOTO(out, rc = -EINVAL);
261                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
262                         GOTO(out, rc = -EINVAL);
263
264                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
265                         char *mdt;
266                         mdt = strstr(lustre_cfg_string(lcfg, 0), "-MDT");
267                         /* 1.8 configs don't have "-MDT0000" at the end */
268                         if (mdt == NULL) {
269                                 mdt_index = 0;
270                         } else {
271                                 long long_index;
272                                 rc = lodname2mdt_index(
273                                         lustre_cfg_string(lcfg, 0),
274                                         &long_index);
275                                 if (rc != 0)
276                                         GOTO(out, rc);
277                                 mdt_index = long_index;
278                         }
279                         rc = lod_add_device(env, lod, arg1, index, gen,
280                                             mdt_index, LUSTRE_OSC_NAME, 1);
281                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
282                         mdt_index = index;
283                         rc = lod_add_device(env, lod, arg1, index, gen,
284                                             mdt_index, LUSTRE_MDC_NAME, 1);
285                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
286                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
287                         mdt_index = 0;
288                         rc = lod_add_device(env, lod, arg1, index, gen,
289                                             mdt_index, LUSTRE_OSC_NAME, 0);
290                 } else {
291                         rc = lod_del_device(env, lod,
292                                             &lod->lod_ost_descs,
293                                             arg1, index, gen, true);
294                 }
295
296                 break;
297         }
298
299         case LCFG_PARAM: {
300                 struct lprocfs_static_vars  v = { 0 };
301                 struct obd_device         *obd = lod2obd(lod);
302
303                 lprocfs_lod_init_vars(&v);
304
305                 rc = class_process_proc_param(PARAM_LOV, v.obd_vars, lcfg, obd);
306                 if (rc > 0)
307                         rc = 0;
308                 GOTO(out, rc);
309         }
310         case LCFG_CLEANUP:
311         case LCFG_PRE_CLEANUP: {
312                 lu_dev_del_linkage(dev->ld_site, dev);
313                 lod_cleanup_desc_tgts(env, lod, &lod->lod_mdt_descs, lcfg);
314                 lod_cleanup_desc_tgts(env, lod, &lod->lod_ost_descs, lcfg);
315                 if (lcfg->lcfg_command == LCFG_PRE_CLEANUP)
316                         break;
317                 /*
318                  * do cleanup on underlying storage only when
319                  * all OSPs are cleaned up, as they use that OSD as well
320                  */
321                 next = &lod->lod_child->dd_lu_dev;
322                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
323                 if (rc)
324                         CERROR("%s: can't process %u: %d\n",
325                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
326
327                 rc = obd_disconnect(lod->lod_child_exp);
328                 if (rc)
329                         CERROR("error in disconnect from storage: %d\n", rc);
330                 break;
331         }
332         default:
333                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
334                       lcfg->lcfg_command);
335                rc = -EINVAL;
336                break;
337         }
338
339 out:
340         RETURN(rc);
341 }
342
343 static int lod_recovery_complete(const struct lu_env *env,
344                                  struct lu_device *dev)
345 {
346         struct lod_device   *lod = lu2lod_dev(dev);
347         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
348         int                  i, rc;
349         ENTRY;
350
351         LASSERT(lod->lod_recovery_completed == 0);
352         lod->lod_recovery_completed = 1;
353
354         rc = next->ld_ops->ldo_recovery_complete(env, next);
355
356         lod_getref(&lod->lod_ost_descs);
357         if (lod->lod_osts_size > 0) {
358                 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
359                         struct lod_tgt_desc *tgt;
360                         tgt = OST_TGT(lod, i);
361                         LASSERT(tgt && tgt->ltd_tgt);
362                         next = &tgt->ltd_ost->dd_lu_dev;
363                         rc = next->ld_ops->ldo_recovery_complete(env, next);
364                         if (rc)
365                                 CERROR("%s: can't complete recovery on #%d:"
366                                         "%d\n", lod2obd(lod)->obd_name, i, rc);
367                 }
368         }
369         lod_putref(lod, &lod->lod_ost_descs);
370         RETURN(rc);
371 }
372
373 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
374                        struct lu_device *cdev)
375 {
376         struct lod_device   *lod = lu2lod_dev(cdev);
377         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
378         int                  rc;
379         ENTRY;
380
381         rc = next->ld_ops->ldo_prepare(env, pdev, next);
382         if (rc != 0) {
383                 CERROR("%s: prepare bottom error: rc = %d\n",
384                        lod2obd(lod)->obd_name, rc);
385                 RETURN(rc);
386         }
387
388         lod->lod_initialized = 1;
389
390         RETURN(rc);
391 }
392
393 const struct lu_device_operations lod_lu_ops = {
394         .ldo_object_alloc       = lod_object_alloc,
395         .ldo_process_config     = lod_process_config,
396         .ldo_recovery_complete  = lod_recovery_complete,
397         .ldo_prepare            = lod_prepare,
398 };
399
400 static int lod_root_get(const struct lu_env *env,
401                         struct dt_device *dev, struct lu_fid *f)
402 {
403         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
404 }
405
406 static int lod_statfs(const struct lu_env *env,
407                       struct dt_device *dev, struct obd_statfs *sfs)
408 {
409         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
410 }
411
412 static struct thandle *lod_trans_create(const struct lu_env *env,
413                                         struct dt_device *dev)
414 {
415         struct thandle *th;
416
417         th = dt_trans_create(env, dt2lod_dev(dev)->lod_child);
418         if (IS_ERR(th))
419                 return th;
420
421         return th;
422 }
423
424 static int lod_trans_start(const struct lu_env *env, struct dt_device *dev,
425                            struct thandle *th)
426 {
427         struct lod_device *lod = dt2lod_dev((struct dt_device *) dev);
428         int rc = 0;
429
430         if (unlikely(th->th_update != NULL)) {
431                 struct thandle_update *tu = th->th_update;
432                 struct update_request *update;
433
434                 list_for_each_entry(update, &tu->tu_remote_update_list,
435                                     ur_list) {
436                         LASSERT(update->ur_dt != NULL);
437                         rc = dt_trans_start(env, update->ur_dt, th);
438                         if (rc != 0)
439                                 return rc;
440                 }
441         }
442         return dt_trans_start(env, lod->lod_child, th);
443 }
444
445 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
446                           struct thandle *th)
447 {
448         struct thandle_update   *tu = th->th_update;
449         struct update_request   *update;
450         struct update_request   *tmp;
451         int                     rc2 = 0;
452         int                     rc;
453         ENTRY;
454
455         rc = dt_trans_stop(env, th->th_dev, th);
456         if (likely(tu == NULL))
457                 RETURN(rc);
458
459         list_for_each_entry_safe(update, tmp, &tu->tu_remote_update_list,
460                                  ur_list) {
461                 /* update will be freed inside dt_trans_stop */
462                 rc2 = dt_trans_stop(env, update->ur_dt, th);
463                 if (unlikely(rc2 != 0 && rc == 0))
464                         rc = rc2;
465         }
466
467         RETURN(rc);
468 }
469
470 static void lod_conf_get(const struct lu_env *env,
471                          const struct dt_device *dev,
472                          struct dt_device_param *param)
473 {
474         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
475 }
476
477 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
478 {
479         struct lod_device   *lod = dt2lod_dev(dev);
480         struct lod_ost_desc *ost;
481         int                  rc = 0, i;
482         ENTRY;
483
484         lod_getref(&lod->lod_ost_descs);
485         lod_foreach_ost(lod, i) {
486                 ost = OST_TGT(lod, i);
487                 LASSERT(ost && ost->ltd_ost);
488                 rc = dt_sync(env, ost->ltd_ost);
489                 if (rc) {
490                         CERROR("%s: can't sync %u: %d\n",
491                                lod2obd(lod)->obd_name, i, rc);
492                         break;
493                 }
494         }
495         lod_putref(lod, &lod->lod_ost_descs);
496         if (rc == 0)
497                 rc = dt_sync(env, lod->lod_child);
498
499         RETURN(rc);
500 }
501
502 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
503 {
504         return dt_ro(env, dt2lod_dev(dev)->lod_child);
505 }
506
507 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
508 {
509         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
510 }
511
512 static int lod_init_capa_ctxt(const struct lu_env *env, struct dt_device *dev,
513                               int mode, unsigned long timeout,
514                               __u32 alg, struct lustre_capa_key *keys)
515 {
516         struct dt_device *next = dt2lod_dev(dev)->lod_child;
517         return dt_init_capa_ctxt(env, next, mode, timeout, alg, keys);
518 }
519
520 static const struct dt_device_operations lod_dt_ops = {
521         .dt_root_get         = lod_root_get,
522         .dt_statfs           = lod_statfs,
523         .dt_trans_create     = lod_trans_create,
524         .dt_trans_start      = lod_trans_start,
525         .dt_trans_stop       = lod_trans_stop,
526         .dt_conf_get         = lod_conf_get,
527         .dt_sync             = lod_sync,
528         .dt_ro               = lod_ro,
529         .dt_commit_async     = lod_commit_async,
530         .dt_init_capa_ctxt   = lod_init_capa_ctxt,
531 };
532
533 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
534                               struct lustre_cfg *cfg)
535 {
536         struct obd_connect_data *data = NULL;
537         struct obd_device       *obd;
538         char                    *nextdev = NULL, *p, *s;
539         int                      rc, len = 0;
540         ENTRY;
541
542         LASSERT(cfg);
543         LASSERT(lod->lod_child_exp == NULL);
544
545         /* compatibility hack: we still use old config logs
546          * which specify LOV, but we need to learn underlying
547          * OSD device, which is supposed to be:
548          *  <fsname>-MDTxxxx-osd
549          *
550          * 2.x MGS generates lines like the following:
551          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
552          * 1.8 MGS generates lines like the following:
553          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
554          *
555          * we use "-MDT" to differentiate 2.x from 1.8 */
556
557         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
558                 len = strlen(p) + 1;
559                 OBD_ALLOC(nextdev, len);
560                 if (nextdev == NULL)
561                         GOTO(out, rc = -ENOMEM);
562
563                 strcpy(nextdev, p);
564                 s = strstr(nextdev, "-mdtlov");
565                 if (unlikely(s == NULL)) {
566                         CERROR("unable to parse device name %s\n",
567                                lustre_cfg_string(cfg, 0));
568                         GOTO(out, rc = -EINVAL);
569                 }
570
571                 if (strstr(nextdev, "-MDT")) {
572                         /* 2.x config */
573                         strcpy(s, "-osd");
574                 } else {
575                         /* 1.8 config */
576                         strcpy(s, "-MDT0000-osd");
577                 }
578         } else {
579                 CERROR("unable to parse device name %s\n",
580                        lustre_cfg_string(cfg, 0));
581                 GOTO(out, rc = -EINVAL);
582         }
583
584         OBD_ALLOC_PTR(data);
585         if (data == NULL)
586                 GOTO(out, rc = -ENOMEM);
587
588         obd = class_name2obd(nextdev);
589         if (obd == NULL) {
590                 CERROR("can not locate next device: %s\n", nextdev);
591                 GOTO(out, rc = -ENOTCONN);
592         }
593
594         data->ocd_connect_flags = OBD_CONNECT_VERSION;
595         data->ocd_version = LUSTRE_VERSION_CODE;
596
597         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
598                          data, NULL);
599         if (rc) {
600                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
601                 GOTO(out, rc);
602         }
603
604         lod->lod_dt_dev.dd_lu_dev.ld_site =
605                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
606         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
607         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
608
609 out:
610         if (data)
611                 OBD_FREE_PTR(data);
612         if (nextdev)
613                 OBD_FREE(nextdev, len);
614         RETURN(rc);
615 }
616
617 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
618 {
619         mutex_init(&ltd->ltd_mutex);
620         init_rwsem(&ltd->ltd_rw_sem);
621
622         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
623          * added to the LOD, see lod_add_device() */
624         ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
625         if (ltd->ltd_tgt_bitmap == NULL)
626                 RETURN(-ENOMEM);
627
628         ltd->ltd_tgts_size  = 32;
629         ltd->ltd_tgtnr      = 0;
630
631         ltd->ltd_death_row = 0;
632         ltd->ltd_refcount  = 0;
633         return 0;
634 }
635
636 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
637                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
638 {
639         struct dt_device_param ddp;
640         struct obd_device     *obd;
641         int                    rc;
642         ENTRY;
643
644         obd = class_name2obd(lustre_cfg_string(cfg, 0));
645         if (obd == NULL) {
646                 CERROR("Cannot find obd with name %s\n",
647                        lustre_cfg_string(cfg, 0));
648                 RETURN(-ENODEV);
649         }
650
651         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
652         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
653         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
654         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
655
656         rc = lod_connect_to_osd(env, lod, cfg);
657         if (rc)
658                 RETURN(rc);
659
660         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
661         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
662
663         /* setup obd to be used with old lov code */
664         rc = lod_pools_init(lod, cfg);
665         if (rc)
666                 GOTO(out_disconnect, rc);
667
668         rc = lod_procfs_init(lod);
669         if (rc)
670                 GOTO(out_pools, rc);
671
672         spin_lock_init(&lod->lod_desc_lock);
673         spin_lock_init(&lod->lod_connects_lock);
674         lod_tgt_desc_init(&lod->lod_mdt_descs);
675         lod_tgt_desc_init(&lod->lod_ost_descs);
676
677         RETURN(0);
678
679 out_pools:
680         lod_pools_fini(lod);
681 out_disconnect:
682         obd_disconnect(lod->lod_child_exp);
683         RETURN(rc);
684 }
685
686 static struct lu_device *lod_device_free(const struct lu_env *env,
687                                          struct lu_device *lu)
688 {
689         struct lod_device *lod = lu2lod_dev(lu);
690         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
691         ENTRY;
692
693         LASSERT(cfs_atomic_read(&lu->ld_ref) == 0);
694         dt_device_fini(&lod->lod_dt_dev);
695         OBD_FREE_PTR(lod);
696         RETURN(next);
697 }
698
699 static struct lu_device *lod_device_alloc(const struct lu_env *env,
700                                           struct lu_device_type *type,
701                                           struct lustre_cfg *lcfg)
702 {
703         struct lod_device *lod;
704         struct lu_device  *lu_dev;
705
706         OBD_ALLOC_PTR(lod);
707         if (lod == NULL) {
708                 lu_dev = ERR_PTR(-ENOMEM);
709         } else {
710                 int rc;
711
712                 lu_dev = lod2lu_dev(lod);
713                 dt_device_init(&lod->lod_dt_dev, type);
714                 rc = lod_init0(env, lod, type, lcfg);
715                 if (rc != 0) {
716                         lod_device_free(env, lu_dev);
717                         lu_dev = ERR_PTR(rc);
718                 }
719         }
720
721         return lu_dev;
722 }
723
724 static struct lu_device *lod_device_fini(const struct lu_env *env,
725                                          struct lu_device *d)
726 {
727         struct lod_device *lod = lu2lod_dev(d);
728         int                rc;
729         ENTRY;
730
731         lod_pools_fini(lod);
732
733         lod_procfs_fini(lod);
734
735         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
736         if (rc)
737                 CERROR("%s:can not fini ost descs %d\n",
738                         lod2obd(lod)->obd_name, rc);
739
740         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
741         if (rc)
742                 CERROR("%s:can not fini mdt descs %d\n",
743                         lod2obd(lod)->obd_name, rc);
744
745         RETURN(NULL);
746 }
747
748 /*
749  * we use exports to track all LOD users
750  */
751 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
752                            struct obd_device *obd, struct obd_uuid *cluuid,
753                            struct obd_connect_data *data, void *localdata)
754 {
755         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
756         struct lustre_handle  conn;
757         int                   rc;
758         ENTRY;
759
760         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
761
762         rc = class_connect(&conn, obd, cluuid);
763         if (rc)
764                 RETURN(rc);
765
766         *exp = class_conn2export(&conn);
767
768         spin_lock(&lod->lod_connects_lock);
769         lod->lod_connects++;
770         /* at the moment we expect the only user */
771         LASSERT(lod->lod_connects == 1);
772         spin_unlock(&lod->lod_connects_lock);
773
774         RETURN(0);
775 }
776
777 /*
778  * once last export (we don't count self-export) disappeared
779  * lod can be released
780  */
781 static int lod_obd_disconnect(struct obd_export *exp)
782 {
783         struct obd_device *obd = exp->exp_obd;
784         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
785         int                rc, release = 0;
786         ENTRY;
787
788         /* Only disconnect the underlying layers on the final disconnect. */
789         spin_lock(&lod->lod_connects_lock);
790         lod->lod_connects--;
791         if (lod->lod_connects != 0) {
792                 /* why should there be more than 1 connect? */
793                 spin_unlock(&lod->lod_connects_lock);
794                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
795                        lod->lod_connects);
796                 goto out;
797         }
798         spin_unlock(&lod->lod_connects_lock);
799
800         /* the last user of lod has gone, let's release the device */
801         release = 1;
802
803 out:
804         rc = class_disconnect(exp); /* bz 9811 */
805
806         if (rc == 0 && release)
807                 class_manual_cleanup(obd);
808         RETURN(rc);
809 }
810
811 LU_KEY_INIT(lod, struct lod_thread_info);
812
813 static void lod_key_fini(const struct lu_context *ctx,
814                 struct lu_context_key *key, void *data)
815 {
816         struct lod_thread_info *info = data;
817         /* allocated in lod_get_lov_ea
818          * XXX: this is overload, a tread may have such store but used only
819          * once. Probably better would be pool of such stores per LOD.
820          */
821         if (info->lti_ea_store) {
822                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
823                 info->lti_ea_store = NULL;
824                 info->lti_ea_store_size = 0;
825         }
826         OBD_FREE_PTR(info);
827 }
828
829 /* context key: lod_thread_key */
830 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
831
832 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
833
834 static struct lu_device_type_operations lod_device_type_ops = {
835         .ldto_init           = lod_type_init,
836         .ldto_fini           = lod_type_fini,
837
838         .ldto_start          = lod_type_start,
839         .ldto_stop           = lod_type_stop,
840
841         .ldto_device_alloc   = lod_device_alloc,
842         .ldto_device_free    = lod_device_free,
843
844         .ldto_device_fini    = lod_device_fini
845 };
846
847 static struct lu_device_type lod_device_type = {
848         .ldt_tags     = LU_DEVICE_DT,
849         .ldt_name     = LUSTRE_LOD_NAME,
850         .ldt_ops      = &lod_device_type_ops,
851         .ldt_ctx_tags = LCT_MD_THREAD,
852 };
853
854 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
855                             __u32 keylen, void *key, __u32 *vallen, void *val,
856                             struct lov_stripe_md *lsm)
857 {
858         int rc = -EINVAL;
859
860         if (KEY_IS(KEY_OSP_CONNECTED)) {
861                 struct obd_device       *obd = exp->exp_obd;
862                 struct lod_device       *d;
863                 struct lod_ost_desc     *ost;
864                 int                     i, rc = 1;
865
866                 if (!obd->obd_set_up || obd->obd_stopping)
867                         RETURN(-EAGAIN);
868
869                 d = lu2lod_dev(obd->obd_lu_dev);
870                 lod_getref(&d->lod_ost_descs);
871                 lod_foreach_ost(d, i) {
872                         ost = OST_TGT(d, i);
873                         LASSERT(ost && ost->ltd_ost);
874
875                         rc = obd_get_info(env, ost->ltd_exp, keylen, key,
876                                           vallen, val, lsm);
877                         /* one healthy device is enough */
878                         if (rc == 0)
879                                 break;
880                 }
881                 lod_putref(d, &d->lod_ost_descs);
882                 RETURN(rc);
883         }
884
885         RETURN(rc);
886 }
887
888 static struct obd_ops lod_obd_device_ops = {
889         .o_owner        = THIS_MODULE,
890         .o_connect      = lod_obd_connect,
891         .o_disconnect   = lod_obd_disconnect,
892         .o_get_info     = lod_obd_get_info,
893         .o_pool_new     = lod_pool_new,
894         .o_pool_rem     = lod_pool_remove,
895         .o_pool_add     = lod_pool_add,
896         .o_pool_del     = lod_pool_del,
897 };
898
899 static int __init lod_mod_init(void)
900 {
901         struct lprocfs_static_vars  lvars = { 0 };
902         cfs_proc_dir_entry_t       *lov_proc_dir;
903         int                         rc;
904
905         rc = lu_kmem_init(lod_caches);
906         if (rc)
907                 return rc;
908
909         lprocfs_lod_init_vars(&lvars);
910
911         rc = class_register_type(&lod_obd_device_ops, NULL, NULL,
912 #ifndef HAVE_ONLY_PROCFS_SEQ
913                                 lvars.module_vars,
914 #endif
915                                 LUSTRE_LOD_NAME, &lod_device_type);
916         if (rc) {
917                 lu_kmem_fini(lod_caches);
918                 return rc;
919         }
920
921         /* create "lov" entry in procfs for compatibility purposes */
922         lov_proc_dir = lprocfs_srch(proc_lustre_root, "lov");
923         if (lov_proc_dir == NULL) {
924                 lov_proc_dir = lprocfs_register("lov", proc_lustre_root,
925                                                 NULL, NULL);
926                 if (IS_ERR(lov_proc_dir))
927                         CERROR("lod: can't create compat entry \"lov\": %d\n",
928                                (int)PTR_ERR(lov_proc_dir));
929         }
930
931         return rc;
932 }
933
934 static void __exit lod_mod_exit(void)
935 {
936
937         lprocfs_try_remove_proc_entry("lov", proc_lustre_root);
938
939         class_unregister_type(LUSTRE_LOD_NAME);
940         lu_kmem_fini(lod_caches);
941 }
942
943 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
944 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
945 MODULE_LICENSE("GPL");
946
947 module_init(lod_mod_init);
948 module_exit(lod_mod_exit);
949