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