Whamcloud - gitweb
cb22b2f844ff222d0729afe0202ffc77a9d40086
[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  * This file provides code related to the Light Weight Proxy (LWP) managing
30  * the connections established from OST to MDT, and MDT to MDT0.
31  *
32  * A LWP connection is used to send quota and FLD query requests. It's not
33  * recoverable, which means target server doesn't have an on-disk record in
34  * the last_rcvd file to remember the connection. Once LWP reconnect after
35  * server reboot, server will always regard it as a new connection.
36  *
37  * Author: <di.wang@intel.com>
38  * Author: <yawei.niu@intel.com>
39  */
40 #define DEBUG_SUBSYSTEM S_OST
41
42 #include <obd_class.h>
43 #include <lustre_param.h>
44 #include <lustre_log.h>
45 #include <libcfs/libcfs_string.h>
46
47 struct lwp_device {
48         struct lu_device        lpd_dev;
49         struct obd_device      *lpd_obd;   /* corresponding OBD device */
50         struct obd_uuid         lpd_cluuid;/* UUID of LWP */
51         struct obd_export      *lpd_exp;   /* export of LWP */
52         int                     lpd_connects; /* use count, 0 or 1 */
53 };
54
55 static inline struct lwp_device *lu2lwp_dev(struct lu_device *d)
56 {
57         return container_of0(d, struct lwp_device, lpd_dev);
58 }
59
60 static inline struct lu_device *lwp2lu_dev(struct lwp_device *d)
61 {
62         return &d->lpd_dev;
63 }
64
65 /**
66  * Setup LWP device.
67  *
68  * \param[in] env       environment passed by caller
69  * \param[in] lwp       LWP device to be setup
70  * \param[in] nidstring remote target NID
71  *
72  * \retval              0 on success
73  * \retval              negative number on error
74  */
75 static int lwp_setup(const struct lu_env *env, struct lwp_device *lwp,
76                      char *nidstring)
77 {
78         struct lustre_cfg_bufs  *bufs = NULL;
79         struct lustre_cfg       *lcfg = NULL;
80         char                    *lwp_name = lwp->lpd_obd->obd_name;
81         char                    *server_uuid = NULL;
82         char                    *ptr;
83         class_uuid_t             uuid;
84         struct obd_import       *imp;
85         int                      len = strlen(lwp_name) + 1;
86         int                      rc;
87         ENTRY;
88
89         OBD_ALLOC_PTR(bufs);
90         if (bufs == NULL)
91                 RETURN(-ENOMEM);
92
93         OBD_ALLOC(server_uuid, len);
94         if (server_uuid == NULL)
95                 GOTO(out, rc = -ENOMEM);
96
97         snprintf(server_uuid, len, "-%s-", LUSTRE_LWP_NAME);
98         ptr = cfs_strrstr(lwp_name, server_uuid);
99         if (ptr == NULL) {
100                 CERROR("%s: failed to get server_uuid from lwp_name: rc = %d\n",
101                        lwp_name, -EINVAL);
102                 GOTO(out, rc = -EINVAL);
103         }
104
105         strncpy(server_uuid, lwp_name, ptr - lwp_name);
106         server_uuid[ptr - lwp_name] = '\0';
107         strlcat(server_uuid, "_UUID", len);
108         lustre_cfg_bufs_reset(bufs, lwp_name);
109         lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
110         lustre_cfg_bufs_set_string(bufs, 2, nidstring);
111         lcfg = lustre_cfg_new(LCFG_SETUP, bufs);
112         if (lcfg == NULL)
113                 GOTO(out, rc = -ENOMEM);
114
115         rc = client_obd_setup(lwp->lpd_obd, lcfg);
116         if (rc != 0) {
117                 CERROR("%s: client obd setup error: rc = %d\n",
118                        lwp->lpd_obd->obd_name, rc);
119                 GOTO(out, rc);
120         }
121
122         imp = lwp->lpd_obd->u.cli.cl_import;
123         rc = ptlrpc_init_import(imp);
124         if (rc)
125                 GOTO(out, rc);
126
127         ll_generate_random_uuid(uuid);
128         class_uuid_unparse(uuid, &lwp->lpd_cluuid);
129 out:
130         if (bufs != NULL)
131                 OBD_FREE_PTR(bufs);
132         if (server_uuid != NULL)
133                 OBD_FREE(server_uuid, len);
134         if (lcfg != NULL)
135                 lustre_cfg_free(lcfg);
136         if (rc)
137                 client_obd_cleanup(lwp->lpd_obd);
138
139         RETURN(rc);
140 }
141
142 /**
143  * Disconnect the import from LWP.
144  *
145  * \param[in] d         LWP device to be disconnected
146  *
147  * \retval              0 on success
148  * \retval              negative number on error
149  */
150 static int lwp_disconnect(struct lwp_device *d)
151 {
152         struct obd_import *imp;
153         int rc = 0;
154
155         imp = d->lpd_obd->u.cli.cl_import;
156
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 because that would cause all requests
161          * to be dropped.
162          */
163         LASSERT(imp != NULL);
164         spin_lock(&imp->imp_lock);
165         imp->imp_deactive = 1;
166         spin_unlock(&imp->imp_lock);
167
168         ptlrpc_deactivate_import(imp);
169
170         /*
171          * Some non-replayable imports (MDS's OSCs) are pinged, so just
172          * delete it regardless.  (It's safe to delete an import that was
173          * never added.)
174          */
175         ptlrpc_pinger_del_import(imp);
176         rc = ptlrpc_disconnect_import(imp, 0);
177         if (rc != 0)
178                 CWARN("%s: can't disconnect: rc = %d\n",
179                       d->lpd_obd->obd_name, rc);
180
181         ptlrpc_invalidate_import(imp);
182
183         RETURN(rc);
184 }
185
186 /**
187  * Implementation of lu_device_operations::ldo_process_config.
188  *
189  * Process a Lustre configuration request.
190  *
191  * \param[in] env       environment passed by caller
192  * \param[in] dev       device to be processed
193  * \param[in] lcfg      lustre_cfg, LCFG_PRE_CLEANUP or LCFG_CLEANUP
194  *
195  * \retval              0 on success
196  * \retval              negative number on error
197  */
198 static int lwp_process_config(const struct lu_env *env,
199                               struct lu_device *dev, struct lustre_cfg *lcfg)
200 {
201         struct lwp_device               *d = lu2lwp_dev(dev);
202         int                              rc;
203         ENTRY;
204
205         switch (lcfg->lcfg_command) {
206         case LCFG_PRE_CLEANUP:
207         case LCFG_CLEANUP:
208                 rc = lwp_disconnect(d);
209                 break;
210         case LCFG_PARAM:
211                 rc = -ENOSYS;
212                 break;
213         default:
214                 CERROR("%s: unknown command %u\n",
215                        (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
216                 rc = 0;
217                 break;
218         }
219
220         RETURN(rc);
221 }
222
223 const struct lu_device_operations lwp_lu_ops = {
224         .ldo_process_config     = lwp_process_config,
225 };
226
227 /**
228  * Initialize LWP device.
229  *
230  * \param[in] env       environment passed by caller
231  * \param[in] lwp       device to be initialized
232  * \param[in] ldt       not used
233  * \param[in] cfg       lustre_cfg contains remote target uuid
234  *
235  * \retval              0 on success
236  * \retval              -ENODEV if the device name cannot be found
237  * \retval              negative numbers on other errors
238  */
239 int lwp_init0(const struct lu_env *env, struct lwp_device *lwp,
240               struct lu_device_type *ldt, struct lustre_cfg *cfg)
241 {
242         int                        rc;
243         ENTRY;
244
245         lwp->lpd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
246         if (lwp->lpd_obd == NULL) {
247                 CERROR("Cannot find obd with name %s\n",
248                        lustre_cfg_string(cfg, 0));
249                 RETURN(-ENODEV);
250         }
251
252         lwp->lpd_dev.ld_ops = &lwp_lu_ops;
253         lwp->lpd_obd->obd_lu_dev = &lwp->lpd_dev;
254
255         rc = ptlrpcd_addref();
256         if (rc) {
257                 CERROR("%s: ptlrpcd addref error: rc =%d\n",
258                        lwp->lpd_obd->obd_name, rc);
259                 RETURN(rc);
260         }
261
262         rc = lwp_setup(env, lwp, lustre_cfg_string(cfg, 1));
263         if (rc) {
264                 CERROR("%s: setup lwp failed. %d\n",
265                        lwp->lpd_obd->obd_name, rc);
266                 ptlrpcd_decref();
267                 RETURN(rc);
268         }
269
270         if (lprocfs_obd_setup(lwp->lpd_obd) == 0)
271                 ptlrpc_lprocfs_register_obd(lwp->lpd_obd);
272
273         RETURN(0);
274 }
275
276 /**
277  * Implementation of lu_device_type_operations::ldto_device_free.
278  *
279  * Free a LWP device.
280  *
281  * \param[in] env       environment passed by caller
282  * \param[in] lu        device to be freed
283  *
284  * \retval              NULL to indicate that this is the bottom device
285  *                      of the stack and there are no more devices
286  *                      below this one to be cleaned up.
287  */
288 static struct lu_device *lwp_device_free(const struct lu_env *env,
289                                          struct lu_device *lu)
290 {
291         struct lwp_device *m = lu2lwp_dev(lu);
292         ENTRY;
293
294         if (atomic_read(&lu->ld_ref) && lu->ld_site) {
295                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
296                 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
297         }
298         lu_device_fini(&m->lpd_dev);
299         OBD_FREE_PTR(m);
300         RETURN(NULL);
301 }
302
303 /**
304  * Implementation of lu_device_type_operations::ldto_device_alloc.
305  *
306  * Allocate a LWP device.
307  *
308  * \param[in] env       environment passed by caller
309  * \param[in] ldt       device type whose name is LUSTRE_LWP_NAME
310  * \param[in] lcfg      lustre_cfg contains remote target UUID
311  *
312  * \retval              pointer of allocated LWP device on success
313  * \retval              ERR_PTR(errno) on error
314  */
315 static struct lu_device *lwp_device_alloc(const struct lu_env *env,
316                                           struct lu_device_type *ldt,
317                                           struct lustre_cfg *lcfg)
318 {
319         struct lwp_device *lwp;
320         struct lu_device  *ludev;
321
322         OBD_ALLOC_PTR(lwp);
323         if (lwp == NULL) {
324                 ludev = ERR_PTR(-ENOMEM);
325         } else {
326                 int rc;
327
328                 ludev = lwp2lu_dev(lwp);
329                 lu_device_init(&lwp->lpd_dev, ldt);
330                 rc = lwp_init0(env, lwp, ldt, lcfg);
331                 if (rc != 0) {
332                         lwp_device_free(env, ludev);
333                         ludev = ERR_PTR(rc);
334                 }
335         }
336         return ludev;
337 }
338
339
340 /**
341  * Implementation of lu_device_type_operations::ltdo_device_fini.
342  *
343  * Finalize LWP device.
344  *
345  * \param[in] env       environment passed by caller
346  * \param[in] ludev     device to be finalized
347  *
348  * \retval              NULL on success
349  */
350 static struct lu_device *lwp_device_fini(const struct lu_env *env,
351                                          struct lu_device *ludev)
352 {
353         struct lwp_device *m = lu2lwp_dev(ludev);
354         struct obd_import *imp;
355         int                rc;
356         ENTRY;
357
358         if (m->lpd_exp != NULL)
359                 class_disconnect(m->lpd_exp);
360
361         imp = m->lpd_obd->u.cli.cl_import;
362
363         if (imp->imp_rq_pool) {
364                 ptlrpc_free_rq_pool(imp->imp_rq_pool);
365                 imp->imp_rq_pool = NULL;
366         }
367
368         LASSERT(m->lpd_obd);
369         ptlrpc_lprocfs_unregister_obd(m->lpd_obd);
370         lprocfs_obd_cleanup(m->lpd_obd);
371
372         rc = client_obd_cleanup(m->lpd_obd);
373         LASSERTF(rc == 0, "error %d\n", rc);
374
375         ptlrpcd_decref();
376
377         RETURN(NULL);
378 }
379
380 static struct lu_device_type_operations lwp_device_type_ops = {
381         .ldto_device_alloc   = lwp_device_alloc,
382         .ldto_device_free    = lwp_device_free,
383         .ldto_device_fini    = lwp_device_fini
384 };
385
386 struct lu_device_type lwp_device_type = {
387         .ldt_tags     = LU_DEVICE_DT,
388         .ldt_name     = LUSTRE_LWP_NAME,
389         .ldt_ops      = &lwp_device_type_ops,
390         .ldt_ctx_tags = LCT_MD_THREAD
391 };
392
393 /**
394  * Implementation of OBD device operations obd_ops::o_connect.
395  *
396  * Create export for LWP, and connect to target server.
397  *
398  * \param[in] env       the environment passed by caller
399  * \param[out] exp      export for the connection to be established
400  * \param[in] obd       OBD device to perform the connect on
401  * \param[in] cluuid    UUID of the OBD device
402  * \param[in] data      connect data containing compatibility flags
403  * \param[in] localdata not used
404  *
405  * \retval              0 on success
406  * \retval              negative number on error
407  */
408 static int lwp_obd_connect(const struct lu_env *env, struct obd_export **exp,
409                            struct obd_device *obd, struct obd_uuid *cluuid,
410                            struct obd_connect_data *data, void *localdata)
411 {
412         struct lwp_device       *lwp = lu2lwp_dev(obd->obd_lu_dev);
413         struct client_obd       *cli = &lwp->lpd_obd->u.cli;
414         struct obd_import       *imp = cli->cl_import;
415         struct obd_connect_data *ocd;
416         struct lustre_handle     conn;
417         int                      rc;
418
419         ENTRY;
420
421         CDEBUG(D_CONFIG, "connect #%d\n", lwp->lpd_connects);
422
423         *exp = NULL;
424         down_write(&cli->cl_sem);
425         rc = class_connect(&conn, obd, cluuid);
426         if (rc != 0)
427                 GOTO(out_sem, rc);
428
429         *exp = class_conn2export(&conn);
430         lwp->lpd_exp = *exp;
431
432         lwp->lpd_connects++;
433         LASSERT(lwp->lpd_connects == 1);
434
435         imp->imp_dlm_handle = conn;
436         rc = ptlrpc_init_import(imp);
437         if (rc != 0)
438                 GOTO(out_dis, rc);
439
440         LASSERT(data != NULL);
441         ocd = &imp->imp_connect_data;
442         *ocd = *data;
443
444         LASSERT(ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT);
445
446         ocd->ocd_version = LUSTRE_VERSION_CODE;
447         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
448
449         rc = ptlrpc_connect_import(imp);
450         if (rc != 0) {
451                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
452                 GOTO(out_dis, rc);
453         }
454
455         ptlrpc_pinger_add_import(imp);
456
457         GOTO(out_dis, rc = 0);
458
459 out_dis:
460         if (rc != 0) {
461                 class_disconnect(*exp);
462                 *exp = NULL;
463                 lwp->lpd_exp = NULL;
464         }
465
466 out_sem:
467         up_write(&cli->cl_sem);
468
469         return rc;
470 }
471
472 /**
473  * Implementation of OBD device operations obd_ops::o_disconnect.
474  *
475  * Release export for the LWP. Only disconnect the underlying layers
476  * on the final disconnect.
477  *
478  * \param[in] exp       the export to perform disconnect on
479  *
480  * \retval              0 on success
481  * \retval              negative number on error
482  */
483 static int lwp_obd_disconnect(struct obd_export *exp)
484 {
485         struct obd_device *obd = exp->exp_obd;
486         struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
487         int                rc;
488         ENTRY;
489
490         LASSERT(lwp->lpd_connects == 1);
491         lwp->lpd_connects--;
492
493         rc = class_disconnect(exp);
494         if (rc)
495                 CERROR("%s: class disconnect error: rc = %d\n",
496                        obd->obd_name, rc);
497
498         RETURN(rc);
499 }
500
501 /**
502  * Handle import events for the LWP device.
503  *
504  * \param[in] obd       OBD device associated with the import
505  * \param[in] imp       the import which event happened on
506  * \param[in] event     event type
507  *
508  * \retval              0 on success
509  * \retval              negative number on error
510  */
511 static int lwp_import_event(struct obd_device *obd, struct obd_import *imp,
512                             enum obd_import_event event)
513 {
514         switch (event) {
515         case IMP_EVENT_DISCON:
516         case IMP_EVENT_INACTIVE:
517         case IMP_EVENT_ACTIVE:
518                 break;
519         case IMP_EVENT_INVALIDATE:
520                 if (obd->obd_namespace == NULL)
521                         break;
522                 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
523                 break;
524         case IMP_EVENT_OCD:
525                 break;
526         default:
527                 CERROR("%s: unsupported import event: %#x\n",
528                        obd->obd_name, event);
529         }
530         return 0;
531 }
532
533 struct obd_ops lwp_obd_device_ops = {
534         .o_owner        = THIS_MODULE,
535         .o_add_conn     = client_import_add_conn,
536         .o_del_conn     = client_import_del_conn,
537         .o_connect      = lwp_obd_connect,
538         .o_disconnect   = lwp_obd_disconnect,
539         .o_import_event = lwp_import_event,
540 };