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