Whamcloud - gitweb
LU-2675 lustre: remove lustre/include/linux/
[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 <obd_class.h>
93 #include <md_object.h>
94 #include <lustre_fid.h>
95 #include <lustre_param.h>
96 #include <lustre_update.h>
97
98 #include "lod_internal.h"
99
100 /*
101  * Lookup target by FID.
102  *
103  * Lookup MDT/OST target index by FID. Type of the target can be
104  * specific or any.
105  *
106  * \param[in] env               LU environment provided by the caller
107  * \param[in] lod               lod device
108  * \param[in] fid               FID
109  * \param[out] tgt              result target index
110  * \param[in] type              expected type of the target:
111  *                              LU_SEQ_RANGE_{MDT,OST,ANY}
112  *
113  * \retval 0                    on success
114  * \retval negative             negated errno on error
115  **/
116 int lod_fld_lookup(const struct lu_env *env, struct lod_device *lod,
117                    const struct lu_fid *fid, __u32 *tgt, int *type)
118 {
119         struct lu_seq_range     range = { 0 };
120         struct lu_server_fld    *server_fld;
121         int rc;
122         ENTRY;
123
124         if (!fid_is_sane(fid)) {
125                 CERROR("%s: invalid FID "DFID"\n", lod2obd(lod)->obd_name,
126                        PFID(fid));
127                 RETURN(-EIO);
128         }
129
130         if (fid_is_idif(fid)) {
131                 *tgt = fid_idif_ost_idx(fid);
132                 *type = LU_SEQ_RANGE_OST;
133                 RETURN(0);
134         }
135
136         if (!lod->lod_initialized || (!fid_seq_in_fldb(fid_seq(fid)))) {
137                 LASSERT(lu_site2seq(lod2lu_dev(lod)->ld_site) != NULL);
138
139                 *tgt = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_node_id;
140                 *type = LU_SEQ_RANGE_MDT;
141                 RETURN(0);
142         }
143
144         server_fld = lu_site2seq(lod2lu_dev(lod)->ld_site)->ss_server_fld;
145         fld_range_set_type(&range, *type);
146         rc = fld_server_lookup(env, server_fld, fid_seq(fid), &range);
147         if (rc != 0)
148                 RETURN(rc);
149
150         *tgt = range.lsr_index;
151         *type = range.lsr_flags;
152
153         CDEBUG(D_INFO, "%s: got tgt %x for sequence: "LPX64"\n",
154                lod2obd(lod)->obd_name, *tgt, fid_seq(fid));
155
156         RETURN(0);
157 }
158
159 extern struct lu_object_operations lod_lu_obj_ops;
160 extern struct dt_object_operations lod_obj_ops;
161
162 /* Slab for OSD object allocation */
163 struct kmem_cache *lod_object_kmem;
164
165 static struct lu_kmem_descr lod_caches[] = {
166         {
167                 .ckd_cache = &lod_object_kmem,
168                 .ckd_name  = "lod_obj",
169                 .ckd_size  = sizeof(struct lod_object)
170         },
171         {
172                 .ckd_cache = NULL
173         }
174 };
175
176 static struct lu_device *lod_device_fini(const struct lu_env *env,
177                                          struct lu_device *d);
178
179 /**
180  * Implementation of lu_device_operations::ldo_object_alloc() for LOD
181  *
182  * Allocates and initializes LOD's slice in the given object.
183  *
184  * see include/lu_object.h for the details.
185  */
186 struct lu_object *lod_object_alloc(const struct lu_env *env,
187                                    const struct lu_object_header *hdr,
188                                    struct lu_device *dev)
189 {
190         struct lod_object       *lod_obj;
191         struct lu_object        *lu_obj;
192         ENTRY;
193
194         OBD_SLAB_ALLOC_PTR_GFP(lod_obj, lod_object_kmem, GFP_NOFS);
195         if (lod_obj == NULL)
196                 RETURN(ERR_PTR(-ENOMEM));
197
198         lu_obj = lod2lu_obj(lod_obj);
199         dt_object_init(&lod_obj->ldo_obj, NULL, dev);
200         lod_obj->ldo_obj.do_ops = &lod_obj_ops;
201         lu_obj->lo_ops = &lod_lu_obj_ops;
202
203         RETURN(lu_obj);
204 }
205
206 /**
207  * Cleanup table of target's descriptors.
208  *
209  * The function goes through all the targets in the given table
210  * and apply given configuration command on to the targets.
211  * Used to cleanup the targets at unmount.
212  *
213  * \param[in] env               LU environment provided by the caller
214  * \param[in] lod               lod device
215  * \param[in] ltd               target's table to go through
216  * \param[in] lcfg              configuration command to apply
217  *
218  * \retval 0                    on success
219  * \retval negative             negated errno on error
220  **/
221 static int lod_cleanup_desc_tgts(const struct lu_env *env,
222                                  struct lod_device *lod,
223                                  struct lod_tgt_descs *ltd,
224                                  struct lustre_cfg *lcfg)
225 {
226         struct lu_device  *next;
227         int rc = 0;
228         unsigned int i;
229
230         lod_getref(ltd);
231         if (ltd->ltd_tgts_size <= 0) {
232                 lod_putref(lod, ltd);
233                 return 0;
234         }
235         cfs_foreach_bit(ltd->ltd_tgt_bitmap, i) {
236                 struct lod_tgt_desc *tgt;
237                 int rc1;
238
239                 tgt = LTD_TGT(ltd, i);
240                 LASSERT(tgt && tgt->ltd_tgt);
241                 next = &tgt->ltd_tgt->dd_lu_dev;
242                 rc1 = next->ld_ops->ldo_process_config(env, next, lcfg);
243                 if (rc1) {
244                         CERROR("%s: error cleaning up LOD index %u: cmd %#x"
245                                ": rc = %d\n", lod2obd(lod)->obd_name, i,
246                                lcfg->lcfg_command, rc1);
247                         rc = rc1;
248                 }
249         }
250         lod_putref(lod, ltd);
251         return rc;
252 }
253
254 /**
255  * Extract MDT target index from a device name.
256  *
257  * a helper function to extract index from the given device name
258  * like "fsname-MDTxxxx-mdtlov"
259  *
260  * \param[in] lodname   device name
261  * \param[out] index    extracted index
262  *
263  * \retval 0            on success
264  * \retval -EINVAL      if the name is invalid
265  */
266 static int lodname2mdt_index(char *lodname, long *index)
267 {
268         char *ptr, *tmp;
269
270         ptr = strrchr(lodname, '-');
271         if (ptr == NULL) {
272                 CERROR("invalid MDT index in '%s'\n", lodname);
273                 return -EINVAL;
274         }
275
276         if (strncmp(ptr, "-mdtlov", 7) != 0) {
277                 CERROR("invalid MDT index in '%s'\n", lodname);
278                 return -EINVAL;
279         }
280
281         if ((unsigned long)ptr - (unsigned long)lodname <= 8) {
282                 CERROR("invalid MDT index in '%s'\n", lodname);
283                 return -EINVAL;
284         }
285
286         if (strncmp(ptr - 8, "-MDT", 4) != 0) {
287                 CERROR("invalid MDT index in '%s'\n", lodname);
288                 return -EINVAL;
289         }
290
291         *index = simple_strtol(ptr - 4, &tmp, 16);
292         if (*tmp != '-' || *index > INT_MAX || *index < 0) {
293                 CERROR("invalid MDT index in '%s'\n", lodname);
294                 return -EINVAL;
295         }
296         return 0;
297 }
298
299 /**
300  * Implementation of lu_device_operations::ldo_process_config() for LOD
301  *
302  * The method is called by the configuration subsystem during setup,
303  * cleanup and when the configuration changes. The method processes
304  * few specific commands like adding/removing the targets, changing
305  * the runtime parameters.
306
307  * \param[in] env               LU environment provided by the caller
308  * \param[in] dev               lod device
309  * \param[in] lcfg              configuration command to apply
310  *
311  * \retval 0                    on success
312  * \retval negative             negated errno on error
313  *
314  * The examples are below.
315  *
316  * Add osc config log:
317  * marker  20 (flags=0x01, v2.2.49.56) lustre-OST0001  'add osc'
318  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nidxxx
319  * attach    0:lustre-OST0001-osc-MDT0001  1:osc  2:lustre-MDT0001-mdtlov_UUID
320  * setup     0:lustre-OST0001-osc-MDT0001  1:lustre-OST0001_UUID  2:nid
321  * lov_modify_tgts add 0:lustre-MDT0001-mdtlov  1:lustre-OST0001_UUID  2:1  3:1
322  * marker  20 (flags=0x02, v2.2.49.56) lustre-OST0001  'add osc'
323  *
324  * Add mdc config log:
325  * marker  10 (flags=0x01, v2.2.49.56) lustre-MDT0000  'add osp'
326  * add_uuid  nid=192.168.122.162@tcp(0x20000c0a87aa2)  0:  1:nid
327  * attach 0:lustre-MDT0000-osp-MDT0001  1:osp  2:lustre-MDT0001-mdtlov_UUID
328  * setup     0:lustre-MDT0000-osp-MDT0001  1:lustre-MDT0000_UUID  2:nid
329  * modify_mdc_tgts add 0:lustre-MDT0001  1:lustre-MDT0000_UUID  2:0  3:1
330  * marker  10 (flags=0x02, v2.2.49.56) lustre-MDT0000_UUID  'add osp'
331  */
332 static int lod_process_config(const struct lu_env *env,
333                               struct lu_device *dev,
334                               struct lustre_cfg *lcfg)
335 {
336         struct lod_device *lod = lu2lod_dev(dev);
337         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
338         char              *arg1;
339         int                rc = 0;
340         ENTRY;
341
342         switch(lcfg->lcfg_command) {
343         case LCFG_LOV_DEL_OBD:
344         case LCFG_LOV_ADD_INA:
345         case LCFG_LOV_ADD_OBD:
346         case LCFG_ADD_MDC: {
347                 __u32 index;
348                 __u32 mdt_index;
349                 int gen;
350                 /* lov_modify_tgts add  0:lov_mdsA  1:osp  2:0  3:1
351                  * modify_mdc_tgts add  0:lustre-MDT0001
352                  *                    1:lustre-MDT0001-mdc0002
353                  *                    2:2  3:1*/
354                 arg1 = lustre_cfg_string(lcfg, 1);
355
356                 if (sscanf(lustre_cfg_buf(lcfg, 2), "%d", &index) != 1)
357                         GOTO(out, rc = -EINVAL);
358                 if (sscanf(lustre_cfg_buf(lcfg, 3), "%d", &gen) != 1)
359                         GOTO(out, rc = -EINVAL);
360
361                 if (lcfg->lcfg_command == LCFG_LOV_ADD_OBD) {
362                         char *mdt;
363                         mdt = strstr(lustre_cfg_string(lcfg, 0), "-MDT");
364                         /* 1.8 configs don't have "-MDT0000" at the end */
365                         if (mdt == NULL) {
366                                 mdt_index = 0;
367                         } else {
368                                 long long_index;
369                                 rc = lodname2mdt_index(
370                                         lustre_cfg_string(lcfg, 0),
371                                         &long_index);
372                                 if (rc != 0)
373                                         GOTO(out, rc);
374                                 mdt_index = long_index;
375                         }
376                         rc = lod_add_device(env, lod, arg1, index, gen,
377                                             mdt_index, LUSTRE_OSC_NAME, 1);
378                 } else if (lcfg->lcfg_command == LCFG_ADD_MDC) {
379                         mdt_index = index;
380                         rc = lod_add_device(env, lod, arg1, index, gen,
381                                             mdt_index, LUSTRE_MDC_NAME, 1);
382                 } else if (lcfg->lcfg_command == LCFG_LOV_ADD_INA) {
383                         /*FIXME: Add mdt_index for LCFG_LOV_ADD_INA*/
384                         mdt_index = 0;
385                         rc = lod_add_device(env, lod, arg1, index, gen,
386                                             mdt_index, LUSTRE_OSC_NAME, 0);
387                 } else {
388                         rc = lod_del_device(env, lod,
389                                             &lod->lod_ost_descs,
390                                             arg1, index, gen, true);
391                 }
392
393                 break;
394         }
395
396         case LCFG_PARAM: {
397                 struct obd_device *obd = lod2obd(lod);
398
399                 rc = class_process_proc_param(PARAM_LOV, obd->obd_vars,
400                                               lcfg, obd);
401                 if (rc > 0)
402                         rc = 0;
403                 GOTO(out, rc);
404         }
405         case LCFG_CLEANUP:
406         case LCFG_PRE_CLEANUP: {
407                 lu_dev_del_linkage(dev->ld_site, dev);
408                 lod_cleanup_desc_tgts(env, lod, &lod->lod_mdt_descs, lcfg);
409                 lod_cleanup_desc_tgts(env, lod, &lod->lod_ost_descs, lcfg);
410                 if (lcfg->lcfg_command == LCFG_PRE_CLEANUP)
411                         break;
412                 /*
413                  * do cleanup on underlying storage only when
414                  * all OSPs are cleaned up, as they use that OSD as well
415                  */
416                 next = &lod->lod_child->dd_lu_dev;
417                 rc = next->ld_ops->ldo_process_config(env, next, lcfg);
418                 if (rc)
419                         CERROR("%s: can't process %u: %d\n",
420                                lod2obd(lod)->obd_name, lcfg->lcfg_command, rc);
421
422                 rc = obd_disconnect(lod->lod_child_exp);
423                 if (rc)
424                         CERROR("error in disconnect from storage: %d\n", rc);
425                 break;
426         }
427         default:
428                CERROR("%s: unknown command %u\n", lod2obd(lod)->obd_name,
429                       lcfg->lcfg_command);
430                rc = -EINVAL;
431                break;
432         }
433
434 out:
435         RETURN(rc);
436 }
437
438 /**
439  * Implementation of lu_device_operations::ldo_recovery_complete() for LOD
440  *
441  * The method is called once the recovery is complete. This implementation
442  * distributes the notification to all the known targets.
443  *
444  * see include/lu_object.h for the details
445  */
446 static int lod_recovery_complete(const struct lu_env *env,
447                                  struct lu_device *dev)
448 {
449         struct lod_device   *lod = lu2lod_dev(dev);
450         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
451         unsigned int         i;
452         int                  rc;
453         ENTRY;
454
455         LASSERT(lod->lod_recovery_completed == 0);
456         lod->lod_recovery_completed = 1;
457
458         rc = next->ld_ops->ldo_recovery_complete(env, next);
459
460         lod_getref(&lod->lod_ost_descs);
461         if (lod->lod_osts_size > 0) {
462                 cfs_foreach_bit(lod->lod_ost_bitmap, i) {
463                         struct lod_tgt_desc *tgt;
464                         tgt = OST_TGT(lod, i);
465                         LASSERT(tgt && tgt->ltd_tgt);
466                         next = &tgt->ltd_ost->dd_lu_dev;
467                         rc = next->ld_ops->ldo_recovery_complete(env, next);
468                         if (rc)
469                                 CERROR("%s: can't complete recovery on #%d:"
470                                         "%d\n", lod2obd(lod)->obd_name, i, rc);
471                 }
472         }
473         lod_putref(lod, &lod->lod_ost_descs);
474         RETURN(rc);
475 }
476
477 /**
478  * Implementation of lu_device_operations::ldo_prepare() for LOD
479  *
480  * see include/lu_object.h for the details.
481  */
482 static int lod_prepare(const struct lu_env *env, struct lu_device *pdev,
483                        struct lu_device *cdev)
484 {
485         struct lod_device   *lod = lu2lod_dev(cdev);
486         struct lu_device    *next = &lod->lod_child->dd_lu_dev;
487         int                  rc;
488         ENTRY;
489
490         rc = next->ld_ops->ldo_prepare(env, pdev, next);
491         if (rc != 0) {
492                 CERROR("%s: prepare bottom error: rc = %d\n",
493                        lod2obd(lod)->obd_name, rc);
494                 RETURN(rc);
495         }
496
497         lod->lod_initialized = 1;
498
499         RETURN(rc);
500 }
501
502 const struct lu_device_operations lod_lu_ops = {
503         .ldo_object_alloc       = lod_object_alloc,
504         .ldo_process_config     = lod_process_config,
505         .ldo_recovery_complete  = lod_recovery_complete,
506         .ldo_prepare            = lod_prepare,
507 };
508
509 /**
510  * Implementation of dt_device_operations::dt_root_get() for LOD
511  *
512  * see include/dt_object.h for the details.
513  */
514 static int lod_root_get(const struct lu_env *env,
515                         struct dt_device *dev, struct lu_fid *f)
516 {
517         return dt_root_get(env, dt2lod_dev(dev)->lod_child, f);
518 }
519
520 /**
521  * Implementation of dt_device_operations::dt_statfs() for LOD
522  *
523  * see include/dt_object.h for the details.
524  */
525 static int lod_statfs(const struct lu_env *env,
526                       struct dt_device *dev, struct obd_statfs *sfs)
527 {
528         return dt_statfs(env, dt2lod_dev(dev)->lod_child, sfs);
529 }
530
531 /**
532  * Implementation of dt_device_operations::dt_trans_create() for LOD
533  *
534  * Creates a transaction using local (to this node) OSD.
535  *
536  * see include/dt_object.h for the details.
537  */
538 static struct thandle *lod_trans_create(const struct lu_env *env,
539                                         struct dt_device *dev)
540 {
541         struct thandle *th;
542
543         th = dt_trans_create(env, dt2lod_dev(dev)->lod_child);
544         if (IS_ERR(th))
545                 return th;
546
547         return th;
548 }
549
550 /**
551  * Implementation of dt_device_operations::dt_trans_start() for LOD
552  *
553  * Starts the set of local transactions using the targets involved
554  * in declare phase. Initial support for the distributed transactions.
555  *
556  * see include/dt_object.h for the details.
557  */
558 static int lod_trans_start(const struct lu_env *env, struct dt_device *dev,
559                            struct thandle *th)
560 {
561         struct lod_device *lod = dt2lod_dev((struct dt_device *) dev);
562         int rc = 0;
563
564         if (unlikely(th->th_update != NULL)) {
565                 struct thandle_update *tu = th->th_update;
566                 struct dt_update_request *update;
567
568                 list_for_each_entry(update, &tu->tu_remote_update_list,
569                                     dur_list) {
570                         LASSERT(update->dur_dt != NULL);
571                         rc = dt_trans_start(env, update->dur_dt, th);
572                         if (rc != 0)
573                                 return rc;
574                 }
575         }
576         return dt_trans_start(env, lod->lod_child, th);
577 }
578
579 /**
580  * Implementation of dt_device_operations::dt_trans_stop() for LOD
581  *
582  * Stops the set of local transactions using the targets involved
583  * in declare phase. Initial support for the distributed transactions.
584  *
585  * see include/dt_object.h for the details.
586  */
587 static int lod_trans_stop(const struct lu_env *env, struct dt_device *dt,
588                           struct thandle *th)
589 {
590         struct thandle_update           *tu = th->th_update;
591         struct dt_update_request        *update;
592         struct dt_update_request        *tmp;
593         int                             rc2 = 0;
594         int                             rc;
595         ENTRY;
596
597         rc = dt_trans_stop(env, th->th_dev, th);
598         if (likely(tu == NULL))
599                 RETURN(rc);
600
601         list_for_each_entry_safe(update, tmp,
602                                  &tu->tu_remote_update_list,
603                                  dur_list) {
604                 /* update will be freed inside dt_trans_stop */
605                 rc2 = dt_trans_stop(env, update->dur_dt, th);
606                 if (unlikely(rc2 != 0 && rc == 0))
607                         rc = rc2;
608         }
609
610         RETURN(rc);
611 }
612
613 /**
614  * Implementation of dt_device_operations::dt_conf_get() for LOD
615  *
616  * Currently returns the configuration provided by the local OSD.
617  *
618  * see include/dt_object.h for the details.
619  */
620 static void lod_conf_get(const struct lu_env *env,
621                          const struct dt_device *dev,
622                          struct dt_device_param *param)
623 {
624         dt_conf_get(env, dt2lod_dev((struct dt_device *)dev)->lod_child, param);
625 }
626
627 /**
628  * Implementation of dt_device_operations::dt_sync() for LOD
629  *
630  * Syncs all known OST targets. Very very expensive and used
631  * rarely by LFSCK now. Should not be used in general.
632  *
633  * see include/dt_object.h for the details.
634  */
635 static int lod_sync(const struct lu_env *env, struct dt_device *dev)
636 {
637         struct lod_device   *lod = dt2lod_dev(dev);
638         struct lod_ost_desc *ost;
639         unsigned int         i;
640         int                  rc = 0;
641         ENTRY;
642
643         lod_getref(&lod->lod_ost_descs);
644         lod_foreach_ost(lod, i) {
645                 ost = OST_TGT(lod, i);
646                 LASSERT(ost && ost->ltd_ost);
647                 rc = dt_sync(env, ost->ltd_ost);
648                 if (rc) {
649                         CERROR("%s: can't sync %u: %d\n",
650                                lod2obd(lod)->obd_name, i, rc);
651                         break;
652                 }
653         }
654         lod_putref(lod, &lod->lod_ost_descs);
655         if (rc == 0)
656                 rc = dt_sync(env, lod->lod_child);
657
658         RETURN(rc);
659 }
660
661 /**
662  * Implementation of dt_device_operations::dt_ro() for LOD
663  *
664  * Turns local OSD read-only, used for the testing only.
665  *
666  * see include/dt_object.h for the details.
667  */
668 static int lod_ro(const struct lu_env *env, struct dt_device *dev)
669 {
670         return dt_ro(env, dt2lod_dev(dev)->lod_child);
671 }
672
673 /**
674  * Implementation of dt_device_operations::dt_commit_async() for LOD
675  *
676  * Asks local OSD to commit sooner.
677  *
678  * see include/dt_object.h for the details.
679  */
680 static int lod_commit_async(const struct lu_env *env, struct dt_device *dev)
681 {
682         return dt_commit_async(env, dt2lod_dev(dev)->lod_child);
683 }
684
685 /**
686  * Not used
687  */
688 static int lod_init_capa_ctxt(const struct lu_env *env, struct dt_device *dev,
689                               int mode, unsigned long timeout,
690                               __u32 alg, struct lustre_capa_key *keys)
691 {
692         struct dt_device *next = dt2lod_dev(dev)->lod_child;
693         return dt_init_capa_ctxt(env, next, mode, timeout, alg, keys);
694 }
695
696 static const struct dt_device_operations lod_dt_ops = {
697         .dt_root_get         = lod_root_get,
698         .dt_statfs           = lod_statfs,
699         .dt_trans_create     = lod_trans_create,
700         .dt_trans_start      = lod_trans_start,
701         .dt_trans_stop       = lod_trans_stop,
702         .dt_conf_get         = lod_conf_get,
703         .dt_sync             = lod_sync,
704         .dt_ro               = lod_ro,
705         .dt_commit_async     = lod_commit_async,
706         .dt_init_capa_ctxt   = lod_init_capa_ctxt,
707 };
708
709 /**
710  * Connect to a local OSD.
711  *
712  * Used to connect to the local OSD at mount. OSD name is taken from the
713  * configuration command passed. This connection is used to identify LU
714  * site and pin the OSD from early removal.
715  *
716  * \param[in] env               LU environment provided by the caller
717  * \param[in] lod               lod device
718  * \param[in] cfg               configuration command to apply
719  *
720  * \retval 0                    on success
721  * \retval negative             negated errno on error
722  **/
723 static int lod_connect_to_osd(const struct lu_env *env, struct lod_device *lod,
724                               struct lustre_cfg *cfg)
725 {
726         struct obd_connect_data *data = NULL;
727         struct obd_device       *obd;
728         char                    *nextdev = NULL, *p, *s;
729         int                      rc, len = 0;
730         ENTRY;
731
732         LASSERT(cfg);
733         LASSERT(lod->lod_child_exp == NULL);
734
735         /* compatibility hack: we still use old config logs
736          * which specify LOV, but we need to learn underlying
737          * OSD device, which is supposed to be:
738          *  <fsname>-MDTxxxx-osd
739          *
740          * 2.x MGS generates lines like the following:
741          *   #03 (176)lov_setup 0:lustre-MDT0000-mdtlov  1:(struct lov_desc)
742          * 1.8 MGS generates lines like the following:
743          *   #03 (168)lov_setup 0:lustre-mdtlov  1:(struct lov_desc)
744          *
745          * we use "-MDT" to differentiate 2.x from 1.8 */
746
747         if ((p = lustre_cfg_string(cfg, 0)) && strstr(p, "-mdtlov")) {
748                 len = strlen(p) + 1;
749                 OBD_ALLOC(nextdev, len);
750                 if (nextdev == NULL)
751                         GOTO(out, rc = -ENOMEM);
752
753                 strcpy(nextdev, p);
754                 s = strstr(nextdev, "-mdtlov");
755                 if (unlikely(s == NULL)) {
756                         CERROR("unable to parse device name %s\n",
757                                lustre_cfg_string(cfg, 0));
758                         GOTO(out, rc = -EINVAL);
759                 }
760
761                 if (strstr(nextdev, "-MDT")) {
762                         /* 2.x config */
763                         strcpy(s, "-osd");
764                 } else {
765                         /* 1.8 config */
766                         strcpy(s, "-MDT0000-osd");
767                 }
768         } else {
769                 CERROR("unable to parse device name %s\n",
770                        lustre_cfg_string(cfg, 0));
771                 GOTO(out, rc = -EINVAL);
772         }
773
774         OBD_ALLOC_PTR(data);
775         if (data == NULL)
776                 GOTO(out, rc = -ENOMEM);
777
778         obd = class_name2obd(nextdev);
779         if (obd == NULL) {
780                 CERROR("can not locate next device: %s\n", nextdev);
781                 GOTO(out, rc = -ENOTCONN);
782         }
783
784         data->ocd_connect_flags = OBD_CONNECT_VERSION;
785         data->ocd_version = LUSTRE_VERSION_CODE;
786
787         rc = obd_connect(env, &lod->lod_child_exp, obd, &obd->obd_uuid,
788                          data, NULL);
789         if (rc) {
790                 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
791                 GOTO(out, rc);
792         }
793
794         lod->lod_dt_dev.dd_lu_dev.ld_site =
795                 lod->lod_child_exp->exp_obd->obd_lu_dev->ld_site;
796         LASSERT(lod->lod_dt_dev.dd_lu_dev.ld_site);
797         lod->lod_child = lu2dt_dev(lod->lod_child_exp->exp_obd->obd_lu_dev);
798
799 out:
800         if (data)
801                 OBD_FREE_PTR(data);
802         if (nextdev)
803                 OBD_FREE(nextdev, len);
804         RETURN(rc);
805 }
806
807 /**
808  * Allocate and initialize target table.
809  *
810  * A helper function to initialize the target table and allocate
811  * a bitmap of the available targets.
812  *
813  * \param[in] ltd               target's table to initialize
814  *
815  * \retval 0                    on success
816  * \retval negative             negated errno on error
817  **/
818 static int lod_tgt_desc_init(struct lod_tgt_descs *ltd)
819 {
820         mutex_init(&ltd->ltd_mutex);
821         init_rwsem(&ltd->ltd_rw_sem);
822
823         /* the OST array and bitmap are allocated/grown dynamically as OSTs are
824          * added to the LOD, see lod_add_device() */
825         ltd->ltd_tgt_bitmap = CFS_ALLOCATE_BITMAP(32);
826         if (ltd->ltd_tgt_bitmap == NULL)
827                 RETURN(-ENOMEM);
828
829         ltd->ltd_tgts_size  = 32;
830         ltd->ltd_tgtnr      = 0;
831
832         ltd->ltd_death_row = 0;
833         ltd->ltd_refcount  = 0;
834         return 0;
835 }
836
837 /**
838  * Initialize LOD device at setup.
839  *
840  * Initializes the given LOD device using the original configuration command.
841  * The function initiates a connection to the local OSD and initializes few
842  * internal structures like pools, target tables, etc.
843  *
844  * \param[in] env               LU environment provided by the caller
845  * \param[in] lod               lod device
846  * \param[in] ldt               not used
847  * \param[in] cfg               configuration command
848  *
849  * \retval 0                    on success
850  * \retval negative             negated errno on error
851  **/
852 static int lod_init0(const struct lu_env *env, struct lod_device *lod,
853                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
854 {
855         struct dt_device_param ddp;
856         struct obd_device     *obd;
857         int                    rc;
858         ENTRY;
859
860         obd = class_name2obd(lustre_cfg_string(cfg, 0));
861         if (obd == NULL) {
862                 CERROR("Cannot find obd with name %s\n",
863                        lustre_cfg_string(cfg, 0));
864                 RETURN(-ENODEV);
865         }
866
867         obd->obd_lu_dev = &lod->lod_dt_dev.dd_lu_dev;
868         lod->lod_dt_dev.dd_lu_dev.ld_obd = obd;
869         lod->lod_dt_dev.dd_lu_dev.ld_ops = &lod_lu_ops;
870         lod->lod_dt_dev.dd_ops = &lod_dt_ops;
871
872         rc = lod_connect_to_osd(env, lod, cfg);
873         if (rc)
874                 RETURN(rc);
875
876         dt_conf_get(env, &lod->lod_dt_dev, &ddp);
877         lod->lod_osd_max_easize = ddp.ddp_max_ea_size;
878
879         /* setup obd to be used with old lov code */
880         rc = lod_pools_init(lod, cfg);
881         if (rc)
882                 GOTO(out_disconnect, rc);
883
884         rc = lod_procfs_init(lod);
885         if (rc)
886                 GOTO(out_pools, rc);
887
888         spin_lock_init(&lod->lod_desc_lock);
889         spin_lock_init(&lod->lod_connects_lock);
890         lod_tgt_desc_init(&lod->lod_mdt_descs);
891         lod_tgt_desc_init(&lod->lod_ost_descs);
892
893         RETURN(0);
894
895 out_pools:
896         lod_pools_fini(lod);
897 out_disconnect:
898         obd_disconnect(lod->lod_child_exp);
899         RETURN(rc);
900 }
901
902 /**
903  * Implementation of lu_device_type_operations::ldto_device_free() for LOD
904  *
905  * Releases the memory allocated for LOD device.
906  *
907  * see include/lu_object.h for the details.
908  */
909 static struct lu_device *lod_device_free(const struct lu_env *env,
910                                          struct lu_device *lu)
911 {
912         struct lod_device *lod = lu2lod_dev(lu);
913         struct lu_device  *next = &lod->lod_child->dd_lu_dev;
914         ENTRY;
915
916         LASSERT(atomic_read(&lu->ld_ref) == 0);
917         dt_device_fini(&lod->lod_dt_dev);
918         OBD_FREE_PTR(lod);
919         RETURN(next);
920 }
921
922 /**
923  * Implementation of lu_device_type_operations::ldto_device_alloc() for LOD
924  *
925  * Allocates LOD device and calls the helpers to initialize it.
926  *
927  * see include/lu_object.h for the details.
928  */
929 static struct lu_device *lod_device_alloc(const struct lu_env *env,
930                                           struct lu_device_type *type,
931                                           struct lustre_cfg *lcfg)
932 {
933         struct lod_device *lod;
934         struct lu_device  *lu_dev;
935
936         OBD_ALLOC_PTR(lod);
937         if (lod == NULL) {
938                 lu_dev = ERR_PTR(-ENOMEM);
939         } else {
940                 int rc;
941
942                 lu_dev = lod2lu_dev(lod);
943                 dt_device_init(&lod->lod_dt_dev, type);
944                 rc = lod_init0(env, lod, type, lcfg);
945                 if (rc != 0) {
946                         lod_device_free(env, lu_dev);
947                         lu_dev = ERR_PTR(rc);
948                 }
949         }
950
951         return lu_dev;
952 }
953
954 /**
955  * Implementation of lu_device_type_operations::ldto_device_fini() for LOD
956  *
957  * Releases the internal resources used by LOD device.
958  *
959  * see include/lu_object.h for the details.
960  */
961 static struct lu_device *lod_device_fini(const struct lu_env *env,
962                                          struct lu_device *d)
963 {
964         struct lod_device *lod = lu2lod_dev(d);
965         int                rc;
966         ENTRY;
967
968         lod_pools_fini(lod);
969
970         lod_procfs_fini(lod);
971
972         rc = lod_fini_tgt(env, lod, &lod->lod_ost_descs, true);
973         if (rc)
974                 CERROR("%s:can not fini ost descs %d\n",
975                         lod2obd(lod)->obd_name, rc);
976
977         rc = lod_fini_tgt(env, lod, &lod->lod_mdt_descs, false);
978         if (rc)
979                 CERROR("%s:can not fini mdt descs %d\n",
980                         lod2obd(lod)->obd_name, rc);
981
982         RETURN(NULL);
983 }
984
985 /**
986  * Implementation of obd_ops::o_connect() for LOD
987  *
988  * Used to track all the users of this specific LOD device,
989  * so the device stays up until the last user disconnected.
990  *
991  * \param[in] env               LU environment provided by the caller
992  * \param[out] exp              export the caller will be using to access LOD
993  * \param[in] obd               OBD device representing LOD device
994  * \param[in] cluuid            unique identifier of the caller
995  * \param[in] data              not used
996  * \param[in] localdata         not used
997  *
998  * \retval 0                    on success
999  * \retval negative             negated errno on error
1000  **/
1001 static int lod_obd_connect(const struct lu_env *env, struct obd_export **exp,
1002                            struct obd_device *obd, struct obd_uuid *cluuid,
1003                            struct obd_connect_data *data, void *localdata)
1004 {
1005         struct lod_device    *lod = lu2lod_dev(obd->obd_lu_dev);
1006         struct lustre_handle  conn;
1007         int                   rc;
1008         ENTRY;
1009
1010         CDEBUG(D_CONFIG, "connect #%d\n", lod->lod_connects);
1011
1012         rc = class_connect(&conn, obd, cluuid);
1013         if (rc)
1014                 RETURN(rc);
1015
1016         *exp = class_conn2export(&conn);
1017
1018         spin_lock(&lod->lod_connects_lock);
1019         lod->lod_connects++;
1020         /* at the moment we expect the only user */
1021         LASSERT(lod->lod_connects == 1);
1022         spin_unlock(&lod->lod_connects_lock);
1023
1024         RETURN(0);
1025 }
1026
1027 /**
1028  *
1029  * Implementation of obd_ops::o_disconnect() for LOD
1030  *
1031  * When the caller doesn't need to use this LOD instance, it calls
1032  * obd_disconnect() and LOD releases corresponding export/reference count.
1033  * Once all the users gone, LOD device is released.
1034  *
1035  * \param[in] exp               export provided to the caller in obd_connect()
1036  *
1037  * \retval 0                    on success
1038  * \retval negative             negated errno on error
1039  **/
1040 static int lod_obd_disconnect(struct obd_export *exp)
1041 {
1042         struct obd_device *obd = exp->exp_obd;
1043         struct lod_device *lod = lu2lod_dev(obd->obd_lu_dev);
1044         int                rc, release = 0;
1045         ENTRY;
1046
1047         /* Only disconnect the underlying layers on the final disconnect. */
1048         spin_lock(&lod->lod_connects_lock);
1049         lod->lod_connects--;
1050         if (lod->lod_connects != 0) {
1051                 /* why should there be more than 1 connect? */
1052                 spin_unlock(&lod->lod_connects_lock);
1053                 CERROR("%s: disconnect #%d\n", exp->exp_obd->obd_name,
1054                        lod->lod_connects);
1055                 goto out;
1056         }
1057         spin_unlock(&lod->lod_connects_lock);
1058
1059         /* the last user of lod has gone, let's release the device */
1060         release = 1;
1061
1062 out:
1063         rc = class_disconnect(exp); /* bz 9811 */
1064
1065         if (rc == 0 && release)
1066                 class_manual_cleanup(obd);
1067         RETURN(rc);
1068 }
1069
1070 LU_KEY_INIT(lod, struct lod_thread_info);
1071
1072 static void lod_key_fini(const struct lu_context *ctx,
1073                 struct lu_context_key *key, void *data)
1074 {
1075         struct lod_thread_info *info = data;
1076         /* allocated in lod_get_lov_ea
1077          * XXX: this is overload, a tread may have such store but used only
1078          * once. Probably better would be pool of such stores per LOD.
1079          */
1080         if (info->lti_ea_store) {
1081                 OBD_FREE_LARGE(info->lti_ea_store, info->lti_ea_store_size);
1082                 info->lti_ea_store = NULL;
1083                 info->lti_ea_store_size = 0;
1084         }
1085         lu_buf_free(&info->lti_linkea_buf);
1086         OBD_FREE_PTR(info);
1087 }
1088
1089 /* context key: lod_thread_key */
1090 LU_CONTEXT_KEY_DEFINE(lod, LCT_MD_THREAD);
1091
1092 LU_TYPE_INIT_FINI(lod, &lod_thread_key);
1093
1094 static struct lu_device_type_operations lod_device_type_ops = {
1095         .ldto_init           = lod_type_init,
1096         .ldto_fini           = lod_type_fini,
1097
1098         .ldto_start          = lod_type_start,
1099         .ldto_stop           = lod_type_stop,
1100
1101         .ldto_device_alloc   = lod_device_alloc,
1102         .ldto_device_free    = lod_device_free,
1103
1104         .ldto_device_fini    = lod_device_fini
1105 };
1106
1107 static struct lu_device_type lod_device_type = {
1108         .ldt_tags     = LU_DEVICE_DT,
1109         .ldt_name     = LUSTRE_LOD_NAME,
1110         .ldt_ops      = &lod_device_type_ops,
1111         .ldt_ctx_tags = LCT_MD_THREAD,
1112 };
1113
1114 /**
1115  * Implementation of obd_ops::o_get_info() for LOD
1116  *
1117  * Currently, there is only one supported key: KEY_OSP_CONNECTED , to provide
1118  * the caller binary status whether LOD has seen connection to any OST target.
1119  *
1120  * \param[in] env               LU environment provided by the caller
1121  * \param[in] exp               export of the caller
1122  * \param[in] keylen            len of the key
1123  * \param[in] key               the key
1124  * \param[in] vallen            not used
1125  * \param[in] val               not used
1126  * \param[in] lsm               not used
1127  *
1128  * \retval                      0 if a connection was seen
1129  * \retval                      -EAGAIN if LOD isn't running yet or no
1130  *                              connection has been seen yet
1131  * \retval                      -EINVAL if not supported key is requested
1132  **/
1133 static int lod_obd_get_info(const struct lu_env *env, struct obd_export *exp,
1134                             __u32 keylen, void *key, __u32 *vallen, void *val,
1135                             struct lov_stripe_md *lsm)
1136 {
1137         int rc = -EINVAL;
1138
1139         if (KEY_IS(KEY_OSP_CONNECTED)) {
1140                 struct obd_device       *obd = exp->exp_obd;
1141                 struct lod_device       *d;
1142                 struct lod_ost_desc     *ost;
1143                 unsigned int            i;
1144                 int                     rc = 1;
1145
1146                 if (!obd->obd_set_up || obd->obd_stopping)
1147                         RETURN(-EAGAIN);
1148
1149                 d = lu2lod_dev(obd->obd_lu_dev);
1150                 lod_getref(&d->lod_ost_descs);
1151                 lod_foreach_ost(d, i) {
1152                         ost = OST_TGT(d, i);
1153                         LASSERT(ost && ost->ltd_ost);
1154
1155                         rc = obd_get_info(env, ost->ltd_exp, keylen, key,
1156                                           vallen, val, lsm);
1157                         /* one healthy device is enough */
1158                         if (rc == 0)
1159                                 break;
1160                 }
1161                 lod_putref(d, &d->lod_ost_descs);
1162                 RETURN(rc);
1163         }
1164
1165         RETURN(rc);
1166 }
1167
1168 static struct obd_ops lod_obd_device_ops = {
1169         .o_owner        = THIS_MODULE,
1170         .o_connect      = lod_obd_connect,
1171         .o_disconnect   = lod_obd_disconnect,
1172         .o_get_info     = lod_obd_get_info,
1173         .o_pool_new     = lod_pool_new,
1174         .o_pool_rem     = lod_pool_remove,
1175         .o_pool_add     = lod_pool_add,
1176         .o_pool_del     = lod_pool_del,
1177 };
1178
1179 static int __init lod_mod_init(void)
1180 {
1181         struct obd_type *type;
1182         int rc;
1183
1184         rc = lu_kmem_init(lod_caches);
1185         if (rc)
1186                 return rc;
1187
1188         rc = class_register_type(&lod_obd_device_ops, NULL, true, NULL,
1189                                  LUSTRE_LOD_NAME, &lod_device_type);
1190         if (rc) {
1191                 lu_kmem_fini(lod_caches);
1192                 return rc;
1193         }
1194
1195         /* create "lov" entry in procfs for compatibility purposes */
1196         type = class_search_type(LUSTRE_LOV_NAME);
1197         if (type != NULL && type->typ_procroot != NULL)
1198                 return rc;
1199
1200         type = class_search_type(LUSTRE_LOD_NAME);
1201         type->typ_procsym = lprocfs_seq_register("lov", proc_lustre_root,
1202                                                  NULL, NULL);
1203         if (IS_ERR(type->typ_procsym)) {
1204                 CERROR("lod: can't create compat entry \"lov\": %d\n",
1205                        (int)PTR_ERR(type->typ_procsym));
1206                 type->typ_procsym = NULL;
1207         }
1208         return rc;
1209 }
1210
1211 static void __exit lod_mod_exit(void)
1212 {
1213         class_unregister_type(LUSTRE_LOD_NAME);
1214         lu_kmem_fini(lod_caches);
1215 }
1216
1217 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
1218 MODULE_DESCRIPTION("Lustre Logical Object Device ("LUSTRE_LOD_NAME")");
1219 MODULE_LICENSE("GPL");
1220
1221 module_init(lod_mod_init);
1222 module_exit(lod_mod_exit);
1223