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