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