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