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