Whamcloud - gitweb
LU-3536 lod: write updates to update log
[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, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/lod/lod_dev.c
33  *
34  * Lustre Logical Object Device
35  *
36  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
37  * Author: Mikhail Pershin <mike.pershin@intel.com>
38  */
39 /**
40  * The Logical Object Device (LOD) layer manages access to striped
41  * objects (both regular files and directories). It implements the DT
42  * device and object APIs and is responsible for creating, storing,
43  * and loading striping information as an extended attribute of the
44  * underlying OSD object. LOD is the server side analog of the LOV and
45  * LMV layers on the client side.
46  *
47  * Metadata LU object stack (layers of the same compound LU object,
48  * all have the same FID):
49  *
50  *        MDT
51  *         |      MD API
52  *        MDD
53  *         |      DT API
54  *        LOD
55  *       /   \    DT API
56  *     OSD   OSP
57  *
58  * During LOD object initialization the localness or remoteness of the
59  * object FID dictates the choice between OSD and OSP.
60  *
61  * An LOD object (file or directory) with N stripes (each has a
62  * different FID):
63  *
64  *          LOD
65  *           |
66  *   +---+---+---+...+
67  *   |   |   |   |   |
68  *   S0  S1  S2  S3  S(N-1)  OS[DP] objects, seen as DT objects by LOD
69  *
70  * When upper layers must access an object's stripes (which are
71  * themselves OST or MDT LU objects) LOD finds these objects by their
72  * FIDs and stores them as an array of DT object pointers on the
73  * object. Declarations and operations on LOD objects are received by
74  * LOD (as DT object operations) and performed on the underlying
75  * OS[DP] object and (as needed) on the stripes. From the perspective
76  * of LOD, a stripe-less file (created by mknod() or open with
77  * O_LOV_DELAY_CREATE) is an object which does not yet have stripes,
78  * while a non-striped directory (created by mkdir()) is an object
79  * which will never have stripes.
80  *
81  * The LOD layer also implements a small subset of the OBD device API
82  * to support MDT stack initialization and finalization (an MDD device
83  * connects and disconnects itself to and from the underlying LOD
84  * device), and pool management. In turn LOD uses the OBD device API
85  * to connect it self to the underlying OSD, and to connect itself to
86  * OSP devices representing the MDTs and OSTs that bear the stripes of
87  * its objects.
88  */
89
90 #define DEBUG_SUBSYSTEM S_MDS
91
92 #include <linux/kthread.h>
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 #include <lustre_log.h>
99
100 #include "lod_internal.h"
101
102 static const char lod_update_log_name[] = "update_log";
103 static const char lod_update_log_dir_name[] = "update_log_dir";
104
105 /*
106  * Lookup target by FID.
107  *
108  * Lookup MDT/OST target index by FID. Type of the target can be
109  * specific or any.
110  *
111  * \param[in] env               LU environment provided by the caller
112  * \param[in] lod               lod device
113  * \param[in] fid               FID
114  * \param[out] tgt              result target index
115  * \param[in] type              expected type of the target:
116  *                              LU_SEQ_RANGE_{MDT,OST,ANY}
117  *
118  * \retval 0                    on success
119  * \retval negative             negated errno on error
120  **/
121 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
122                    const struct lu_fid *fid, __u32 *tgt, int *type)
123 {
124         struct lu_seq_range     range = { 0 };
125         struct lu_server_fld    *server_fld;
126         int rc;
127         ENTRY;
128
129         if (!fid_is_sane(fid)) {
130                 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
131                        PFID(fid));
132                 RETURN(-EIO);
133         }
134
135         if (fid_is_idif(fid)) {
136                 *tgt = fid_idif_ost_idx(fid);
137                 *type = LU_SEQ_RANGE_OST;
138                 RETURN(0);
139         }
140
141         if (fid_is_update_log(fid) || fid_is_update_log_dir(fid)) {
142                 *tgt = fid_oid(fid);
143                 *type = LU_SEQ_RANGE_MDT;
144                 RETURN(0);
145         }
146
147         if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
148                 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
149
150                 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
151                 *type = LU_SEQ_RANGE_MDT;
152                 RETURN(0);
153         }
154
155         server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
156         fld_range_set_type(&range, *type);
157         rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
158         if (rc != 0)
159                 RETURN(rc);
160
161         *tgt = range.lsr_index;
162         *type = range.lsr_flags;
163
164         CDEBUG(D_INFO, "%s: got tgt %x for sequence: "LPX64"\n",
165                lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
166
167         RETURN(0);
168 }
169
170 /* Slab for OSD object allocation */
171 struct kmem_cache *lod_object_kmem;
172
173 static struct lu_kmem_descr lod_caches[] = {
174         {
175                 .ckd_cache = &lod_object_kmem,
176                 .ckd_name  = "lod_obj",
177                 .ckd_size  = sizeof(struct lod_object)
178         },
179         {
180                 .ckd_cache = NULL
181         }
182 };
183
184 static struct lu_device *lod_device_fini(const struct lu_env *env,
185                                          struct lu_device *d);
186
187 /**
188  * Implementation of lu_device_operations::ldo_object_alloc() for LOD
189  *
190  * Allocates and initializes LOD's slice in the given object.
191  *
192  * see include/lu_object.h for the details.
193  */
194 static struct lu_object *lod_object_alloc(const struct lu_env *env,
195                                           const struct lu_object_header *hdr,
196                                           struct lu_device *dev)
197 {
198         struct lod_object       *lod_obj;
199         struct lu_object        *lu_obj;
200         ENTRY;
201
202         OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
203         if (lod_obj == NULL)
204                 RETURN(ERR_PTR(-ENOMEM));
205
206         lu_obj = lod2lu_obj(lod_obj);
207         dt_object_init(&lod_obj->ldo_obj, NULL, dev);
208         lod_obj->ldo_obj.do_ops = &lod_obj_ops;
209         lu_obj->lo_ops = &lod_lu_obj_ops;
210
211         RETURN(lu_obj);
212 }
213
214 /**
215  * Cleanup table of target's descriptors.
216  *
217  * The function goes through all the targets in the given table
218  * and apply given configuration command on to the targets.
219  * Used to cleanup the targets at unmount.
220  *
221  * \param[in] env               LU environment provided by the caller
222  * \param[in] lod               lod device
223  * \param[in] ltd               target's table to go through
224  * \param[in] lcfg              configuration command to apply
225  *
226  * \retval 0                    on success
227  * \retval negative             negated errno on error
228  **/
229 static int lod_sub_process_config(const struct lu_env *env,
230                                  struct lod_device *lod,
231                                  struct lod_tgt_descs *ltd,
232                                  struct lustre_cfg *lcfg)
233 {
234         struct lu_device  *next;
235         int rc = 0;
236         unsigned int i;
237
238         lod_getref(ltd);
239         if (ltd->ltd_tgts_size <= 0) {
240                 lod_putref(lod, ltd);
241                 return 0;
242         }
243         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
244                 struct lod_tgt_desc *tgt;
245                 int rc1;
246
247                 tgt = LTD_TGT(ltd, i);
248                 LASSERT(tgt && tgt->ltd_tgt);
249                 next = &tgt->ltd_tgt->dd_lu_dev;
250                 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
251                 if (rc1) {
252                         CERROR("%s: error cleaning up LOD index %u: cmd %#x"
253                                ": rc = %d\n", lod2obd(lod)->obd_name, i,
254                                lcfg->lcfg_command, rc1);
255                         rc = rc1;
256                 }
257         }
258         lod_putref(lod, ltd);
259         return rc;
260 }
261
262 struct lod_recovery_data {
263         struct lod_device       *lrd_lod;
264         struct lod_tgt_desc     *lrd_ltd;
265         struct ptlrpc_thread    *lrd_thread;
266         __u32                   lrd_idx;
267 };
268
269 /**
270  * recovery thread for update log
271  *
272  * Start recovery thread and prepare the sub llog, then it will retrieve
273  * the update records from the correpondent MDT and do recovery.
274  *
275  * \param[in] arg       pointer to the recovery data
276  *
277  * \retval              0 if recovery succeeds
278  * \retval              negative errno if recovery failed.
279  */
280 static int lod_sub_recovery_thread(void *arg)
281 {
282         struct lod_recovery_data        *lrd = arg;
283         struct lod_device               *lod = lrd->lrd_lod;
284         struct dt_device                *dt;
285         struct ptlrpc_thread            *thread = lrd->lrd_thread;
286         struct lu_env                   env;
287         int                             rc;
288         ENTRY;
289
290         thread->t_flags = SVC_RUNNING;
291         wake_up(&thread->t_ctl_waitq);
292
293         rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
294         if (rc != 0) {
295                 OBD_FREE_PTR(lrd);
296                 CERROR("%s: can't initialize env: rc = %d\n",
297                        lod2obd(lod)->obd_name, rc);
298                 RETURN(rc);
299         }
300
301         if (lrd->lrd_ltd == NULL)
302                 dt = lod->lod_child;
303         else
304                 dt = lrd->lrd_ltd->ltd_tgt;
305
306         rc = lod_sub_prep_llog(&env, lod, dt, lrd->lrd_idx);
307         if (rc != 0)
308                 GOTO(out, rc);
309
310         /* XXX do recovery in the following patches */
311
312 out:
313         OBD_FREE_PTR(lrd);
314         thread->t_flags = SVC_STOPPED;
315         wake_up(&thread->t_ctl_waitq);
316         lu_env_fini(&env);
317         RETURN(rc);
318 }
319
320 /**
321  * Extract MDT target index from a device name.
322  *
323  * a helper function to extract index from the given device name
324  * like "fsname-MDTxxxx-mdtlov"
325  *
326  * \param[in] lodname           device name
327  * \param[out] mdt_index        extracted index
328  *
329  * \retval 0            on success
330  * \retval -EINVAL      if the name is invalid
331  */
332 int lodname2mdt_index(char *lodname, __u32 *mdt_index)
333 {
334         unsigned long index;
335         char *ptr, *tmp;
336
337         /* 1.8 configs don't have "-MDT0000" at the end */
338         ptr = strstr(lodname, "-MDT");
339         if (ptr == NULL) {
340                 *mdt_index = 0;
341                 return 0;
342         }
343
344         ptr = strrchr(lodname, '-');
345         if (ptr == NULL) {
346                 CERROR("invalid MDT index in '%s'\n", lodname);
347                 return -EINVAL;
348         }
349
350         if (strncmp(ptr, "-mdtlov", 7) != 0) {
351                 CERROR("invalid MDT index in '%s'\n", lodname);
352                 return -EINVAL;
353         }
354
355         if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
356                 CERROR("invalid MDT index in '%s'\n", lodname);
357                 return -EINVAL;
358         }
359
360         if (strncmp(ptr - 8, "-MDT", 4) != 0) {
361                 CERROR("invalid MDT index in '%s'\n", lodname);
362                 return -EINVAL;
363         }
364
365         index = simple_strtol(ptr - 4, &tmp, 16);
366         if (*tmp != '-' || index > INT_MAX) {
367                 CERROR("invalid MDT index in '%s'\n", lodname);
368                 return -EINVAL;
369         }
370         *mdt_index = index;
371         return 0;
372 }
373
374 /**
375  * Init sub llog context
376  *
377  * Setup update llog ctxt for update recovery threads, then start the
378  * recovery thread (lod_sub_recovery_thread) to read update llog from
379  * the correspondent MDT to do update recovery.
380  *
381  * \param[in] env       execution environment
382  * \param[in] lod       lod device to do update recovery
383  * \param[in] dt        sub dt device for which the recovery thread is
384  *
385  * \retval              0 if initialization succeeds.
386  * \retval              negative errno if initialization fails.
387  */
388 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
389                       struct dt_device *dt)
390 {
391         struct obd_device               *obd;
392         struct lod_recovery_data        *lrd = NULL;
393         struct ptlrpc_thread            *thread;
394         struct task_struct              *task;
395         struct l_wait_info              lwi = { 0 };
396         struct lod_tgt_desc             *sub_ltd = NULL;
397         __u32                           index;
398         int                             rc;
399         ENTRY;
400
401         OBD_ALLOC_PTR(lrd);
402         if (lrd == NULL)
403                 RETURN(-ENOMEM);
404
405         if (lod->lod_child == dt) {
406                 thread = &lod->lod_child_recovery_thread;
407                 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &index);
408                 if (rc != 0) {
409                         OBD_FREE_PTR(lrd);
410                         RETURN(rc);
411                 }
412         } else {
413                 struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
414                 struct lod_tgt_desc     *tgt = NULL;
415                 unsigned int            i;
416
417                 mutex_lock(&ltd->ltd_mutex);
418                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
419                         tgt = LTD_TGT(ltd, i);
420                         if (tgt->ltd_tgt == dt) {
421                                 index = tgt->ltd_index;
422                                 sub_ltd = tgt;
423                                 break;
424                         }
425                 }
426                 mutex_unlock(&ltd->ltd_mutex);
427                 OBD_ALLOC_PTR(tgt->ltd_recovery_thread);
428                 if (tgt->ltd_recovery_thread == NULL) {
429                         OBD_FREE_PTR(lrd);
430                         RETURN(-ENOMEM);
431                 }
432                 thread = tgt->ltd_recovery_thread;
433         }
434
435         lrd->lrd_lod = lod;
436         lrd->lrd_ltd = sub_ltd;
437         lrd->lrd_thread = thread;
438         lrd->lrd_idx = index;
439         init_waitqueue_head(&thread->t_ctl_waitq);
440
441         obd = dt->dd_lu_dev.ld_obd;
442         obd->obd_lvfs_ctxt.dt = dt;
443         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
444                         NULL, &llog_common_cat_ops);
445         if (rc < 0) {
446                 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
447                        obd->obd_name, rc);
448                 OBD_FREE_PTR(lrd);
449                 RETURN(rc);
450         }
451
452         /* Start the recovery thread */
453         task = kthread_run(lod_sub_recovery_thread, lrd, "lod_recov_%04x",
454                            index);
455         if (IS_ERR(task)) {
456                 rc = PTR_ERR(task);
457                 OBD_FREE_PTR(lrd);
458                 CERROR("%s: cannot start recovery thread: rc = %d\n",
459                        obd->obd_name, rc);
460                 GOTO(out_llog, rc);
461         }
462
463         l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
464                                           thread->t_flags & SVC_STOPPED, &lwi);
465 out_llog:
466         if (rc != 0)
467                 lod_sub_fini_llog(env, dt, thread);
468
469         RETURN(rc);
470 }
471
472 /**
473  * finish sub llog context
474  *
475  * Stop update recovery thread for the sub device, then cleanup the
476  * correspondent llog ctxt.
477  *
478  * \param[in] env       execution environment
479  * \param[in] dt        dt device(lod) to do update recovery
480  * \param[in] thread    recovery thread on this sub device
481  */
482 void lod_sub_fini_llog(const struct lu_env *env,
483                        struct dt_device *dt, struct ptlrpc_thread *thread)
484 {
485         struct obd_device       *obd;
486         struct llog_ctxt        *ctxt;
487
488         CDEBUG(D_INFO, "%s: finish sub llog\n", dt->dd_lu_dev.ld_obd->obd_name);
489         /* Stop recovery thread first */
490         if (thread != NULL && thread->t_flags & SVC_RUNNING) {
491                 thread->t_flags = SVC_STOPPING;
492                 wake_up(&thread->t_ctl_waitq);
493                 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
494         }
495
496         obd = dt->dd_lu_dev.ld_obd;
497         ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
498         if (ctxt == NULL)
499                 return;
500
501         if (ctxt->loc_handle != NULL)
502                 llog_cat_close(env, ctxt->loc_handle);
503
504         llog_cleanup(env, ctxt);
505 }
506
507 /**
508  * finish all sub llog
509  *
510  * cleanup all of sub llog ctxt on the LOD.
511  *
512  * \param[in] env       execution environment
513  * \param[in] lod       lod device to do update recovery
514  */
515 void lod_sub_fini_all_llogs(const struct lu_env *env, struct lod_device *lod)
516 {
517         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
518         unsigned int i;
519
520         /* Stop the update log commit cancel threads and finish master
521          * llog ctxt */
522         lod_sub_fini_llog(env, lod->lod_child, &lod->lod_child_recovery_thread);
523
524         lod_getref(ltd);
525         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
526                 struct lod_tgt_desc     *tgt;
527
528                 tgt = LTD_TGT(ltd, i);
529                 if (tgt->ltd_recovery_thread != NULL) {
530                         lod_sub_fini_llog(env, tgt->ltd_tgt,
531                                           tgt->ltd_recovery_thread);
532                         OBD_FREE_PTR(tgt->ltd_recovery_thread);
533                         tgt->ltd_recovery_thread = NULL;
534                 }
535         }
536
537         lod_putref(lod, ltd);
538 }
539
540 /**
541  * Implementation of lu_device_operations::ldo_process_config() for LOD
542  *
543  * The method is called by the configuration subsystem during setup,
544  * cleanup and when the configuration changes. The method processes
545  * few specific commands like adding/removing the targets, changing
546  * the runtime parameters.
547
548  * \param[in] env               LU environment provided by the caller
549  * \param[in] dev               lod device
550  * \param[in] lcfg              configuration command to apply
551  *
552  * \retval 0                    on success
553  * \retval negative             negated errno on error
554  *
555  * The examples are below.
556  *
557  * Add osc config log:
558  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
559  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
560  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
561  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
562  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
563  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
564  *
565  * Add mdc config log:
566  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
567  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
568  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
569  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
570  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
571  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
572  */
573 static int lod_process_config(const struct lu_env *env,
574                               struct lu_device *dev,
575                               struct lustre_cfg *lcfg)
576 {
577         struct lod_device *lod = lu2lod_dev(dev);
578         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
579         char              *arg1;
580         int                rc = 0;
581         ENTRY;
582
583         switch(lcfg->lcfg_command) {
584         case LCFG_LOV_DEL_OBD:
585         case LCFG_LOV_ADD_INA:
586         case LCFG_LOV_ADD_OBD:
587         case LCFG_ADD_MDC: {
588                 __u32 index;
589                 __u32 mdt_index;
590                 int gen;
591                 /* lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
592                  * modify_mdc_tgts add  0:lustre-MDT0001
593                  *                    1:lustre-MDT0001-mdc0002
594                  *                    2:2  3:1*/
595                 arg1 = lustre_cfg_string(lcfg, 1);
596
597                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
598                         GOTO(out, rc = -EINVAL);
599                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
600                         GOTO(out, rc = -EINVAL);
601
602                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
603                         __u32 mdt_index;
604
605                         rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
606                                                &mdt_index);
607                         if (rc != 0)
608                                 GOTO(out, rc);
609
610                         rc = lod_add_device(env, lod, arg1, index, gen,
611                                             mdt_index, LUSTRE_OSC_NAME, 1);
612                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
613                         mdt_index = index;
614                         rc = lod_add_device(env, lod, arg1, index, gen,
615                                             mdt_index, LUSTRE_MDC_NAME, 1);
616                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
617                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
618                         mdt_index = 0;
619                         rc = lod_add_device(env, lod, arg1, index, gen,
620                                             mdt_index, LUSTRE_OSC_NAME, 0);
621                 } else {
622                         rc = lod_del_device(env, lod,
623                                             &lod->lod_ost_descs,
624                                             arg1, index, gen, true);
625                 }
626
627                 break;
628         }
629
630         case LCFG_PARAM: {
631                 struct obd_device *obd = lod2obd(lod);
632
633                 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
634                                               lcfg, obd);
635                 if (rc > 0)
636                         rc = 0;
637                 GOTO(out, rc);
638         }
639         case LCFG_PRE_CLEANUP: {
640                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
641                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
642                 next = &lod->lod_child->dd_lu_dev;
643                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
644                 if (rc != 0)
645                         CDEBUG(D_HA, "%s: can't process %u: %d\n",
646                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
647
648                 lod_sub_fini_all_llogs(env, lod);
649                 break;
650         }
651         case LCFG_CLEANUP: {
652                 /*
653                  * do cleanup on underlying storage only when
654                  * all OSPs are cleaned up, as they use that OSD as well
655                  */
656                 lu_dev_del_linkage(dev->ld_site, dev);
657                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
658                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
659                 next = &lod->lod_child->dd_lu_dev;
660                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
661                 if (rc)
662                         CERROR("%s: can't process %u: %d\n",
663                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
664
665                 rc = obd_disconnect(lod->lod_child_exp);
666                 if (rc)
667                         CERROR("error in disconnect from storage: %d\n", rc);
668                 break;
669         }
670         default:
671                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
672                       lcfg->lcfg_command);
673                rc = -EINVAL;
674                break;
675         }
676
677 out:
678         RETURN(rc);
679 }
680
681 /**
682  * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
683  *
684  * The method is called once the recovery is complete. This implementation
685  * distributes the notification to all the known targets.
686  *
687  * see include/lu_object.h for the details
688  */
689 static int lod_recovery_complete(const struct lu_env *env,
690                                  struct lu_device *dev)
691 {
692         struct lod_device   *lod = lu2lod_dev(dev);
693         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
694         unsigned int         i;
695         int                  rc;
696         ENTRY;
697
698         LASSERT(lod->lod_recovery_completed == 0);
699         lod->lod_recovery_completed = 1;
700
701         rc = next->ld_ops->ldo_recovery_complete(env, next);
702
703         lod_getref(&lod->lod_ost_descs);
704         if (lod->lod_osts_size > 0) {
705                 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
706                         struct lod_tgt_desc *tgt;
707                         tgt = OST_TGT(lod, i);
708                         LASSERT(tgt && tgt->ltd_tgt);
709                         next = &tgt->ltd_ost->dd_lu_dev;
710                         rc = next->ld_ops->ldo_recovery_complete(env, next);
711                         if (rc)
712                                 CERROR("%s: can't complete recovery on #%d:"
713                                         "%d\n", lod2obd(lod)->obd_name, i, rc);
714                 }
715         }
716         lod_putref(lod, &lod->lod_ost_descs);
717         RETURN(rc);
718 }
719
720 /**
721  * Init update logs on all sub device
722  *
723  * LOD initialize update logs on all of sub devices. Because the initialization
724  * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
725  * lod_object_init(), this API has to be called after LOD is initialized.
726  * \param[in] env       execution environment
727  * \param[in] lod       lod device
728  *
729  * \retval              0 if update log is initialized successfully.
730  * \retval              negative errno if initialization fails.
731  */
732 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
733 {
734         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
735         int                     rc;
736         unsigned int            i;
737         ENTRY;
738
739         /* llog must be setup after LOD is initialized, because llog
740          * initialization include FLD lookup */
741         LASSERT(lod->lod_initialized);
742
743         /* Init the llog in its own stack */
744         rc = lod_sub_init_llog(env, lod, lod->lod_child);
745         if (rc < 0)
746                 RETURN(rc);
747
748         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
749                 struct lod_tgt_desc     *tgt;
750
751                 tgt = LTD_TGT(ltd, i);
752                 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
753                 if (rc != 0)
754                         break;
755         }
756
757         RETURN(rc);
758 }
759
760 /**
761  * Implementation of lu_device_operations::ldo_prepare() for LOD
762  *
763  * see include/lu_object.h for the details.
764  */
765 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
766                        struct lu_device *cdev)
767 {
768         struct lod_device       *lod = lu2lod_dev(cdev);
769         struct lu_device        *next = &lod->lod_child->dd_lu_dev;
770         struct lu_fid           *fid = &lod_env_info(env)->lti_fid;
771         int                     rc;
772         struct dt_object        *root;
773         struct dt_object        *dto;
774         __u32                   index;
775         ENTRY;
776
777         rc = next->ld_ops->ldo_prepare(env, pdev, next);
778         if (rc != 0) {
779                 CERROR("%s: prepare bottom error: rc = %d\n",
780                        lod2obd(lod)->obd_name, rc);
781                 RETURN(rc);
782         }
783
784         lod->lod_initialized = 1;
785
786         rc = dt_root_get(env, lod->lod_child, fid);
787         if (rc < 0)
788                 RETURN(rc);
789
790         root = dt_locate(env, lod->lod_child, fid);
791         if (IS_ERR(root))
792                 RETURN(PTR_ERR(root));
793
794         index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
795         lu_update_log_fid(fid, index);
796
797         /* Create update log object */
798         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
799                                                  fid, root,
800                                                  lod_update_log_name,
801                                                  S_IFREG | S_IRUGO | S_IWUSR);
802         if (IS_ERR(dto))
803                 GOTO(out_put, rc = PTR_ERR(dto));
804
805         lu_object_put(env, &dto->do_lu);
806
807         /* Create update log dir */
808         lu_update_log_dir_fid(fid, index);
809         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
810                                                  fid, root,
811                                                  lod_update_log_dir_name,
812                                                  S_IFDIR | S_IRUGO | S_IWUSR);
813         if (IS_ERR(dto))
814                 GOTO(out_put, rc = PTR_ERR(dto));
815
816         lu_object_put(env, &dto->do_lu);
817
818         rc = lod_sub_init_llogs(env, lod);
819         if (rc < 0)
820                 GOTO(out_put, rc);
821
822 out_put:
823         lu_object_put(env, &root->do_lu);
824
825         RETURN(rc);
826 }
827
828 const struct lu_device_operations lod_lu_ops = {
829         .ldo_object_alloc       = lod_object_alloc,
830         .ldo_process_config     = lod_process_config,
831         .ldo_recovery_complete  = lod_recovery_complete,
832         .ldo_prepare            = lod_prepare,
833 };
834
835 /**
836  * Implementation of dt_device_operations::dt_root_get() for LOD
837  *
838  * see include/dt_object.h for the details.
839  */
840 static int lod_root_get(const struct lu_env *env,
841                         struct dt_device *dev, struct lu_fid *f)
842 {
843         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
844 }
845
846 /**
847  * Implementation of dt_device_operations::dt_statfs() for LOD
848  *
849  * see include/dt_object.h for the details.
850  */
851 static int lod_statfs(const struct lu_env *env,
852                       struct dt_device *dev, struct obd_statfs *sfs)
853 {
854         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
855 }
856
857 /**
858  * Implementation of dt_device_operations::dt_trans_create() for LOD
859  *
860  * Creates a transaction using local (to this node) OSD.
861  *
862  * see include/dt_object.h for the details.
863  */
864 static struct thandle *lod_trans_create(const struct lu_env *env,
865                                         struct dt_device *dt)
866 {
867         struct thandle *th;
868
869         th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
870         if (IS_ERR(th))
871                 return th;
872
873         th->th_dev = dt;
874
875         return th;
876 }
877
878 /**
879  * Implementation of dt_device_operations::dt_trans_start() for LOD
880  *
881  * Starts the set of local transactions using the targets involved
882  * in declare phase. Initial support for the distributed transactions.
883  *
884  * see include/dt_object.h for the details.
885  */
886 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
887                            struct thandle *th)
888 {
889         return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
890 }
891
892 static int lod_trans_cb_add(struct thandle *th,
893                             struct dt_txn_commit_cb *dcb)
894 {
895         struct top_thandle      *top_th = container_of(th, struct top_thandle,
896                                                        tt_super);
897         return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
898 }
899
900 /**
901  * Implementation of dt_device_operations::dt_trans_stop() for LOD
902  *
903  * Stops the set of local transactions using the targets involved
904  * in declare phase. Initial support for the distributed transactions.
905  *
906  * see include/dt_object.h for the details.
907  */
908 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
909                           struct thandle *th)
910 {
911         return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
912 }
913
914 /**
915  * Implementation of dt_device_operations::dt_conf_get() for LOD
916  *
917  * Currently returns the configuration provided by the local OSD.
918  *
919  * see include/dt_object.h for the details.
920  */
921 static void lod_conf_get(const struct lu_env *env,
922                          const struct dt_device *dev,
923                          struct dt_device_param *param)
924 {
925         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
926 }
927
928 /**
929  * Implementation of dt_device_operations::dt_sync() for LOD
930  *
931  * Syncs all known OST targets. Very very expensive and used
932  * rarely by LFSCK now. Should not be used in general.
933  *
934  * see include/dt_object.h for the details.
935  */
936 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
937 {
938         struct lod_device   *lod = dt2lod_dev(dev);
939         struct lod_ost_desc *ost;
940         unsigned int         i;
941         int                  rc = 0;
942         ENTRY;
943
944         lod_getref(&lod->lod_ost_descs);
945         lod_foreach_ost(lod, i) {
946                 ost = OST_TGT(lod, i);
947                 LASSERT(ost && ost->ltd_ost);
948                 rc = dt_sync(env, ost->ltd_ost);
949                 if (rc) {
950                         CERROR("%s: can't sync %u: %d\n",
951                                lod2obd(lod)->obd_name, i, rc);
952                         break;
953                 }
954         }
955         lod_putref(lod, &lod->lod_ost_descs);
956         if (rc == 0)
957                 rc = dt_sync(env, lod->lod_child);
958
959         RETURN(rc);
960 }
961
962 /**
963  * Implementation of dt_device_operations::dt_ro() for LOD
964  *
965  * Turns local OSD read-only, used for the testing only.
966  *
967  * see include/dt_object.h for the details.
968  */
969 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
970 {
971         return dt_ro(env, dt2lod_dev(dev)->lod_child);
972 }
973
974 /**
975  * Implementation of dt_device_operations::dt_commit_async() for LOD
976  *
977  * Asks local OSD to commit sooner.
978  *
979  * see include/dt_object.h for the details.
980  */
981 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
982 {
983         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
984 }
985
986 static const struct dt_device_operations lod_dt_ops = {
987         .dt_root_get         = lod_root_get,
988         .dt_statfs           = lod_statfs,
989         .dt_trans_create     = lod_trans_create,
990         .dt_trans_start      = lod_trans_start,
991         .dt_trans_stop       = lod_trans_stop,
992         .dt_conf_get         = lod_conf_get,
993         .dt_sync             = lod_sync,
994         .dt_ro               = lod_ro,
995         .dt_commit_async     = lod_commit_async,
996         .dt_trans_cb_add     = lod_trans_cb_add,
997 };
998
999 /**
1000  * Connect to a local OSD.
1001  *
1002  * Used to connect to the local OSD at mount. OSD name is taken from the
1003  * configuration command passed. This connection is used to identify LU
1004  * site and pin the OSD from early removal.
1005  *
1006  * \param[in] env               LU environment provided by the caller
1007  * \param[in] lod               lod device
1008  * \param[in] cfg               configuration command to apply
1009  *
1010  * \retval 0                    on success
1011  * \retval negative             negated errno on error
1012  **/
1013 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1014                               struct lustre_cfg *cfg)
1015 {
1016         struct obd_connect_data *data = NULL;
1017         struct obd_device       *obd;
1018         char                    *nextdev = NULL, *p, *s;
1019         int                      rc, len = 0;
1020         ENTRY;
1021
1022         LASSERT(cfg);
1023         LASSERT(lod->lod_child_exp == NULL);
1024
1025         /* compatibility hack: we still use old config logs
1026          * which specify LOV, but we need to learn underlying
1027          * OSD device, which is supposed to be:
1028          *  <fsname>-MDTxxxx-osd
1029          *
1030          * 2.x MGS generates lines like the following:
1031          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
1032          * 1.8 MGS generates lines like the following:
1033          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
1034          *
1035          * we use "-MDT" to differentiate 2.x from 1.8 */
1036
1037         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1038                 len = strlen(p) + 1;
1039                 OBD_ALLOC(nextdev, len);
1040                 if (nextdev == NULL)
1041                         GOTO(out, rc = -ENOMEM);
1042
1043                 strcpy(nextdev, p);
1044                 s = strstr(nextdev, "-mdtlov");
1045                 if (unlikely(s == NULL)) {
1046                         CERROR("unable to parse device name %s\n",
1047                                lustre_cfg_string(cfg, 0));
1048                         GOTO(out, rc = -EINVAL);
1049                 }
1050
1051                 if (strstr(nextdev, "-MDT")) {
1052                         /* 2.x config */
1053                         strcpy(s, "-osd");
1054                 } else {
1055                         /* 1.8 config */
1056                         strcpy(s, "-MDT0000-osd");
1057                 }
1058         } else {
1059                 CERROR("unable to parse device name %s\n",
1060                        lustre_cfg_string(cfg, 0));
1061                 GOTO(out, rc = -EINVAL);
1062         }
1063
1064         OBD_ALLOC_PTR(data);
1065         if (data == NULL)
1066                 GOTO(out, rc = -ENOMEM);
1067
1068         obd = class_name2obd(nextdev);
1069         if (obd == NULL) {
1070                 CERROR("can not locate next device: %s\n", nextdev);
1071                 GOTO(out, rc = -ENOTCONN);
1072         }
1073
1074         data->ocd_connect_flags = OBD_CONNECT_VERSION;
1075         data->ocd_version = LUSTRE_VERSION_CODE;
1076
1077         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1078                          data, NULL);
1079         if (rc) {
1080                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1081                 GOTO(out, rc);
1082         }
1083
1084         lod->lod_dt_dev.dd_lu_dev.ld_site =
1085                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1086         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1087         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1088
1089 out:
1090         if (data)
1091                 OBD_FREE_PTR(data);
1092         if (nextdev)
1093                 OBD_FREE(nextdev, len);
1094         RETURN(rc);
1095 }
1096
1097 /**
1098  * Allocate and initialize target table.
1099  *
1100  * A helper function to initialize the target table and allocate
1101  * a bitmap of the available targets.
1102  *
1103  * \param[in] ltd               target's table to initialize
1104  *
1105  * \retval 0                    on success
1106  * \retval negative             negated errno on error
1107  **/
1108 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1109 {
1110         mutex_init(&ltd->ltd_mutex);
1111         init_rwsem(&ltd->ltd_rw_sem);
1112
1113         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1114          * added to the LOD, see lod_add_device() */
1115         ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1116         if (ltd->ltd_tgt_bitmap == NULL)
1117                 RETURN(-ENOMEM);
1118
1119         ltd->ltd_tgts_size  = 32;
1120         ltd->ltd_tgtnr      = 0;
1121
1122         ltd->ltd_death_row = 0;
1123         ltd->ltd_refcount  = 0;
1124         return 0;
1125 }
1126
1127 /**
1128  * Initialize LOD device at setup.
1129  *
1130  * Initializes the given LOD device using the original configuration command.
1131  * The function initiates a connection to the local OSD and initializes few
1132  * internal structures like pools, target tables, etc.
1133  *
1134  * \param[in] env               LU environment provided by the caller
1135  * \param[in] lod               lod device
1136  * \param[in] ldt               not used
1137  * \param[in] cfg               configuration command
1138  *
1139  * \retval 0                    on success
1140  * \retval negative             negated errno on error
1141  **/
1142 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1143                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
1144 {
1145         struct dt_device_param ddp;
1146         struct obd_device     *obd;
1147         int                    rc;
1148         ENTRY;
1149
1150         obd = class_name2obd(lustre_cfg_string(cfg, 0));
1151         if (obd == NULL) {
1152                 CERROR("Cannot find obd with name %s\n",
1153                        lustre_cfg_string(cfg, 0));
1154                 RETURN(-ENODEV);
1155         }
1156
1157         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1158         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1159         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1160         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1161
1162         rc = lod_connect_to_osd(env, lod, cfg);
1163         if (rc)
1164                 RETURN(rc);
1165
1166         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1167         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1168
1169         /* setup obd to be used with old lov code */
1170         rc = lod_pools_init(lod, cfg);
1171         if (rc)
1172                 GOTO(out_disconnect, rc);
1173
1174         rc = lod_procfs_init(lod);
1175         if (rc)
1176                 GOTO(out_pools, rc);
1177
1178         spin_lock_init(&lod->lod_desc_lock);
1179         spin_lock_init(&lod->lod_connects_lock);
1180         lod_tgt_desc_init(&lod->lod_mdt_descs);
1181         lod_tgt_desc_init(&lod->lod_ost_descs);
1182
1183         RETURN(0);
1184
1185 out_pools:
1186         lod_pools_fini(lod);
1187 out_disconnect:
1188         obd_disconnect(lod->lod_child_exp);
1189         RETURN(rc);
1190 }
1191
1192 /**
1193  * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1194  *
1195  * Releases the memory allocated for LOD device.
1196  *
1197  * see include/lu_object.h for the details.
1198  */
1199 static struct lu_device *lod_device_free(const struct lu_env *env,
1200                                          struct lu_device *lu)
1201 {
1202         struct lod_device *lod = lu2lod_dev(lu);
1203         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
1204         ENTRY;
1205
1206         LASSERT(atomic_read(&lu->ld_ref) == 0);
1207         dt_device_fini(&lod->lod_dt_dev);
1208         OBD_FREE_PTR(lod);
1209         RETURN(next);
1210 }
1211
1212 /**
1213  * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1214  *
1215  * Allocates LOD device and calls the helpers to initialize it.
1216  *
1217  * see include/lu_object.h for the details.
1218  */
1219 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1220                                           struct lu_device_type *type,
1221                                           struct lustre_cfg *lcfg)
1222 {
1223         struct lod_device *lod;
1224         struct lu_device  *lu_dev;
1225
1226         OBD_ALLOC_PTR(lod);
1227         if (lod == NULL) {
1228                 lu_dev = ERR_PTR(-ENOMEM);
1229         } else {
1230                 int rc;
1231
1232                 lu_dev = lod2lu_dev(lod);
1233                 dt_device_init(&lod->lod_dt_dev, type);
1234                 rc = lod_init0(env, lod, type, lcfg);
1235                 if (rc != 0) {
1236                         lod_device_free(env, lu_dev);
1237                         lu_dev = ERR_PTR(rc);
1238                 }
1239         }
1240
1241         return lu_dev;
1242 }
1243
1244 /**
1245  * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1246  *
1247  * Releases the internal resources used by LOD device.
1248  *
1249  * see include/lu_object.h for the details.
1250  */
1251 static struct lu_device *lod_device_fini(const struct lu_env *env,
1252                                          struct lu_device *d)
1253 {
1254         struct lod_device *lod = lu2lod_dev(d);
1255         int                rc;
1256         ENTRY;
1257
1258         lod_pools_fini(lod);
1259
1260         lod_procfs_fini(lod);
1261
1262         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1263         if (rc)
1264                 CERROR("%s:can not fini ost descs %d\n",
1265                         lod2obd(lod)->obd_name, rc);
1266
1267         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1268         if (rc)
1269                 CERROR("%s:can not fini mdt descs %d\n",
1270                         lod2obd(lod)->obd_name, rc);
1271
1272         RETURN(NULL);
1273 }
1274
1275 /**
1276  * Implementation of obd_ops::o_connect() for LOD
1277  *
1278  * Used to track all the users of this specific LOD device,
1279  * so the device stays up until the last user disconnected.
1280  *
1281  * \param[in] env               LU environment provided by the caller
1282  * \param[out] exp              export the caller will be using to access LOD
1283  * \param[in] obd               OBD device representing LOD device
1284  * \param[in] cluuid            unique identifier of the caller
1285  * \param[in] data              not used
1286  * \param[in] localdata         not used
1287  *
1288  * \retval 0                    on success
1289  * \retval negative             negated errno on error
1290  **/
1291 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1292                            struct obd_device *obd, struct obd_uuid *cluuid,
1293                            struct obd_connect_data *data, void *localdata)
1294 {
1295         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
1296         struct lustre_handle  conn;
1297         int                   rc;
1298         ENTRY;
1299
1300         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1301
1302         rc = class_connect(&conn, obd, cluuid);
1303         if (rc)
1304                 RETURN(rc);
1305
1306         *exp = class_conn2export(&conn);
1307
1308         spin_lock(&lod->lod_connects_lock);
1309         lod->lod_connects++;
1310         /* at the moment we expect the only user */
1311         LASSERT(lod->lod_connects == 1);
1312         spin_unlock(&lod->lod_connects_lock);
1313
1314         RETURN(0);
1315 }
1316
1317 /**
1318  *
1319  * Implementation of obd_ops::o_disconnect() for LOD
1320  *
1321  * When the caller doesn't need to use this LOD instance, it calls
1322  * obd_disconnect() and LOD releases corresponding export/reference count.
1323  * Once all the users gone, LOD device is released.
1324  *
1325  * \param[in] exp               export provided to the caller in obd_connect()
1326  *
1327  * \retval 0                    on success
1328  * \retval negative             negated errno on error
1329  **/
1330 static int lod_obd_disconnect(struct obd_export *exp)
1331 {
1332         struct obd_device *obd = exp->exp_obd;
1333         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1334         int                rc, release = 0;
1335         ENTRY;
1336
1337         /* Only disconnect the underlying layers on the final disconnect. */
1338         spin_lock(&lod->lod_connects_lock);
1339         lod->lod_connects--;
1340         if (lod->lod_connects != 0) {
1341                 /* why should there be more than 1 connect? */
1342                 spin_unlock(&lod->lod_connects_lock);
1343                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1344                        lod->lod_connects);
1345                 goto out;
1346         }
1347         spin_unlock(&lod->lod_connects_lock);
1348
1349         /* the last user of lod has gone, let's release the device */
1350         release = 1;
1351
1352 out:
1353         rc = class_disconnect(exp); /* bz 9811 */
1354
1355         if (rc == 0 && release)
1356                 class_manual_cleanup(obd);
1357         RETURN(rc);
1358 }
1359
1360 LU_KEY_INIT(lod, struct lod_thread_info);
1361
1362 static void lod_key_fini(const struct lu_context *ctx,
1363                 struct lu_context_key *key, void *data)
1364 {
1365         struct lod_thread_info *info = data;
1366         /* allocated in lod_get_lov_ea
1367          * XXX: this is overload, a tread may have such store but used only
1368          * once. Probably better would be pool of such stores per LOD.
1369          */
1370         if (info->lti_ea_store) {
1371                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1372                 info->lti_ea_store = NULL;
1373                 info->lti_ea_store_size = 0;
1374         }
1375         lu_buf_free(&info->lti_linkea_buf);
1376         OBD_FREE_PTR(info);
1377 }
1378
1379 /* context key: lod_thread_key */
1380 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1381
1382 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1383
1384 static struct lu_device_type_operations lod_device_type_ops = {
1385         .ldto_init           = lod_type_init,
1386         .ldto_fini           = lod_type_fini,
1387
1388         .ldto_start          = lod_type_start,
1389         .ldto_stop           = lod_type_stop,
1390
1391         .ldto_device_alloc   = lod_device_alloc,
1392         .ldto_device_free    = lod_device_free,
1393
1394         .ldto_device_fini    = lod_device_fini
1395 };
1396
1397 static struct lu_device_type lod_device_type = {
1398         .ldt_tags     = LU_DEVICE_DT,
1399         .ldt_name     = LUSTRE_LOD_NAME,
1400         .ldt_ops      = &lod_device_type_ops,
1401         .ldt_ctx_tags = LCT_MD_THREAD,
1402 };
1403
1404 /**
1405  * Implementation of obd_ops::o_get_info() for LOD
1406  *
1407  * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1408  * the caller binary status whether LOD has seen connection to any OST target.
1409  * It will also check if the MDT update log context being initialized (if
1410  * needed).
1411  *
1412  * \param[in] env               LU environment provided by the caller
1413  * \param[in] exp               export of the caller
1414  * \param[in] keylen            len of the key
1415  * \param[in] key               the key
1416  * \param[in] vallen            not used
1417  * \param[in] val               not used
1418  *
1419  * \retval                      0 if a connection was seen
1420  * \retval                      -EAGAIN if LOD isn't running yet or no
1421  *                              connection has been seen yet
1422  * \retval                      -EINVAL if not supported key is requested
1423  **/
1424 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1425                             __u32 keylen, void *key, __u32 *vallen, void *val)
1426 {
1427         int rc = -EINVAL;
1428
1429         if (KEY_IS(KEY_OSP_CONNECTED)) {
1430                 struct obd_device       *obd = exp->exp_obd;
1431                 struct lod_device       *d;
1432                 struct lod_tgt_desc     *tgt;
1433                 unsigned int            i;
1434                 int                     rc = 1;
1435
1436                 if (!obd->obd_set_up || obd->obd_stopping)
1437                         RETURN(-EAGAIN);
1438
1439                 d = lu2lod_dev(obd->obd_lu_dev);
1440                 lod_getref(&d->lod_ost_descs);
1441                 lod_foreach_ost(d, i) {
1442                         tgt = OST_TGT(d, i);
1443                         LASSERT(tgt && tgt->ltd_tgt);
1444                         rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1445                                           vallen, val);
1446                         /* one healthy device is enough */
1447                         if (rc == 0)
1448                                 break;
1449                 }
1450                 lod_putref(d, &d->lod_ost_descs);
1451
1452                 lod_getref(&d->lod_mdt_descs);
1453                 lod_foreach_mdt(d, i) {
1454                         struct llog_ctxt *ctxt;
1455
1456                         tgt = MDT_TGT(d, i);
1457                         LASSERT(tgt != NULL);
1458                         LASSERT(tgt->ltd_tgt != NULL);
1459                         ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
1460                                                 LLOG_UPDATELOG_ORIG_CTXT);
1461                         if (ctxt == NULL) {
1462                                 rc = -EAGAIN;
1463                                 break;
1464                         }
1465                         if (ctxt->loc_handle == NULL) {
1466                                 rc = -EAGAIN;
1467                                 llog_ctxt_put(ctxt);
1468                                 break;
1469                         }
1470                         llog_ctxt_put(ctxt);
1471                 }
1472                 lod_putref(d, &d->lod_mdt_descs);
1473
1474                 RETURN(rc);
1475         }
1476
1477         RETURN(rc);
1478 }
1479
1480 static struct obd_ops lod_obd_device_ops = {
1481         .o_owner        = THIS_MODULE,
1482         .o_connect      = lod_obd_connect,
1483         .o_disconnect   = lod_obd_disconnect,
1484         .o_get_info     = lod_obd_get_info,
1485         .o_pool_new     = lod_pool_new,
1486         .o_pool_rem     = lod_pool_remove,
1487         .o_pool_add     = lod_pool_add,
1488         .o_pool_del     = lod_pool_del,
1489 };
1490
1491 static int __init lod_mod_init(void)
1492 {
1493         struct obd_type *type;
1494         int rc;
1495
1496         rc = lu_kmem_init(lod_caches);
1497         if (rc)
1498                 return rc;
1499
1500         rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
1501                                  LUSTRE_LOD_NAME, &lod_device_type);
1502         if (rc) {
1503                 lu_kmem_fini(lod_caches);
1504                 return rc;
1505         }
1506
1507         /* create "lov" entry in procfs for compatibility purposes */
1508         type = class_search_type(LUSTRE_LOV_NAME);
1509         if (type != NULL && type->typ_procroot != NULL)
1510                 return rc;
1511
1512         type = class_search_type(LUSTRE_LOD_NAME);
1513         type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
1514                                              NULL, NULL);
1515         if (IS_ERR(type->typ_procsym)) {
1516                 CERROR("lod: can't create compat entry \"lov\": %d\n",
1517                        (int)PTR_ERR(type->typ_procsym));
1518                 type->typ_procsym = NULL;
1519         }
1520         return rc;
1521 }
1522
1523 static void __exit lod_mod_exit(void)
1524 {
1525         class_unregister_type(LUSTRE_LOD_NAME);
1526         lu_kmem_fini(lod_caches);
1527 }
1528
1529 MODULE_AUTHOR("Intel Corporation. <https://wiki.hpdd.intel.com/>");
1530 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
1531 MODULE_LICENSE("GPL");
1532
1533 module_init(lod_mod_init);
1534 module_exit(lod_mod_exit);
1535