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