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