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