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