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