Whamcloud - gitweb
d8c83aa3523dec06ea495cd2268303e277bce068
[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, 2017, 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 <uapi/linux/lustre/lustre_param.h>
44 #include <lustre_log.h>
45 #include <linux/kthread.h>
46
47 #include "osp_internal.h"
48
49 struct lwp_device {
50         struct lu_device        lpd_dev;
51         struct obd_device      *lpd_obd;   /* corresponding OBD device */
52         struct obd_export      *lpd_exp;   /* export of LWP */
53         struct task_struct     *lpd_notify_task; /* notify thread */
54         int                     lpd_connects; /* use count, 0 or 1 */
55 };
56
57 static inline struct lwp_device *lu2lwp_dev(struct lu_device *d)
58 {
59         return container_of_safe(d, struct lwp_device, lpd_dev);
60 }
61
62 static inline struct lu_device *lwp2lu_dev(struct lwp_device *d)
63 {
64         return &d->lpd_dev;
65 }
66
67 /**
68  * Setup LWP device.
69  *
70  * \param[in] env       environment passed by caller
71  * \param[in] lwp       LWP device to be setup
72  * \param[in] nidstring remote target NID
73  *
74  * \retval              0 on success
75  * \retval              negative number on error
76  */
77 static int lwp_setup(const struct lu_env *env, struct lwp_device *lwp,
78                      char *nidstring)
79 {
80         struct lustre_cfg_bufs  *bufs = NULL;
81         struct lustre_cfg       *lcfg = NULL;
82         char                    *lwp_name = lwp->lpd_obd->obd_name;
83         char                    *server_uuid = NULL;
84         char                    *ptr;
85         struct obd_import       *imp;
86         int                      len = strlen(lwp_name) + 1;
87         int                      rc;
88         ENTRY;
89
90         lwp->lpd_notify_task = NULL;
91
92         OBD_ALLOC_PTR(bufs);
93         if (bufs == NULL)
94                 RETURN(-ENOMEM);
95
96         OBD_ALLOC(server_uuid, len);
97         if (server_uuid == NULL)
98                 GOTO(out, rc = -ENOMEM);
99
100         snprintf(server_uuid, len, "-%s-", LUSTRE_LWP_NAME);
101         ptr = cfs_strrstr(lwp_name, server_uuid);
102         if (ptr == NULL) {
103                 CERROR("%s: failed to get server_uuid from lwp_name: rc = %d\n",
104                        lwp_name, -EINVAL);
105                 GOTO(out, rc = -EINVAL);
106         }
107
108         strncpy(server_uuid, lwp_name, ptr - lwp_name);
109         server_uuid[ptr - lwp_name] = '\0';
110         strlcat(server_uuid, "_UUID", len);
111         lustre_cfg_bufs_reset(bufs, lwp_name);
112         lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
113         lustre_cfg_bufs_set_string(bufs, 2, nidstring);
114         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
115         if (!lcfg)
116                 GOTO(out, rc = -ENOMEM);
117         lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
118
119         rc = client_obd_setup(lwp->lpd_obd, lcfg);
120         if (rc != 0) {
121                 CERROR("%s: client obd setup error: rc = %d\n",
122                        lwp->lpd_obd->obd_name, rc);
123                 GOTO(out, rc);
124         }
125
126         imp = lwp->lpd_obd->u.cli.cl_import;
127         rc = ptlrpc_init_import(imp);
128 out:
129         if (bufs != NULL)
130                 OBD_FREE_PTR(bufs);
131         if (server_uuid != NULL)
132                 OBD_FREE(server_uuid, len);
133         if (lcfg)
134                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
135                                               lcfg->lcfg_buflens));
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 static 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 static 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 = lprocfs_obd_setup(lwp->lpd_obd, true);
263         if (rc) {
264                 CERROR("%s: lprocfs_obd_setup failed. %d\n",
265                        lwp->lpd_obd->obd_name, rc);
266                 ptlrpcd_decref();
267                 RETURN(rc);
268         }
269
270         rc = lwp_setup(env, lwp, lustre_cfg_string(cfg, 1));
271         if (rc) {
272                 CERROR("%s: setup lwp failed. %d\n",
273                        lwp->lpd_obd->obd_name, rc);
274                 lprocfs_obd_cleanup(lwp->lpd_obd);
275                 ptlrpcd_decref();
276                 RETURN(rc);
277         }
278
279         rc = sptlrpc_lprocfs_cliobd_attach(lwp->lpd_obd);
280         if (rc) {
281                 CERROR("%s: sptlrpc_lprocfs_cliobd_attached failed. %d\n",
282                        lwp->lpd_obd->obd_name, rc);
283                 ptlrpcd_decref();
284                 RETURN(rc);
285         }
286
287         ptlrpc_lprocfs_register_obd(lwp->lpd_obd);
288
289         RETURN(0);
290 }
291
292 /**
293  * Implementation of lu_device_type_operations::ldto_device_free.
294  *
295  * Free a LWP device.
296  *
297  * \param[in] env       environment passed by caller
298  * \param[in] lu        device to be freed
299  *
300  * \retval              NULL to indicate that this is the bottom device
301  *                      of the stack and there are no more devices
302  *                      below this one to be cleaned up.
303  */
304 static struct lu_device *lwp_device_free(const struct lu_env *env,
305                                          struct lu_device *lu)
306 {
307         struct lwp_device *m = lu2lwp_dev(lu);
308         ENTRY;
309
310         if (atomic_read(&lu->ld_ref) && lu->ld_site) {
311                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
312                 lu_site_print(env, lu->ld_site, &msgdata, lu_cdebug_printer);
313         }
314         lu_device_fini(&m->lpd_dev);
315         OBD_FREE_PTR(m);
316         RETURN(NULL);
317 }
318
319 /**
320  * Implementation of lu_device_type_operations::ldto_device_alloc.
321  *
322  * Allocate a LWP device.
323  *
324  * \param[in] env       environment passed by caller
325  * \param[in] ldt       device type whose name is LUSTRE_LWP_NAME
326  * \param[in] lcfg      lustre_cfg contains remote target UUID
327  *
328  * \retval              pointer of allocated LWP device on success
329  * \retval              ERR_PTR(errno) on error
330  */
331 static struct lu_device *lwp_device_alloc(const struct lu_env *env,
332                                           struct lu_device_type *ldt,
333                                           struct lustre_cfg *lcfg)
334 {
335         struct lwp_device *lwp;
336         struct lu_device  *ludev;
337
338         OBD_ALLOC_PTR(lwp);
339         if (lwp == NULL) {
340                 ludev = ERR_PTR(-ENOMEM);
341         } else {
342                 int rc;
343
344                 ludev = lwp2lu_dev(lwp);
345                 lu_device_init(&lwp->lpd_dev, ldt);
346                 rc = lwp_init0(env, lwp, ldt, lcfg);
347                 if (rc != 0) {
348                         lwp_device_free(env, ludev);
349                         ludev = ERR_PTR(rc);
350                 }
351         }
352         return ludev;
353 }
354
355
356 /**
357  * Implementation of lu_device_type_operations::ltdo_device_fini.
358  *
359  * Finalize LWP device.
360  *
361  * \param[in] env       environment passed by caller
362  * \param[in] ludev     device to be finalized
363  *
364  * \retval              NULL on success
365  */
366 static struct lu_device *lwp_device_fini(const struct lu_env *env,
367                                          struct lu_device *ludev)
368 {
369         struct lwp_device       *m = lu2lwp_dev(ludev);
370         struct task_struct      *task = NULL;
371         int                      rc;
372         ENTRY;
373
374         task = xchg(&m->lpd_notify_task, NULL);
375         if (task) {
376                 kthread_stop(task);
377                 class_export_put(m->lpd_exp);
378         }
379
380         if (m->lpd_exp != NULL)
381                 class_disconnect(m->lpd_exp);
382
383         LASSERT(m->lpd_obd);
384         rc = client_obd_cleanup(m->lpd_obd);
385         LASSERTF(rc == 0, "error %d\n", rc);
386
387         ptlrpc_lprocfs_unregister_obd(m->lpd_obd);
388
389         ptlrpcd_decref();
390
391         RETURN(NULL);
392 }
393
394 static struct lu_device_type_operations lwp_device_type_ops = {
395         .ldto_device_alloc   = lwp_device_alloc,
396         .ldto_device_free    = lwp_device_free,
397         .ldto_device_fini    = lwp_device_fini
398 };
399
400 struct lu_device_type lwp_device_type = {
401         .ldt_tags     = LU_DEVICE_DT,
402         .ldt_name     = LUSTRE_LWP_NAME,
403         .ldt_ops      = &lwp_device_type_ops,
404         .ldt_ctx_tags = LCT_MD_THREAD
405 };
406
407 static int lwp_notify_main(void *args)
408 {
409         struct obd_export       *exp = (struct obd_export *)args;
410         struct lwp_device       *lwp;
411
412         LASSERT(exp != NULL);
413
414         lwp = lu2lwp_dev(exp->exp_obd->obd_lu_dev);
415
416         lustre_notify_lwp_list(exp);
417
418         if (xchg(&lwp->lpd_notify_task, NULL) == NULL)
419                 /* lwp_device_fini() is waiting for me
420                  * Note that the wakeup comes direct from
421                  * kthread_stop, not from wake_up_var().
422                  * lwp_device_fini() will call class_export_put().
423                  */
424                 wait_var_event(lwp, kthread_should_stop());
425         else
426                 class_export_put(exp);
427
428         return 0;
429 }
430
431 /*
432  * Some notify callbacks may cause deadlock in failover
433  * scenario, so we have to start thread to run callbacks
434  * asynchronously. See LU-6273.
435  */
436 static void lwp_notify_users(struct obd_export *exp)
437 {
438         struct lwp_device       *lwp;
439         struct task_struct      *task;
440         char                     name[MTI_NAME_MAXLEN];
441
442         LASSERT(exp != NULL);
443         lwp = lu2lwp_dev(exp->exp_obd->obd_lu_dev);
444
445         snprintf(name, MTI_NAME_MAXLEN, "lwp_notify_%s",
446                  exp->exp_obd->obd_name);
447
448         /* Notify happens only on LWP setup, so there shouldn't
449          * be notify thread running */
450         if (lwp->lpd_notify_task) {
451                 CERROR("LWP notify thread: %s wasn't stopped\n", name);
452                 return;
453         }
454
455         task = kthread_create(lwp_notify_main, exp, name);
456         if (IS_ERR(task)) {
457                 CERROR("Failed to start LWP notify thread:%s. %lu\n",
458                        name, PTR_ERR(task));
459         } else {
460                 lwp->lpd_notify_task = task;
461                 class_export_get(exp);
462                 wake_up_process(task);
463         }
464 }
465
466 /**
467  * Implementation of OBD device operations obd_ops::o_connect.
468  *
469  * Create export for LWP, and connect to target server.
470  *
471  * \param[in] env       the environment passed by caller
472  * \param[out] exp      export for the connection to be established
473  * \param[in] obd       OBD device to perform the connect on
474  * \param[in] cluuid    UUID of the OBD device
475  * \param[in] data      connect data containing compatibility flags
476  * \param[in] localdata not used
477  *
478  * \retval              0 on success
479  * \retval              negative number on error
480  */
481 static int lwp_obd_connect(const struct lu_env *env, struct obd_export **exp,
482                            struct obd_device *obd, struct obd_uuid *cluuid,
483                            struct obd_connect_data *data, void *localdata)
484 {
485         struct lwp_device       *lwp = lu2lwp_dev(obd->obd_lu_dev);
486         struct client_obd       *cli = &lwp->lpd_obd->u.cli;
487         struct obd_import       *imp = cli->cl_import;
488         struct obd_connect_data *ocd;
489         struct lustre_handle     conn;
490         int                      rc;
491
492         ENTRY;
493
494         CDEBUG(D_CONFIG, "connect #%d\n", lwp->lpd_connects);
495
496         *exp = NULL;
497         down_write(&cli->cl_sem);
498         rc = class_connect(&conn, obd, cluuid);
499         if (rc != 0)
500                 GOTO(out_sem, rc);
501
502         *exp = class_conn2export(&conn);
503         lwp->lpd_exp = *exp;
504
505         lwp->lpd_connects++;
506         LASSERT(lwp->lpd_connects == 1);
507
508         imp->imp_dlm_handle = conn;
509         rc = ptlrpc_init_import(imp);
510         if (rc != 0)
511                 GOTO(out_dis, rc);
512
513         LASSERT(data != NULL);
514         ocd = &imp->imp_connect_data;
515         *ocd = *data;
516
517         LASSERT(ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT);
518
519         ocd->ocd_version = LUSTRE_VERSION_CODE;
520         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
521         imp->imp_connect_flags2_orig = ocd->ocd_connect_flags2;
522
523         rc = ptlrpc_connect_import(imp);
524         if (rc != 0) {
525                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
526                 GOTO(out_dis, rc);
527         }
528
529         ptlrpc_pinger_add_import(imp);
530
531         GOTO(out_dis, rc = 0);
532
533 out_dis:
534         if (rc != 0) {
535                 class_disconnect(*exp);
536                 *exp = NULL;
537                 lwp->lpd_exp = NULL;
538         }
539
540 out_sem:
541         up_write(&cli->cl_sem);
542
543         if (rc == 0)
544                 lwp_notify_users(*exp);
545
546         return rc;
547 }
548
549 /**
550  * Implementation of OBD device operations obd_ops::o_disconnect.
551  *
552  * Release export for the LWP. Only disconnect the underlying layers
553  * on the final disconnect.
554  *
555  * \param[in] exp       the export to perform disconnect on
556  *
557  * \retval              0 on success
558  * \retval              negative number on error
559  */
560 static int lwp_obd_disconnect(struct obd_export *exp)
561 {
562         struct obd_device *obd = exp->exp_obd;
563         struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
564         int                rc;
565         ENTRY;
566
567         LASSERT(lwp->lpd_connects == 1);
568         lwp->lpd_connects--;
569
570         rc = class_disconnect(exp);
571         if (rc)
572                 CERROR("%s: class disconnect error: rc = %d\n",
573                        obd->obd_name, rc);
574
575         RETURN(rc);
576 }
577
578 /**
579  * Handle import events for the LWP device.
580  *
581  * \param[in] obd       OBD device associated with the import
582  * \param[in] imp       the import which event happened on
583  * \param[in] event     event type
584  *
585  * \retval              0 on success
586  * \retval              negative number on error
587  */
588 static int lwp_import_event(struct obd_device *obd, struct obd_import *imp,
589                             enum obd_import_event event)
590 {
591         switch (event) {
592         case IMP_EVENT_DISCON:
593         case IMP_EVENT_INACTIVE:
594         case IMP_EVENT_ACTIVE:
595                 break;
596         case IMP_EVENT_INVALIDATE:
597                 if (obd->obd_namespace == NULL)
598                         break;
599                 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
600                 break;
601         case IMP_EVENT_OCD:
602                 break;
603         default:
604                 CERROR("%s: unsupported import event: %#x\n",
605                        obd->obd_name, event);
606         }
607         return 0;
608 }
609
610 static int lwp_set_info_async(const struct lu_env *env,
611                               struct obd_export *exp,
612                               u32 keylen, void *key,
613                               u32 vallen, void *val,
614                               struct ptlrpc_request_set *set)
615 {
616         ENTRY;
617
618         if (KEY_IS(KEY_SPTLRPC_CONF)) {
619                 sptlrpc_conf_client_adapt(exp->exp_obd);
620                 RETURN(0);
621         }
622
623         CERROR("Unknown key %s\n", (char *)key);
624         RETURN(-EINVAL);
625 }
626
627 const struct obd_ops lwp_obd_device_ops = {
628         .o_owner        = THIS_MODULE,
629         .o_add_conn     = client_import_add_conn,
630         .o_del_conn     = client_import_del_conn,
631         .o_connect      = lwp_obd_connect,
632         .o_disconnect   = lwp_obd_disconnect,
633         .o_import_event = lwp_import_event,
634         .o_set_info_async   = lwp_set_info_async,
635 };