Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[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         int                      uuid_len = -1;
86         struct obd_import       *imp;
87         int                      len = strlen(lwp_name) + 1;
88         int                      rc;
89         const char              *lwp_marker = "-" LUSTRE_LWP_NAME "-";
90         ENTRY;
91
92         lwp->lpd_notify_task = NULL;
93
94         OBD_ALLOC_PTR(bufs);
95         if (bufs == NULL)
96                 RETURN(-ENOMEM);
97
98         OBD_ALLOC(server_uuid, len);
99         if (server_uuid == NULL)
100                 GOTO(out, rc = -ENOMEM);
101
102         ptr = lwp_name;
103         while (ptr && (ptr = strstr(ptr+1, lwp_marker)) != NULL)
104                 uuid_len = ptr - lwp_name;
105
106         if (uuid_len < 0) {
107                 CERROR("%s: failed to get server_uuid from lwp_name: rc = %d\n",
108                        lwp_name, -EINVAL);
109                 GOTO(out, rc = -EINVAL);
110         }
111
112         strncpy(server_uuid, lwp_name, uuid_len);
113         strlcat(server_uuid, "_UUID", len);
114         lustre_cfg_bufs_reset(bufs, lwp_name);
115         lustre_cfg_bufs_set_string(bufs, 1, server_uuid);
116         lustre_cfg_bufs_set_string(bufs, 2, nidstring);
117         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount, bufs->lcfg_buflen));
118         if (!lcfg)
119                 GOTO(out, rc = -ENOMEM);
120         lustre_cfg_init(lcfg, LCFG_SETUP, bufs);
121
122         rc = client_obd_setup(lwp->lpd_obd, lcfg);
123         if (rc != 0) {
124                 CERROR("%s: client obd setup error: rc = %d\n",
125                        lwp->lpd_obd->obd_name, rc);
126                 GOTO(out, rc);
127         }
128
129         imp = lwp->lpd_obd->u.cli.cl_import;
130         rc = ptlrpc_init_import(imp);
131 out:
132         if (bufs != NULL)
133                 OBD_FREE_PTR(bufs);
134         if (server_uuid != NULL)
135                 OBD_FREE(server_uuid, len);
136         if (lcfg)
137                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
138                                               lcfg->lcfg_buflens));
139         if (rc)
140                 client_obd_cleanup(lwp->lpd_obd);
141
142         RETURN(rc);
143 }
144
145 /**
146  * Disconnect the import from LWP.
147  *
148  * \param[in] d         LWP device to be disconnected
149  *
150  * \retval              0 on success
151  * \retval              negative number on error
152  */
153 static int lwp_disconnect(struct lwp_device *d)
154 {
155         struct obd_import *imp;
156         int rc = 0;
157
158         imp = d->lpd_obd->u.cli.cl_import;
159
160         /*
161          * Mark import deactivated now, so we don't try to reconnect if any
162          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
163          * fully deactivate the import because that would cause all requests
164          * to be dropped.
165          */
166         LASSERT(imp != NULL);
167         spin_lock(&imp->imp_lock);
168         imp->imp_deactive = 1;
169         spin_unlock(&imp->imp_lock);
170
171         ptlrpc_deactivate_import(imp);
172
173         /*
174          * Some non-replayable imports (MDS's OSCs) are pinged, so just
175          * delete it regardless.  (It's safe to delete an import that was
176          * never added.)
177          */
178         ptlrpc_pinger_del_import(imp);
179         rc = ptlrpc_disconnect_import(imp, 0);
180         if (rc != 0)
181                 CWARN("%s: can't disconnect: rc = %d\n",
182                       d->lpd_obd->obd_name, rc);
183
184         ptlrpc_invalidate_import(imp);
185
186         RETURN(rc);
187 }
188
189 /**
190  * Implementation of lu_device_operations::ldo_process_config.
191  *
192  * Process a Lustre configuration request.
193  *
194  * \param[in] env       environment passed by caller
195  * \param[in] dev       device to be processed
196  * \param[in] lcfg      lustre_cfg, LCFG_PRE_CLEANUP or LCFG_CLEANUP
197  *
198  * \retval              0 on success
199  * \retval              negative number on error
200  */
201 static int lwp_process_config(const struct lu_env *env,
202                               struct lu_device *dev, struct lustre_cfg *lcfg)
203 {
204         struct lwp_device               *d = lu2lwp_dev(dev);
205         int                              rc;
206         ENTRY;
207
208         switch (lcfg->lcfg_command) {
209         case LCFG_PRE_CLEANUP:
210         case LCFG_CLEANUP:
211                 rc = lwp_disconnect(d);
212                 break;
213         case LCFG_PARAM:
214                 rc = -ENOSYS;
215                 break;
216         default:
217                 CERROR("%s: unknown command %u\n",
218                        (char *)lustre_cfg_string(lcfg, 0), lcfg->lcfg_command);
219                 rc = 0;
220                 break;
221         }
222
223         RETURN(rc);
224 }
225
226 static const struct lu_device_operations lwp_lu_ops = {
227         .ldo_process_config     = lwp_process_config,
228 };
229
230 /**
231  * Initialize LWP device.
232  *
233  * \param[in] env       environment passed by caller
234  * \param[in] lwp       device to be initialized
235  * \param[in] ldt       not used
236  * \param[in] cfg       lustre_cfg contains remote target uuid
237  *
238  * \retval              0 on success
239  * \retval              -ENODEV if the device name cannot be found
240  * \retval              negative numbers on other errors
241  */
242 static int lwp_init0(const struct lu_env *env, struct lwp_device *lwp,
243                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
244 {
245         int                        rc;
246         ENTRY;
247
248         lwp->lpd_obd = class_name2obd(lustre_cfg_string(cfg, 0));
249         if (lwp->lpd_obd == NULL) {
250                 CERROR("Cannot find obd with name %s\n",
251                        lustre_cfg_string(cfg, 0));
252                 RETURN(-ENODEV);
253         }
254
255         lwp->lpd_dev.ld_ops = &lwp_lu_ops;
256         lwp->lpd_obd->obd_lu_dev = &lwp->lpd_dev;
257
258         rc = ptlrpcd_addref();
259         if (rc) {
260                 CERROR("%s: ptlrpcd addref error: rc =%d\n",
261                        lwp->lpd_obd->obd_name, rc);
262                 RETURN(rc);
263         }
264
265         rc = lprocfs_obd_setup(lwp->lpd_obd, true);
266         if (rc) {
267                 CERROR("%s: lprocfs_obd_setup failed. %d\n",
268                        lwp->lpd_obd->obd_name, rc);
269                 ptlrpcd_decref();
270                 RETURN(rc);
271         }
272
273         rc = lwp_setup(env, lwp, lustre_cfg_string(cfg, 1));
274         if (rc) {
275                 CERROR("%s: setup lwp failed. %d\n",
276                        lwp->lpd_obd->obd_name, rc);
277                 lprocfs_obd_cleanup(lwp->lpd_obd);
278                 ptlrpcd_decref();
279                 RETURN(rc);
280         }
281
282         rc = sptlrpc_lprocfs_cliobd_attach(lwp->lpd_obd);
283         if (rc) {
284                 CERROR("%s: sptlrpc_lprocfs_cliobd_attached failed. %d\n",
285                        lwp->lpd_obd->obd_name, rc);
286                 ptlrpcd_decref();
287                 RETURN(rc);
288         }
289
290         ptlrpc_lprocfs_register_obd(lwp->lpd_obd);
291
292         RETURN(0);
293 }
294
295 /**
296  * Implementation of lu_device_type_operations::ldto_device_free.
297  *
298  * Free a LWP device.
299  *
300  * \param[in] env       environment passed by caller
301  * \param[in] lu        device to be freed
302  *
303  * \retval              NULL to indicate that this is the bottom device
304  *                      of the stack and there are no more devices
305  *                      below this one to be cleaned up.
306  */
307 static struct lu_device *lwp_device_free(const struct lu_env *env,
308                                          struct lu_device *lu)
309 {
310         struct lwp_device *m = lu2lwp_dev(lu);
311         ENTRY;
312
313         lu_site_print(env, lu->ld_site, &lu->ld_ref, D_ERROR,
314                       lu_cdebug_printer);
315         lu_device_fini(&m->lpd_dev);
316         OBD_FREE_PTR(m);
317         RETURN(NULL);
318 }
319
320 /**
321  * Implementation of lu_device_type_operations::ldto_device_alloc.
322  *
323  * Allocate a LWP device.
324  *
325  * \param[in] env       environment passed by caller
326  * \param[in] ldt       device type whose name is LUSTRE_LWP_NAME
327  * \param[in] lcfg      lustre_cfg contains remote target UUID
328  *
329  * \retval              pointer of allocated LWP device on success
330  * \retval              ERR_PTR(errno) on error
331  */
332 static struct lu_device *lwp_device_alloc(const struct lu_env *env,
333                                           struct lu_device_type *ldt,
334                                           struct lustre_cfg *lcfg)
335 {
336         struct lwp_device *lwp;
337         struct lu_device  *ludev;
338
339         OBD_ALLOC_PTR(lwp);
340         if (lwp == NULL) {
341                 ludev = ERR_PTR(-ENOMEM);
342         } else {
343                 int rc;
344
345                 ludev = lwp2lu_dev(lwp);
346                 lu_device_init(&lwp->lpd_dev, ldt);
347                 rc = lwp_init0(env, lwp, ldt, lcfg);
348                 if (rc != 0) {
349                         lwp_device_free(env, ludev);
350                         ludev = ERR_PTR(rc);
351                 }
352         }
353         return ludev;
354 }
355
356
357 /**
358  * Implementation of lu_device_type_operations::ltdo_device_fini.
359  *
360  * Finalize LWP device.
361  *
362  * \param[in] env       environment passed by caller
363  * \param[in] ludev     device to be finalized
364  *
365  * \retval              NULL on success
366  */
367 static struct lu_device *lwp_device_fini(const struct lu_env *env,
368                                          struct lu_device *ludev)
369 {
370         struct lwp_device       *m = lu2lwp_dev(ludev);
371         struct task_struct      *task = NULL;
372         int                      rc;
373         ENTRY;
374
375         task = xchg(&m->lpd_notify_task, NULL);
376         if (task) {
377                 kthread_stop(task);
378                 class_export_put(m->lpd_exp);
379         }
380
381         if (m->lpd_exp != NULL)
382                 class_disconnect(m->lpd_exp);
383
384         LASSERT(m->lpd_obd);
385         rc = client_obd_cleanup(m->lpd_obd);
386         LASSERTF(rc == 0, "error %d\n", rc);
387
388         ptlrpc_lprocfs_unregister_obd(m->lpd_obd);
389
390         ptlrpcd_decref();
391
392         RETURN(NULL);
393 }
394
395 static const struct lu_device_type_operations lwp_device_type_ops = {
396         .ldto_device_alloc      = lwp_device_alloc,
397         .ldto_device_free       = lwp_device_free,
398         .ldto_device_fini       = lwp_device_fini
399 };
400
401 struct lu_device_type lwp_device_type = {
402         .ldt_tags     = LU_DEVICE_DT,
403         .ldt_name     = LUSTRE_LWP_NAME,
404         .ldt_ops      = &lwp_device_type_ops,
405         .ldt_ctx_tags = LCT_MD_THREAD
406 };
407
408 static int lwp_notify_main(void *args)
409 {
410         struct obd_export       *exp = (struct obd_export *)args;
411         struct lwp_device       *lwp;
412
413         LASSERT(exp != NULL);
414
415         lwp = lu2lwp_dev(exp->exp_obd->obd_lu_dev);
416
417         lustre_notify_lwp_list(exp);
418
419         if (xchg(&lwp->lpd_notify_task, NULL) == NULL)
420                 /* lwp_device_fini() is waiting for me
421                  * Note that the wakeup comes direct from
422                  * kthread_stop, not from wake_up_var().
423                  * lwp_device_fini() will call class_export_put().
424                  */
425                 wait_var_event(lwp, kthread_should_stop());
426         else
427                 class_export_put(exp);
428
429         return 0;
430 }
431
432 /*
433  * Some notify callbacks may cause deadlock in failover
434  * scenario, so we have to start thread to run callbacks
435  * asynchronously. See LU-6273.
436  */
437 static void lwp_notify_users(struct obd_export *exp)
438 {
439         struct lwp_device       *lwp;
440         struct task_struct      *task;
441         char                     name[MTI_NAME_MAXLEN];
442
443         LASSERT(exp != NULL);
444         lwp = lu2lwp_dev(exp->exp_obd->obd_lu_dev);
445
446         snprintf(name, MTI_NAME_MAXLEN, "lwp_notify_%s",
447                  exp->exp_obd->obd_name);
448
449         /* Notify happens only on LWP setup, so there shouldn't
450          * be notify thread running */
451         if (lwp->lpd_notify_task) {
452                 CERROR("LWP notify thread: %s wasn't stopped\n", name);
453                 return;
454         }
455
456         task = kthread_create(lwp_notify_main, exp, name);
457         if (IS_ERR(task)) {
458                 CERROR("Failed to start LWP notify thread:%s. %lu\n",
459                        name, PTR_ERR(task));
460         } else {
461                 lwp->lpd_notify_task = task;
462                 class_export_get(exp);
463                 wake_up_process(task);
464         }
465 }
466
467 /**
468  * Implementation of OBD device operations obd_ops::o_connect.
469  *
470  * Create export for LWP, and connect to target server.
471  *
472  * \param[in] env       the environment passed by caller
473  * \param[out] exp      export for the connection to be established
474  * \param[in] obd       OBD device to perform the connect on
475  * \param[in] cluuid    UUID of the OBD device
476  * \param[in] data      connect data containing compatibility flags
477  * \param[in] localdata not used
478  *
479  * \retval              0 on success
480  * \retval              negative number on error
481  */
482 static int lwp_obd_connect(const struct lu_env *env, struct obd_export **exp,
483                            struct obd_device *obd, struct obd_uuid *cluuid,
484                            struct obd_connect_data *data, void *localdata)
485 {
486         struct lwp_device       *lwp = lu2lwp_dev(obd->obd_lu_dev);
487         struct client_obd       *cli = &lwp->lpd_obd->u.cli;
488         struct obd_import       *imp = cli->cl_import;
489         struct obd_connect_data *ocd;
490         struct lustre_handle     conn;
491         int                      rc;
492
493         ENTRY;
494
495         CDEBUG(D_CONFIG, "connect #%d\n", lwp->lpd_connects);
496
497         *exp = NULL;
498         down_write(&cli->cl_sem);
499         rc = class_connect(&conn, obd, cluuid);
500         if (rc != 0)
501                 GOTO(out_sem, rc);
502
503         *exp = class_conn2export(&conn);
504         lwp->lpd_exp = *exp;
505
506         lwp->lpd_connects++;
507         LASSERT(lwp->lpd_connects == 1);
508
509         imp->imp_dlm_handle = conn;
510         rc = ptlrpc_init_import(imp);
511         if (rc != 0)
512                 GOTO(out_dis, rc);
513
514         LASSERT(data != NULL);
515         ocd = &imp->imp_connect_data;
516         *ocd = *data;
517
518         LASSERT(ocd->ocd_connect_flags & OBD_CONNECT_LIGHTWEIGHT);
519
520         ocd->ocd_version = LUSTRE_VERSION_CODE;
521         imp->imp_connect_flags_orig = ocd->ocd_connect_flags;
522         imp->imp_connect_flags2_orig = ocd->ocd_connect_flags2;
523
524         rc = ptlrpc_connect_import(imp);
525         if (rc != 0) {
526                 CERROR("%s: can't connect obd: rc = %d\n", obd->obd_name, rc);
527                 GOTO(out_dis, rc);
528         }
529
530         ptlrpc_pinger_add_import(imp);
531
532         GOTO(out_dis, rc = 0);
533
534 out_dis:
535         if (rc != 0) {
536                 class_disconnect(*exp);
537                 *exp = NULL;
538                 lwp->lpd_exp = NULL;
539         }
540
541 out_sem:
542         up_write(&cli->cl_sem);
543
544         if (rc == 0)
545                 lwp_notify_users(*exp);
546
547         return rc;
548 }
549
550 /**
551  * Implementation of OBD device operations obd_ops::o_disconnect.
552  *
553  * Release export for the LWP. Only disconnect the underlying layers
554  * on the final disconnect.
555  *
556  * \param[in] exp       the export to perform disconnect on
557  *
558  * \retval              0 on success
559  * \retval              negative number on error
560  */
561 static int lwp_obd_disconnect(struct obd_export *exp)
562 {
563         struct obd_device *obd = exp->exp_obd;
564         struct lwp_device *lwp = lu2lwp_dev(obd->obd_lu_dev);
565         int                rc;
566         ENTRY;
567
568         LASSERT(lwp->lpd_connects == 1);
569         lwp->lpd_connects--;
570
571         rc = class_disconnect(exp);
572         if (rc)
573                 CERROR("%s: class disconnect error: rc = %d\n",
574                        obd->obd_name, rc);
575
576         RETURN(rc);
577 }
578
579 /**
580  * Handle import events for the LWP device.
581  *
582  * \param[in] obd       OBD device associated with the import
583  * \param[in] imp       the import which event happened on
584  * \param[in] event     event type
585  *
586  * \retval              0 on success
587  * \retval              negative number on error
588  */
589 static int lwp_import_event(struct obd_device *obd, struct obd_import *imp,
590                             enum obd_import_event event)
591 {
592         switch (event) {
593         case IMP_EVENT_DISCON:
594         case IMP_EVENT_INACTIVE:
595         case IMP_EVENT_ACTIVE:
596                 break;
597         case IMP_EVENT_INVALIDATE:
598                 if (obd->obd_namespace == NULL)
599                         break;
600                 ldlm_namespace_cleanup(obd->obd_namespace, LDLM_FL_LOCAL_ONLY);
601                 break;
602         case IMP_EVENT_OCD:
603                 break;
604         default:
605                 CERROR("%s: unsupported import event: %#x\n",
606                        obd->obd_name, event);
607         }
608         return 0;
609 }
610
611 static int lwp_set_info_async(const struct lu_env *env,
612                               struct obd_export *exp,
613                               u32 keylen, void *key,
614                               u32 vallen, void *val,
615                               struct ptlrpc_request_set *set)
616 {
617         ENTRY;
618
619         if (KEY_IS(KEY_SPTLRPC_CONF)) {
620                 sptlrpc_conf_client_adapt(exp->exp_obd);
621                 RETURN(0);
622         }
623
624         CERROR("Unknown key %s\n", (char *)key);
625         RETURN(-EINVAL);
626 }
627
628 const struct obd_ops lwp_obd_device_ops = {
629         .o_owner        = THIS_MODULE,
630         .o_add_conn     = client_import_add_conn,
631         .o_del_conn     = client_import_del_conn,
632         .o_connect      = lwp_obd_connect,
633         .o_disconnect   = lwp_obd_disconnect,
634         .o_import_event = lwp_import_event,
635         .o_set_info_async   = lwp_set_info_async,
636 };