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