Whamcloud - gitweb
7886704dfc7513d64e28becb65c200e15f6ca16c
[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  * Stop sub recovery thread
635  *
636  * Stop sub recovery thread on all subs.
637  *
638  * \param[in] env       execution environment
639  * \param[in] lod       lod device to do update recovery
640  */
641 static void lod_sub_stop_recovery_threads(const struct lu_env *env,
642                                           struct lod_device *lod)
643 {
644         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
645         struct ptlrpc_thread    *thread;
646         unsigned int i;
647
648         /* Stop the update log commit cancel threads and finish master
649          * llog ctxt */
650         thread = &lod->lod_child_recovery_thread;
651         /* Stop recovery thread first */
652         if (thread != NULL && thread->t_flags & SVC_RUNNING) {
653                 thread->t_flags = SVC_STOPPING;
654                 wake_up(&thread->t_ctl_waitq);
655                 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
656         }
657
658         lod_getref(ltd);
659         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
660                 struct lod_tgt_desc     *tgt;
661
662                 tgt = LTD_TGT(ltd, i);
663                 thread = tgt->ltd_recovery_thread;
664                 if (thread != NULL && thread->t_flags & SVC_RUNNING) {
665                         thread->t_flags = SVC_STOPPING;
666                         wake_up(&thread->t_ctl_waitq);
667                         wait_event(thread->t_ctl_waitq,
668                                    thread->t_flags & SVC_STOPPED);
669                         OBD_FREE_PTR(tgt->ltd_recovery_thread);
670                         tgt->ltd_recovery_thread = NULL;
671                 }
672         }
673
674         lod_putref(lod, ltd);
675 }
676
677 /**
678  * finish all sub llog
679  *
680  * cleanup all of sub llog ctxt on the LOD.
681  *
682  * \param[in] env       execution environment
683  * \param[in] lod       lod device to do update recovery
684  */
685 static void lod_sub_fini_all_llogs(const struct lu_env *env,
686                                    struct lod_device *lod)
687 {
688         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
689         unsigned int i;
690
691         /* Stop the update log commit cancel threads and finish master
692          * llog ctxt */
693         lod_sub_fini_llog(env, lod->lod_child,
694                           &lod->lod_child_recovery_thread);
695         lod_getref(ltd);
696         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
697                 struct lod_tgt_desc     *tgt;
698
699                 tgt = LTD_TGT(ltd, i);
700                 lod_sub_fini_llog(env, tgt->ltd_tgt,
701                                   tgt->ltd_recovery_thread);
702         }
703
704         lod_putref(lod, ltd);
705 }
706
707 /**
708  * Prepare distribute txn
709  *
710  * Prepare distribute txn structure for LOD
711  *
712  * \param[in] env       execution environment
713  * \param[in] lod_device  LOD device
714  *
715  * \retval              0 if preparation succeeds.
716  * \retval              negative errno if preparation fails.
717  */
718 static int lod_prepare_distribute_txn(const struct lu_env *env,
719                                       struct lod_device *lod)
720 {
721         struct target_distribute_txn_data *tdtd;
722         struct lu_target                  *lut;
723         int                               rc;
724         ENTRY;
725
726         /* Init update recovery data */
727         OBD_ALLOC_PTR(tdtd);
728         if (tdtd == NULL)
729                 RETURN(-ENOMEM);
730
731         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
732         tdtd->tdtd_dt = &lod->lod_dt_dev;
733         rc = distribute_txn_init(env, lut, tdtd,
734                 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
735
736         if (rc < 0) {
737                 CERROR("%s: cannot init distribute txn: rc = %d\n",
738                        lod2obd(lod)->obd_name, rc);
739                 OBD_FREE_PTR(tdtd);
740                 RETURN(rc);
741         }
742
743         lut->lut_tdtd = tdtd;
744
745         RETURN(0);
746 }
747
748 /**
749  * Finish distribute txn
750  *
751  * Release the resource holding by distribute txn, i.e. stop distribute
752  * txn thread.
753  *
754  * \param[in] env       execution environment
755  * \param[in] lod       lod device
756  */
757 static void lod_fini_distribute_txn(const struct lu_env *env,
758                                     struct lod_device *lod)
759 {
760         struct lu_target                  *lut;
761
762         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
763         if (lut->lut_tdtd == NULL)
764                 return;
765
766         distribute_txn_fini(env, lut->lut_tdtd);
767
768         OBD_FREE_PTR(lut->lut_tdtd);
769         lut->lut_tdtd = NULL;
770 }
771
772 /**
773  * Implementation of lu_device_operations::ldo_process_config() for LOD
774  *
775  * The method is called by the configuration subsystem during setup,
776  * cleanup and when the configuration changes. The method processes
777  * few specific commands like adding/removing the targets, changing
778  * the runtime parameters.
779
780  * \param[in] env               LU environment provided by the caller
781  * \param[in] dev               lod device
782  * \param[in] lcfg              configuration command to apply
783  *
784  * \retval 0                    on success
785  * \retval negative             negated errno on error
786  *
787  * The examples are below.
788  *
789  * Add osc config log:
790  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
791  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
792  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
793  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
794  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
795  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
796  *
797  * Add mdc config log:
798  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
799  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
800  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
801  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
802  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
803  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
804  */
805 static int lod_process_config(const struct lu_env *env,
806                               struct lu_device *dev,
807                               struct lustre_cfg *lcfg)
808 {
809         struct lod_device *lod = lu2lod_dev(dev);
810         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
811         char              *arg1;
812         int                rc = 0;
813         ENTRY;
814
815         switch(lcfg->lcfg_command) {
816         case LCFG_LOV_DEL_OBD:
817         case LCFG_LOV_ADD_INA:
818         case LCFG_LOV_ADD_OBD:
819         case LCFG_ADD_MDC: {
820                 __u32 index;
821                 __u32 mdt_index;
822                 int gen;
823                 /* lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
824                  * modify_mdc_tgts add  0:lustre-MDT0001
825                  *                    1:lustre-MDT0001-mdc0002
826                  *                    2:2  3:1*/
827                 arg1 = lustre_cfg_string(lcfg, 1);
828
829                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
830                         GOTO(out, rc = -EINVAL);
831                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
832                         GOTO(out, rc = -EINVAL);
833
834                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
835                         __u32 mdt_index;
836
837                         rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
838                                                &mdt_index);
839                         if (rc != 0)
840                                 GOTO(out, rc);
841
842                         rc = lod_add_device(env, lod, arg1, index, gen,
843                                             mdt_index, LUSTRE_OSC_NAME, 1);
844                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
845                         mdt_index = index;
846                         rc = lod_add_device(env, lod, arg1, index, gen,
847                                             mdt_index, LUSTRE_MDC_NAME, 1);
848                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
849                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
850                         mdt_index = 0;
851                         rc = lod_add_device(env, lod, arg1, index, gen,
852                                             mdt_index, LUSTRE_OSC_NAME, 0);
853                 } else {
854                         rc = lod_del_device(env, lod,
855                                             &lod->lod_ost_descs,
856                                             arg1, index, gen, true);
857                 }
858
859                 break;
860         }
861
862         case LCFG_PARAM: {
863                 struct obd_device *obd;
864                 char *param;
865
866                 /* Check if it is activate/deactivate mdc
867                  * lustre-MDTXXXX-osp-MDTXXXX.active=1 */
868                 param = lustre_cfg_buf(lcfg, 1);
869                 if (strstr(param, "osp") != NULL &&
870                     strstr(param, ".active=") != NULL) {
871                         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
872                         struct lod_tgt_desc     *sub_tgt = NULL;
873                         char *ptr;
874                         char *tmp;
875                         int i;
876
877                         ptr = strstr(param, ".");
878                         *ptr = '\0';
879                         obd = class_name2obd(param);
880                         if (obd == NULL) {
881                                 CERROR("%s: can not find %s: rc = %d\n",
882                                        lod2obd(lod)->obd_name, param, -EINVAL);
883                                 *ptr = '.';
884                                 GOTO(out, rc);
885                         }
886
887                         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
888                                 struct lod_tgt_desc *tgt;
889
890                                 tgt = LTD_TGT(ltd, i);
891                                 if (tgt->ltd_tgt->dd_lu_dev.ld_obd == obd) {
892                                         sub_tgt = tgt;
893                                         break;
894                                 }
895                         }
896
897                         if (sub_tgt == NULL) {
898                                 CERROR("%s: can not find %s: rc = %d\n",
899                                        lod2obd(lod)->obd_name, param, -EINVAL);
900                                 *ptr = '.';
901                                 GOTO(out, rc);
902                         }
903
904                         *ptr = '.';
905                         tmp = strstr(param, "=");
906                         tmp++;
907                         if (*tmp == '1') {
908                                 struct llog_ctxt *ctxt;
909
910                                 obd = sub_tgt->ltd_tgt->dd_lu_dev.ld_obd;
911                                 ctxt = llog_get_context(obd,
912                                                 LLOG_UPDATELOG_ORIG_CTXT);
913                                 if (ctxt == NULL) {
914                                         rc = llog_setup(env, obd, &obd->obd_olg,
915                                                        LLOG_UPDATELOG_ORIG_CTXT,
916                                                     NULL, &llog_common_cat_ops);
917                                         if (rc < 0)
918                                                 GOTO(out, rc);
919                                 } else {
920                                         llog_ctxt_put(ctxt);
921                                 }
922                                 rc = lod_sub_prep_llog(env, lod,
923                                                        sub_tgt->ltd_tgt,
924                                                        sub_tgt->ltd_index);
925                                 if (rc == 0)
926                                         sub_tgt->ltd_active = 1;
927                         } else {
928                                 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
929                                                   NULL);
930                                 sub_tgt->ltd_active = 0;
931                         }
932                         GOTO(out, rc);
933                 }
934
935                 obd = lod2obd(lod);
936                 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
937                                               lcfg, obd);
938                 if (rc > 0)
939                         rc = 0;
940                 GOTO(out, rc);
941         }
942         case LCFG_PRE_CLEANUP: {
943                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
944                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
945                 next = &lod->lod_child->dd_lu_dev;
946                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
947                 if (rc != 0)
948                         CDEBUG(D_HA, "%s: can't process %u: %d\n",
949                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
950
951                 lod_sub_stop_recovery_threads(env, lod);
952                 lod_fini_distribute_txn(env, lod);
953                 lod_sub_fini_all_llogs(env, lod);
954                 break;
955         }
956         case LCFG_CLEANUP: {
957                 /*
958                  * do cleanup on underlying storage only when
959                  * all OSPs are cleaned up, as they use that OSD as well
960                  */
961                 lu_dev_del_linkage(dev->ld_site, dev);
962                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
963                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
964                 next = &lod->lod_child->dd_lu_dev;
965                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
966                 if (rc)
967                         CERROR("%s: can't process %u: %d\n",
968                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
969
970                 rc = obd_disconnect(lod->lod_child_exp);
971                 if (rc)
972                         CERROR("error in disconnect from storage: %d\n", rc);
973                 break;
974         }
975         default:
976                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
977                       lcfg->lcfg_command);
978                rc = -EINVAL;
979                break;
980         }
981
982 out:
983         RETURN(rc);
984 }
985
986 /**
987  * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
988  *
989  * The method is called once the recovery is complete. This implementation
990  * distributes the notification to all the known targets.
991  *
992  * see include/lu_object.h for the details
993  */
994 static int lod_recovery_complete(const struct lu_env *env,
995                                  struct lu_device *dev)
996 {
997         struct lod_device   *lod = lu2lod_dev(dev);
998         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
999         unsigned int         i;
1000         int                  rc;
1001         ENTRY;
1002
1003         LASSERT(lod->lod_recovery_completed == 0);
1004         lod->lod_recovery_completed = 1;
1005
1006         rc = next->ld_ops->ldo_recovery_complete(env, next);
1007
1008         lod_getref(&lod->lod_ost_descs);
1009         if (lod->lod_osts_size > 0) {
1010                 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
1011                         struct lod_tgt_desc *tgt;
1012                         tgt = OST_TGT(lod, i);
1013                         LASSERT(tgt && tgt->ltd_tgt);
1014                         next = &tgt->ltd_ost->dd_lu_dev;
1015                         rc = next->ld_ops->ldo_recovery_complete(env, next);
1016                         if (rc)
1017                                 CERROR("%s: can't complete recovery on #%d:"
1018                                         "%d\n", lod2obd(lod)->obd_name, i, rc);
1019                 }
1020         }
1021         lod_putref(lod, &lod->lod_ost_descs);
1022         RETURN(rc);
1023 }
1024
1025 /**
1026  * Init update logs on all sub device
1027  *
1028  * LOD initialize update logs on all of sub devices. Because the initialization
1029  * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1030  * lod_object_init(), this API has to be called after LOD is initialized.
1031  * \param[in] env       execution environment
1032  * \param[in] lod       lod device
1033  *
1034  * \retval              0 if update log is initialized successfully.
1035  * \retval              negative errno if initialization fails.
1036  */
1037 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1038 {
1039         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
1040         int                     rc;
1041         unsigned int            i;
1042         ENTRY;
1043
1044         /* llog must be setup after LOD is initialized, because llog
1045          * initialization include FLD lookup */
1046         LASSERT(lod->lod_initialized);
1047
1048         /* Init the llog in its own stack */
1049         rc = lod_sub_init_llog(env, lod, lod->lod_child);
1050         if (rc < 0)
1051                 RETURN(rc);
1052
1053         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
1054                 struct lod_tgt_desc     *tgt;
1055
1056                 tgt = LTD_TGT(ltd, i);
1057                 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
1058                 if (rc != 0)
1059                         break;
1060         }
1061
1062         RETURN(rc);
1063 }
1064
1065 /**
1066  * Implementation of lu_device_operations::ldo_prepare() for LOD
1067  *
1068  * see include/lu_object.h for the details.
1069  */
1070 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1071                        struct lu_device *cdev)
1072 {
1073         struct lod_device       *lod = lu2lod_dev(cdev);
1074         struct lu_device        *next = &lod->lod_child->dd_lu_dev;
1075         struct lu_fid           *fid = &lod_env_info(env)->lti_fid;
1076         int                     rc;
1077         struct dt_object        *root;
1078         struct dt_object        *dto;
1079         __u32                   index;
1080         ENTRY;
1081
1082         rc = next->ld_ops->ldo_prepare(env, pdev, next);
1083         if (rc != 0) {
1084                 CERROR("%s: prepare bottom error: rc = %d\n",
1085                        lod2obd(lod)->obd_name, rc);
1086                 RETURN(rc);
1087         }
1088
1089         lod->lod_initialized = 1;
1090
1091         rc = dt_root_get(env, lod->lod_child, fid);
1092         if (rc < 0)
1093                 RETURN(rc);
1094
1095         root = dt_locate(env, lod->lod_child, fid);
1096         if (IS_ERR(root))
1097                 RETURN(PTR_ERR(root));
1098
1099         /* Create update log object */
1100         index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1101         lu_update_log_fid(fid, index);
1102
1103         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1104                                                  fid, root,
1105                                                  lod_update_log_name,
1106                                                  S_IFREG | S_IRUGO | S_IWUSR);
1107         if (IS_ERR(dto))
1108                 GOTO(out_put, rc = PTR_ERR(dto));
1109
1110         lu_object_put(env, &dto->do_lu);
1111
1112         /* Create update log dir */
1113         lu_update_log_dir_fid(fid, index);
1114         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1115                                                  fid, root,
1116                                                  lod_update_log_dir_name,
1117                                                  S_IFDIR | S_IRUGO | S_IWUSR);
1118         if (IS_ERR(dto))
1119                 GOTO(out_put, rc = PTR_ERR(dto));
1120
1121         lu_object_put(env, &dto->do_lu);
1122
1123         rc = lod_prepare_distribute_txn(env, lod);
1124         if (rc != 0)
1125                 GOTO(out_put, rc);
1126
1127         rc = lod_sub_init_llogs(env, lod);
1128         if (rc != 0)
1129                 GOTO(out_put, rc);
1130
1131 out_put:
1132         lu_object_put(env, &root->do_lu);
1133
1134         RETURN(rc);
1135 }
1136
1137 const struct lu_device_operations lod_lu_ops = {
1138         .ldo_object_alloc       = lod_object_alloc,
1139         .ldo_process_config     = lod_process_config,
1140         .ldo_recovery_complete  = lod_recovery_complete,
1141         .ldo_prepare            = lod_prepare,
1142 };
1143
1144 /**
1145  * Implementation of dt_device_operations::dt_root_get() for LOD
1146  *
1147  * see include/dt_object.h for the details.
1148  */
1149 static int lod_root_get(const struct lu_env *env,
1150                         struct dt_device *dev, struct lu_fid *f)
1151 {
1152         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1153 }
1154
1155 /**
1156  * Implementation of dt_device_operations::dt_statfs() for LOD
1157  *
1158  * see include/dt_object.h for the details.
1159  */
1160 static int lod_statfs(const struct lu_env *env,
1161                       struct dt_device *dev, struct obd_statfs *sfs)
1162 {
1163         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1164 }
1165
1166 /**
1167  * Implementation of dt_device_operations::dt_trans_create() for LOD
1168  *
1169  * Creates a transaction using local (to this node) OSD.
1170  *
1171  * see include/dt_object.h for the details.
1172  */
1173 static struct thandle *lod_trans_create(const struct lu_env *env,
1174                                         struct dt_device *dt)
1175 {
1176         struct thandle *th;
1177
1178         th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1179         if (IS_ERR(th))
1180                 return th;
1181
1182         th->th_dev = dt;
1183
1184         return th;
1185 }
1186
1187 /**
1188  * Implementation of dt_device_operations::dt_trans_start() for LOD
1189  *
1190  * Starts the set of local transactions using the targets involved
1191  * in declare phase. Initial support for the distributed transactions.
1192  *
1193  * see include/dt_object.h for the details.
1194  */
1195 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1196                            struct thandle *th)
1197 {
1198         return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
1199 }
1200
1201 static int lod_trans_cb_add(struct thandle *th,
1202                             struct dt_txn_commit_cb *dcb)
1203 {
1204         struct top_thandle      *top_th = container_of(th, struct top_thandle,
1205                                                        tt_super);
1206         return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1207 }
1208
1209 /**
1210  * add noop update to the update records
1211  *
1212  * Add noop updates to the update records, which is only used in
1213  * test right now.
1214  *
1215  * \param[in] env       execution environment
1216  * \param[in] dt        dt device of lod
1217  * \param[in] th        thandle
1218  * \param[in] count     the count of update records to be added.
1219  *
1220  * \retval              0 if adding succeeds.
1221  * \retval              negative errno if adding fails.
1222  */
1223 static int lod_add_noop_records(const struct lu_env *env,
1224                                 struct dt_device *dt, struct thandle *th,
1225                                 int count)
1226 {
1227         struct top_thandle *top_th;
1228         struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1229         int i;
1230         int rc = 0;
1231
1232         top_th = container_of(th, struct top_thandle, tt_super);
1233         if (top_th->tt_multiple_thandle == NULL)
1234                 return 0;
1235
1236         fid_zero(fid);
1237         for (i = 0; i < count; i++) {
1238                 rc = update_record_pack(noop, th, fid);
1239                 if (rc < 0)
1240                         return rc;
1241         }
1242         return rc;
1243 }
1244
1245 /**
1246  * Implementation of dt_device_operations::dt_trans_stop() for LOD
1247  *
1248  * Stops the set of local transactions using the targets involved
1249  * in declare phase. Initial support for the distributed transactions.
1250  *
1251  * see include/dt_object.h for the details.
1252  */
1253 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1254                           struct thandle *th)
1255 {
1256         if (OBD_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1257                 int rc;
1258
1259                 rc = lod_add_noop_records(env, dt, th, 5000);
1260                 if (rc < 0)
1261                         RETURN(rc);
1262         }
1263         return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1264 }
1265
1266 /**
1267  * Implementation of dt_device_operations::dt_conf_get() for LOD
1268  *
1269  * Currently returns the configuration provided by the local OSD.
1270  *
1271  * see include/dt_object.h for the details.
1272  */
1273 static void lod_conf_get(const struct lu_env *env,
1274                          const struct dt_device *dev,
1275                          struct dt_device_param *param)
1276 {
1277         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1278 }
1279
1280 /**
1281  * Implementation of dt_device_operations::dt_sync() for LOD
1282  *
1283  * Syncs all known OST targets. Very very expensive and used
1284  * rarely by LFSCK now. Should not be used in general.
1285  *
1286  * see include/dt_object.h for the details.
1287  */
1288 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1289 {
1290         struct lod_device   *lod = dt2lod_dev(dev);
1291         struct lod_ost_desc *ost;
1292         unsigned int         i;
1293         int                  rc = 0;
1294         ENTRY;
1295
1296         lod_getref(&lod->lod_ost_descs);
1297         lod_foreach_ost(lod, i) {
1298                 ost = OST_TGT(lod, i);
1299                 LASSERT(ost && ost->ltd_ost);
1300                 rc = dt_sync(env, ost->ltd_ost);
1301                 if (rc) {
1302                         CERROR("%s: can't sync %u: %d\n",
1303                                lod2obd(lod)->obd_name, i, rc);
1304                         break;
1305                 }
1306         }
1307         lod_putref(lod, &lod->lod_ost_descs);
1308         if (rc == 0)
1309                 rc = dt_sync(env, lod->lod_child);
1310
1311         RETURN(rc);
1312 }
1313
1314 /**
1315  * Implementation of dt_device_operations::dt_ro() for LOD
1316  *
1317  * Turns local OSD read-only, used for the testing only.
1318  *
1319  * see include/dt_object.h for the details.
1320  */
1321 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1322 {
1323         return dt_ro(env, dt2lod_dev(dev)->lod_child);
1324 }
1325
1326 /**
1327  * Implementation of dt_device_operations::dt_commit_async() for LOD
1328  *
1329  * Asks local OSD to commit sooner.
1330  *
1331  * see include/dt_object.h for the details.
1332  */
1333 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1334 {
1335         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1336 }
1337
1338 static const struct dt_device_operations lod_dt_ops = {
1339         .dt_root_get         = lod_root_get,
1340         .dt_statfs           = lod_statfs,
1341         .dt_trans_create     = lod_trans_create,
1342         .dt_trans_start      = lod_trans_start,
1343         .dt_trans_stop       = lod_trans_stop,
1344         .dt_conf_get         = lod_conf_get,
1345         .dt_sync             = lod_sync,
1346         .dt_ro               = lod_ro,
1347         .dt_commit_async     = lod_commit_async,
1348         .dt_trans_cb_add     = lod_trans_cb_add,
1349 };
1350
1351 /**
1352  * Connect to a local OSD.
1353  *
1354  * Used to connect to the local OSD at mount. OSD name is taken from the
1355  * configuration command passed. This connection is used to identify LU
1356  * site and pin the OSD from early removal.
1357  *
1358  * \param[in] env               LU environment provided by the caller
1359  * \param[in] lod               lod device
1360  * \param[in] cfg               configuration command to apply
1361  *
1362  * \retval 0                    on success
1363  * \retval negative             negated errno on error
1364  **/
1365 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1366                               struct lustre_cfg *cfg)
1367 {
1368         struct obd_connect_data *data = NULL;
1369         struct obd_device       *obd;
1370         char                    *nextdev = NULL, *p, *s;
1371         int                      rc, len = 0;
1372         ENTRY;
1373
1374         LASSERT(cfg);
1375         LASSERT(lod->lod_child_exp == NULL);
1376
1377         /* compatibility hack: we still use old config logs
1378          * which specify LOV, but we need to learn underlying
1379          * OSD device, which is supposed to be:
1380          *  <fsname>-MDTxxxx-osd
1381          *
1382          * 2.x MGS generates lines like the following:
1383          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
1384          * 1.8 MGS generates lines like the following:
1385          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
1386          *
1387          * we use "-MDT" to differentiate 2.x from 1.8 */
1388
1389         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1390                 len = strlen(p) + 6;
1391                 OBD_ALLOC(nextdev, len);
1392                 if (nextdev == NULL)
1393                         GOTO(out, rc = -ENOMEM);
1394
1395                 strcpy(nextdev, p);
1396                 s = strstr(nextdev, "-mdtlov");
1397                 if (unlikely(s == NULL)) {
1398                         CERROR("unable to parse device name %s\n",
1399                                lustre_cfg_string(cfg, 0));
1400                         GOTO(out, rc = -EINVAL);
1401                 }
1402
1403                 if (strstr(nextdev, "-MDT")) {
1404                         /* 2.x config */
1405                         strcpy(s, "-osd");
1406                 } else {
1407                         /* 1.8 config */
1408                         strcpy(s, "-MDT0000-osd");
1409                 }
1410         } else {
1411                 CERROR("unable to parse device name %s\n",
1412                        lustre_cfg_string(cfg, 0));
1413                 GOTO(out, rc = -EINVAL);
1414         }
1415
1416         OBD_ALLOC_PTR(data);
1417         if (data == NULL)
1418                 GOTO(out, rc = -ENOMEM);
1419
1420         obd = class_name2obd(nextdev);
1421         if (obd == NULL) {
1422                 CERROR("can not locate next device: %s\n", nextdev);
1423                 GOTO(out, rc = -ENOTCONN);
1424         }
1425
1426         data->ocd_connect_flags = OBD_CONNECT_VERSION;
1427         data->ocd_version = LUSTRE_VERSION_CODE;
1428
1429         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1430                          data, NULL);
1431         if (rc) {
1432                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1433                 GOTO(out, rc);
1434         }
1435
1436         lod->lod_dt_dev.dd_lu_dev.ld_site =
1437                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1438         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1439         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1440
1441 out:
1442         if (data)
1443                 OBD_FREE_PTR(data);
1444         if (nextdev)
1445                 OBD_FREE(nextdev, len);
1446         RETURN(rc);
1447 }
1448
1449 /**
1450  * Allocate and initialize target table.
1451  *
1452  * A helper function to initialize the target table and allocate
1453  * a bitmap of the available targets.
1454  *
1455  * \param[in] ltd               target's table to initialize
1456  *
1457  * \retval 0                    on success
1458  * \retval negative             negated errno on error
1459  **/
1460 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1461 {
1462         mutex_init(&ltd->ltd_mutex);
1463         init_rwsem(&ltd->ltd_rw_sem);
1464
1465         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1466          * added to the LOD, see lod_add_device() */
1467         ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1468         if (ltd->ltd_tgt_bitmap == NULL)
1469                 RETURN(-ENOMEM);
1470
1471         ltd->ltd_tgts_size  = 32;
1472         ltd->ltd_tgtnr      = 0;
1473
1474         ltd->ltd_death_row = 0;
1475         ltd->ltd_refcount  = 0;
1476         return 0;
1477 }
1478
1479 /**
1480  * Initialize LOD device at setup.
1481  *
1482  * Initializes the given LOD device using the original configuration command.
1483  * The function initiates a connection to the local OSD and initializes few
1484  * internal structures like pools, target tables, etc.
1485  *
1486  * \param[in] env               LU environment provided by the caller
1487  * \param[in] lod               lod device
1488  * \param[in] ldt               not used
1489  * \param[in] cfg               configuration command
1490  *
1491  * \retval 0                    on success
1492  * \retval negative             negated errno on error
1493  **/
1494 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1495                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
1496 {
1497         struct dt_device_param ddp;
1498         struct obd_device     *obd;
1499         int                    rc;
1500         ENTRY;
1501
1502         obd = class_name2obd(lustre_cfg_string(cfg, 0));
1503         if (obd == NULL) {
1504                 CERROR("Cannot find obd with name %s\n",
1505                        lustre_cfg_string(cfg, 0));
1506                 RETURN(-ENODEV);
1507         }
1508
1509         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1510         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1511         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1512         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1513
1514         rc = lod_connect_to_osd(env, lod, cfg);
1515         if (rc)
1516                 RETURN(rc);
1517
1518         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1519         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1520
1521         /* setup obd to be used with old lov code */
1522         rc = lod_pools_init(lod, cfg);
1523         if (rc)
1524                 GOTO(out_disconnect, rc);
1525
1526         rc = lod_procfs_init(lod);
1527         if (rc)
1528                 GOTO(out_pools, rc);
1529
1530         spin_lock_init(&lod->lod_desc_lock);
1531         spin_lock_init(&lod->lod_connects_lock);
1532         lod_tgt_desc_init(&lod->lod_mdt_descs);
1533         lod_tgt_desc_init(&lod->lod_ost_descs);
1534
1535         RETURN(0);
1536
1537 out_pools:
1538         lod_pools_fini(lod);
1539 out_disconnect:
1540         obd_disconnect(lod->lod_child_exp);
1541         RETURN(rc);
1542 }
1543
1544 /**
1545  * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1546  *
1547  * Releases the memory allocated for LOD device.
1548  *
1549  * see include/lu_object.h for the details.
1550  */
1551 static struct lu_device *lod_device_free(const struct lu_env *env,
1552                                          struct lu_device *lu)
1553 {
1554         struct lod_device *lod = lu2lod_dev(lu);
1555         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
1556         ENTRY;
1557
1558         LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
1559         dt_device_fini(&lod->lod_dt_dev);
1560         OBD_FREE_PTR(lod);
1561         RETURN(next);
1562 }
1563
1564 /**
1565  * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1566  *
1567  * Allocates LOD device and calls the helpers to initialize it.
1568  *
1569  * see include/lu_object.h for the details.
1570  */
1571 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1572                                           struct lu_device_type *type,
1573                                           struct lustre_cfg *lcfg)
1574 {
1575         struct lod_device *lod;
1576         struct lu_device  *lu_dev;
1577
1578         OBD_ALLOC_PTR(lod);
1579         if (lod == NULL) {
1580                 lu_dev = ERR_PTR(-ENOMEM);
1581         } else {
1582                 int rc;
1583
1584                 lu_dev = lod2lu_dev(lod);
1585                 dt_device_init(&lod->lod_dt_dev, type);
1586                 rc = lod_init0(env, lod, type, lcfg);
1587                 if (rc != 0) {
1588                         lod_device_free(env, lu_dev);
1589                         lu_dev = ERR_PTR(rc);
1590                 }
1591         }
1592
1593         return lu_dev;
1594 }
1595
1596 /**
1597  * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1598  *
1599  * Releases the internal resources used by LOD device.
1600  *
1601  * see include/lu_object.h for the details.
1602  */
1603 static struct lu_device *lod_device_fini(const struct lu_env *env,
1604                                          struct lu_device *d)
1605 {
1606         struct lod_device *lod = lu2lod_dev(d);
1607         int                rc;
1608         ENTRY;
1609
1610         lod_pools_fini(lod);
1611
1612         lod_procfs_fini(lod);
1613
1614         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1615         if (rc)
1616                 CERROR("%s:can not fini ost descs %d\n",
1617                         lod2obd(lod)->obd_name, rc);
1618
1619         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1620         if (rc)
1621                 CERROR("%s:can not fini mdt descs %d\n",
1622                         lod2obd(lod)->obd_name, rc);
1623
1624         RETURN(NULL);
1625 }
1626
1627 /**
1628  * Implementation of obd_ops::o_connect() for LOD
1629  *
1630  * Used to track all the users of this specific LOD device,
1631  * so the device stays up until the last user disconnected.
1632  *
1633  * \param[in] env               LU environment provided by the caller
1634  * \param[out] exp              export the caller will be using to access LOD
1635  * \param[in] obd               OBD device representing LOD device
1636  * \param[in] cluuid            unique identifier of the caller
1637  * \param[in] data              not used
1638  * \param[in] localdata         not used
1639  *
1640  * \retval 0                    on success
1641  * \retval negative             negated errno on error
1642  **/
1643 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1644                            struct obd_device *obd, struct obd_uuid *cluuid,
1645                            struct obd_connect_data *data, void *localdata)
1646 {
1647         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
1648         struct lustre_handle  conn;
1649         int                   rc;
1650         ENTRY;
1651
1652         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1653
1654         rc = class_connect(&conn, obd, cluuid);
1655         if (rc)
1656                 RETURN(rc);
1657
1658         *exp = class_conn2export(&conn);
1659
1660         spin_lock(&lod->lod_connects_lock);
1661         lod->lod_connects++;
1662         /* at the moment we expect the only user */
1663         LASSERT(lod->lod_connects == 1);
1664         spin_unlock(&lod->lod_connects_lock);
1665
1666         RETURN(0);
1667 }
1668
1669 /**
1670  *
1671  * Implementation of obd_ops::o_disconnect() for LOD
1672  *
1673  * When the caller doesn't need to use this LOD instance, it calls
1674  * obd_disconnect() and LOD releases corresponding export/reference count.
1675  * Once all the users gone, LOD device is released.
1676  *
1677  * \param[in] exp               export provided to the caller in obd_connect()
1678  *
1679  * \retval 0                    on success
1680  * \retval negative             negated errno on error
1681  **/
1682 static int lod_obd_disconnect(struct obd_export *exp)
1683 {
1684         struct obd_device *obd = exp->exp_obd;
1685         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1686         int                rc, release = 0;
1687         ENTRY;
1688
1689         /* Only disconnect the underlying layers on the final disconnect. */
1690         spin_lock(&lod->lod_connects_lock);
1691         lod->lod_connects--;
1692         if (lod->lod_connects != 0) {
1693                 /* why should there be more than 1 connect? */
1694                 spin_unlock(&lod->lod_connects_lock);
1695                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1696                        lod->lod_connects);
1697                 goto out;
1698         }
1699         spin_unlock(&lod->lod_connects_lock);
1700
1701         /* the last user of lod has gone, let's release the device */
1702         release = 1;
1703
1704 out:
1705         rc = class_disconnect(exp); /* bz 9811 */
1706
1707         if (rc == 0 && release)
1708                 class_manual_cleanup(obd);
1709         RETURN(rc);
1710 }
1711
1712 LU_KEY_INIT(lod, struct lod_thread_info);
1713
1714 static void lod_key_fini(const struct lu_context *ctx,
1715                 struct lu_context_key *key, void *data)
1716 {
1717         struct lod_thread_info *info = data;
1718         /* allocated in lod_get_lov_ea
1719          * XXX: this is overload, a tread may have such store but used only
1720          * once. Probably better would be pool of such stores per LOD.
1721          */
1722         if (info->lti_ea_store) {
1723                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1724                 info->lti_ea_store = NULL;
1725                 info->lti_ea_store_size = 0;
1726         }
1727         lu_buf_free(&info->lti_linkea_buf);
1728         OBD_FREE_PTR(info);
1729 }
1730
1731 /* context key: lod_thread_key */
1732 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1733
1734 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1735
1736 static struct lu_device_type_operations lod_device_type_ops = {
1737         .ldto_init           = lod_type_init,
1738         .ldto_fini           = lod_type_fini,
1739
1740         .ldto_start          = lod_type_start,
1741         .ldto_stop           = lod_type_stop,
1742
1743         .ldto_device_alloc   = lod_device_alloc,
1744         .ldto_device_free    = lod_device_free,
1745
1746         .ldto_device_fini    = lod_device_fini
1747 };
1748
1749 static struct lu_device_type lod_device_type = {
1750         .ldt_tags     = LU_DEVICE_DT,
1751         .ldt_name     = LUSTRE_LOD_NAME,
1752         .ldt_ops      = &lod_device_type_ops,
1753         .ldt_ctx_tags = LCT_MD_THREAD,
1754 };
1755
1756 /**
1757  * Implementation of obd_ops::o_get_info() for LOD
1758  *
1759  * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1760  * the caller binary status whether LOD has seen connection to any OST target.
1761  * It will also check if the MDT update log context being initialized (if
1762  * needed).
1763  *
1764  * \param[in] env               LU environment provided by the caller
1765  * \param[in] exp               export of the caller
1766  * \param[in] keylen            len of the key
1767  * \param[in] key               the key
1768  * \param[in] vallen            not used
1769  * \param[in] val               not used
1770  *
1771  * \retval                      0 if a connection was seen
1772  * \retval                      -EAGAIN if LOD isn't running yet or no
1773  *                              connection has been seen yet
1774  * \retval                      -EINVAL if not supported key is requested
1775  **/
1776 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1777                             __u32 keylen, void *key, __u32 *vallen, void *val)
1778 {
1779         int rc = -EINVAL;
1780
1781         if (KEY_IS(KEY_OSP_CONNECTED)) {
1782                 struct obd_device       *obd = exp->exp_obd;
1783                 struct lod_device       *d;
1784                 struct lod_tgt_desc     *tgt;
1785                 unsigned int            i;
1786                 int                     rc = 1;
1787
1788                 if (!obd->obd_set_up || obd->obd_stopping)
1789                         RETURN(-EAGAIN);
1790
1791                 d = lu2lod_dev(obd->obd_lu_dev);
1792                 lod_getref(&d->lod_ost_descs);
1793                 lod_foreach_ost(d, i) {
1794                         tgt = OST_TGT(d, i);
1795                         LASSERT(tgt && tgt->ltd_tgt);
1796                         rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1797                                           vallen, val);
1798                         /* one healthy device is enough */
1799                         if (rc == 0)
1800                                 break;
1801                 }
1802                 lod_putref(d, &d->lod_ost_descs);
1803
1804                 lod_getref(&d->lod_mdt_descs);
1805                 lod_foreach_mdt(d, i) {
1806                         struct llog_ctxt *ctxt;
1807
1808                         tgt = MDT_TGT(d, i);
1809                         LASSERT(tgt != NULL);
1810                         LASSERT(tgt->ltd_tgt != NULL);
1811                         if (!tgt->ltd_active)
1812                                 continue;
1813
1814                         ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
1815                                                 LLOG_UPDATELOG_ORIG_CTXT);
1816                         if (ctxt == NULL) {
1817                                 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1818                                        obd->obd_name,
1819                                       tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1820                                 rc = -EAGAIN;
1821                                 break;
1822                         }
1823                         if (ctxt->loc_handle == NULL) {
1824                                 CDEBUG(D_INFO, "%s: %s is not ready.\n",
1825                                        obd->obd_name,
1826                                       tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
1827                                 rc = -EAGAIN;
1828                                 llog_ctxt_put(ctxt);
1829                                 break;
1830                         }
1831                         llog_ctxt_put(ctxt);
1832                 }
1833                 lod_putref(d, &d->lod_mdt_descs);
1834
1835                 RETURN(rc);
1836         }
1837
1838         RETURN(rc);
1839 }
1840
1841 static struct obd_ops lod_obd_device_ops = {
1842         .o_owner        = THIS_MODULE,
1843         .o_connect      = lod_obd_connect,
1844         .o_disconnect   = lod_obd_disconnect,
1845         .o_get_info     = lod_obd_get_info,
1846         .o_pool_new     = lod_pool_new,
1847         .o_pool_rem     = lod_pool_remove,
1848         .o_pool_add     = lod_pool_add,
1849         .o_pool_del     = lod_pool_del,
1850 };
1851
1852 static int __init lod_mod_init(void)
1853 {
1854         struct obd_type *type;
1855         int rc;
1856
1857         rc = lu_kmem_init(lod_caches);
1858         if (rc)
1859                 return rc;
1860
1861         rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
1862                                  LUSTRE_LOD_NAME, &lod_device_type);
1863         if (rc) {
1864                 lu_kmem_fini(lod_caches);
1865                 return rc;
1866         }
1867
1868         /* create "lov" entry in procfs for compatibility purposes */
1869         type = class_search_type(LUSTRE_LOV_NAME);
1870         if (type != NULL && type->typ_procroot != NULL)
1871                 return rc;
1872
1873         type = class_search_type(LUSTRE_LOD_NAME);
1874         type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
1875                                              NULL, NULL);
1876         if (IS_ERR(type->typ_procsym)) {
1877                 CERROR("lod: can't create compat entry \"lov\": %d\n",
1878                        (int)PTR_ERR(type->typ_procsym));
1879                 type->typ_procsym = NULL;
1880         }
1881         return rc;
1882 }
1883
1884 static void __exit lod_mod_exit(void)
1885 {
1886         class_unregister_type(LUSTRE_LOD_NAME);
1887         lu_kmem_fini(lod_caches);
1888 }
1889
1890 MODULE_AUTHOR("Intel Corporation. <https://wiki.hpdd.intel.com/>");
1891 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
1892 MODULE_LICENSE("GPL");
1893
1894 module_init(lod_mod_init);
1895 module_exit(lod_mod_exit);
1896