Whamcloud - gitweb
LU-3951 lfsck: LWP connection from OST-x to MDT-y
[fs/lustre-release.git] / lustre / osp / lwp_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, 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) 2013, Intel Corporation.
25  * Use is subject to license terms.
26  *
27  * lustre/osp/lwp_dev.c
28  *
29  * Light Weight Proxy, which is just for managing the connection established
30  * from OSTs/MDTs to MDT0.
31  *
32  * Author: <di.wang@intel.com>
33  * Author: <yawei.niu@intel.com>
34  */
35 #define DEBUG_SUBSYSTEM S_OST
36
37 #include <obd_class.h>
38 #include <lustre_param.h>
39 #include <lustre_log.h>
40 #include <libcfs/libcfs_string.h>
41
42 struct lwp_device {
43         struct lu_device        lpd_dev;
44         struct obd_device       *lpd_obd;
45         struct obd_uuid         lpd_cluuid;
46         struct obd_export       *lpd_exp;
47         int                     lpd_connects;
48 };
49
50 static inline struct lwp_device *lu2lwp_dev(struct lu_device *d)
51 {
52         return container_of0(d, struct lwp_device, lpd_dev);
53 }
54
55 static inline struct lu_device *lwp2lu_dev(struct lwp_device *d)
56 {
57         return &d->lpd_dev;
58 }
59
60 static int lwp_setup(const struct lu_env *env, struct lwp_device *lwp,
61                      char *nidstring)
62 {
63         struct lustre_cfg_bufs  *bufs = NULL;
64         struct lustre_cfg       *lcfg = NULL;
65         char                    *lwp_name = lwp->lpd_obd->obd_name;
66         char                    *server_uuid = NULL;
67         char                    *ptr;
68         class_uuid_t             uuid;
69         struct obd_import       *imp;
70         int                      len = strlen(lwp_name);
71         int                      rc;
72         ENTRY;
73
74         OBD_ALLOC_PTR(bufs);
75         if (bufs == NULL)
76                 RETURN(-ENOMEM);
77
78         OBD_ALLOC(server_uuid, len);
79         if (server_uuid == NULL)
80                 GOTO(out, rc = -ENOMEM);
81
82         snprintf(server_uuid, len, "-%s-", LUSTRE_LWP_NAME);
83         ptr = cfs_strrstr(lwp_name, server_uuid);
84         if (ptr == NULL) {
85                 CERROR("%s: failed to get server_uuid from lwp_name: rc = %d\n",
86                        lwp_name, -EINVAL);
87                 GOTO(out, rc = -EINVAL);
88         }
89
90         strncpy(server_uuid, lwp_name, ptr - lwp_name);
91         server_uuid[ptr - lwp_name] = '\0';
92         strncat(server_uuid, "_UUID", len - 1);
93         lustre_cfg_bufs_reset(bufs, lwp_name);
94         lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
95         lustre_cfg_bufs_set_string(bufs, 2, nidstring);
96         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
97         if (lcfg == NULL)
98                 GOTO(out, rc = -ENOMEM);
99
100         rc = client_obd_setup(lwp->lpd_obd, lcfg);
101         if (rc != 0) {
102                 CERROR("%s: client obd setup error: rc = %d\n",
103                        lwp->lpd_obd->obd_name, rc);
104                 GOTO(out, rc);
105         }
106
107         imp = lwp->lpd_obd->u.cli.cl_import;
108         rc = ptlrpc_init_import(imp);
109         if (rc)
110                 GOTO(out, rc);
111
112         ll_generate_random_uuid(uuid);
113         class_uuid_unparse(uuid, &lwp->lpd_cluuid);
114 out:
115         if (bufs != NULL)
116                 OBD_FREE_PTR(bufs);
117         if (server_uuid != NULL)
118                 OBD_FREE(server_uuid, len);
119         if (lcfg != NULL)
120                 lustre_cfg_free(lcfg);
121         if (rc)
122                 client_obd_cleanup(lwp->lpd_obd);
123
124         RETURN(rc);
125 }
126
127 static int lwp_disconnect(struct lwp_device *d)
128 {
129         struct obd_import *imp;
130         int rc = 0;
131
132         imp = d->lpd_obd->u.cli.cl_import;
133
134         /* Mark import deactivated now, so we don't try to reconnect if any
135          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
136          * fully deactivate the import, or that would drop all requests. */
137         LASSERT(imp != NULL);
138         spin_lock(&imp->imp_lock);
139         imp->imp_deactive = 1;
140         spin_unlock(&imp->imp_lock);
141
142         ptlrpc_deactivate_import(imp);
143
144         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
145          * delete it regardless.  (It's safe to delete an import that was
146          * never added.) */
147         ptlrpc_pinger_del_import(imp);
148         rc = ptlrpc_disconnect_import(imp, 0);
149         if (rc != 0 && rc != -ETIMEDOUT && rc != -ENOTCONN && rc != -ESHUTDOWN)
150                 CWARN("%s: can't disconnect: rc = %d\n",
151                       d->lpd_obd->obd_name, rc);
152         else
153                 rc = 0;
154
155         ptlrpc_invalidate_import(imp);
156
157         RETURN(rc);
158 }
159
160 static int lwp_process_config(const struct lu_env *env,
161                               struct lu_device *dev, struct lustre_cfg *lcfg)
162 {
163         struct lwp_device               *d = lu2lwp_dev(dev);
164         int                              rc;
165         ENTRY;
166
167         switch (lcfg->lcfg_command) {
168         case LCFG_PRE_CLEANUP:
169         case LCFG_CLEANUP:
170                 rc = lwp_disconnect(d);
171                 break;
172         case LCFG_PARAM:
173                 rc = -ENOSYS;
174                 break;
175         default:
176                 CERROR("%s: unknown command %u\n",
177                        (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
178                 rc = 0;
179                 break;
180         }
181
182         RETURN(rc);
183 }
184
185 const struct lu_device_operations lwp_lu_ops = {
186         .ldo_process_config     = lwp_process_config,
187 };
188
189 static struct lprocfs_vars lprocfs_lwp_module_vars[] = {
190         { "num_refs",           lprocfs_rd_numrefs, 0, 0 },
191         { 0 }
192 };
193
194 static struct lprocfs_vars lprocfs_lwp_obd_vars[] = {
195         { 0 }
196 };
197
198 void lprocfs_lwp_init_vars(struct lprocfs_static_vars *lvars)
199 {
200         lvars->module_vars = lprocfs_lwp_module_vars;
201         lvars->obd_vars = lprocfs_lwp_obd_vars;
202 }
203
204 int lwp_init0(const struct lu_env *env, struct lwp_device *lwp,
205               struct lu_device_type *ldt, struct lustre_cfg *cfg)
206 {
207         struct lprocfs_static_vars lvars = { 0 };
208         int                        rc;
209         ENTRY;
210
211         lwp->lpd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
212         if (lwp->lpd_obd == NULL) {
213                 CERROR("Cannot find obd with name %s\n",
214                        lustre_cfg_string(cfg, 0));
215                 RETURN(-ENODEV);
216         }
217
218         lwp->lpd_dev.ld_ops = &lwp_lu_ops;
219         lwp->lpd_obd->obd_lu_dev = &lwp->lpd_dev;
220
221         rc = ptlrpcd_addref();
222         if (rc) {
223                 CERROR("%s: ptlrpcd addref error: rc =%d\n",
224                        lwp->lpd_obd->obd_name, rc);
225                 RETURN(rc);
226         }
227
228         rc = lwp_setup(env, lwp, lustre_cfg_string(cfg, 1));
229         if (rc) {
230                 CERROR("%s: setup lwp failed. %d\n",
231                        lwp->lpd_obd->obd_name, rc);
232                 ptlrpcd_decref();
233                 RETURN(rc);
234         }
235
236         lprocfs_lwp_init_vars(&lvars);
237         if (lprocfs_obd_setup(lwp->lpd_obd, lvars.obd_vars) == 0)
238                 ptlrpc_lprocfs_register_obd(lwp->lpd_obd);
239
240         RETURN(0);
241 }
242
243 static struct lu_device *lwp_device_free(const struct lu_env *env,
244                                          struct lu_device *lu)
245 {
246         struct lwp_device *m = lu2lwp_dev(lu);
247         ENTRY;
248
249         if (cfs_atomic_read(&lu->ld_ref) && lu->ld_site) {
250                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
251                 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
252         }
253         lu_device_fini(&m->lpd_dev);
254         OBD_FREE_PTR(m);
255         RETURN(NULL);
256 }
257
258 static struct lu_device *lwp_device_alloc(const struct lu_env *env,
259                                           struct lu_device_type *t,
260                                           struct lustre_cfg *lcfg)
261 {
262         struct lwp_device *lwp;
263         struct lu_device  *l;
264
265         OBD_ALLOC_PTR(lwp);
266         if (lwp == NULL) {
267                 l = ERR_PTR(-ENOMEM);
268         } else {
269                 int rc;
270
271                 l = lwp2lu_dev(lwp);
272                 lu_device_init(&lwp->lpd_dev, t);
273                 rc = lwp_init0(env, lwp, t, lcfg);
274                 if (rc != 0) {
275                         lwp_device_free(env, l);
276                         l = ERR_PTR(rc);
277                 }
278         }
279         return l;
280 }
281
282
283 static struct lu_device *lwp_device_fini(const struct lu_env *env,
284                                          struct lu_device *d)
285 {
286         struct lwp_device *m = lu2lwp_dev(d);
287         struct obd_import *imp;
288         int                rc;
289         ENTRY;
290
291         if (m->lpd_exp != NULL)
292                 class_disconnect(m->lpd_exp);
293
294         imp = m->lpd_obd->u.cli.cl_import;
295
296         if (imp->imp_rq_pool) {
297                 ptlrpc_free_rq_pool(imp->imp_rq_pool);
298                 imp->imp_rq_pool = NULL;
299         }
300
301         LASSERT(m->lpd_obd);
302         ptlrpc_lprocfs_unregister_obd(m->lpd_obd);
303         lprocfs_obd_cleanup(m->lpd_obd);
304
305         rc = client_obd_cleanup(m->lpd_obd);
306         LASSERTF(rc == 0, "error %d\n", rc);
307
308         ptlrpcd_decref();
309
310         RETURN(NULL);
311 }
312
313 static struct lu_device_type_operations lwp_device_type_ops = {
314         .ldto_device_alloc   = lwp_device_alloc,
315         .ldto_device_free    = lwp_device_free,
316         .ldto_device_fini    = lwp_device_fini
317 };
318
319 struct lu_device_type lwp_device_type = {
320         .ldt_tags     = LU_DEVICE_DT,
321         .ldt_name     = LUSTRE_LWP_NAME,
322         .ldt_ops      = &lwp_device_type_ops,
323         .ldt_ctx_tags = LCT_MD_THREAD
324 };
325
326 static int lwp_obd_connect(const struct lu_env *env, struct obd_export **exp,
327                            struct obd_device *obd, struct obd_uuid *cluuid,
328                            struct obd_connect_data *data, void *localdata)
329 {
330         struct lwp_device       *lwp = lu2lwp_dev(obd->obd_lu_dev);
331         struct client_obd       *cli = &lwp->lpd_obd->u.cli;
332         struct obd_import       *imp = cli->cl_import;
333         struct obd_connect_data *ocd;
334         struct lustre_handle     conn;
335         int                      rc;
336
337         ENTRY;
338
339         CDEBUG(D_CONFIG, "connect #%d\n", lwp->lpd_connects);
340
341         *exp = NULL;
342         down_write(&cli->cl_sem);
343         rc = class_connect(&conn, obd, cluuid);
344         if (rc != 0)
345                 GOTO(out_sem, rc);
346
347         *exp = class_conn2export(&conn);
348         lwp->lpd_exp = *exp;
349
350         /* Why should there ever be more than 1 connect? */
351         lwp->lpd_connects++;
352         LASSERT(lwp->lpd_connects == 1);
353
354         imp->imp_dlm_handle = conn;
355         rc = ptlrpc_init_import(imp);
356         if (rc != 0)
357                 GOTO(out_dis, rc);
358
359         LASSERT(data != NULL);
360         ocd = &imp->imp_connect_data;
361         *ocd = *data;
362
363         LASSERT(ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT);
364
365         ocd->ocd_version = LUSTRE_VERSION_CODE;
366         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
367
368         rc = ptlrpc_connect_import(imp);
369         if (rc != 0) {
370                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
371                 GOTO(out_dis, rc);
372         }
373
374         ptlrpc_pinger_add_import(imp);
375
376         GOTO(out_dis, rc = 0);
377
378 out_dis:
379         if (rc != 0) {
380                 class_disconnect(*exp);
381                 *exp = NULL;
382                 lwp->lpd_exp = NULL;
383         }
384
385 out_sem:
386         up_write(&cli->cl_sem);
387
388         return rc;
389 }
390
391 static int lwp_obd_disconnect(struct obd_export *exp)
392 {
393         struct obd_device *obd = exp->exp_obd;
394         struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
395         int                rc;
396         ENTRY;
397
398         /* Only disconnect the underlying layers on the final disconnect. */
399         LASSERT(lwp->lpd_connects == 1);
400         lwp->lpd_connects--;
401
402         rc = class_disconnect(exp);
403         if (rc)
404                 CERROR("%s: class disconnect error: rc = %d\n",
405                        obd->obd_name, rc);
406
407         RETURN(rc);
408 }
409
410 static int lwp_import_event(struct obd_device *obd, struct obd_import *imp,
411                             enum obd_import_event event)
412 {
413         switch (event) {
414         case IMP_EVENT_DISCON:
415         case IMP_EVENT_INACTIVE:
416         case IMP_EVENT_ACTIVE:
417                 break;
418         case IMP_EVENT_INVALIDATE:
419                 if (obd->obd_namespace == NULL)
420                         break;
421                 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
422                 break;
423         case IMP_EVENT_OCD:
424                 break;
425         default:
426                 CERROR("%s: unsupported import event: %#x\n",
427                        obd->obd_name, event);
428         }
429         return 0;
430 }
431
432 struct obd_ops lwp_obd_device_ops = {
433         .o_owner        = THIS_MODULE,
434         .o_add_conn     = client_import_add_conn,
435         .o_del_conn     = client_import_del_conn,
436         .o_connect      = lwp_obd_connect,
437         .o_disconnect   = lwp_obd_disconnect,
438         .o_import_event = lwp_import_event,
439 };