Whamcloud - gitweb
LU-4413 ptlrpc: don't try to recover no_recov connection
[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)
150                 CWARN("%s: can't disconnect: rc = %d\n",
151                       d->lpd_obd->obd_name, rc);
152
153         ptlrpc_invalidate_import(imp);
154
155         RETURN(rc);
156 }
157
158 static int lwp_process_config(const struct lu_env *env,
159                               struct lu_device *dev, struct lustre_cfg *lcfg)
160 {
161         struct lwp_device               *d = lu2lwp_dev(dev);
162         int                              rc;
163         ENTRY;
164
165         switch (lcfg->lcfg_command) {
166         case LCFG_PRE_CLEANUP:
167         case LCFG_CLEANUP:
168                 rc = lwp_disconnect(d);
169                 break;
170         case LCFG_PARAM:
171                 rc = -ENOSYS;
172                 break;
173         default:
174                 CERROR("%s: unknown command %u\n",
175                        (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
176                 rc = 0;
177                 break;
178         }
179
180         RETURN(rc);
181 }
182
183 const struct lu_device_operations lwp_lu_ops = {
184         .ldo_process_config     = lwp_process_config,
185 };
186
187 static struct lprocfs_vars lprocfs_lwp_module_vars[] = {
188         { "num_refs",           lprocfs_rd_numrefs, 0, 0 },
189         { 0 }
190 };
191
192 static struct lprocfs_vars lprocfs_lwp_obd_vars[] = {
193         { 0 }
194 };
195
196 void lprocfs_lwp_init_vars(struct lprocfs_static_vars *lvars)
197 {
198         lvars->module_vars = lprocfs_lwp_module_vars;
199         lvars->obd_vars = lprocfs_lwp_obd_vars;
200 }
201
202 int lwp_init0(const struct lu_env *env, struct lwp_device *lwp,
203               struct lu_device_type *ldt, struct lustre_cfg *cfg)
204 {
205         struct lprocfs_static_vars lvars = { 0 };
206         int                        rc;
207         ENTRY;
208
209         lwp->lpd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
210         if (lwp->lpd_obd == NULL) {
211                 CERROR("Cannot find obd with name %s\n",
212                        lustre_cfg_string(cfg, 0));
213                 RETURN(-ENODEV);
214         }
215
216         lwp->lpd_dev.ld_ops = &lwp_lu_ops;
217         lwp->lpd_obd->obd_lu_dev = &lwp->lpd_dev;
218
219         rc = ptlrpcd_addref();
220         if (rc) {
221                 CERROR("%s: ptlrpcd addref error: rc =%d\n",
222                        lwp->lpd_obd->obd_name, rc);
223                 RETURN(rc);
224         }
225
226         rc = lwp_setup(env, lwp, lustre_cfg_string(cfg, 1));
227         if (rc) {
228                 CERROR("%s: setup lwp failed. %d\n",
229                        lwp->lpd_obd->obd_name, rc);
230                 ptlrpcd_decref();
231                 RETURN(rc);
232         }
233
234         lprocfs_lwp_init_vars(&lvars);
235         if (lprocfs_obd_setup(lwp->lpd_obd, lvars.obd_vars) == 0)
236                 ptlrpc_lprocfs_register_obd(lwp->lpd_obd);
237
238         RETURN(0);
239 }
240
241 static struct lu_device *lwp_device_free(const struct lu_env *env,
242                                          struct lu_device *lu)
243 {
244         struct lwp_device *m = lu2lwp_dev(lu);
245         ENTRY;
246
247         if (cfs_atomic_read(&lu->ld_ref) && lu->ld_site) {
248                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
249                 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
250         }
251         lu_device_fini(&m->lpd_dev);
252         OBD_FREE_PTR(m);
253         RETURN(NULL);
254 }
255
256 static struct lu_device *lwp_device_alloc(const struct lu_env *env,
257                                           struct lu_device_type *t,
258                                           struct lustre_cfg *lcfg)
259 {
260         struct lwp_device *lwp;
261         struct lu_device  *l;
262
263         OBD_ALLOC_PTR(lwp);
264         if (lwp == NULL) {
265                 l = ERR_PTR(-ENOMEM);
266         } else {
267                 int rc;
268
269                 l = lwp2lu_dev(lwp);
270                 lu_device_init(&lwp->lpd_dev, t);
271                 rc = lwp_init0(env, lwp, t, lcfg);
272                 if (rc != 0) {
273                         lwp_device_free(env, l);
274                         l = ERR_PTR(rc);
275                 }
276         }
277         return l;
278 }
279
280
281 static struct lu_device *lwp_device_fini(const struct lu_env *env,
282                                          struct lu_device *d)
283 {
284         struct lwp_device *m = lu2lwp_dev(d);
285         struct obd_import *imp;
286         int                rc;
287         ENTRY;
288
289         if (m->lpd_exp != NULL)
290                 class_disconnect(m->lpd_exp);
291
292         imp = m->lpd_obd->u.cli.cl_import;
293
294         if (imp->imp_rq_pool) {
295                 ptlrpc_free_rq_pool(imp->imp_rq_pool);
296                 imp->imp_rq_pool = NULL;
297         }
298
299         LASSERT(m->lpd_obd);
300         ptlrpc_lprocfs_unregister_obd(m->lpd_obd);
301         lprocfs_obd_cleanup(m->lpd_obd);
302
303         rc = client_obd_cleanup(m->lpd_obd);
304         LASSERTF(rc == 0, "error %d\n", rc);
305
306         ptlrpcd_decref();
307
308         RETURN(NULL);
309 }
310
311 static struct lu_device_type_operations lwp_device_type_ops = {
312         .ldto_device_alloc   = lwp_device_alloc,
313         .ldto_device_free    = lwp_device_free,
314         .ldto_device_fini    = lwp_device_fini
315 };
316
317 struct lu_device_type lwp_device_type = {
318         .ldt_tags     = LU_DEVICE_DT,
319         .ldt_name     = LUSTRE_LWP_NAME,
320         .ldt_ops      = &lwp_device_type_ops,
321         .ldt_ctx_tags = LCT_MD_THREAD
322 };
323
324 static int lwp_obd_connect(const struct lu_env *env, struct obd_export **exp,
325                            struct obd_device *obd, struct obd_uuid *cluuid,
326                            struct obd_connect_data *data, void *localdata)
327 {
328         struct lwp_device       *lwp = lu2lwp_dev(obd->obd_lu_dev);
329         struct client_obd       *cli = &lwp->lpd_obd->u.cli;
330         struct obd_import       *imp = cli->cl_import;
331         struct obd_connect_data *ocd;
332         struct lustre_handle     conn;
333         int                      rc;
334
335         ENTRY;
336
337         CDEBUG(D_CONFIG, "connect #%d\n", lwp->lpd_connects);
338
339         *exp = NULL;
340         down_write(&cli->cl_sem);
341         rc = class_connect(&conn, obd, cluuid);
342         if (rc != 0)
343                 GOTO(out_sem, rc);
344
345         *exp = class_conn2export(&conn);
346         lwp->lpd_exp = *exp;
347
348         /* Why should there ever be more than 1 connect? */
349         lwp->lpd_connects++;
350         LASSERT(lwp->lpd_connects == 1);
351
352         imp->imp_dlm_handle = conn;
353         rc = ptlrpc_init_import(imp);
354         if (rc != 0)
355                 GOTO(out_dis, rc);
356
357         LASSERT(data != NULL);
358         ocd = &imp->imp_connect_data;
359         *ocd = *data;
360
361         LASSERT(ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT);
362
363         ocd->ocd_version = LUSTRE_VERSION_CODE;
364         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
365
366         rc = ptlrpc_connect_import(imp);
367         if (rc != 0) {
368                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
369                 GOTO(out_dis, rc);
370         }
371
372         ptlrpc_pinger_add_import(imp);
373
374         GOTO(out_dis, rc = 0);
375
376 out_dis:
377         if (rc != 0) {
378                 class_disconnect(*exp);
379                 *exp = NULL;
380                 lwp->lpd_exp = NULL;
381         }
382
383 out_sem:
384         up_write(&cli->cl_sem);
385
386         return rc;
387 }
388
389 static int lwp_obd_disconnect(struct obd_export *exp)
390 {
391         struct obd_device *obd = exp->exp_obd;
392         struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
393         int                rc;
394         ENTRY;
395
396         /* Only disconnect the underlying layers on the final disconnect. */
397         LASSERT(lwp->lpd_connects == 1);
398         lwp->lpd_connects--;
399
400         rc = class_disconnect(exp);
401         if (rc)
402                 CERROR("%s: class disconnect error: rc = %d\n",
403                        obd->obd_name, rc);
404
405         RETURN(rc);
406 }
407
408 static int lwp_import_event(struct obd_device *obd, struct obd_import *imp,
409                             enum obd_import_event event)
410 {
411         switch (event) {
412         case IMP_EVENT_DISCON:
413         case IMP_EVENT_INACTIVE:
414         case IMP_EVENT_ACTIVE:
415                 break;
416         case IMP_EVENT_INVALIDATE:
417                 if (obd->obd_namespace == NULL)
418                         break;
419                 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
420                 break;
421         case IMP_EVENT_OCD:
422                 break;
423         default:
424                 CERROR("%s: unsupported import event: %#x\n",
425                        obd->obd_name, event);
426         }
427         return 0;
428 }
429
430 struct obd_ops lwp_obd_device_ops = {
431         .o_owner        = THIS_MODULE,
432         .o_add_conn     = client_import_add_conn,
433         .o_del_conn     = client_import_del_conn,
434         .o_connect      = lwp_obd_connect,
435         .o_disconnect   = lwp_obd_disconnect,
436         .o_import_event = lwp_import_event,
437 };