Whamcloud - gitweb
LU-1842 osp: osp-on-ost device
[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, Inc.
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_on_ost(ospname))
51                 return -EINVAL;
52
53         sprintf(fsname, "-%s-", LUSTRE_OSP_NAME);
54
55         ptr = strstr(ospname, fsname);
56         if (ptr) {
57                 strncpy(fsname, ospname, ptr - ospname);
58                 fsname[ptr - ospname] = '\0';
59         }
60         return 0;
61 }
62
63 static int osp_setup_for_ost(const struct lu_env *env, struct osp_device *osp,
64                              char *nidstring)
65 {
66         struct lustre_cfg_bufs  *bufs = NULL;
67         struct lustre_cfg       *lcfg = NULL;
68         char                    *ospname = osp->opd_obd->obd_name;
69         char                    *fsname = NULL;
70         char                    *server_uuid = NULL;
71         class_uuid_t             uuid;
72         struct obd_import       *imp;
73         int                      rc;
74         ENTRY;
75
76         OBD_ALLOC_PTR(bufs);
77         if (bufs == NULL)
78                 RETURN(-ENOMEM);
79
80         OBD_ALLOC(fsname, strlen(ospname));
81         if (fsname == NULL)
82                 GOTO(out, rc = -ENOMEM);
83
84         rc = osp_name2fsname(ospname, fsname);
85         if (rc) {
86                 CERROR("%s: name change error: rc %d\n", ospname, rc);
87                 GOTO(out, rc);
88         }
89
90         OBD_ALLOC(server_uuid, strlen(fsname) + 15);
91         if (server_uuid == NULL)
92                 GOTO(out, rc = -ENOMEM);
93
94         sprintf(server_uuid, "%s-MDT0000_UUID", fsname);
95         lustre_cfg_bufs_reset(bufs, ospname);
96         lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
97         lustre_cfg_bufs_set_string(bufs, 2, nidstring);
98         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
99         if (lcfg == NULL)
100                 GOTO(out, rc = -ENOMEM);
101
102         rc = client_obd_setup(osp->opd_obd, lcfg);
103         if (rc != 0) {
104                 CERROR("%s: client obd setup error: rc = %d\n",
105                        osp->opd_obd->obd_name, rc);
106                 GOTO(out, rc);
107         }
108
109         imp = osp->opd_obd->u.cli.cl_import;
110         rc = ptlrpc_init_import(imp);
111         if (rc)
112                 GOTO(out, rc);
113
114         ll_generate_random_uuid(uuid);
115         class_uuid_unparse(uuid, &osp->opd_cluuid);
116 out:
117         if (bufs != NULL)
118                 OBD_FREE_PTR(bufs);
119         if (server_uuid != NULL)
120                 OBD_FREE(server_uuid, strlen(fsname) + 15);
121         if (fsname != NULL)
122                 OBD_FREE(fsname, strlen(ospname));
123         if (lcfg != NULL)
124                 lustre_cfg_free(lcfg);
125         if (rc)
126                 client_obd_cleanup(osp->opd_obd);
127         RETURN(rc);
128 }
129
130 int osp_fini_for_ost(struct osp_device *osp)
131 {
132         if (osp->opd_exp != NULL)
133                 class_disconnect(osp->opd_exp);
134         return 0;
135 }
136
137 int osp_init_for_ost(const struct lu_env *env, struct osp_device *osp,
138                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
139 {
140         struct lprocfs_static_vars lvars = { 0 };
141         int                        rc;
142         ENTRY;
143
144         osp->opd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
145         if (osp->opd_obd == NULL) {
146                 CERROR("Cannot find obd with name %s\n",
147                        lustre_cfg_string(cfg, 0));
148                 RETURN(-ENODEV);
149         }
150
151         osp->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
152         osp->opd_dt_dev.dd_ops = &osp_dt_ops;
153         osp->opd_obd->obd_lu_dev = &osp->opd_dt_dev.dd_lu_dev;
154
155         rc = ptlrpcd_addref();
156         if (rc) {
157                 CERROR("%s: ptlrpcd addref error: rc =%d\n",
158                        osp->opd_obd->obd_name, rc);
159                 RETURN(rc);
160         }
161
162         rc = osp_setup_for_ost(env, osp, lustre_cfg_string(cfg, 1));
163         if (rc) {
164                 CERROR("%s: osp_setup_for_ost error: rc =%d\n",
165                        osp->opd_obd->obd_name, rc);
166                 ptlrpcd_decref();
167                 RETURN(rc);
168         }
169
170         lprocfs_osp_init_vars(&lvars);
171         if (lprocfs_obd_setup(osp->opd_obd, lvars.obd_vars) == 0)
172                 ptlrpc_lprocfs_register_obd(osp->opd_obd);
173
174         RETURN(0);
175 }