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