Whamcloud - gitweb
b=21900 ost-pools test_25: FAIL
[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  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 {
66         __u64 resname = 0;
67
68         if (len > 8) {
69                 CERROR("name too long: %s\n", name);
70                 return -EINVAL;
71         }
72         if (len <= 0) {
73                 CERROR("missing name: %s\n", name);
74                 return -EINVAL;
75         }
76         memcpy(&resname, name, len);
77
78         memset(res_id, 0, sizeof(*res_id));
79
80         /* Always use the same endianness for the resid */
81         res_id->name[0] = cpu_to_le64(resname);
82         CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
83                res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
84         return 0;
85 }
86
87 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id)
88 {
89         /* fsname is at most 8 chars long, maybe contain "-".
90          * e.g. "lustre", "SUN-000" */
91         return mgc_name2resid(fsname, strlen(fsname), res_id);
92 }
93 EXPORT_SYMBOL(mgc_fsname2resid);
94
95 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id)
96 {
97         char *name_end;
98         int len;
99
100         /* logname consists of "fsname-nodetype".
101          * e.g. "lustre-MDT0001", "SUN-000-client" */
102         name_end = strrchr(logname, '-');
103         LASSERT(name_end);
104         len = name_end - logname;
105         return mgc_name2resid(logname, len, res_id);
106 }
107
108
109 /********************** config llog list **********************/
110 static struct list_head config_llog_list = LIST_HEAD_INIT(config_llog_list);
111 static spinlock_t       config_list_lock = SPIN_LOCK_UNLOCKED;
112
113 /* Take a reference to a config log */
114 static int config_log_get(struct config_llog_data *cld)
115 {
116         ENTRY;
117         if (cld->cld_stopping)
118                 RETURN(1);
119         atomic_inc(&cld->cld_refcount);
120         CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
121                atomic_read(&cld->cld_refcount));
122         RETURN(0);
123 }
124
125 /* Drop a reference to a config log.  When no longer referenced,
126    we can free the config log data */
127 static void config_log_put(struct config_llog_data *cld)
128 {
129         ENTRY;
130         CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
131                atomic_read(&cld->cld_refcount));
132         if (atomic_dec_and_test(&cld->cld_refcount)) {
133                 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
134                 class_export_put(cld->cld_mgcexp);
135                 spin_lock(&config_list_lock);
136                 list_del(&cld->cld_list_chain);
137                 spin_unlock(&config_list_lock);
138                 OBD_FREE(cld->cld_logname, strlen(cld->cld_logname) + 1);
139                 if (cld->cld_cfg.cfg_instance != NULL)
140                         OBD_FREE(cld->cld_cfg.cfg_instance,
141                                  strlen(cld->cld_cfg.cfg_instance) + 1);
142                 OBD_FREE(cld, sizeof(*cld));
143         }
144         EXIT;
145 }
146
147 /* Find a config log by name */
148 static struct config_llog_data *config_log_find(char *logname,
149                                                struct config_llog_instance *cfg)
150 {
151         struct list_head *tmp;
152         struct config_llog_data *cld;
153         char *logid = logname;
154         int match_instance = 0;
155         ENTRY;
156
157         if (cfg && cfg->cfg_instance) {
158                 match_instance++;
159                 logid = cfg->cfg_instance;
160         }
161         if (!logid) {
162                 CERROR("No log specified\n");
163                 RETURN(ERR_PTR(-EINVAL));
164         }
165
166         spin_lock(&config_list_lock);
167         list_for_each(tmp, &config_llog_list) {
168                 cld = list_entry(tmp, struct config_llog_data, cld_list_chain);
169                 if (match_instance && cld->cld_cfg.cfg_instance &&
170                     strcmp(logid, cld->cld_cfg.cfg_instance) == 0)
171                         goto out_found;
172                 if (!match_instance &&
173                     strcmp(logid, cld->cld_logname) == 0)
174                         goto out_found;
175         }
176         spin_unlock(&config_list_lock);
177
178         CDEBUG(D_CONFIG, "can't get log %s\n", logid);
179         RETURN(ERR_PTR(-ENOENT));
180 out_found:
181         atomic_inc(&cld->cld_refcount);
182         spin_unlock(&config_list_lock);
183         RETURN(cld);
184 }
185
186 /* Add this log to our list of active logs.
187    We have one active log per "mount" - client instance or servername.
188    Each instance may be at a different point in the log. */
189 static int config_log_add(char *logname, struct config_llog_instance *cfg,
190                           struct super_block *sb)
191 {
192         struct config_llog_data *cld;
193         struct lustre_sb_info *lsi = s2lsi(sb);
194         int rc;
195         ENTRY;
196
197         CDEBUG(D_MGC, "adding config log %s:%s\n", logname, cfg->cfg_instance);
198
199         OBD_ALLOC(cld, sizeof(*cld));
200         if (!cld)
201                 RETURN(-ENOMEM);
202         OBD_ALLOC(cld->cld_logname, strlen(logname) + 1);
203         if (!cld->cld_logname) {
204                 OBD_FREE(cld, sizeof(*cld));
205                 RETURN(-ENOMEM);
206         }
207         strcpy(cld->cld_logname, logname);
208         cld->cld_cfg = *cfg;
209         cld->cld_cfg.cfg_last_idx = 0;
210         cld->cld_cfg.cfg_flags = 0;
211         cld->cld_cfg.cfg_sb = sb;
212         atomic_set(&cld->cld_refcount, 1);
213         init_mutex(&cld->cld_sem);
214
215         /* Keep the mgc around until we are done */
216         cld->cld_mgcexp = class_export_get(lsi->lsi_mgc->obd_self_export);
217
218         if (cfg->cfg_instance != NULL) {
219                 OBD_ALLOC(cld->cld_cfg.cfg_instance,
220                           strlen(cfg->cfg_instance) + 1);
221                 strcpy(cld->cld_cfg.cfg_instance, cfg->cfg_instance);
222         }
223         rc = mgc_logname2resid(logname, &cld->cld_resid);
224         spin_lock(&config_list_lock);
225         list_add(&cld->cld_list_chain, &config_llog_list);
226         spin_unlock(&config_list_lock);
227
228         if (rc) {
229                 config_log_put(cld);
230                 RETURN(rc);
231         }
232
233         RETURN(rc);
234 }
235
236 /* Stop watching for updates on this log. */
237 static int config_log_end(char *logname, struct config_llog_instance *cfg)
238 {
239         struct config_llog_data *cld;
240         int rc = 0;
241         ENTRY;
242
243         cld = config_log_find(logname, cfg);
244         if (IS_ERR(cld))
245                 RETURN(PTR_ERR(cld));
246         /* drop the ref from the find */
247         config_log_put(cld);
248
249         down(&cld->cld_sem);
250         cld->cld_stopping = 1;
251         up(&cld->cld_sem);
252
253         /* drop the start ref */
254         config_log_put(cld);
255         CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
256                rc);
257         RETURN(rc);
258 }
259
260 /* reenqueue any lost locks */
261 #define RQ_RUNNING 0x1
262 #define RQ_NOW     0x2
263 #define RQ_LATER   0x4
264 #define RQ_STOP    0x8
265 static int rq_state = 0;
266 static cfs_waitq_t rq_waitq;
267
268 static int mgc_process_log(struct obd_device *mgc,
269                            struct config_llog_data *cld);
270
271 static int mgc_requeue_thread(void *data)
272 {
273         struct l_wait_info lwi_now, lwi_later;
274         struct config_llog_data *cld, *n;
275         char name[] = "ll_cfg_requeue";
276         int rc = 0;
277         ENTRY;
278
279         cfs_daemonize(name);
280
281         CDEBUG(D_MGC, "Starting requeue thread\n");
282
283         lwi_later = LWI_TIMEOUT(60 * HZ, NULL, NULL);
284         l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP), &lwi_later);
285
286         /* Keep trying failed locks periodically */
287         spin_lock(&config_list_lock);
288         while (rq_state & (RQ_NOW | RQ_LATER)) {
289                 /* Any new or requeued lostlocks will change the state */
290                 rq_state &= ~(RQ_NOW | RQ_LATER);
291                 spin_unlock(&config_list_lock);
292
293                 /* Always wait a few seconds to allow the server who
294                    caused the lock revocation to finish its setup, plus some
295                    random so everyone doesn't try to reconnect at once. */
296                 lwi_now = LWI_TIMEOUT(3 * HZ + (ll_rand() & 0xff) * (HZ / 100),
297                                       NULL, NULL);
298                 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi_now);
299
300                 spin_lock(&config_list_lock);
301                 list_for_each_entry_safe(cld, n, &config_llog_list,
302                                          cld_list_chain) {
303                         spin_unlock(&config_list_lock);
304
305                         if (cld->cld_lostlock) {
306                                 CDEBUG(D_MGC, "updating log %s\n",
307                                        cld->cld_logname);
308                                 cld->cld_lostlock = 0;
309                                 rc = mgc_process_log(cld->cld_mgcexp->exp_obd,
310                                                      cld);
311                                 /* Whether we enqueued again or not in
312                                    mgc_process_log, we're done with the ref
313                                    from the old enqueue */
314                                 config_log_put(cld);
315                         }
316
317                         spin_lock(&config_list_lock);
318                 }
319                 spin_unlock(&config_list_lock);
320
321                 /* Wait a bit to see if anyone else needs a requeue */
322                 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
323                              &lwi_later);
324                 spin_lock(&config_list_lock);
325         }
326         /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
327         rq_state &= ~RQ_RUNNING;
328         spin_unlock(&config_list_lock);
329
330         CDEBUG(D_MGC, "Ending requeue thread\n");
331         RETURN(rc);
332 }
333
334 /* Add a cld to the list to requeue.  Start the requeue thread if needed.
335    We are responsible for dropping the config log reference from here on out. */
336 static int mgc_requeue_add(struct config_llog_data *cld, int later)
337 {
338         int rc = 0;
339
340         CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
341                cld->cld_logname, later, atomic_read(&cld->cld_refcount),
342                cld->cld_stopping, rq_state);
343
344         /* Hold lock for rq_state */
345         spin_lock(&config_list_lock);
346
347         if (cld->cld_stopping || (rq_state & RQ_STOP)) {
348                 spin_unlock(&config_list_lock);
349                 config_log_put(cld);
350                 RETURN(0);
351         }
352
353         cld->cld_lostlock = 1;
354
355         if (!(rq_state & RQ_RUNNING)) {
356                 LASSERT(rq_state == 0);
357                 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
358                 spin_unlock(&config_list_lock);
359                 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
360                                        CLONE_VM | CLONE_FILES);
361                 if (rc < 0) {
362                         CERROR("log %s: cannot start requeue thread (%d),"
363                                "no more log updates!\n", cld->cld_logname, rc);
364                         /* Drop the ref, since the rq thread won't */
365                         cld->cld_lostlock = 0;
366                         config_log_put(cld);
367                         rq_state = 0;
368                         RETURN(rc);
369                 }
370         } else {
371                 rq_state |= later ? RQ_LATER : RQ_NOW;
372                 spin_unlock(&config_list_lock);
373                 cfs_waitq_signal(&rq_waitq);
374         }
375
376         RETURN(0);
377 }
378
379 /********************** class fns **********************/
380
381 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
382                         struct vfsmount *mnt)
383 {
384         struct lvfs_run_ctxt saved;
385         struct lustre_sb_info *lsi = s2lsi(sb);
386         struct client_obd *cli = &obd->u.cli;
387         struct dentry *dentry;
388         char *label;
389         int err = 0;
390         ENTRY;
391
392         LASSERT(lsi);
393         LASSERT(lsi->lsi_srv_mnt == mnt);
394
395         /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
396         down(&cli->cl_mgc_sem);
397
398         cleanup_group_info();
399
400         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
401         if (IS_ERR(obd->obd_fsops)) {
402                 up(&cli->cl_mgc_sem);
403                 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
404                        PTR_ERR(obd->obd_fsops));
405                 RETURN(PTR_ERR(obd->obd_fsops));
406         }
407
408         cli->cl_mgc_vfsmnt = mnt;
409         fsfilt_setup(obd, mnt->mnt_sb);
410
411         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
412         obd->obd_lvfs_ctxt.pwdmnt = mnt;
413         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
414         obd->obd_lvfs_ctxt.fs = get_ds();
415
416         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
417         dentry = lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
418                                 strlen(MOUNT_CONFIGS_DIR));
419         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
420         if (IS_ERR(dentry)) {
421                 err = PTR_ERR(dentry);
422                 CERROR("cannot lookup %s directory: rc = %d\n",
423                        MOUNT_CONFIGS_DIR, err);
424                 GOTO(err_ops, err);
425         }
426         cli->cl_mgc_configs_dir = dentry;
427
428         /* We take an obd ref to insure that we can't get to mgc_cleanup
429            without calling mgc_fs_cleanup first. */
430         class_incref(obd);
431
432         label = fsfilt_get_label(obd, mnt->mnt_sb);
433         if (label)
434                 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
435
436         /* We keep the cl_mgc_sem until mgc_fs_cleanup */
437         RETURN(0);
438
439 err_ops:
440         fsfilt_put_ops(obd->obd_fsops);
441         obd->obd_fsops = NULL;
442         cli->cl_mgc_vfsmnt = NULL;
443         up(&cli->cl_mgc_sem);
444         RETURN(err);
445 }
446
447 static int mgc_fs_cleanup(struct obd_device *obd)
448 {
449         struct client_obd *cli = &obd->u.cli;
450         int rc = 0;
451         ENTRY;
452
453         LASSERT(cli->cl_mgc_vfsmnt != NULL);
454
455         if (cli->cl_mgc_configs_dir != NULL) {
456                 struct lvfs_run_ctxt saved;
457                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
458                 l_dput(cli->cl_mgc_configs_dir);
459                 cli->cl_mgc_configs_dir = NULL;
460                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
461                 class_decref(obd);
462         }
463
464         cli->cl_mgc_vfsmnt = NULL;
465         if (obd->obd_fsops)
466                 fsfilt_put_ops(obd->obd_fsops);
467
468         up(&cli->cl_mgc_sem);
469
470         RETURN(rc);
471 }
472
473 static atomic_t mgc_count = ATOMIC_INIT(0);
474 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
475 {
476         int rc = 0;
477         ENTRY;
478
479         switch (stage) {
480         case OBD_CLEANUP_EARLY:
481                 break;
482         case OBD_CLEANUP_EXPORTS:
483                 if (atomic_dec_and_test(&mgc_count)) {
484                         /* Kick the requeue waitq - cld's should all be
485                            stopping */
486                         spin_lock(&config_list_lock);
487                         rq_state |= RQ_STOP;
488                         spin_unlock(&config_list_lock);
489                         cfs_waitq_signal(&rq_waitq);
490                 }
491                 /* client import will not have been cleaned. */
492                 down_write(&obd->u.cli.cl_sem);
493                 if (obd->u.cli.cl_import) {
494                         struct obd_import *imp;
495                         imp = obd->u.cli.cl_import;
496                         CERROR("client import never connected\n");
497                         class_destroy_import(imp);
498                         obd->u.cli.cl_import = NULL;
499                 }
500                 up_write(&obd->u.cli.cl_sem);
501
502                 rc = obd_llog_finish(obd, 0);
503                 if (rc != 0)
504                         CERROR("failed to cleanup llogging subsystems\n");
505                 break;
506         case OBD_CLEANUP_SELF_EXP:
507                 break;
508         case OBD_CLEANUP_OBD:
509                 break;
510         }
511         RETURN(rc);
512 }
513
514 static int mgc_cleanup(struct obd_device *obd)
515 {
516         struct client_obd *cli = &obd->u.cli;
517         int rc;
518         ENTRY;
519
520         LASSERT(cli->cl_mgc_vfsmnt == NULL);
521
522         /* COMPAT_146 - old config logs may have added profiles we don't
523            know about */
524         if (obd->obd_type->typ_refcnt <= 1)
525                 /* Only for the last mgc */
526                 class_del_profiles();
527
528         lprocfs_obd_cleanup(obd);
529         ptlrpcd_decref();
530
531         rc = client_obd_cleanup(obd);
532         RETURN(rc);
533 }
534
535 static int mgc_setup(struct obd_device *obd, obd_count len, void *buf)
536 {
537         struct lprocfs_static_vars lvars;
538         int rc;
539         ENTRY;
540
541         ptlrpcd_addref();
542
543         rc = client_obd_setup(obd, len, buf);
544         if (rc)
545                 GOTO(err_decref, rc);
546
547         rc = obd_llog_init(obd, obd, NULL);
548         if (rc) {
549                 CERROR("failed to setup llogging subsystems\n");
550                 GOTO(err_cleanup, rc);
551         }
552
553         lprocfs_mgc_init_vars(&lvars);
554         lprocfs_obd_setup(obd, lvars.obd_vars);
555
556         spin_lock(&config_list_lock);
557         atomic_inc(&mgc_count);
558         if (atomic_read(&mgc_count) == 1) {
559                 rq_state &= ~RQ_STOP;
560                 cfs_waitq_init(&rq_waitq);
561         }
562         spin_unlock(&config_list_lock);
563
564         RETURN(rc);
565
566 err_cleanup:
567         client_obd_cleanup(obd);
568 err_decref:
569         ptlrpcd_decref();
570         RETURN(rc);
571 }
572
573 /* based on ll_mdc_blocking_ast */
574 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
575                             void *data, int flag)
576 {
577         struct lustre_handle lockh;
578         struct config_llog_data *cld = (struct config_llog_data *)data;
579         int rc = 0;
580         ENTRY;
581
582         switch (flag) {
583         case LDLM_CB_BLOCKING:
584                 /* mgs wants the lock, give it up... */
585                 LDLM_DEBUG(lock, "MGC blocking CB");
586                 ldlm_lock2handle(lock, &lockh);
587                 rc = ldlm_cli_cancel(&lockh);
588                 break;
589         case LDLM_CB_CANCELING: {
590                 /* We've given up the lock, prepare ourselves to update. */
591                 LDLM_DEBUG(lock, "MGC cancel CB");
592
593                 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
594                        lock->l_resource->lr_name.name[0],
595                        (char *)&lock->l_resource->lr_name.name[0]);
596
597                 if (!cld) {
598                         CERROR("missing data, won't requeue\n");
599                         break;
600                 }
601                 /* Are we done with this log? */
602                 if (cld->cld_stopping) {
603                         CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
604                                cld->cld_logname);
605                         config_log_put(cld);
606                         break;
607                 }
608                 /* Make sure not to re-enqueue when the mgc is stopping
609                    (we get called from client_disconnect_export) */
610                 if (!lock->l_conn_export ||
611                     !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
612                         CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
613                                cld->cld_logname);
614                         config_log_put(cld);
615                         break;
616                 }
617                 /* Did we fail to get the lock? */
618                 if (lock->l_req_mode != lock->l_granted_mode) {
619                         CDEBUG(D_MGC, "log %s: original grant failed, will "
620                                "requeue later\n", cld->cld_logname);
621                         /* Try to re-enqueue later */
622                         rc = mgc_requeue_add(cld, 1);
623                         break;
624                 }
625                 /* Re-enqueue now */
626                 rc = mgc_requeue_add(cld, 0);
627                 break;
628         }
629         default:
630                 LBUG();
631         }
632
633
634         if (rc) {
635                 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
636                        "blocking" : "cancel", rc);
637                 LDLM_ERROR(lock, "MGC ast");
638         }
639         RETURN(rc);
640 }
641
642 /* Take a config lock so we can get cancel notifications */
643 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
644                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
645                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
646                        void *data, __u32 lvb_len, void *lvb_swabber,
647                        struct lustre_handle *lockh)
648 {
649         struct config_llog_data *cld = (struct config_llog_data *)data;
650         struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
651                 ldlm_completion_ast, NULL, data};
652
653         int rc;
654         ENTRY;
655
656         CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
657                cld->cld_resid.name[0]);
658
659         /* We can only drop this config log ref when we drop the lock */
660         if (config_log_get(cld))
661                 RETURN(ELDLM_LOCK_ABORTED);
662
663         /* We need a callback for every lockholder, so don't try to
664            ldlm_lock_match (see rev 1.1.2.11.2.47) */
665
666         rc = ldlm_cli_enqueue(exp, NULL, &einfo, cld->cld_resid,
667                               NULL, flags, NULL, 0, NULL, lockh, 0);
668         /* A failed enqueue should still call the mgc_blocking_ast,
669            where it will be requeued if needed ("grant failed"). */
670
671         RETURN(rc);
672 }
673
674 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
675                       __u32 mode, struct lustre_handle *lockh, int flags,
676                       obd_off end)
677 {
678         ENTRY;
679
680         ldlm_lock_decref(lockh, mode);
681
682         RETURN(0);
683 }
684
685 #if 0
686 static int mgc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
687                          void *karg, void *uarg)
688 {
689         struct obd_device *obd = exp->exp_obd;
690         struct obd_ioctl_data *data = karg;
691         struct llog_ctxt *ctxt;
692         struct lvfs_run_ctxt saved;
693         int rc;
694         ENTRY;
695
696         if (!try_module_get(THIS_MODULE)) {
697                 CERROR("Can't get module. Is it alive?");
698                 return -EINVAL;
699         }
700         switch (cmd) {
701         /* REPLicator context */
702         case OBD_IOC_PARSE: {
703                 CERROR("MGC parsing llog %s\n", data->ioc_inlbuf1);
704                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
705                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
706                 GOTO(out, rc);
707         }
708 #ifdef __KERNEL__
709         case OBD_IOC_LLOG_INFO:
710         case OBD_IOC_LLOG_PRINT: {
711                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
712                 rc = llog_ioctl(ctxt, cmd, data);
713
714                 GOTO(out, rc);
715         }
716 #endif
717         /* ORIGinator context */
718         case OBD_IOC_DUMP_LOG: {
719                 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
720                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
721                 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
722                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
723                 if (rc)
724                         RETURN(rc);
725
726                 GOTO(out, rc);
727         }
728         default:
729                 CERROR("mgc_ioctl(): unrecognised ioctl %#x\n", cmd);
730                 GOTO(out, rc = -ENOTTY);
731         }
732 out:
733         module_put(THIS_MODULE);
734
735         return rc;
736 }
737 #endif
738
739 /* Send target_reg message to MGS */
740 static int mgc_target_register(struct obd_export *exp,
741                                struct mgs_target_info *mti)
742 {
743         struct ptlrpc_request *req;
744         struct mgs_target_info *req_mti, *rep_mti;
745         int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_mti) };
746         int rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*mti) };
747         int rc;
748         ENTRY;
749
750         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
751                               MGS_TARGET_REG, 2, size, NULL);
752         if (!req)
753                 RETURN(-ENOMEM);
754
755         req_mti = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_mti));
756         if (!req_mti) {
757                 ptlrpc_req_finished(req);
758                 RETURN(-ENOMEM);
759         }
760
761         memcpy(req_mti, mti, sizeof(*req_mti));
762         ptlrpc_req_set_repsize(req, 2, rep_size);
763         CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
764
765         rc = ptlrpc_queue_wait(req);
766         if (!rc) {
767                 rep_mti = lustre_swab_repbuf(req, REPLY_REC_OFF,
768                                              sizeof(*rep_mti),
769                                              lustre_swab_mgs_target_info);
770                 memcpy(mti, rep_mti, sizeof(*rep_mti));
771                 CDEBUG(D_MGC, "register %s got index = %d\n",
772                        mti->mti_svname, mti->mti_stripe_index);
773         }
774         ptlrpc_req_finished(req);
775
776         RETURN(rc);
777 }
778
779 /* Send parameter to MGS*/
780 static int mgc_set_mgs_param(struct obd_export *exp,
781                              struct mgs_send_param *msp)
782 {
783         struct ptlrpc_request *req;
784         struct mgs_send_param *req_msp, *rep_msp;
785         int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_msp) };
786         __u32 rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*msp) };
787         int rc;
788         ENTRY;
789
790         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
791                               MGS_SET_INFO, 2, size, NULL);
792         if (!req)
793                 RETURN(-ENOMEM);
794
795         req_msp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_msp));
796         if (!req_msp) {
797                 ptlrpc_req_finished(req);
798                 RETURN(-ENOMEM);
799         }
800
801         memcpy(req_msp, msp, sizeof(*req_msp));
802         ptlrpc_req_set_repsize(req, 2, rep_size);
803
804         rc = ptlrpc_queue_wait(req);
805         if (!rc) {
806                 rep_msp = lustre_swab_repbuf(req, REPLY_REC_OFF,
807                                              sizeof(*rep_msp), NULL);
808                 memcpy(msp, rep_msp, sizeof(*rep_msp));
809         }
810
811         ptlrpc_req_finished(req);
812
813         RETURN(rc);
814 }
815
816 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
817                        void *key, obd_count vallen, void *val,
818                        struct ptlrpc_request_set *set)
819 {
820         struct obd_import *imp = class_exp2cliimp(exp);
821         int rc = -EINVAL;
822         ENTRY;
823
824         /* Try to "recover" the initial connection; i.e. retry */
825         if (KEY_IS(KEY_INIT_RECOV)) {
826                 if (vallen != sizeof(int))
827                         RETURN(-EINVAL);
828                 spin_lock(&imp->imp_lock);
829                 imp->imp_initial_recov = *(int *)val;
830                 spin_unlock(&imp->imp_lock);
831                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
832                        exp->exp_obd->obd_name, imp->imp_initial_recov);
833                 RETURN(0);
834         }
835         /* Turn off initial_recov after we try all backup servers once */
836         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
837                 int value;
838                 if (vallen != sizeof(int))
839                         RETURN(-EINVAL);
840                 value = *(int *)val;
841                 spin_lock(&imp->imp_lock);
842                 imp->imp_initial_recov_bk = value > 0;
843                 /* Even after the initial connection, give up all comms if
844                    nobody answers the first time. */
845                 imp->imp_recon_bk = 1;
846                 spin_unlock(&imp->imp_lock);
847                 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
848                        imp->imp_obd->obd_name, value, imp->imp_initial_recov,
849                        imp->imp_deactive, imp->imp_invalid,
850                        imp->imp_replayable, imp->imp_obd->obd_replayable,
851                        ptlrpc_import_state_name(imp->imp_state));
852                 /* Resurrect if we previously died */
853                 if (imp->imp_invalid || value > 1)
854                         ptlrpc_reconnect_import(imp);
855                 RETURN(0);
856         }
857         /* FIXME move this to mgc_process_config */
858         if (KEY_IS(KEY_REGISTER_TARGET)) {
859                 struct mgs_target_info *mti;
860                 if (vallen != sizeof(struct mgs_target_info))
861                         RETURN(-EINVAL);
862                 mti = (struct mgs_target_info *)val;
863                 CDEBUG(D_MGC, "register_target %s %#x\n",
864                        mti->mti_svname, mti->mti_flags);
865                 rc =  mgc_target_register(exp, mti);
866                 RETURN(rc);
867         }
868         if (KEY_IS(KEY_SET_FS)) {
869                 struct super_block *sb = (struct super_block *)val;
870                 struct lustre_sb_info *lsi;
871                 if (vallen != sizeof(struct super_block))
872                         RETURN(-EINVAL);
873                 lsi = s2lsi(sb);
874                 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
875                 if (rc) {
876                         CERROR("set_fs got %d\n", rc);
877                 }
878                 RETURN(rc);
879         }
880         if (KEY_IS(KEY_CLEAR_FS)) {
881                 if (vallen != 0)
882                         RETURN(-EINVAL);
883                 rc = mgc_fs_cleanup(exp->exp_obd);
884                 if (rc) {
885                         CERROR("clear_fs got %d\n", rc);
886                 }
887                 RETURN(rc);
888         }
889         if (KEY_IS(KEY_SET_INFO)) {
890                 struct mgs_send_param *msp;
891
892                 msp = (struct mgs_send_param *)val;
893                 rc =  mgc_set_mgs_param(exp, msp);
894                 RETURN(rc);
895         }
896
897         RETURN(rc);
898 }
899
900 static int mgc_import_event(struct obd_device *obd,
901                             struct obd_import *imp,
902                             enum obd_import_event event)
903 {
904         int rc = 0;
905
906         LASSERT(imp->imp_obd == obd);
907         CDEBUG(D_MGC, "import event %#x\n", event);
908
909         switch (event) {
910         case IMP_EVENT_DISCON:
911                 break;
912         case IMP_EVENT_INACTIVE:
913                 break;
914         case IMP_EVENT_INVALIDATE: {
915                 struct ldlm_namespace *ns = obd->obd_namespace;
916                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
917                 break;
918         }
919         case IMP_EVENT_ACTIVE:
920                 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
921                 /* Clearing obd_no_recov allows us to continue pinging */
922                 obd->obd_no_recov = 0;
923                 break;
924         case IMP_EVENT_OCD:
925                 break;
926         default:
927                 CERROR("Unknown import event %#x\n", event);
928                 LBUG();
929         }
930         RETURN(rc);
931 }
932
933 static int mgc_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
934                          int *index)
935 {
936         struct llog_ctxt *ctxt;
937         int rc;
938         ENTRY;
939
940         rc = llog_setup(obd, LLOG_CONFIG_ORIG_CTXT, disk_obd, 0, NULL,
941                         &llog_lvfs_ops);
942         if (rc)
943                 RETURN(rc);
944
945         rc = llog_setup(obd, LLOG_CONFIG_REPL_CTXT, disk_obd, 0, NULL,
946                         &llog_client_ops);
947         if (rc == 0) {
948                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
949                 llog_initiator_connect(ctxt);
950                 llog_ctxt_put(ctxt);
951         } else {
952                 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
953                 if (ctxt)
954                         llog_cleanup(ctxt);
955         }
956
957         RETURN(rc);
958 }
959
960 static int mgc_llog_finish(struct obd_device *obd, int count)
961 {
962         int rc;
963         ENTRY;
964
965         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_REPL_CTXT));
966         rc = llog_cleanup(llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT));
967
968         RETURN(rc);
969 }
970
971 /* identical to mgs_log_is_empty */
972 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
973                             char *name)
974 {
975         struct lvfs_run_ctxt saved;
976         struct llog_handle *llh;
977         int rc = 0;
978
979         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
980         rc = llog_create(ctxt, &llh, NULL, name);
981         if (rc == 0) {
982                 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
983                 rc = llog_get_size(llh);
984                 llog_close(llh);
985         }
986         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
987         /* header is record 1 */
988         return(rc <= 1);
989 }
990
991 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
992                             void *data)
993 {
994         struct llog_rec_hdr local_rec = *rec;
995         struct llog_handle *local_llh = (struct llog_handle *)data;
996         char *cfg_buf = (char*) (rec + 1);
997         struct lustre_cfg *lcfg;
998         int rc = 0;
999         ENTRY;
1000
1001         /* Append all records */
1002         local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
1003         rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
1004                             (void *)cfg_buf, -1);
1005
1006         lcfg = (struct lustre_cfg *)cfg_buf;
1007         CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
1008                rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
1009                lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
1010
1011         RETURN(rc);
1012 }
1013
1014 /* Copy a remote log locally */
1015 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1016                          struct llog_ctxt *lctxt, char *logname)
1017 {
1018         struct llog_handle *local_llh, *remote_llh;
1019         struct obd_uuid *uuid;
1020         char *temp_log;
1021         int rc, rc2;
1022         ENTRY;
1023
1024         /* Write new log to a temp name, then vfs_rename over logname
1025            upon successful completion. */
1026
1027         OBD_ALLOC(temp_log, strlen(logname) + 1);
1028         if (!temp_log)
1029                 RETURN(-ENOMEM);
1030         sprintf(temp_log, "%sT", logname);
1031
1032         /* Make sure there's no old temp log */
1033         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1034         if (rc)
1035                 GOTO(out, rc);
1036         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1037         if (rc)
1038                 GOTO(out, rc);
1039         rc = llog_destroy(local_llh);
1040         llog_free_handle(local_llh);
1041         if (rc)
1042                 GOTO(out, rc);
1043
1044         /* open local log */
1045         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1046         if (rc)
1047                 GOTO(out, rc);
1048
1049         /* set the log header uuid for fun */
1050         OBD_ALLOC_PTR(uuid);
1051         obd_str2uuid(uuid, logname);
1052         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1053         OBD_FREE_PTR(uuid);
1054         if (rc)
1055                 GOTO(out_closel, rc);
1056
1057         /* open remote log */
1058         rc = llog_create(rctxt, &remote_llh, NULL, logname);
1059         if (rc)
1060                 GOTO(out_closel, rc);
1061         rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1062         if (rc)
1063                 GOTO(out_closer, rc);
1064
1065         /* Copy remote log */
1066         rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1067
1068 out_closer:
1069         rc2 = llog_close(remote_llh);
1070         if (!rc)
1071                 rc = rc2;
1072 out_closel:
1073         rc2 = llog_close(local_llh);
1074         if (!rc)
1075                 rc = rc2;
1076
1077         /* We've copied the remote log to the local temp log, now
1078            replace the old local log with the temp log. */
1079         if (!rc) {
1080                 struct client_obd *cli = &obd->u.cli;
1081                 LASSERT(cli);
1082                 LASSERT(cli->cl_mgc_configs_dir);
1083                 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1084                                    temp_log, logname);
1085         }
1086         CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1087 out:
1088         if (rc)
1089                 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1090         OBD_FREE(temp_log, strlen(logname) + 1);
1091         RETURN(rc);
1092 }
1093
1094 /* Get a config log from the MGS and process it.
1095    This func is called for both clients and servers. */
1096 static int mgc_process_log(struct obd_device *mgc,
1097                            struct config_llog_data *cld)
1098 {
1099         struct llog_ctxt *ctxt, *lctxt;
1100         struct lustre_handle lockh;
1101         struct client_obd *cli = &mgc->u.cli;
1102         struct lvfs_run_ctxt saved;
1103         struct lustre_sb_info *lsi;
1104         int rc = 0, rcl, flags = 0, must_pop = 0;
1105         ENTRY;
1106
1107         if (!cld || !cld->cld_cfg.cfg_sb) {
1108                 /* This should never happen */
1109                 CERROR("Missing cld, aborting log update\n");
1110                 RETURN(-EINVAL);
1111         }
1112
1113         /* Serialize update from the same log */
1114         down(&cld->cld_sem);
1115         if (cld->cld_stopping) {
1116                 up(&cld->cld_sem);
1117                 RETURN(0);
1118         }
1119
1120         OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1121
1122         lsi = s2lsi(cld->cld_cfg.cfg_sb);
1123
1124         CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1125                cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1126
1127         ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1128         if (!ctxt) {
1129                 up(&cld->cld_sem);
1130                 CERROR("missing llog context\n");
1131                 RETURN(-EINVAL);
1132         }
1133
1134         /* Get the cfg lock on the llog */
1135         rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1136                           LCK_CR, &flags, NULL, NULL, NULL,
1137                           cld, 0, NULL, &lockh);
1138         if (rcl)
1139                 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1140
1141         lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1142
1143         /* Copy the setup log locally if we can. Don't mess around if we're
1144            running an MGS though (logs are already local). */
1145         if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1146             (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1147             !IS_MGS(lsi->lsi_ldd)) {
1148                 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1149                 must_pop++;
1150                 if (rcl == 0)
1151                         /* Only try to copy log if we have the lock. */
1152                         rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1153                 if (rcl || rc) {
1154                         if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1155                                 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1156                                                    "log %s and no local copy."
1157                                                    "\n", cld->cld_logname);
1158                                 GOTO(out_pop, rc = -ENOTCONN);
1159                         }
1160                         CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1161                                       "copy for now, will try to update later.\n",
1162                                cld->cld_logname);
1163                 }
1164                 /* Now, whether we copied or not, start using the local llog.
1165                    If we failed to copy, we'll start using whatever the old
1166                    log has. */
1167                 llog_ctxt_put(ctxt);
1168                 ctxt = lctxt;
1169         }
1170
1171         /* logname and instance info should be the same, so use our
1172            copy of the instance for the update.  The cfg_last_idx will
1173            be updated here. */
1174         rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1175
1176 out_pop:
1177         llog_ctxt_put(ctxt);
1178         if (ctxt != lctxt)
1179                 llog_ctxt_put(lctxt);
1180         if (must_pop)
1181                 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1182
1183         /* Now drop the lock so MGS can revoke it */
1184         if (!rcl) {
1185                 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1186                                  LCK_CR, &lockh, 0, 0);
1187                 if (rcl)
1188                         CERROR("Can't drop cfg lock: %d\n", rcl);
1189         }
1190         up(&cld->cld_sem);
1191
1192         CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1193                mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1194
1195         RETURN(rc);
1196 }
1197
1198 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1199 {
1200         struct lustre_cfg *lcfg = buf;
1201         int cmd;
1202         int rc = 0;
1203         ENTRY;
1204
1205         switch(cmd = lcfg->lcfg_command) {
1206         case LCFG_LOV_ADD_OBD: {
1207                 /* Add any new target, not just osts */
1208                 struct mgs_target_info *mti;
1209
1210                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1211                     sizeof(struct mgs_target_info))
1212                         GOTO(out, rc = -EINVAL);
1213
1214                 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1215                 CDEBUG(D_MGC, "add_target %s %#x\n",
1216                        mti->mti_svname, mti->mti_flags);
1217                 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1218                 break;
1219         }
1220         case LCFG_LOV_DEL_OBD:
1221                 /* Remove target from the fs? */
1222                 /* FIXME */
1223                 CERROR("lov_del_obd unimplemented\n");
1224                 rc = -ENOSYS;
1225                 break;
1226         case LCFG_LOG_START: {
1227                 struct config_llog_data *cld;
1228                 struct config_llog_instance *cfg;
1229                 struct super_block *sb;
1230                 char *logname = lustre_cfg_string(lcfg, 1);
1231                 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1232                 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1233
1234                 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1235                        cfg->cfg_last_idx);
1236
1237                 /* We're only called through here on the initial mount */
1238                 rc = config_log_add(logname, cfg, sb);
1239                 if (rc)
1240                         break;
1241                 cld = config_log_find(logname, cfg);
1242                 if (IS_ERR(cld)) {
1243                         rc = PTR_ERR(cld);
1244                         break;
1245                 }
1246
1247                 /* COMPAT_146 */
1248                 /* FIXME only set this for old logs!  Right now this forces
1249                    us to always skip the "inside markers" check */
1250                 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1251
1252                 rc = mgc_process_log(obd, cld);
1253                 config_log_put(cld);
1254
1255                 break;
1256         }
1257         case LCFG_LOG_END: {
1258                 struct config_llog_instance *cfg = NULL;
1259                 char *logname = lustre_cfg_string(lcfg, 1);
1260                 if (lcfg->lcfg_bufcount >= 2)
1261                         cfg = (struct config_llog_instance *)lustre_cfg_buf(
1262                                 lcfg, 2);
1263                 rc = config_log_end(logname, cfg);
1264                 break;
1265         }
1266         default: {
1267                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1268                 GOTO(out, rc = -EINVAL);
1269
1270         }
1271         }
1272 out:
1273         RETURN(rc);
1274 }
1275
1276 struct obd_ops mgc_obd_ops = {
1277         .o_owner        = THIS_MODULE,
1278         .o_setup        = mgc_setup,
1279         .o_precleanup   = mgc_precleanup,
1280         .o_cleanup      = mgc_cleanup,
1281         .o_add_conn     = client_import_add_conn,
1282         .o_del_conn     = client_import_del_conn,
1283         .o_connect      = client_connect_import,
1284         .o_disconnect   = client_disconnect_export,
1285         //.o_enqueue      = mgc_enqueue,
1286         .o_cancel       = mgc_cancel,
1287         //.o_iocontrol    = mgc_iocontrol,
1288         .o_set_info_async = mgc_set_info_async,
1289         .o_import_event = mgc_import_event,
1290         .o_llog_init    = mgc_llog_init,
1291         .o_llog_finish  = mgc_llog_finish,
1292         .o_process_config = mgc_process_config,
1293 };
1294
1295 int __init mgc_init(void)
1296 {
1297         return class_register_type(&mgc_obd_ops, NULL, LUSTRE_MGC_NAME);
1298 }
1299
1300 #ifdef __KERNEL__
1301 static void /*__exit*/ mgc_exit(void)
1302 {
1303         class_unregister_type(LUSTRE_MGC_NAME);
1304 }
1305
1306 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1307 MODULE_DESCRIPTION("Lustre Management Client");
1308 MODULE_LICENSE("GPL");
1309
1310 module_init(mgc_init);
1311 module_exit(mgc_exit);
1312 #endif