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