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