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