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