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