Whamcloud - gitweb
New tag 2.15.63
[fs/lustre-release.git] / lustre / mgc / mgc_request.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, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/mgc/mgc_request.c
32  *
33  * Author: Nathan Rutman <nathan@clusterfs.com>
34  */
35
36 #define DEBUG_SUBSYSTEM S_MGC
37 #define D_MGC D_CONFIG /*|D_WARNING*/
38
39 #include <linux/module.h>
40 #include <linux/random.h>
41
42 #include <lprocfs_status.h>
43 #include <lustre_dlm.h>
44 #include <lustre_disk.h>
45 #include <lustre_log.h>
46 #include <lustre_swab.h>
47 #include <obd_class.h>
48
49 #include "mgc_internal.h"
50
51 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
52                           enum mgs_cfg_type type)
53 {
54         __u64 resname = 0;
55
56         if (len > sizeof(resname)) {
57                 CERROR("name too long: %s\n", name);
58                 return -EINVAL;
59         }
60         if (len <= 0) {
61                 CERROR("missing name: %s\n", name);
62                 return -EINVAL;
63         }
64         memcpy(&resname, name, len);
65
66         /* Always use the same endianness for the resid */
67         memset(res_id, 0, sizeof(*res_id));
68         res_id->name[0] = cpu_to_le64(resname);
69         /* XXX: unfortunately, sptlprc and config llog share one lock */
70         switch (type) {
71         case MGS_CFG_T_CONFIG:
72         case MGS_CFG_T_SPTLRPC:
73                 resname = 0;
74                 break;
75         case MGS_CFG_T_RECOVER:
76         case MGS_CFG_T_PARAMS:
77 #ifdef HAVE_SERVER_SUPPORT
78         case MGS_CFG_T_NODEMAP:
79         case MGS_CFG_T_BARRIER:
80 #endif
81                 resname = type;
82                 break;
83         default:
84                 LBUG();
85         }
86         res_id->name[1] = cpu_to_le64(resname);
87         CDEBUG(D_MGC, "log %s to resid %#llx/%#llx (%.8s)\n", name,
88                res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
89         return 0;
90 }
91
92 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id,
93                      enum mgs_cfg_type type)
94 {
95         /* fsname is at most 8 chars long, can contain "-". eg. lustre, lu-0 */
96         return mgc_name2resid(fsname, strlen(fsname), res_id, type);
97 }
98 EXPORT_SYMBOL(mgc_fsname2resid);
99
100 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id,
101                       enum mgs_cfg_type type)
102 {
103         char *name_end;
104         int len;
105
106         /* logname consists of "fsname-nodetype". eg. "lustre-MDT0001",
107          * "SUN-000-client" there is an exception: llog "params"
108          */
109         name_end = strrchr(logname, '-');
110         if (!name_end)
111                 len = strlen(logname);
112         else
113                 len = name_end - logname;
114         return mgc_name2resid(logname, len, res_id, type);
115 }
116 EXPORT_SYMBOL(mgc_logname2resid);
117
118 /********************** config llog list **********************/
119 static LIST_HEAD(config_llog_list);
120 static DEFINE_SPINLOCK(config_list_lock);       /* protects config_llog_list */
121
122 /* Take a reference to a config log */
123 static int config_log_get(struct config_llog_data *cld)
124 {
125         ENTRY;
126         refcount_inc(&cld->cld_refcount);
127         CDEBUG(D_INFO, "log %s (%p) refs %d\n", cld->cld_logname, cld,
128                 refcount_read(&cld->cld_refcount));
129         RETURN(0);
130 }
131
132 /* Drop a reference to a config log. When no longer referenced, We can free the
133  * config log data
134  */
135 static void config_log_put(struct config_llog_data *cld)
136 {
137         ENTRY;
138
139         if (unlikely(!cld))
140                 RETURN_EXIT;
141
142         CDEBUG(D_INFO, "log %s(%p) refs %d\n", cld->cld_logname, cld,
143                 refcount_read(&cld->cld_refcount));
144
145         /* spinlock to make sure no item with 0 refcount in the list */
146         if (refcount_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
147                 list_del(&cld->cld_list_chain);
148                 spin_unlock(&config_list_lock);
149
150                 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
151 #ifdef HAVE_SERVER_SUPPORT
152                 config_log_put(cld->cld_barrier);
153                 config_log_put(cld->cld_nodemap);
154 #endif
155                 config_log_put(cld->cld_recover);
156                 config_log_put(cld->cld_params);
157                 config_log_put(cld->cld_sptlrpc);
158                 if (cld_is_sptlrpc(cld)) {
159                         cld->cld_stopping = 1;
160                         sptlrpc_conf_log_stop(cld->cld_logname);
161                 }
162
163                 class_export_put(cld->cld_mgcexp);
164                 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
165         }
166
167         EXIT;
168 }
169
170 /* Find a config log by name */
171 static
172 struct config_llog_data *config_log_find(char *logname,
173                                          struct config_llog_instance *cfg)
174 {
175         struct config_llog_data *cld;
176         struct config_llog_data *found = NULL;
177         unsigned long cfg_instance;
178
179         ENTRY;
180         LASSERT(logname != NULL);
181
182         cfg_instance = cfg ? cfg->cfg_instance : 0;
183         spin_lock(&config_list_lock);
184         list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
185                 /* check if cfg_instance is the one we want */
186                 if (cfg_instance != cld->cld_cfg.cfg_instance)
187                         continue;
188
189                 /* instance may be NULL, should check name */
190                 if (strcmp(logname, cld->cld_logname) == 0) {
191                         found = cld;
192                         config_log_get(found);
193                         break;
194                 }
195         }
196         spin_unlock(&config_list_lock);
197         RETURN(found);
198 }
199
200 static
201 struct config_llog_data *do_config_log_add(struct obd_device *obd,
202                                            char *logname,
203                                            enum mgs_cfg_type type,
204                                            struct config_llog_instance *cfg,
205                                            struct super_block *sb)
206 {
207         struct config_llog_data *cld;
208         int rc;
209         int logname_size;
210
211         ENTRY;
212
213         CDEBUG(D_MGC, "do adding config log %s-%016lx\n", logname,
214                cfg ? cfg->cfg_instance : 0);
215
216         logname_size = strlen(logname) + 1;
217         OBD_ALLOC(cld, sizeof(*cld) + logname_size);
218         if (!cld)
219                 RETURN(ERR_PTR(-ENOMEM));
220
221         rc = mgc_logname2resid(logname, &cld->cld_resid, type);
222         if (rc) {
223                 OBD_FREE(cld, sizeof(*cld) + logname_size);
224                 RETURN(ERR_PTR(rc));
225         }
226
227         strscpy(cld->cld_logname, logname, logname_size);
228         if (cfg)
229                 cld->cld_cfg = *cfg;
230         else
231                 cld->cld_cfg.cfg_callback = class_config_llog_handler;
232         mutex_init(&cld->cld_lock);
233         cld->cld_cfg.cfg_last_idx = 0;
234         cld->cld_cfg.cfg_flags = 0;
235         cld->cld_cfg.cfg_sb = sb;
236         cld->cld_type = type;
237         refcount_set(&cld->cld_refcount, 1);
238
239         /* Keep the mgc around until we are done */
240         cld->cld_mgcexp = class_export_get(obd->obd_self_export);
241
242         if (cld_is_sptlrpc(cld))
243                 sptlrpc_conf_log_start(logname);
244
245         spin_lock(&config_list_lock);
246         list_add(&cld->cld_list_chain, &config_llog_list);
247         spin_unlock(&config_list_lock);
248
249         if (cld_is_sptlrpc(cld) || cld_is_nodemap(cld) || cld_is_barrier(cld)) {
250                 rc = mgc_process_log(obd, cld);
251                 if (rc && rc != -ENOENT)
252                         CERROR("%s: failed processing log, type %d: rc = %d\n",
253                                obd->obd_name, type, rc);
254         }
255
256         RETURN(cld);
257 }
258
259 static struct config_llog_data *
260 config_recover_log_add(struct obd_device *obd, char *fsname,
261                        struct config_llog_instance *cfg,
262                        struct super_block *sb)
263 {
264         struct config_llog_instance lcfg = *cfg;
265         struct config_llog_data *cld;
266         bool is_server = IS_SERVER(s2lsi(sb));
267         char logname[32];
268
269 #ifdef HAVE_SERVER_SUPPORT
270         if (IS_OST(s2lsi(sb)))
271                 return NULL;
272
273         /* for osp-on-ost, see lustre_start_osp() */
274         if (IS_MDT(s2lsi(sb)) && lcfg.cfg_instance)
275                 return NULL;
276 #endif
277         /* We have to use different llog for clients and MDTs for DNE,
278          * where only clients are notified if one of DNE server restarts.
279          */
280         LASSERT(strlen(fsname) < sizeof(logname) / 2);
281         strncpy(logname, fsname, sizeof(logname));
282
283         LASSERT(is_server ? lcfg.cfg_instance == 0 : lcfg.cfg_instance != 0);
284         if (is_server)
285                 lcfg.cfg_instance = ll_get_cfg_instance(sb);
286         scnprintf(logname, sizeof(logname), "%s-%s", fsname,
287                   is_server ? "mdtir" : "cliir");
288
289         cld = do_config_log_add(obd, logname, MGS_CFG_T_RECOVER, &lcfg, sb);
290         return cld;
291 }
292
293 static struct config_llog_data *
294 config_log_find_or_add(struct obd_device *obd, char *logname,
295                        struct super_block *sb, enum mgs_cfg_type type,
296                        struct config_llog_instance *cfg)
297 {
298         struct config_llog_instance lcfg = *cfg;
299         struct config_llog_data *cld;
300
301         /* Note class_config_llog_handler() depends on getting "obd" back */
302         /* for sptlrpc, sb is only provided to be able to make a local copy,
303          * not for the instance
304          */
305         if (sb && type != MGS_CFG_T_SPTLRPC)
306                 lcfg.cfg_instance = ll_get_cfg_instance(sb);
307         else
308                 lcfg.cfg_instance = (unsigned long)obd;
309
310         cld = config_log_find(logname, &lcfg);
311         if (unlikely(cld != NULL))
312                 return cld;
313
314         return do_config_log_add(obd, logname, type, &lcfg, sb);
315 }
316
317 /** Add this log to the list of active logs watched by an MGC.
318  * Active means we're watching for updates.
319  * We have one active log per "mount" - client instance or servername.
320  * Each instance may be at a different point in the log.
321  */
322 static struct config_llog_data *
323 config_log_add(struct obd_device *obd, char *logname,
324                struct config_llog_instance *cfg, struct super_block *sb)
325 {
326         struct lustre_sb_info *lsi = s2lsi(sb);
327         struct config_llog_data *cld = NULL;
328         struct config_llog_data *sptlrpc_cld = NULL;
329         struct config_llog_data *params_cld = NULL;
330 #ifdef HAVE_SERVER_SUPPORT
331         struct config_llog_data *nodemap_cld = NULL;
332         struct config_llog_data *barrier_cld = NULL;
333 #endif
334         char seclogname[32];
335         char *ptr;
336         int rc;
337         bool locked = false;
338
339         ENTRY;
340
341         CDEBUG(D_MGC, "add config log %s-%016lx\n", logname,
342                cfg->cfg_instance);
343
344         /*
345          * for each regular log, the depended sptlrpc log name is
346          * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
347          */
348         ptr = strrchr(logname, '-');
349         if (ptr == NULL || ptr - logname > 8) {
350                 CERROR("logname %s is too long\n", logname);
351                 RETURN(ERR_PTR(-EINVAL));
352         }
353
354         memcpy(seclogname, logname, ptr - logname);
355         strcpy(seclogname + (ptr - logname), "-sptlrpc");
356
357         if (cfg->cfg_sub_clds & CONFIG_SUB_SPTLRPC) {
358                 sptlrpc_cld = config_log_find_or_add(obd, seclogname, sb,
359                                                      MGS_CFG_T_SPTLRPC, cfg);
360                 if (IS_ERR(sptlrpc_cld)) {
361                         rc = PTR_ERR(sptlrpc_cld);
362                         CERROR("%s: can't create sptlrpc log %s: rc = %d\n",
363                                obd->obd_name, seclogname, rc);
364                         GOTO(out_err, rc);
365                 }
366         }
367
368         if (cfg->cfg_sub_clds & CONFIG_SUB_PARAMS) {
369                 params_cld = config_log_find_or_add(obd, PARAMS_FILENAME, sb,
370                                                     MGS_CFG_T_PARAMS, cfg);
371                 if (IS_ERR(params_cld)) {
372                         rc = PTR_ERR(params_cld);
373                         CERROR("%s: can't create params log: rc = %d\n",
374                                obd->obd_name, rc);
375                         GOTO(out_sptlrpc, rc);
376                 }
377         }
378
379 #ifdef HAVE_SERVER_SUPPORT
380         if (!IS_MGS(lsi) && cfg->cfg_sub_clds & CONFIG_SUB_NODEMAP) {
381                 nodemap_cld = config_log_find_or_add(obd, LUSTRE_NODEMAP_NAME,
382                                                      NULL, MGS_CFG_T_NODEMAP,
383                                                      cfg);
384                 if (IS_ERR(nodemap_cld)) {
385                         rc = PTR_ERR(nodemap_cld);
386                         CERROR("%s: cannot create nodemap log: rc = %d\n",
387                                obd->obd_name, rc);
388                         GOTO(out_params, rc);
389                 }
390         }
391
392         if (IS_MDT(s2lsi(sb)) && cfg->cfg_sub_clds & CONFIG_SUB_BARRIER) {
393                 snprintf(seclogname + (ptr - logname), sizeof(seclogname) - 1,
394                          "-%s", BARRIER_FILENAME);
395                 barrier_cld = config_log_find_or_add(obd, seclogname, sb,
396                                                      MGS_CFG_T_BARRIER, cfg);
397                 if (IS_ERR(barrier_cld)) {
398                         rc = PTR_ERR(barrier_cld);
399                         CERROR("%s: can't create barrier log: rc = %d\n",
400                                obd->obd_name, rc);
401                         GOTO(out_nodemap, rc);
402                 }
403         }
404 #endif
405         cld = do_config_log_add(obd, logname, MGS_CFG_T_CONFIG, cfg, sb);
406         if (IS_ERR(cld)) {
407                 rc = PTR_ERR(cld);
408                 CERROR("%s: can't create log: rc = %d\n",
409                        obd->obd_name, rc);
410 #ifdef HAVE_SERVER_SUPPORT
411                 GOTO(out_barrier, rc);
412 #else
413                 GOTO(out_params, rc);
414 #endif
415         }
416
417         LASSERT(lsi->lsi_lmd);
418         if (!test_bit(LMD_FLG_NOIR, lsi->lsi_lmd->lmd_flags) &&
419             cfg->cfg_sub_clds & CONFIG_SUB_RECOVER) {
420                 struct config_llog_data *recover_cld;
421
422                 ptr = strrchr(seclogname, '-');
423                 if (ptr != NULL) {
424                         *ptr = 0;
425                 } else {
426                         CERROR("%s: sptlrpc log name not correct, %s: rc = %d\n",
427                                obd->obd_name, seclogname, -EINVAL);
428                         GOTO(out_cld, rc = -EINVAL);
429                 }
430
431                 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
432                 if (IS_ERR(recover_cld)) {
433                         rc = PTR_ERR(recover_cld);
434                         CERROR("%s: can't create recover log: rc = %d\n",
435                                obd->obd_name, rc);
436                         GOTO(out_cld, rc);
437                 }
438
439                 mutex_lock(&cld->cld_lock);
440                 locked = true;
441                 cld->cld_recover = recover_cld;
442         }
443
444         if (!locked)
445                 mutex_lock(&cld->cld_lock);
446 #ifdef HAVE_SERVER_SUPPORT
447         cld->cld_barrier = barrier_cld;
448         cld->cld_nodemap = nodemap_cld;
449 #endif
450         cld->cld_params = params_cld;
451         cld->cld_sptlrpc = sptlrpc_cld;
452         mutex_unlock(&cld->cld_lock);
453
454         RETURN(cld);
455
456 out_cld:
457         config_log_put(cld);
458 #ifdef HAVE_SERVER_SUPPORT
459 out_barrier:
460         config_log_put(barrier_cld);
461 out_nodemap:
462         config_log_put(nodemap_cld);
463 #endif
464 out_params:
465         config_log_put(params_cld);
466 out_sptlrpc:
467         config_log_put(sptlrpc_cld);
468 out_err:
469         return ERR_PTR(rc);
470 }
471
472 DEFINE_MUTEX(llog_process_lock);
473
474 static inline void config_mark_cld_stop_nolock(struct config_llog_data *cld)
475 {
476         ENTRY;
477
478         spin_lock(&config_list_lock);
479         cld->cld_stopping = 1;
480         spin_unlock(&config_list_lock);
481
482         CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
483         if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
484                 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
485 }
486
487 static inline void config_mark_cld_stop(struct config_llog_data *cld)
488 {
489         if (cld) {
490                 mutex_lock(&cld->cld_lock);
491                 config_mark_cld_stop_nolock(cld);
492                 mutex_unlock(&cld->cld_lock);
493         }
494 }
495
496 /** Stop watching for updates on this log.
497  */
498 static int config_log_end(char *logname, struct config_llog_instance *cfg)
499 {
500         struct config_llog_data *cld;
501         struct config_llog_data *cld_sptlrpc = NULL;
502         struct config_llog_data *cld_params = NULL;
503         struct config_llog_data *cld_recover = NULL;
504         struct config_llog_data *cld_nodemap = NULL;
505         struct config_llog_data *cld_barrier = NULL;
506         int rc = 0;
507
508         ENTRY;
509
510         cld = config_log_find(logname, cfg);
511         if (cld == NULL)
512                 RETURN(-ENOENT);
513
514         mutex_lock(&cld->cld_lock);
515         /*
516          * if cld_stopping is set, it means we didn't start the log thus
517          * not owning the start ref. this can happen after previous umount:
518          * the cld still hanging there waiting for lock cancel, and we
519          * remount again but failed in the middle and call log_end without
520          * calling start_log.
521          */
522         if (unlikely(cld->cld_stopping)) {
523                 mutex_unlock(&cld->cld_lock);
524                 /* drop the ref from the find */
525                 config_log_put(cld);
526                 RETURN(rc);
527         }
528
529         cld_recover = cld->cld_recover;
530         cld->cld_recover = NULL;
531         cld_params = cld->cld_params;
532         cld->cld_params = NULL;
533         cld_nodemap = cld->cld_nodemap;
534         cld->cld_nodemap = NULL;
535         cld_barrier = cld->cld_barrier;
536         cld->cld_barrier = NULL;
537         cld_sptlrpc = cld->cld_sptlrpc;
538         cld->cld_sptlrpc = NULL;
539
540         config_mark_cld_stop_nolock(cld);
541         mutex_unlock(&cld->cld_lock);
542
543         config_mark_cld_stop(cld_recover);
544         config_log_put(cld_recover);
545         config_mark_cld_stop(cld_params);
546         config_log_put(cld_params);
547         config_mark_cld_stop(cld_barrier);
548         config_log_put(cld_barrier);
549         /* don't explicitly set cld_stopping on sptlrpc lock here, as other
550          * targets may be active, it will be done in config_log_put if necessary
551          */
552         config_log_put(cld_sptlrpc);
553         /* don't set cld_stopping on nm lock as other targets may be active */
554         config_log_put(cld_nodemap);
555
556         /* drop the ref from the find */
557         config_log_put(cld);
558         /* drop the start ref */
559         config_log_put(cld);
560
561         CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
562                rc);
563         RETURN(rc);
564 }
565
566 int lprocfs_mgc_rd_ir_state(struct seq_file *m, void *data)
567 {
568         struct obd_device       *obd = data;
569         struct obd_import       *imp;
570         struct obd_connect_data *ocd;
571         struct config_llog_data *cld;
572         int rc = 0;
573
574         ENTRY;
575         LASSERT(obd);
576         with_imp_locked(obd, imp, rc) {
577                 ocd = &imp->imp_connect_data;
578
579                 seq_printf(m, "imperative_recovery: %s\n",
580                            OCD_HAS_FLAG(ocd, IMP_RECOV) ?
581                            "ENABLED" : "DISABLED");
582         }
583         if (rc)
584                 RETURN(rc);
585
586         seq_puts(m, "client_state:\n");
587
588         spin_lock(&config_list_lock);
589         list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
590                 if (cld->cld_recover == NULL)
591                         continue;
592                 seq_printf(m,  "    - { client: %s, nidtbl_version: %u }\n",
593                            cld->cld_logname,
594                            cld->cld_recover->cld_cfg.cfg_last_idx);
595         }
596         spin_unlock(&config_list_lock);
597
598         RETURN(0);
599 }
600
601 /* reenqueue any lost locks */
602 #define RQ_RUNNING      0x1
603 #define RQ_NOW          0x2
604 #define RQ_LATER        0x4
605 #define RQ_STOP         0x8
606 #define RQ_PRECLEANUP   0x10
607 static int                    rq_state;
608 static wait_queue_head_t      rq_waitq;
609 static DECLARE_COMPLETION(rq_exit);
610 static DECLARE_COMPLETION(rq_start);
611
612 static void do_requeue(struct config_llog_data *cld)
613 {
614         int rc = 0;
615
616         ENTRY;
617
618         /*
619          * Do not run mgc_process_log on a disconnected export or an
620          * export which is being disconnected. Take the client
621          * semaphore to make the check non-racy.
622          */
623         down_read_nested(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem,
624                          OBD_CLI_SEM_MGC);
625         if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
626                 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
627                 rc = mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
628                 if (rc && rc != -ENOENT)
629                         CERROR("failed processing log: %d\n", rc);
630         } else {
631                 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
632                        cld->cld_logname);
633         }
634         up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
635
636         EXIT;
637 }
638
639 static int mgc_requeue_thread(void *data)
640 {
641         int rc = 0;
642         bool first = true;
643
644         ENTRY;
645
646         CDEBUG(D_MGC, "Starting requeue thread\n");
647
648         /* Keep trying failed locks periodically */
649         spin_lock(&config_list_lock);
650         rq_state |= RQ_RUNNING;
651         while (!(rq_state & RQ_STOP)) {
652                 struct config_llog_data *cld, *cld_prev;
653                 int to;
654
655                 /* Any new or requeued lostlocks will change the state */
656                 rq_state &= ~(RQ_NOW | RQ_LATER);
657                 spin_unlock(&config_list_lock);
658
659                 if (first) {
660                         first = false;
661                         complete(&rq_start);
662                 }
663
664                 /* Always wait a few seconds to allow the server who
665                  * caused the lock revocation to finish its setup, plus some
666                  * random so everyone doesn't try to reconnect at once.
667                  */
668                 to = mgc_requeue_timeout_min == 0 ? 1 : mgc_requeue_timeout_min;
669                 to = cfs_time_seconds(mgc_requeue_timeout_min) +
670                         get_random_u32_below(cfs_time_seconds(to));
671                 wait_event_idle_timeout(rq_waitq,
672                                         rq_state & (RQ_STOP | RQ_PRECLEANUP),
673                                         to);
674
675                 /*
676                  * iterate & processing through the list. for each cld, process
677                  * its depending sptlrpc cld firstly (if any) and then itself.
678                  *
679                  * it's guaranteed any item in the list must have
680                  * reference > 0; and if cld_lostlock is set, at
681                  * least one reference is taken by the previous enqueue.
682                  */
683                 cld_prev = NULL;
684
685                 spin_lock(&config_list_lock);
686                 rq_state &= ~RQ_PRECLEANUP;
687                 list_for_each_entry(cld, &config_llog_list,
688                                     cld_list_chain) {
689                         if (!cld->cld_lostlock || cld->cld_stopping)
690                                 continue;
691
692                         /* hold reference to avoid being freed during
693                          * subsequent processing.
694                          */
695                         config_log_get(cld);
696                         cld->cld_lostlock = 0;
697                         spin_unlock(&config_list_lock);
698
699                         config_log_put(cld_prev);
700                         cld_prev = cld;
701
702                         if (likely(!(rq_state & RQ_STOP))) {
703                                 do_requeue(cld);
704                                 spin_lock(&config_list_lock);
705                         } else {
706                                 spin_lock(&config_list_lock);
707                                 break;
708                         }
709                 }
710                 spin_unlock(&config_list_lock);
711                 config_log_put(cld_prev);
712
713                 /* Wait a bit to see if anyone else needs a requeue */
714                 wait_event_idle(rq_waitq, rq_state & (RQ_NOW | RQ_STOP));
715                 spin_lock(&config_list_lock);
716         }
717
718         /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
719         rq_state &= ~RQ_RUNNING;
720         spin_unlock(&config_list_lock);
721
722         complete(&rq_exit);
723
724         CDEBUG(D_MGC, "Ending requeue thread\n");
725         RETURN(rc);
726 }
727
728 /* Add a cld to the list to requeue.  Start the requeue thread if needed.
729  * We are responsible for dropping the config log reference from here on out.
730  */
731 static void mgc_requeue_add(struct config_llog_data *cld)
732 {
733         bool wakeup = false;
734
735         ENTRY;
736
737         CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
738                 cld->cld_logname, refcount_read(&cld->cld_refcount),
739                 cld->cld_stopping, rq_state);
740
741         /* lets cancel an existent lock to mark cld as "lostlock" */
742         CDEBUG(D_INFO, "lockh %#llx\n", cld->cld_lockh.cookie);
743         if (!ldlm_lock_addref_try(&cld->cld_lockh, LCK_CR))
744                 ldlm_lock_decref_and_cancel(&cld->cld_lockh, LCK_CR);
745
746         mutex_lock(&cld->cld_lock);
747         spin_lock(&config_list_lock);
748         if (!(rq_state & RQ_STOP) && !cld->cld_stopping) {
749                 cld->cld_lostlock = 1;
750                 rq_state |= RQ_NOW;
751                 wakeup = true;
752         }
753         spin_unlock(&config_list_lock);
754         mutex_unlock(&cld->cld_lock);
755         if (wakeup)
756                 wake_up(&rq_waitq);
757
758         EXIT;
759 }
760
761 /********************** class fns **********************/
762 static int mgc_llog_init(const struct lu_env *env, struct obd_device *obd)
763 {
764         struct llog_ctxt        *ctxt;
765         int                      rc;
766
767         ENTRY;
768
769         /* setup only remote ctxt, the local disk context is switched per each
770          * filesystem during mgc_fs_setup()
771          */
772         rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_REPL_CTXT, obd,
773                         &llog_client_ops);
774         if (rc)
775                 RETURN(rc);
776
777         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
778         LASSERT(ctxt);
779
780         llog_initiator_connect(ctxt);
781         llog_ctxt_put(ctxt);
782
783         RETURN(0);
784 }
785
786 static int mgc_llog_fini(const struct lu_env *env, struct obd_device *obd)
787 {
788         struct llog_ctxt *ctxt;
789
790         ENTRY;
791
792         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
793         if (ctxt)
794                 llog_cleanup(env, ctxt);
795
796         RETURN(0);
797 }
798
799
800 static atomic_t mgc_count = ATOMIC_INIT(0);
801 static int mgc_precleanup(struct obd_device *obd)
802 {
803         int     rc = 0;
804         int     temp;
805
806         ENTRY;
807
808         if (atomic_dec_and_test(&mgc_count)) {
809                 LASSERT(rq_state & RQ_RUNNING);
810                 /* stop requeue thread */
811                 temp = RQ_STOP;
812         } else {
813                 /* wakeup requeue thread to clean our cld */
814                 temp = RQ_NOW | RQ_PRECLEANUP;
815         }
816
817         spin_lock(&config_list_lock);
818         rq_state |= temp;
819         spin_unlock(&config_list_lock);
820         wake_up(&rq_waitq);
821
822         if (temp & RQ_STOP)
823                 wait_for_completion(&rq_exit);
824         obd_cleanup_client_import(obd);
825
826         rc = mgc_llog_fini(NULL, obd);
827         if (rc != 0)
828                 CERROR("failed to cleanup llogging subsystems\n");
829
830         RETURN(rc);
831 }
832
833 static int mgc_cleanup(struct obd_device *obd)
834 {
835         int rc;
836
837         ENTRY;
838
839         /* COMPAT_146 - old config logs may have added profiles secretly */
840         if (atomic_read(&obd->obd_type->typ_refcnt) <= 1)
841                 /* Only for the last mgc */
842                 class_del_profiles();
843
844         lprocfs_obd_cleanup(obd);
845         ptlrpcd_decref();
846
847         rc = client_obd_cleanup(obd);
848         RETURN(rc);
849 }
850
851 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
852 {
853         struct task_struct      *task;
854         int                      rc;
855
856         ENTRY;
857
858         rc = ptlrpcd_addref();
859         if (rc < 0)
860                 RETURN(rc);
861
862         rc = client_obd_setup(obd, lcfg);
863         if (rc)
864                 GOTO(err_decref, rc);
865
866         rc = mgc_llog_init(NULL, obd);
867         if (rc) {
868                 CERROR("failed to setup llogging subsystems\n");
869                 GOTO(err_cleanup, rc);
870         }
871
872         rc = mgc_tunables_init(obd);
873         if (rc)
874                 GOTO(err_sysfs, rc);
875
876         if (atomic_inc_return(&mgc_count) == 1) {
877                 rq_state = 0;
878                 init_waitqueue_head(&rq_waitq);
879
880                 /* start requeue thread */
881                 task = kthread_run(mgc_requeue_thread, NULL, "ll_cfg_requeue");
882                 if (IS_ERR(task)) {
883                         rc = PTR_ERR(task);
884                         CERROR("%s: cannot start requeue thread: rc = %d; "
885                                "no more log updates\n",
886                                obd->obd_name, rc);
887                         GOTO(err_sysfs, rc);
888                 }
889                 /* rc is the task_struct pointer of mgc_requeue_thread. */
890                 rc = 0;
891                 wait_for_completion(&rq_start);
892         }
893
894         RETURN(rc);
895
896 err_sysfs:
897         lprocfs_obd_cleanup(obd);
898 err_cleanup:
899         client_obd_cleanup(obd);
900 err_decref:
901         ptlrpcd_decref();
902         RETURN(rc);
903 }
904
905 /* based on ll_mdc_blocking_ast */
906 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
907                             void *data, int flag)
908 {
909         struct lustre_handle lockh;
910         struct config_llog_data *cld = (struct config_llog_data *)data;
911         int rc = 0;
912
913         ENTRY;
914
915         switch (flag) {
916         case LDLM_CB_BLOCKING:
917                 /* mgs wants the lock, give it up... */
918                 LDLM_DEBUG(lock, "MGC blocking CB");
919                 ldlm_lock2handle(lock, &lockh);
920                 rc = ldlm_cli_cancel(&lockh, LCF_ASYNC);
921                 break;
922         case LDLM_CB_CANCELING:
923                 /* We've given up the lock, prepare ourselves to update. */
924                 LDLM_DEBUG(lock, "MGC cancel CB");
925
926                 CDEBUG(D_MGC, "Lock res "DLDLMRES" (%.8s)\n",
927                        PLDLMRES(lock->l_resource),
928                        (char *)&lock->l_resource->lr_name.name[0]);
929
930                 if (!cld) {
931                         CDEBUG(D_INFO, "missing data, won't requeue\n");
932                         break;
933                 }
934
935                 lock->l_ast_data = NULL;
936                 cld->cld_lockh.cookie = 0;
937                 /* Are we done with this log? */
938                 if (cld->cld_stopping) {
939                         CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
940                                 cld->cld_logname);
941                         config_log_put(cld);
942                         break;
943                 }
944                 /* Make sure not to re-enqueue when the mgc is stopping
945                  * (we get called from client_disconnect_export)
946                  */
947                 if (lock->l_conn_export == NULL ||
948                     lock->l_conn_export->exp_obd->u.cli.cl_conn_count == 0) {
949                         CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
950                                 cld->cld_logname);
951                         config_log_put(cld);
952                         break;
953                 }
954
955                 /* Re-enqueue now */
956                 mgc_requeue_add(cld);
957                 config_log_put(cld);
958                 break;
959         default:
960                 LBUG();
961         }
962
963         RETURN(rc);
964 }
965
966 /* Take a config lock so we can get cancel notifications */
967 static int mgc_enqueue(struct obd_export *exp, enum ldlm_type type,
968                        union ldlm_policy_data *policy, enum ldlm_mode mode,
969                        __u64 *flags, ldlm_glimpse_callback glimpse_callback,
970                        void *data, __u32 lvb_len, void *lvb_swabber,
971                        struct lustre_handle *lockh)
972 {
973         struct config_llog_data *cld = (struct config_llog_data *)data;
974         struct ldlm_enqueue_info einfo = {
975                 .ei_type        = type,
976                 .ei_mode        = mode,
977                 .ei_cb_bl       = mgc_blocking_ast,
978                 .ei_cb_cp       = ldlm_completion_ast,
979                 .ei_cb_gl       = glimpse_callback,
980         };
981         struct ptlrpc_request *req;
982         int short_limit = cld_is_sptlrpc(cld);
983         int rc;
984
985         ENTRY;
986
987         if (!exp)
988                 RETURN(-EBADR);
989
990         CDEBUG(D_MGC, "Enqueue for %s (res %#llx)\n", cld->cld_logname,
991                cld->cld_resid.name[0]);
992
993         /* We need a callback for every lockholder, so don't try to
994          * ldlm_lock_match (see rev 1.1.2.11.2.47)
995          */
996         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
997                                         &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
998                                         LDLM_ENQUEUE);
999         if (req == NULL)
1000                 RETURN(-ENOMEM);
1001
1002         req_capsule_set_size(&req->rq_pill, &RMF_DLM_LVB, RCL_SERVER, 0);
1003         ptlrpc_request_set_replen(req);
1004
1005         /* check if this is server or client */
1006         if (cld->cld_cfg.cfg_sb &&
1007             IS_SERVER(s2lsi(cld->cld_cfg.cfg_sb)))
1008                 short_limit = 1;
1009
1010         /* Limit how long we will wait for the enqueue to complete */
1011         req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT(exp->exp_obd);
1012         rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
1013                               NULL, 0, LVB_T_NONE, lockh, 0);
1014         /* A failed enqueue should still call the mgc_blocking_ast,
1015          * where it will be requeued if needed ("grant failed").
1016          */
1017         ptlrpc_req_finished(req);
1018         RETURN(rc);
1019 }
1020
1021 static int mgc_cancel(struct obd_export *exp, enum ldlm_mode mode,
1022                       struct lustre_handle *lockh)
1023 {
1024         ENTRY;
1025
1026         ldlm_lock_decref(lockh, mode);
1027
1028         RETURN(0);
1029 }
1030
1031 static void mgc_notify_active(struct obd_device *unused)
1032 {
1033         /* wakeup mgc_requeue_thread to requeue mgc lock */
1034         spin_lock(&config_list_lock);
1035         rq_state |= RQ_NOW;
1036         spin_unlock(&config_list_lock);
1037         wake_up(&rq_waitq);
1038
1039         /* TODO: Help the MGS rebuild nidtbl. -jay */
1040 }
1041
1042 static int mgc_set_info_async(const struct lu_env *env, struct obd_export *exp,
1043                               u32 keylen, void *key,
1044                               u32 vallen, void *val,
1045                               struct ptlrpc_request_set *set)
1046 {
1047         int rc = -EINVAL;
1048
1049         ENTRY;
1050
1051         /* Turn off initial_recov after we try all backup servers once */
1052         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1053                 struct obd_import *imp = class_exp2cliimp(exp);
1054                 int value;
1055
1056                 if (vallen != sizeof(int))
1057                         RETURN(-EINVAL);
1058                 value = *(int *)val;
1059                 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1060                        imp->imp_obd->obd_name, value,
1061                        imp->imp_deactive, imp->imp_invalid,
1062                        imp->imp_replayable, imp->imp_obd->obd_replayable,
1063                        ptlrpc_import_state_name(imp->imp_state));
1064                 /* Resurrect the import immediately if
1065                  * 1. we previously got disconnected,
1066                  * 2. value > 1 (at the same node with MGS)
1067                  */
1068                 if (value > 1) {
1069                         struct adaptive_timeout *at;
1070
1071                         at = &imp->imp_at.iat_net_latency;
1072                         at_reset(at, INITIAL_CONNECT_TIMEOUT);
1073                 }
1074
1075                 if (imp->imp_state == LUSTRE_IMP_DISCON || value > 1)
1076                         ptlrpc_reconnect_import(imp);
1077
1078                 RETURN(0);
1079         }
1080
1081         if (KEY_IS(KEY_MGSSEC)) {
1082                 struct client_obd     *cli = &exp->exp_obd->u.cli;
1083                 struct sptlrpc_flavor  flvr;
1084
1085                 /*
1086                  * empty string means using current flavor, if which haven't
1087                  * been set yet, set it as null.
1088                  *
1089                  * if flavor has been set previously, check the asking flavor
1090                  * must match the existing one.
1091                  */
1092                 if (vallen == 0) {
1093                         if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1094                                 RETURN(0);
1095                         val = "null";
1096                         vallen = 4;
1097                 }
1098
1099                 rc = sptlrpc_parse_flavor(val, &flvr);
1100                 if (rc) {
1101                         CERROR("invalid sptlrpc flavor %s to MGS\n",
1102                                (char *) val);
1103                         RETURN(rc);
1104                 }
1105
1106                 /* caller already hold a mutex */
1107                 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1108                         cli->cl_flvr_mgc = flvr;
1109                 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1110                                   sizeof(flvr)) != 0) {
1111                         char    str[20];
1112
1113                         sptlrpc_flavor2name(&cli->cl_flvr_mgc, str,
1114                                             sizeof(str));
1115                         LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but currently %s is in use\n",
1116                                        (char *) val, str);
1117                         rc = -EPERM;
1118                 }
1119                 RETURN(rc);
1120         }
1121
1122 #ifdef HAVE_SERVER_SUPPORT
1123         rc = mgc_set_info_async_server(env, exp, keylen, key, vallen, val, set);
1124 #endif
1125         RETURN(rc);
1126 }
1127
1128 static int mgc_get_info(const struct lu_env *env, struct obd_export *exp,
1129                         __u32 keylen, void *key, __u32 *vallen, void *val)
1130 {
1131         int rc = -EINVAL;
1132
1133         if (KEY_IS(KEY_CONN_DATA)) {
1134                 struct obd_import *imp = class_exp2cliimp(exp);
1135                 struct obd_connect_data *data = val;
1136
1137                 if (*vallen == sizeof(*data)) {
1138                         *data = imp->imp_connect_data;
1139                         rc = 0;
1140                 }
1141         }
1142
1143         return rc;
1144 }
1145
1146 static int mgc_import_event(struct obd_device *obd,
1147                             struct obd_import *imp,
1148                             enum obd_import_event event)
1149 {
1150         int rc = 0;
1151
1152         LASSERT(imp->imp_obd == obd);
1153         CDEBUG(D_MGC, "import event %#x\n", event);
1154
1155         switch (event) {
1156         case IMP_EVENT_DISCON:
1157                 /* MGC imports should not wait for recovery */
1158                 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1159                         ptlrpc_pinger_ir_down();
1160                 break;
1161         case IMP_EVENT_INACTIVE:
1162                 break;
1163         case IMP_EVENT_INVALIDATE: {
1164                 struct ldlm_namespace *ns = obd->obd_namespace;
1165
1166                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1167                 break;
1168         }
1169         case IMP_EVENT_ACTIVE:
1170                 CDEBUG(D_INFO, "%s: Reactivating import\n", obd->obd_name);
1171                 /* Clearing obd_no_recov allows us to continue pinging */
1172                 obd->obd_no_recov = 0;
1173                 mgc_notify_active(obd);
1174                 if (OCD_HAS_FLAG(&imp->imp_connect_data, IMP_RECOV))
1175                         ptlrpc_pinger_ir_up();
1176                 break;
1177         case IMP_EVENT_OCD:
1178                 break;
1179         case IMP_EVENT_DEACTIVATE:
1180         case IMP_EVENT_ACTIVATE:
1181                 break;
1182         default:
1183                 CERROR("Unknown import event %#x\n", event);
1184                 LBUG();
1185         }
1186         RETURN(rc);
1187 }
1188
1189 static int mgc_apply_recover_logs(struct obd_device *mgc,
1190                                   struct config_llog_data *cld,
1191                                   __u64 max_version,
1192                                   void *data, int datalen, bool mne_swab)
1193 {
1194         struct config_llog_instance *cfg = &cld->cld_cfg;
1195         struct lustre_cfg *lcfg;
1196         struct lustre_cfg_bufs bufs;
1197         u64 prev_version = 0;
1198         char inst[MTI_NAME_MAXLEN + 1];
1199         char *buf;
1200         int bufsz;
1201         int pos = 0;
1202         int rc  = 0;
1203         int off = 0;
1204         unsigned long dynamic_nids;
1205
1206         ENTRY;
1207         LASSERT(cfg->cfg_instance != 0);
1208         LASSERT(ll_get_cfg_instance(cfg->cfg_sb) == cfg->cfg_instance);
1209
1210         /* get dynamic nids setting */
1211         dynamic_nids = mgc->obd_dynamic_nids;
1212
1213         if (!IS_SERVER(s2lsi(cfg->cfg_sb))) {
1214                 pos = snprintf(inst, sizeof(inst), "%016lx", cfg->cfg_instance);
1215                 if (pos >= PAGE_SIZE)
1216                         return -E2BIG;
1217 #ifdef HAVE_SERVER_SUPPORT
1218         } else {
1219                 struct lustre_sb_info *lsi = s2lsi(cfg->cfg_sb);
1220
1221                 LASSERT(IS_MDT(lsi));
1222                 rc = server_name2svname(lsi->lsi_svname, inst, NULL,
1223                                         sizeof(inst));
1224                 if (rc)
1225                         RETURN(-EINVAL);
1226 #endif /* HAVE_SERVER_SUPPORT */
1227         }
1228
1229         OBD_ALLOC(buf, PAGE_SIZE);
1230         if (!buf)
1231                 return -ENOMEM;
1232         bufsz = PAGE_SIZE;
1233         pos = 0;
1234
1235         while (datalen > 0) {
1236                 struct mgs_nidtbl_entry *entry = (data + off);
1237                 struct lnet_nid *nidlist = NULL;
1238                 int entry_len = sizeof(*entry);
1239                 struct obd_device *obd;
1240                 struct obd_import *imp;
1241                 struct obd_uuid *uuid;
1242                 char *obdname;
1243                 char *cname;
1244                 char *params;
1245                 bool is_ost;
1246
1247                 rc = -EINVAL;
1248                 /* sanity checks */
1249                 if (datalen < entry_len) /* really short on data */
1250                         break;
1251
1252                 /* swab non nid data */
1253                 if (mne_swab)
1254                         lustre_swab_mgs_nidtbl_entry_header(entry);
1255
1256                 if (entry->mne_nid_count == 0) /* at least one nid entry */
1257                         break;
1258
1259                 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1260                 if (datalen < entry_len) /* must have entry_len at least */
1261                         break;
1262
1263                 if (entry->mne_length > PAGE_SIZE) {
1264                         CERROR("MNE too large (%u)\n", entry->mne_length);
1265                         break;
1266                 }
1267
1268                 /* improper mne_lenth */
1269                 if (entry->mne_length < entry_len)
1270                         break;
1271
1272                 /* entry length reports larger than all the data passed in */
1273                 if (datalen < entry->mne_length)
1274                         break;
1275
1276                 /* Looks OK. Can process this entry? else move to next entry */
1277                 off += entry->mne_length;
1278                 datalen -= entry->mne_length;
1279                 if (datalen < 0)
1280                         break;
1281
1282                 if (entry->mne_version > max_version) {
1283                         CERROR("entry index(%lld) is over max_index(%lld)\n",
1284                                entry->mne_version, max_version);
1285                         break;
1286                 }
1287
1288                 if (prev_version >= entry->mne_version) {
1289                         CERROR("index unsorted, prev %lld, now %lld\n",
1290                                prev_version, entry->mne_version);
1291                         break;
1292                 }
1293                 prev_version = entry->mne_version;
1294
1295                 if (entry->mne_nid_type == 0) {
1296                         struct lnet_nid *nid;
1297                         int i;
1298
1299                         OBD_ALLOC_PTR_ARRAY(nidlist, entry->mne_nid_count);
1300                         if (!nidlist) {
1301                                 rc = -ENOMEM;
1302                                 break;
1303                         }
1304
1305                         /* Keep this nid data swab for normal mixed
1306                          * endian handling. LU-1644
1307                          */
1308                         if (mne_swab)
1309                                 lustre_swab_mgs_nidtbl_entry_content(entry);
1310
1311                         /* Turn old NID format to newer format. */
1312                         nid = nidlist;
1313                         for (i = 0; i < entry->mne_nid_count; i++) {
1314                                 lnet_nid4_to_nid(entry->u.nids[i], nid);
1315                                 nid += sizeof(struct lnet_nid);
1316                         }
1317                 } else {
1318                         /* Handle the case if struct lnet_nid is expanded in
1319                          * the future. The MGS should prevent this but just
1320                          * in case.
1321                          */
1322                         if (entry->mne_nid_size > sizeof(struct lnet_nid))
1323                                 continue;
1324
1325                         nidlist = entry->u.nidlist;
1326                 }
1327
1328                 /*
1329                  * Write a string with format "nid::instance" to
1330                  * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1331                  */
1332                 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1333                 memset(buf, 0, bufsz);
1334                 obdname = buf;
1335                 pos = 0;
1336
1337                 /* lustre-OST0001-osc-<instance #> */
1338                 strcpy(obdname, cld->cld_logname);
1339                 cname = strrchr(obdname, '-');
1340                 if (cname == NULL) {
1341                         CERROR("mgc %s: invalid logname %s\n",
1342                                mgc->obd_name, obdname);
1343                         if (entry->mne_nid_type == 0)
1344                                 OBD_FREE_PTR_ARRAY(nidlist,
1345                                                    entry->mne_nid_count);
1346                         break;
1347                 }
1348
1349                 pos = cname - obdname;
1350                 obdname[pos] = 0;
1351                 pos += sprintf(obdname + pos, "-%s%04x",
1352                                is_ost ? "OST" : "MDT", entry->mne_index);
1353
1354                 cname = is_ost ? "osc" : "mdc",
1355                         pos += snprintf(obdname + pos, bufsz, "-%s-%s", cname,
1356                                         inst);
1357                 lustre_cfg_bufs_reset(&bufs, obdname);
1358
1359                 /* find the obd by obdname */
1360                 obd = class_name2obd(obdname);
1361                 if (obd == NULL) {
1362                         CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1363                                mgc->obd_name, obdname);
1364                         rc = 0;
1365                         /* this is a safe race, when the ost is starting up...*/
1366                         continue;
1367                 }
1368
1369                 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1370                 ++pos;
1371                 params = buf + pos;
1372                 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1373                 uuid = (struct obd_uuid *)(buf + pos);
1374
1375                 with_imp_locked(obd, imp, rc) {
1376                         struct obd_uuid server_uuid;
1377                         char *primary_nid;
1378                         int prim_nid_len;
1379
1380                         /* iterate all nids to find one */
1381                         /* find uuid by nid */
1382                         /* create import entries if they don't exist */
1383                         rc = client_import_add_nids_to_conn(imp, nidlist,
1384                                                            entry->mne_nid_count,
1385                                                            entry->mne_nid_size,
1386                                                            uuid);
1387                         if (rc != -ENOENT || !dynamic_nids)
1388                                 continue;
1389
1390                         /* create a new connection for this import */
1391                         primary_nid = libcfs_nidstr(&nidlist[0]);
1392                         prim_nid_len = strlen(primary_nid) + 1;
1393                         if (prim_nid_len > UUID_MAX)
1394                                 goto fail;
1395
1396                         strncpy(server_uuid.uuid, primary_nid,
1397                                 prim_nid_len);
1398
1399                         CDEBUG(D_INFO, "Adding a connection for %s\n",
1400                                primary_nid);
1401
1402                         rc = client_import_dyn_add_conn(imp, &server_uuid,
1403                                                         &nidlist[0], 1);
1404                         if (rc < 0) {
1405                                 CERROR("%s: Failed to add new connection with NID '%s' to import: rc = %d\n",
1406                                        obd->obd_name, primary_nid, rc);
1407                                 goto fail;
1408                         }
1409
1410                         rc = client_import_add_nids_to_conn(imp, nidlist,
1411                                                            entry->mne_nid_count,
1412                                                            entry->mne_nid_size,
1413                                                            uuid);
1414                         if (rc < 0)
1415                                 CERROR("%s: failed to lookup UUID: rc = %d\n",
1416                                        obd->obd_name, rc);
1417 fail:;
1418                 }
1419
1420                 if (rc == -ENODEV) {
1421                         /* client does not connect to the OST yet */
1422                         rc = 0;
1423                         goto free_nids;
1424                 }
1425
1426                 if (rc < 0 && rc != -ENOSPC) {
1427                         CERROR("mgc: cannot find UUID by nid '%s': rc = %d\n",
1428                                libcfs_nidstr(&nidlist[0]), rc);
1429
1430                         /* For old NID format case the nidlist was allocated. */
1431                         if (entry->mne_nid_type == 0)
1432                                 OBD_FREE_PTR_ARRAY(nidlist,
1433                                                    entry->mne_nid_count);
1434                         break;
1435                 }
1436
1437                 CDEBUG(D_INFO, "Found UUID '%s' by NID '%s'\n",
1438                        uuid->uuid, libcfs_nidstr(&nidlist[0]));
1439
1440                 pos += strlen(uuid->uuid);
1441                 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1442                 LASSERT(pos < bufsz);
1443
1444                 lustre_cfg_bufs_set_string(&bufs, 1, params);
1445
1446                 OBD_ALLOC(lcfg, lustre_cfg_len(bufs.lcfg_bufcount,
1447                                                bufs.lcfg_buflen));
1448                 if (!lcfg) {
1449                         rc = -ENOMEM;
1450                         break;
1451                 }
1452                 lustre_cfg_init(lcfg, LCFG_PARAM, &bufs);
1453
1454                 CDEBUG(D_INFO, "ir apply logs %lld/%lld for %s -> %s\n",
1455                        prev_version, max_version, obdname, params);
1456
1457                 rc = class_process_config(lcfg);
1458                 OBD_FREE(lcfg, lustre_cfg_len(lcfg->lcfg_bufcount,
1459                                               lcfg->lcfg_buflens));
1460                 if (rc)
1461                         CDEBUG(D_INFO, "process config for %s error %d\n",
1462                                obdname, rc);
1463
1464                 /* continue, even one with error */
1465 free_nids:
1466                 /* For old NID format case the nidlist was allocated. */
1467                 if (entry->mne_nid_type == 0)
1468                         OBD_FREE_PTR_ARRAY(nidlist, entry->mne_nid_count);
1469         }
1470
1471         OBD_FREE(buf, PAGE_SIZE);
1472
1473         RETURN(rc);
1474 }
1475
1476 /**
1477  * This function is called if this client was notified for target restarting
1478  * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery or
1479  * nodemap logs.
1480  */
1481 static int mgc_process_recover_log(struct obd_device *obd,
1482                                            struct config_llog_data *cld)
1483 {
1484         struct ptlrpc_request *req = NULL;
1485         struct config_llog_instance *cfg = &cld->cld_cfg;
1486         struct mgs_config_body *body;
1487         struct mgs_config_res *res;
1488         struct ptlrpc_bulk_desc *desc;
1489         struct page **pages = NULL;
1490         int nrpages = 0;
1491         bool eof = true;
1492         bool mne_swab = false;
1493         int i;
1494         int ealen;
1495         int rc;
1496
1497         ENTRY;
1498
1499         /* allocate buffer for bulk transfer.
1500          * if this is the first time for this mgs to read logs,
1501          * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1502          * once; otherwise, it only reads increment of logs, this should be
1503          * small and CONFIG_READ_NRPAGES will be used.
1504          */
1505         nrpages = CONFIG_READ_NRPAGES;
1506         if (cfg->cfg_last_idx == 0)
1507                 nrpages = CONFIG_READ_NRPAGES_INIT;
1508
1509         OBD_ALLOC_PTR_ARRAY_LARGE(pages, nrpages);
1510         if (pages == NULL)
1511                 GOTO(out, rc = -ENOMEM);
1512
1513         for (i = 0; i < nrpages; i++) {
1514                 pages[i] = alloc_page(GFP_KERNEL);
1515                 if (pages[i] == NULL)
1516                         GOTO(out, rc = -ENOMEM);
1517         }
1518
1519 again:
1520         LASSERT(cld_is_recover(cld));
1521         LASSERT(mutex_is_locked(&cld->cld_lock));
1522         req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1523                                    &RQF_MGS_CONFIG_READ);
1524         if (req == NULL)
1525                 GOTO(out, rc = -ENOMEM);
1526
1527         rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1528         if (rc)
1529                 GOTO(out, rc);
1530
1531         /* pack request */
1532         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1533         LASSERT(body != NULL);
1534         LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1535         rc = strscpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name));
1536         if (rc < 0)
1537                 GOTO(out, rc);
1538         body->mcb_offset = cfg->cfg_last_idx + 1;
1539         body->mcb_type = cld->cld_type;
1540         body->mcb_bits = PAGE_SHIFT;
1541         body->mcb_units = nrpages;
1542         body->mcb_rec_nid_size = sizeof(struct lnet_nid);
1543
1544         /* allocate bulk transfer descriptor */
1545         desc = ptlrpc_prep_bulk_imp(req, nrpages, 1,
1546                                     PTLRPC_BULK_PUT_SINK,
1547                                     MGS_BULK_PORTAL,
1548                                     &ptlrpc_bulk_kiov_pin_ops);
1549         if (desc == NULL)
1550                 GOTO(out, rc = -ENOMEM);
1551
1552         for (i = 0; i < nrpages; i++)
1553                 desc->bd_frag_ops->add_kiov_frag(desc, pages[i], 0,
1554                                                  PAGE_SIZE);
1555
1556         ptlrpc_request_set_replen(req);
1557         rc = ptlrpc_queue_wait(req);
1558         if (rc)
1559                 GOTO(out, rc);
1560
1561         res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1562         if (!res)
1563                 GOTO(out, rc = -EPROTO);
1564
1565         if (res->mcr_size < res->mcr_offset)
1566                 GOTO(out, rc = -EINVAL);
1567
1568         /* always update the index even though it might have errors with
1569          * handling the recover logs
1570          */
1571         cfg->cfg_last_idx = res->mcr_offset;
1572         eof = res->mcr_offset == res->mcr_size;
1573
1574         CDEBUG(D_INFO, "Latest version %lld, more %d.\n",
1575                res->mcr_offset, eof == false);
1576
1577         ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1578         if (ealen < 0)
1579                 GOTO(out, rc = ealen);
1580
1581         if (ealen > nrpages << PAGE_SHIFT)
1582                 GOTO(out, rc = -EINVAL);
1583
1584         if (ealen == 0) { /* no logs transferred */
1585                 if (!eof)
1586                         rc = -EINVAL;
1587                 GOTO(out, rc);
1588         }
1589
1590         mne_swab = req_capsule_rep_need_swab(&req->rq_pill);
1591
1592         for (i = 0; i < nrpages && ealen > 0; i++) {
1593                 int rc2;
1594                 union lu_page *ptr;
1595
1596                 ptr = kmap(pages[i]);
1597                 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1598                                              min_t(int, ealen, PAGE_SIZE),
1599                                              mne_swab);
1600                 kunmap(pages[i]);
1601                 if (rc2 < 0) {
1602                         CWARN("%s: error processing %s log recovery: rc = %d\n",
1603                               obd->obd_name,
1604                               cld->cld_logname,
1605                               rc2);
1606                         GOTO(out, rc = rc2);
1607                 }
1608
1609                 ealen -= PAGE_SIZE;
1610         }
1611
1612 out:
1613         if (req) {
1614                 ptlrpc_req_finished(req);
1615                 req = NULL;
1616         }
1617
1618         if (rc == 0 && !eof)
1619                 goto again;
1620
1621         if (pages) {
1622                 for (i = 0; i < nrpages; i++) {
1623                         if (pages[i] == NULL)
1624                                 break;
1625                         __free_page(pages[i]);
1626                 }
1627                 OBD_FREE_PTR_ARRAY_LARGE(pages, nrpages);
1628         }
1629         return rc;
1630 }
1631
1632 /* local_only means it cannot get remote llogs */
1633 static int mgc_process_cfg_log(struct obd_device *mgc,
1634                                struct config_llog_data *cld, int local_only)
1635 {
1636         struct llog_ctxt *ctxt;
1637         struct lustre_sb_info *lsi = NULL;
1638         int rc = 0;
1639         struct lu_env *env;
1640
1641         ENTRY;
1642         LASSERT(cld);
1643         LASSERT(mutex_is_locked(&cld->cld_lock));
1644
1645         if (cld->cld_cfg.cfg_sb)
1646                 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1647
1648         /* sptlrpc llog must not keep ref to sb,
1649          * it was just needed to get lsi
1650          */
1651         if (cld_is_sptlrpc(cld))
1652                 cld->cld_cfg.cfg_sb = NULL;
1653
1654         OBD_ALLOC_PTR(env);
1655         if (!env)
1656                 RETURN(-ENOMEM);
1657
1658         rc = lu_env_init(env, LCT_MG_THREAD);
1659         if (rc)
1660                 GOTO(out_free, rc);
1661
1662         ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1663         LASSERT(ctxt);
1664 #ifdef HAVE_SERVER_SUPPORT
1665         rc = mgc_process_server_cfg_log(env, &ctxt, lsi, mgc, cld,
1666                                         local_only);
1667         if (rc == -EIO && local_only)
1668                 GOTO(out_pop, rc);
1669         if (rc && rc != -ENOENT)
1670 #else
1671         if (local_only)
1672                 GOTO(out_pop, rc = -EIO);
1673 #endif
1674                 rc = class_config_parse_llog(env, ctxt, cld->cld_logname,
1675                                              &cld->cld_cfg);
1676
1677         /*
1678          * update settings on existing OBDs.
1679          * the logname must be <fsname>-sptlrpc
1680          */
1681         if (rc == 0 && cld_is_sptlrpc(cld))
1682                 class_notify_sptlrpc_conf(cld->cld_logname,
1683                                           strlen(cld->cld_logname) -
1684                                           strlen("-sptlrpc"));
1685         EXIT;
1686
1687 out_pop:
1688         __llog_ctxt_put(env, ctxt);
1689         lu_env_fini(env);
1690 out_free:
1691         OBD_FREE_PTR(env);
1692         return rc;
1693 }
1694
1695 static bool mgc_import_in_recovery(struct obd_import *imp)
1696 {
1697         bool in_recovery = true;
1698
1699         spin_lock(&imp->imp_lock);
1700         if (imp->imp_state == LUSTRE_IMP_FULL ||
1701             imp->imp_state == LUSTRE_IMP_CLOSED)
1702                 in_recovery = false;
1703         spin_unlock(&imp->imp_lock);
1704
1705         return in_recovery;
1706 }
1707
1708 /**
1709  * Get a configuration log from the MGS and process it.
1710  *
1711  * This function is called for both clients and servers to process the
1712  * configuration log from the MGS.  The MGC enqueues a DLM lock on the
1713  * log from the MGS, and if the lock gets revoked the MGC will be notified
1714  * by the lock cancellation callback that the config log has changed,
1715  * and will enqueue another MGS lock on it, and then continue processing
1716  * the new additions to the end of the log.
1717  *
1718  * Since the MGC import is not replayable, if the import is being evicted
1719  * (rcl == -ESHUTDOWN, \see ptlrpc_import_delay_req()), retry to process
1720  * the log until recovery is finished or the import is closed.
1721  *
1722  * Make a local copy of the log before parsing it if appropriate (non-MGS
1723  * server) so that the server can start even when the MGS is down.
1724  *
1725  * There shouldn't be multiple processes running process_log at once --
1726  * sounds like badness.  It actually might be fine, as long as they're not
1727  * trying to update from the same log simultaneously, in which case we
1728  * should use a per-log semaphore instead of cld_lock.
1729  *
1730  * \param[in] mgc       MGC device by which to fetch the configuration log
1731  * \param[in] cld       log processing state (stored in lock callback data)
1732  *
1733  * \retval              0 on success
1734  * \retval              negative errno on failure
1735  */
1736 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1737 {
1738         struct lustre_handle lockh = { 0 };
1739         __u64 flags = LDLM_FL_NO_LRU;
1740         int rc = 0, rcl;
1741         bool retry = false;
1742
1743         ENTRY;
1744
1745         LASSERT(cld != NULL);
1746
1747         /* I don't want multiple processes running process_log at once --
1748          * sounds like badness.  It actually might be fine, as long as
1749          * we're not trying to update from the same log
1750          * simultaneously (in which case we should use a per-log sem.)
1751          */
1752 restart:
1753         mutex_lock(&cld->cld_lock);
1754         if (cld->cld_stopping) {
1755                 mutex_unlock(&cld->cld_lock);
1756                 RETURN(0);
1757         }
1758
1759         CFS_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1760
1761         CDEBUG(D_MGC, "Process log %s-%016lx from %d\n", cld->cld_logname,
1762                cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1763
1764         /* Get the cfg lock on the llog */
1765         rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, LDLM_PLAIN, NULL,
1766                           LCK_CR, &flags,
1767                           cld_is_barrier(cld) ? mgc_barrier_glimpse_ast : NULL,
1768                           cld, 0, NULL, &lockh);
1769         if (rcl == 0) {
1770                 /* Get the cld, it will be released in mgc_blocking_ast. */
1771                 config_log_get(cld);
1772                 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1773                 LASSERT(!lustre_handle_is_used(&cld->cld_lockh));
1774                 LASSERT(rc == 0);
1775                 cld->cld_lockh = lockh;
1776         } else {
1777                 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1778                 cld->cld_lockh.cookie = 0;
1779
1780                 if (rcl == -ESHUTDOWN &&
1781                     atomic_read(&mgc->u.cli.cl_mgc_refcount) > 0 && !retry) {
1782                         struct obd_import *imp;
1783                         long timeout = cfs_time_seconds(obd_timeout);
1784
1785                         mutex_unlock(&cld->cld_lock);
1786                         imp = class_exp2cliimp(mgc->u.cli.cl_mgc_mgsexp);
1787
1788                         /* Let's force the pinger, and wait the import to be
1789                          * connected, note: since mgc import is non-replayable,
1790                          * and even the import state is disconnected, it does
1791                          * not mean the "recovery" is stopped, so we will keep
1792                          * waitting until timeout or the import state is
1793                          * FULL or closed
1794                          */
1795                         ptlrpc_pinger_force(imp);
1796
1797                         wait_event_idle_timeout(imp->imp_recovery_waitq,
1798                                                 !mgc_import_in_recovery(imp),
1799                                                 timeout);
1800
1801                         if (imp->imp_state == LUSTRE_IMP_FULL) {
1802                                 retry = true;
1803                                 goto restart;
1804                         } else {
1805                                 mutex_lock(&cld->cld_lock);
1806                                 /* unlock/lock mutex, so check stopping again */
1807                                 if (cld->cld_stopping) {
1808                                         mutex_unlock(&cld->cld_lock);
1809                                         RETURN(0);
1810                                 }
1811                                 spin_lock(&config_list_lock);
1812                                 cld->cld_lostlock = 1;
1813                                 spin_unlock(&config_list_lock);
1814                         }
1815                 } else {
1816                         /* mark cld_lostlock so that it will requeue
1817                          * after MGC becomes available.
1818                          */
1819                         spin_lock(&config_list_lock);
1820                         cld->cld_lostlock = 1;
1821                         spin_unlock(&config_list_lock);
1822                 }
1823         }
1824
1825         if (cld_is_recover(cld) && !rcl)
1826                 rc = mgc_process_recover_log(mgc, cld);
1827 #ifdef HAVE_SERVER_SUPPORT
1828         else if (cld_is_nodemap(cld)) {
1829                 if (rcl)
1830                         rc = rcl;
1831                 else
1832                         rc = mgc_process_nodemap_log(mgc, cld);
1833         }
1834 #endif
1835         else if (!cld_is_barrier(cld))
1836                 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1837
1838         CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1839                mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1840         if (rc != -ETIMEDOUT && rc != -EIO && rc != -EAGAIN) {
1841                 cld->cld_processed = 1;
1842                 wake_up(&rq_waitq);
1843         }
1844
1845         /* Now drop the lock so MGS can revoke it */
1846         if (!rcl) {
1847                 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, LCK_CR, &lockh);
1848                 if (rcl)
1849                         CERROR("Can't drop cfg lock: %d\n", rcl);
1850         }
1851         mutex_unlock(&cld->cld_lock);
1852
1853         /* requeue nodemap lock immediately if transfer was interrupted */
1854         if ((cld_is_nodemap(cld) && rc == -EAGAIN) ||
1855             (cld_is_recover(cld) && rc)) {
1856                 if (cld_is_recover(cld))
1857                         CWARN("%s: IR log %s failed, not fatal: rc = %d\n",
1858                               mgc->obd_name, cld->cld_logname, rc);
1859                 mgc_requeue_add(cld);
1860                 rc = 0;
1861         }
1862
1863         RETURN(rc);
1864 }
1865
1866
1867 /* Called from lustre_process_log.
1868  * LCFG_LOG_START gets the config log from the MGS, processes it to start
1869  * any services, and adds it to the list logs to watch (follow).
1870  */
1871 static int mgc_process_config(struct obd_device *obd, size_t len, void *buf)
1872 {
1873         struct lustre_cfg *lcfg = buf;
1874         struct config_llog_instance *cfg = NULL;
1875         char *logname;
1876         int rc;
1877
1878         ENTRY;
1879 #ifdef HAVE_SERVER_SUPPORT
1880         rc = mgc_process_config_server(obd, len, buf);
1881         if (rc != -ENOENT)
1882                 RETURN(rc);
1883 #endif
1884
1885         switch (lcfg->lcfg_command) {
1886         case LCFG_SPTLRPC_CONF: {
1887                 rc = sptlrpc_process_config(lcfg);
1888                 break;
1889         }
1890         case LCFG_LOG_START: {
1891                 struct config_llog_data *cld;
1892                 struct super_block *sb;
1893
1894                 logname = lustre_cfg_string(lcfg, 1);
1895                 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1896                 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1897
1898                 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1899                        cfg->cfg_last_idx);
1900
1901                 /* We're only called through here on the initial mount */
1902                 cld = config_log_add(obd, logname, cfg, sb);
1903                 if (IS_ERR(cld)) {
1904                         rc = PTR_ERR(cld);
1905                         break;
1906                 }
1907
1908                 /* if it exists, the sptlrpc config log really needs to be
1909                  * correctly processed before processing other logs,
1910                  * otherwise client might use incorrect sec flavor
1911                  */
1912                 if (cld->cld_sptlrpc && !cld->cld_sptlrpc->cld_processed) {
1913                         unsigned int timeout = 120;
1914
1915                         /* we do not want to wait forever,
1916                          * we prefer a (excessively) long timeout
1917                          */
1918                         timeout = max(20 * mgc_requeue_timeout_min, timeout);
1919                         wait_event_idle_timeout(rq_waitq,
1920                                                 cld->cld_sptlrpc->cld_processed,
1921                                                 cfs_time_seconds(timeout));
1922                 }
1923
1924                 rc = mgc_process_log(obd, cld);
1925                 if (rc == 0 && cld->cld_recover != NULL) {
1926                         if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1927                                          imp_connect_data, IMP_RECOV)) {
1928                                 rc = mgc_process_log(obd, cld->cld_recover);
1929                         } else {
1930                                 struct config_llog_data *cir;
1931
1932                                 mutex_lock(&cld->cld_lock);
1933                                 cir = cld->cld_recover;
1934                                 cld->cld_recover = NULL;
1935                                 mutex_unlock(&cld->cld_lock);
1936                                 config_log_put(cir);
1937                         }
1938
1939                         if (rc)
1940                                 CERROR("Cannot process recover llog %d\n", rc);
1941                 }
1942
1943                 if (rc == 0 && cld->cld_params != NULL) {
1944                         rc = mgc_process_log(obd, cld->cld_params);
1945                         if (rc == -ENOENT) {
1946                                 CDEBUG(D_MGC,
1947                                        "There is no params config file yet\n");
1948                                 rc = 0;
1949                         }
1950                         /* params log is optional */
1951                         if (rc)
1952                                 CERROR("%s: can't process params llog: rc = %d\n",
1953                                        obd->obd_name, rc);
1954                 }
1955
1956                 break;
1957         }
1958         case LCFG_LOG_END: {
1959                 logname = lustre_cfg_string(lcfg, 1);
1960
1961                 if (lcfg->lcfg_bufcount >= 2)
1962                         cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1963                 rc = config_log_end(logname, cfg);
1964                 break;
1965         }
1966         default:
1967                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1968                 rc = -EINVAL;
1969                 break;
1970         }
1971
1972         RETURN(rc);
1973 }
1974
1975 static const struct obd_ops mgc_obd_ops = {
1976         .o_owner        = THIS_MODULE,
1977         .o_setup        = mgc_setup,
1978         .o_precleanup   = mgc_precleanup,
1979         .o_cleanup      = mgc_cleanup,
1980         .o_add_conn     = client_import_add_conn,
1981         .o_del_conn     = client_import_del_conn,
1982         .o_connect      = client_connect_import,
1983         .o_disconnect   = client_disconnect_export,
1984         .o_set_info_async = mgc_set_info_async,
1985         .o_get_info       = mgc_get_info,
1986         .o_import_event = mgc_import_event,
1987         .o_process_config = mgc_process_config,
1988 };
1989
1990 static int mgc_param_requeue_timeout_min_set(const char *val,
1991                                      cfs_kernel_param_arg_t *kp)
1992 {
1993         int rc;
1994         unsigned int num;
1995
1996         rc = kstrtouint(val, 0, &num);
1997         if (rc < 0)
1998                 return rc;
1999         if (num > 120)
2000                 return -EINVAL;
2001
2002         mgc_requeue_timeout_min = num;
2003
2004         return 0;
2005 }
2006
2007 static const struct kernel_param_ops param_ops_requeue_timeout_min = {
2008         .set = mgc_param_requeue_timeout_min_set,
2009         .get = param_get_uint,
2010 };
2011
2012 #define param_check_requeue_timeout_min(name, p) \
2013                 __param_check(name, p, unsigned int)
2014
2015 unsigned int mgc_requeue_timeout_min = MGC_TIMEOUT_MIN_SECONDS;
2016 #ifdef HAVE_KERNEL_PARAM_OPS
2017 module_param(mgc_requeue_timeout_min, requeue_timeout_min, 0644);
2018 #else
2019 module_param_call(mgc_requeue_timeout_min, mgc_param_requeue_timeout_min_set,
2020                   param_get_uint, &param_ops_requeue_timeout_min, 0644);
2021 #endif
2022 MODULE_PARM_DESC(mgc_requeue_timeout_min, "Minimal requeue time to refresh logs");
2023
2024 static int __init mgc_init(void)
2025 {
2026         int rc;
2027
2028         rc = libcfs_setup();
2029         if (rc)
2030                 return rc;
2031
2032         return class_register_type(&mgc_obd_ops, NULL, false,
2033                                    LUSTRE_MGC_NAME, NULL);
2034 }
2035
2036 static void __exit mgc_exit(void)
2037 {
2038         class_unregister_type(LUSTRE_MGC_NAME);
2039 }
2040
2041 MODULE_AUTHOR("OpenSFS, Inc. <http://www.lustre.org/>");
2042 MODULE_DESCRIPTION("Lustre Management Client");
2043 MODULE_VERSION(LUSTRE_VERSION_STRING);
2044 MODULE_LICENSE("GPL");
2045
2046 module_init(mgc_init);
2047 module_exit(mgc_exit);