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