Whamcloud - gitweb
a857df1e43f93228a80baef8e430cc7990763460
[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, 2017, 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 <uapi/linux/lustre/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
128         ENTRY;
129
130         if (!fid_is_sane(fid)) {
131                 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
132                        PFID(fid));
133                 RETURN(-EIO);
134         }
135
136         if (fid_is_idif(fid)) {
137                 *tgt = fid_idif_ost_idx(fid);
138                 *type = LU_SEQ_RANGE_OST;
139                 RETURN(0);
140         }
141
142         if (fid_is_update_log(fid) || fid_is_update_log_dir(fid)) {
143                 *tgt = fid_oid(fid);
144                 *type = LU_SEQ_RANGE_MDT;
145                 RETURN(0);
146         }
147
148         if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
149                 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
150
151                 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
152                 *type = LU_SEQ_RANGE_MDT;
153                 RETURN(0);
154         }
155
156         server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
157         if (!server_fld)
158                 RETURN(-EIO);
159
160         fld_range_set_type(&range, *type);
161         rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
162         if (rc != 0)
163                 RETURN(rc);
164
165         *tgt = range.lsr_index;
166         *type = range.lsr_flags;
167
168         CDEBUG(D_INFO, "%s: got tgt %x for sequence: %#llx\n",
169                lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
170
171         RETURN(0);
172 }
173
174 /* Slab for OSD object allocation */
175 struct kmem_cache *lod_object_kmem;
176
177 /* Slab for dt_txn_callback */
178 struct kmem_cache *lod_txn_callback_kmem;
179 static struct lu_kmem_descr lod_caches[] = {
180         {
181                 .ckd_cache = &lod_object_kmem,
182                 .ckd_name  = "lod_obj",
183                 .ckd_size  = sizeof(struct lod_object)
184         },
185         {
186                 .ckd_cache = &lod_txn_callback_kmem,
187                 .ckd_name  = "lod_txn_callback",
188                 .ckd_size  = sizeof(struct dt_txn_callback)
189         },
190         {
191                 .ckd_cache = NULL
192         }
193 };
194
195 static struct lu_device *lod_device_fini(const struct lu_env *env,
196                                          struct lu_device *d);
197
198 /**
199  * Implementation of lu_device_operations::ldo_object_alloc() for LOD
200  *
201  * Allocates and initializes LOD's slice in the given object.
202  *
203  * see include/lu_object.h for the details.
204  */
205 static struct lu_object *lod_object_alloc(const struct lu_env *env,
206                                           const struct lu_object_header *hdr,
207                                           struct lu_device *dev)
208 {
209         struct lod_object *lod_obj;
210         struct lu_object *lu_obj;
211
212         ENTRY;
213
214         OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
215         if (!lod_obj)
216                 RETURN(ERR_PTR(-ENOMEM));
217
218         mutex_init(&lod_obj->ldo_layout_mutex);
219         lu_obj = lod2lu_obj(lod_obj);
220         dt_object_init(&lod_obj->ldo_obj, NULL, dev);
221         lod_obj->ldo_obj.do_ops = &lod_obj_ops;
222         lu_obj->lo_ops = &lod_lu_obj_ops;
223
224         RETURN(lu_obj);
225 }
226
227 /**
228  * Process the config log for all sub device.
229  *
230  * The function goes through all the targets in the given table
231  * and apply given configuration command on to the targets.
232  * Used to cleanup the targets at unmount.
233  *
234  * \param[in] env               LU environment provided by the caller
235  * \param[in] lod               lod device
236  * \param[in] ltd               target's table to go through
237  * \param[in] lcfg              configuration command to apply
238  *
239  * \retval 0                    on success
240  * \retval negative             negated errno on error
241  **/
242 static int lod_sub_process_config(const struct lu_env *env,
243                                  struct lod_device *lod,
244                                  struct lod_tgt_descs *ltd,
245                                  struct lustre_cfg *lcfg)
246 {
247         struct lu_device *next;
248         struct lu_tgt_desc *tgt;
249         int rc = 0;
250
251         lod_getref(ltd);
252         ltd_foreach_tgt(ltd, tgt) {
253                 int rc1;
254
255                 LASSERT(tgt && tgt->ltd_tgt);
256                 next = &tgt->ltd_tgt->dd_lu_dev;
257                 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
258                 if (rc1) {
259                         CERROR("%s: error cleaning up LOD index %u: cmd %#x : rc = %d\n",
260                                lod2obd(lod)->obd_name, tgt->ltd_index,
261                                lcfg->lcfg_command, rc1);
262                         rc = rc1;
263                 }
264         }
265         lod_putref(lod, ltd);
266         return rc;
267 }
268
269 struct lod_recovery_data {
270         struct lod_device       *lrd_lod;
271         struct lod_tgt_desc     *lrd_ltd;
272         struct ptlrpc_thread    *lrd_thread;
273         u32                     lrd_idx;
274 };
275
276
277 /**
278  * process update recovery record
279  *
280  * Add the update recovery recode to the update recovery list in
281  * lod_recovery_data. Then the recovery thread (target_recovery_thread)
282  * will redo these updates.
283  *
284  * \param[in]env        execution environment
285  * \param[in]llh        log handle of update record
286  * \param[in]rec        update record to be replayed
287  * \param[in]data       update recovery data which holds the necessary
288  *                      arguments for recovery (see struct lod_recovery_data)
289  *
290  * \retval              0 if the record is processed successfully.
291  * \retval              negative errno if the record processing fails.
292  */
293 static int lod_process_recovery_updates(const struct lu_env *env,
294                                         struct llog_handle *llh,
295                                         struct llog_rec_hdr *rec,
296                                         void *data)
297 {
298         struct lod_recovery_data *lrd = data;
299         struct llog_cookie *cookie = &lod_env_info(env)->lti_cookie;
300         struct lu_target *lut;
301         u32 index = 0;
302
303         ENTRY;
304
305         if (!lrd->lrd_ltd) {
306                 int rc;
307
308                 rc = lodname2mdt_index(lod2obd(lrd->lrd_lod)->obd_name, &index);
309                 if (rc != 0)
310                         return rc;
311         } else {
312                 index = lrd->lrd_ltd->ltd_index;
313         }
314
315         if (rec->lrh_len !=
316                 llog_update_record_size((struct llog_update_record *)rec)) {
317                 CERROR("%s: broken update record! index %u "DFID".%u: rc = %d\n",
318                        lod2obd(lrd->lrd_lod)->obd_name, index,
319                        PFID(&llh->lgh_id.lgl_oi.oi_fid), rec->lrh_index, -EIO);
320                 return -EINVAL;
321         }
322
323         cookie->lgc_lgl = llh->lgh_id;
324         cookie->lgc_index = rec->lrh_index;
325         cookie->lgc_subsys = LLOG_UPDATELOG_ORIG_CTXT;
326
327         CDEBUG(D_HA, "%s: process recovery updates "DFID".%u\n",
328                lod2obd(lrd->lrd_lod)->obd_name,
329                PFID(&llh->lgh_id.lgl_oi.oi_fid), rec->lrh_index);
330         lut = lod2lu_dev(lrd->lrd_lod)->ld_site->ls_tgt;
331
332         if (lut->lut_obd->obd_stopping ||
333             lut->lut_obd->obd_abort_recovery)
334                 return -ESHUTDOWN;
335
336         return insert_update_records_to_replay_list(lut->lut_tdtd,
337                                         (struct llog_update_record *)rec,
338                                         cookie, index);
339 }
340
341 /**
342  * recovery thread for update log
343  *
344  * Start recovery thread and prepare the sub llog, then it will retrieve
345  * the update records from the correpondent MDT and do recovery.
346  *
347  * \param[in] arg       pointer to the recovery data
348  *
349  * \retval              0 if recovery succeeds
350  * \retval              negative errno if recovery failed.
351  */
352 static int lod_sub_recovery_thread(void *arg)
353 {
354         struct lod_recovery_data *lrd = arg;
355         struct lod_device *lod = lrd->lrd_lod;
356         struct dt_device *dt;
357         struct ptlrpc_thread *thread = lrd->lrd_thread;
358         struct llog_ctxt *ctxt = NULL;
359         struct lu_env env;
360         struct lu_target *lut;
361         struct lu_tgt_desc *mdt = NULL;
362         time64_t start;
363         int retries = 0;
364         int rc;
365
366         ENTRY;
367
368         thread->t_flags = SVC_RUNNING;
369         wake_up(&thread->t_ctl_waitq);
370
371         rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
372         if (rc != 0) {
373                 OBD_FREE_PTR(lrd);
374                 CERROR("%s: can't initialize env: rc = %d\n",
375                        lod2obd(lod)->obd_name, rc);
376                 RETURN(rc);
377         }
378
379         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
380         atomic_inc(&lut->lut_tdtd->tdtd_recovery_threads_count);
381         if (!lrd->lrd_ltd)
382                 dt = lod->lod_child;
383         else
384                 dt = lrd->lrd_ltd->ltd_tgt;
385
386         start = ktime_get_real_seconds();
387
388 again:
389         rc = lod_sub_prep_llog(&env, lod, dt, lrd->lrd_idx);
390         if (!rc && !lod->lod_child->dd_rdonly) {
391                 /* Process the recovery record */
392                 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
393                                         LLOG_UPDATELOG_ORIG_CTXT);
394                 LASSERT(ctxt != NULL);
395                 LASSERT(ctxt->loc_handle != NULL);
396
397                 rc = llog_cat_process(&env, ctxt->loc_handle,
398                                       lod_process_recovery_updates, lrd, 0, 0);
399         }
400
401         if (rc < 0) {
402                 struct lu_device *top_device;
403
404                 top_device = lod->lod_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
405                 /*
406                  * Because the remote target might failover at the same time,
407                  * let's retry here
408                  */
409                 if ((rc == -ETIMEDOUT || rc == -EAGAIN || rc == -EIO) &&
410                      dt != lod->lod_child &&
411                     !top_device->ld_obd->obd_abort_recovery &&
412                     !top_device->ld_obd->obd_stopping) {
413                         if (ctxt) {
414                                 if (ctxt->loc_handle)
415                                         llog_cat_close(&env,
416                                                        ctxt->loc_handle);
417                                 llog_ctxt_put(ctxt);
418                         }
419                         retries++;
420                         CDEBUG(D_HA, "%s get update log failed %d, retry\n",
421                                dt->dd_lu_dev.ld_obd->obd_name, rc);
422                         goto again;
423                 }
424
425                 CERROR("%s get update log failed: rc = %d\n",
426                        dt->dd_lu_dev.ld_obd->obd_name, rc);
427                 llog_ctxt_put(ctxt);
428
429                 spin_lock(&top_device->ld_obd->obd_dev_lock);
430                 if (!top_device->ld_obd->obd_abort_recovery &&
431                     !top_device->ld_obd->obd_stopping)
432                         top_device->ld_obd->obd_abort_recovery = 1;
433                 spin_unlock(&top_device->ld_obd->obd_dev_lock);
434
435                 GOTO(out, rc);
436         }
437         llog_ctxt_put(ctxt);
438
439         CDEBUG(D_HA, "%s retrieved update log, duration %lld, retries %d\n",
440                dt->dd_lu_dev.ld_obd->obd_name, ktime_get_real_seconds() - start,
441                retries);
442
443         spin_lock(&lod->lod_lock);
444         if (!lrd->lrd_ltd)
445                 lod->lod_child_got_update_log = 1;
446         else
447                 lrd->lrd_ltd->ltd_got_update_log = 1;
448
449         if (!lod->lod_child_got_update_log) {
450                 spin_unlock(&lod->lod_lock);
451                 GOTO(out, rc = 0);
452         }
453
454         lod_foreach_mdt(lod, mdt) {
455                 if (!mdt->ltd_got_update_log) {
456                         spin_unlock(&lod->lod_lock);
457                         GOTO(out, rc = 0);
458                 }
459         }
460         lut->lut_tdtd->tdtd_replay_ready = 1;
461         spin_unlock(&lod->lod_lock);
462
463         CDEBUG(D_HA, "%s got update logs from all MDTs.\n",
464                lut->lut_obd->obd_name);
465         wake_up(&lut->lut_obd->obd_next_transno_waitq);
466         EXIT;
467
468 out:
469         OBD_FREE_PTR(lrd);
470         thread->t_flags = SVC_STOPPED;
471         atomic_dec(&lut->lut_tdtd->tdtd_recovery_threads_count);
472         wake_up(&lut->lut_tdtd->tdtd_recovery_threads_waitq);
473         wake_up(&thread->t_ctl_waitq);
474         lu_env_fini(&env);
475         return rc;
476 }
477
478 /**
479  * finish sub llog context
480  *
481  * Stop update recovery thread for the sub device, then cleanup the
482  * correspondent llog ctxt.
483  *
484  * \param[in] env      execution environment
485  * \param[in] lod      lod device to do update recovery
486  * \param[in] thread   recovery thread on this sub device
487  */
488 void lod_sub_fini_llog(const struct lu_env *env,
489                        struct dt_device *dt, struct ptlrpc_thread *thread)
490 {
491         struct obd_device *obd;
492         struct llog_ctxt *ctxt;
493
494         ENTRY;
495
496         obd = dt->dd_lu_dev.ld_obd;
497         CDEBUG(D_INFO, "%s: finish sub llog\n", obd->obd_name);
498         /* Stop recovery thread first */
499         if (thread && thread->t_flags & SVC_RUNNING) {
500                 thread->t_flags = SVC_STOPPING;
501                 wake_up(&thread->t_ctl_waitq);
502                 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
503         }
504
505         ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
506         if (!ctxt)
507                 RETURN_EXIT;
508
509         if (ctxt->loc_handle)
510                 llog_cat_close(env, ctxt->loc_handle);
511
512         llog_cleanup(env, ctxt);
513
514         RETURN_EXIT;
515 }
516
517 /**
518  * Extract MDT target index from a device name.
519  *
520  * a helper function to extract index from the given device name
521  * like "fsname-MDTxxxx-mdtlov"
522  *
523  * \param[in] lodname           device name
524  * \param[out] mdt_index        extracted index
525  *
526  * \retval 0            on success
527  * \retval -EINVAL      if the name is invalid
528  */
529 int lodname2mdt_index(char *lodname, u32 *mdt_index)
530 {
531         unsigned long index;
532         char *ptr, *tmp;
533         int rc;
534
535         /* 1.8 configs don't have "-MDT0000" at the end */
536         ptr = strstr(lodname, "-MDT");
537         if (!ptr) {
538                 *mdt_index = 0;
539                 return 0;
540         }
541
542         ptr = strrchr(lodname, '-');
543         if (!ptr) {
544                 rc = -EINVAL;
545                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
546                 return rc;
547         }
548
549         if (strncmp(ptr, "-mdtlov", 7) != 0) {
550                 rc = -EINVAL;
551                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
552                 return rc;
553         }
554
555         if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
556                 rc = -EINVAL;
557                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
558                 return rc;
559         }
560
561         if (strncmp(ptr - 8, "-MDT", 4) != 0) {
562                 rc = -EINVAL;
563                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
564                 return rc;
565         }
566
567         index = simple_strtol(ptr - 4, &tmp, 16);
568         if (*tmp != '-' || index > INT_MAX) {
569                 rc = -EINVAL;
570                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
571                 return rc;
572         }
573         *mdt_index = index;
574         return 0;
575 }
576
577 /**
578  * Init sub llog context
579  *
580  * Setup update llog ctxt for update recovery threads, then start the
581  * recovery thread (lod_sub_recovery_thread) to read update llog from
582  * the correspondent MDT to do update recovery.
583  *
584  * \param[in] env       execution environment
585  * \param[in] lod       lod device to do update recovery
586  * \param[in] dt        sub dt device for which the recovery thread is
587  *
588  * \retval              0 if initialization succeeds.
589  * \retval              negative errno if initialization fails.
590  */
591 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
592                       struct dt_device *dt)
593 {
594         struct obd_device *obd;
595         struct lod_recovery_data *lrd = NULL;
596         struct ptlrpc_thread *thread;
597         struct task_struct *task;
598         struct l_wait_info lwi = { 0 };
599         struct lod_tgt_desc *subtgt = NULL;
600         u32 index;
601         u32 master_index;
602         int rc;
603
604         ENTRY;
605
606         rc = lodname2mdt_index(lod2obd(lod)->obd_name, &master_index);
607         if (rc != 0)
608                 RETURN(rc);
609
610         OBD_ALLOC_PTR(lrd);
611         if (!lrd)
612                 RETURN(-ENOMEM);
613
614         if (lod->lod_child == dt) {
615                 thread = &lod->lod_child_recovery_thread;
616                 index = master_index;
617         } else {
618                 struct lu_tgt_desc *mdt;
619
620                 lod_foreach_mdt(lod, mdt) {
621                         if (mdt->ltd_tgt == dt) {
622                                 index = mdt->ltd_index;
623                                 subtgt = mdt;
624                                 break;
625                         }
626                 }
627                 LASSERT(subtgt != NULL);
628                 OBD_ALLOC_PTR(subtgt->ltd_recovery_thread);
629                 if (!subtgt->ltd_recovery_thread)
630                         GOTO(free_lrd, rc = -ENOMEM);
631
632                 thread = subtgt->ltd_recovery_thread;
633         }
634
635         CDEBUG(D_INFO, "%s init sub log %s\n", lod2obd(lod)->obd_name,
636                dt->dd_lu_dev.ld_obd->obd_name);
637         lrd->lrd_lod = lod;
638         lrd->lrd_ltd = subtgt;
639         lrd->lrd_thread = thread;
640         lrd->lrd_idx = index;
641         init_waitqueue_head(&thread->t_ctl_waitq);
642
643         obd = dt->dd_lu_dev.ld_obd;
644         obd->obd_lvfs_ctxt.dt = dt;
645         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
646                         NULL, &llog_common_cat_ops);
647         if (rc < 0) {
648                 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
649                        obd->obd_name, rc);
650                 GOTO(free_thread, rc);
651         }
652
653         /* Start the recovery thread */
654         task = kthread_run(lod_sub_recovery_thread, lrd, "lod%04x_rec%04x",
655                            master_index, index);
656         if (IS_ERR(task)) {
657                 rc = PTR_ERR(task);
658                 CERROR("%s: cannot start recovery thread: rc = %d\n",
659                        obd->obd_name, rc);
660                 GOTO(out_llog, rc);
661         }
662
663         l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
664                                           thread->t_flags & SVC_STOPPED, &lwi);
665
666         RETURN(0);
667 out_llog:
668         lod_sub_fini_llog(env, dt, thread);
669 free_thread:
670         if (lod->lod_child != dt) {
671                 OBD_FREE_PTR(subtgt->ltd_recovery_thread);
672                 subtgt->ltd_recovery_thread = NULL;
673         }
674 free_lrd:
675         OBD_FREE_PTR(lrd);
676         RETURN(rc);
677 }
678
679 /**
680  * Stop sub recovery thread
681  *
682  * Stop sub recovery thread on all subs.
683  *
684  * \param[in] env       execution environment
685  * \param[in] lod       lod device to do update recovery
686  */
687 static void lod_sub_stop_recovery_threads(const struct lu_env *env,
688                                           struct lod_device *lod)
689 {
690         struct ptlrpc_thread *thread;
691         struct lu_tgt_desc *mdt;
692
693         /*
694          * Stop the update log commit cancel threads and finish master
695          * llog ctxt
696          */
697         thread = &lod->lod_child_recovery_thread;
698         /* Stop recovery thread first */
699         if (thread && thread->t_flags & SVC_RUNNING) {
700                 thread->t_flags = SVC_STOPPING;
701                 wake_up(&thread->t_ctl_waitq);
702                 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
703         }
704
705         lod_getref(&lod->lod_mdt_descs);
706         lod_foreach_mdt(lod, mdt) {
707                 thread = mdt->ltd_recovery_thread;
708                 if (thread && thread->t_flags & SVC_RUNNING) {
709                         thread->t_flags = SVC_STOPPING;
710                         wake_up(&thread->t_ctl_waitq);
711                         wait_event(thread->t_ctl_waitq,
712                                    thread->t_flags & SVC_STOPPED);
713                         OBD_FREE_PTR(mdt->ltd_recovery_thread);
714                         mdt->ltd_recovery_thread = NULL;
715                 }
716         }
717         lod_putref(lod, &lod->lod_mdt_descs);
718 }
719
720 /**
721  * finish all sub llog
722  *
723  * cleanup all of sub llog ctxt on the LOD.
724  *
725  * \param[in] env       execution environment
726  * \param[in] lod       lod device to do update recovery
727  */
728 static void lod_sub_fini_all_llogs(const struct lu_env *env,
729                                    struct lod_device *lod)
730 {
731         struct lu_tgt_desc *mdt;
732
733         /*
734          * Stop the update log commit cancel threads and finish master
735          * llog ctxt
736          */
737         lod_sub_fini_llog(env, lod->lod_child,
738                           &lod->lod_child_recovery_thread);
739         lod_getref(&lod->lod_mdt_descs);
740         lod_foreach_mdt(lod, mdt)
741                 lod_sub_fini_llog(env, mdt->ltd_tgt,
742                                   mdt->ltd_recovery_thread);
743         lod_putref(lod, &lod->lod_mdt_descs);
744 }
745
746 static char *lod_show_update_logs_retrievers(void *data, int *size, int *count)
747 {
748         struct lod_device *lod = (struct lod_device *)data;
749         struct lu_target *lut = lod2lu_dev(lod)->ld_site->ls_tgt;
750         struct lu_tgt_desc *mdt = NULL;
751         char *buf;
752         int len = 0;
753         int rc;
754         int i;
755
756         *count = atomic_read(&lut->lut_tdtd->tdtd_recovery_threads_count);
757         if (*count == 0) {
758                 *size = 0;
759                 return NULL;
760         }
761
762         *size = 5 * *count + 1;
763         OBD_ALLOC(buf, *size);
764         if (!buf)
765                 return NULL;
766
767         *count = 0;
768         memset(buf, 0, *size);
769
770         if (!lod->lod_child_got_update_log) {
771                 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i);
772                 LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc);
773
774                 rc = snprintf(buf + len, *size - len, " %04x", i);
775                 LASSERT(rc > 0);
776
777                 len += rc;
778                 (*count)++;
779         }
780
781         lod_foreach_mdt(lod, mdt) {
782                 if (!mdt->ltd_got_update_log) {
783                         rc = snprintf(buf + len, *size - len, " %04x",
784                                       mdt->ltd_index);
785                         if (unlikely(rc <= 0))
786                                 break;
787
788                         len += rc;
789                         (*count)++;
790                 }
791         }
792
793         return buf;
794 }
795
796 /**
797  * Prepare distribute txn
798  *
799  * Prepare distribute txn structure for LOD
800  *
801  * \param[in] env       execution environment
802  * \param[in] lod_device  LOD device
803  *
804  * \retval              0 if preparation succeeds.
805  * \retval              negative errno if preparation fails.
806  */
807 static int lod_prepare_distribute_txn(const struct lu_env *env,
808                                       struct lod_device *lod)
809 {
810         struct target_distribute_txn_data *tdtd;
811         struct lu_target *lut;
812         int rc;
813
814         ENTRY;
815
816         /* Init update recovery data */
817         OBD_ALLOC_PTR(tdtd);
818         if (!tdtd)
819                 RETURN(-ENOMEM);
820
821         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
822         tdtd->tdtd_dt = &lod->lod_dt_dev;
823         rc = distribute_txn_init(env, lut, tdtd,
824                 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
825
826         if (rc < 0) {
827                 CERROR("%s: cannot init distribute txn: rc = %d\n",
828                        lod2obd(lod)->obd_name, rc);
829                 OBD_FREE_PTR(tdtd);
830                 RETURN(rc);
831         }
832
833         tdtd->tdtd_show_update_logs_retrievers =
834                 lod_show_update_logs_retrievers;
835         tdtd->tdtd_show_retrievers_cbdata = lod;
836
837         lut->lut_tdtd = tdtd;
838
839         RETURN(0);
840 }
841
842 /**
843  * Finish distribute txn
844  *
845  * Release the resource holding by distribute txn, i.e. stop distribute
846  * txn thread.
847  *
848  * \param[in] env       execution environment
849  * \param[in] lod       lod device
850  */
851 static void lod_fini_distribute_txn(const struct lu_env *env,
852                                     struct lod_device *lod)
853 {
854         struct lu_target *lut;
855
856         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
857         target_recovery_fini(lut->lut_obd);
858         if (!lut->lut_tdtd)
859                 return;
860
861         distribute_txn_fini(env, lut->lut_tdtd);
862
863         OBD_FREE_PTR(lut->lut_tdtd);
864         lut->lut_tdtd = NULL;
865 }
866
867 /**
868  * Implementation of lu_device_operations::ldo_process_config() for LOD
869  *
870  * The method is called by the configuration subsystem during setup,
871  * cleanup and when the configuration changes. The method processes
872  * few specific commands like adding/removing the targets, changing
873  * the runtime parameters.
874
875  * \param[in] env               LU environment provided by the caller
876  * \param[in] dev               lod device
877  * \param[in] lcfg              configuration command to apply
878  *
879  * \retval 0                    on success
880  * \retval negative             negated errno on error
881  *
882  * The examples are below.
883  *
884  * Add osc config log:
885  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
886  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
887  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
888  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
889  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
890  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
891  *
892  * Add mdc config log:
893  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
894  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
895  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
896  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
897  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
898  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
899  */
900 static int lod_process_config(const struct lu_env *env,
901                               struct lu_device *dev,
902                               struct lustre_cfg *lcfg)
903 {
904         struct lod_device *lod = lu2lod_dev(dev);
905         struct lu_device *next = &lod->lod_child->dd_lu_dev;
906         char *arg1;
907         int rc = 0;
908
909         ENTRY;
910
911         switch (lcfg->lcfg_command) {
912         case LCFG_LOV_DEL_OBD:
913         case LCFG_LOV_ADD_INA:
914         case LCFG_LOV_ADD_OBD:
915         case LCFG_ADD_MDC: {
916                 u32 index;
917                 u32 mdt_index;
918                 int gen;
919                 /*
920                  * lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
921                  * modify_mdc_tgts add  0:lustre-MDT0001
922                  *                    1:lustre-MDT0001-mdc0002
923                  *                    2:2  3:1
924                  */
925                 arg1 = lustre_cfg_string(lcfg, 1);
926
927                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
928                         GOTO(out, rc = -EINVAL);
929                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
930                         GOTO(out, rc = -EINVAL);
931
932                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
933                         u32 mdt_index;
934
935                         rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
936                                                &mdt_index);
937                         if (rc != 0)
938                                 GOTO(out, rc);
939
940                         rc = lod_add_device(env, lod, arg1, index, gen,
941                                             mdt_index, LUSTRE_OSC_NAME, 1);
942                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
943                         mdt_index = index;
944                         rc = lod_add_device(env, lod, arg1, index, gen,
945                                             mdt_index, LUSTRE_MDC_NAME, 1);
946                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
947                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
948                         mdt_index = 0;
949                         rc = lod_add_device(env, lod, arg1, index, gen,
950                                             mdt_index, LUSTRE_OSC_NAME, 0);
951                 } else {
952                         rc = lod_del_device(env, lod, &lod->lod_ost_descs,
953                                             arg1, index, gen);
954                 }
955
956                 break;
957         }
958
959         case LCFG_PARAM: {
960                 struct obd_device *obd;
961                 ssize_t count;
962                 char *param;
963
964                 /*
965                  * Check if it is activate/deactivate mdc
966                  * lustre-MDTXXXX-osp-MDTXXXX.active=1
967                  */
968                 param = lustre_cfg_buf(lcfg, 1);
969                 if (strstr(param, "osp") && strstr(param, ".active=")) {
970                         struct lod_tgt_desc *sub_tgt = NULL;
971                         struct lu_tgt_desc *mdt;
972                         char *ptr;
973                         char *tmp;
974
975                         ptr = strstr(param, ".");
976                         *ptr = '\0';
977                         obd = class_name2obd(param);
978                         if (!obd) {
979                                 CERROR("%s: can not find %s: rc = %d\n",
980                                        lod2obd(lod)->obd_name, param, -EINVAL);
981                                 *ptr = '.';
982                                 GOTO(out, rc);
983                         }
984
985                         lod_foreach_mdt(lod, mdt) {
986                                 if (mdt->ltd_tgt->dd_lu_dev.ld_obd == obd) {
987                                         sub_tgt = mdt;
988                                         break;
989                                 }
990                         }
991
992                         if (!sub_tgt) {
993                                 CERROR("%s: can not find %s: rc = %d\n",
994                                        lod2obd(lod)->obd_name, param, -EINVAL);
995                                 *ptr = '.';
996                                 GOTO(out, rc);
997                         }
998
999                         *ptr = '.';
1000                         tmp = strstr(param, "=");
1001                         tmp++;
1002                         if (*tmp == '1') {
1003                                 struct llog_ctxt *ctxt;
1004
1005                                 obd = sub_tgt->ltd_tgt->dd_lu_dev.ld_obd;
1006                                 ctxt = llog_get_context(obd,
1007                                                 LLOG_UPDATELOG_ORIG_CTXT);
1008                                 if (!ctxt) {
1009                                         rc = llog_setup(env, obd, &obd->obd_olg,
1010                                                        LLOG_UPDATELOG_ORIG_CTXT,
1011                                                     NULL, &llog_common_cat_ops);
1012                                         if (rc < 0)
1013                                                 GOTO(out, rc);
1014                                 } else {
1015                                         llog_ctxt_put(ctxt);
1016                                 }
1017                                 rc = lod_sub_prep_llog(env, lod,
1018                                                        sub_tgt->ltd_tgt,
1019                                                        sub_tgt->ltd_index);
1020                                 if (rc == 0)
1021                                         sub_tgt->ltd_active = 1;
1022                         } else {
1023                                 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
1024                                                   NULL);
1025                                 sub_tgt->ltd_active = 0;
1026                         }
1027                         GOTO(out, rc);
1028                 }
1029
1030
1031                 if (strstr(param, PARAM_LOD) != NULL)
1032                         count = class_modify_config(lcfg, PARAM_LOD,
1033                                                     &lod->lod_dt_dev.dd_kobj);
1034                 else
1035                         count = class_modify_config(lcfg, PARAM_LOV,
1036                                                     &lod->lod_dt_dev.dd_kobj);
1037                 rc = count > 0 ? 0 : count;
1038                 GOTO(out, rc);
1039         }
1040         case LCFG_PRE_CLEANUP: {
1041                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1042                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1043                 next = &lod->lod_child->dd_lu_dev;
1044                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1045                 if (rc != 0)
1046                         CDEBUG(D_HA, "%s: can't process %u: %d\n",
1047                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1048
1049                 lod_sub_stop_recovery_threads(env, lod);
1050                 lod_fini_distribute_txn(env, lod);
1051                 lod_sub_fini_all_llogs(env, lod);
1052                 break;
1053         }
1054         case LCFG_CLEANUP: {
1055                 if (lod->lod_md_root) {
1056                         dt_object_put(env, &lod->lod_md_root->ldo_obj);
1057                         lod->lod_md_root = NULL;
1058                 }
1059
1060                 /*
1061                  * do cleanup on underlying storage only when
1062                  * all OSPs are cleaned up, as they use that OSD as well
1063                  */
1064                 lu_dev_del_linkage(dev->ld_site, dev);
1065                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1066                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1067                 next = &lod->lod_child->dd_lu_dev;
1068                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1069                 if (rc)
1070                         CERROR("%s: can't process %u: rc = %d\n",
1071                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1072
1073                 rc = obd_disconnect(lod->lod_child_exp);
1074                 if (rc)
1075                         CERROR("error in disconnect from storage: rc = %d\n",
1076                                rc);
1077                 break;
1078         }
1079         default:
1080                 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1081                        lcfg->lcfg_command);
1082                 rc = -EINVAL;
1083                 break;
1084         }
1085
1086 out:
1087         RETURN(rc);
1088 }
1089
1090 /**
1091  * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1092  *
1093  * The method is called once the recovery is complete. This implementation
1094  * distributes the notification to all the known targets.
1095  *
1096  * see include/lu_object.h for the details
1097  */
1098 static int lod_recovery_complete(const struct lu_env *env,
1099                                  struct lu_device *dev)
1100 {
1101         struct lod_device *lod = lu2lod_dev(dev);
1102         struct lu_device *next = &lod->lod_child->dd_lu_dev;
1103         struct lod_tgt_desc *tgt;
1104         int rc;
1105
1106         ENTRY;
1107
1108         LASSERT(lod->lod_recovery_completed == 0);
1109         lod->lod_recovery_completed = 1;
1110
1111         rc = next->ld_ops->ldo_recovery_complete(env, next);
1112
1113         lod_getref(&lod->lod_ost_descs);
1114         if (lod->lod_ost_descs.ltd_tgts_size > 0) {
1115                 lod_foreach_ost(lod, tgt) {
1116                         LASSERT(tgt && tgt->ltd_tgt);
1117                         next = &tgt->ltd_tgt->dd_lu_dev;
1118                         rc = next->ld_ops->ldo_recovery_complete(env, next);
1119                         if (rc)
1120                                 CERROR("%s: can't complete recovery on #%d: rc = %d\n",
1121                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1122                                        rc);
1123                 }
1124         }
1125         lod_putref(lod, &lod->lod_ost_descs);
1126         RETURN(rc);
1127 }
1128
1129 /**
1130  * Init update logs on all sub device
1131  *
1132  * LOD initialize update logs on all of sub devices. Because the initialization
1133  * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1134  * lod_object_init(), this API has to be called after LOD is initialized.
1135  * \param[in] env       execution environment
1136  * \param[in] lod       lod device
1137  *
1138  * \retval              0 if update log is initialized successfully.
1139  * \retval              negative errno if initialization fails.
1140  */
1141 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1142 {
1143         struct lu_tgt_desc *mdt;
1144         int rc;
1145
1146         ENTRY;
1147
1148         /*
1149          * llog must be setup after LOD is initialized, because llog
1150          * initialization include FLD lookup
1151          */
1152         LASSERT(lod->lod_initialized);
1153
1154         /* Init the llog in its own stack */
1155         rc = lod_sub_init_llog(env, lod, lod->lod_child);
1156         if (rc < 0)
1157                 RETURN(rc);
1158
1159         lod_foreach_mdt(lod, mdt) {
1160                 rc = lod_sub_init_llog(env, lod, mdt->ltd_tgt);
1161                 if (rc != 0)
1162                         break;
1163         }
1164
1165         RETURN(rc);
1166 }
1167
1168 /**
1169  * Implementation of lu_device_operations::ldo_prepare() for LOD
1170  *
1171  * see include/lu_object.h for the details.
1172  */
1173 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1174                        struct lu_device *cdev)
1175 {
1176         struct lod_device *lod = lu2lod_dev(cdev);
1177         struct lu_device *next = &lod->lod_child->dd_lu_dev;
1178         struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1179         int rc;
1180         struct dt_object *root;
1181         struct dt_object *dto;
1182         u32 index;
1183
1184         ENTRY;
1185
1186         rc = next->ld_ops->ldo_prepare(env, pdev, next);
1187         if (rc != 0) {
1188                 CERROR("%s: prepare bottom error: rc = %d\n",
1189                        lod2obd(lod)->obd_name, rc);
1190                 RETURN(rc);
1191         }
1192
1193         lod->lod_initialized = 1;
1194
1195         rc = dt_root_get(env, lod->lod_child, fid);
1196         if (rc < 0)
1197                 RETURN(rc);
1198
1199         root = dt_locate(env, lod->lod_child, fid);
1200         if (IS_ERR(root))
1201                 RETURN(PTR_ERR(root));
1202
1203         /* Create update log object */
1204         index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1205         lu_update_log_fid(fid, index);
1206
1207         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1208                                                  fid, root,
1209                                                  lod_update_log_name,
1210                                                  S_IFREG | 0644);
1211         if (IS_ERR(dto))
1212                 GOTO(out_put, rc = PTR_ERR(dto));
1213
1214         dt_object_put(env, dto);
1215
1216         /* Create update log dir */
1217         lu_update_log_dir_fid(fid, index);
1218         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1219                                                  fid, root,
1220                                                  lod_update_log_dir_name,
1221                                                  S_IFDIR | 0644);
1222         if (IS_ERR(dto))
1223                 GOTO(out_put, rc = PTR_ERR(dto));
1224
1225         dt_object_put(env, dto);
1226
1227         rc = lod_prepare_distribute_txn(env, lod);
1228         if (rc != 0)
1229                 GOTO(out_put, rc);
1230
1231         rc = lod_sub_init_llogs(env, lod);
1232         if (rc != 0)
1233                 GOTO(out_put, rc);
1234
1235 out_put:
1236         dt_object_put(env, root);
1237
1238         RETURN(rc);
1239 }
1240
1241 const struct lu_device_operations lod_lu_ops = {
1242         .ldo_object_alloc       = lod_object_alloc,
1243         .ldo_process_config     = lod_process_config,
1244         .ldo_recovery_complete  = lod_recovery_complete,
1245         .ldo_prepare            = lod_prepare,
1246 };
1247
1248 /**
1249  * Implementation of dt_device_operations::dt_root_get() for LOD
1250  *
1251  * see include/dt_object.h for the details.
1252  */
1253 static int lod_root_get(const struct lu_env *env,
1254                         struct dt_device *dev, struct lu_fid *f)
1255 {
1256         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1257 }
1258
1259 static void lod_statfs_sum(struct obd_statfs *sfs,
1260                              struct obd_statfs *ost_sfs, int *bs)
1261 {
1262         while (ost_sfs->os_bsize < *bs) {
1263                 *bs >>= 1;
1264                 sfs->os_bsize >>= 1;
1265                 sfs->os_bavail <<= 1;
1266                 sfs->os_blocks <<= 1;
1267                 sfs->os_bfree <<= 1;
1268                 sfs->os_granted <<= 1;
1269         }
1270         while (ost_sfs->os_bsize > *bs) {
1271                 ost_sfs->os_bsize >>= 1;
1272                 ost_sfs->os_bavail <<= 1;
1273                 ost_sfs->os_blocks <<= 1;
1274                 ost_sfs->os_bfree <<= 1;
1275                 ost_sfs->os_granted <<= 1;
1276         }
1277         sfs->os_bavail += ost_sfs->os_bavail;
1278         sfs->os_blocks += ost_sfs->os_blocks;
1279         sfs->os_bfree += ost_sfs->os_bfree;
1280         sfs->os_granted += ost_sfs->os_granted;
1281 }
1282
1283 /**
1284  * Implementation of dt_device_operations::dt_statfs() for LOD
1285  *
1286  * see include/dt_object.h for the details.
1287  */
1288 static int lod_statfs(const struct lu_env *env, struct dt_device *dev,
1289                       struct obd_statfs *sfs, struct obd_statfs_info *info)
1290 {
1291         struct lod_device *lod = dt2lod_dev(dev);
1292         struct lu_tgt_desc *tgt;
1293         struct obd_statfs ost_sfs;
1294         u64 ost_files = 0;
1295         u64 ost_ffree = 0;
1296         int rc, bs;
1297
1298         rc = dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1299         if (rc)
1300                 GOTO(out, rc);
1301
1302         bs = sfs->os_bsize;
1303
1304         sfs->os_bavail = 0;
1305         sfs->os_blocks = 0;
1306         sfs->os_bfree = 0;
1307         sfs->os_granted = 0;
1308
1309         lod_getref(&lod->lod_mdt_descs);
1310         lod_foreach_mdt(lod, tgt) {
1311                 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1312                 /* ignore errors */
1313                 if (rc)
1314                         continue;
1315                 sfs->os_files += ost_sfs.os_files;
1316                 sfs->os_ffree += ost_sfs.os_ffree;
1317                 lod_statfs_sum(sfs, &ost_sfs, &bs);
1318         }
1319         lod_putref(lod, &lod->lod_mdt_descs);
1320
1321         /*
1322          * at some point we can check whether DoM is enabled and
1323          * decide how to account MDT space. for simplicity let's
1324          * just fallback to pre-DoM policy if any OST is alive
1325          */
1326         lod_getref(&lod->lod_ost_descs);
1327         lod_foreach_ost(lod, tgt) {
1328                 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1329                 /* ignore errors */
1330                 if (rc || ost_sfs.os_bsize == 0)
1331                         continue;
1332                 if (!ost_files) {
1333                         /*
1334                          * if only MDTs with DoM then report only MDT blocks,
1335                          * otherwise show only OST blocks, and DoM is "free"
1336                          */
1337                         sfs->os_bavail = 0;
1338                         sfs->os_blocks = 0;
1339                         sfs->os_bfree = 0;
1340                         sfs->os_granted = 0;
1341                 }
1342                 ost_files += ost_sfs.os_files;
1343                 ost_ffree += ost_sfs.os_ffree;
1344                 ost_sfs.os_bavail += ost_sfs.os_granted;
1345                 lod_statfs_sum(sfs, &ost_sfs, &bs);
1346                 LASSERTF(bs == ost_sfs.os_bsize, "%d != %d\n",
1347                         (int)sfs->os_bsize, (int)ost_sfs.os_bsize);
1348         }
1349         lod_putref(lod, &lod->lod_ost_descs);
1350         sfs->os_state |= OS_STATE_SUM;
1351
1352         /* If we have _some_ OSTs, but don't have as many free objects on the
1353          * OSTs as inodes on the MDTs, reduce the reported number of inodes
1354          * to compensate, so that the "inodes in use" number is correct.
1355          * This should be kept in sync with ll_statfs_internal().
1356          */
1357         if (ost_files && ost_ffree < sfs->os_ffree) {
1358                 sfs->os_files = (sfs->os_files - sfs->os_ffree) + ost_ffree;
1359                 sfs->os_ffree = ost_ffree;
1360         }
1361
1362         /* a single successful statfs should be enough */
1363         rc = 0;
1364
1365 out:
1366         RETURN(rc);
1367 }
1368
1369 /**
1370  * Implementation of dt_device_operations::dt_trans_create() for LOD
1371  *
1372  * Creates a transaction using local (to this node) OSD.
1373  *
1374  * see include/dt_object.h for the details.
1375  */
1376 static struct thandle *lod_trans_create(const struct lu_env *env,
1377                                         struct dt_device *dt)
1378 {
1379         struct thandle *th;
1380
1381         th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1382         if (IS_ERR(th))
1383                 return th;
1384
1385         th->th_dev = dt;
1386
1387         return th;
1388 }
1389
1390 /**
1391  * Implementation of dt_device_operations::dt_trans_start() for LOD
1392  *
1393  * Starts the set of local transactions using the targets involved
1394  * in declare phase. Initial support for the distributed transactions.
1395  *
1396  * see include/dt_object.h for the details.
1397  */
1398 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1399                            struct thandle *th)
1400 {
1401         return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
1402 }
1403
1404 static int lod_trans_cb_add(struct thandle *th,
1405                             struct dt_txn_commit_cb *dcb)
1406 {
1407         struct top_thandle      *top_th = container_of(th, struct top_thandle,
1408                                                        tt_super);
1409         return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1410 }
1411
1412 /**
1413  * add noop update to the update records
1414  *
1415  * Add noop updates to the update records, which is only used in
1416  * test right now.
1417  *
1418  * \param[in] env       execution environment
1419  * \param[in] dt        dt device of lod
1420  * \param[in] th        thandle
1421  * \param[in] count     the count of update records to be added.
1422  *
1423  * \retval              0 if adding succeeds.
1424  * \retval              negative errno if adding fails.
1425  */
1426 static int lod_add_noop_records(const struct lu_env *env,
1427                                 struct dt_device *dt, struct thandle *th,
1428                                 int count)
1429 {
1430         struct top_thandle *top_th;
1431         struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1432         int i;
1433         int rc = 0;
1434
1435         top_th = container_of(th, struct top_thandle, tt_super);
1436         if (!top_th->tt_multiple_thandle)
1437                 return 0;
1438
1439         fid_zero(fid);
1440         for (i = 0; i < count; i++) {
1441                 rc = update_record_pack(noop, th, fid);
1442                 if (rc < 0)
1443                         return rc;
1444         }
1445         return rc;
1446 }
1447
1448 /**
1449  * Implementation of dt_device_operations::dt_trans_stop() for LOD
1450  *
1451  * Stops the set of local transactions using the targets involved
1452  * in declare phase. Initial support for the distributed transactions.
1453  *
1454  * see include/dt_object.h for the details.
1455  */
1456 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1457                           struct thandle *th)
1458 {
1459         if (OBD_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1460                 int rc;
1461
1462                 rc = lod_add_noop_records(env, dt, th, 5000);
1463                 if (rc < 0)
1464                         RETURN(rc);
1465         }
1466         return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1467 }
1468
1469 /**
1470  * Implementation of dt_device_operations::dt_conf_get() for LOD
1471  *
1472  * Currently returns the configuration provided by the local OSD.
1473  *
1474  * see include/dt_object.h for the details.
1475  */
1476 static void lod_conf_get(const struct lu_env *env,
1477                          const struct dt_device *dev,
1478                          struct dt_device_param *param)
1479 {
1480         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1481 }
1482
1483 /**
1484  * Implementation of dt_device_operations::dt_sync() for LOD
1485  *
1486  * Syncs all known OST targets. Very very expensive and used
1487  * rarely by LFSCK now. Should not be used in general.
1488  *
1489  * see include/dt_object.h for the details.
1490  */
1491 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1492 {
1493         struct lod_device *lod = dt2lod_dev(dev);
1494         struct lu_tgt_desc *tgt;
1495         int rc = 0;
1496
1497         ENTRY;
1498
1499         lod_getref(&lod->lod_ost_descs);
1500         lod_foreach_ost(lod, tgt) {
1501                 if (!tgt->ltd_active)
1502                         continue;
1503                 rc = dt_sync(env, tgt->ltd_tgt);
1504                 if (rc) {
1505                         if (rc != -ENOTCONN) {
1506                                 CERROR("%s: can't sync ost %u: rc = %d\n",
1507                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1508                                        rc);
1509                                 break;
1510                         }
1511                         rc = 0;
1512                 }
1513         }
1514         lod_putref(lod, &lod->lod_ost_descs);
1515
1516         if (rc)
1517                 RETURN(rc);
1518
1519         lod_getref(&lod->lod_mdt_descs);
1520         lod_foreach_mdt(lod, tgt) {
1521                 if (!tgt->ltd_active)
1522                         continue;
1523                 rc = dt_sync(env, tgt->ltd_tgt);
1524                 if (rc) {
1525                         if (rc != -ENOTCONN) {
1526                                 CERROR("%s: can't sync mdt %u: rc = %d\n",
1527                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1528                                        rc);
1529                                 break;
1530                         }
1531                         rc = 0;
1532                 }
1533         }
1534         lod_putref(lod, &lod->lod_mdt_descs);
1535
1536         if (rc == 0)
1537                 rc = dt_sync(env, lod->lod_child);
1538
1539         RETURN(rc);
1540 }
1541
1542 /**
1543  * Implementation of dt_device_operations::dt_ro() for LOD
1544  *
1545  * Turns local OSD read-only, used for the testing only.
1546  *
1547  * see include/dt_object.h for the details.
1548  */
1549 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1550 {
1551         return dt_ro(env, dt2lod_dev(dev)->lod_child);
1552 }
1553
1554 /**
1555  * Implementation of dt_device_operations::dt_commit_async() for LOD
1556  *
1557  * Asks local OSD to commit sooner.
1558  *
1559  * see include/dt_object.h for the details.
1560  */
1561 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1562 {
1563         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1564 }
1565
1566 static const struct dt_device_operations lod_dt_ops = {
1567         .dt_root_get         = lod_root_get,
1568         .dt_statfs           = lod_statfs,
1569         .dt_trans_create     = lod_trans_create,
1570         .dt_trans_start      = lod_trans_start,
1571         .dt_trans_stop       = lod_trans_stop,
1572         .dt_conf_get         = lod_conf_get,
1573         .dt_sync             = lod_sync,
1574         .dt_ro               = lod_ro,
1575         .dt_commit_async     = lod_commit_async,
1576         .dt_trans_cb_add     = lod_trans_cb_add,
1577 };
1578
1579 /**
1580  * Connect to a local OSD.
1581  *
1582  * Used to connect to the local OSD at mount. OSD name is taken from the
1583  * configuration command passed. This connection is used to identify LU
1584  * site and pin the OSD from early removal.
1585  *
1586  * \param[in] env               LU environment provided by the caller
1587  * \param[in] lod               lod device
1588  * \param[in] cfg               configuration command to apply
1589  *
1590  * \retval 0                    on success
1591  * \retval negative             negated errno on error
1592  **/
1593 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1594                               struct lustre_cfg *cfg)
1595 {
1596         struct obd_connect_data *data = NULL;
1597         struct obd_device *obd;
1598         char *nextdev = NULL, *p, *s;
1599         int rc, len = 0;
1600
1601         ENTRY;
1602
1603         LASSERT(cfg);
1604         LASSERT(lod->lod_child_exp == NULL);
1605
1606         /*
1607          * compatibility hack: we still use old config logs
1608          * which specify LOV, but we need to learn underlying
1609          * OSD device, which is supposed to be:
1610          *  <fsname>-MDTxxxx-osd
1611          *
1612          * 2.x MGS generates lines like the following:
1613          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
1614          * 1.8 MGS generates lines like the following:
1615          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
1616          *
1617          * we use "-MDT" to differentiate 2.x from 1.8
1618          */
1619         p = lustre_cfg_string(cfg, 0);
1620         if (p && strstr(p, "-mdtlov")) {
1621                 len = strlen(p) + 6;
1622                 OBD_ALLOC(nextdev, len);
1623                 if (!nextdev)
1624                         GOTO(out, rc = -ENOMEM);
1625
1626                 strcpy(nextdev, p);
1627                 s = strstr(nextdev, "-mdtlov");
1628                 if (unlikely(!s)) {
1629                         CERROR("%s: unable to parse device name: rc = %d\n",
1630                                lustre_cfg_string(cfg, 0), -EINVAL);
1631                         GOTO(out, rc = -EINVAL);
1632                 }
1633
1634                 if (strstr(nextdev, "-MDT")) {
1635                         /* 2.x config */
1636                         strcpy(s, "-osd");
1637                 } else {
1638                         /* 1.8 config */
1639                         strcpy(s, "-MDT0000-osd");
1640                 }
1641         } else {
1642                 CERROR("%s: unable to parse device name: rc = %d\n",
1643                        lustre_cfg_string(cfg, 0), -EINVAL);
1644                 GOTO(out, rc = -EINVAL);
1645         }
1646
1647         OBD_ALLOC_PTR(data);
1648         if (!data)
1649                 GOTO(out, rc = -ENOMEM);
1650
1651         obd = class_name2obd(nextdev);
1652         if (!obd) {
1653                 CERROR("%s: can not locate next device: rc = %d\n",
1654                        nextdev, -ENOTCONN);
1655                 GOTO(out, rc = -ENOTCONN);
1656         }
1657
1658         data->ocd_connect_flags = OBD_CONNECT_VERSION;
1659         data->ocd_version = LUSTRE_VERSION_CODE;
1660
1661         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1662                          data, NULL);
1663         if (rc) {
1664                 CERROR("%s: cannot connect to next dev: rc = %d\n",
1665                        nextdev, rc);
1666                 GOTO(out, rc);
1667         }
1668
1669         lod->lod_dt_dev.dd_lu_dev.ld_site =
1670                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1671         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1672         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1673
1674 out:
1675         if (data)
1676                 OBD_FREE_PTR(data);
1677         if (nextdev)
1678                 OBD_FREE(nextdev, len);
1679         RETURN(rc);
1680 }
1681
1682 /**
1683  * Initialize LOD device at setup.
1684  *
1685  * Initializes the given LOD device using the original configuration command.
1686  * The function initiates a connection to the local OSD and initializes few
1687  * internal structures like pools, target tables, etc.
1688  *
1689  * \param[in] env               LU environment provided by the caller
1690  * \param[in] lod               lod device
1691  * \param[in] ldt               not used
1692  * \param[in] cfg               configuration command
1693  *
1694  * \retval 0                    on success
1695  * \retval negative             negated errno on error
1696  **/
1697 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1698                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
1699 {
1700         struct dt_device_param ddp;
1701         struct obd_device *obd;
1702         int rc;
1703
1704         ENTRY;
1705
1706         obd = class_name2obd(lustre_cfg_string(cfg, 0));
1707         if (!obd) {
1708                 rc = -ENODEV;
1709                 CERROR("Cannot find obd with name '%s': rc = %d\n",
1710                        lustre_cfg_string(cfg, 0), rc);
1711                 RETURN(rc);
1712         }
1713
1714         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1715         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1716         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1717         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1718
1719         rc = lod_connect_to_osd(env, lod, cfg);
1720         if (rc)
1721                 RETURN(rc);
1722
1723         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1724         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1725         lod->lod_dom_max_stripesize = (1ULL << 20); /* 1Mb as default value */
1726
1727         /* setup obd to be used with old lov code */
1728         rc = lod_pools_init(lod, cfg);
1729         if (rc)
1730                 GOTO(out_disconnect, rc);
1731
1732         rc = lod_procfs_init(lod);
1733         if (rc)
1734                 GOTO(out_pools, rc);
1735
1736         spin_lock_init(&lod->lod_lock);
1737         spin_lock_init(&lod->lod_connects_lock);
1738         lu_tgt_descs_init(&lod->lod_mdt_descs, true);
1739         lu_tgt_descs_init(&lod->lod_ost_descs, false);
1740
1741         RETURN(0);
1742
1743 out_pools:
1744         lod_pools_fini(lod);
1745 out_disconnect:
1746         obd_disconnect(lod->lod_child_exp);
1747         RETURN(rc);
1748 }
1749
1750 /**
1751  * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1752  *
1753  * Releases the memory allocated for LOD device.
1754  *
1755  * see include/lu_object.h for the details.
1756  */
1757 static struct lu_device *lod_device_free(const struct lu_env *env,
1758                                          struct lu_device *lu)
1759 {
1760         struct lod_device *lod = lu2lod_dev(lu);
1761         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
1762
1763         ENTRY;
1764
1765         if (atomic_read(&lu->ld_ref) > 0 &&
1766             !cfs_hash_is_empty(lu->ld_site->ls_obj_hash)) {
1767                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1768                 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
1769         }
1770         LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
1771         dt_device_fini(&lod->lod_dt_dev);
1772         OBD_FREE_PTR(lod);
1773         RETURN(next);
1774 }
1775
1776 /**
1777  * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1778  *
1779  * Allocates LOD device and calls the helpers to initialize it.
1780  *
1781  * see include/lu_object.h for the details.
1782  */
1783 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1784                                           struct lu_device_type *type,
1785                                           struct lustre_cfg *lcfg)
1786 {
1787         struct lod_device *lod;
1788         struct lu_device *lu_dev;
1789
1790         OBD_ALLOC_PTR(lod);
1791         if (!lod) {
1792                 lu_dev = ERR_PTR(-ENOMEM);
1793         } else {
1794                 int rc;
1795
1796                 lu_dev = lod2lu_dev(lod);
1797                 dt_device_init(&lod->lod_dt_dev, type);
1798                 rc = lod_init0(env, lod, type, lcfg);
1799                 if (rc != 0) {
1800                         lod_device_free(env, lu_dev);
1801                         lu_dev = ERR_PTR(rc);
1802                 }
1803         }
1804
1805         return lu_dev;
1806 }
1807
1808 static void lod_avoid_guide_fini(struct lod_avoid_guide *lag)
1809 {
1810         if (lag->lag_oss_avoid_array)
1811                 OBD_FREE(lag->lag_oss_avoid_array,
1812                          sizeof(u32) * lag->lag_oaa_size);
1813         if (lag->lag_ost_avoid_bitmap)
1814                 CFS_FREE_BITMAP(lag->lag_ost_avoid_bitmap);
1815 }
1816
1817 /**
1818  * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1819  *
1820  * Releases the internal resources used by LOD device.
1821  *
1822  * see include/lu_object.h for the details.
1823  */
1824 static struct lu_device *lod_device_fini(const struct lu_env *env,
1825                                          struct lu_device *d)
1826 {
1827         struct lod_device *lod = lu2lod_dev(d);
1828         int rc;
1829
1830         ENTRY;
1831
1832         lod_pools_fini(lod);
1833
1834         lod_procfs_fini(lod);
1835
1836         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs);
1837         if (rc)
1838                 CERROR("%s: can not fini ost descriptors: rc =  %d\n",
1839                         lod2obd(lod)->obd_name, rc);
1840
1841         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs);
1842         if (rc)
1843                 CERROR("%s: can not fini mdt descriptors: rc =  %d\n",
1844                         lod2obd(lod)->obd_name, rc);
1845
1846         RETURN(NULL);
1847 }
1848
1849 /**
1850  * Implementation of obd_ops::o_connect() for LOD
1851  *
1852  * Used to track all the users of this specific LOD device,
1853  * so the device stays up until the last user disconnected.
1854  *
1855  * \param[in] env               LU environment provided by the caller
1856  * \param[out] exp              export the caller will be using to access LOD
1857  * \param[in] obd               OBD device representing LOD device
1858  * \param[in] cluuid            unique identifier of the caller
1859  * \param[in] data              not used
1860  * \param[in] localdata         not used
1861  *
1862  * \retval 0                    on success
1863  * \retval negative             negated errno on error
1864  **/
1865 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1866                            struct obd_device *obd, struct obd_uuid *cluuid,
1867                            struct obd_connect_data *data, void *localdata)
1868 {
1869         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1870         struct lustre_handle conn;
1871         int rc;
1872
1873         ENTRY;
1874
1875         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1876
1877         rc = class_connect(&conn, obd, cluuid);
1878         if (rc)
1879                 RETURN(rc);
1880
1881         *exp = class_conn2export(&conn);
1882
1883         spin_lock(&lod->lod_connects_lock);
1884         lod->lod_connects++;
1885         /* at the moment we expect the only user */
1886         LASSERT(lod->lod_connects == 1);
1887         spin_unlock(&lod->lod_connects_lock);
1888
1889         RETURN(0);
1890 }
1891
1892 /**
1893  *
1894  * Implementation of obd_ops::o_disconnect() for LOD
1895  *
1896  * When the caller doesn't need to use this LOD instance, it calls
1897  * obd_disconnect() and LOD releases corresponding export/reference count.
1898  * Once all the users gone, LOD device is released.
1899  *
1900  * \param[in] exp               export provided to the caller in obd_connect()
1901  *
1902  * \retval 0                    on success
1903  * \retval negative             negated errno on error
1904  **/
1905 static int lod_obd_disconnect(struct obd_export *exp)
1906 {
1907         struct obd_device *obd = exp->exp_obd;
1908         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1909         int rc, release = 0;
1910
1911         ENTRY;
1912
1913         /* Only disconnect the underlying layers on the final disconnect. */
1914         spin_lock(&lod->lod_connects_lock);
1915         lod->lod_connects--;
1916         if (lod->lod_connects != 0) {
1917                 /* why should there be more than 1 connect? */
1918                 spin_unlock(&lod->lod_connects_lock);
1919                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1920                        lod->lod_connects);
1921                 goto out;
1922         }
1923         spin_unlock(&lod->lod_connects_lock);
1924
1925         /* the last user of lod has gone, let's release the device */
1926         release = 1;
1927
1928 out:
1929         rc = class_disconnect(exp); /* bz 9811 */
1930
1931         if (rc == 0 && release)
1932                 class_manual_cleanup(obd);
1933         RETURN(rc);
1934 }
1935
1936 LU_KEY_INIT(lod, struct lod_thread_info);
1937
1938 static void lod_key_fini(const struct lu_context *ctx,
1939                 struct lu_context_key *key, void *data)
1940 {
1941         struct lod_thread_info *info = data;
1942         struct lod_layout_component *lds =
1943                                 info->lti_def_striping.lds_def_comp_entries;
1944
1945         /*
1946          * allocated in lod_get_lov_ea
1947          * XXX: this is overload, a tread may have such store but used only
1948          * once. Probably better would be pool of such stores per LOD.
1949          */
1950         if (info->lti_ea_store) {
1951                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1952                 info->lti_ea_store = NULL;
1953                 info->lti_ea_store_size = 0;
1954         }
1955         lu_buf_free(&info->lti_linkea_buf);
1956
1957         if (lds)
1958                 lod_free_def_comp_entries(&info->lti_def_striping);
1959
1960         if (info->lti_comp_size > 0)
1961                 OBD_FREE(info->lti_comp_idx,
1962                          info->lti_comp_size * sizeof(u32));
1963
1964         lod_avoid_guide_fini(&info->lti_avoid);
1965
1966         OBD_FREE_PTR(info);
1967 }
1968
1969 /* context key: lod_thread_key */
1970 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1971
1972 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1973
1974 static struct lu_device_type_operations lod_device_type_ops = {
1975         .ldto_init           = lod_type_init,
1976         .ldto_fini           = lod_type_fini,
1977
1978         .ldto_start          = lod_type_start,
1979         .ldto_stop           = lod_type_stop,
1980
1981         .ldto_device_alloc   = lod_device_alloc,
1982         .ldto_device_free    = lod_device_free,
1983
1984         .ldto_device_fini    = lod_device_fini
1985 };
1986
1987 static struct lu_device_type lod_device_type = {
1988         .ldt_tags     = LU_DEVICE_DT,
1989         .ldt_name     = LUSTRE_LOD_NAME,
1990         .ldt_ops      = &lod_device_type_ops,
1991         .ldt_ctx_tags = LCT_MD_THREAD,
1992 };
1993
1994 /**
1995  * Implementation of obd_ops::o_get_info() for LOD
1996  *
1997  * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1998  * the caller binary status whether LOD has seen connection to any OST target.
1999  * It will also check if the MDT update log context being initialized (if
2000  * needed).
2001  *
2002  * \param[in] env               LU environment provided by the caller
2003  * \param[in] exp               export of the caller
2004  * \param[in] keylen            len of the key
2005  * \param[in] key               the key
2006  * \param[in] vallen            not used
2007  * \param[in] val               not used
2008  *
2009  * \retval                      0 if a connection was seen
2010  * \retval                      -EAGAIN if LOD isn't running yet or no
2011  *                              connection has been seen yet
2012  * \retval                      -EINVAL if not supported key is requested
2013  **/
2014 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
2015                             u32 keylen, void *key, u32 *vallen, void *val)
2016 {
2017         int rc = -EINVAL;
2018
2019         if (KEY_IS(KEY_OSP_CONNECTED)) {
2020                 struct obd_device *obd = exp->exp_obd;
2021                 struct lod_device *d;
2022                 struct lod_tgt_desc *tgt;
2023                 int rc = 1;
2024
2025                 if (!obd->obd_set_up || obd->obd_stopping)
2026                         RETURN(-EAGAIN);
2027
2028                 d = lu2lod_dev(obd->obd_lu_dev);
2029                 lod_getref(&d->lod_ost_descs);
2030                 lod_foreach_ost(d, tgt) {
2031                         rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2032                                           vallen, val);
2033                         /* one healthy device is enough */
2034                         if (rc == 0)
2035                                 break;
2036                 }
2037                 lod_putref(d, &d->lod_ost_descs);
2038
2039                 lod_getref(&d->lod_mdt_descs);
2040                 lod_foreach_mdt(d, tgt) {
2041                         struct llog_ctxt *ctxt;
2042
2043                         if (!tgt->ltd_active)
2044                                 continue;
2045
2046                         ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
2047                                                 LLOG_UPDATELOG_ORIG_CTXT);
2048                         if (!ctxt) {
2049                                 CDEBUG(D_INFO, "%s: %s is not ready.\n",
2050                                        obd->obd_name,
2051                                       tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
2052                                 rc = -EAGAIN;
2053                                 break;
2054                         }
2055                         if (!ctxt->loc_handle) {
2056                                 CDEBUG(D_INFO, "%s: %s is not ready.\n",
2057                                        obd->obd_name,
2058                                       tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
2059                                 rc = -EAGAIN;
2060                                 llog_ctxt_put(ctxt);
2061                                 break;
2062                         }
2063                         llog_ctxt_put(ctxt);
2064                 }
2065                 lod_putref(d, &d->lod_mdt_descs);
2066
2067                 RETURN(rc);
2068         }
2069
2070         RETURN(rc);
2071 }
2072
2073 static int lod_obd_set_info_async(const struct lu_env *env,
2074                                   struct obd_export *exp,
2075                                   u32 keylen, void *key,
2076                                   u32 vallen, void *val,
2077                                   struct ptlrpc_request_set *set)
2078 {
2079         struct obd_device *obd = class_exp2obd(exp);
2080         struct lod_device *d;
2081         struct lod_tgt_desc *tgt;
2082         int no_set = 0;
2083         int rc = 0, rc2;
2084
2085         ENTRY;
2086
2087         if (!set) {
2088                 no_set = 1;
2089                 set = ptlrpc_prep_set();
2090                 if (!set)
2091                         RETURN(-ENOMEM);
2092         }
2093
2094         d = lu2lod_dev(obd->obd_lu_dev);
2095         lod_getref(&d->lod_ost_descs);
2096         lod_foreach_ost(d, tgt) {
2097                 if (!tgt->ltd_active)
2098                         continue;
2099
2100                 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2101                                          vallen, val, set);
2102                 if (rc2 != 0 && rc == 0)
2103                         rc = rc2;
2104         }
2105         lod_putref(d, &d->lod_ost_descs);
2106
2107         lod_getref(&d->lod_mdt_descs);
2108         lod_foreach_mdt(d, tgt) {
2109                 if (!tgt->ltd_active)
2110                         continue;
2111                 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2112                                          vallen, val, set);
2113                 if (rc2 != 0 && rc == 0)
2114                         rc = rc2;
2115         }
2116         lod_putref(d, &d->lod_mdt_descs);
2117
2118
2119         if (no_set) {
2120                 rc2 = ptlrpc_set_wait(env, set);
2121                 if (rc2 == 0 && rc == 0)
2122                         rc = rc2;
2123                 ptlrpc_set_destroy(set);
2124         }
2125         RETURN(rc);
2126 }
2127
2128 static struct obd_ops lod_obd_device_ops = {
2129         .o_owner        = THIS_MODULE,
2130         .o_connect      = lod_obd_connect,
2131         .o_disconnect   = lod_obd_disconnect,
2132         .o_get_info     = lod_obd_get_info,
2133         .o_set_info_async = lod_obd_set_info_async,
2134         .o_pool_new     = lod_pool_new,
2135         .o_pool_rem     = lod_pool_remove,
2136         .o_pool_add     = lod_pool_add,
2137         .o_pool_del     = lod_pool_del,
2138 };
2139
2140 static int __init lod_init(void)
2141 {
2142         struct obd_type *sym;
2143         int rc;
2144
2145         rc = lu_kmem_init(lod_caches);
2146         if (rc)
2147                 return rc;
2148
2149         rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
2150                                  LUSTRE_LOD_NAME, &lod_device_type);
2151         if (rc) {
2152                 lu_kmem_fini(lod_caches);
2153                 return rc;
2154         }
2155
2156         /* create "lov" entry for compatibility purposes */
2157         sym = class_add_symlinks(LUSTRE_LOV_NAME, true);
2158         if (IS_ERR(sym)) {
2159                 rc = PTR_ERR(sym);
2160                 /* does real "lov" already exist ? */
2161                 if (rc == -EEXIST)
2162                         rc = 0;
2163         }
2164
2165         return rc;
2166 }
2167
2168 static void __exit lod_exit(void)
2169 {
2170         struct obd_type *sym = class_search_type(LUSTRE_LOV_NAME);
2171
2172         /* if this was never fully initialized by the lov layer
2173          * then we are responsible for freeing this obd_type
2174          */
2175         if (sym) {
2176                 /* final put if we manage this obd type */
2177                 if (sym->typ_sym_filter)
2178                         kobject_put(&sym->typ_kobj);
2179                 /* put reference taken by class_search_type */
2180                 kobject_put(&sym->typ_kobj);
2181         }
2182
2183         class_unregister_type(LUSTRE_LOD_NAME);
2184         lu_kmem_fini(lod_caches);
2185 }
2186
2187 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2188 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2189 MODULE_VERSION(LUSTRE_VERSION_STRING);
2190 MODULE_LICENSE("GPL");
2191
2192 module_init(lod_init);
2193 module_exit(lod_exit);