Whamcloud - gitweb
LU-1187 lod: Fix config log and setup process for DNE
[fs/lustre-release.git] / lustre / osp / osp_ost.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, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 021110-1307, USA
20  *
21  * GPL HEADER END
22  */
23 /*
24  * Copyright (c) 2012, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * lustre/osp/osp_ost.c
28  *
29  * OSP on OST for communicating with MDT0
30  *
31  * Author: <di.wang@whamcloud.com>
32  */
33 #ifndef EXPORT_SYMTAB
34 # define EXPORT_SYMTAB
35 #endif
36 #define DEBUG_SUBSYSTEM S_OST
37
38 #include <obd_class.h>
39 #include <lustre_param.h>
40 #include <lustre_log.h>
41
42 #include "osp_internal.h"
43
44 static int osp_name2fsname(char *ospname, char *fsname)
45 {
46         char *ptr;
47
48         LASSERT(ospname != NULL);
49         LASSERT(fsname != NULL);
50         if (!is_osp_for_connection(ospname))
51                 return -EINVAL;
52
53         sprintf(fsname, "-%s-", LUSTRE_OSP_NAME);
54
55         ptr = strstr(ospname, fsname);
56         if (ptr == NULL)
57                 return -EINVAL;
58
59         while (*(--ptr) != '-') {
60                 if (ptr == ospname)
61                         return -EINVAL;
62         }
63
64         strncpy(fsname, ospname, ptr - ospname);
65         fsname[ptr - ospname] = '\0';
66
67         return 0;
68 }
69
70 static int osp_setup_for_ost(const struct lu_env *env, struct osp_device *osp,
71                              char *nidstring)
72 {
73         struct lustre_cfg_bufs  *bufs = NULL;
74         struct lustre_cfg       *lcfg = NULL;
75         char                    *ospname = osp->opd_obd->obd_name;
76         char                    *fsname = NULL;
77         char                    *server_uuid = NULL;
78         class_uuid_t             uuid;
79         struct obd_import       *imp;
80         int                      rc;
81         ENTRY;
82
83         OBD_ALLOC_PTR(bufs);
84         if (bufs == NULL)
85                 RETURN(-ENOMEM);
86
87         OBD_ALLOC(fsname, strlen(ospname));
88         if (fsname == NULL)
89                 GOTO(out, rc = -ENOMEM);
90
91         rc = osp_name2fsname(ospname, fsname);
92         if (rc) {
93                 CERROR("%s: name change error: rc %d\n", ospname, rc);
94                 GOTO(out, rc);
95         }
96
97         OBD_ALLOC(server_uuid, strlen(fsname) + 15);
98         if (server_uuid == NULL)
99                 GOTO(out, rc = -ENOMEM);
100
101         sprintf(server_uuid, "%s-MDT0000_UUID", fsname);
102         lustre_cfg_bufs_reset(bufs, ospname);
103         lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
104         lustre_cfg_bufs_set_string(bufs, 2, nidstring);
105         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
106         if (lcfg == NULL)
107                 GOTO(out, rc = -ENOMEM);
108
109         osp->opd_connect_mdt = 1;
110         rc = client_obd_setup(osp->opd_obd, lcfg);
111         if (rc != 0) {
112                 CERROR("%s: client obd setup error: rc = %d\n",
113                        osp->opd_obd->obd_name, rc);
114                 GOTO(out, rc);
115         }
116
117         imp = osp->opd_obd->u.cli.cl_import;
118         rc = ptlrpc_init_import(imp);
119         if (rc)
120                 GOTO(out, rc);
121
122         ll_generate_random_uuid(uuid);
123         class_uuid_unparse(uuid, &osp->opd_cluuid);
124 out:
125         if (bufs != NULL)
126                 OBD_FREE_PTR(bufs);
127         if (server_uuid != NULL)
128                 OBD_FREE(server_uuid, strlen(fsname) + 15);
129         if (fsname != NULL)
130                 OBD_FREE(fsname, strlen(ospname));
131         if (lcfg != NULL)
132                 lustre_cfg_free(lcfg);
133         if (rc)
134                 client_obd_cleanup(osp->opd_obd);
135         RETURN(rc);
136 }
137
138 int osp_fini_for_ost(struct osp_device *osp)
139 {
140         if (osp->opd_exp != NULL)
141                 class_disconnect(osp->opd_exp);
142         return 0;
143 }
144
145 int osp_init_for_ost(const struct lu_env *env, struct osp_device *osp,
146                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
147 {
148         struct lprocfs_static_vars lvars = { 0 };
149         int                        rc;
150         ENTRY;
151
152         osp->opd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
153         if (osp->opd_obd == NULL) {
154                 CERROR("Cannot find obd with name %s\n",
155                        lustre_cfg_string(cfg, 0));
156                 RETURN(-ENODEV);
157         }
158
159         osp->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
160         osp->opd_dt_dev.dd_ops = &osp_dt_ops;
161         osp->opd_obd->obd_lu_dev = &osp->opd_dt_dev.dd_lu_dev;
162
163         rc = ptlrpcd_addref();
164         if (rc) {
165                 CERROR("%s: ptlrpcd addref error: rc =%d\n",
166                        osp->opd_obd->obd_name, rc);
167                 RETURN(rc);
168         }
169
170         rc = osp_setup_for_ost(env, osp, lustre_cfg_string(cfg, 1));
171         if (rc) {
172                 CERROR("%s: osp_setup_for_ost error: rc =%d\n",
173                        osp->opd_obd->obd_name, rc);
174                 ptlrpcd_decref();
175                 RETURN(rc);
176         }
177
178         lprocfs_osp_init_vars(&lvars);
179         if (lprocfs_obd_setup(osp->opd_obd, lvars.obd_vars) == 0)
180                 ptlrpc_lprocfs_register_obd(osp->opd_obd);
181
182         RETURN(0);
183 }