Whamcloud - gitweb
LU-16634 obdclass: improve iocontrol error messages
[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  *
31  * lustre/lod/lod_dev.c
32  *
33  * Lustre Logical Object Device
34  *
35  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
36  * Author: Mikhail Pershin <mike.pershin@intel.com>
37  */
38 /**
39  * The Logical Object Device (LOD) layer manages access to striped
40  * objects (both regular files and directories). It implements the DT
41  * device and object APIs and is responsible for creating, storing,
42  * and loading striping information as an extended attribute of the
43  * underlying OSD object. LOD is the server side analog of the LOV and
44  * LMV layers on the client side.
45  *
46  * Metadata LU object stack (layers of the same compound LU object,
47  * all have the same FID):
48  *
49  *        MDT
50  *         |      MD API
51  *        MDD
52  *         |      DT API
53  *        LOD
54  *       /   \    DT API
55  *     OSD   OSP
56  *
57  * During LOD object initialization the localness or remoteness of the
58  * object FID dictates the choice between OSD and OSP.
59  *
60  * An LOD object (file or directory) with N stripes (each has a
61  * different FID):
62  *
63  *          LOD
64  *           |
65  *   +---+---+---+...+
66  *   |   |   |   |   |
67  *   S0  S1  S2  S3  S(N-1)  OS[DP] objects, seen as DT objects by LOD
68  *
69  * When upper layers must access an object's stripes (which are
70  * themselves OST or MDT LU objects) LOD finds these objects by their
71  * FIDs and stores them as an array of DT object pointers on the
72  * object. Declarations and operations on LOD objects are received by
73  * LOD (as DT object operations) and performed on the underlying
74  * OS[DP] object and (as needed) on the stripes. From the perspective
75  * of LOD, a stripe-less file (created by mknod() or open with
76  * O_LOV_DELAY_CREATE) is an object which does not yet have stripes,
77  * while a non-striped directory (created by mkdir()) is an object
78  * which will never have stripes.
79  *
80  * The LOD layer also implements a small subset of the OBD device API
81  * to support MDT stack initialization and finalization (an MDD device
82  * connects and disconnects itself to and from the underlying LOD
83  * device), and pool management. In turn LOD uses the OBD device API
84  * to connect it self to the underlying OSD, and to connect itself to
85  * OSP devices representing the MDTs and OSTs that bear the stripes of
86  * its objects.
87  */
88
89 #define DEBUG_SUBSYSTEM S_MDS
90
91 #include <linux/kthread.h>
92 #include <obd_class.h>
93 #include <md_object.h>
94 #include <lustre_fid.h>
95 #include <uapi/linux/lustre/lustre_param.h>
96 #include <uapi/linux/lustre/lustre_ioctl.h>
97 #include <lustre_update.h>
98 #include <lustre_log.h>
99 #include <lustre_lmv.h>
100 #include <llog_swab.h>
101
102 #include "lod_internal.h"
103
104 static const char lod_update_log_name[] = "update_log";
105 static const char lod_update_log_dir_name[] = "update_log_dir";
106
107 /*
108  * Lookup target by FID.
109  *
110  * Lookup MDT/OST target index by FID. Type of the target can be
111  * specific or any.
112  *
113  * \param[in] env               LU environment provided by the caller
114  * \param[in] lod               lod device
115  * \param[in] fid               FID
116  * \param[out] tgt              result target index
117  * \param[in] type              expected type of the target:
118  *                              LU_SEQ_RANGE_{MDT,OST,ANY}
119  *
120  * \retval 0                    on success
121  * \retval negative             negated errno on error
122  **/
123 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
124                    const struct lu_fid *fid, u32 *tgt, int *type)
125 {
126         struct lu_seq_range range = { 0 };
127         struct lu_server_fld *server_fld;
128         int rc;
129
130         ENTRY;
131
132         if (!fid_is_sane(fid)) {
133                 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
134                        PFID(fid));
135                 RETURN(-EIO);
136         }
137
138         if (fid_is_idif(fid)) {
139                 *tgt = fid_idif_ost_idx(fid);
140                 *type = LU_SEQ_RANGE_OST;
141                 RETURN(0);
142         }
143
144         if (fid_is_update_log(fid) || fid_is_update_log_dir(fid)) {
145                 *tgt = fid_oid(fid);
146                 *type = LU_SEQ_RANGE_MDT;
147                 RETURN(0);
148         }
149
150         if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
151                 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
152
153                 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
154                 *type = LU_SEQ_RANGE_MDT;
155                 RETURN(0);
156         }
157
158         server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
159         if (!server_fld)
160                 RETURN(-EIO);
161
162         fld_range_set_type(&range, *type);
163         rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
164         if (rc != 0)
165                 RETURN(rc);
166
167         *tgt = range.lsr_index;
168         *type = range.lsr_flags;
169
170         CDEBUG(D_INFO, "%s: got tgt %x for sequence: %#llx\n",
171                lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
172
173         RETURN(0);
174 }
175
176 /* Slab for OSD object allocation */
177 struct kmem_cache *lod_object_kmem;
178
179 /* Slab for dt_txn_callback */
180 struct kmem_cache *lod_txn_callback_kmem;
181 static struct lu_kmem_descr lod_caches[] = {
182         {
183                 .ckd_cache = &lod_object_kmem,
184                 .ckd_name  = "lod_obj",
185                 .ckd_size  = sizeof(struct lod_object)
186         },
187         {
188                 .ckd_cache = &lod_txn_callback_kmem,
189                 .ckd_name  = "lod_txn_callback",
190                 .ckd_size  = sizeof(struct dt_txn_callback)
191         },
192         {
193                 .ckd_cache = NULL
194         }
195 };
196
197 static struct lu_device *lod_device_fini(const struct lu_env *env,
198                                          struct lu_device *d);
199
200 /**
201  * Implementation of lu_device_operations::ldo_object_alloc() for LOD
202  *
203  * Allocates and initializes LOD's slice in the given object.
204  *
205  * see include/lu_object.h for the details.
206  */
207 static struct lu_object *lod_object_alloc(const struct lu_env *env,
208                                           const struct lu_object_header *hdr,
209                                           struct lu_device *dev)
210 {
211         struct lod_object *lod_obj;
212         struct lu_object *lu_obj;
213
214         ENTRY;
215
216         OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
217         if (!lod_obj)
218                 RETURN(ERR_PTR(-ENOMEM));
219
220         mutex_init(&lod_obj->ldo_layout_mutex);
221         lu_obj = lod2lu_obj(lod_obj);
222         dt_object_init(&lod_obj->ldo_obj, NULL, dev);
223         lod_obj->ldo_obj.do_ops = &lod_obj_ops;
224         lu_obj->lo_ops = &lod_lu_obj_ops;
225
226         RETURN(lu_obj);
227 }
228
229 /**
230  * Process the config log for all sub device.
231  *
232  * The function goes through all the targets in the given table
233  * and apply given configuration command on to the targets.
234  * Used to cleanup the targets at unmount.
235  *
236  * \param[in] env               LU environment provided by the caller
237  * \param[in] lod               lod device
238  * \param[in] ltd               target's table to go through
239  * \param[in] lcfg              configuration command to apply
240  *
241  * \retval 0                    on success
242  * \retval negative             negated errno on error
243  **/
244 static int lod_sub_process_config(const struct lu_env *env,
245                                  struct lod_device *lod,
246                                  struct lod_tgt_descs *ltd,
247                                  struct lustre_cfg *lcfg)
248 {
249         struct lu_device *next;
250         struct lu_tgt_desc *tgt;
251         int rc = 0;
252
253         lod_getref(ltd);
254         ltd_foreach_tgt(ltd, tgt) {
255                 int rc1;
256
257                 LASSERT(tgt && tgt->ltd_tgt);
258                 next = &tgt->ltd_tgt->dd_lu_dev;
259                 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
260                 if (rc1) {
261                         CERROR("%s: error cleaning up LOD index %u: cmd %#x : rc = %d\n",
262                                lod2obd(lod)->obd_name, tgt->ltd_index,
263                                lcfg->lcfg_command, rc1);
264                         rc = rc1;
265                 }
266         }
267         lod_putref(lod, ltd);
268         return rc;
269 }
270
271 struct lod_recovery_data {
272         struct lod_device       *lrd_lod;
273         struct lod_tgt_desc     *lrd_ltd;
274         struct task_struct      **lrd_task;
275         u32                     lrd_idx;
276         struct lu_env           lrd_env;
277         struct completion       *lrd_started;
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
306         ENTRY;
307
308         if (!lrd->lrd_ltd) {
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: rc = %d\n",
321                        lod2obd(lrd->lrd_lod)->obd_name, index,
322                        PLOGID(&llh->lgh_id), 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                PLOGID(&llh->lgh_id), rec->lrh_index);
333         lut = lod2lu_dev(lrd->lrd_lod)->ld_site->ls_tgt;
334
335         if (obd_mdt_recovery_abort(lut->lut_obd))
336                 return -ESHUTDOWN;
337
338         return insert_update_records_to_replay_list(lut->lut_tdtd,
339                                         (struct llog_update_record *)rec,
340                                         cookie, index);
341 }
342
343 /* retain old catalog, create new catalog and update catlist */
344 static int lod_sub_recreate_llog(const struct lu_env *env,
345                                  struct lod_device *lod, struct dt_device *dt,
346                                  int index)
347 {
348         struct lod_thread_info *lti = lod_env_info(env);
349         struct llog_ctxt *ctxt;
350         struct llog_handle *lgh;
351         struct llog_catid *cid = &lti->lti_cid;
352         struct lu_fid *fid = &lti->lti_fid;
353         struct obd_device *obd;
354         int rc;
355
356         ENTRY;
357         lu_update_log_fid(fid, index);
358         rc = lodname2mdt_index(lod2obd(lod)->obd_name, (__u32 *)&index);
359         if (rc < 0)
360                 RETURN(rc);
361
362         rc = llog_osd_get_cat_list(env, dt, index, 1, NULL, fid);
363         if (rc < 0) {
364                 CERROR("%s: can't access update_log: rc = %d\n",
365                        lod2obd(lod)->obd_name, rc);
366                 RETURN(rc);
367         }
368
369         obd = dt->dd_lu_dev.ld_obd;
370         ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
371         LASSERT(ctxt != NULL);
372         if (ctxt->loc_handle) {
373                 /* retain old catalog */
374                 llog_retain(env, ctxt->loc_handle);
375                 llog_cat_close(env, ctxt->loc_handle);
376                 LASSERT(!ctxt->loc_handle);
377         }
378
379         ctxt->loc_flags |= LLOG_CTXT_FLAG_NORMAL_FID;
380         ctxt->loc_chunk_size = LLOG_MIN_CHUNK_SIZE * 4;
381         rc = llog_open_create(env, ctxt, &lgh, NULL, NULL);
382         if (rc < 0)
383                 GOTO(out_put, rc);
384
385         LASSERT(lgh != NULL);
386         rc = llog_init_handle(env, lgh, LLOG_F_IS_CAT, NULL);
387         if (rc != 0)
388                 GOTO(out_close, rc);
389
390         cid->lci_logid = lgh->lgh_id;
391         rc = llog_osd_put_cat_list(env, dt, index, 1, cid, fid);
392         if (rc != 0)
393                 GOTO(out_close, rc);
394
395         ctxt->loc_handle = lgh;
396
397         CDEBUG(D_INFO, "%s: recreate catalog "DFID"\n",
398                obd->obd_name, PLOGID(&cid->lci_logid));
399 out_close:
400         if (rc)
401                 llog_cat_close(env, lgh);
402 out_put:
403         llog_ctxt_put(ctxt);
404         RETURN(rc);
405 }
406
407 /* retain update catalog and llogs, and create a new catalog */
408 static int lod_sub_cancel_llog(const struct lu_env *env,
409                                struct lod_device *lod, struct dt_device *dt,
410                                int index)
411 {
412         struct llog_ctxt *ctxt;
413         int rc = 0;
414
415         ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
416                                 LLOG_UPDATELOG_ORIG_CTXT);
417         if (!ctxt)
418                 return 0;
419
420         if (ctxt->loc_handle) {
421                 LCONSOLE(D_INFO, "%s: cancel update llog "DFID"\n",
422                          dt->dd_lu_dev.ld_obd->obd_name,
423                          PLOGID(&ctxt->loc_handle->lgh_id));
424                 /* set retention on logs to simplify reclamation */
425                 llog_process_or_fork(env, ctxt->loc_handle, llog_cat_retain_cb,
426                                      NULL, NULL, false);
427                 /* retain old catalog and create a new one */
428                 lod_sub_recreate_llog(env, lod, dt, index);
429         }
430         llog_ctxt_put(ctxt);
431         return rc;
432 }
433
434 /**
435  * recovery thread for update log
436  *
437  * Start recovery thread and prepare the sub llog, then it will retrieve
438  * the update records from the correpondent MDT and do recovery.
439  *
440  * \param[in] arg       pointer to the recovery data
441  *
442  * \retval              0 if recovery succeeds
443  * \retval              negative errno if recovery failed.
444  */
445 static int lod_sub_recovery_thread(void *arg)
446 {
447         struct lod_recovery_data *lrd = arg;
448         struct lod_device *lod = lrd->lrd_lod;
449         struct dt_device *dt;
450         struct llog_ctxt *ctxt = NULL;
451         struct lu_env *env = &lrd->lrd_env;
452         struct lu_target *lut;
453         struct lu_tgt_desc *mdt = NULL;
454         struct lu_device *top_device;
455         time64_t start;
456         int retries = 0;
457         int rc;
458
459         ENTRY;
460
461         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
462         atomic_inc(&lut->lut_tdtd->tdtd_recovery_threads_count);
463         if (!lrd->lrd_ltd)
464                 dt = lod->lod_child;
465         else
466                 dt = lrd->lrd_ltd->ltd_tgt;
467
468         start = ktime_get_real_seconds();
469         complete(lrd->lrd_started);
470
471 again:
472
473         if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_TGT_RECOVERY_CONNECT)) &&
474             lrd->lrd_ltd) {
475                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_RECOVERY_CONNECT, cfs_fail_val);
476                 rc = -EIO;
477         } else {
478                 rc = lod_sub_prep_llog(env, lod, dt, lrd->lrd_idx);
479         }
480
481         if (!rc && !lod->lod_child->dd_rdonly) {
482                 /* Process the recovery record */
483                 ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
484                                         LLOG_UPDATELOG_ORIG_CTXT);
485                 LASSERT(ctxt != NULL);
486                 LASSERT(ctxt->loc_handle != NULL);
487
488                 rc = llog_cat_process(env, ctxt->loc_handle,
489                                       lod_process_recovery_updates, lrd, 0, 0);
490         }
491
492         top_device = lod->lod_dt_dev.dd_lu_dev.ld_site->ls_top_dev;
493         if (rc < 0 && dt != lod->lod_child &&
494             !obd_mdt_recovery_abort(top_device->ld_obd)) {
495                 if (rc == -EBADR) {
496                         /* remote update llog is shorter than expected from
497                          * local header. Cached copy could be de-synced during
498                          * recovery, trust remote llog data
499                          */
500                         CDEBUG(D_HA, "%s update log data de-sync\n",
501                                dt->dd_lu_dev.ld_obd->obd_name);
502                         rc = 0;
503                 } else if (rc == -ETIMEDOUT || rc == -EAGAIN || rc == -EIO) {
504                         /*
505                          * the remote target might failover at the same time,
506                          * let's retry here
507                          */
508                         if (ctxt) {
509                                 if (ctxt->loc_handle)
510                                         llog_cat_close(env, ctxt->loc_handle);
511                                 llog_ctxt_put(ctxt);
512                                 ctxt = NULL;
513                         }
514                         retries++;
515                         CDEBUG(D_HA, "%s get update log failed %d, retry\n",
516                                dt->dd_lu_dev.ld_obd->obd_name, rc);
517                         goto again;
518                 }
519         }
520
521         llog_ctxt_put(ctxt);
522         if (rc < 0) {
523                 CERROR("%s: get update log duration %lld, retries %d, failed: rc = %d\n",
524                        dt->dd_lu_dev.ld_obd->obd_name,
525                        ktime_get_real_seconds() - start, retries, rc);
526                 /* abort MDT recovery of this target, but not all targets,
527                  * because recovery still has chance to succeed.
528                  */
529                 if (!obd_mdt_recovery_abort(top_device->ld_obd))
530                         lod_sub_cancel_llog(env, lod, dt, lrd->lrd_idx);
531         } else {
532                 CDEBUG(D_HA,
533                        "%s retrieved update log, duration %lld, retries %d\n",
534                        dt->dd_lu_dev.ld_obd->obd_name,
535                        ktime_get_real_seconds() - start, retries);
536         }
537
538         spin_lock(&lod->lod_lock);
539         if (!lrd->lrd_ltd)
540                 lod->lod_child_got_update_log = 1;
541         else
542                 lrd->lrd_ltd->ltd_got_update_log = 1;
543
544         if (!lod->lod_child_got_update_log) {
545                 spin_unlock(&lod->lod_lock);
546                 GOTO(out, rc);
547         }
548
549         lod_foreach_mdt(lod, mdt) {
550                 if (!mdt->ltd_got_update_log) {
551                         spin_unlock(&lod->lod_lock);
552                         GOTO(out, rc);
553                 }
554         }
555         lut->lut_tdtd->tdtd_replay_ready = 1;
556         spin_unlock(&lod->lod_lock);
557
558         CDEBUG(D_HA, "%s got update logs from all MDTs.\n",
559                lut->lut_obd->obd_name);
560         wake_up(&lut->lut_obd->obd_next_transno_waitq);
561         EXIT;
562
563 out:
564         atomic_dec(&lut->lut_tdtd->tdtd_recovery_threads_count);
565         wake_up(&lut->lut_tdtd->tdtd_recovery_threads_waitq);
566         if (xchg(lrd->lrd_task, NULL) == NULL)
567                 /* Someone is waiting for us to finish, need
568                  * to synchronize cleanly.
569                  */
570                 wait_var_event(lrd, kthread_should_stop());
571         lu_env_fini(env);
572         OBD_FREE_PTR(lrd);
573         return rc;
574 }
575
576 /**
577  * finish sub llog context
578  *
579  * Stop update recovery thread for the sub device, then cleanup the
580  * correspondent llog ctxt.
581  *
582  * \param[in] env      execution environment
583  * \param[in] lod      lod device to do update recovery
584  * \param[in] thread   recovery thread on this sub device
585  */
586 void lod_sub_fini_llog(const struct lu_env *env,
587                        struct dt_device *dt, struct task_struct **thread)
588 {
589         struct obd_device *obd;
590         struct llog_ctxt *ctxt;
591         struct task_struct *task = NULL;
592
593         ENTRY;
594
595         obd = dt->dd_lu_dev.ld_obd;
596         CDEBUG(D_INFO, "%s: finish sub llog\n", obd->obd_name);
597         /* Wait for recovery thread to complete */
598         if (thread)
599                 task = xchg(thread, NULL);
600         if (task)
601                 kthread_stop(task);
602
603         ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
604         if (!ctxt)
605                 RETURN_EXIT;
606
607         if (ctxt->loc_handle)
608                 llog_cat_close(env, ctxt->loc_handle);
609
610         llog_cleanup(env, ctxt);
611
612         RETURN_EXIT;
613 }
614
615 /**
616  * Extract MDT target index from a device name.
617  *
618  * a helper function to extract index from the given device name
619  * like "fsname-MDTxxxx-mdtlov"
620  *
621  * \param[in] lodname           device name
622  * \param[out] mdt_index        extracted index
623  *
624  * \retval 0            on success
625  * \retval -EINVAL      if the name is invalid
626  */
627 int lodname2mdt_index(char *lodname, u32 *mdt_index)
628 {
629         u32 index;
630         const char *ptr, *tmp;
631         int rc;
632
633         /* 1.8 configs don't have "-MDT0000" at the end */
634         ptr = strstr(lodname, "-MDT");
635         if (!ptr) {
636                 *mdt_index = 0;
637                 return 0;
638         }
639
640         ptr = strrchr(lodname, '-');
641         if (!ptr) {
642                 rc = -EINVAL;
643                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
644                 return rc;
645         }
646
647         if (strncmp(ptr, "-mdtlov", 7) != 0) {
648                 rc = -EINVAL;
649                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
650                 return rc;
651         }
652
653         if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
654                 rc = -EINVAL;
655                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
656                 return rc;
657         }
658
659         if (strncmp(ptr - 8, "-MDT", 4) != 0) {
660                 rc = -EINVAL;
661                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
662                 return rc;
663         }
664
665         rc = target_name2index(ptr - 7, &index, &tmp);
666         if (rc < 0 || rc & LDD_F_SV_ALL || *tmp != '-') {
667                 rc = -EINVAL;
668                 CERROR("invalid MDT index in '%s': rc = %d\n", lodname, rc);
669                 return rc;
670         }
671         *mdt_index = index;
672         return 0;
673 }
674
675 /**
676  * Init sub llog context
677  *
678  * Setup update llog ctxt for update recovery threads, then start the
679  * recovery thread (lod_sub_recovery_thread) to read update llog from
680  * the correspondent MDT to do update recovery.
681  *
682  * \param[in] env       execution environment
683  * \param[in] lod       lod device to do update recovery
684  * \param[in] dt        sub dt device for which the recovery thread is
685  *
686  * \retval              0 if initialization succeeds.
687  * \retval              negative errno if initialization fails.
688  */
689 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
690                       struct dt_device *dt)
691 {
692         struct obd_device *obd;
693         struct lod_recovery_data *lrd = NULL;
694         DECLARE_COMPLETION_ONSTACK(started);
695         struct task_struct **taskp;
696         struct task_struct *task;
697         struct lod_tgt_desc *subtgt = NULL;
698         u32 index;
699         u32 master_index;
700         int rc;
701
702         ENTRY;
703
704         rc = lodname2mdt_index(lod2obd(lod)->obd_name, &master_index);
705         if (rc != 0)
706                 RETURN(rc);
707
708         OBD_ALLOC_PTR(lrd);
709         if (!lrd)
710                 RETURN(-ENOMEM);
711
712         if (lod->lod_child == dt) {
713                 taskp = &lod->lod_child_recovery_task;
714                 index = master_index;
715         } else {
716                 struct lu_tgt_desc *mdt;
717
718                 lod_foreach_mdt(lod, mdt) {
719                         if (mdt->ltd_tgt == dt) {
720                                 index = mdt->ltd_index;
721                                 subtgt = mdt;
722                                 break;
723                         }
724                 }
725                 LASSERT(subtgt != NULL);
726                 taskp = &subtgt->ltd_recovery_task;
727         }
728
729         CDEBUG(D_INFO, "%s init sub log %s\n", lod2obd(lod)->obd_name,
730                dt->dd_lu_dev.ld_obd->obd_name);
731         lrd->lrd_lod = lod;
732         lrd->lrd_ltd = subtgt;
733         lrd->lrd_task = taskp;
734         lrd->lrd_idx = index;
735         lrd->lrd_started = &started;
736
737         obd = dt->dd_lu_dev.ld_obd;
738         obd->obd_lvfs_ctxt.dt = dt;
739         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
740                         NULL, &llog_common_cat_ops);
741         if (rc < 0) {
742                 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
743                        obd->obd_name, rc);
744                 GOTO(free_lrd, rc);
745         }
746
747         rc = lu_env_init(&lrd->lrd_env, LCT_LOCAL | LCT_MD_THREAD);
748         if (rc != 0) {
749                 CERROR("%s: can't initialize env: rc = %d\n",
750                        lod2obd(lod)->obd_name, rc);
751                 GOTO(free_lrd, rc);
752         }
753
754         /* Start the recovery thread */
755         task = kthread_create(lod_sub_recovery_thread, lrd, "lod%04x_rec%04x",
756                               master_index, index);
757         if (IS_ERR(task)) {
758                 rc = PTR_ERR(task);
759                 CERROR("%s: cannot start recovery thread: rc = %d\n",
760                        obd->obd_name, rc);
761                 lu_env_fini(&lrd->lrd_env);
762                 GOTO(out_llog, rc);
763         }
764         *taskp = task;
765         wake_up_process(task);
766         wait_for_completion(&started);
767
768         RETURN(0);
769 out_llog:
770         lod_sub_fini_llog(env, dt, taskp);
771 free_lrd:
772         OBD_FREE_PTR(lrd);
773         RETURN(rc);
774 }
775
776 /**
777  * Stop sub recovery thread
778  *
779  * Stop sub recovery thread on all subs.
780  *
781  * \param[in] env       execution environment
782  * \param[in] lod       lod device to do update recovery
783  */
784 static void lod_sub_stop_recovery_threads(const struct lu_env *env,
785                                           struct lod_device *lod)
786 {
787         struct task_struct *task;
788         struct lu_tgt_desc *mdt;
789
790         /*
791          * Stop the update log commit cancel threads and finish master
792          * llog ctxt
793          */
794         task = xchg(&lod->lod_child_recovery_task, NULL);
795         if (task)
796                 kthread_stop(task);
797
798         lod_getref(&lod->lod_mdt_descs);
799         lod_foreach_mdt(lod, mdt) {
800                 task = xchg(&mdt->ltd_recovery_task, NULL);
801                 if (task)
802                         kthread_stop(task);
803         }
804         lod_putref(lod, &lod->lod_mdt_descs);
805 }
806
807 /**
808  * finish all sub llog
809  *
810  * cleanup all of sub llog ctxt on the LOD.
811  *
812  * \param[in] env       execution environment
813  * \param[in] lod       lod device to do update recovery
814  */
815 static void lod_sub_fini_all_llogs(const struct lu_env *env,
816                                    struct lod_device *lod)
817 {
818         struct lu_tgt_desc *mdt;
819
820         /*
821          * Stop the update log commit cancel threads and finish master
822          * llog ctxt
823          */
824         lod_sub_fini_llog(env, lod->lod_child,
825                           &lod->lod_child_recovery_task);
826         lod_getref(&lod->lod_mdt_descs);
827         lod_foreach_mdt(lod, mdt)
828                 lod_sub_fini_llog(env, mdt->ltd_tgt,
829                                   &mdt->ltd_recovery_task);
830         lod_putref(lod, &lod->lod_mdt_descs);
831 }
832
833 static char *lod_show_update_logs_retrievers(void *data, int *size, int *count)
834 {
835         struct lod_device *lod = (struct lod_device *)data;
836         struct lu_target *lut = lod2lu_dev(lod)->ld_site->ls_tgt;
837         struct lu_tgt_desc *mdt = NULL;
838         char *buf;
839         int len = 0;
840         int rc;
841         int i;
842
843         *count = atomic_read(&lut->lut_tdtd->tdtd_recovery_threads_count);
844         if (*count == 0) {
845                 *size = 0;
846                 return NULL;
847         }
848
849         *size = 5 * *count + 1;
850         OBD_ALLOC(buf, *size);
851         if (!buf)
852                 return NULL;
853
854         *count = 0;
855         memset(buf, 0, *size);
856
857         if (!lod->lod_child_got_update_log) {
858                 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &i);
859                 LASSERTF(rc == 0, "Fail to parse target index: rc = %d\n", rc);
860
861                 rc = scnprintf(buf + len, *size - len, " %04x", i);
862                 LASSERT(rc > 0);
863
864                 len += rc;
865                 (*count)++;
866         }
867
868         lod_foreach_mdt(lod, mdt) {
869                 if (!mdt->ltd_got_update_log) {
870                         rc = scnprintf(buf + len, *size - len, " %04x",
871                                        mdt->ltd_index);
872                         if (unlikely(rc <= 0))
873                                 break;
874
875                         len += rc;
876                         (*count)++;
877                 }
878         }
879
880         return buf;
881 }
882
883 /**
884  * Prepare distribute txn
885  *
886  * Prepare distribute txn structure for LOD
887  *
888  * \param[in] env       execution environment
889  * \param[in] lod_device  LOD device
890  *
891  * \retval              0 if preparation succeeds.
892  * \retval              negative errno if preparation fails.
893  */
894 static int lod_prepare_distribute_txn(const struct lu_env *env,
895                                       struct lod_device *lod)
896 {
897         struct target_distribute_txn_data *tdtd;
898         struct lu_target *lut;
899         int rc;
900
901         ENTRY;
902
903         /* Init update recovery data */
904         OBD_ALLOC_PTR(tdtd);
905         if (!tdtd)
906                 RETURN(-ENOMEM);
907
908         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
909         tdtd->tdtd_dt = &lod->lod_dt_dev;
910         rc = distribute_txn_init(env, lut, tdtd,
911                 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
912
913         if (rc < 0) {
914                 CERROR("%s: cannot init distribute txn: rc = %d\n",
915                        lod2obd(lod)->obd_name, rc);
916                 OBD_FREE_PTR(tdtd);
917                 RETURN(rc);
918         }
919
920         tdtd->tdtd_show_update_logs_retrievers =
921                 lod_show_update_logs_retrievers;
922         tdtd->tdtd_show_retrievers_cbdata = lod;
923
924         lut->lut_tdtd = tdtd;
925
926         RETURN(0);
927 }
928
929 /**
930  * Finish distribute txn
931  *
932  * Release the resource holding by distribute txn, i.e. stop distribute
933  * txn thread.
934  *
935  * \param[in] env       execution environment
936  * \param[in] lod       lod device
937  */
938 static void lod_fini_distribute_txn(const struct lu_env *env,
939                                     struct lod_device *lod)
940 {
941         struct lu_target *lut;
942
943         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
944         target_recovery_fini(lut->lut_obd);
945         if (!lut->lut_tdtd)
946                 return;
947
948         distribute_txn_fini(env, lut->lut_tdtd);
949
950         OBD_FREE_PTR(lut->lut_tdtd);
951         lut->lut_tdtd = NULL;
952 }
953
954 /**
955  * Implementation of lu_device_operations::ldo_process_config() for LOD
956  *
957  * The method is called by the configuration subsystem during setup,
958  * cleanup and when the configuration changes. The method processes
959  * few specific commands like adding/removing the targets, changing
960  * the runtime parameters.
961
962  * \param[in] env               LU environment provided by the caller
963  * \param[in] dev               lod device
964  * \param[in] lcfg              configuration command to apply
965  *
966  * \retval 0                    on success
967  * \retval negative             negated errno on error
968  *
969  * The examples are below.
970  *
971  * Add osc config log:
972  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
973  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
974  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
975  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
976  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
977  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
978  *
979  * Add mdc config log:
980  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
981  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
982  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
983  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
984  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
985  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
986  */
987 static int lod_process_config(const struct lu_env *env,
988                               struct lu_device *dev,
989                               struct lustre_cfg *lcfg)
990 {
991         struct lod_device *lod = lu2lod_dev(dev);
992         struct lu_device *next = &lod->lod_child->dd_lu_dev;
993         char *arg1;
994         int rc = 0;
995
996         ENTRY;
997
998         switch (lcfg->lcfg_command) {
999         case LCFG_LOV_DEL_OBD:
1000         case LCFG_LOV_ADD_INA:
1001         case LCFG_LOV_ADD_OBD:
1002         case LCFG_ADD_MDC: {
1003                 u32 index;
1004                 u32 mdt_index;
1005                 int gen;
1006                 /*
1007                  * lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
1008                  * modify_mdc_tgts add  0:lustre-MDT0001
1009                  *                    1:lustre-MDT0001-mdc0002
1010                  *                    2:2  3:1
1011                  */
1012                 arg1 = lustre_cfg_string(lcfg, 1);
1013
1014                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
1015                         GOTO(out, rc = -EINVAL);
1016                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
1017                         GOTO(out, rc = -EINVAL);
1018
1019                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
1020                         u32 mdt_index;
1021
1022                         rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
1023                                                &mdt_index);
1024                         if (rc != 0)
1025                                 GOTO(out, rc);
1026
1027                         rc = lod_add_device(env, lod, arg1, index, gen,
1028                                             mdt_index, LUSTRE_OSC_NAME, 1);
1029                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
1030                         mdt_index = index;
1031                         rc = lod_add_device(env, lod, arg1, index, gen,
1032                                             mdt_index, LUSTRE_MDC_NAME, 1);
1033                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
1034                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
1035                         mdt_index = 0;
1036                         rc = lod_add_device(env, lod, arg1, index, gen,
1037                                             mdt_index, LUSTRE_OSC_NAME, 0);
1038                 } else {
1039                         rc = lod_del_device(env, lod, &lod->lod_ost_descs,
1040                                             arg1, index, gen);
1041                 }
1042
1043                 break;
1044         }
1045
1046         case LCFG_PARAM: {
1047                 struct obd_device *obd;
1048                 ssize_t count;
1049                 char *param;
1050
1051                 /*
1052                  * Check if it is activate/deactivate mdc
1053                  * lustre-MDTXXXX-osp-MDTXXXX.active=1
1054                  */
1055                 param = lustre_cfg_buf(lcfg, 1);
1056                 if (strstr(param, "osp") && strstr(param, ".active=")) {
1057                         struct lod_tgt_desc *sub_tgt = NULL;
1058                         struct lu_tgt_desc *mdt;
1059                         char *ptr;
1060                         char *tmp;
1061
1062                         ptr = strstr(param, ".");
1063                         *ptr = '\0';
1064                         obd = class_name2obd(param);
1065                         if (!obd) {
1066                                 CERROR("%s: can not find %s: rc = %d\n",
1067                                        lod2obd(lod)->obd_name, param, -EINVAL);
1068                                 *ptr = '.';
1069                                 GOTO(out, rc);
1070                         }
1071
1072                         lod_foreach_mdt(lod, mdt) {
1073                                 if (mdt->ltd_tgt->dd_lu_dev.ld_obd == obd) {
1074                                         sub_tgt = mdt;
1075                                         break;
1076                                 }
1077                         }
1078
1079                         if (!sub_tgt) {
1080                                 CERROR("%s: can not find %s: rc = %d\n",
1081                                        lod2obd(lod)->obd_name, param, -EINVAL);
1082                                 *ptr = '.';
1083                                 GOTO(out, rc);
1084                         }
1085
1086                         *ptr = '.';
1087                         tmp = strstr(param, "=");
1088                         tmp++;
1089                         if (*tmp == '1') {
1090                                 struct llog_ctxt *ctxt;
1091
1092                                 obd = sub_tgt->ltd_tgt->dd_lu_dev.ld_obd;
1093                                 ctxt = llog_get_context(obd,
1094                                                 LLOG_UPDATELOG_ORIG_CTXT);
1095                                 if (!ctxt) {
1096                                         rc = llog_setup(env, obd, &obd->obd_olg,
1097                                                        LLOG_UPDATELOG_ORIG_CTXT,
1098                                                     NULL, &llog_common_cat_ops);
1099                                         if (rc < 0)
1100                                                 GOTO(out, rc);
1101                                 } else {
1102                                         llog_ctxt_put(ctxt);
1103                                 }
1104                                 rc = lod_sub_prep_llog(env, lod,
1105                                                        sub_tgt->ltd_tgt,
1106                                                        sub_tgt->ltd_index);
1107                                 if (rc == 0)
1108                                         sub_tgt->ltd_active = 1;
1109                         } else {
1110                                 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
1111                                                   NULL);
1112                                 sub_tgt->ltd_active = 0;
1113                         }
1114                         GOTO(out, rc);
1115                 }
1116
1117
1118                 if (strstr(param, PARAM_LOD) != NULL)
1119                         count = class_modify_config(lcfg, PARAM_LOD,
1120                                                     &lod->lod_dt_dev.dd_kobj);
1121                 else
1122                         count = class_modify_config(lcfg, PARAM_LOV,
1123                                                     &lod->lod_dt_dev.dd_kobj);
1124                 rc = count > 0 ? 0 : count;
1125                 GOTO(out, rc);
1126         }
1127         case LCFG_PRE_CLEANUP: {
1128                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1129                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1130                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_RECOVERY_CONNECT, cfs_fail_val * 2);
1131                 next = &lod->lod_child->dd_lu_dev;
1132                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1133                 if (rc != 0)
1134                         CDEBUG(D_HA, "%s: can't process %u: %d\n",
1135                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1136
1137                 lod_sub_stop_recovery_threads(env, lod);
1138                 lod_fini_distribute_txn(env, lod);
1139                 lod_sub_fini_all_llogs(env, lod);
1140                 break;
1141         }
1142         case LCFG_CLEANUP: {
1143                 if (lod->lod_md_root) {
1144                         dt_object_put(env, &lod->lod_md_root->ldo_obj);
1145                         lod->lod_md_root = NULL;
1146                 }
1147
1148                 /*
1149                  * do cleanup on underlying storage only when
1150                  * all OSPs are cleaned up, as they use that OSD as well
1151                  */
1152                 lu_dev_del_linkage(dev->ld_site, dev);
1153                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1154                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1155                 next = &lod->lod_child->dd_lu_dev;
1156                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1157                 if (rc)
1158                         CERROR("%s: can't process %u: rc = %d\n",
1159                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1160
1161                 rc = obd_disconnect(lod->lod_child_exp);
1162                 if (rc)
1163                         CERROR("error in disconnect from storage: rc = %d\n",
1164                                rc);
1165                 break;
1166         }
1167         default:
1168                 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1169                        lcfg->lcfg_command);
1170                 rc = -EINVAL;
1171                 break;
1172         }
1173
1174 out:
1175         RETURN(rc);
1176 }
1177
1178 /**
1179  * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1180  *
1181  * The method is called once the recovery is complete. This implementation
1182  * distributes the notification to all the known targets.
1183  *
1184  * see include/lu_object.h for the details
1185  */
1186 static int lod_recovery_complete(const struct lu_env *env,
1187                                  struct lu_device *dev)
1188 {
1189         struct lod_device *lod = lu2lod_dev(dev);
1190         struct lu_device *next = &lod->lod_child->dd_lu_dev;
1191         struct lod_tgt_desc *tgt;
1192         int rc;
1193
1194         ENTRY;
1195
1196         LASSERT(lod->lod_recovery_completed == 0);
1197         lod->lod_recovery_completed = 1;
1198
1199         rc = next->ld_ops->ldo_recovery_complete(env, next);
1200
1201         lod_getref(&lod->lod_ost_descs);
1202         if (lod->lod_ost_descs.ltd_tgts_size > 0) {
1203                 lod_foreach_ost(lod, tgt) {
1204                         LASSERT(tgt && tgt->ltd_tgt);
1205                         next = &tgt->ltd_tgt->dd_lu_dev;
1206                         rc = next->ld_ops->ldo_recovery_complete(env, next);
1207                         if (rc)
1208                                 CERROR("%s: can't complete recovery on #%d: rc = %d\n",
1209                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1210                                        rc);
1211                 }
1212         }
1213         lod_putref(lod, &lod->lod_ost_descs);
1214         RETURN(rc);
1215 }
1216
1217 /**
1218  * Init update logs on all sub device
1219  *
1220  * LOD initialize update logs on all of sub devices. Because the initialization
1221  * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1222  * lod_object_init(), this API has to be called after LOD is initialized.
1223  * \param[in] env       execution environment
1224  * \param[in] lod       lod device
1225  *
1226  * \retval              0 if update log is initialized successfully.
1227  * \retval              negative errno if initialization fails.
1228  */
1229 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1230 {
1231         struct lu_tgt_desc *mdt;
1232         int rc;
1233
1234         ENTRY;
1235
1236         /*
1237          * llog must be setup after LOD is initialized, because llog
1238          * initialization include FLD lookup
1239          */
1240         LASSERT(lod->lod_initialized);
1241
1242         /* Init the llog in its own stack */
1243         rc = lod_sub_init_llog(env, lod, lod->lod_child);
1244         if (rc < 0)
1245                 RETURN(rc);
1246
1247         lod_foreach_mdt(lod, mdt) {
1248                 rc = lod_sub_init_llog(env, lod, mdt->ltd_tgt);
1249                 if (rc != 0)
1250                         break;
1251         }
1252
1253         RETURN(rc);
1254 }
1255
1256 #define UPDATE_LOG_MAX_AGE      (30 * 24 * 60 * 60)     /* 30 days, in sec */
1257
1258 static int lod_update_log_stale(const struct lu_env *env, struct dt_object *dto,
1259                                 struct lu_buf *buf)
1260 {
1261         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
1262         struct llog_log_hdr *hdr;
1263         loff_t off = 0;
1264         int rc;
1265
1266         ENTRY;
1267         rc = dt_attr_get(env, dto, attr);
1268         if (rc)
1269                 RETURN(rc);
1270
1271         if (!(attr->la_valid & (LA_CTIME | LA_SIZE)))
1272                 RETURN(-EFAULT);
1273
1274         /* by default update log ctime is not set */
1275         if (attr->la_ctime == 0)
1276                 RETURN(0);
1277
1278         /* update log not expired yet */
1279         if (attr->la_ctime + UPDATE_LOG_MAX_AGE > ktime_get_real_seconds())
1280                 RETURN(0);
1281
1282         if (attr->la_size == 0)
1283                 RETURN(-EFAULT);
1284
1285         rc = dt_read(env, dto, buf, &off);
1286         if (rc < 0)
1287                 RETURN(rc);
1288
1289         hdr = (struct llog_log_hdr *)buf->lb_buf;
1290         if (LLOG_REC_HDR_NEEDS_SWABBING(&hdr->llh_hdr))
1291                 lustre_swab_llog_hdr(hdr);
1292         /* log header is sane and flag LLOG_F_MAX_AGE|LLOG_F_RM_ON_ERR is set */
1293         if (rc >= sizeof(*hdr) &&
1294             hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC &&
1295             (hdr->llh_flags & (LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR)) ==
1296             (LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR))
1297                 RETURN(1);
1298
1299         RETURN(0);
1300 }
1301
1302 /**
1303  * Reclaim stale update log.
1304  *
1305  * When update log is canceld (upon recovery abort), it's not destroy, but
1306  * canceled from catlist, and set ctime and LLOG_F_MAX_AGE|LLOG_F_RM_ON_ERR,
1307  * which is kept for debug. If it expired (more than UPDATE_LOG_MAX_AGE seconds
1308  * passed), destroy it to save space.
1309  */
1310 static int lod_update_log_gc(const struct lu_env *env, struct lod_device *lod,
1311                              struct dt_object *dir, struct dt_object *dto,
1312                              const char *name)
1313 {
1314         struct dt_device *dt = lod->lod_child;
1315         struct thandle *th;
1316         int rc;
1317
1318         ENTRY;
1319         th = dt_trans_create(env, dt);
1320         if (IS_ERR(th))
1321                 RETURN(PTR_ERR(th));
1322
1323         rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
1324         if (rc)
1325                 GOTO(out_trans, rc);
1326
1327         rc = dt_declare_destroy(env, dto, th);
1328         if (rc)
1329                 GOTO(out_trans, rc);
1330
1331         rc = dt_trans_start_local(env, dt, th);
1332         if (rc)
1333                 GOTO(out_trans, rc);
1334
1335         rc = dt_delete(env, dir, (const struct dt_key *)name, th);
1336         if (rc)
1337                 GOTO(out_trans, rc);
1338
1339         rc = dt_destroy(env, dto, th);
1340         GOTO(out_trans, rc);
1341 out_trans:
1342         dt_trans_stop(env, dt, th);
1343
1344         return rc;
1345 }
1346
1347 /* reclaim stale update llogs under "update_log_dir" */
1348 static int lod_update_log_dir_gc(const struct lu_env *env,
1349                                  struct lod_device *lod,
1350                                  struct dt_object *dir)
1351 {
1352         struct lod_thread_info *info = lod_env_info(env);
1353         struct lu_buf *buf = &info->lti_linkea_buf;
1354         struct lu_dirent *ent = (struct lu_dirent *)info->lti_key;
1355         struct lu_fid *fid = &info->lti_fid;
1356         struct dt_it *it;
1357         const struct dt_it_ops *iops;
1358         struct dt_object *dto;
1359         int rc;
1360
1361         ENTRY;
1362
1363         if (unlikely(!dt_try_as_dir(env, dir, true)))
1364                 RETURN(-ENOTDIR);
1365
1366         lu_buf_alloc(buf, sizeof(struct llog_log_hdr));
1367         if (!buf->lb_buf)
1368                 RETURN(-ENOMEM);
1369
1370         iops = &dir->do_index_ops->dio_it;
1371         it = iops->init(env, dir, LUDA_64BITHASH);
1372         if (IS_ERR(it))
1373                 GOTO(out, rc = PTR_ERR(it));
1374
1375         rc = iops->load(env, it, 0);
1376         if (rc == 0)
1377                 rc = iops->next(env, it);
1378         else if (rc > 0)
1379                 rc = 0;
1380
1381         while (rc == 0) {
1382                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1383                 if (rc != 0)
1384                         break;
1385
1386                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1387                 if (ent->lde_name[0] == '.') {
1388                         if (ent->lde_namelen == 1)
1389                                 goto next;
1390
1391                         if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
1392                                 goto next;
1393                 }
1394
1395                 fid_le_to_cpu(fid, &ent->lde_fid);
1396                 dto = dt_locate(env, lod->lod_child, fid);
1397                 if (IS_ERR(dto))
1398                         goto next;
1399
1400                 buf->lb_len = sizeof(struct llog_log_hdr);
1401                 if (lod_update_log_stale(env, dto, buf) == 1)
1402                         lod_update_log_gc(env, lod, dir, dto, ent->lde_name);
1403                 dt_object_put(env, dto);
1404 next:
1405                 rc = iops->next(env, it);
1406         }
1407
1408         iops->put(env, it);
1409         iops->fini(env, it);
1410 out:
1411         buf->lb_len = sizeof(struct llog_log_hdr);
1412         lu_buf_free(buf);
1413
1414         RETURN(rc > 0 ? 0 : rc);
1415 }
1416
1417 /**
1418  * Implementation of lu_device_operations::ldo_prepare() for LOD
1419  *
1420  * see include/lu_object.h for the details.
1421  */
1422 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1423                        struct lu_device *cdev)
1424 {
1425         struct lod_device *lod = lu2lod_dev(cdev);
1426         struct lu_device *next = &lod->lod_child->dd_lu_dev;
1427         struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1428         int rc;
1429         struct dt_object *root;
1430         struct dt_object *dto;
1431         u32 index;
1432
1433         ENTRY;
1434
1435         rc = next->ld_ops->ldo_prepare(env, pdev, next);
1436         if (rc != 0) {
1437                 CERROR("%s: prepare bottom error: rc = %d\n",
1438                        lod2obd(lod)->obd_name, rc);
1439                 RETURN(rc);
1440         }
1441
1442         lod->lod_initialized = 1;
1443
1444         rc = dt_root_get(env, lod->lod_child, fid);
1445         if (rc < 0)
1446                 RETURN(rc);
1447
1448         root = dt_locate(env, lod->lod_child, fid);
1449         if (IS_ERR(root))
1450                 RETURN(PTR_ERR(root));
1451
1452         /* Create update log object */
1453         index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1454         lu_update_log_fid(fid, index);
1455
1456         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1457                                                  fid, root,
1458                                                  lod_update_log_name,
1459                                                  S_IFREG | 0644);
1460         if (IS_ERR(dto))
1461                 GOTO(out_put, rc = PTR_ERR(dto));
1462
1463         dt_object_put(env, dto);
1464
1465         /* Create update log dir */
1466         lu_update_log_dir_fid(fid, index);
1467         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1468                                                  fid, root,
1469                                                  lod_update_log_dir_name,
1470                                                  S_IFDIR | 0644);
1471         if (IS_ERR(dto))
1472                 GOTO(out_put, rc = PTR_ERR(dto));
1473
1474         lod_update_log_dir_gc(env, lod, dto);
1475         dt_object_put(env, dto);
1476
1477         rc = lod_prepare_distribute_txn(env, lod);
1478         if (rc != 0)
1479                 GOTO(out_put, rc);
1480
1481         rc = lod_sub_init_llogs(env, lod);
1482         if (rc != 0)
1483                 GOTO(out_put, rc);
1484
1485 out_put:
1486         dt_object_put(env, root);
1487
1488         RETURN(rc);
1489 }
1490
1491 /**
1492  * Implementation of lu_device_operations::ldo_fid_alloc() for LOD
1493  *
1494  * Find corresponding device by passed parent and name, and allocate FID from
1495  * there.
1496  *
1497  * see include/lu_object.h for the details.
1498  */
1499 static int lod_fid_alloc(const struct lu_env *env, struct lu_device *d,
1500                          struct lu_fid *fid, struct lu_object *parent,
1501                          const struct lu_name *name)
1502 {
1503         struct lod_device *lod = lu2lod_dev(d);
1504         struct lod_object *lo = lu2lod_obj(parent);
1505         struct dt_device *next;
1506         int rc;
1507
1508         ENTRY;
1509
1510         /* if @parent is remote, we don't know whether its layout was changed,
1511          * always reload layout.
1512          */
1513         if (lu_object_remote(parent))
1514                 lod_striping_free(env, lo);
1515
1516         rc = lod_striping_load(env, lo);
1517         if (rc)
1518                 RETURN(rc);
1519
1520         if (lo->ldo_dir_stripe_count > 0 && name) {
1521                 struct dt_object *stripe;
1522                 int idx;
1523
1524                 idx = __lmv_name_to_stripe_index(lo->ldo_dir_hash_type,
1525                                                  lo->ldo_dir_stripe_count,
1526                                                  lo->ldo_dir_migrate_hash,
1527                                                  lo->ldo_dir_migrate_offset,
1528                                                  name->ln_name,
1529                                                  name->ln_namelen, true);
1530                 if (idx < 0)
1531                         RETURN(idx);
1532
1533                 stripe = lo->ldo_stripe[idx];
1534                 if (!stripe || !dt_object_exists(stripe))
1535                         RETURN(-ENODEV);
1536
1537                 next = lu2dt_dev(stripe->do_lu.lo_dev);
1538         } else {
1539                 next = lod->lod_child;
1540         }
1541
1542         rc = dt_fid_alloc(env, next, fid, parent, name);
1543
1544         RETURN(rc);
1545 }
1546
1547 const struct lu_device_operations lod_lu_ops = {
1548         .ldo_object_alloc       = lod_object_alloc,
1549         .ldo_process_config     = lod_process_config,
1550         .ldo_recovery_complete  = lod_recovery_complete,
1551         .ldo_prepare            = lod_prepare,
1552         .ldo_fid_alloc          = lod_fid_alloc,
1553 };
1554
1555 /**
1556  * Implementation of dt_device_operations::dt_root_get() for LOD
1557  *
1558  * see include/dt_object.h for the details.
1559  */
1560 static int lod_root_get(const struct lu_env *env,
1561                         struct dt_device *dev, struct lu_fid *f)
1562 {
1563         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1564 }
1565
1566 static void lod_statfs_sum(struct obd_statfs *sfs,
1567                              struct obd_statfs *ost_sfs, int *bs)
1568 {
1569         while (ost_sfs->os_bsize < *bs) {
1570                 *bs >>= 1;
1571                 sfs->os_bsize >>= 1;
1572                 sfs->os_bavail <<= 1;
1573                 sfs->os_blocks <<= 1;
1574                 sfs->os_bfree <<= 1;
1575                 sfs->os_granted <<= 1;
1576         }
1577         while (ost_sfs->os_bsize > *bs) {
1578                 ost_sfs->os_bsize >>= 1;
1579                 ost_sfs->os_bavail <<= 1;
1580                 ost_sfs->os_blocks <<= 1;
1581                 ost_sfs->os_bfree <<= 1;
1582                 ost_sfs->os_granted <<= 1;
1583         }
1584         sfs->os_bavail += ost_sfs->os_bavail;
1585         sfs->os_blocks += ost_sfs->os_blocks;
1586         sfs->os_bfree += ost_sfs->os_bfree;
1587         sfs->os_granted += ost_sfs->os_granted;
1588 }
1589
1590 /**
1591  * Implementation of dt_device_operations::dt_statfs() for LOD
1592  *
1593  * see include/dt_object.h for the details.
1594  */
1595 static int lod_statfs(const struct lu_env *env, struct dt_device *dev,
1596                       struct obd_statfs *sfs, struct obd_statfs_info *info)
1597 {
1598         struct lod_device *lod = dt2lod_dev(dev);
1599         struct lu_tgt_desc *tgt;
1600         struct obd_statfs ost_sfs;
1601         u64 ost_files = 0;
1602         u64 ost_ffree = 0;
1603         int rc, bs;
1604
1605         rc = dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1606         if (rc)
1607                 GOTO(out, rc);
1608
1609         bs = sfs->os_bsize;
1610
1611         sfs->os_bavail = 0;
1612         sfs->os_blocks = 0;
1613         sfs->os_bfree = 0;
1614         sfs->os_granted = 0;
1615
1616         lod_getref(&lod->lod_mdt_descs);
1617         lod_foreach_mdt(lod, tgt) {
1618                 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1619                 /* ignore errors */
1620                 if (rc)
1621                         continue;
1622                 sfs->os_files += ost_sfs.os_files;
1623                 sfs->os_ffree += ost_sfs.os_ffree;
1624                 lod_statfs_sum(sfs, &ost_sfs, &bs);
1625         }
1626         lod_putref(lod, &lod->lod_mdt_descs);
1627
1628         /*
1629          * at some point we can check whether DoM is enabled and
1630          * decide how to account MDT space. for simplicity let's
1631          * just fallback to pre-DoM policy if any OST is alive
1632          */
1633         lod_getref(&lod->lod_ost_descs);
1634         lod_foreach_ost(lod, tgt) {
1635                 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1636                 /* ignore errors */
1637                 if (rc || ost_sfs.os_bsize == 0)
1638                         continue;
1639                 if (!ost_files) {
1640                         /*
1641                          * if only MDTs with DoM then report only MDT blocks,
1642                          * otherwise show only OST blocks, and DoM is "free"
1643                          */
1644                         sfs->os_bavail = 0;
1645                         sfs->os_blocks = 0;
1646                         sfs->os_bfree = 0;
1647                         sfs->os_granted = 0;
1648                 }
1649                 ost_files += ost_sfs.os_files;
1650                 ost_ffree += ost_sfs.os_ffree;
1651                 ost_sfs.os_bavail += ost_sfs.os_granted;
1652                 lod_statfs_sum(sfs, &ost_sfs, &bs);
1653                 LASSERTF(bs == ost_sfs.os_bsize, "%d != %d\n",
1654                         (int)sfs->os_bsize, (int)ost_sfs.os_bsize);
1655         }
1656         lod_putref(lod, &lod->lod_ost_descs);
1657         sfs->os_state |= OS_STATFS_SUM;
1658
1659         /* If we have _some_ OSTs, but don't have as many free objects on the
1660          * OSTs as inodes on the MDTs, reduce the reported number of inodes
1661          * to compensate, so that the "inodes in use" number is correct.
1662          * This should be kept in sync with ll_statfs_internal().
1663          */
1664         if (ost_files && ost_ffree < sfs->os_ffree) {
1665                 sfs->os_files = (sfs->os_files - sfs->os_ffree) + ost_ffree;
1666                 sfs->os_ffree = ost_ffree;
1667         }
1668
1669         /* a single successful statfs should be enough */
1670         rc = 0;
1671
1672 out:
1673         RETURN(rc);
1674 }
1675
1676 /**
1677  * Implementation of dt_device_operations::dt_trans_create() for LOD
1678  *
1679  * Creates a transaction using local (to this node) OSD.
1680  *
1681  * see include/dt_object.h for the details.
1682  */
1683 static struct thandle *lod_trans_create(const struct lu_env *env,
1684                                         struct dt_device *dt)
1685 {
1686         struct thandle *th;
1687
1688         th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1689         if (IS_ERR(th))
1690                 return th;
1691
1692         th->th_dev = dt;
1693
1694         return th;
1695 }
1696
1697 /* distributed transaction failure may cause object missing or disconnected
1698  * directories, check space before transaction start.
1699  */
1700 static int lod_trans_space_check(const struct lu_env *env,
1701                                  struct lod_device *lod,
1702                                  struct thandle *th)
1703 {
1704         struct lod_thread_info *info = lod_env_info(env);
1705         struct obd_statfs *sfs = &info->lti_osfs;
1706         struct top_thandle *top_th = container_of(th, struct top_thandle,
1707                                                   tt_super);
1708         struct top_multiple_thandle *tmt = top_th->tt_multiple_thandle;
1709         struct sub_thandle *st;
1710         int rc;
1711
1712         if (likely(!tmt))
1713                 return 0;
1714
1715         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1716                 struct dt_device *sub_dt;
1717
1718                 if (st->st_sub_th == NULL)
1719                         continue;
1720
1721                 if (st->st_sub_th == top_th->tt_master_sub_thandle)
1722                         continue;
1723
1724                 sub_dt = st->st_sub_th->th_dev;
1725                 rc = dt_statfs(env, sub_dt, sfs);
1726                 if (rc) {
1727                         CDEBUG(D_INFO, "%s: fail - statfs error: rc = %d\n",
1728                                sub_dt->dd_lu_dev.ld_obd->obd_name, rc);
1729                         /* statfs may fail during recovery, skip check */
1730                         if (!lod->lod_recovery_completed)
1731                                 rc = 0;
1732                         return rc;
1733                 }
1734
1735                 if (unlikely(sfs->os_state &
1736                              (OS_STATFS_ENOINO | OS_STATFS_ENOSPC))) {
1737                         CDEBUG(D_INFO, "%s: fail - target state %x: rc = %d\n",
1738                                sub_dt->dd_lu_dev.ld_obd->obd_name,
1739                                sfs->os_state, -ENOSPC);
1740                         return -ENOSPC;
1741                 }
1742         }
1743
1744         return 0;
1745 }
1746
1747 /**
1748  * Implementation of dt_device_operations::dt_trans_start() for LOD
1749  *
1750  * Starts the set of local transactions using the targets involved
1751  * in declare phase. Initial support for the distributed transactions.
1752  *
1753  * see include/dt_object.h for the details.
1754  */
1755 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1756                            struct thandle *th)
1757 {
1758         struct lod_device *lod = dt2lod_dev(dt);
1759
1760         if (lod->lod_dist_txn_check_space) {
1761                 int rc;
1762
1763                 rc = lod_trans_space_check(env, lod, th);
1764                 if (rc)
1765                         return rc;
1766         }
1767
1768         return top_trans_start(env, lod->lod_child, th);
1769 }
1770
1771 static int lod_trans_cb_add(struct thandle *th,
1772                             struct dt_txn_commit_cb *dcb)
1773 {
1774         struct top_thandle      *top_th = container_of(th, struct top_thandle,
1775                                                        tt_super);
1776         return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1777 }
1778
1779 /**
1780  * add noop update to the update records
1781  *
1782  * Add noop updates to the update records, which is only used in
1783  * test right now.
1784  *
1785  * \param[in] env       execution environment
1786  * \param[in] dt        dt device of lod
1787  * \param[in] th        thandle
1788  * \param[in] count     the count of update records to be added.
1789  *
1790  * \retval              0 if adding succeeds.
1791  * \retval              negative errno if adding fails.
1792  */
1793 static int lod_add_noop_records(const struct lu_env *env,
1794                                 struct dt_device *dt, struct thandle *th,
1795                                 int count)
1796 {
1797         struct top_thandle *top_th;
1798         struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1799         int i;
1800         int rc = 0;
1801
1802         top_th = container_of(th, struct top_thandle, tt_super);
1803         if (!top_th->tt_multiple_thandle)
1804                 return 0;
1805
1806         fid_zero(fid);
1807         for (i = 0; i < count; i++) {
1808                 rc = update_record_pack(noop, th, fid);
1809                 if (rc < 0)
1810                         return rc;
1811         }
1812         return rc;
1813 }
1814
1815 /**
1816  * Implementation of dt_device_operations::dt_trans_stop() for LOD
1817  *
1818  * Stops the set of local transactions using the targets involved
1819  * in declare phase. Initial support for the distributed transactions.
1820  *
1821  * see include/dt_object.h for the details.
1822  */
1823 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1824                           struct thandle *th)
1825 {
1826         if (OBD_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1827                 int rc;
1828
1829                 rc = lod_add_noop_records(env, dt, th, 5000);
1830                 if (rc < 0)
1831                         RETURN(rc);
1832         }
1833         return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1834 }
1835
1836 /**
1837  * Implementation of dt_device_operations::dt_conf_get() for LOD
1838  *
1839  * Currently returns the configuration provided by the local OSD.
1840  *
1841  * see include/dt_object.h for the details.
1842  */
1843 static void lod_conf_get(const struct lu_env *env,
1844                          const struct dt_device *dev,
1845                          struct dt_device_param *param)
1846 {
1847         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1848 }
1849
1850 /**
1851  * Implementation of dt_device_operations::dt_sync() for LOD
1852  *
1853  * Syncs all known OST targets. Very very expensive and used
1854  * rarely by LFSCK now. Should not be used in general.
1855  *
1856  * see include/dt_object.h for the details.
1857  */
1858 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1859 {
1860         struct lod_device *lod = dt2lod_dev(dev);
1861         struct lu_tgt_desc *tgt;
1862         int rc = 0;
1863
1864         ENTRY;
1865
1866         lod_getref(&lod->lod_ost_descs);
1867         lod_foreach_ost(lod, tgt) {
1868                 if (!tgt->ltd_active)
1869                         continue;
1870                 rc = dt_sync(env, tgt->ltd_tgt);
1871                 if (rc) {
1872                         if (rc != -ENOTCONN) {
1873                                 CERROR("%s: can't sync ost %u: rc = %d\n",
1874                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1875                                        rc);
1876                                 break;
1877                         }
1878                         rc = 0;
1879                 }
1880         }
1881         lod_putref(lod, &lod->lod_ost_descs);
1882
1883         if (rc)
1884                 RETURN(rc);
1885
1886         lod_getref(&lod->lod_mdt_descs);
1887         lod_foreach_mdt(lod, tgt) {
1888                 if (!tgt->ltd_active)
1889                         continue;
1890                 rc = dt_sync(env, tgt->ltd_tgt);
1891                 if (rc) {
1892                         if (rc != -ENOTCONN) {
1893                                 CERROR("%s: can't sync mdt %u: rc = %d\n",
1894                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1895                                        rc);
1896                                 break;
1897                         }
1898                         rc = 0;
1899                 }
1900         }
1901         lod_putref(lod, &lod->lod_mdt_descs);
1902
1903         if (rc == 0)
1904                 rc = dt_sync(env, lod->lod_child);
1905
1906         RETURN(rc);
1907 }
1908
1909 /**
1910  * Implementation of dt_device_operations::dt_ro() for LOD
1911  *
1912  * Turns local OSD read-only, used for the testing only.
1913  *
1914  * see include/dt_object.h for the details.
1915  */
1916 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1917 {
1918         return dt_ro(env, dt2lod_dev(dev)->lod_child);
1919 }
1920
1921 /**
1922  * Implementation of dt_device_operations::dt_commit_async() for LOD
1923  *
1924  * Asks local OSD to commit sooner.
1925  *
1926  * see include/dt_object.h for the details.
1927  */
1928 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1929 {
1930         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1931 }
1932
1933 static const struct dt_device_operations lod_dt_ops = {
1934         .dt_root_get         = lod_root_get,
1935         .dt_statfs           = lod_statfs,
1936         .dt_trans_create     = lod_trans_create,
1937         .dt_trans_start      = lod_trans_start,
1938         .dt_trans_stop       = lod_trans_stop,
1939         .dt_conf_get         = lod_conf_get,
1940         .dt_sync             = lod_sync,
1941         .dt_ro               = lod_ro,
1942         .dt_commit_async     = lod_commit_async,
1943         .dt_trans_cb_add     = lod_trans_cb_add,
1944 };
1945
1946 /**
1947  * Connect to a local OSD.
1948  *
1949  * Used to connect to the local OSD at mount. OSD name is taken from the
1950  * configuration command passed. This connection is used to identify LU
1951  * site and pin the OSD from early removal.
1952  *
1953  * \param[in] env               LU environment provided by the caller
1954  * \param[in] lod               lod device
1955  * \param[in] cfg               configuration command to apply
1956  *
1957  * \retval 0                    on success
1958  * \retval negative             negated errno on error
1959  **/
1960 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1961                               struct lustre_cfg *cfg)
1962 {
1963         struct obd_connect_data *data = NULL;
1964         struct obd_device *obd;
1965         char *nextdev = NULL, *p, *s;
1966         int rc, len = 0;
1967
1968         ENTRY;
1969
1970         LASSERT(cfg);
1971         LASSERT(lod->lod_child_exp == NULL);
1972
1973         /*
1974          * compatibility hack: we still use old config logs
1975          * which specify LOV, but we need to learn underlying
1976          * OSD device, which is supposed to be:
1977          *  <fsname>-MDTxxxx-osd
1978          *
1979          * 2.x MGS generates lines like the following:
1980          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
1981          * 1.8 MGS generates lines like the following:
1982          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
1983          *
1984          * we use "-MDT" to differentiate 2.x from 1.8
1985          */
1986         p = lustre_cfg_string(cfg, 0);
1987         if (p && strstr(p, "-mdtlov")) {
1988                 len = strlen(p) + 6;
1989                 OBD_ALLOC(nextdev, len);
1990                 if (!nextdev)
1991                         GOTO(out, rc = -ENOMEM);
1992
1993                 strcpy(nextdev, p);
1994                 s = strstr(nextdev, "-mdtlov");
1995                 if (unlikely(!s)) {
1996                         CERROR("%s: unable to parse device name: rc = %d\n",
1997                                lustre_cfg_string(cfg, 0), -EINVAL);
1998                         GOTO(out, rc = -EINVAL);
1999                 }
2000
2001                 if (strstr(nextdev, "-MDT")) {
2002                         /* 2.x config */
2003                         strcpy(s, "-osd");
2004                 } else {
2005                         /* 1.8 config */
2006                         strcpy(s, "-MDT0000-osd");
2007                 }
2008         } else {
2009                 CERROR("%s: unable to parse device name: rc = %d\n",
2010                        lustre_cfg_string(cfg, 0), -EINVAL);
2011                 GOTO(out, rc = -EINVAL);
2012         }
2013
2014         OBD_ALLOC_PTR(data);
2015         if (!data)
2016                 GOTO(out, rc = -ENOMEM);
2017
2018         obd = class_name2obd(nextdev);
2019         if (!obd) {
2020                 CERROR("%s: can not locate next device: rc = %d\n",
2021                        nextdev, -ENOTCONN);
2022                 GOTO(out, rc = -ENOTCONN);
2023         }
2024
2025         data->ocd_connect_flags = OBD_CONNECT_VERSION;
2026         data->ocd_version = LUSTRE_VERSION_CODE;
2027
2028         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
2029                          data, NULL);
2030         if (rc) {
2031                 CERROR("%s: cannot connect to next dev: rc = %d\n",
2032                        nextdev, rc);
2033                 GOTO(out, rc);
2034         }
2035
2036         lod->lod_dt_dev.dd_lu_dev.ld_site =
2037                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
2038         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
2039         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
2040
2041 out:
2042         if (data)
2043                 OBD_FREE_PTR(data);
2044         if (nextdev)
2045                 OBD_FREE(nextdev, len);
2046         RETURN(rc);
2047 }
2048
2049 static int lod_lsfs_init(const struct lu_env *env, struct lod_device *d)
2050 {
2051         struct obd_statfs sfs;
2052         int rc;
2053
2054         rc = dt_statfs(env, d->lod_child, &sfs);
2055         if (rc) {
2056                 CDEBUG(D_LAYOUT, "%s: failed to get OSD statfs, rc = %d\n",
2057                        lod2obd(d)->obd_name, rc);
2058                 return rc;
2059         }
2060
2061         /* udpate local OSD cached statfs data */
2062         spin_lock_init(&d->lod_lsfs_lock);
2063         d->lod_lsfs_age = ktime_get_seconds();
2064         d->lod_lsfs_total_mb = (sfs.os_blocks * sfs.os_bsize) >> 20;
2065         d->lod_lsfs_free_mb = (sfs.os_bfree * sfs.os_bsize) >> 20;
2066         return 0;
2067 }
2068
2069 /**
2070  * Initialize LOD device at setup.
2071  *
2072  * Initializes the given LOD device using the original configuration command.
2073  * The function initiates a connection to the local OSD and initializes few
2074  * internal structures like pools, target tables, etc.
2075  *
2076  * \param[in] env               LU environment provided by the caller
2077  * \param[in] lod               lod device
2078  * \param[in] ldt               not used
2079  * \param[in] cfg               configuration command
2080  *
2081  * \retval 0                    on success
2082  * \retval negative             negated errno on error
2083  **/
2084 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
2085                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
2086 {
2087         struct dt_device_param ddp;
2088         struct obd_device *obd;
2089         int rc;
2090
2091         ENTRY;
2092
2093         obd = class_name2obd(lustre_cfg_string(cfg, 0));
2094         if (!obd) {
2095                 rc = -ENODEV;
2096                 CERROR("Cannot find obd with name '%s': rc = %d\n",
2097                        lustre_cfg_string(cfg, 0), rc);
2098                 RETURN(rc);
2099         }
2100
2101         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
2102         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
2103         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
2104         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
2105
2106         rc = lod_connect_to_osd(env, lod, cfg);
2107         if (rc)
2108                 RETURN(rc);
2109
2110         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
2111         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
2112         lod->lod_dom_stripesize_max_kb = (1ULL << 10); /* 1Mb is default */
2113         lod->lod_max_stripecount = 0;
2114
2115         /* initialize local statfs cached values */
2116         rc = lod_lsfs_init(env, lod);
2117         if (rc)
2118                 GOTO(out_disconnect, rc);
2119
2120         /* default threshold as half of total space, in MiB */
2121         lod->lod_dom_threshold_free_mb = lod->lod_lsfs_total_mb / 2;
2122         /* set default DoM stripe size based on free space amount */
2123         lod_dom_stripesize_recalc(lod);
2124
2125         /* setup obd to be used with old lov code */
2126         rc = lod_pools_init(lod, cfg);
2127         if (rc)
2128                 GOTO(out_disconnect, rc);
2129
2130         rc = lod_procfs_init(lod);
2131         if (rc)
2132                 GOTO(out_pools, rc);
2133
2134         spin_lock_init(&lod->lod_lock);
2135         spin_lock_init(&lod->lod_connects_lock);
2136         lu_tgt_descs_init(&lod->lod_mdt_descs, true);
2137         lu_tgt_descs_init(&lod->lod_ost_descs, false);
2138         lu_qos_rr_init(&lod->lod_mdt_descs.ltd_qos.lq_rr);
2139         lu_qos_rr_init(&lod->lod_ost_descs.ltd_qos.lq_rr);
2140         lod->lod_dist_txn_check_space = 1;
2141
2142         RETURN(0);
2143
2144 out_pools:
2145         lod_pools_fini(lod);
2146 out_disconnect:
2147         obd_disconnect(lod->lod_child_exp);
2148         RETURN(rc);
2149 }
2150
2151 /**
2152  * Implementation of lu_device_type_operations::ldto_device_free() for LOD
2153  *
2154  * Releases the memory allocated for LOD device.
2155  *
2156  * see include/lu_object.h for the details.
2157  */
2158 static struct lu_device *lod_device_free(const struct lu_env *env,
2159                                          struct lu_device *lu)
2160 {
2161         struct lod_device *lod = lu2lod_dev(lu);
2162         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
2163
2164         ENTRY;
2165
2166         if (atomic_read(&lu->ld_site->ls_obj_hash.nelems)) {
2167                 lu_site_print(env, lu->ld_site, &lu->ld_ref, D_ERROR,
2168                               lu_cdebug_printer);
2169         }
2170         LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
2171         dt_device_fini(&lod->lod_dt_dev);
2172         OBD_FREE_PTR(lod);
2173         RETURN(next);
2174 }
2175
2176 /**
2177  * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
2178  *
2179  * Allocates LOD device and calls the helpers to initialize it.
2180  *
2181  * see include/lu_object.h for the details.
2182  */
2183 static struct lu_device *lod_device_alloc(const struct lu_env *env,
2184                                           struct lu_device_type *type,
2185                                           struct lustre_cfg *lcfg)
2186 {
2187         struct lod_device *lod;
2188         struct lu_device *lu_dev;
2189
2190         OBD_ALLOC_PTR(lod);
2191         if (!lod) {
2192                 lu_dev = ERR_PTR(-ENOMEM);
2193         } else {
2194                 int rc;
2195
2196                 lu_dev = lod2lu_dev(lod);
2197                 dt_device_init(&lod->lod_dt_dev, type);
2198                 rc = lod_init0(env, lod, type, lcfg);
2199                 if (rc != 0) {
2200                         lod_device_free(env, lu_dev);
2201                         lu_dev = ERR_PTR(rc);
2202                 }
2203         }
2204
2205         return lu_dev;
2206 }
2207
2208 static void lod_avoid_guide_fini(struct lod_avoid_guide *lag)
2209 {
2210         if (lag->lag_oss_avoid_array)
2211                 OBD_FREE_PTR_ARRAY(lag->lag_oss_avoid_array,
2212                                    lag->lag_oaa_size);
2213         bitmap_free(lag->lag_ost_avoid_bitmap);
2214 }
2215
2216 /**
2217  * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
2218  *
2219  * Releases the internal resources used by LOD device.
2220  *
2221  * see include/lu_object.h for the details.
2222  */
2223 static struct lu_device *lod_device_fini(const struct lu_env *env,
2224                                          struct lu_device *d)
2225 {
2226         struct lod_device *lod = lu2lod_dev(d);
2227         int rc;
2228
2229         ENTRY;
2230
2231         lod_pools_fini(lod);
2232
2233         lod_procfs_fini(lod);
2234
2235         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs);
2236         if (rc)
2237                 CERROR("%s: can not fini ost descriptors: rc =  %d\n",
2238                         lod2obd(lod)->obd_name, rc);
2239
2240         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs);
2241         if (rc)
2242                 CERROR("%s: can not fini mdt descriptors: rc =  %d\n",
2243                         lod2obd(lod)->obd_name, rc);
2244
2245         RETURN(NULL);
2246 }
2247
2248 /**
2249  * Implementation of obd_ops::o_connect() for LOD
2250  *
2251  * Used to track all the users of this specific LOD device,
2252  * so the device stays up until the last user disconnected.
2253  *
2254  * \param[in] env               LU environment provided by the caller
2255  * \param[out] exp              export the caller will be using to access LOD
2256  * \param[in] obd               OBD device representing LOD device
2257  * \param[in] cluuid            unique identifier of the caller
2258  * \param[in] data              not used
2259  * \param[in] localdata         not used
2260  *
2261  * \retval 0                    on success
2262  * \retval negative             negated errno on error
2263  **/
2264 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
2265                            struct obd_device *obd, struct obd_uuid *cluuid,
2266                            struct obd_connect_data *data, void *localdata)
2267 {
2268         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2269         struct lustre_handle conn;
2270         int rc;
2271
2272         ENTRY;
2273
2274         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
2275
2276         rc = class_connect(&conn, obd, cluuid);
2277         if (rc)
2278                 RETURN(rc);
2279
2280         *exp = class_conn2export(&conn);
2281
2282         spin_lock(&lod->lod_connects_lock);
2283         lod->lod_connects++;
2284         /* at the moment we expect the only user */
2285         LASSERT(lod->lod_connects == 1);
2286         spin_unlock(&lod->lod_connects_lock);
2287
2288         RETURN(0);
2289 }
2290
2291 /**
2292  *
2293  * Implementation of obd_ops::o_disconnect() for LOD
2294  *
2295  * When the caller doesn't need to use this LOD instance, it calls
2296  * obd_disconnect() and LOD releases corresponding export/reference count.
2297  * Once all the users gone, LOD device is released.
2298  *
2299  * \param[in] exp               export provided to the caller in obd_connect()
2300  *
2301  * \retval 0                    on success
2302  * \retval negative             negated errno on error
2303  **/
2304 static int lod_obd_disconnect(struct obd_export *exp)
2305 {
2306         struct obd_device *obd = exp->exp_obd;
2307         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2308         int rc, release = 0;
2309
2310         ENTRY;
2311
2312         /* Only disconnect the underlying layers on the final disconnect. */
2313         spin_lock(&lod->lod_connects_lock);
2314         lod->lod_connects--;
2315         if (lod->lod_connects != 0) {
2316                 /* why should there be more than 1 connect? */
2317                 spin_unlock(&lod->lod_connects_lock);
2318                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
2319                        lod->lod_connects);
2320                 goto out;
2321         }
2322         spin_unlock(&lod->lod_connects_lock);
2323
2324         /* the last user of lod has gone, let's release the device */
2325         release = 1;
2326
2327 out:
2328         rc = class_disconnect(exp); /* bz 9811 */
2329
2330         if (rc == 0 && release)
2331                 class_manual_cleanup(obd);
2332         RETURN(rc);
2333 }
2334
2335 LU_KEY_INIT(lod, struct lod_thread_info);
2336
2337 static void lod_key_fini(const struct lu_context *ctx,
2338                 struct lu_context_key *key, void *data)
2339 {
2340         struct lod_thread_info *info = data;
2341         struct lod_layout_component *lds =
2342                                 info->lti_def_striping.lds_def_comp_entries;
2343
2344         /*
2345          * allocated in lod_get_lov_ea
2346          * XXX: this is overload, a tread may have such store but used only
2347          * once. Probably better would be pool of such stores per LOD.
2348          */
2349         if (info->lti_ea_store) {
2350                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
2351                 info->lti_ea_store = NULL;
2352                 info->lti_ea_store_size = 0;
2353         }
2354         lu_buf_free(&info->lti_linkea_buf);
2355
2356         if (lds)
2357                 lod_free_def_comp_entries(&info->lti_def_striping);
2358
2359         if (info->lti_comp_size > 0)
2360                 OBD_FREE_PTR_ARRAY(info->lti_comp_idx,
2361                                    info->lti_comp_size);
2362
2363         lod_avoid_guide_fini(&info->lti_avoid);
2364
2365         OBD_FREE_PTR(info);
2366 }
2367
2368 /* context key: lod_thread_key */
2369 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
2370
2371 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
2372
2373 static const struct lu_device_type_operations lod_device_type_ops = {
2374         .ldto_init              = lod_type_init,
2375         .ldto_fini              = lod_type_fini,
2376
2377         .ldto_start             = lod_type_start,
2378         .ldto_stop              = lod_type_stop,
2379
2380         .ldto_device_alloc      = lod_device_alloc,
2381         .ldto_device_free       = lod_device_free,
2382
2383         .ldto_device_fini       = lod_device_fini
2384 };
2385
2386 static struct lu_device_type lod_device_type = {
2387         .ldt_tags     = LU_DEVICE_DT,
2388         .ldt_name     = LUSTRE_LOD_NAME,
2389         .ldt_ops      = &lod_device_type_ops,
2390         .ldt_ctx_tags = LCT_MD_THREAD,
2391 };
2392
2393 /**
2394  * Implementation of obd_ops::o_get_info() for LOD
2395  *
2396  * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
2397  * the caller binary status whether LOD has seen connection to any OST target.
2398  * It will also check if the MDT update log context being initialized (if
2399  * needed).
2400  *
2401  * \param[in] env               LU environment provided by the caller
2402  * \param[in] exp               export of the caller
2403  * \param[in] keylen            len of the key
2404  * \param[in] key               the key
2405  * \param[in] vallen            not used
2406  * \param[in] val               not used
2407  *
2408  * \retval                      0 if a connection was seen
2409  * \retval                      -EAGAIN if LOD isn't running yet or no
2410  *                              connection has been seen yet
2411  * \retval                      -EINVAL if not supported key is requested
2412  **/
2413 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
2414                             u32 keylen, void *key, u32 *vallen, void *val)
2415 {
2416         int rc = -EINVAL;
2417
2418         if (KEY_IS(KEY_OSP_CONNECTED)) {
2419                 struct obd_device *obd = exp->exp_obd;
2420                 struct lod_device *d;
2421                 struct lod_tgt_desc *tgt;
2422                 int rc = 1;
2423
2424                 if (!obd->obd_set_up || obd->obd_stopping)
2425                         RETURN(-EAGAIN);
2426
2427                 d = lu2lod_dev(obd->obd_lu_dev);
2428                 lod_getref(&d->lod_ost_descs);
2429                 lod_foreach_ost(d, tgt) {
2430                         rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2431                                           vallen, val);
2432                         /* one healthy device is enough */
2433                         if (rc == 0)
2434                                 break;
2435                 }
2436                 lod_putref(d, &d->lod_ost_descs);
2437
2438                 lod_getref(&d->lod_mdt_descs);
2439                 lod_foreach_mdt(d, tgt) {
2440                         struct llog_ctxt *ctxt;
2441
2442                         if (!tgt->ltd_active)
2443                                 continue;
2444
2445                         ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
2446                                                 LLOG_UPDATELOG_ORIG_CTXT);
2447                         if (!ctxt) {
2448                                 CDEBUG(D_INFO, "%s: %s is not ready.\n",
2449                                        obd->obd_name,
2450                                       tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
2451                                 rc = -EAGAIN;
2452                                 break;
2453                         }
2454                         if (!ctxt->loc_handle) {
2455                                 CDEBUG(D_INFO, "%s: %s is not ready.\n",
2456                                        obd->obd_name,
2457                                       tgt->ltd_tgt->dd_lu_dev.ld_obd->obd_name);
2458                                 rc = -EAGAIN;
2459                                 llog_ctxt_put(ctxt);
2460                                 break;
2461                         }
2462                         llog_ctxt_put(ctxt);
2463                 }
2464                 lod_putref(d, &d->lod_mdt_descs);
2465
2466                 RETURN(rc);
2467         }
2468
2469         RETURN(rc);
2470 }
2471
2472 static int lod_obd_set_info_async(const struct lu_env *env,
2473                                   struct obd_export *exp,
2474                                   u32 keylen, void *key,
2475                                   u32 vallen, void *val,
2476                                   struct ptlrpc_request_set *set)
2477 {
2478         struct obd_device *obd = class_exp2obd(exp);
2479         struct lod_device *d;
2480         struct lod_tgt_desc *tgt;
2481         int no_set = 0;
2482         int rc = 0, rc2;
2483
2484         ENTRY;
2485
2486         if (!set) {
2487                 no_set = 1;
2488                 set = ptlrpc_prep_set();
2489                 if (!set)
2490                         RETURN(-ENOMEM);
2491         }
2492
2493         d = lu2lod_dev(obd->obd_lu_dev);
2494         lod_getref(&d->lod_ost_descs);
2495         lod_foreach_ost(d, tgt) {
2496                 if (!tgt->ltd_active)
2497                         continue;
2498
2499                 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2500                                          vallen, val, set);
2501                 if (rc2 != 0 && rc == 0)
2502                         rc = rc2;
2503         }
2504         lod_putref(d, &d->lod_ost_descs);
2505
2506         lod_getref(&d->lod_mdt_descs);
2507         lod_foreach_mdt(d, tgt) {
2508                 if (!tgt->ltd_active)
2509                         continue;
2510                 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2511                                          vallen, val, set);
2512                 if (rc2 != 0 && rc == 0)
2513                         rc = rc2;
2514         }
2515         lod_putref(d, &d->lod_mdt_descs);
2516
2517
2518         if (no_set) {
2519                 rc2 = ptlrpc_set_wait(env, set);
2520                 if (rc2 == 0 && rc == 0)
2521                         rc = rc2;
2522                 ptlrpc_set_destroy(set);
2523         }
2524         RETURN(rc);
2525 }
2526
2527
2528 #define QMT0_DEV_NAME_LEN (LUSTRE_MAXFSNAME + sizeof("-QMT0000"))
2529 static struct obd_device *obd_find_qmt0(char *obd_name)
2530 {
2531         char qmt_name[QMT0_DEV_NAME_LEN];
2532         struct obd_device *qmt = NULL;
2533
2534         if (!server_name2fsname(obd_name, qmt_name, NULL)) {
2535                 strlcat(qmt_name, "-QMT0000", QMT0_DEV_NAME_LEN);
2536                 qmt = class_name2obd(qmt_name);
2537         }
2538
2539         return qmt;
2540 }
2541
2542 /* Run QMT0000 pool operations only for MDT0000 */
2543 static inline bool lod_pool_need_qmt0(const char *obd_name)
2544 {
2545         __u32 idx;
2546         int type;
2547
2548         type = server_name2index(obd_name, &idx, NULL);
2549
2550         return type == LDD_F_SV_TYPE_MDT && idx == 0;
2551 }
2552
2553 static int lod_pool_new_q(struct obd_device *obd, char *poolname)
2554 {
2555         int err = lod_pool_new(obd, poolname);
2556
2557         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2558                 obd = obd_find_qmt0(obd->obd_name);
2559                 if (obd)
2560                         obd_pool_new(obd, poolname);
2561         }
2562
2563         return err;
2564 }
2565
2566 static int lod_pool_remove_q(struct obd_device *obd, char *poolname,
2567                              char *ostname)
2568 {
2569         int err = lod_pool_remove(obd, poolname, ostname);
2570
2571         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2572                 obd = obd_find_qmt0(obd->obd_name);
2573                 if (obd)
2574                         obd_pool_rem(obd, poolname, ostname);
2575         }
2576
2577         return err;
2578 }
2579
2580 static int lod_pool_add_q(struct obd_device *obd, char *poolname, char *ostname)
2581 {
2582         int err = lod_pool_add(obd, poolname, ostname);
2583
2584         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2585                 obd = obd_find_qmt0(obd->obd_name);
2586                 if (obd)
2587                         obd_pool_add(obd, poolname, ostname);
2588         }
2589
2590         return err;
2591 }
2592
2593 static int lod_pool_del_q(struct obd_device *obd, char *poolname)
2594 {
2595         int err = lod_pool_del(obd, poolname);
2596
2597         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2598                 obd = obd_find_qmt0(obd->obd_name);
2599                 if (obd)
2600                         obd_pool_del(obd, poolname);
2601         }
2602
2603         return err;
2604 }
2605
2606 static inline int lod_sub_print_llog(const struct lu_env *env,
2607                                      struct dt_device *dt, void *data)
2608 {
2609         struct llog_ctxt *ctxt;
2610         int rc = 0;
2611
2612         ENTRY;
2613         ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
2614                                 LLOG_UPDATELOG_ORIG_CTXT);
2615         if (!ctxt)
2616                 RETURN(0);
2617
2618         if (ctxt->loc_handle) {
2619                 struct llog_print_data *lprd = data;
2620                 struct obd_ioctl_data *ioc_data = lprd->lprd_data;
2621                 int l, remains;
2622                 long from;
2623                 char *out;
2624
2625                 LASSERT(ioc_data);
2626                 if (ioc_data->ioc_inllen1 > 0) {
2627                         remains = ioc_data->ioc_inllen4 +
2628                                   round_up(ioc_data->ioc_inllen1, 8) +
2629                                   round_up(ioc_data->ioc_inllen2, 8) +
2630                                   round_up(ioc_data->ioc_inllen3, 8);
2631
2632                         rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
2633                         if (rc)
2634                                 GOTO(ctxt_put, rc);
2635
2636                         /* second iteration from jt_llog_print_iter() */
2637                         if (from > 1)
2638                                 GOTO(ctxt_put, rc = 0);
2639
2640                         out = ioc_data->ioc_bulk;
2641                         ioc_data->ioc_inllen1 = 0;
2642                 } else {
2643                         out = ioc_data->ioc_bulk + ioc_data->ioc_offset;
2644                         remains = ioc_data->ioc_count;
2645                 }
2646
2647                 l = snprintf(out, remains, "%s [catalog]: "DFID"\n",
2648                              ctxt->loc_obd->obd_name,
2649                              PLOGID(&ctxt->loc_handle->lgh_id));
2650                 out += l;
2651                 remains -= l;
2652                 if (remains <= 0) {
2653                         CERROR("%s: not enough space for print log records: rc = %d\n",
2654                                ctxt->loc_obd->obd_name, -LLOG_EEMPTY);
2655                         GOTO(ctxt_put, rc = -LLOG_EEMPTY);
2656                 }
2657
2658                 ioc_data->ioc_offset += l;
2659                 ioc_data->ioc_count = remains;
2660
2661                 rc = llog_process_or_fork(env, ctxt->loc_handle, llog_print_cb,
2662                                           data, NULL, false);
2663         }
2664         GOTO(ctxt_put, rc);
2665 ctxt_put:
2666         llog_ctxt_put(ctxt);
2667
2668         return rc;
2669 }
2670
2671 /* print update catalog and update logs FID of all sub devices */
2672 static int lod_llog_print(const struct lu_env *env, struct lod_device *lod,
2673                           void *data)
2674 {
2675         struct lod_tgt_desc *mdt;
2676         bool empty = true;
2677         int rc = 0;
2678
2679         ENTRY;
2680         rc = lod_sub_print_llog(env, lod->lod_child, data);
2681         if (!rc) {
2682                 empty = false;
2683         } else if (rc == -LLOG_EEMPTY) {
2684                 rc = 0;
2685         } else {
2686                 CERROR("%s: llog_print failed: rc = %d\n",
2687                        lod2obd(lod)->obd_name, rc);
2688                 RETURN(rc);
2689         }
2690
2691         lod_getref(&lod->lod_mdt_descs);
2692         lod_foreach_mdt(lod, mdt) {
2693                 rc = lod_sub_print_llog(env, mdt->ltd_tgt, data);
2694                 if (!rc) {
2695                         empty = false;
2696                 } else if (rc == -LLOG_EEMPTY) {
2697                         rc = 0;
2698                 } else {
2699                         CERROR("%s: llog_print of MDT %u failed: rc = %d\n",
2700                                lod2obd(lod)->obd_name, mdt->ltd_index, rc);
2701                         break;
2702                 }
2703         }
2704         lod_putref(lod, &lod->lod_mdt_descs);
2705
2706         RETURN(rc ? rc : empty ? -LLOG_EEMPTY : 0);
2707 }
2708
2709 /* cancel update catalog from update catlist */
2710 static int lod_llog_cancel(const struct lu_env *env, struct lod_device *lod)
2711 {
2712         struct lod_tgt_desc *tgt;
2713         int index;
2714         int rc;
2715         int rc2;
2716
2717         rc = lodname2mdt_index(lod2obd(lod)->obd_name, (__u32 *)&index);
2718         if (rc < 0)
2719                 return rc;
2720
2721         rc = lod_sub_cancel_llog(env, lod, lod->lod_child, index);
2722
2723         lod_getref(&lod->lod_mdt_descs);
2724         lod_foreach_mdt(lod, tgt) {
2725                 LASSERT(tgt && tgt->ltd_tgt);
2726                 rc2 = lod_sub_cancel_llog(env, lod, tgt->ltd_tgt,
2727                                           tgt->ltd_index);
2728                 if (rc2 && !rc)
2729                         rc = rc2;
2730         }
2731         lod_putref(lod, &lod->lod_mdt_descs);
2732
2733         return rc;
2734 }
2735
2736 static int lod_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2737                          void *karg, void __user *uarg)
2738 {
2739         struct obd_device *obd = exp->exp_obd;
2740         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2741         struct obd_ioctl_data *data = karg;
2742         struct lu_env env;
2743         int rc;
2744
2745         ENTRY;
2746         CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
2747                obd->obd_name, cmd, len, karg, uarg);
2748
2749         rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
2750         if (rc) {
2751                 CERROR("%s: can't initialize env: rc = %d\n",
2752                        lod2obd(lod)->obd_name, rc);
2753                 RETURN(rc);
2754         }
2755
2756         switch (cmd) {
2757         case OBD_IOC_LLOG_PRINT: {
2758                 struct llog_print_data lprd = {
2759                         .lprd_data = data,
2760                         .lprd_raw = data->ioc_u32_1,
2761                 };
2762                 char *logname;
2763
2764                 logname = data->ioc_inlbuf1;
2765                 if (strcmp(logname, lod_update_log_name) != 0) {
2766                         rc = -EINVAL;
2767                         CERROR("%s: llog iocontrol support %s only: rc = %d\n",
2768                                lod2obd(lod)->obd_name, lod_update_log_name, rc);
2769                         RETURN(rc);
2770                 }
2771
2772                 LASSERT(data->ioc_inllen1 > 0);
2773                 rc = lod_llog_print(&env, lod, &lprd);
2774                 break;
2775         }
2776         case OBD_IOC_LLOG_CANCEL:
2777                 rc = lod_llog_cancel(&env, lod);
2778                 break;
2779         default:
2780                 rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
2781                 break;
2782         }
2783         lu_env_fini(&env);
2784
2785         RETURN(rc);
2786 }
2787
2788 static const struct obd_ops lod_obd_device_ops = {
2789         .o_owner        = THIS_MODULE,
2790         .o_connect      = lod_obd_connect,
2791         .o_disconnect   = lod_obd_disconnect,
2792         .o_get_info     = lod_obd_get_info,
2793         .o_set_info_async = lod_obd_set_info_async,
2794         .o_pool_new     = lod_pool_new_q,
2795         .o_pool_rem     = lod_pool_remove_q,
2796         .o_pool_add     = lod_pool_add_q,
2797         .o_pool_del     = lod_pool_del_q,
2798         .o_iocontrol    = lod_iocontrol,
2799 };
2800
2801 static int __init lod_init(void)
2802 {
2803         struct obd_type *sym;
2804         int rc;
2805
2806         rc = lu_kmem_init(lod_caches);
2807         if (rc)
2808                 return rc;
2809
2810         rc = class_register_type(&lod_obd_device_ops, NULL, true,
2811                                  LUSTRE_LOD_NAME, &lod_device_type);
2812         if (rc) {
2813                 lu_kmem_fini(lod_caches);
2814                 return rc;
2815         }
2816
2817         /* create "lov" entry for compatibility purposes */
2818         sym = class_add_symlinks(LUSTRE_LOV_NAME, true);
2819         if (IS_ERR(sym)) {
2820                 rc = PTR_ERR(sym);
2821                 /* does real "lov" already exist ? */
2822                 if (rc == -EEXIST)
2823                         rc = 0;
2824         }
2825
2826         return rc;
2827 }
2828
2829 static void __exit lod_exit(void)
2830 {
2831         struct obd_type *sym = class_search_type(LUSTRE_LOV_NAME);
2832
2833         /* if this was never fully initialized by the lov layer
2834          * then we are responsible for freeing this obd_type
2835          */
2836         if (sym) {
2837                 /* final put if we manage this obd type */
2838                 if (sym->typ_sym_filter)
2839                         kobject_put(&sym->typ_kobj);
2840                 /* put reference taken by class_search_type */
2841                 kobject_put(&sym->typ_kobj);
2842         }
2843
2844         class_unregister_type(LUSTRE_LOD_NAME);
2845         lu_kmem_fini(lod_caches);
2846 }
2847
2848 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2849 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2850 MODULE_VERSION(LUSTRE_VERSION_STRING);
2851 MODULE_LICENSE("GPL");
2852
2853 module_init(lod_init);
2854 module_exit(lod_exit);