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