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