Whamcloud - gitweb
LU-15277 quota: don't print extra default quota info
[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         }
428         /* retain old catalog and create a new one */
429         lod_sub_recreate_llog(env, lod, dt, index);
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(CFS_FAIL_PRECHECK(OBD_FAIL_TGT_RECOVERY_CONNECT)) &&
474             lrd->lrd_ltd) {
475                 CFS_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                                 sub_tgt->ltd_active = !rc;
1108                         } else {
1109                                 lod_sub_fini_llog(env, sub_tgt->ltd_tgt,
1110                                                   NULL);
1111                                 sub_tgt->ltd_active = 0;
1112                         }
1113                         GOTO(out, rc);
1114                 }
1115
1116
1117                 if (strstr(param, PARAM_LOD) != NULL)
1118                         count = class_modify_config(lcfg, PARAM_LOD,
1119                                                     &lod->lod_dt_dev.dd_kobj);
1120                 else
1121                         count = class_modify_config(lcfg, PARAM_LOV,
1122                                                     &lod->lod_dt_dev.dd_kobj);
1123                 rc = count > 0 ? 0 : count;
1124                 GOTO(out, rc);
1125         }
1126         case LCFG_PRE_CLEANUP: {
1127                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1128                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1129                 CFS_FAIL_TIMEOUT(OBD_FAIL_TGT_RECOVERY_CONNECT, cfs_fail_val * 2);
1130                 next = &lod->lod_child->dd_lu_dev;
1131                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1132                 if (rc != 0)
1133                         CDEBUG(D_HA, "%s: can't process %u: %d\n",
1134                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1135
1136                 lod_sub_stop_recovery_threads(env, lod);
1137                 lod_fini_distribute_txn(env, lod);
1138                 lod_sub_fini_all_llogs(env, lod);
1139                 break;
1140         }
1141         case LCFG_CLEANUP: {
1142                 if (lod->lod_md_root) {
1143                         dt_object_put(env, &lod->lod_md_root->ldo_obj);
1144                         lod->lod_md_root = NULL;
1145                 }
1146
1147                 /*
1148                  * do cleanup on underlying storage only when
1149                  * all OSPs are cleaned up, as they use that OSD as well
1150                  */
1151                 lu_dev_del_linkage(dev->ld_site, dev);
1152                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
1153                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
1154                 next = &lod->lod_child->dd_lu_dev;
1155                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
1156                 if (rc)
1157                         CERROR("%s: can't process %u: rc = %d\n",
1158                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
1159
1160                 rc = obd_disconnect(lod->lod_child_exp);
1161                 if (rc)
1162                         CERROR("error in disconnect from storage: rc = %d\n",
1163                                rc);
1164                 break;
1165         }
1166         default:
1167                 CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
1168                        lcfg->lcfg_command);
1169                 rc = -EINVAL;
1170                 break;
1171         }
1172
1173 out:
1174         RETURN(rc);
1175 }
1176
1177 /**
1178  * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
1179  *
1180  * The method is called once the recovery is complete. This implementation
1181  * distributes the notification to all the known targets.
1182  *
1183  * see include/lu_object.h for the details
1184  */
1185 static int lod_recovery_complete(const struct lu_env *env,
1186                                  struct lu_device *dev)
1187 {
1188         struct lod_device *lod = lu2lod_dev(dev);
1189         struct lu_device *next = &lod->lod_child->dd_lu_dev;
1190         struct lod_tgt_desc *tgt;
1191         int rc;
1192
1193         ENTRY;
1194
1195         LASSERT(lod->lod_recovery_completed == 0);
1196         lod->lod_recovery_completed = 1;
1197
1198         rc = next->ld_ops->ldo_recovery_complete(env, next);
1199
1200         lod_getref(&lod->lod_ost_descs);
1201         if (lod->lod_ost_descs.ltd_tgts_size > 0) {
1202                 lod_foreach_ost(lod, tgt) {
1203                         LASSERT(tgt && tgt->ltd_tgt);
1204                         next = &tgt->ltd_tgt->dd_lu_dev;
1205                         rc = next->ld_ops->ldo_recovery_complete(env, next);
1206                         if (rc)
1207                                 CERROR("%s: can't complete recovery on #%d: rc = %d\n",
1208                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1209                                        rc);
1210                 }
1211         }
1212         lod_putref(lod, &lod->lod_ost_descs);
1213         RETURN(rc);
1214 }
1215
1216 /**
1217  * Init update logs on all sub device
1218  *
1219  * LOD initialize update logs on all of sub devices. Because the initialization
1220  * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
1221  * lod_object_init(), this API has to be called after LOD is initialized.
1222  * \param[in] env       execution environment
1223  * \param[in] lod       lod device
1224  *
1225  * \retval              0 if update log is initialized successfully.
1226  * \retval              negative errno if initialization fails.
1227  */
1228 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
1229 {
1230         struct lu_tgt_desc *mdt;
1231         int rc;
1232
1233         ENTRY;
1234
1235         /*
1236          * llog must be setup after LOD is initialized, because llog
1237          * initialization include FLD lookup
1238          */
1239         LASSERT(lod->lod_initialized);
1240
1241         /* Init the llog in its own stack */
1242         rc = lod_sub_init_llog(env, lod, lod->lod_child);
1243         if (rc < 0)
1244                 RETURN(rc);
1245
1246         lod_foreach_mdt(lod, mdt) {
1247                 lod_sub_init_llog(env, lod, mdt->ltd_tgt);
1248         }
1249
1250         RETURN(rc);
1251 }
1252
1253 #define UPDATE_LOG_MAX_AGE      (30 * 24 * 60 * 60)     /* 30 days, in sec */
1254
1255 static int lod_update_log_stale(const struct lu_env *env, struct dt_object *dto,
1256                                 struct lu_buf *buf)
1257 {
1258         struct lu_attr *attr = &lod_env_info(env)->lti_attr;
1259         struct llog_log_hdr *hdr;
1260         loff_t off = 0;
1261         int rc;
1262
1263         ENTRY;
1264         rc = dt_attr_get(env, dto, attr);
1265         if (rc)
1266                 RETURN(rc);
1267
1268         if (!(attr->la_valid & (LA_CTIME | LA_SIZE)))
1269                 RETURN(-EFAULT);
1270
1271         /* by default update log ctime is not set */
1272         if (attr->la_ctime == 0)
1273                 RETURN(0);
1274
1275         /* update log not expired yet */
1276         if (attr->la_ctime + UPDATE_LOG_MAX_AGE > ktime_get_real_seconds())
1277                 RETURN(0);
1278
1279         if (attr->la_size == 0)
1280                 RETURN(-EFAULT);
1281
1282         rc = dt_read(env, dto, buf, &off);
1283         if (rc < 0)
1284                 RETURN(rc);
1285
1286         hdr = (struct llog_log_hdr *)buf->lb_buf;
1287         if (LLOG_REC_HDR_NEEDS_SWABBING(&hdr->llh_hdr))
1288                 lustre_swab_llog_hdr(hdr);
1289         /* log header is sane and flag LLOG_F_MAX_AGE|LLOG_F_RM_ON_ERR is set */
1290         if (rc >= sizeof(*hdr) &&
1291             hdr->llh_hdr.lrh_type == LLOG_HDR_MAGIC &&
1292             (hdr->llh_flags & (LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR)) ==
1293             (LLOG_F_MAX_AGE | LLOG_F_RM_ON_ERR))
1294                 RETURN(1);
1295
1296         RETURN(0);
1297 }
1298
1299 /**
1300  * Reclaim stale update log.
1301  *
1302  * When update log is canceld (upon recovery abort), it's not destroy, but
1303  * canceled from catlist, and set ctime and LLOG_F_MAX_AGE|LLOG_F_RM_ON_ERR,
1304  * which is kept for debug. If it expired (more than UPDATE_LOG_MAX_AGE seconds
1305  * passed), destroy it to save space.
1306  */
1307 static int lod_update_log_gc(const struct lu_env *env, struct lod_device *lod,
1308                              struct dt_object *dir, struct dt_object *dto,
1309                              const char *name)
1310 {
1311         struct dt_device *dt = lod->lod_child;
1312         struct thandle *th;
1313         int rc;
1314
1315         ENTRY;
1316         th = dt_trans_create(env, dt);
1317         if (IS_ERR(th))
1318                 RETURN(PTR_ERR(th));
1319
1320         rc = dt_declare_delete(env, dir, (const struct dt_key *)name, th);
1321         if (rc)
1322                 GOTO(out_trans, rc);
1323
1324         rc = dt_declare_destroy(env, dto, th);
1325         if (rc)
1326                 GOTO(out_trans, rc);
1327
1328         rc = dt_trans_start_local(env, dt, th);
1329         if (rc)
1330                 GOTO(out_trans, rc);
1331
1332         rc = dt_delete(env, dir, (const struct dt_key *)name, th);
1333         if (rc)
1334                 GOTO(out_trans, rc);
1335
1336         rc = dt_destroy(env, dto, th);
1337         GOTO(out_trans, rc);
1338 out_trans:
1339         dt_trans_stop(env, dt, th);
1340
1341         return rc;
1342 }
1343
1344 /* reclaim stale update llogs under "update_log_dir" */
1345 static int lod_update_log_dir_gc(const struct lu_env *env,
1346                                  struct lod_device *lod,
1347                                  struct dt_object *dir)
1348 {
1349         struct lod_thread_info *info = lod_env_info(env);
1350         struct lu_buf *buf = &info->lti_linkea_buf;
1351         struct lu_dirent *ent = (struct lu_dirent *)info->lti_key;
1352         struct lu_fid *fid = &info->lti_fid;
1353         struct dt_it *it;
1354         const struct dt_it_ops *iops;
1355         struct dt_object *dto;
1356         int rc;
1357
1358         ENTRY;
1359
1360         if (unlikely(!dt_try_as_dir(env, dir, true)))
1361                 RETURN(-ENOTDIR);
1362
1363         lu_buf_alloc(buf, sizeof(struct llog_log_hdr));
1364         if (!buf->lb_buf)
1365                 RETURN(-ENOMEM);
1366
1367         iops = &dir->do_index_ops->dio_it;
1368         it = iops->init(env, dir, LUDA_64BITHASH);
1369         if (IS_ERR(it))
1370                 GOTO(out, rc = PTR_ERR(it));
1371
1372         rc = iops->load(env, it, 0);
1373         if (rc == 0)
1374                 rc = iops->next(env, it);
1375         else if (rc > 0)
1376                 rc = 0;
1377
1378         while (rc == 0) {
1379                 rc = iops->rec(env, it, (struct dt_rec *)ent, LUDA_64BITHASH);
1380                 if (rc != 0)
1381                         break;
1382
1383                 ent->lde_namelen = le16_to_cpu(ent->lde_namelen);
1384                 if (ent->lde_name[0] == '.') {
1385                         if (ent->lde_namelen == 1)
1386                                 goto next;
1387
1388                         if (ent->lde_namelen == 2 && ent->lde_name[1] == '.')
1389                                 goto next;
1390                 }
1391
1392                 fid_le_to_cpu(fid, &ent->lde_fid);
1393                 dto = dt_locate(env, lod->lod_child, fid);
1394                 if (IS_ERR(dto))
1395                         goto next;
1396
1397                 buf->lb_len = sizeof(struct llog_log_hdr);
1398                 if (lod_update_log_stale(env, dto, buf) == 1)
1399                         lod_update_log_gc(env, lod, dir, dto, ent->lde_name);
1400                 dt_object_put(env, dto);
1401 next:
1402                 rc = iops->next(env, it);
1403         }
1404
1405         iops->put(env, it);
1406         iops->fini(env, it);
1407 out:
1408         buf->lb_len = sizeof(struct llog_log_hdr);
1409         lu_buf_free(buf);
1410
1411         RETURN(rc > 0 ? 0 : rc);
1412 }
1413
1414 /**
1415  * Implementation of lu_device_operations::ldo_prepare() for LOD
1416  *
1417  * see include/lu_object.h for the details.
1418  */
1419 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
1420                        struct lu_device *cdev)
1421 {
1422         struct lod_device *lod = lu2lod_dev(cdev);
1423         struct lu_device *next = &lod->lod_child->dd_lu_dev;
1424         struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1425         int rc;
1426         struct dt_object *root;
1427         struct dt_object *dto;
1428         u32 index;
1429
1430         ENTRY;
1431
1432         rc = next->ld_ops->ldo_prepare(env, pdev, next);
1433         if (rc != 0) {
1434                 CERROR("%s: prepare bottom error: rc = %d\n",
1435                        lod2obd(lod)->obd_name, rc);
1436                 RETURN(rc);
1437         }
1438
1439         lod->lod_initialized = 1;
1440
1441         rc = dt_root_get(env, lod->lod_child, fid);
1442         if (rc < 0)
1443                 RETURN(rc);
1444
1445         root = dt_locate(env, lod->lod_child, fid);
1446         if (IS_ERR(root))
1447                 RETURN(PTR_ERR(root));
1448
1449         /* Create update log object */
1450         index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
1451         lu_update_log_fid(fid, index);
1452
1453         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1454                                                  fid, root,
1455                                                  lod_update_log_name,
1456                                                  S_IFREG | 0644);
1457         if (IS_ERR(dto))
1458                 GOTO(out_put, rc = PTR_ERR(dto));
1459
1460         dt_object_put(env, dto);
1461
1462         /* Create update log dir */
1463         lu_update_log_dir_fid(fid, index);
1464         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
1465                                                  fid, root,
1466                                                  lod_update_log_dir_name,
1467                                                  S_IFDIR | 0644);
1468         if (IS_ERR(dto))
1469                 GOTO(out_put, rc = PTR_ERR(dto));
1470
1471         lod_update_log_dir_gc(env, lod, dto);
1472         dt_object_put(env, dto);
1473
1474         rc = lod_prepare_distribute_txn(env, lod);
1475         if (rc != 0)
1476                 GOTO(out_put, rc);
1477
1478         rc = lod_sub_init_llogs(env, lod);
1479         if (rc != 0)
1480                 GOTO(out_put, rc);
1481
1482 out_put:
1483         dt_object_put(env, root);
1484
1485         RETURN(rc);
1486 }
1487
1488 /**
1489  * Implementation of lu_device_operations::ldo_fid_alloc() for LOD
1490  *
1491  * Find corresponding device by passed parent and name, and allocate FID from
1492  * there.
1493  *
1494  * see include/lu_object.h for the details.
1495  */
1496 static int lod_fid_alloc(const struct lu_env *env, struct lu_device *d,
1497                          struct lu_fid *fid, struct lu_object *parent,
1498                          const struct lu_name *name)
1499 {
1500         struct lod_device *lod = lu2lod_dev(d);
1501         struct lod_object *lo = lu2lod_obj(parent);
1502         struct dt_device *next;
1503         int rc;
1504
1505         ENTRY;
1506
1507         /* if @parent is remote, we don't know whether its layout was changed,
1508          * always reload layout.
1509          */
1510         if (lu_object_remote(parent))
1511                 lod_striping_free(env, lo);
1512
1513         rc = lod_striping_load(env, lo);
1514         if (rc)
1515                 RETURN(rc);
1516
1517         if (lo->ldo_dir_stripe_count > 0 && name) {
1518                 struct dt_object *stripe;
1519                 int idx;
1520
1521                 idx = __lmv_name_to_stripe_index(lo->ldo_dir_hash_type,
1522                                                  lo->ldo_dir_stripe_count,
1523                                                  lo->ldo_dir_migrate_hash,
1524                                                  lo->ldo_dir_migrate_offset,
1525                                                  name->ln_name,
1526                                                  name->ln_namelen, true);
1527                 if (idx < 0)
1528                         RETURN(idx);
1529
1530                 stripe = lo->ldo_stripe[idx];
1531                 if (!stripe || !dt_object_exists(stripe))
1532                         RETURN(-ENODEV);
1533
1534                 next = lu2dt_dev(stripe->do_lu.lo_dev);
1535         } else {
1536                 next = lod->lod_child;
1537         }
1538
1539         rc = dt_fid_alloc(env, next, fid, parent, name);
1540
1541         RETURN(rc);
1542 }
1543
1544 const struct lu_device_operations lod_lu_ops = {
1545         .ldo_object_alloc       = lod_object_alloc,
1546         .ldo_process_config     = lod_process_config,
1547         .ldo_recovery_complete  = lod_recovery_complete,
1548         .ldo_prepare            = lod_prepare,
1549         .ldo_fid_alloc          = lod_fid_alloc,
1550 };
1551
1552 /**
1553  * Implementation of dt_device_operations::dt_root_get() for LOD
1554  *
1555  * see include/dt_object.h for the details.
1556  */
1557 static int lod_root_get(const struct lu_env *env,
1558                         struct dt_device *dev, struct lu_fid *f)
1559 {
1560         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
1561 }
1562
1563 static void lod_statfs_sum(struct obd_statfs *sfs,
1564                              struct obd_statfs *ost_sfs, int *bs)
1565 {
1566         while (ost_sfs->os_bsize < *bs) {
1567                 *bs >>= 1;
1568                 sfs->os_bsize >>= 1;
1569                 sfs->os_bavail <<= 1;
1570                 sfs->os_blocks <<= 1;
1571                 sfs->os_bfree <<= 1;
1572                 sfs->os_granted <<= 1;
1573         }
1574         while (ost_sfs->os_bsize > *bs) {
1575                 ost_sfs->os_bsize >>= 1;
1576                 ost_sfs->os_bavail <<= 1;
1577                 ost_sfs->os_blocks <<= 1;
1578                 ost_sfs->os_bfree <<= 1;
1579                 ost_sfs->os_granted <<= 1;
1580         }
1581         sfs->os_bavail += ost_sfs->os_bavail;
1582         sfs->os_blocks += ost_sfs->os_blocks;
1583         sfs->os_bfree += ost_sfs->os_bfree;
1584         sfs->os_granted += ost_sfs->os_granted;
1585 }
1586
1587 /**
1588  * Implementation of dt_device_operations::dt_statfs() for LOD
1589  *
1590  * see include/dt_object.h for the details.
1591  */
1592 static int lod_statfs(const struct lu_env *env, struct dt_device *dev,
1593                       struct obd_statfs *sfs, struct obd_statfs_info *info)
1594 {
1595         struct lod_device *lod = dt2lod_dev(dev);
1596         struct lu_tgt_desc *tgt;
1597         struct obd_statfs ost_sfs;
1598         u64 ost_files = 0;
1599         u64 ost_ffree = 0;
1600         int rc, bs;
1601
1602         rc = dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
1603         if (rc)
1604                 GOTO(out, rc);
1605
1606         bs = sfs->os_bsize;
1607
1608         sfs->os_bavail = 0;
1609         sfs->os_blocks = 0;
1610         sfs->os_bfree = 0;
1611         sfs->os_granted = 0;
1612
1613         lod_getref(&lod->lod_mdt_descs);
1614         lod_foreach_mdt(lod, tgt) {
1615                 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1616                 /* ignore errors */
1617                 if (rc)
1618                         continue;
1619                 sfs->os_files += ost_sfs.os_files;
1620                 sfs->os_ffree += ost_sfs.os_ffree;
1621                 lod_statfs_sum(sfs, &ost_sfs, &bs);
1622         }
1623         lod_putref(lod, &lod->lod_mdt_descs);
1624
1625         /*
1626          * at some point we can check whether DoM is enabled and
1627          * decide how to account MDT space. for simplicity let's
1628          * just fallback to pre-DoM policy if any OST is alive
1629          */
1630         lod_getref(&lod->lod_ost_descs);
1631         lod_foreach_ost(lod, tgt) {
1632                 rc = dt_statfs(env, tgt->ltd_tgt, &ost_sfs);
1633                 /* ignore errors */
1634                 if (rc || ost_sfs.os_bsize == 0)
1635                         continue;
1636                 if (!ost_files) {
1637                         /*
1638                          * if only MDTs with DoM then report only MDT blocks,
1639                          * otherwise show only OST blocks, and DoM is "free"
1640                          */
1641                         sfs->os_bavail = 0;
1642                         sfs->os_blocks = 0;
1643                         sfs->os_bfree = 0;
1644                         sfs->os_granted = 0;
1645                 }
1646                 ost_files += ost_sfs.os_files;
1647                 ost_ffree += ost_sfs.os_ffree;
1648                 ost_sfs.os_bavail += ost_sfs.os_granted;
1649                 lod_statfs_sum(sfs, &ost_sfs, &bs);
1650                 LASSERTF(bs == ost_sfs.os_bsize, "%d != %d\n",
1651                         (int)sfs->os_bsize, (int)ost_sfs.os_bsize);
1652         }
1653         lod_putref(lod, &lod->lod_ost_descs);
1654         sfs->os_state |= OS_STATFS_SUM;
1655
1656         /* If we have _some_ OSTs, but don't have as many free objects on the
1657          * OSTs as inodes on the MDTs, reduce the reported number of inodes
1658          * to compensate, so that the "inodes in use" number is correct.
1659          * This should be kept in sync with ll_statfs_internal().
1660          */
1661         if (ost_files && ost_ffree < sfs->os_ffree) {
1662                 sfs->os_files = (sfs->os_files - sfs->os_ffree) + ost_ffree;
1663                 sfs->os_ffree = ost_ffree;
1664         }
1665
1666         /* a single successful statfs should be enough */
1667         rc = 0;
1668
1669 out:
1670         RETURN(rc);
1671 }
1672
1673 /**
1674  * Implementation of dt_device_operations::dt_trans_create() for LOD
1675  *
1676  * Creates a transaction using local (to this node) OSD.
1677  *
1678  * see include/dt_object.h for the details.
1679  */
1680 static struct thandle *lod_trans_create(const struct lu_env *env,
1681                                         struct dt_device *dt)
1682 {
1683         struct lod_thread_info *info = lod_env_info(env);
1684         struct thandle *th;
1685
1686         th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
1687         if (IS_ERR(th))
1688                 return th;
1689
1690         th->th_dev = dt;
1691
1692         /* initialize some lod_thread_info members */
1693         info->lti_obj_count = 0;
1694
1695         return th;
1696 }
1697
1698 /* distributed transaction failure may cause object missing or disconnected
1699  * directories, check space before transaction start.
1700  */
1701 static int lod_trans_space_check(const struct lu_env *env,
1702                                  struct lod_device *lod,
1703                                  struct thandle *th)
1704 {
1705         struct lod_thread_info *info = lod_env_info(env);
1706         struct obd_statfs *sfs = &info->lti_osfs;
1707         struct top_thandle *top_th = container_of(th, struct top_thandle,
1708                                                   tt_super);
1709         struct top_multiple_thandle *tmt = top_th->tt_multiple_thandle;
1710         struct sub_thandle *st;
1711         int rc;
1712
1713         if (likely(!tmt))
1714                 return 0;
1715
1716         list_for_each_entry(st, &tmt->tmt_sub_thandle_list, st_sub_list) {
1717                 struct dt_device *sub_dt;
1718
1719                 if (st->st_sub_th == NULL)
1720                         continue;
1721
1722                 if (st->st_sub_th == top_th->tt_master_sub_thandle)
1723                         continue;
1724
1725                 sub_dt = st->st_sub_th->th_dev;
1726                 rc = dt_statfs(env, sub_dt, sfs);
1727                 if (rc) {
1728                         CDEBUG(D_INFO, "%s: fail - statfs error: rc = %d\n",
1729                                sub_dt->dd_lu_dev.ld_obd->obd_name, rc);
1730                         /* statfs may fail during recovery, skip check */
1731                         if (!lod->lod_recovery_completed)
1732                                 rc = 0;
1733                         return rc;
1734                 }
1735
1736                 if (unlikely(sfs->os_state &
1737                              (OS_STATFS_ENOINO | OS_STATFS_ENOSPC))) {
1738                         CDEBUG(D_INFO, "%s: fail - target state %x: rc = %d\n",
1739                                sub_dt->dd_lu_dev.ld_obd->obd_name,
1740                                sfs->os_state, -ENOSPC);
1741                         return -ENOSPC;
1742                 }
1743         }
1744
1745         return 0;
1746 }
1747
1748 /**
1749  * Implementation of dt_device_operations::dt_trans_start() for LOD
1750  *
1751  * Starts the set of local transactions using the targets involved
1752  * in declare phase. Initial support for the distributed transactions.
1753  *
1754  * see include/dt_object.h for the details.
1755  */
1756 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
1757                            struct thandle *th)
1758 {
1759         struct lod_device *lod = dt2lod_dev(dt);
1760
1761         if (lod->lod_dist_txn_check_space) {
1762                 int rc;
1763
1764                 rc = lod_trans_space_check(env, lod, th);
1765                 if (rc)
1766                         return rc;
1767         }
1768
1769         return top_trans_start(env, lod->lod_child, th);
1770 }
1771
1772 static int lod_trans_cb_add(struct thandle *th,
1773                             struct dt_txn_commit_cb *dcb)
1774 {
1775         struct top_thandle      *top_th = container_of(th, struct top_thandle,
1776                                                        tt_super);
1777         return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
1778 }
1779
1780 /**
1781  * add noop update to the update records
1782  *
1783  * Add noop updates to the update records, which is only used in
1784  * test right now.
1785  *
1786  * \param[in] env       execution environment
1787  * \param[in] dt        dt device of lod
1788  * \param[in] th        thandle
1789  * \param[in] count     the count of update records to be added.
1790  *
1791  * \retval              0 if adding succeeds.
1792  * \retval              negative errno if adding fails.
1793  */
1794 static int lod_add_noop_records(const struct lu_env *env,
1795                                 struct dt_device *dt, struct thandle *th,
1796                                 int count)
1797 {
1798         struct top_thandle *top_th;
1799         struct lu_fid *fid = &lod_env_info(env)->lti_fid;
1800         int i;
1801         int rc = 0;
1802
1803         top_th = container_of(th, struct top_thandle, tt_super);
1804         if (!top_th->tt_multiple_thandle)
1805                 return 0;
1806
1807         fid_zero(fid);
1808         for (i = 0; i < count; i++) {
1809                 rc = update_record_pack(noop, th, fid);
1810                 if (rc < 0)
1811                         return rc;
1812         }
1813         return rc;
1814 }
1815
1816 /**
1817  * Implementation of dt_device_operations::dt_trans_stop() for LOD
1818  *
1819  * Stops the set of local transactions using the targets involved
1820  * in declare phase. Initial support for the distributed transactions.
1821  *
1822  * see include/dt_object.h for the details.
1823  */
1824 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
1825                           struct thandle *th)
1826 {
1827         if (CFS_FAIL_CHECK(OBD_FAIL_SPLIT_UPDATE_REC)) {
1828                 int rc;
1829
1830                 rc = lod_add_noop_records(env, dt, th, 5000);
1831                 if (rc < 0)
1832                         RETURN(rc);
1833         }
1834         return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
1835 }
1836
1837 /**
1838  * Implementation of dt_device_operations::dt_conf_get() for LOD
1839  *
1840  * Currently returns the configuration provided by the local OSD.
1841  *
1842  * see include/dt_object.h for the details.
1843  */
1844 static void lod_conf_get(const struct lu_env *env,
1845                          const struct dt_device *dev,
1846                          struct dt_device_param *param)
1847 {
1848         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1849 }
1850
1851 /**
1852  * Implementation of dt_device_operations::dt_sync() for LOD
1853  *
1854  * Syncs all known OST targets. Very very expensive and used
1855  * rarely by LFSCK now. Should not be used in general.
1856  *
1857  * see include/dt_object.h for the details.
1858  */
1859 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1860 {
1861         struct lod_device *lod = dt2lod_dev(dev);
1862         struct lu_tgt_desc *tgt;
1863         int rc = 0;
1864
1865         ENTRY;
1866
1867         lod_getref(&lod->lod_ost_descs);
1868         lod_foreach_ost(lod, tgt) {
1869                 if (tgt->ltd_discon)
1870                         continue;
1871                 rc = dt_sync(env, tgt->ltd_tgt);
1872                 if (rc) {
1873                         if (rc != -ENOTCONN) {
1874                                 CERROR("%s: can't sync ost %u: rc = %d\n",
1875                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1876                                        rc);
1877                                 break;
1878                         }
1879                         rc = 0;
1880                 }
1881         }
1882         lod_putref(lod, &lod->lod_ost_descs);
1883
1884         if (rc)
1885                 RETURN(rc);
1886
1887         lod_getref(&lod->lod_mdt_descs);
1888         lod_foreach_mdt(lod, tgt) {
1889                 if (tgt->ltd_discon)
1890                         continue;
1891                 rc = dt_sync(env, tgt->ltd_tgt);
1892                 if (rc) {
1893                         if (rc != -ENOTCONN) {
1894                                 CERROR("%s: can't sync mdt %u: rc = %d\n",
1895                                        lod2obd(lod)->obd_name, tgt->ltd_index,
1896                                        rc);
1897                                 break;
1898                         }
1899                         rc = 0;
1900                 }
1901         }
1902         lod_putref(lod, &lod->lod_mdt_descs);
1903
1904         if (rc == 0)
1905                 rc = dt_sync(env, lod->lod_child);
1906
1907         RETURN(rc);
1908 }
1909
1910 /**
1911  * Implementation of dt_device_operations::dt_ro() for LOD
1912  *
1913  * Turns local OSD read-only, used for the testing only.
1914  *
1915  * see include/dt_object.h for the details.
1916  */
1917 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1918 {
1919         return dt_ro(env, dt2lod_dev(dev)->lod_child);
1920 }
1921
1922 /**
1923  * Implementation of dt_device_operations::dt_commit_async() for LOD
1924  *
1925  * Asks local OSD to commit sooner.
1926  *
1927  * see include/dt_object.h for the details.
1928  */
1929 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1930 {
1931         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1932 }
1933
1934 static const struct dt_device_operations lod_dt_ops = {
1935         .dt_root_get         = lod_root_get,
1936         .dt_statfs           = lod_statfs,
1937         .dt_trans_create     = lod_trans_create,
1938         .dt_trans_start      = lod_trans_start,
1939         .dt_trans_stop       = lod_trans_stop,
1940         .dt_conf_get         = lod_conf_get,
1941         .dt_sync             = lod_sync,
1942         .dt_ro               = lod_ro,
1943         .dt_commit_async     = lod_commit_async,
1944         .dt_trans_cb_add     = lod_trans_cb_add,
1945 };
1946
1947 /**
1948  * Connect to a local OSD.
1949  *
1950  * Used to connect to the local OSD at mount. OSD name is taken from the
1951  * configuration command passed. This connection is used to identify LU
1952  * site and pin the OSD from early removal.
1953  *
1954  * \param[in] env               LU environment provided by the caller
1955  * \param[in] lod               lod device
1956  * \param[in] cfg               configuration command to apply
1957  *
1958  * \retval 0                    on success
1959  * \retval negative             negated errno on error
1960  **/
1961 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1962                               struct lustre_cfg *cfg)
1963 {
1964         struct obd_connect_data *data = NULL;
1965         struct obd_device *obd;
1966         char *nextdev = NULL, *p, *s;
1967         int rc, len = 0;
1968
1969         ENTRY;
1970
1971         LASSERT(cfg);
1972         LASSERT(lod->lod_child_exp == NULL);
1973
1974         /*
1975          * compatibility hack: we still use old config logs
1976          * which specify LOV, but we need to learn underlying
1977          * OSD device, which is supposed to be:
1978          *  <fsname>-MDTxxxx-osd
1979          *
1980          * 2.x MGS generates lines like the following:
1981          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
1982          * 1.8 MGS generates lines like the following:
1983          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
1984          *
1985          * we use "-MDT" to differentiate 2.x from 1.8
1986          */
1987         p = lustre_cfg_string(cfg, 0);
1988         if (p && strstr(p, "-mdtlov")) {
1989                 len = strlen(p) + 6;
1990                 OBD_ALLOC(nextdev, len);
1991                 if (!nextdev)
1992                         GOTO(out, rc = -ENOMEM);
1993
1994                 strcpy(nextdev, p);
1995                 s = strstr(nextdev, "-mdtlov");
1996                 if (unlikely(!s)) {
1997                         CERROR("%s: unable to parse device name: rc = %d\n",
1998                                lustre_cfg_string(cfg, 0), -EINVAL);
1999                         GOTO(out, rc = -EINVAL);
2000                 }
2001
2002                 if (strstr(nextdev, "-MDT")) {
2003                         /* 2.x config */
2004                         strcpy(s, "-osd");
2005                 } else {
2006                         /* 1.8 config */
2007                         strcpy(s, "-MDT0000-osd");
2008                 }
2009         } else {
2010                 CERROR("%s: unable to parse device name: rc = %d\n",
2011                        lustre_cfg_string(cfg, 0), -EINVAL);
2012                 GOTO(out, rc = -EINVAL);
2013         }
2014
2015         OBD_ALLOC_PTR(data);
2016         if (!data)
2017                 GOTO(out, rc = -ENOMEM);
2018
2019         obd = class_name2obd(nextdev);
2020         if (!obd) {
2021                 CERROR("%s: can not locate next device: rc = %d\n",
2022                        nextdev, -ENOTCONN);
2023                 GOTO(out, rc = -ENOTCONN);
2024         }
2025
2026         data->ocd_connect_flags = OBD_CONNECT_VERSION;
2027         data->ocd_version = LUSTRE_VERSION_CODE;
2028
2029         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
2030                          data, NULL);
2031         if (rc) {
2032                 CERROR("%s: cannot connect to next dev: rc = %d\n",
2033                        nextdev, rc);
2034                 GOTO(out, rc);
2035         }
2036
2037         lod->lod_dt_dev.dd_lu_dev.ld_site =
2038                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
2039         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
2040         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
2041
2042 out:
2043         if (data)
2044                 OBD_FREE_PTR(data);
2045         if (nextdev)
2046                 OBD_FREE(nextdev, len);
2047         RETURN(rc);
2048 }
2049
2050 static int lod_lsfs_init(const struct lu_env *env, struct lod_device *d)
2051 {
2052         struct obd_statfs sfs;
2053         int rc;
2054
2055         rc = dt_statfs(env, d->lod_child, &sfs);
2056         if (rc) {
2057                 CDEBUG(D_LAYOUT, "%s: failed to get OSD statfs, rc = %d\n",
2058                        lod2obd(d)->obd_name, rc);
2059                 return rc;
2060         }
2061
2062         /* udpate local OSD cached statfs data */
2063         spin_lock_init(&d->lod_lsfs_lock);
2064         d->lod_lsfs_age = ktime_get_seconds();
2065         d->lod_lsfs_total_mb = (sfs.os_blocks * sfs.os_bsize) >> 20;
2066         d->lod_lsfs_free_mb = (sfs.os_bfree * sfs.os_bsize) >> 20;
2067         return 0;
2068 }
2069
2070 /**
2071  * Initialize LOD device at setup.
2072  *
2073  * Initializes the given LOD device using the original configuration command.
2074  * The function initiates a connection to the local OSD and initializes few
2075  * internal structures like pools, target tables, etc.
2076  *
2077  * \param[in] env               LU environment provided by the caller
2078  * \param[in] lod               lod device
2079  * \param[in] ldt               not used
2080  * \param[in] cfg               configuration command
2081  *
2082  * \retval 0                    on success
2083  * \retval negative             negated errno on error
2084  **/
2085 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
2086                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
2087 {
2088         struct dt_device_param ddp;
2089         struct obd_device *obd;
2090         int rc;
2091
2092         ENTRY;
2093
2094         obd = class_name2obd(lustre_cfg_string(cfg, 0));
2095         if (!obd) {
2096                 rc = -ENODEV;
2097                 CERROR("Cannot find obd with name '%s': rc = %d\n",
2098                        lustre_cfg_string(cfg, 0), rc);
2099                 RETURN(rc);
2100         }
2101
2102         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
2103         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
2104         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
2105         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
2106
2107         rc = lod_connect_to_osd(env, lod, cfg);
2108         if (rc)
2109                 RETURN(rc);
2110
2111         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
2112         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
2113         lod->lod_dom_stripesize_max_kb = (1ULL << 10); /* 1Mb is default */
2114         lod->lod_max_stripecount = 0;
2115
2116         /* initialize local statfs cached values */
2117         rc = lod_lsfs_init(env, lod);
2118         if (rc)
2119                 GOTO(out_disconnect, rc);
2120
2121         /* default threshold as half of total space, in MiB */
2122         lod->lod_dom_threshold_free_mb = lod->lod_lsfs_total_mb / 2;
2123         /* set default DoM stripe size based on free space amount */
2124         lod_dom_stripesize_recalc(lod);
2125
2126         /* setup obd to be used with old lov code */
2127         rc = lod_pools_init(lod, cfg);
2128         if (rc)
2129                 GOTO(out_disconnect, rc);
2130
2131         rc = lod_procfs_init(lod);
2132         if (rc)
2133                 GOTO(out_pools, rc);
2134
2135         spin_lock_init(&lod->lod_lock);
2136         spin_lock_init(&lod->lod_connects_lock);
2137         lu_tgt_descs_init(&lod->lod_mdt_descs, true);
2138         lu_tgt_descs_init(&lod->lod_ost_descs, false);
2139         lu_qos_rr_init(&lod->lod_mdt_descs.ltd_qos.lq_rr);
2140         lu_qos_rr_init(&lod->lod_ost_descs.ltd_qos.lq_rr);
2141         lod->lod_dist_txn_check_space = 1;
2142
2143         RETURN(0);
2144
2145 out_pools:
2146         lod_pools_fini(lod);
2147 out_disconnect:
2148         obd_disconnect(lod->lod_child_exp);
2149         RETURN(rc);
2150 }
2151
2152 /**
2153  * Implementation of lu_device_type_operations::ldto_device_free() for LOD
2154  *
2155  * Releases the memory allocated for LOD device.
2156  *
2157  * see include/lu_object.h for the details.
2158  */
2159 static struct lu_device *lod_device_free(const struct lu_env *env,
2160                                          struct lu_device *lu)
2161 {
2162         struct lod_device *lod = lu2lod_dev(lu);
2163         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
2164
2165         ENTRY;
2166
2167         if (atomic_read(&lu->ld_site->ls_obj_hash.nelems)) {
2168                 lu_site_print(env, lu->ld_site, &lu->ld_ref, D_ERROR,
2169                               lu_cdebug_printer);
2170         }
2171         LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %px\n", lu);
2172         dt_device_fini(&lod->lod_dt_dev);
2173         OBD_FREE_PTR(lod);
2174         RETURN(next);
2175 }
2176
2177 /**
2178  * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
2179  *
2180  * Allocates LOD device and calls the helpers to initialize it.
2181  *
2182  * see include/lu_object.h for the details.
2183  */
2184 static struct lu_device *lod_device_alloc(const struct lu_env *env,
2185                                           struct lu_device_type *type,
2186                                           struct lustre_cfg *lcfg)
2187 {
2188         struct lod_device *lod;
2189         struct lu_device *lu_dev;
2190
2191         OBD_ALLOC_PTR(lod);
2192         if (!lod) {
2193                 lu_dev = ERR_PTR(-ENOMEM);
2194         } else {
2195                 int rc;
2196
2197                 lu_dev = lod2lu_dev(lod);
2198                 dt_device_init(&lod->lod_dt_dev, type);
2199                 rc = lod_init0(env, lod, type, lcfg);
2200                 if (rc != 0) {
2201                         lod_device_free(env, lu_dev);
2202                         lu_dev = ERR_PTR(rc);
2203                 }
2204         }
2205
2206         return lu_dev;
2207 }
2208
2209 static void lod_avoid_guide_fini(struct lod_avoid_guide *lag)
2210 {
2211         if (lag->lag_oss_avoid_array)
2212                 OBD_FREE_PTR_ARRAY(lag->lag_oss_avoid_array,
2213                                    lag->lag_oaa_size);
2214         bitmap_free(lag->lag_ost_avoid_bitmap);
2215 }
2216
2217 /**
2218  * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
2219  *
2220  * Releases the internal resources used by LOD device.
2221  *
2222  * see include/lu_object.h for the details.
2223  */
2224 static struct lu_device *lod_device_fini(const struct lu_env *env,
2225                                          struct lu_device *d)
2226 {
2227         struct lod_device *lod = lu2lod_dev(d);
2228         int rc;
2229
2230         ENTRY;
2231
2232         lod_pools_fini(lod);
2233
2234         lod_procfs_fini(lod);
2235
2236         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs);
2237         if (rc)
2238                 CERROR("%s: can not fini ost descriptors: rc =  %d\n",
2239                         lod2obd(lod)->obd_name, rc);
2240
2241         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs);
2242         if (rc)
2243                 CERROR("%s: can not fini mdt descriptors: rc =  %d\n",
2244                         lod2obd(lod)->obd_name, rc);
2245
2246         RETURN(NULL);
2247 }
2248
2249 /**
2250  * Implementation of obd_ops::o_connect() for LOD
2251  *
2252  * Used to track all the users of this specific LOD device,
2253  * so the device stays up until the last user disconnected.
2254  *
2255  * \param[in] env               LU environment provided by the caller
2256  * \param[out] exp              export the caller will be using to access LOD
2257  * \param[in] obd               OBD device representing LOD device
2258  * \param[in] cluuid            unique identifier of the caller
2259  * \param[in] data              not used
2260  * \param[in] localdata         not used
2261  *
2262  * \retval 0                    on success
2263  * \retval negative             negated errno on error
2264  **/
2265 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
2266                            struct obd_device *obd, struct obd_uuid *cluuid,
2267                            struct obd_connect_data *data, void *localdata)
2268 {
2269         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2270         struct lustre_handle conn;
2271         int rc;
2272
2273         ENTRY;
2274
2275         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
2276
2277         rc = class_connect(&conn, obd, cluuid);
2278         if (rc)
2279                 RETURN(rc);
2280
2281         *exp = class_conn2export(&conn);
2282
2283         spin_lock(&lod->lod_connects_lock);
2284         lod->lod_connects++;
2285         /* at the moment we expect the only user */
2286         LASSERT(lod->lod_connects == 1);
2287         spin_unlock(&lod->lod_connects_lock);
2288
2289         RETURN(0);
2290 }
2291
2292 /**
2293  *
2294  * Implementation of obd_ops::o_disconnect() for LOD
2295  *
2296  * When the caller doesn't need to use this LOD instance, it calls
2297  * obd_disconnect() and LOD releases corresponding export/reference count.
2298  * Once all the users gone, LOD device is released.
2299  *
2300  * \param[in] exp               export provided to the caller in obd_connect()
2301  *
2302  * \retval 0                    on success
2303  * \retval negative             negated errno on error
2304  **/
2305 static int lod_obd_disconnect(struct obd_export *exp)
2306 {
2307         struct obd_device *obd = exp->exp_obd;
2308         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2309         int rc, release = 0;
2310
2311         ENTRY;
2312
2313         /* Only disconnect the underlying layers on the final disconnect. */
2314         spin_lock(&lod->lod_connects_lock);
2315         lod->lod_connects--;
2316         if (lod->lod_connects != 0) {
2317                 /* why should there be more than 1 connect? */
2318                 spin_unlock(&lod->lod_connects_lock);
2319                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
2320                        lod->lod_connects);
2321                 goto out;
2322         }
2323         spin_unlock(&lod->lod_connects_lock);
2324
2325         /* the last user of lod has gone, let's release the device */
2326         release = 1;
2327
2328 out:
2329         rc = class_disconnect(exp); /* bz 9811 */
2330
2331         if (rc == 0 && release)
2332                 class_manual_cleanup(obd);
2333         RETURN(rc);
2334 }
2335
2336 LU_KEY_INIT(lod, struct lod_thread_info);
2337
2338 static void lod_key_fini(const struct lu_context *ctx,
2339                 struct lu_context_key *key, void *data)
2340 {
2341         struct lod_thread_info *info = data;
2342         struct lod_layout_component *lds =
2343                                 info->lti_def_striping.lds_def_comp_entries;
2344
2345         /*
2346          * allocated in lod_get_lov_ea
2347          * XXX: this is overload, a tread may have such store but used only
2348          * once. Probably better would be pool of such stores per LOD.
2349          */
2350         if (info->lti_ea_store) {
2351                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
2352                 info->lti_ea_store = NULL;
2353                 info->lti_ea_store_size = 0;
2354         }
2355         lu_buf_free(&info->lti_linkea_buf);
2356
2357         if (lds)
2358                 lod_free_def_comp_entries(&info->lti_def_striping);
2359
2360         if (info->lti_comp_size > 0)
2361                 OBD_FREE_PTR_ARRAY(info->lti_comp_idx,
2362                                    info->lti_comp_size);
2363
2364         lod_avoid_guide_fini(&info->lti_avoid);
2365
2366         OBD_FREE_PTR(info);
2367 }
2368
2369 /* context key: lod_thread_key */
2370 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
2371
2372 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
2373
2374 static const struct lu_device_type_operations lod_device_type_ops = {
2375         .ldto_init              = lod_type_init,
2376         .ldto_fini              = lod_type_fini,
2377
2378         .ldto_start             = lod_type_start,
2379         .ldto_stop              = lod_type_stop,
2380
2381         .ldto_device_alloc      = lod_device_alloc,
2382         .ldto_device_free       = lod_device_free,
2383
2384         .ldto_device_fini       = lod_device_fini
2385 };
2386
2387 static struct lu_device_type lod_device_type = {
2388         .ldt_tags     = LU_DEVICE_DT,
2389         .ldt_name     = LUSTRE_LOD_NAME,
2390         .ldt_ops      = &lod_device_type_ops,
2391         .ldt_ctx_tags = LCT_MD_THREAD,
2392 };
2393
2394 /**
2395  * Implementation of obd_ops::o_get_info() for LOD
2396  *
2397  * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
2398  * the caller binary status whether LOD has seen connection to any OST target.
2399  * It will also check if the MDT update log context being initialized (if
2400  * needed).
2401  *
2402  * \param[in] env               LU environment provided by the caller
2403  * \param[in] exp               export of the caller
2404  * \param[in] keylen            len of the key
2405  * \param[in] key               the key
2406  * \param[in] vallen            not used
2407  * \param[in] val               not used
2408  *
2409  * \retval                      0 if a connection was seen
2410  * \retval                      -EAGAIN if LOD isn't running yet or no
2411  *                              connection has been seen yet
2412  * \retval                      -EINVAL if not supported key is requested
2413  **/
2414 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
2415                             u32 keylen, void *key, u32 *vallen, void *val)
2416 {
2417         int rc = -EINVAL;
2418
2419         if (KEY_IS(KEY_OSP_CONNECTED)) {
2420                 struct obd_device *obd = exp->exp_obd;
2421                 struct lod_device *d;
2422                 struct lod_tgt_desc *tgt;
2423                 int rc = 1;
2424
2425                 if (!obd->obd_set_up || obd->obd_stopping)
2426                         RETURN(-EAGAIN);
2427
2428                 d = lu2lod_dev(obd->obd_lu_dev);
2429                 lod_getref(&d->lod_ost_descs);
2430                 lod_foreach_ost(d, tgt) {
2431                         rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
2432                                           vallen, val);
2433                         /* one healthy device is enough */
2434                         if (rc == 0)
2435                                 break;
2436                 }
2437                 lod_putref(d, &d->lod_ost_descs);
2438
2439                 lod_getref(&d->lod_mdt_descs);
2440                 lod_foreach_mdt(d, tgt) {
2441                         struct llog_ctxt *ctxt;
2442                         struct obd_device *ld = tgt->ltd_tgt->dd_lu_dev.ld_obd;
2443
2444                         if (!tgt->ltd_active)
2445                                 continue;
2446
2447                         ctxt = llog_get_context(ld, LLOG_UPDATELOG_ORIG_CTXT);
2448                         LASSERT(ctxt != NULL);
2449                         if (!ctxt->loc_handle) {
2450                                 CDEBUG(D_INFO, "%s: %s is not ready(%p).\n",
2451                                        obd->obd_name, ld->obd_name, ctxt);
2452                                 llog_ctxt_put(ctxt);
2453                                 rc = -EAGAIN;
2454                                 break;
2455                         }
2456                         llog_ctxt_put(ctxt);
2457                 }
2458                 lod_putref(d, &d->lod_mdt_descs);
2459
2460                 RETURN(rc);
2461         }
2462
2463         RETURN(rc);
2464 }
2465
2466 static int lod_obd_set_info_async(const struct lu_env *env,
2467                                   struct obd_export *exp,
2468                                   u32 keylen, void *key,
2469                                   u32 vallen, void *val,
2470                                   struct ptlrpc_request_set *set)
2471 {
2472         struct obd_device *obd = class_exp2obd(exp);
2473         struct lod_device *d;
2474         struct lod_tgt_desc *tgt;
2475         int no_set = 0;
2476         int rc = 0, rc2;
2477
2478         ENTRY;
2479
2480         if (!set) {
2481                 no_set = 1;
2482                 set = ptlrpc_prep_set();
2483                 if (!set)
2484                         RETURN(-ENOMEM);
2485         }
2486
2487         d = lu2lod_dev(obd->obd_lu_dev);
2488         lod_getref(&d->lod_ost_descs);
2489         lod_foreach_ost(d, tgt) {
2490                 if (tgt->ltd_discon)
2491                         continue;
2492
2493                 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2494                                          vallen, val, set);
2495                 if (rc2 != 0 && rc == 0)
2496                         rc = rc2;
2497         }
2498         lod_putref(d, &d->lod_ost_descs);
2499
2500         lod_getref(&d->lod_mdt_descs);
2501         lod_foreach_mdt(d, tgt) {
2502                 if (tgt->ltd_discon)
2503                         continue;
2504
2505                 rc2 = obd_set_info_async(env, tgt->ltd_exp, keylen, key,
2506                                          vallen, val, set);
2507                 if (rc2 != 0 && rc == 0)
2508                         rc = rc2;
2509         }
2510         lod_putref(d, &d->lod_mdt_descs);
2511
2512
2513         if (no_set) {
2514                 rc2 = ptlrpc_set_wait(env, set);
2515                 if (rc2 == 0 && rc == 0)
2516                         rc = rc2;
2517                 ptlrpc_set_destroy(set);
2518         }
2519         RETURN(rc);
2520 }
2521
2522
2523 #define QMT0_DEV_NAME_LEN (LUSTRE_MAXFSNAME + sizeof("-QMT0000"))
2524 static struct obd_device *obd_find_qmt0(char *obd_name)
2525 {
2526         char qmt_name[QMT0_DEV_NAME_LEN];
2527         struct obd_device *qmt = NULL;
2528
2529         if (!server_name2fsname(obd_name, qmt_name, NULL)) {
2530                 strlcat(qmt_name, "-QMT0000", QMT0_DEV_NAME_LEN);
2531                 qmt = class_name2obd(qmt_name);
2532         }
2533
2534         return qmt;
2535 }
2536
2537 /* Run QMT0000 pool operations only for MDT0000 */
2538 static inline bool lod_pool_need_qmt0(const char *obd_name)
2539 {
2540         __u32 idx;
2541         int type;
2542
2543         type = server_name2index(obd_name, &idx, NULL);
2544
2545         return type == LDD_F_SV_TYPE_MDT && idx == 0;
2546 }
2547
2548 static int lod_pool_new_q(struct obd_device *obd, char *poolname)
2549 {
2550         int err = lod_pool_new(obd, poolname);
2551
2552         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2553                 obd = obd_find_qmt0(obd->obd_name);
2554                 if (obd)
2555                         obd_pool_new(obd, poolname);
2556         }
2557
2558         return err;
2559 }
2560
2561 static int lod_pool_remove_q(struct obd_device *obd, char *poolname,
2562                              char *ostname)
2563 {
2564         int err = lod_pool_remove(obd, poolname, ostname);
2565
2566         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2567                 obd = obd_find_qmt0(obd->obd_name);
2568                 if (obd)
2569                         obd_pool_rem(obd, poolname, ostname);
2570         }
2571
2572         return err;
2573 }
2574
2575 static int lod_pool_add_q(struct obd_device *obd, char *poolname, char *ostname)
2576 {
2577         int err = lod_pool_add(obd, poolname, ostname);
2578
2579         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2580                 obd = obd_find_qmt0(obd->obd_name);
2581                 if (obd)
2582                         obd_pool_add(obd, poolname, ostname);
2583         }
2584
2585         return err;
2586 }
2587
2588 static int lod_pool_del_q(struct obd_device *obd, char *poolname)
2589 {
2590         int err = lod_pool_del(obd, poolname);
2591
2592         if (!err && lod_pool_need_qmt0(obd->obd_name)) {
2593                 obd = obd_find_qmt0(obd->obd_name);
2594                 if (obd)
2595                         obd_pool_del(obd, poolname);
2596         }
2597
2598         return err;
2599 }
2600
2601 static inline int lod_sub_print_llog(const struct lu_env *env,
2602                                      struct dt_device *dt, void *data)
2603 {
2604         struct llog_ctxt *ctxt;
2605         int rc = 0;
2606
2607         ENTRY;
2608         ctxt = llog_get_context(dt->dd_lu_dev.ld_obd,
2609                                 LLOG_UPDATELOG_ORIG_CTXT);
2610         if (!ctxt)
2611                 RETURN(0);
2612
2613         if (ctxt->loc_handle) {
2614                 struct llog_print_data *lprd = data;
2615                 struct obd_ioctl_data *ioc_data = lprd->lprd_data;
2616                 int l, remains;
2617                 long from;
2618                 char *out;
2619
2620                 LASSERT(ioc_data);
2621                 if (ioc_data->ioc_inllen1 > 0) {
2622                         remains = ioc_data->ioc_inllen4 +
2623                                   round_up(ioc_data->ioc_inllen1, 8) +
2624                                   round_up(ioc_data->ioc_inllen2, 8) +
2625                                   round_up(ioc_data->ioc_inllen3, 8);
2626
2627                         rc = kstrtol(ioc_data->ioc_inlbuf2, 0, &from);
2628                         if (rc)
2629                                 GOTO(ctxt_put, rc);
2630
2631                         /* second iteration from jt_llog_print_iter() */
2632                         if (from > 1)
2633                                 GOTO(ctxt_put, rc = 0);
2634
2635                         out = ioc_data->ioc_bulk;
2636                         ioc_data->ioc_inllen1 = 0;
2637                 } else {
2638                         out = ioc_data->ioc_bulk + ioc_data->ioc_offset;
2639                         remains = ioc_data->ioc_count;
2640                 }
2641
2642                 l = snprintf(out, remains, "%s [catalog]: "DFID"\n",
2643                              ctxt->loc_obd->obd_name,
2644                              PLOGID(&ctxt->loc_handle->lgh_id));
2645                 out += l;
2646                 remains -= l;
2647                 if (remains <= 0) {
2648                         CERROR("%s: not enough space for print log records: rc = %d\n",
2649                                ctxt->loc_obd->obd_name, -LLOG_EEMPTY);
2650                         GOTO(ctxt_put, rc = -LLOG_EEMPTY);
2651                 }
2652
2653                 ioc_data->ioc_offset += l;
2654                 ioc_data->ioc_count = remains;
2655
2656                 rc = llog_process_or_fork(env, ctxt->loc_handle, llog_print_cb,
2657                                           data, NULL, false);
2658         }
2659         GOTO(ctxt_put, rc);
2660 ctxt_put:
2661         llog_ctxt_put(ctxt);
2662
2663         return rc;
2664 }
2665
2666 /* print update catalog and update logs FID of all sub devices */
2667 static int lod_llog_print(const struct lu_env *env, struct lod_device *lod,
2668                           void *data)
2669 {
2670         struct lod_tgt_desc *mdt;
2671         bool empty = true;
2672         int rc = 0;
2673
2674         ENTRY;
2675         rc = lod_sub_print_llog(env, lod->lod_child, data);
2676         if (!rc) {
2677                 empty = false;
2678         } else if (rc == -LLOG_EEMPTY) {
2679                 rc = 0;
2680         } else {
2681                 CERROR("%s: llog_print failed: rc = %d\n",
2682                        lod2obd(lod)->obd_name, rc);
2683                 RETURN(rc);
2684         }
2685
2686         lod_getref(&lod->lod_mdt_descs);
2687         lod_foreach_mdt(lod, mdt) {
2688                 rc = lod_sub_print_llog(env, mdt->ltd_tgt, data);
2689                 if (!rc) {
2690                         empty = false;
2691                 } else if (rc == -LLOG_EEMPTY) {
2692                         rc = 0;
2693                 } else {
2694                         CERROR("%s: llog_print of MDT %u failed: rc = %d\n",
2695                                lod2obd(lod)->obd_name, mdt->ltd_index, rc);
2696                         break;
2697                 }
2698         }
2699         lod_putref(lod, &lod->lod_mdt_descs);
2700
2701         RETURN(rc ? rc : empty ? -LLOG_EEMPTY : 0);
2702 }
2703
2704 /* cancel update catalog from update catlist */
2705 static int lod_llog_cancel(const struct lu_env *env, struct lod_device *lod)
2706 {
2707         struct lod_tgt_desc *tgt;
2708         int index;
2709         int rc;
2710         int rc2;
2711
2712         rc = lodname2mdt_index(lod2obd(lod)->obd_name, (__u32 *)&index);
2713         if (rc < 0)
2714                 return rc;
2715
2716         rc = lod_sub_cancel_llog(env, lod, lod->lod_child, index);
2717
2718         lod_getref(&lod->lod_mdt_descs);
2719         lod_foreach_mdt(lod, tgt) {
2720                 LASSERT(tgt && tgt->ltd_tgt);
2721                 rc2 = lod_sub_cancel_llog(env, lod, tgt->ltd_tgt,
2722                                           tgt->ltd_index);
2723                 if (rc2 && !rc)
2724                         rc = rc2;
2725         }
2726         lod_putref(lod, &lod->lod_mdt_descs);
2727
2728         return rc;
2729 }
2730
2731 static int lod_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
2732                          void *karg, void __user *uarg)
2733 {
2734         struct obd_device *obd = exp->exp_obd;
2735         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
2736         struct obd_ioctl_data *data;
2737         struct lu_env env;
2738         int rc;
2739
2740         ENTRY;
2741         CDEBUG(D_IOCTL, "%s: cmd=%x len=%u karg=%pK uarg=%pK\n",
2742                obd->obd_name, cmd, len, karg, uarg);
2743         if (unlikely(karg == NULL))
2744                 RETURN(OBD_IOC_ERROR(obd->obd_name, cmd, "karg=NULL", -EINVAL));
2745         data = karg;
2746
2747         rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
2748         if (rc) {
2749                 CERROR("%s: can't initialize env: rc = %d\n",
2750                        lod2obd(lod)->obd_name, rc);
2751                 RETURN(rc);
2752         }
2753
2754         switch (cmd) {
2755         case OBD_IOC_LLOG_PRINT: {
2756                 struct llog_print_data lprd = {
2757                         .lprd_data = data,
2758                         .lprd_raw = data->ioc_u32_1,
2759                 };
2760                 char *logname;
2761
2762                 logname = data->ioc_inlbuf1;
2763                 if (strcmp(logname, lod_update_log_name) != 0) {
2764                         rc = -EINVAL;
2765                         CERROR("%s: llog iocontrol support %s only: rc = %d\n",
2766                                lod2obd(lod)->obd_name, lod_update_log_name, rc);
2767                         RETURN(rc);
2768                 }
2769
2770                 LASSERT(data->ioc_inllen1 > 0);
2771                 rc = lod_llog_print(&env, lod, &lprd);
2772                 break;
2773         }
2774         case OBD_IOC_LLOG_CANCEL:
2775                 rc = lod_llog_cancel(&env, lod);
2776                 break;
2777         default:
2778                 rc = OBD_IOC_ERROR(obd->obd_name, cmd, "unrecognized", -ENOTTY);
2779                 break;
2780         }
2781         lu_env_fini(&env);
2782
2783         RETURN(rc);
2784 }
2785
2786 static const struct obd_ops lod_obd_device_ops = {
2787         .o_owner        = THIS_MODULE,
2788         .o_connect      = lod_obd_connect,
2789         .o_disconnect   = lod_obd_disconnect,
2790         .o_get_info     = lod_obd_get_info,
2791         .o_set_info_async = lod_obd_set_info_async,
2792         .o_pool_new     = lod_pool_new_q,
2793         .o_pool_rem     = lod_pool_remove_q,
2794         .o_pool_add     = lod_pool_add_q,
2795         .o_pool_del     = lod_pool_del_q,
2796         .o_iocontrol    = lod_iocontrol,
2797 };
2798
2799 static int __init lod_init(void)
2800 {
2801         struct obd_type *sym;
2802         int rc;
2803
2804         rc = libcfs_setup();
2805         if (rc)
2806                 return rc;
2807
2808         rc = lu_kmem_init(lod_caches);
2809         if (rc)
2810                 return rc;
2811
2812         rc = class_register_type(&lod_obd_device_ops, NULL, true,
2813                                  LUSTRE_LOD_NAME, &lod_device_type);
2814         if (rc) {
2815                 lu_kmem_fini(lod_caches);
2816                 return rc;
2817         }
2818
2819         /* create "lov" entry for compatibility purposes */
2820         sym = class_add_symlinks(LUSTRE_LOV_NAME, true);
2821         if (IS_ERR(sym)) {
2822                 rc = PTR_ERR(sym);
2823                 /* does real "lov" already exist ? */
2824                 if (rc == -EEXIST)
2825                         rc = 0;
2826         }
2827
2828         return rc;
2829 }
2830
2831 static void __exit lod_exit(void)
2832 {
2833         struct obd_type *sym = class_search_type(LUSTRE_LOV_NAME);
2834
2835         /* if this was never fully initialized by the lov layer
2836          * then we are responsible for freeing this obd_type
2837          */
2838         if (sym) {
2839                 /* final put if we manage this obd type */
2840                 if (sym->typ_sym_filter)
2841                         kobject_put(&sym->typ_kobj);
2842                 /* put reference taken by class_search_type */
2843                 kobject_put(&sym->typ_kobj);
2844         }
2845
2846         class_unregister_type(LUSTRE_LOD_NAME);
2847         lu_kmem_fini(lod_caches);
2848 }
2849
2850 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2851 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
2852 MODULE_VERSION(LUSTRE_VERSION_STRING);
2853 MODULE_LICENSE("GPL");
2854
2855 module_init(lod_init);
2856 module_exit(lod_exit);