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