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