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