Whamcloud - gitweb
LU-1187 tests: Add DNE test cases in sanity.
[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 == 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         rc = client_obd_setup(osp->opd_obd, lcfg);
110         if (rc != 0) {
111                 CERROR("%s: client obd setup error: rc = %d\n",
112                        osp->opd_obd->obd_name, rc);
113                 GOTO(out, rc);
114         }
115
116         imp = osp->opd_obd->u.cli.cl_import;
117         rc = ptlrpc_init_import(imp);
118         if (rc)
119                 GOTO(out, rc);
120
121         ll_generate_random_uuid(uuid);
122         class_uuid_unparse(uuid, &osp->opd_cluuid);
123 out:
124         if (bufs != NULL)
125                 OBD_FREE_PTR(bufs);
126         if (server_uuid != NULL)
127                 OBD_FREE(server_uuid, strlen(fsname) + 15);
128         if (fsname != NULL)
129                 OBD_FREE(fsname, strlen(ospname));
130         if (lcfg != NULL)
131                 lustre_cfg_free(lcfg);
132         if (rc)
133                 client_obd_cleanup(osp->opd_obd);
134         RETURN(rc);
135 }
136
137 int osp_fini_for_ost(struct osp_device *osp)
138 {
139         if (osp->opd_exp != NULL)
140                 class_disconnect(osp->opd_exp);
141         return 0;
142 }
143
144 int osp_init_for_ost(const struct lu_env *env, struct osp_device *osp,
145                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
146 {
147         struct lprocfs_static_vars lvars = { 0 };
148         int                        rc;
149         ENTRY;
150
151         osp->opd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
152         if (osp->opd_obd == NULL) {
153                 CERROR("Cannot find obd with name %s\n",
154                        lustre_cfg_string(cfg, 0));
155                 RETURN(-ENODEV);
156         }
157
158         osp->opd_dt_dev.dd_lu_dev.ld_ops = &osp_lu_ops;
159         osp->opd_dt_dev.dd_ops = &osp_dt_ops;
160         osp->opd_obd->obd_lu_dev = &osp->opd_dt_dev.dd_lu_dev;
161
162         rc = ptlrpcd_addref();
163         if (rc) {
164                 CERROR("%s: ptlrpcd addref error: rc =%d\n",
165                        osp->opd_obd->obd_name, rc);
166                 RETURN(rc);
167         }
168
169         rc = osp_setup_for_ost(env, osp, lustre_cfg_string(cfg, 1));
170         if (rc) {
171                 CERROR("%s: osp_setup_for_ost error: rc =%d\n",
172                        osp->opd_obd->obd_name, rc);
173                 ptlrpcd_decref();
174                 RETURN(rc);
175         }
176
177         lprocfs_osp_init_vars(&lvars);
178         if (lprocfs_obd_setup(osp->opd_obd, lvars.obd_vars) == 0)
179                 ptlrpc_lprocfs_register_obd(osp->opd_obd);
180
181         RETURN(0);
182 }