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