Whamcloud - gitweb
f4c7d13f67d76187773e244a810913704a1dc3b5
[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 int lprocfs_mgc_rd_ir_state(char *page, char **start, off_t off,
417                             int count, int *eof, void *data)
418 {
419         struct obd_device       *obd = data;
420         struct obd_import       *imp = obd->u.cli.cl_import;
421         struct obd_connect_data *ocd = &imp->imp_connect_data;
422         struct config_llog_data *cld;
423         int rc = 0;
424         ENTRY;
425
426         rc = snprintf(page, count, "imperative_recovery: %s\n",
427                       OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ON" : "OFF");
428         rc += snprintf(page + rc, count - rc, "client_state:\n");
429
430         cfs_spin_lock(&config_list_lock);
431         cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
432                 if (cld->cld_recover == NULL)
433                         continue;
434                 rc += snprintf(page + rc, count - rc,
435                                "    - { client: %s, nidtbl_version: %u }\n",
436                                cld->cld_logname,
437                                cld->cld_recover->cld_cfg.cfg_last_idx);
438         }
439         cfs_spin_unlock(&config_list_lock);
440
441         RETURN(rc);
442 }
443
444 /* reenqueue any lost locks */
445 #define RQ_RUNNING 0x1
446 #define RQ_NOW     0x2
447 #define RQ_LATER   0x4
448 #define RQ_STOP    0x8
449 static int                    rq_state = 0;
450 static cfs_waitq_t            rq_waitq;
451 static CFS_DECLARE_COMPLETION(rq_exit);
452
453 static void do_requeue(struct config_llog_data *cld)
454 {
455         ENTRY;
456         LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
457
458         /* Do not run mgc_process_log on a disconnected export or an
459            export which is being disconnected. Take the client
460            semaphore to make the check non-racy. */
461         cfs_down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
462         if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
463                 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
464                 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
465         } else {
466                 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
467                        cld->cld_logname);
468         }
469         cfs_up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
470
471         EXIT;
472 }
473
474 /* this timeout represents how many seconds MGC should wait before
475  * requeue config and recover lock to the MGS. We need to randomize this
476  * in order to not flood the MGS.
477  */
478 #define MGC_TIMEOUT_MIN_SECONDS   5
479 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
480
481 static int mgc_requeue_thread(void *data)
482 {
483         char name[] = "ll_cfg_requeue";
484         int rc = 0;
485         ENTRY;
486
487         cfs_daemonize(name);
488
489         CDEBUG(D_MGC, "Starting requeue thread\n");
490
491         /* Keep trying failed locks periodically */
492         cfs_spin_lock(&config_list_lock);
493         rq_state |= RQ_RUNNING;
494         while (1) {
495                 struct l_wait_info lwi;
496                 struct config_llog_data *cld, *cld_prev;
497                 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
498                 int stopped = !!(rq_state & RQ_STOP);
499                 int to;
500
501                 /* Any new or requeued lostlocks will change the state */
502                 rq_state &= ~(RQ_NOW | RQ_LATER);
503                 cfs_spin_unlock(&config_list_lock);
504
505                 /* Always wait a few seconds to allow the server who
506                    caused the lock revocation to finish its setup, plus some
507                    random so everyone doesn't try to reconnect at once. */
508                 to = MGC_TIMEOUT_MIN_SECONDS * CFS_HZ;
509                 to += rand * CFS_HZ / 100; /* rand is centi-seconds */
510                 lwi = LWI_TIMEOUT(to, NULL, NULL);
511                 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
512
513                 /*
514                  * iterate & processing through the list. for each cld, process
515                  * its depending sptlrpc cld firstly (if any) and then itself.
516                  *
517                  * it's guaranteed any item in the list must have
518                  * reference > 0; and if cld_lostlock is set, at
519                  * least one reference is taken by the previous enqueue.
520                  */
521                 cld_prev = NULL;
522
523                 cfs_spin_lock(&config_list_lock);
524                 cfs_list_for_each_entry(cld, &config_llog_list,
525                                         cld_list_chain) {
526                         if (!cld->cld_lostlock)
527                                 continue;
528
529                         cfs_spin_unlock(&config_list_lock);
530
531                         LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
532
533                         /* Whether we enqueued again or not in mgc_process_log,
534                          * we're done with the ref from the old enqueue */
535                         if (cld_prev)
536                                 config_log_put(cld_prev);
537                         cld_prev = cld;
538
539                         cld->cld_lostlock = 0;
540                         if (likely(!stopped))
541                                 do_requeue(cld);
542
543                         cfs_spin_lock(&config_list_lock);
544                 }
545                 cfs_spin_unlock(&config_list_lock);
546                 if (cld_prev)
547                         config_log_put(cld_prev);
548
549                 /* break after scanning the list so that we can drop
550                  * refcount to losing lock clds */
551                 if (unlikely(stopped)) {
552                         cfs_spin_lock(&config_list_lock);
553                         break;
554                 }
555
556                 /* Wait a bit to see if anyone else needs a requeue */
557                 lwi = (struct l_wait_info) { 0 };
558                 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
559                              &lwi);
560                 cfs_spin_lock(&config_list_lock);
561         }
562         /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
563         rq_state &= ~RQ_RUNNING;
564         cfs_spin_unlock(&config_list_lock);
565
566         cfs_complete(&rq_exit);
567
568         CDEBUG(D_MGC, "Ending requeue thread\n");
569         RETURN(rc);
570 }
571
572 /* Add a cld to the list to requeue.  Start the requeue thread if needed.
573    We are responsible for dropping the config log reference from here on out. */
574 static void mgc_requeue_add(struct config_llog_data *cld)
575 {
576         ENTRY;
577
578         CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
579                cld->cld_logname, cfs_atomic_read(&cld->cld_refcount),
580                cld->cld_stopping, rq_state);
581         LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
582
583         cfs_mutex_lock(&cld->cld_lock);
584         if (cld->cld_stopping || cld->cld_lostlock) {
585                 cfs_mutex_unlock(&cld->cld_lock);
586                 RETURN_EXIT;
587         }
588         /* this refcount will be released in mgc_requeue_thread. */
589         config_log_get(cld);
590         cld->cld_lostlock = 1;
591         cfs_mutex_unlock(&cld->cld_lock);
592
593         /* Hold lock for rq_state */
594         cfs_spin_lock(&config_list_lock);
595         if (rq_state & RQ_STOP) {
596                 cfs_spin_unlock(&config_list_lock);
597                 cld->cld_lostlock = 0;
598                 config_log_put(cld);
599         } else {
600                 rq_state |= RQ_NOW;
601                 cfs_spin_unlock(&config_list_lock);
602                 cfs_waitq_signal(&rq_waitq);
603         }
604         EXIT;
605 }
606
607 /********************** class fns **********************/
608
609 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
610                         struct vfsmount *mnt)
611 {
612         struct lvfs_run_ctxt saved;
613         struct lustre_sb_info *lsi = s2lsi(sb);
614         struct client_obd *cli = &obd->u.cli;
615         struct dentry *dentry;
616         char *label;
617         int err = 0;
618         ENTRY;
619
620         LASSERT(lsi);
621         LASSERT(lsi->lsi_srv_mnt == mnt);
622
623         /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
624         cfs_down(&cli->cl_mgc_sem);
625
626         cfs_cleanup_group_info();
627
628         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
629         if (IS_ERR(obd->obd_fsops)) {
630                 cfs_up(&cli->cl_mgc_sem);
631                 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
632                        PTR_ERR(obd->obd_fsops));
633                 RETURN(PTR_ERR(obd->obd_fsops));
634         }
635
636         cli->cl_mgc_vfsmnt = mnt;
637         fsfilt_setup(obd, mnt->mnt_sb);
638
639         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
640         obd->obd_lvfs_ctxt.pwdmnt = mnt;
641         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
642         obd->obd_lvfs_ctxt.fs = get_ds();
643
644         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
645         dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
646                                    strlen(MOUNT_CONFIGS_DIR));
647         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
648         if (IS_ERR(dentry)) {
649                 err = PTR_ERR(dentry);
650                 CERROR("cannot lookup %s directory: rc = %d\n",
651                        MOUNT_CONFIGS_DIR, err);
652                 GOTO(err_ops, err);
653         }
654         cli->cl_mgc_configs_dir = dentry;
655
656         /* We take an obd ref to insure that we can't get to mgc_cleanup
657            without calling mgc_fs_cleanup first. */
658         class_incref(obd, "mgc_fs", obd);
659
660         label = fsfilt_get_label(obd, mnt->mnt_sb);
661         if (label)
662                 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
663
664         /* We keep the cl_mgc_sem until mgc_fs_cleanup */
665         RETURN(0);
666
667 err_ops:
668         fsfilt_put_ops(obd->obd_fsops);
669         obd->obd_fsops = NULL;
670         cli->cl_mgc_vfsmnt = NULL;
671         cfs_up(&cli->cl_mgc_sem);
672         RETURN(err);
673 }
674
675 static int mgc_fs_cleanup(struct obd_device *obd)
676 {
677         struct client_obd *cli = &obd->u.cli;
678         int rc = 0;
679         ENTRY;
680
681         LASSERT(cli->cl_mgc_vfsmnt != NULL);
682
683         if (cli->cl_mgc_configs_dir != NULL) {
684                 struct lvfs_run_ctxt saved;
685                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
686                 l_dput(cli->cl_mgc_configs_dir);
687                 cli->cl_mgc_configs_dir = NULL;
688                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
689                 class_decref(obd, "mgc_fs", obd);
690         }
691
692         cli->cl_mgc_vfsmnt = NULL;
693         if (obd->obd_fsops)
694                 fsfilt_put_ops(obd->obd_fsops);
695
696         cfs_up(&cli->cl_mgc_sem);
697
698         RETURN(rc);
699 }
700
701 static cfs_atomic_t mgc_count = CFS_ATOMIC_INIT(0);
702 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
703 {
704         int rc = 0;
705         ENTRY;
706
707         switch (stage) {
708         case OBD_CLEANUP_EARLY:
709                 break;
710         case OBD_CLEANUP_EXPORTS:
711                 if (cfs_atomic_dec_and_test(&mgc_count)) {
712                         int running;
713                         /* stop requeue thread */
714                         cfs_spin_lock(&config_list_lock);
715                         running = rq_state & RQ_RUNNING;
716                         if (running)
717                                 rq_state |= RQ_STOP;
718                         cfs_spin_unlock(&config_list_lock);
719                         if (running) {
720                                 cfs_waitq_signal(&rq_waitq);
721                                 cfs_wait_for_completion(&rq_exit);
722                         }
723                 }
724                 obd_cleanup_client_import(obd);
725                 rc = obd_llog_finish(obd, 0);
726                 if (rc != 0)
727                         CERROR("failed to cleanup llogging subsystems\n");
728                 break;
729         }
730         RETURN(rc);
731 }
732
733 static int mgc_cleanup(struct obd_device *obd)
734 {
735         struct client_obd *cli = &obd->u.cli;
736         int rc;
737         ENTRY;
738
739         LASSERT(cli->cl_mgc_vfsmnt == NULL);
740
741         /* COMPAT_146 - old config logs may have added profiles we don't
742            know about */
743         if (obd->obd_type->typ_refcnt <= 1)
744                 /* Only for the last mgc */
745                 class_del_profiles();
746
747         lprocfs_obd_cleanup(obd);
748         ptlrpcd_decref();
749
750         rc = client_obd_cleanup(obd);
751         RETURN(rc);
752 }
753
754 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
755 {
756         struct lprocfs_static_vars lvars;
757         int rc;
758         ENTRY;
759
760         ptlrpcd_addref();
761
762         rc = client_obd_setup(obd, lcfg);
763         if (rc)
764                 GOTO(err_decref, rc);
765
766         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
767         if (rc) {
768                 CERROR("failed to setup llogging subsystems\n");
769                 GOTO(err_cleanup, rc);
770         }
771
772         lprocfs_mgc_init_vars(&lvars);
773         lprocfs_obd_setup(obd, lvars.obd_vars);
774         sptlrpc_lprocfs_cliobd_attach(obd);
775
776         if (cfs_atomic_inc_return(&mgc_count) == 1) {
777                 rq_state = 0;
778                 cfs_waitq_init(&rq_waitq);
779
780                 /* start requeue thread */
781                 rc = cfs_create_thread(mgc_requeue_thread, NULL,
782                                        CFS_DAEMON_FLAGS);
783                 if (rc < 0) {
784                         CERROR("%s: Cannot start requeue thread (%d),"
785                                "no more log updates!\n",
786                                obd->obd_name, rc);
787                         GOTO(err_cleanup, rc);
788                 }
789                 /* rc is the pid of mgc_requeue_thread. */
790                 rc = 0;
791         }
792
793         RETURN(rc);
794
795 err_cleanup:
796         client_obd_cleanup(obd);
797 err_decref:
798         ptlrpcd_decref();
799         RETURN(rc);
800 }
801
802 /* based on ll_mdc_blocking_ast */
803 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
804                             void *data, int flag)
805 {
806         struct lustre_handle lockh;
807         struct config_llog_data *cld = (struct config_llog_data *)data;
808         int rc = 0;
809         ENTRY;
810
811         switch (flag) {
812         case LDLM_CB_BLOCKING:
813                 /* mgs wants the lock, give it up... */
814                 LDLM_DEBUG(lock, "MGC blocking CB");
815                 ldlm_lock2handle(lock, &lockh);
816                 rc = ldlm_cli_cancel(&lockh);
817                 break;
818         case LDLM_CB_CANCELING:
819                 /* We've given up the lock, prepare ourselves to update. */
820                 LDLM_DEBUG(lock, "MGC cancel CB");
821
822                 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
823                        lock->l_resource->lr_name.name[0],
824                        (char *)&lock->l_resource->lr_name.name[0]);
825
826                 if (!cld) {
827                         CDEBUG(D_INFO, "missing data, won't requeue\n");
828                         break;
829                 }
830
831                 /* held at mgc_process_log(). */
832                 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
833                 /* Are we done with this log? */
834                 if (cld->cld_stopping) {
835                         CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
836                                cld->cld_logname);
837                         config_log_put(cld);
838                         break;
839                 }
840                 /* Make sure not to re-enqueue when the mgc is stopping
841                    (we get called from client_disconnect_export) */
842                 if (!lock->l_conn_export ||
843                     !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
844                         CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
845                                cld->cld_logname);
846                         config_log_put(cld);
847                         break;
848                 }
849
850                 /* Re-enqueue now */
851                 mgc_requeue_add(cld);
852                 config_log_put(cld);
853                 break;
854         default:
855                 LBUG();
856         }
857
858         RETURN(rc);
859 }
860
861 /* Not sure where this should go... */
862 #define  MGC_ENQUEUE_LIMIT 50
863 #define  MGC_TARGET_REG_LIMIT 10
864 #define  MGC_SEND_PARAM_LIMIT 10
865
866 /* Send parameter to MGS*/
867 static int mgc_set_mgs_param(struct obd_export *exp,
868                              struct mgs_send_param *msp)
869 {
870         struct ptlrpc_request *req;
871         struct mgs_send_param *req_msp, *rep_msp;
872         int rc;
873         ENTRY;
874
875         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
876                                         &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
877                                         MGS_SET_INFO);
878         if (!req)
879                 RETURN(-ENOMEM);
880
881         req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
882         if (!req_msp) {
883                 ptlrpc_req_finished(req);
884                 RETURN(-ENOMEM);
885         }
886
887         memcpy(req_msp, msp, sizeof(*req_msp));
888         ptlrpc_request_set_replen(req);
889
890         /* Limit how long we will wait for the enqueue to complete */
891         req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
892         rc = ptlrpc_queue_wait(req);
893         if (!rc) {
894                 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
895                 memcpy(msp, rep_msp, sizeof(*rep_msp));
896         }
897
898         ptlrpc_req_finished(req);
899
900         RETURN(rc);
901 }
902
903 /* Take a config lock so we can get cancel notifications */
904 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
905                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
906                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
907                        void *data, __u32 lvb_len, void *lvb_swabber,
908                        struct lustre_handle *lockh)
909 {
910         struct config_llog_data *cld = (struct config_llog_data *)data;
911         struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
912                          ldlm_completion_ast, NULL, NULL, NULL };
913         struct ptlrpc_request *req;
914         int short_limit = cld_is_sptlrpc(cld);
915         int rc;
916         ENTRY;
917
918         CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
919                cld->cld_resid.name[0]);
920
921         /* We need a callback for every lockholder, so don't try to
922            ldlm_lock_match (see rev 1.1.2.11.2.47) */
923         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
924                                         &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
925                                         LDLM_ENQUEUE);
926         if (req == NULL)
927                 RETURN(-ENOMEM);
928         ptlrpc_request_set_replen(req);
929
930         /* check if this is server or client */
931         if (cld->cld_cfg.cfg_sb) {
932                 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
933                 if (lsi && (lsi->lsi_flags & LSI_SERVER))
934                         short_limit = 1;
935         }
936         /* Limit how long we will wait for the enqueue to complete */
937         req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
938         rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
939                               NULL, 0, lockh, 0);
940         /* A failed enqueue should still call the mgc_blocking_ast,
941            where it will be requeued if needed ("grant failed"). */
942         ptlrpc_req_finished(req);
943         RETURN(rc);
944 }
945
946 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
947                       __u32 mode, struct lustre_handle *lockh)
948 {
949         ENTRY;
950
951         ldlm_lock_decref(lockh, mode);
952
953         RETURN(0);
954 }
955
956 static void mgc_notify_active(struct obd_device *unused)
957 {
958         /* wakeup mgc_requeue_thread to requeue mgc lock */
959         cfs_spin_lock(&config_list_lock);
960         rq_state |= RQ_NOW;
961         cfs_spin_unlock(&config_list_lock);
962         cfs_waitq_signal(&rq_waitq);
963
964         /* TODO: Help the MGS rebuild nidtbl. -jay */
965 }
966
967 /* Send target_reg message to MGS */
968 static int mgc_target_register(struct obd_export *exp,
969                                struct mgs_target_info *mti)
970 {
971         struct ptlrpc_request  *req;
972         struct mgs_target_info *req_mti, *rep_mti;
973         int                     rc;
974         ENTRY;
975
976         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
977                                         &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
978                                         MGS_TARGET_REG);
979         if (req == NULL)
980                 RETURN(-ENOMEM);
981
982         req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
983         if (!req_mti) {
984                 ptlrpc_req_finished(req);
985                 RETURN(-ENOMEM);
986         }
987
988         memcpy(req_mti, mti, sizeof(*req_mti));
989         ptlrpc_request_set_replen(req);
990         CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
991         /* Limit how long we will wait for the enqueue to complete */
992         req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
993
994         rc = ptlrpc_queue_wait(req);
995         if (!rc) {
996                 rep_mti = req_capsule_server_get(&req->rq_pill,
997                                                  &RMF_MGS_TARGET_INFO);
998                 memcpy(mti, rep_mti, sizeof(*rep_mti));
999                 CDEBUG(D_MGC, "register %s got index = %d\n",
1000                        mti->mti_svname, mti->mti_stripe_index);
1001         }
1002         ptlrpc_req_finished(req);
1003
1004         RETURN(rc);
1005 }
1006
1007 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
1008                        void *key, obd_count vallen, void *val,
1009                        struct ptlrpc_request_set *set)
1010 {
1011         int rc = -EINVAL;
1012         ENTRY;
1013
1014         /* Turn off initial_recov after we try all backup servers once */
1015         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1016                 struct obd_import *imp = class_exp2cliimp(exp);
1017                 int value;
1018                 if (vallen != sizeof(int))
1019                         RETURN(-EINVAL);
1020                 value = *(int *)val;
1021                 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1022                        imp->imp_obd->obd_name, value,
1023                        imp->imp_deactive, imp->imp_invalid,
1024                        imp->imp_replayable, imp->imp_obd->obd_replayable,
1025                        ptlrpc_import_state_name(imp->imp_state));
1026                 /* Resurrect if we previously died */
1027                 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1028                      imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1029                         ptlrpc_reconnect_import(imp);
1030                 RETURN(0);
1031         }
1032         /* FIXME move this to mgc_process_config */
1033         if (KEY_IS(KEY_REGISTER_TARGET)) {
1034                 struct mgs_target_info *mti;
1035                 if (vallen != sizeof(struct mgs_target_info))
1036                         RETURN(-EINVAL);
1037                 mti = (struct mgs_target_info *)val;
1038                 CDEBUG(D_MGC, "register_target %s %#x\n",
1039                        mti->mti_svname, mti->mti_flags);
1040                 rc =  mgc_target_register(exp, mti);
1041                 RETURN(rc);
1042         }
1043         if (KEY_IS(KEY_SET_FS)) {
1044                 struct super_block *sb = (struct super_block *)val;
1045                 struct lustre_sb_info *lsi;
1046                 if (vallen != sizeof(struct super_block))
1047                         RETURN(-EINVAL);
1048                 lsi = s2lsi(sb);
1049                 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
1050                 if (rc) {
1051                         CERROR("set_fs got %d\n", rc);
1052                 }
1053                 RETURN(rc);
1054         }
1055         if (KEY_IS(KEY_CLEAR_FS)) {
1056                 if (vallen != 0)
1057                         RETURN(-EINVAL);
1058                 rc = mgc_fs_cleanup(exp->exp_obd);
1059                 if (rc) {
1060                         CERROR("clear_fs got %d\n", rc);
1061                 }
1062                 RETURN(rc);
1063         }
1064         if (KEY_IS(KEY_SET_INFO)) {
1065                 struct mgs_send_param *msp;
1066
1067                 msp = (struct mgs_send_param *)val;
1068                 rc =  mgc_set_mgs_param(exp, msp);
1069                 RETURN(rc);
1070         }
1071         if (KEY_IS(KEY_MGSSEC)) {
1072                 struct client_obd     *cli = &exp->exp_obd->u.cli;
1073                 struct sptlrpc_flavor  flvr;
1074
1075                 /*
1076                  * empty string means using current flavor, if which haven't
1077                  * been set yet, set it as null.
1078                  *
1079                  * if flavor has been set previously, check the asking flavor
1080                  * must match the existing one.
1081                  */
1082                 if (vallen == 0) {
1083                         if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1084                                 RETURN(0);
1085                         val = "null";
1086                         vallen = 4;
1087                 }
1088
1089                 rc = sptlrpc_parse_flavor(val, &flvr);
1090                 if (rc) {
1091                         CERROR("invalid sptlrpc flavor %s to MGS\n",
1092                                (char *) val);
1093                         RETURN(rc);
1094                 }
1095
1096                 /*
1097                  * caller already hold a mutex
1098                  */
1099                 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1100                         cli->cl_flvr_mgc = flvr;
1101                 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1102                                   sizeof(flvr)) != 0) {
1103                         char    str[20];
1104
1105                         sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1106                                             str, sizeof(str));
1107                         LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1108                                        "currently %s is in use\n",
1109                                        (char *) val, str);
1110                         rc = -EPERM;
1111                 }
1112                 RETURN(rc);
1113         }
1114
1115         RETURN(rc);
1116 }
1117
1118 static int mgc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1119                         __u32 *vallen, void *val, struct lov_stripe_md *unused)
1120 {
1121         int rc = -EINVAL;
1122
1123         if (KEY_IS(KEY_CONN_DATA)) {
1124                 struct obd_import *imp = class_exp2cliimp(exp);
1125                 struct obd_connect_data *data = val;
1126
1127                 if (*vallen == sizeof(*data)) {
1128                         *data = imp->imp_connect_data;
1129                         rc = 0;
1130                 }
1131         }
1132
1133         return rc;
1134 }
1135
1136 static int mgc_import_event(struct obd_device *obd,
1137                             struct obd_import *imp,
1138                             enum obd_import_event event)
1139 {
1140         int rc = 0;
1141
1142         LASSERT(imp->imp_obd == obd);
1143         CDEBUG(D_MGC, "import event %#x\n", event);
1144
1145         switch (event) {
1146         case IMP_EVENT_DISCON:
1147                 /* MGC imports should not wait for recovery */
1148                 break;
1149         case IMP_EVENT_INACTIVE:
1150                 break;
1151         case IMP_EVENT_INVALIDATE: {
1152                 struct ldlm_namespace *ns = obd->obd_namespace;
1153                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1154                 break;
1155         }
1156         case IMP_EVENT_ACTIVE:
1157                 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
1158                 /* Clearing obd_no_recov allows us to continue pinging */
1159                 obd->obd_no_recov = 0;
1160                 mgc_notify_active(obd);
1161                 break;
1162         case IMP_EVENT_OCD:
1163                 break;
1164         case IMP_EVENT_DEACTIVATE:
1165         case IMP_EVENT_ACTIVATE:
1166                 break;
1167         default:
1168                 CERROR("Unknown import event %#x\n", event);
1169                 LBUG();
1170         }
1171         RETURN(rc);
1172 }
1173
1174 static int mgc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1175                          struct obd_device *tgt, int *index)
1176 {
1177         struct llog_ctxt *ctxt;
1178         int rc;
1179         ENTRY;
1180
1181         LASSERT(olg == &obd->obd_olg);
1182
1183         rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
1184                         &llog_lvfs_ops);
1185         if (rc)
1186                 RETURN(rc);
1187
1188         rc = llog_setup(obd, olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1189                         &llog_client_ops);
1190         if (rc == 0) {
1191                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1192                 if (!ctxt) {
1193                         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1194                         if (ctxt)
1195                                 llog_cleanup(ctxt);
1196                         RETURN(-ENODEV);
1197                 }
1198                 llog_initiator_connect(ctxt);
1199                 llog_ctxt_put(ctxt);
1200         } else {
1201                 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1202                 if (ctxt)
1203                         llog_cleanup(ctxt);
1204         }
1205
1206         RETURN(rc);
1207 }
1208
1209 static int mgc_llog_finish(struct obd_device *obd, int count)
1210 {
1211         struct llog_ctxt *ctxt;
1212         int rc = 0, rc2 = 0;
1213         ENTRY;
1214
1215         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1216         if (ctxt)
1217                 rc = llog_cleanup(ctxt);
1218
1219         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1220         if (ctxt)
1221                 rc2 = llog_cleanup(ctxt);
1222
1223         if (!rc)
1224                 rc = rc2;
1225
1226         RETURN(rc);
1227 }
1228
1229 enum {
1230         CONFIG_READ_NRPAGES_INIT = 1 << (20 - CFS_PAGE_SHIFT),
1231         CONFIG_READ_NRPAGES      = 4
1232 };
1233
1234 static int mgc_apply_recover_logs(struct obd_device *obd,
1235                                   struct config_llog_data *cld,
1236                                   __u64 max_version,
1237                                   void *data, int datalen)
1238 {
1239         struct config_llog_instance *cfg = &cld->cld_cfg;
1240         struct lustre_sb_info       *lsi = s2lsi(cfg->cfg_sb);
1241         struct mgs_nidtbl_entry *entry;
1242         struct lustre_cfg       *lcfg;
1243         struct lustre_cfg_bufs   bufs;
1244         u64   prev_version = 0;
1245         char *inst;
1246         char *buf;
1247         int   bufsz = CFS_PAGE_SIZE;
1248         int   pos;
1249         int   rc  = 0;
1250         int   off = 0;
1251
1252         OBD_ALLOC(buf, CFS_PAGE_SIZE);
1253         if (buf == NULL)
1254                 return -ENOMEM;
1255
1256         LASSERT(cfg->cfg_instance != NULL);
1257         LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1258         inst = buf;
1259         if (!(lsi->lsi_flags & LSI_SERVER)) {
1260                 pos = sprintf(inst, "%p", cfg->cfg_instance);
1261         } else {
1262                 LASSERT(IS_MDT(lsi->lsi_ldd));
1263                 pos = sprintf(inst, "MDT%04x", lsi->lsi_ldd->ldd_svindex);
1264         }
1265         buf   += pos + 1;
1266         bufsz -= pos + 1;
1267
1268         while (datalen > 0) {
1269                 int   entry_len = sizeof(*entry);
1270                 int   is_ost;
1271                 struct obd_device *obd;
1272                 char *obdname;
1273                 char *cname;
1274                 char *params;
1275                 char *uuid;
1276
1277                 rc = -EINVAL;
1278                 if (datalen < sizeof(*entry))
1279                         break;
1280
1281                 entry = (typeof(entry))(data + off);
1282
1283                 /* sanity check */
1284                 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1285                         break;
1286                 if (entry->mne_nid_count == 0) /* at least one nid entry */
1287                         break;
1288                 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1289                         break;
1290
1291                 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1292                 if (datalen < entry_len) /* must have entry_len at least */
1293                         break;
1294
1295                 lustre_swab_mgs_nidtbl_entry(entry);
1296                 LASSERT(entry->mne_length <= CFS_PAGE_SIZE);
1297                 if (entry->mne_length < entry_len)
1298                         break;
1299
1300                 off     += entry->mne_length;
1301                 datalen -= entry->mne_length;
1302                 if (datalen < 0)
1303                         break;
1304
1305                 if (entry->mne_version > max_version) {
1306                         CERROR("entry index(%lld) is over max_index(%lld)\n",
1307                                entry->mne_version, max_version);
1308                         break;
1309                 }
1310
1311                 if (prev_version >= entry->mne_version) {
1312                         CERROR("index unsorted, prev %lld, now %lld\n",
1313                                prev_version, entry->mne_version);
1314                         break;
1315                 }
1316                 prev_version = entry->mne_version;
1317
1318                 /*
1319                  * Write a string with format "nid::instance" to
1320                  * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1321                  */
1322
1323                 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1324                 memset(buf, 0, bufsz);
1325                 obdname = buf;
1326                 pos = 0;
1327
1328                 /* lustre-OST0001-osc-<instance #> */
1329                 strcpy(obdname, cld->cld_logname);
1330                 cname = strrchr(obdname, '-');
1331                 if (cname == NULL) {
1332                         CERROR("mgc: invalid logname %s\n", obdname);
1333                         break;
1334                 }
1335
1336                 pos = cname - obdname;
1337                 obdname[pos] = 0;
1338                 pos += sprintf(obdname + pos, "-%s%04x",
1339                                   is_ost ? "OST" : "MDT", entry->mne_index);
1340
1341                 cname = is_ost ? "osc" : "mdc",
1342                 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1343                 lustre_cfg_bufs_reset(&bufs, obdname);
1344
1345                 /* find the obd by obdname */
1346                 obd = class_name2obd(obdname);
1347                 if (obd == NULL) {
1348                         CDEBUG(D_INFO, "mgc: cannot find obdname %s\n",
1349                                obdname);
1350
1351                         /* this is a safe race, when the ost is starting up...*/
1352                         continue;
1353                 }
1354
1355                 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1356                 ++pos;
1357                 params = buf + pos;
1358                 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1359                 uuid = buf + pos;
1360
1361                 /* TODO: iterate all nids to find one */
1362                 /* find uuid by nid */
1363                 rc = client_import_find_conn(obd->u.cli.cl_import,
1364                                              entry->u.nids[0],
1365                                              (struct obd_uuid *)uuid);
1366                 if (rc < 0) {
1367                         CERROR("mgc: cannot find uuid by nid %s\n",
1368                                libcfs_nid2str(entry->u.nids[0]));
1369                         break;
1370                 }
1371
1372                 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1373                        uuid, libcfs_nid2str(entry->u.nids[0]));
1374
1375                 pos += strlen(uuid);
1376                 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1377                 LASSERT(pos < bufsz);
1378
1379                 lustre_cfg_bufs_set_string(&bufs, 1, params);
1380
1381                 rc = -ENOMEM;
1382                 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1383                 if (lcfg == NULL) {
1384                         CERROR("mgc: cannot allocate memory\n");
1385                         break;
1386                 }
1387
1388                 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1389                        prev_version, max_version, obdname, params);
1390
1391                 rc = class_process_config(lcfg);
1392                 lustre_cfg_free(lcfg);
1393                 if (rc)
1394                         CDEBUG(D_INFO, "process config for %s error %d\n",
1395                                obdname, rc);
1396
1397                 /* continue, even one with error */
1398         }
1399
1400         OBD_FREE(inst, CFS_PAGE_SIZE);
1401         return rc;
1402 }
1403
1404 /**
1405  * This function is called if this client was notified for target restarting
1406  * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1407  */
1408 static int mgc_process_recover_log(struct obd_device *obd,
1409                                    struct config_llog_data *cld)
1410 {
1411         struct ptlrpc_request *req = NULL;
1412         struct config_llog_instance *cfg = &cld->cld_cfg;
1413         struct mgs_config_body *body;
1414         struct mgs_config_res  *res;
1415         struct ptlrpc_bulk_desc *desc;
1416         cfs_page_t **pages;
1417         int nrpages;
1418         bool eof = true;
1419         int i;
1420         int ealen;
1421         int rc;
1422         ENTRY;
1423
1424         /* allocate buffer for bulk transfer.
1425          * if this is the first time for this mgs to read logs,
1426          * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1427          * once; otherwise, it only reads increment of logs, this should be
1428          * small and CONFIG_READ_NRPAGES will be used.
1429          */
1430         nrpages = CONFIG_READ_NRPAGES;
1431         if (cfg->cfg_last_idx == 0) /* the first time */
1432                 nrpages = CONFIG_READ_NRPAGES_INIT;
1433
1434         OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1435         if (pages == NULL)
1436                 GOTO(out, rc = -ENOMEM);
1437
1438         for (i = 0; i < nrpages; i++) {
1439                 pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1440                 if (pages[i] == NULL)
1441                         GOTO(out, rc = -ENOMEM);
1442         }
1443
1444 again:
1445         LASSERT(cld_is_recover(cld));
1446         LASSERT(cfs_mutex_is_locked(&cld->cld_lock));
1447         req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1448                                    &RQF_MGS_CONFIG_READ);
1449         if (req == NULL)
1450                 GOTO(out, rc = -ENOMEM);
1451
1452         rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1453         if (rc)
1454                 GOTO(out, rc);
1455
1456         /* pack request */
1457         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1458         LASSERT(body != NULL);
1459         LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1460         strncpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name));
1461         body->mcb_offset = cfg->cfg_last_idx + 1;
1462         body->mcb_type   = cld->cld_type;
1463         body->mcb_bits   = CFS_PAGE_SHIFT;
1464         body->mcb_units  = nrpages;
1465
1466         /* allocate bulk transfer descriptor */
1467         desc = ptlrpc_prep_bulk_imp(req, nrpages, BULK_PUT_SINK,
1468                                     MGS_BULK_PORTAL);
1469         if (desc == NULL)
1470                 GOTO(out, rc = -ENOMEM);
1471
1472         for (i = 0; i < nrpages; i++)
1473                 ptlrpc_prep_bulk_page(desc, pages[i], 0, CFS_PAGE_SIZE);
1474
1475         ptlrpc_request_set_replen(req);
1476         rc = ptlrpc_queue_wait(req);
1477         if (rc)
1478                 GOTO(out, rc);
1479
1480         res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1481         if (res->mcr_size < res->mcr_offset)
1482                 GOTO(out, rc = -EINVAL);
1483
1484         /* always update the index even though it might have errors with
1485          * handling the recover logs */
1486         cfg->cfg_last_idx = res->mcr_offset;
1487         eof = res->mcr_offset == res->mcr_size;
1488
1489         CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1490                res->mcr_offset, eof == false);
1491
1492         ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1493         if (ealen < 0)
1494                 GOTO(out, rc = ealen);
1495
1496         if (ealen > nrpages << CFS_PAGE_SHIFT)
1497                 GOTO(out, rc = -EINVAL);
1498
1499         if (ealen == 0) { /* no logs transferred */
1500                 if (!eof)
1501                         rc = -EINVAL;
1502                 GOTO(out, rc);
1503         }
1504
1505         for (i = 0; i < nrpages && ealen > 0; i++) {
1506                 int rc2;
1507                 void *ptr;
1508
1509                 ptr = cfs_kmap(pages[i]);
1510                 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1511                                              min_t(int, ealen, CFS_PAGE_SIZE));
1512                 cfs_kunmap(pages[i]);
1513                 if (rc2 < 0) {
1514                         CWARN("Process recover log %s error %d\n",
1515                               cld->cld_logname, rc2);
1516                         break;
1517                 }
1518
1519                 ealen -= CFS_PAGE_SIZE;
1520         }
1521
1522 out:
1523         if (req)
1524                 ptlrpc_req_finished(req);
1525
1526         if (rc == 0 && !eof)
1527                 goto again;
1528
1529         if (pages) {
1530                 for (i = 0; i < nrpages; i++) {
1531                         if (pages[i] == NULL)
1532                                 break;
1533                         cfs_free_page(pages[i]);
1534                 }
1535                 OBD_FREE(pages, sizeof(*pages) * nrpages);
1536         }
1537         return rc;
1538 }
1539
1540 /* identical to mgs_log_is_empty */
1541 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
1542                             char *name)
1543 {
1544         struct lvfs_run_ctxt saved;
1545         struct llog_handle *llh;
1546         int rc = 0;
1547
1548         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1549         rc = llog_create(ctxt, &llh, NULL, name);
1550         if (rc == 0) {
1551                 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1552                 rc = llog_get_size(llh);
1553                 llog_close(llh);
1554         }
1555         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1556         /* header is record 1 */
1557         return(rc <= 1);
1558 }
1559
1560 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
1561                             void *data)
1562 {
1563         struct llog_rec_hdr local_rec = *rec;
1564         struct llog_handle *local_llh = (struct llog_handle *)data;
1565         char *cfg_buf = (char*) (rec + 1);
1566         struct lustre_cfg *lcfg;
1567         int rc = 0;
1568         ENTRY;
1569
1570         /* Append all records */
1571         local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
1572         rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
1573                             (void *)cfg_buf, -1);
1574
1575         lcfg = (struct lustre_cfg *)cfg_buf;
1576         CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
1577                rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
1578                lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
1579
1580         RETURN(rc);
1581 }
1582
1583 /* Copy a remote log locally */
1584 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1585                          struct llog_ctxt *lctxt, char *logname)
1586 {
1587         struct llog_handle *local_llh, *remote_llh;
1588         struct obd_uuid *uuid;
1589         char *temp_log;
1590         int rc, rc2;
1591         ENTRY;
1592
1593         /* Write new log to a temp name, then vfs_rename over logname
1594            upon successful completion. */
1595
1596         OBD_ALLOC(temp_log, strlen(logname) + 1);
1597         if (!temp_log)
1598                 RETURN(-ENOMEM);
1599         sprintf(temp_log, "%sT", logname);
1600
1601         /* Make sure there's no old temp log */
1602         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1603         if (rc)
1604                 GOTO(out, rc);
1605         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1606         if (rc)
1607                 GOTO(out, rc);
1608         rc = llog_destroy(local_llh);
1609         llog_free_handle(local_llh);
1610         if (rc)
1611                 GOTO(out, rc);
1612
1613         /* open local log */
1614         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1615         if (rc)
1616                 GOTO(out, rc);
1617
1618         /* set the log header uuid for fun */
1619         OBD_ALLOC_PTR(uuid);
1620         obd_str2uuid(uuid, logname);
1621         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1622         OBD_FREE_PTR(uuid);
1623         if (rc)
1624                 GOTO(out_closel, rc);
1625
1626         /* open remote log */
1627         rc = llog_create(rctxt, &remote_llh, NULL, logname);
1628         if (rc)
1629                 GOTO(out_closel, rc);
1630         rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1631         if (rc)
1632                 GOTO(out_closer, rc);
1633
1634         /* Copy remote log */
1635         rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1636
1637 out_closer:
1638         rc2 = llog_close(remote_llh);
1639         if (!rc)
1640                 rc = rc2;
1641 out_closel:
1642         rc2 = llog_close(local_llh);
1643         if (!rc)
1644                 rc = rc2;
1645
1646         /* We've copied the remote log to the local temp log, now
1647            replace the old local log with the temp log. */
1648         if (!rc) {
1649                 struct client_obd *cli = &obd->u.cli;
1650                 LASSERT(cli);
1651                 LASSERT(cli->cl_mgc_configs_dir);
1652                 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1653                                    temp_log, logname);
1654         }
1655         CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1656 out:
1657         if (rc)
1658                 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1659         OBD_FREE(temp_log, strlen(logname) + 1);
1660         RETURN(rc);
1661 }
1662
1663 /* local_only means it cannot get remote llogs */
1664 static int mgc_process_cfg_log(struct obd_device *mgc,
1665                                struct config_llog_data *cld,
1666                                int local_only)
1667 {
1668         struct llog_ctxt *ctxt, *lctxt = NULL;
1669         struct client_obd *cli = &mgc->u.cli;
1670         struct lvfs_run_ctxt *saved_ctxt;
1671         struct lustre_sb_info *lsi = NULL;
1672         int rc = 0, must_pop = 0;
1673         bool sptlrpc_started = false;
1674
1675         ENTRY;
1676
1677         LASSERT(cld);
1678         LASSERT(cfs_mutex_is_locked(&cld->cld_lock));
1679
1680         /*
1681          * local copy of sptlrpc log is controlled elsewhere, don't try to
1682          * read it up here.
1683          */
1684         if (cld_is_sptlrpc(cld) && local_only)
1685                 RETURN(0);
1686
1687         if (cld->cld_cfg.cfg_sb)
1688                 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1689
1690         ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1691         if (!ctxt) {
1692                 CERROR("missing llog context\n");
1693                 RETURN(-EINVAL);
1694         }
1695
1696         OBD_ALLOC_PTR(saved_ctxt);
1697         if (saved_ctxt == NULL)
1698                 RETURN(-ENOMEM);
1699
1700         lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1701
1702         /* Copy the setup log locally if we can. Don't mess around if we're
1703            running an MGS though (logs are already local). */
1704         if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1705             (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1706             !IS_MGS(lsi->lsi_ldd)) {
1707                 push_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1708                 must_pop++;
1709                 if (!local_only)
1710                         /* Only try to copy log if we have the lock. */
1711                         rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1712                 if (local_only || rc) {
1713                         if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1714                                 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1715                                                    "log %s and no local copy."
1716                                                    "\n", cld->cld_logname);
1717                                 GOTO(out_pop, rc = -ENOTCONN);
1718                         }
1719                         CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1720                                "copy for now, will try to update later.\n",
1721                                cld->cld_logname);
1722                 }
1723                 /* Now, whether we copied or not, start using the local llog.
1724                    If we failed to copy, we'll start using whatever the old
1725                    log has. */
1726                 llog_ctxt_put(ctxt);
1727                 ctxt = lctxt;
1728                 lctxt = NULL;
1729         } else if (local_only) { /* no local log at client side */
1730                 GOTO(out_pop, rc = -EIO);
1731         }
1732
1733         if (cld_is_sptlrpc(cld)) {
1734                 sptlrpc_conf_log_update_begin(cld->cld_logname);
1735                 sptlrpc_started = true;
1736         }
1737
1738         /* logname and instance info should be the same, so use our
1739            copy of the instance for the update.  The cfg_last_idx will
1740            be updated here. */
1741         rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1742         EXIT;
1743
1744 out_pop:
1745         llog_ctxt_put(ctxt);
1746         if (lctxt)
1747                 llog_ctxt_put(lctxt);
1748         if (must_pop)
1749                 pop_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1750
1751         OBD_FREE_PTR(saved_ctxt);
1752         /*
1753          * update settings on existing OBDs. doing it inside
1754          * of llog_process_lock so no device is attaching/detaching
1755          * in parallel.
1756          * the logname must be <fsname>-sptlrpc
1757          */
1758         if (sptlrpc_started) {
1759                 LASSERT(cld_is_sptlrpc(cld));
1760                 sptlrpc_conf_log_update_end(cld->cld_logname);
1761                 class_notify_sptlrpc_conf(cld->cld_logname,
1762                                           strlen(cld->cld_logname) -
1763                                           strlen("-sptlrpc"));
1764         }
1765
1766         RETURN(rc);
1767 }
1768
1769 /** Get a config log from the MGS and process it.
1770  * This func is called for both clients and servers.
1771  * Copy the log locally before parsing it if appropriate (non-MGS server)
1772  */
1773 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1774 {
1775         struct lustre_handle lockh = { 0 };
1776         int rc = 0, rcl, flags = 0;
1777         ENTRY;
1778
1779         LASSERT(cld);
1780
1781         /* I don't want multiple processes running process_log at once --
1782            sounds like badness.  It actually might be fine, as long as
1783            we're not trying to update from the same log
1784            simultaneously (in which case we should use a per-log sem.) */
1785         cfs_mutex_lock(&cld->cld_lock);
1786         if (cld->cld_stopping) {
1787                 cfs_mutex_unlock(&cld->cld_lock);
1788                 RETURN(0);
1789         }
1790
1791         OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1792
1793         CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1794                cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1795
1796         /* Get the cfg lock on the llog */
1797         rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1798                           LCK_CR, &flags, NULL, NULL, NULL,
1799                           cld, 0, NULL, &lockh);
1800         if (rcl == 0) {
1801                 /* Get the cld, it will be released in mgc_blocking_ast. */
1802                 config_log_get(cld);
1803                 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1804                 LASSERT(rc == 0);
1805         } else {
1806                 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1807
1808                 /* mark cld_lostlock so that it will requeue
1809                  * after MGC becomes available. */
1810                 cld->cld_lostlock = 1;
1811                 /* Get extra reference, it will be put in requeue thread */
1812                 config_log_get(cld);
1813         }
1814
1815
1816         if (cld_is_recover(cld)) {
1817                 rc = 0; /* this is not a fatal error for recover log */
1818                 if (rcl == 0)
1819                         rc = mgc_process_recover_log(mgc, cld);
1820         } else {
1821                 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1822         }
1823
1824         CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1825                mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1826
1827         cfs_mutex_unlock(&cld->cld_lock);
1828
1829         /* Now drop the lock so MGS can revoke it */
1830         if (!rcl) {
1831                 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1832                                  LCK_CR, &lockh);
1833                 if (rcl)
1834                         CERROR("Can't drop cfg lock: %d\n", rcl);
1835         }
1836
1837         RETURN(rc);
1838 }
1839
1840
1841 /** Called from lustre_process_log.
1842  * LCFG_LOG_START gets the config log from the MGS, processes it to start
1843  * any services, and adds it to the list logs to watch (follow).
1844  */
1845 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1846 {
1847         struct lustre_cfg *lcfg = buf;
1848         struct config_llog_instance *cfg = NULL;
1849         char *logname;
1850         int rc = 0;
1851         ENTRY;
1852
1853         switch(lcfg->lcfg_command) {
1854         case LCFG_LOV_ADD_OBD: {
1855                 /* Overloading this cfg command: register a new target */
1856                 struct mgs_target_info *mti;
1857
1858                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1859                     sizeof(struct mgs_target_info))
1860                         GOTO(out, rc = -EINVAL);
1861
1862                 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1863                 CDEBUG(D_MGC, "add_target %s %#x\n",
1864                        mti->mti_svname, mti->mti_flags);
1865                 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1866                 break;
1867         }
1868         case LCFG_LOV_DEL_OBD:
1869                 /* Unregister has no meaning at the moment. */
1870                 CERROR("lov_del_obd unimplemented\n");
1871                 rc = -ENOSYS;
1872                 break;
1873         case LCFG_SPTLRPC_CONF: {
1874                 rc = sptlrpc_process_config(lcfg);
1875                 break;
1876         }
1877         case LCFG_LOG_START: {
1878                 struct config_llog_data *cld;
1879                 struct super_block *sb;
1880
1881                 logname = lustre_cfg_string(lcfg, 1);
1882                 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1883                 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1884
1885                 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1886                        cfg->cfg_last_idx);
1887
1888                 /* We're only called through here on the initial mount */
1889                 rc = config_log_add(obd, logname, cfg, sb);
1890                 if (rc)
1891                         break;
1892                 cld = config_log_find(logname, cfg);
1893                 if (cld == NULL) {
1894                         rc = -ENOENT;
1895                         break;
1896                 }
1897
1898                 /* COMPAT_146 */
1899                 /* FIXME only set this for old logs!  Right now this forces
1900                    us to always skip the "inside markers" check */
1901                 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1902
1903                 rc = mgc_process_log(obd, cld);
1904                 if (rc == 0 && cld->cld_recover) {
1905                         rc = mgc_process_log(obd, cld->cld_recover);
1906                         if (rc)
1907                                 CERROR("Cannot process recover llog %d\n", rc);
1908                 }
1909                 config_log_put(cld);
1910
1911                 break;
1912         }
1913         case LCFG_LOG_END: {
1914                 logname = lustre_cfg_string(lcfg, 1);
1915
1916                 if (lcfg->lcfg_bufcount >= 2)
1917                         cfg = (struct config_llog_instance *)lustre_cfg_buf(
1918                                 lcfg, 2);
1919                 rc = config_log_end(logname, cfg);
1920                 break;
1921         }
1922         default: {
1923                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1924                 GOTO(out, rc = -EINVAL);
1925
1926         }
1927         }
1928 out:
1929         RETURN(rc);
1930 }
1931
1932 struct obd_ops mgc_obd_ops = {
1933         .o_owner        = THIS_MODULE,
1934         .o_setup        = mgc_setup,
1935         .o_precleanup   = mgc_precleanup,
1936         .o_cleanup      = mgc_cleanup,
1937         .o_add_conn     = client_import_add_conn,
1938         .o_del_conn     = client_import_del_conn,
1939         .o_connect      = client_connect_import,
1940         .o_disconnect   = client_disconnect_export,
1941         //.o_enqueue      = mgc_enqueue,
1942         .o_cancel       = mgc_cancel,
1943         //.o_iocontrol    = mgc_iocontrol,
1944         .o_set_info_async = mgc_set_info_async,
1945         .o_get_info       = mgc_get_info,
1946         .o_import_event = mgc_import_event,
1947         .o_llog_init    = mgc_llog_init,
1948         .o_llog_finish  = mgc_llog_finish,
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