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