Whamcloud - gitweb
LU-3536 lod: cancel update log after all committed
[fs/lustre-release.git] / lustre / lod / lod_dev.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License version 2 for more details.  A copy is
14  * included in the COPYING file that accompanied this code.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright  2009 Sun Microsystems, Inc. All rights reserved
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/lod/lod_dev.c
33  *
34  * Lustre Logical Object Device
35  *
36  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
37  * Author: Mikhail Pershin <mike.pershin@intel.com>
38  */
39 /**
40  * The Logical Object Device (LOD) layer manages access to striped
41  * objects (both regular files and directories). It implements the DT
42  * device and object APIs and is responsible for creating, storing,
43  * and loading striping information as an extended attribute of the
44  * underlying OSD object. LOD is the server side analog of the LOV and
45  * LMV layers on the client side.
46  *
47  * Metadata LU object stack (layers of the same compound LU object,
48  * all have the same FID):
49  *
50  *        MDT
51  *         |      MD API
52  *        MDD
53  *         |      DT API
54  *        LOD
55  *       /   \    DT API
56  *     OSD   OSP
57  *
58  * During LOD object initialization the localness or remoteness of the
59  * object FID dictates the choice between OSD and OSP.
60  *
61  * An LOD object (file or directory) with N stripes (each has a
62  * different FID):
63  *
64  *          LOD
65  *           |
66  *   +---+---+---+...+
67  *   |   |   |   |   |
68  *   S0  S1  S2  S3  S(N-1)  OS[DP] objects, seen as DT objects by LOD
69  *
70  * When upper layers must access an object's stripes (which are
71  * themselves OST or MDT LU objects) LOD finds these objects by their
72  * FIDs and stores them as an array of DT object pointers on the
73  * object. Declarations and operations on LOD objects are received by
74  * LOD (as DT object operations) and performed on the underlying
75  * OS[DP] object and (as needed) on the stripes. From the perspective
76  * of LOD, a stripe-less file (created by mknod() or open with
77  * O_LOV_DELAY_CREATE) is an object which does not yet have stripes,
78  * while a non-striped directory (created by mkdir()) is an object
79  * which will never have stripes.
80  *
81  * The LOD layer also implements a small subset of the OBD device API
82  * to support MDT stack initialization and finalization (an MDD device
83  * connects and disconnects itself to and from the underlying LOD
84  * device), and pool management. In turn LOD uses the OBD device API
85  * to connect it self to the underlying OSD, and to connect itself to
86  * OSP devices representing the MDTs and OSTs that bear the stripes of
87  * its objects.
88  */
89
90 #define DEBUG_SUBSYSTEM S_MDS
91
92 #include <linux/kthread.h>
93 #include <obd_class.h>
94 #include <md_object.h>
95 #include <lustre_fid.h>
96 #include <lustre_param.h>
97 #include <lustre_update.h>
98 #include <lustre_log.h>
99
100 #include "lod_internal.h"
101
102 static const char lod_update_log_name[] = "update_log";
103 static const char lod_update_log_dir_name[] = "update_log_dir";
104
105 /*
106  * Lookup target by FID.
107  *
108  * Lookup MDT/OST target index by FID. Type of the target can be
109  * specific or any.
110  *
111  * \param[in] env               LU environment provided by the caller
112  * \param[in] lod               lod device
113  * \param[in] fid               FID
114  * \param[out] tgt              result target index
115  * \param[in] type              expected type of the target:
116  *                              LU_SEQ_RANGE_{MDT,OST,ANY}
117  *
118  * \retval 0                    on success
119  * \retval negative             negated errno on error
120  **/
121 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
122                    const struct lu_fid *fid, __u32 *tgt, int *type)
123 {
124         struct lu_seq_range     range = { 0 };
125         struct lu_server_fld    *server_fld;
126         int rc;
127         ENTRY;
128
129         if (!fid_is_sane(fid)) {
130                 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
131                        PFID(fid));
132                 RETURN(-EIO);
133         }
134
135         if (fid_is_idif(fid)) {
136                 *tgt = fid_idif_ost_idx(fid);
137                 *type = LU_SEQ_RANGE_OST;
138                 RETURN(0);
139         }
140
141         if (fid_is_update_log(fid) || fid_is_update_log_dir(fid)) {
142                 *tgt = fid_oid(fid);
143                 *type = LU_SEQ_RANGE_MDT;
144                 RETURN(0);
145         }
146
147         if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
148                 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
149
150                 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
151                 *type = LU_SEQ_RANGE_MDT;
152                 RETURN(0);
153         }
154
155         server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
156         if (server_fld == NULL)
157                 RETURN(-EIO);
158
159         fld_range_set_type(&range, *type);
160         rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
161         if (rc != 0)
162                 RETURN(rc);
163
164         *tgt = range.lsr_index;
165         *type = range.lsr_flags;
166
167         CDEBUG(D_INFO, "%s: got tgt %x for sequence: "LPX64"\n",
168                lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
169
170         RETURN(0);
171 }
172
173 /* Slab for OSD object allocation */
174 struct kmem_cache *lod_object_kmem;
175
176 /* Slab for dt_txn_callback */
177 struct kmem_cache *lod_txn_callback_kmem;
178 static struct lu_kmem_descr lod_caches[] = {
179         {
180                 .ckd_cache = &lod_object_kmem,
181                 .ckd_name  = "lod_obj",
182                 .ckd_size  = sizeof(struct lod_object)
183         },
184         {
185                 .ckd_cache = &lod_txn_callback_kmem,
186                 .ckd_name  = "lod_txn_callback",
187                 .ckd_size  = sizeof(struct dt_txn_callback)
188         },
189         {
190                 .ckd_cache = NULL
191         }
192 };
193
194 static struct lu_device *lod_device_fini(const struct lu_env *env,
195                                          struct lu_device *d);
196
197 /**
198  * Implementation of lu_device_operations::ldo_object_alloc() for LOD
199  *
200  * Allocates and initializes LOD's slice in the given object.
201  *
202  * see include/lu_object.h for the details.
203  */
204 static struct lu_object *lod_object_alloc(const struct lu_env *env,
205                                           const struct lu_object_header *hdr,
206                                           struct lu_device *dev)
207 {
208         struct lod_object       *lod_obj;
209         struct lu_object        *lu_obj;
210         ENTRY;
211
212         OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
213         if (lod_obj == NULL)
214                 RETURN(ERR_PTR(-ENOMEM));
215
216         lu_obj = lod2lu_obj(lod_obj);
217         dt_object_init(&lod_obj->ldo_obj, NULL, dev);
218         lod_obj->ldo_obj.do_ops = &lod_obj_ops;
219         lu_obj->lo_ops = &lod_lu_obj_ops;
220
221         RETURN(lu_obj);
222 }
223
224 /**
225  * Process the config log for all sub device.
226  *
227  * The function goes through all the targets in the given table
228  * and apply given configuration command on to the targets.
229  * Used to cleanup the targets at unmount.
230  *
231  * \param[in] env               LU environment provided by the caller
232  * \param[in] lod               lod device
233  * \param[in] ltd               target's table to go through
234  * \param[in] lcfg              configuration command to apply
235  *
236  * \retval 0                    on success
237  * \retval negative             negated errno on error
238  **/
239 static int lod_sub_process_config(const struct lu_env *env,
240                                  struct lod_device *lod,
241                                  struct lod_tgt_descs *ltd,
242                                  struct lustre_cfg *lcfg)
243 {
244         struct lu_device  *next;
245         int rc = 0;
246         unsigned int i;
247
248         lod_getref(ltd);
249         if (ltd->ltd_tgts_size <= 0) {
250                 lod_putref(lod, ltd);
251                 return 0;
252         }
253         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
254                 struct lod_tgt_desc *tgt;
255                 int rc1;
256
257                 tgt = LTD_TGT(ltd, i);
258                 LASSERT(tgt && tgt->ltd_tgt);
259                 next = &tgt->ltd_tgt->dd_lu_dev;
260                 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
261                 if (rc1) {
262                         CERROR("%s: error cleaning up LOD index %u: cmd %#x"
263                                ": rc = %d\n", lod2obd(lod)->obd_name, i,
264                                lcfg->lcfg_command, rc1);
265                         rc = rc1;
266                 }
267         }
268         lod_putref(lod, ltd);
269         return rc;
270 }
271
272 struct lod_recovery_data {
273         struct lod_device       *lrd_lod;
274         struct lod_tgt_desc     *lrd_ltd;
275         struct ptlrpc_thread    *lrd_thread;
276         __u32                   lrd_idx;
277 };
278
279 /**
280  * recovery thread for update log
281  *
282  * Start recovery thread and prepare the sub llog, then it will retrieve
283  * the update records from the correpondent MDT and do recovery.
284  *
285  * \param[in] arg       pointer to the recovery data
286  *
287  * \retval              0 if recovery succeeds
288  * \retval              negative errno if recovery failed.
289  */
290 static int lod_sub_recovery_thread(void *arg)
291 {
292         struct lod_recovery_data        *lrd = arg;
293         struct lod_device               *lod = lrd->lrd_lod;
294         struct dt_device                *dt;
295         struct ptlrpc_thread            *thread = lrd->lrd_thread;
296         struct lu_env                   env;
297         int                             rc;
298         ENTRY;
299
300         thread->t_flags = SVC_RUNNING;
301         wake_up(&thread->t_ctl_waitq);
302
303         rc = lu_env_init(&env, LCT_LOCAL | LCT_MD_THREAD);
304         if (rc != 0) {
305                 OBD_FREE_PTR(lrd);
306                 CERROR("%s: can't initialize env: rc = %d\n",
307                        lod2obd(lod)->obd_name, rc);
308                 RETURN(rc);
309         }
310
311         if (lrd->lrd_ltd == NULL)
312                 dt = lod->lod_child;
313         else
314                 dt = lrd->lrd_ltd->ltd_tgt;
315
316         rc = lod_sub_prep_llog(&env, lod, dt, lrd->lrd_idx);
317         if (rc != 0)
318                 GOTO(out, rc);
319
320         /* XXX do recovery in the following patches */
321
322 out:
323         OBD_FREE_PTR(lrd);
324         thread->t_flags = SVC_STOPPED;
325         wake_up(&thread->t_ctl_waitq);
326         lu_env_fini(&env);
327         RETURN(rc);
328 }
329
330 /**
331  * finish sub llog context
332  *
333  * Stop update recovery thread for the sub device, then cleanup the
334  * correspondent llog ctxt.
335  *
336  * \param[in] env      execution environment
337  * \param[in] lod      lod device to do update recovery
338  * \param[in] thread   recovery thread on this sub device
339  */
340 void lod_sub_fini_llog(const struct lu_env *env,
341                        struct dt_device *dt, struct ptlrpc_thread *thread)
342 {
343         struct obd_device       *obd;
344         struct llog_ctxt        *ctxt;
345         ENTRY;
346
347         obd = dt->dd_lu_dev.ld_obd;
348         CDEBUG(D_INFO, "%s: finish sub llog\n", obd->obd_name);
349         /* Stop recovery thread first */
350         if (thread != NULL && thread->t_flags & SVC_RUNNING) {
351                 thread->t_flags = SVC_STOPPING;
352                 wake_up(&thread->t_ctl_waitq);
353                 wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_STOPPED);
354         }
355
356         ctxt = llog_get_context(obd, LLOG_UPDATELOG_ORIG_CTXT);
357         if (ctxt == NULL)
358                 RETURN_EXIT;
359
360         if (ctxt->loc_handle != NULL)
361                 llog_cat_close(env, ctxt->loc_handle);
362
363         llog_cleanup(env, ctxt);
364
365         RETURN_EXIT;
366 }
367
368 /**
369  * Extract MDT target index from a device name.
370  *
371  * a helper function to extract index from the given device name
372  * like "fsname-MDTxxxx-mdtlov"
373  *
374  * \param[in] lodname           device name
375  * \param[out] mdt_index        extracted index
376  *
377  * \retval 0            on success
378  * \retval -EINVAL      if the name is invalid
379  */
380 int lodname2mdt_index(char *lodname, __u32 *mdt_index)
381 {
382         unsigned long index;
383         char *ptr, *tmp;
384
385         /* 1.8 configs don't have "-MDT0000" at the end */
386         ptr = strstr(lodname, "-MDT");
387         if (ptr == NULL) {
388                 *mdt_index = 0;
389                 return 0;
390         }
391
392         ptr = strrchr(lodname, '-');
393         if (ptr == NULL) {
394                 CERROR("invalid MDT index in '%s'\n", lodname);
395                 return -EINVAL;
396         }
397
398         if (strncmp(ptr, "-mdtlov", 7) != 0) {
399                 CERROR("invalid MDT index in '%s'\n", lodname);
400                 return -EINVAL;
401         }
402
403         if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
404                 CERROR("invalid MDT index in '%s'\n", lodname);
405                 return -EINVAL;
406         }
407
408         if (strncmp(ptr - 8, "-MDT", 4) != 0) {
409                 CERROR("invalid MDT index in '%s'\n", lodname);
410                 return -EINVAL;
411         }
412
413         index = simple_strtol(ptr - 4, &tmp, 16);
414         if (*tmp != '-' || index > INT_MAX) {
415                 CERROR("invalid MDT index in '%s'\n", lodname);
416                 return -EINVAL;
417         }
418         *mdt_index = index;
419         return 0;
420 }
421
422 /**
423  * Init sub llog context
424  *
425  * Setup update llog ctxt for update recovery threads, then start the
426  * recovery thread (lod_sub_recovery_thread) to read update llog from
427  * the correspondent MDT to do update recovery.
428  *
429  * \param[in] env       execution environment
430  * \param[in] lod       lod device to do update recovery
431  * \param[in] dt        sub dt device for which the recovery thread is
432  *
433  * \retval              0 if initialization succeeds.
434  * \retval              negative errno if initialization fails.
435  */
436 int lod_sub_init_llog(const struct lu_env *env, struct lod_device *lod,
437                       struct dt_device *dt)
438 {
439         struct obd_device               *obd;
440         struct lod_recovery_data        *lrd = NULL;
441         struct ptlrpc_thread            *thread;
442         struct task_struct              *task;
443         struct l_wait_info              lwi = { 0 };
444         struct lod_tgt_desc             *sub_ltd = NULL;
445         __u32                           index;
446         int                             rc;
447         ENTRY;
448
449         OBD_ALLOC_PTR(lrd);
450         if (lrd == NULL)
451                 RETURN(-ENOMEM);
452
453         if (lod->lod_child == dt) {
454                 thread = &lod->lod_child_recovery_thread;
455                 rc = lodname2mdt_index(lod2obd(lod)->obd_name, &index);
456                 if (rc != 0)
457                         GOTO(free_lrd, rc);
458         } else {
459                 struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
460                 struct lod_tgt_desc     *tgt = NULL;
461                 unsigned int            i;
462
463                 cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
464                         tgt = LTD_TGT(ltd, i);
465                         if (tgt->ltd_tgt == dt) {
466                                 index = tgt->ltd_index;
467                                 sub_ltd = tgt;
468                                 break;
469                         }
470                 }
471                 LASSERT(sub_ltd != NULL);
472                 OBD_ALLOC_PTR(sub_ltd->ltd_recovery_thread);
473                 if (sub_ltd->ltd_recovery_thread == NULL)
474                         GOTO(free_lrd, rc = -ENOMEM);
475
476                 thread = sub_ltd->ltd_recovery_thread;
477         }
478
479         lrd->lrd_lod = lod;
480         lrd->lrd_ltd = sub_ltd;
481         lrd->lrd_thread = thread;
482         lrd->lrd_idx = index;
483         init_waitqueue_head(&thread->t_ctl_waitq);
484
485         obd = dt->dd_lu_dev.ld_obd;
486         obd->obd_lvfs_ctxt.dt = dt;
487         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_UPDATELOG_ORIG_CTXT,
488                         NULL, &llog_common_cat_ops);
489         if (rc < 0) {
490                 CERROR("%s: cannot setup updatelog llog: rc = %d\n",
491                        obd->obd_name, rc);
492                 GOTO(free_thread, rc);
493         }
494
495         /* Start the recovery thread */
496         task = kthread_run(lod_sub_recovery_thread, lrd, "lod_recov_%04x",
497                            index);
498         if (IS_ERR(task)) {
499                 rc = PTR_ERR(task);
500                 CERROR("%s: cannot start recovery thread: rc = %d\n",
501                        obd->obd_name, rc);
502                 GOTO(out_llog, rc);
503         }
504
505         l_wait_event(thread->t_ctl_waitq, thread->t_flags & SVC_RUNNING ||
506                                           thread->t_flags & SVC_STOPPED, &lwi);
507
508         RETURN(0);
509 out_llog:
510         lod_sub_fini_llog(env, dt, thread);
511 free_thread:
512         if (lod->lod_child != dt) {
513                 OBD_FREE_PTR(sub_ltd->ltd_recovery_thread);
514                 sub_ltd->ltd_recovery_thread = NULL;
515         }
516 free_lrd:
517         OBD_FREE_PTR(lrd);
518         RETURN(rc);
519 }
520
521 /**
522  * finish all sub llog
523  *
524  * cleanup all of sub llog ctxt on the LOD.
525  *
526  * \param[in] env       execution environment
527  * \param[in] lod       lod device to do update recovery
528  */
529 static void lod_sub_fini_all_llogs(const struct lu_env *env,
530                                    struct lod_device *lod)
531 {
532         struct lod_tgt_descs *ltd = &lod->lod_mdt_descs;
533         unsigned int i;
534
535         /* Stop the update log commit cancel threads and finish master
536          * llog ctxt */
537         lod_sub_fini_llog(env, lod->lod_child,
538                           &lod->lod_child_recovery_thread);
539         lod_getref(ltd);
540         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
541                 struct lod_tgt_desc     *tgt;
542
543                 tgt = LTD_TGT(ltd, i);
544                 if (tgt->ltd_recovery_thread != NULL) {
545                         lod_sub_fini_llog(env, tgt->ltd_tgt,
546                                           tgt->ltd_recovery_thread);
547                         OBD_FREE_PTR(tgt->ltd_recovery_thread);
548                         tgt->ltd_recovery_thread = NULL;
549                 }
550         }
551
552         lod_putref(lod, ltd);
553 }
554
555 /**
556  * Prepare distribute txn
557  *
558  * Prepare distribute txn structure for LOD
559  *
560  * \param[in] env       execution environment
561  * \param[in] lod_device  LOD device
562  *
563  * \retval              0 if preparation succeeds.
564  * \retval              negative errno if preparation fails.
565  */
566 static int lod_prepare_distribute_txn(const struct lu_env *env,
567                                       struct lod_device *lod)
568 {
569         struct target_distribute_txn_data *tdtd;
570         struct lu_target                  *lut;
571         int                               rc;
572         ENTRY;
573
574         /* Init update recovery data */
575         OBD_ALLOC_PTR(tdtd);
576         if (tdtd == NULL)
577                 RETURN(-ENOMEM);
578
579         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
580
581         rc = distribute_txn_init(env, lut, tdtd,
582                 lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id);
583
584         if (rc < 0) {
585                 CERROR("%s: cannot init distribute txn: rc = %d\n",
586                        lod2obd(lod)->obd_name, rc);
587                 OBD_FREE_PTR(tdtd);
588                 RETURN(rc);
589         }
590
591         lut->lut_tdtd = tdtd;
592
593         RETURN(0);
594 }
595
596 /**
597  * Finish distribute txn
598  *
599  * Release the resource holding by distribute txn, i.e. stop distribute
600  * txn thread.
601  *
602  * \param[in] env       execution environment
603  * \param[in] lod       lod device
604  */
605 static void lod_fini_distribute_txn(const struct lu_env *env,
606                                     struct lod_device *lod)
607 {
608         struct lu_target                  *lut;
609
610         lut = lod2lu_dev(lod)->ld_site->ls_tgt;
611         if (lut->lut_tdtd == NULL)
612                 return;
613
614         distribute_txn_fini(env, lut->lut_tdtd);
615
616         OBD_FREE_PTR(lut->lut_tdtd);
617         lut->lut_tdtd = NULL;
618 }
619
620 /**
621  * Implementation of lu_device_operations::ldo_process_config() for LOD
622  *
623  * The method is called by the configuration subsystem during setup,
624  * cleanup and when the configuration changes. The method processes
625  * few specific commands like adding/removing the targets, changing
626  * the runtime parameters.
627
628  * \param[in] env               LU environment provided by the caller
629  * \param[in] dev               lod device
630  * \param[in] lcfg              configuration command to apply
631  *
632  * \retval 0                    on success
633  * \retval negative             negated errno on error
634  *
635  * The examples are below.
636  *
637  * Add osc config log:
638  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
639  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
640  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
641  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
642  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
643  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
644  *
645  * Add mdc config log:
646  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
647  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
648  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
649  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
650  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
651  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
652  */
653 static int lod_process_config(const struct lu_env *env,
654                               struct lu_device *dev,
655                               struct lustre_cfg *lcfg)
656 {
657         struct lod_device *lod = lu2lod_dev(dev);
658         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
659         char              *arg1;
660         int                rc = 0;
661         ENTRY;
662
663         switch(lcfg->lcfg_command) {
664         case LCFG_LOV_DEL_OBD:
665         case LCFG_LOV_ADD_INA:
666         case LCFG_LOV_ADD_OBD:
667         case LCFG_ADD_MDC: {
668                 __u32 index;
669                 __u32 mdt_index;
670                 int gen;
671                 /* lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
672                  * modify_mdc_tgts add  0:lustre-MDT0001
673                  *                    1:lustre-MDT0001-mdc0002
674                  *                    2:2  3:1*/
675                 arg1 = lustre_cfg_string(lcfg, 1);
676
677                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
678                         GOTO(out, rc = -EINVAL);
679                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
680                         GOTO(out, rc = -EINVAL);
681
682                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
683                         __u32 mdt_index;
684
685                         rc = lodname2mdt_index(lustre_cfg_string(lcfg, 0),
686                                                &mdt_index);
687                         if (rc != 0)
688                                 GOTO(out, rc);
689
690                         rc = lod_add_device(env, lod, arg1, index, gen,
691                                             mdt_index, LUSTRE_OSC_NAME, 1);
692                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
693                         mdt_index = index;
694                         rc = lod_add_device(env, lod, arg1, index, gen,
695                                             mdt_index, LUSTRE_MDC_NAME, 1);
696                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
697                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
698                         mdt_index = 0;
699                         rc = lod_add_device(env, lod, arg1, index, gen,
700                                             mdt_index, LUSTRE_OSC_NAME, 0);
701                 } else {
702                         rc = lod_del_device(env, lod,
703                                             &lod->lod_ost_descs,
704                                             arg1, index, gen, true);
705                 }
706
707                 break;
708         }
709
710         case LCFG_PARAM: {
711                 struct obd_device *obd = lod2obd(lod);
712
713                 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
714                                               lcfg, obd);
715                 if (rc > 0)
716                         rc = 0;
717                 GOTO(out, rc);
718         }
719         case LCFG_PRE_CLEANUP: {
720                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
721                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
722                 next = &lod->lod_child->dd_lu_dev;
723                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
724                 if (rc != 0)
725                         CDEBUG(D_HA, "%s: can't process %u: %d\n",
726                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
727
728                 lod_fini_distribute_txn(env, lod);
729                 lod_sub_fini_all_llogs(env, lod);
730                 break;
731         }
732         case LCFG_CLEANUP: {
733                 /*
734                  * do cleanup on underlying storage only when
735                  * all OSPs are cleaned up, as they use that OSD as well
736                  */
737                 lu_dev_del_linkage(dev->ld_site, dev);
738                 lod_sub_process_config(env, lod, &lod->lod_mdt_descs, lcfg);
739                 lod_sub_process_config(env, lod, &lod->lod_ost_descs, lcfg);
740                 next = &lod->lod_child->dd_lu_dev;
741                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
742                 if (rc)
743                         CERROR("%s: can't process %u: %d\n",
744                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
745
746                 rc = obd_disconnect(lod->lod_child_exp);
747                 if (rc)
748                         CERROR("error in disconnect from storage: %d\n", rc);
749                 break;
750         }
751         default:
752                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
753                       lcfg->lcfg_command);
754                rc = -EINVAL;
755                break;
756         }
757
758 out:
759         RETURN(rc);
760 }
761
762 /**
763  * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
764  *
765  * The method is called once the recovery is complete. This implementation
766  * distributes the notification to all the known targets.
767  *
768  * see include/lu_object.h for the details
769  */
770 static int lod_recovery_complete(const struct lu_env *env,
771                                  struct lu_device *dev)
772 {
773         struct lod_device   *lod = lu2lod_dev(dev);
774         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
775         unsigned int         i;
776         int                  rc;
777         ENTRY;
778
779         LASSERT(lod->lod_recovery_completed == 0);
780         lod->lod_recovery_completed = 1;
781
782         rc = next->ld_ops->ldo_recovery_complete(env, next);
783
784         lod_getref(&lod->lod_ost_descs);
785         if (lod->lod_osts_size > 0) {
786                 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
787                         struct lod_tgt_desc *tgt;
788                         tgt = OST_TGT(lod, i);
789                         LASSERT(tgt && tgt->ltd_tgt);
790                         next = &tgt->ltd_ost->dd_lu_dev;
791                         rc = next->ld_ops->ldo_recovery_complete(env, next);
792                         if (rc)
793                                 CERROR("%s: can't complete recovery on #%d:"
794                                         "%d\n", lod2obd(lod)->obd_name, i, rc);
795                 }
796         }
797         lod_putref(lod, &lod->lod_ost_descs);
798         RETURN(rc);
799 }
800
801 /**
802  * Init update logs on all sub device
803  *
804  * LOD initialize update logs on all of sub devices. Because the initialization
805  * process might need FLD lookup, see llog_osd_open()->dt_locate()->...->
806  * lod_object_init(), this API has to be called after LOD is initialized.
807  * \param[in] env       execution environment
808  * \param[in] lod       lod device
809  *
810  * \retval              0 if update log is initialized successfully.
811  * \retval              negative errno if initialization fails.
812  */
813 static int lod_sub_init_llogs(const struct lu_env *env, struct lod_device *lod)
814 {
815         struct lod_tgt_descs    *ltd = &lod->lod_mdt_descs;
816         int                     rc;
817         unsigned int            i;
818         ENTRY;
819
820         /* llog must be setup after LOD is initialized, because llog
821          * initialization include FLD lookup */
822         LASSERT(lod->lod_initialized);
823
824         /* Init the llog in its own stack */
825         rc = lod_sub_init_llog(env, lod, lod->lod_child);
826         if (rc < 0)
827                 RETURN(rc);
828
829         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
830                 struct lod_tgt_desc     *tgt;
831
832                 tgt = LTD_TGT(ltd, i);
833                 rc = lod_sub_init_llog(env, lod, tgt->ltd_tgt);
834                 if (rc != 0)
835                         break;
836         }
837
838         RETURN(rc);
839 }
840
841 /**
842  * Implementation of lu_device_operations::ldo_prepare() for LOD
843  *
844  * see include/lu_object.h for the details.
845  */
846 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
847                        struct lu_device *cdev)
848 {
849         struct lod_device       *lod = lu2lod_dev(cdev);
850         struct lu_device        *next = &lod->lod_child->dd_lu_dev;
851         struct lu_fid           *fid = &lod_env_info(env)->lti_fid;
852         int                     rc;
853         struct dt_object        *root;
854         struct dt_object        *dto;
855         __u32                   index;
856         ENTRY;
857
858         rc = next->ld_ops->ldo_prepare(env, pdev, next);
859         if (rc != 0) {
860                 CERROR("%s: prepare bottom error: rc = %d\n",
861                        lod2obd(lod)->obd_name, rc);
862                 RETURN(rc);
863         }
864
865         lod->lod_initialized = 1;
866
867         rc = dt_root_get(env, lod->lod_child, fid);
868         if (rc < 0)
869                 RETURN(rc);
870
871         root = dt_locate(env, lod->lod_child, fid);
872         if (IS_ERR(root))
873                 RETURN(PTR_ERR(root));
874
875         /* Create update log object */
876         index = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
877         lu_update_log_fid(fid, index);
878
879         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
880                                                  fid, root,
881                                                  lod_update_log_name,
882                                                  S_IFREG | S_IRUGO | S_IWUSR);
883         if (IS_ERR(dto))
884                 GOTO(out_put, rc = PTR_ERR(dto));
885
886         lu_object_put(env, &dto->do_lu);
887
888         /* Create update log dir */
889         lu_update_log_dir_fid(fid, index);
890         dto = local_file_find_or_create_with_fid(env, lod->lod_child,
891                                                  fid, root,
892                                                  lod_update_log_dir_name,
893                                                  S_IFDIR | S_IRUGO | S_IWUSR);
894         if (IS_ERR(dto))
895                 GOTO(out_put, rc = PTR_ERR(dto));
896
897         lu_object_put(env, &dto->do_lu);
898
899         rc = lod_prepare_distribute_txn(env, lod);
900         if (rc != 0)
901                 GOTO(out_put, rc);
902
903         rc = lod_sub_init_llogs(env, lod);
904         if (rc != 0)
905                 GOTO(out_put, rc);
906
907 out_put:
908         lu_object_put(env, &root->do_lu);
909
910         RETURN(rc);
911 }
912
913 const struct lu_device_operations lod_lu_ops = {
914         .ldo_object_alloc       = lod_object_alloc,
915         .ldo_process_config     = lod_process_config,
916         .ldo_recovery_complete  = lod_recovery_complete,
917         .ldo_prepare            = lod_prepare,
918 };
919
920 /**
921  * Implementation of dt_device_operations::dt_root_get() for LOD
922  *
923  * see include/dt_object.h for the details.
924  */
925 static int lod_root_get(const struct lu_env *env,
926                         struct dt_device *dev, struct lu_fid *f)
927 {
928         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
929 }
930
931 /**
932  * Implementation of dt_device_operations::dt_statfs() for LOD
933  *
934  * see include/dt_object.h for the details.
935  */
936 static int lod_statfs(const struct lu_env *env,
937                       struct dt_device *dev, struct obd_statfs *sfs)
938 {
939         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
940 }
941
942 /**
943  * Implementation of dt_device_operations::dt_trans_create() for LOD
944  *
945  * Creates a transaction using local (to this node) OSD.
946  *
947  * see include/dt_object.h for the details.
948  */
949 static struct thandle *lod_trans_create(const struct lu_env *env,
950                                         struct dt_device *dt)
951 {
952         struct thandle *th;
953
954         th = top_trans_create(env, dt2lod_dev(dt)->lod_child);
955         if (IS_ERR(th))
956                 return th;
957
958         th->th_dev = dt;
959
960         return th;
961 }
962
963 /**
964  * Implementation of dt_device_operations::dt_trans_start() for LOD
965  *
966  * Starts the set of local transactions using the targets involved
967  * in declare phase. Initial support for the distributed transactions.
968  *
969  * see include/dt_object.h for the details.
970  */
971 static int lod_trans_start(const struct lu_env *env, struct dt_device *dt,
972                            struct thandle *th)
973 {
974         return top_trans_start(env, dt2lod_dev(dt)->lod_child, th);
975 }
976
977 static int lod_trans_cb_add(struct thandle *th,
978                             struct dt_txn_commit_cb *dcb)
979 {
980         struct top_thandle      *top_th = container_of(th, struct top_thandle,
981                                                        tt_super);
982         return dt_trans_cb_add(top_th->tt_master_sub_thandle, dcb);
983 }
984
985 /**
986  * Implementation of dt_device_operations::dt_trans_stop() for LOD
987  *
988  * Stops the set of local transactions using the targets involved
989  * in declare phase. Initial support for the distributed transactions.
990  *
991  * see include/dt_object.h for the details.
992  */
993 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
994                           struct thandle *th)
995 {
996         return top_trans_stop(env, dt2lod_dev(dt)->lod_child, th);
997 }
998
999 /**
1000  * Implementation of dt_device_operations::dt_conf_get() for LOD
1001  *
1002  * Currently returns the configuration provided by the local OSD.
1003  *
1004  * see include/dt_object.h for the details.
1005  */
1006 static void lod_conf_get(const struct lu_env *env,
1007                          const struct dt_device *dev,
1008                          struct dt_device_param *param)
1009 {
1010         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
1011 }
1012
1013 /**
1014  * Implementation of dt_device_operations::dt_sync() for LOD
1015  *
1016  * Syncs all known OST targets. Very very expensive and used
1017  * rarely by LFSCK now. Should not be used in general.
1018  *
1019  * see include/dt_object.h for the details.
1020  */
1021 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
1022 {
1023         struct lod_device   *lod = dt2lod_dev(dev);
1024         struct lod_ost_desc *ost;
1025         unsigned int         i;
1026         int                  rc = 0;
1027         ENTRY;
1028
1029         lod_getref(&lod->lod_ost_descs);
1030         lod_foreach_ost(lod, i) {
1031                 ost = OST_TGT(lod, i);
1032                 LASSERT(ost && ost->ltd_ost);
1033                 rc = dt_sync(env, ost->ltd_ost);
1034                 if (rc) {
1035                         CERROR("%s: can't sync %u: %d\n",
1036                                lod2obd(lod)->obd_name, i, rc);
1037                         break;
1038                 }
1039         }
1040         lod_putref(lod, &lod->lod_ost_descs);
1041         if (rc == 0)
1042                 rc = dt_sync(env, lod->lod_child);
1043
1044         RETURN(rc);
1045 }
1046
1047 /**
1048  * Implementation of dt_device_operations::dt_ro() for LOD
1049  *
1050  * Turns local OSD read-only, used for the testing only.
1051  *
1052  * see include/dt_object.h for the details.
1053  */
1054 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
1055 {
1056         return dt_ro(env, dt2lod_dev(dev)->lod_child);
1057 }
1058
1059 /**
1060  * Implementation of dt_device_operations::dt_commit_async() for LOD
1061  *
1062  * Asks local OSD to commit sooner.
1063  *
1064  * see include/dt_object.h for the details.
1065  */
1066 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
1067 {
1068         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
1069 }
1070
1071 static const struct dt_device_operations lod_dt_ops = {
1072         .dt_root_get         = lod_root_get,
1073         .dt_statfs           = lod_statfs,
1074         .dt_trans_create     = lod_trans_create,
1075         .dt_trans_start      = lod_trans_start,
1076         .dt_trans_stop       = lod_trans_stop,
1077         .dt_conf_get         = lod_conf_get,
1078         .dt_sync             = lod_sync,
1079         .dt_ro               = lod_ro,
1080         .dt_commit_async     = lod_commit_async,
1081         .dt_trans_cb_add     = lod_trans_cb_add,
1082 };
1083
1084 /**
1085  * Connect to a local OSD.
1086  *
1087  * Used to connect to the local OSD at mount. OSD name is taken from the
1088  * configuration command passed. This connection is used to identify LU
1089  * site and pin the OSD from early removal.
1090  *
1091  * \param[in] env               LU environment provided by the caller
1092  * \param[in] lod               lod device
1093  * \param[in] cfg               configuration command to apply
1094  *
1095  * \retval 0                    on success
1096  * \retval negative             negated errno on error
1097  **/
1098 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
1099                               struct lustre_cfg *cfg)
1100 {
1101         struct obd_connect_data *data = NULL;
1102         struct obd_device       *obd;
1103         char                    *nextdev = NULL, *p, *s;
1104         int                      rc, len = 0;
1105         ENTRY;
1106
1107         LASSERT(cfg);
1108         LASSERT(lod->lod_child_exp == NULL);
1109
1110         /* compatibility hack: we still use old config logs
1111          * which specify LOV, but we need to learn underlying
1112          * OSD device, which is supposed to be:
1113          *  <fsname>-MDTxxxx-osd
1114          *
1115          * 2.x MGS generates lines like the following:
1116          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
1117          * 1.8 MGS generates lines like the following:
1118          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
1119          *
1120          * we use "-MDT" to differentiate 2.x from 1.8 */
1121
1122         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
1123                 len = strlen(p) + 1;
1124                 OBD_ALLOC(nextdev, len);
1125                 if (nextdev == NULL)
1126                         GOTO(out, rc = -ENOMEM);
1127
1128                 strcpy(nextdev, p);
1129                 s = strstr(nextdev, "-mdtlov");
1130                 if (unlikely(s == NULL)) {
1131                         CERROR("unable to parse device name %s\n",
1132                                lustre_cfg_string(cfg, 0));
1133                         GOTO(out, rc = -EINVAL);
1134                 }
1135
1136                 if (strstr(nextdev, "-MDT")) {
1137                         /* 2.x config */
1138                         strcpy(s, "-osd");
1139                 } else {
1140                         /* 1.8 config */
1141                         strcpy(s, "-MDT0000-osd");
1142                 }
1143         } else {
1144                 CERROR("unable to parse device name %s\n",
1145                        lustre_cfg_string(cfg, 0));
1146                 GOTO(out, rc = -EINVAL);
1147         }
1148
1149         OBD_ALLOC_PTR(data);
1150         if (data == NULL)
1151                 GOTO(out, rc = -ENOMEM);
1152
1153         obd = class_name2obd(nextdev);
1154         if (obd == NULL) {
1155                 CERROR("can not locate next device: %s\n", nextdev);
1156                 GOTO(out, rc = -ENOTCONN);
1157         }
1158
1159         data->ocd_connect_flags = OBD_CONNECT_VERSION;
1160         data->ocd_version = LUSTRE_VERSION_CODE;
1161
1162         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
1163                          data, NULL);
1164         if (rc) {
1165                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1166                 GOTO(out, rc);
1167         }
1168
1169         lod->lod_dt_dev.dd_lu_dev.ld_site =
1170                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
1171         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
1172         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
1173
1174 out:
1175         if (data)
1176                 OBD_FREE_PTR(data);
1177         if (nextdev)
1178                 OBD_FREE(nextdev, len);
1179         RETURN(rc);
1180 }
1181
1182 /**
1183  * Allocate and initialize target table.
1184  *
1185  * A helper function to initialize the target table and allocate
1186  * a bitmap of the available targets.
1187  *
1188  * \param[in] ltd               target's table to initialize
1189  *
1190  * \retval 0                    on success
1191  * \retval negative             negated errno on error
1192  **/
1193 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
1194 {
1195         mutex_init(&ltd->ltd_mutex);
1196         init_rwsem(&ltd->ltd_rw_sem);
1197
1198         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
1199          * added to the LOD, see lod_add_device() */
1200         ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
1201         if (ltd->ltd_tgt_bitmap == NULL)
1202                 RETURN(-ENOMEM);
1203
1204         ltd->ltd_tgts_size  = 32;
1205         ltd->ltd_tgtnr      = 0;
1206
1207         ltd->ltd_death_row = 0;
1208         ltd->ltd_refcount  = 0;
1209         return 0;
1210 }
1211
1212 /**
1213  * Initialize LOD device at setup.
1214  *
1215  * Initializes the given LOD device using the original configuration command.
1216  * The function initiates a connection to the local OSD and initializes few
1217  * internal structures like pools, target tables, etc.
1218  *
1219  * \param[in] env               LU environment provided by the caller
1220  * \param[in] lod               lod device
1221  * \param[in] ldt               not used
1222  * \param[in] cfg               configuration command
1223  *
1224  * \retval 0                    on success
1225  * \retval negative             negated errno on error
1226  **/
1227 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
1228                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
1229 {
1230         struct dt_device_param ddp;
1231         struct obd_device     *obd;
1232         int                    rc;
1233         ENTRY;
1234
1235         obd = class_name2obd(lustre_cfg_string(cfg, 0));
1236         if (obd == NULL) {
1237                 CERROR("Cannot find obd with name %s\n",
1238                        lustre_cfg_string(cfg, 0));
1239                 RETURN(-ENODEV);
1240         }
1241
1242         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
1243         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
1244         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
1245         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
1246
1247         rc = lod_connect_to_osd(env, lod, cfg);
1248         if (rc)
1249                 RETURN(rc);
1250
1251         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
1252         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
1253
1254         /* setup obd to be used with old lov code */
1255         rc = lod_pools_init(lod, cfg);
1256         if (rc)
1257                 GOTO(out_disconnect, rc);
1258
1259         rc = lod_procfs_init(lod);
1260         if (rc)
1261                 GOTO(out_pools, rc);
1262
1263         spin_lock_init(&lod->lod_desc_lock);
1264         spin_lock_init(&lod->lod_connects_lock);
1265         lod_tgt_desc_init(&lod->lod_mdt_descs);
1266         lod_tgt_desc_init(&lod->lod_ost_descs);
1267
1268         RETURN(0);
1269
1270 out_pools:
1271         lod_pools_fini(lod);
1272 out_disconnect:
1273         obd_disconnect(lod->lod_child_exp);
1274         RETURN(rc);
1275 }
1276
1277 /**
1278  * Implementation of lu_device_type_operations::ldto_device_free() for LOD
1279  *
1280  * Releases the memory allocated for LOD device.
1281  *
1282  * see include/lu_object.h for the details.
1283  */
1284 static struct lu_device *lod_device_free(const struct lu_env *env,
1285                                          struct lu_device *lu)
1286 {
1287         struct lod_device *lod = lu2lod_dev(lu);
1288         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
1289         ENTRY;
1290
1291         LASSERTF(atomic_read(&lu->ld_ref) == 0, "lu is %p\n", lu);
1292         dt_device_fini(&lod->lod_dt_dev);
1293         OBD_FREE_PTR(lod);
1294         RETURN(next);
1295 }
1296
1297 /**
1298  * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
1299  *
1300  * Allocates LOD device and calls the helpers to initialize it.
1301  *
1302  * see include/lu_object.h for the details.
1303  */
1304 static struct lu_device *lod_device_alloc(const struct lu_env *env,
1305                                           struct lu_device_type *type,
1306                                           struct lustre_cfg *lcfg)
1307 {
1308         struct lod_device *lod;
1309         struct lu_device  *lu_dev;
1310
1311         OBD_ALLOC_PTR(lod);
1312         if (lod == NULL) {
1313                 lu_dev = ERR_PTR(-ENOMEM);
1314         } else {
1315                 int rc;
1316
1317                 lu_dev = lod2lu_dev(lod);
1318                 dt_device_init(&lod->lod_dt_dev, type);
1319                 rc = lod_init0(env, lod, type, lcfg);
1320                 if (rc != 0) {
1321                         lod_device_free(env, lu_dev);
1322                         lu_dev = ERR_PTR(rc);
1323                 }
1324         }
1325
1326         return lu_dev;
1327 }
1328
1329 /**
1330  * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
1331  *
1332  * Releases the internal resources used by LOD device.
1333  *
1334  * see include/lu_object.h for the details.
1335  */
1336 static struct lu_device *lod_device_fini(const struct lu_env *env,
1337                                          struct lu_device *d)
1338 {
1339         struct lod_device *lod = lu2lod_dev(d);
1340         int                rc;
1341         ENTRY;
1342
1343         lod_pools_fini(lod);
1344
1345         lod_procfs_fini(lod);
1346
1347         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
1348         if (rc)
1349                 CERROR("%s:can not fini ost descs %d\n",
1350                         lod2obd(lod)->obd_name, rc);
1351
1352         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
1353         if (rc)
1354                 CERROR("%s:can not fini mdt descs %d\n",
1355                         lod2obd(lod)->obd_name, rc);
1356
1357         RETURN(NULL);
1358 }
1359
1360 /**
1361  * Implementation of obd_ops::o_connect() for LOD
1362  *
1363  * Used to track all the users of this specific LOD device,
1364  * so the device stays up until the last user disconnected.
1365  *
1366  * \param[in] env               LU environment provided by the caller
1367  * \param[out] exp              export the caller will be using to access LOD
1368  * \param[in] obd               OBD device representing LOD device
1369  * \param[in] cluuid            unique identifier of the caller
1370  * \param[in] data              not used
1371  * \param[in] localdata         not used
1372  *
1373  * \retval 0                    on success
1374  * \retval negative             negated errno on error
1375  **/
1376 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1377                            struct obd_device *obd, struct obd_uuid *cluuid,
1378                            struct obd_connect_data *data, void *localdata)
1379 {
1380         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
1381         struct lustre_handle  conn;
1382         int                   rc;
1383         ENTRY;
1384
1385         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1386
1387         rc = class_connect(&conn, obd, cluuid);
1388         if (rc)
1389                 RETURN(rc);
1390
1391         *exp = class_conn2export(&conn);
1392
1393         spin_lock(&lod->lod_connects_lock);
1394         lod->lod_connects++;
1395         /* at the moment we expect the only user */
1396         LASSERT(lod->lod_connects == 1);
1397         spin_unlock(&lod->lod_connects_lock);
1398
1399         RETURN(0);
1400 }
1401
1402 /**
1403  *
1404  * Implementation of obd_ops::o_disconnect() for LOD
1405  *
1406  * When the caller doesn't need to use this LOD instance, it calls
1407  * obd_disconnect() and LOD releases corresponding export/reference count.
1408  * Once all the users gone, LOD device is released.
1409  *
1410  * \param[in] exp               export provided to the caller in obd_connect()
1411  *
1412  * \retval 0                    on success
1413  * \retval negative             negated errno on error
1414  **/
1415 static int lod_obd_disconnect(struct obd_export *exp)
1416 {
1417         struct obd_device *obd = exp->exp_obd;
1418         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1419         int                rc, release = 0;
1420         ENTRY;
1421
1422         /* Only disconnect the underlying layers on the final disconnect. */
1423         spin_lock(&lod->lod_connects_lock);
1424         lod->lod_connects--;
1425         if (lod->lod_connects != 0) {
1426                 /* why should there be more than 1 connect? */
1427                 spin_unlock(&lod->lod_connects_lock);
1428                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1429                        lod->lod_connects);
1430                 goto out;
1431         }
1432         spin_unlock(&lod->lod_connects_lock);
1433
1434         /* the last user of lod has gone, let's release the device */
1435         release = 1;
1436
1437 out:
1438         rc = class_disconnect(exp); /* bz 9811 */
1439
1440         if (rc == 0 && release)
1441                 class_manual_cleanup(obd);
1442         RETURN(rc);
1443 }
1444
1445 LU_KEY_INIT(lod, struct lod_thread_info);
1446
1447 static void lod_key_fini(const struct lu_context *ctx,
1448                 struct lu_context_key *key, void *data)
1449 {
1450         struct lod_thread_info *info = data;
1451         /* allocated in lod_get_lov_ea
1452          * XXX: this is overload, a tread may have such store but used only
1453          * once. Probably better would be pool of such stores per LOD.
1454          */
1455         if (info->lti_ea_store) {
1456                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1457                 info->lti_ea_store = NULL;
1458                 info->lti_ea_store_size = 0;
1459         }
1460         lu_buf_free(&info->lti_linkea_buf);
1461         OBD_FREE_PTR(info);
1462 }
1463
1464 /* context key: lod_thread_key */
1465 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1466
1467 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1468
1469 static struct lu_device_type_operations lod_device_type_ops = {
1470         .ldto_init           = lod_type_init,
1471         .ldto_fini           = lod_type_fini,
1472
1473         .ldto_start          = lod_type_start,
1474         .ldto_stop           = lod_type_stop,
1475
1476         .ldto_device_alloc   = lod_device_alloc,
1477         .ldto_device_free    = lod_device_free,
1478
1479         .ldto_device_fini    = lod_device_fini
1480 };
1481
1482 static struct lu_device_type lod_device_type = {
1483         .ldt_tags     = LU_DEVICE_DT,
1484         .ldt_name     = LUSTRE_LOD_NAME,
1485         .ldt_ops      = &lod_device_type_ops,
1486         .ldt_ctx_tags = LCT_MD_THREAD,
1487 };
1488
1489 /**
1490  * Implementation of obd_ops::o_get_info() for LOD
1491  *
1492  * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1493  * the caller binary status whether LOD has seen connection to any OST target.
1494  * It will also check if the MDT update log context being initialized (if
1495  * needed).
1496  *
1497  * \param[in] env               LU environment provided by the caller
1498  * \param[in] exp               export of the caller
1499  * \param[in] keylen            len of the key
1500  * \param[in] key               the key
1501  * \param[in] vallen            not used
1502  * \param[in] val               not used
1503  *
1504  * \retval                      0 if a connection was seen
1505  * \retval                      -EAGAIN if LOD isn't running yet or no
1506  *                              connection has been seen yet
1507  * \retval                      -EINVAL if not supported key is requested
1508  **/
1509 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1510                             __u32 keylen, void *key, __u32 *vallen, void *val)
1511 {
1512         int rc = -EINVAL;
1513
1514         if (KEY_IS(KEY_OSP_CONNECTED)) {
1515                 struct obd_device       *obd = exp->exp_obd;
1516                 struct lod_device       *d;
1517                 struct lod_tgt_desc     *tgt;
1518                 unsigned int            i;
1519                 int                     rc = 1;
1520
1521                 if (!obd->obd_set_up || obd->obd_stopping)
1522                         RETURN(-EAGAIN);
1523
1524                 d = lu2lod_dev(obd->obd_lu_dev);
1525                 lod_getref(&d->lod_ost_descs);
1526                 lod_foreach_ost(d, i) {
1527                         tgt = OST_TGT(d, i);
1528                         LASSERT(tgt && tgt->ltd_tgt);
1529                         rc = obd_get_info(env, tgt->ltd_exp, keylen, key,
1530                                           vallen, val);
1531                         /* one healthy device is enough */
1532                         if (rc == 0)
1533                                 break;
1534                 }
1535                 lod_putref(d, &d->lod_ost_descs);
1536
1537                 lod_getref(&d->lod_mdt_descs);
1538                 lod_foreach_mdt(d, i) {
1539                         struct llog_ctxt *ctxt;
1540
1541                         tgt = MDT_TGT(d, i);
1542                         LASSERT(tgt != NULL);
1543                         LASSERT(tgt->ltd_tgt != NULL);
1544                         ctxt = llog_get_context(tgt->ltd_tgt->dd_lu_dev.ld_obd,
1545                                                 LLOG_UPDATELOG_ORIG_CTXT);
1546                         if (ctxt == NULL) {
1547                                 rc = -EAGAIN;
1548                                 break;
1549                         }
1550                         if (ctxt->loc_handle == NULL) {
1551                                 rc = -EAGAIN;
1552                                 llog_ctxt_put(ctxt);
1553                                 break;
1554                         }
1555                         llog_ctxt_put(ctxt);
1556                 }
1557                 lod_putref(d, &d->lod_mdt_descs);
1558
1559                 RETURN(rc);
1560         }
1561
1562         RETURN(rc);
1563 }
1564
1565 static struct obd_ops lod_obd_device_ops = {
1566         .o_owner        = THIS_MODULE,
1567         .o_connect      = lod_obd_connect,
1568         .o_disconnect   = lod_obd_disconnect,
1569         .o_get_info     = lod_obd_get_info,
1570         .o_pool_new     = lod_pool_new,
1571         .o_pool_rem     = lod_pool_remove,
1572         .o_pool_add     = lod_pool_add,
1573         .o_pool_del     = lod_pool_del,
1574 };
1575
1576 static int __init lod_mod_init(void)
1577 {
1578         struct obd_type *type;
1579         int rc;
1580
1581         rc = lu_kmem_init(lod_caches);
1582         if (rc)
1583                 return rc;
1584
1585         rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
1586                                  LUSTRE_LOD_NAME, &lod_device_type);
1587         if (rc) {
1588                 lu_kmem_fini(lod_caches);
1589                 return rc;
1590         }
1591
1592         /* create "lov" entry in procfs for compatibility purposes */
1593         type = class_search_type(LUSTRE_LOV_NAME);
1594         if (type != NULL && type->typ_procroot != NULL)
1595                 return rc;
1596
1597         type = class_search_type(LUSTRE_LOD_NAME);
1598         type->typ_procsym = lprocfs_register("lov", proc_lustre_root,
1599                                              NULL, NULL);
1600         if (IS_ERR(type->typ_procsym)) {
1601                 CERROR("lod: can't create compat entry \"lov\": %d\n",
1602                        (int)PTR_ERR(type->typ_procsym));
1603                 type->typ_procsym = NULL;
1604         }
1605         return rc;
1606 }
1607
1608 static void __exit lod_mod_exit(void)
1609 {
1610         class_unregister_type(LUSTRE_LOD_NAME);
1611         lu_kmem_fini(lod_caches);
1612 }
1613
1614 MODULE_AUTHOR("Intel Corporation. <https://wiki.hpdd.intel.com/>");
1615 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
1616 MODULE_LICENSE("GPL");
1617
1618 module_init(lod_mod_init);
1619 module_exit(lod_mod_exit);
1620