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