Whamcloud - gitweb
LU-836 ldiskfs: require ldiskfs filesystem has journal
[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  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/mgc/mgc_request.c
39  *
40  * Author: Nathan Rutman <nathan@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_MGC
47 #define D_MGC D_CONFIG /*|D_WARNING*/
48
49 #ifdef __KERNEL__
50 # include <linux/module.h>
51 # include <linux/pagemap.h>
52 # include <linux/miscdevice.h>
53 # include <linux/init.h>
54 #else
55 # include <liblustre.h>
56 #endif
57
58 #include <obd_class.h>
59 #include <lustre_dlm.h>
60 #include <lprocfs_status.h>
61 #include <lustre_log.h>
62 #include <lustre_fsfilt.h>
63 #include <lustre_disk.h>
64 #include "mgc_internal.h"
65
66 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id,
67                           int type)
68 {
69         __u64 resname = 0;
70
71         if (len > 8) {
72                 CERROR("name too long: %s\n", name);
73                 return -EINVAL;
74         }
75         if (len <= 0) {
76                 CERROR("missing name: %s\n", name);
77                 return -EINVAL;
78         }
79         memcpy(&resname, name, len);
80
81         /* Always use the same endianness for the resid */
82         memset(res_id, 0, sizeof(*res_id));
83         res_id->name[0] = cpu_to_le64(resname);
84         /* XXX: unfortunately, sptlprc and config llog share one lock */
85         switch(type) {
86         case CONFIG_T_CONFIG:
87         case CONFIG_T_SPTLRPC:
88                 resname = 0;
89                 break;
90         case CONFIG_T_RECOVER:
91                 resname = type;
92                 break;
93         default:
94                 LBUG();
95         }
96         res_id->name[1] = cpu_to_le64(resname);
97         CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
98                res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
99         return 0;
100 }
101
102 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id, int type)
103 {
104         /* fsname is at most 8 chars long, maybe contain "-".
105          * e.g. "lustre", "SUN-000" */
106         return mgc_name2resid(fsname, strlen(fsname), res_id, type);
107 }
108 EXPORT_SYMBOL(mgc_fsname2resid);
109
110 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id, int type)
111 {
112         char *name_end;
113         int len;
114
115         /* logname consists of "fsname-nodetype".
116          * e.g. "lustre-MDT0001", "SUN-000-client" */
117         name_end = strrchr(logname, '-');
118         LASSERT(name_end);
119         len = name_end - logname;
120         return mgc_name2resid(logname, len, res_id, type);
121 }
122
123 /********************** config llog list **********************/
124 static CFS_LIST_HEAD(config_llog_list);
125 static cfs_spinlock_t config_list_lock = CFS_SPIN_LOCK_UNLOCKED;
126
127 /* Take a reference to a config log */
128 static int config_log_get(struct config_llog_data *cld)
129 {
130         ENTRY;
131         cfs_atomic_inc(&cld->cld_refcount);
132         CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
133                cfs_atomic_read(&cld->cld_refcount));
134         RETURN(0);
135 }
136
137 /* Drop a reference to a config log.  When no longer referenced,
138    we can free the config log data */
139 static void config_log_put(struct config_llog_data *cld)
140 {
141         ENTRY;
142
143         CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
144                cfs_atomic_read(&cld->cld_refcount));
145         LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
146
147         /* spinlock to make sure no item with 0 refcount in the list */
148         if (cfs_atomic_dec_and_lock(&cld->cld_refcount, &config_list_lock)) {
149                 cfs_list_del(&cld->cld_list_chain);
150                 cfs_spin_unlock(&config_list_lock);
151
152                 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
153
154                 if (cld->cld_recover)
155                         config_log_put(cld->cld_recover);
156                 if (cld->cld_sptlrpc)
157                         config_log_put(cld->cld_sptlrpc);
158                 if (cld_is_sptlrpc(cld))
159                         sptlrpc_conf_log_stop(cld->cld_logname);
160
161                 class_export_put(cld->cld_mgcexp);
162                 OBD_FREE(cld, sizeof(*cld) + strlen(cld->cld_logname) + 1);
163         }
164
165         EXIT;
166 }
167
168 /* Find a config log by name */
169 static
170 struct config_llog_data *config_log_find(char *logname,
171                                          struct config_llog_instance *cfg)
172 {
173         struct config_llog_data *cld;
174         struct config_llog_data *found = NULL;
175         void *                   instance;
176         ENTRY;
177
178         LASSERT(logname != NULL);
179
180         instance = cfg ? cfg->cfg_instance : NULL;
181         cfs_spin_lock(&config_list_lock);
182         cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
183                 /* check if instance equals */
184                 if (instance != cld->cld_cfg.cfg_instance)
185                         continue;
186
187                 /* instance may be NULL, should check name */
188                 if (strcmp(logname, cld->cld_logname) == 0) {
189                         found = cld;
190                         break;
191                 }
192         }
193         if (found) {
194                 cfs_atomic_inc(&found->cld_refcount);
195                 LASSERT(found->cld_stopping == 0 || cld_is_sptlrpc(found) == 0);
196         }
197         cfs_spin_unlock(&config_list_lock);
198         RETURN(found);
199 }
200
201 static
202 struct config_llog_data *do_config_log_add(struct obd_device *obd,
203                                            char *logname,
204                                            int type,
205                                            struct config_llog_instance *cfg,
206                                            struct super_block *sb)
207 {
208         struct config_llog_data *cld;
209         int                      rc;
210         ENTRY;
211
212         CDEBUG(D_MGC, "do adding config log %s:%p\n", logname,
213                cfg ? cfg->cfg_instance : 0);
214
215         OBD_ALLOC(cld, sizeof(*cld) + strlen(logname) + 1);
216         if (!cld)
217                 RETURN(ERR_PTR(-ENOMEM));
218
219         strcpy(cld->cld_logname, logname);
220         if (cfg)
221                 cld->cld_cfg = *cfg;
222         cfs_mutex_init(&cld->cld_lock);
223         cld->cld_cfg.cfg_last_idx = 0;
224         cld->cld_cfg.cfg_flags = 0;
225         cld->cld_cfg.cfg_sb = sb;
226         cld->cld_type = type;
227         cfs_atomic_set(&cld->cld_refcount, 1);
228
229         /* Keep the mgc around until we are done */
230         cld->cld_mgcexp = class_export_get(obd->obd_self_export);
231
232         if (cld_is_sptlrpc(cld)) {
233                 sptlrpc_conf_log_start(logname);
234                 cld->cld_cfg.cfg_obdname = obd->obd_name;
235         }
236
237         rc = mgc_logname2resid(logname, &cld->cld_resid, type);
238
239         cfs_spin_lock(&config_list_lock);
240         cfs_list_add(&cld->cld_list_chain, &config_llog_list);
241         cfs_spin_unlock(&config_list_lock);
242
243         if (rc) {
244                 config_log_put(cld);
245                 RETURN(ERR_PTR(rc));
246         }
247
248         if (cld_is_sptlrpc(cld)) {
249                 rc = mgc_process_log(obd, cld);
250                 if (rc)
251                         CERROR("failed processing sptlrpc log: %d\n", rc);
252         }
253
254         RETURN(cld);
255 }
256
257 static struct config_llog_data *config_recover_log_add(struct obd_device *obd,
258         char *fsname,
259         struct config_llog_instance *cfg,
260         struct super_block *sb)
261 {
262         struct config_llog_instance lcfg = *cfg;
263         struct lustre_sb_info *lsi = s2lsi(sb);
264         struct config_llog_data *cld;
265         char logname[32];
266
267         if ((lsi->lsi_flags & LSI_SERVER) && !IS_MDT(lsi->lsi_ldd))
268                 return NULL;
269
270         /* we have to use different llog for clients and mdts for cmd
271          * where only clients are notified if one of cmd server restarts */
272         LASSERT(strlen(fsname) < sizeof(logname) / 2);
273         strcpy(logname, fsname);
274         if (lsi->lsi_flags & LSI_SERVER) { /* mdt */
275                 LASSERT(lcfg.cfg_instance == NULL);
276                 lcfg.cfg_instance = sb;
277                 strcat(logname, "-mdtir");
278         } else {
279                 LASSERT(lcfg.cfg_instance != NULL);
280                 strcat(logname, "-cliir");
281         }
282
283         cld = do_config_log_add(obd, logname, CONFIG_T_RECOVER, &lcfg, sb);
284         return cld;
285 }
286
287
288 /** Add this log to the list of active logs watched by an MGC.
289  * Active means we're watching for updates.
290  * We have one active log per "mount" - client instance or servername.
291  * Each instance may be at a different point in the log.
292  */
293 static int config_log_add(struct obd_device *obd, char *logname,
294                           struct config_llog_instance *cfg,
295                           struct super_block *sb)
296 {
297         struct lustre_sb_info *lsi = s2lsi(sb);
298         struct config_llog_data *cld;
299         struct config_llog_data *sptlrpc_cld;
300         char                     seclogname[32];
301         char                    *ptr;
302         ENTRY;
303
304         CDEBUG(D_MGC, "adding config log %s:%p\n", logname, cfg->cfg_instance);
305
306         /*
307          * for each regular log, the depended sptlrpc log name is
308          * <fsname>-sptlrpc. multiple regular logs may share one sptlrpc log.
309          */
310         ptr = strrchr(logname, '-');
311         if (ptr == NULL || ptr - logname > 8) {
312                 CERROR("logname %s is too long\n", logname);
313                 RETURN(-EINVAL);
314         }
315
316         memcpy(seclogname, logname, ptr - logname);
317         strcpy(seclogname + (ptr - logname), "-sptlrpc");
318
319         sptlrpc_cld = config_log_find(seclogname, NULL);
320         if (sptlrpc_cld == NULL) {
321                 sptlrpc_cld = do_config_log_add(obd, seclogname,
322                                                 CONFIG_T_SPTLRPC, NULL, NULL);
323                 if (IS_ERR(sptlrpc_cld)) {
324                         CERROR("can't create sptlrpc log: %s\n", seclogname);
325                         RETURN(PTR_ERR(sptlrpc_cld));
326                 }
327         }
328
329         cld = do_config_log_add(obd, logname, CONFIG_T_CONFIG, cfg, sb);
330         if (IS_ERR(cld)) {
331                 CERROR("can't create log: %s\n", logname);
332                 config_log_put(sptlrpc_cld);
333                 RETURN(PTR_ERR(cld));
334         }
335
336         cld->cld_sptlrpc = sptlrpc_cld;
337
338         LASSERT(lsi->lsi_lmd);
339         if (!(lsi->lsi_lmd->lmd_flags & LMD_FLG_NOIR)) {
340                 struct config_llog_data *recover_cld;
341                 *strrchr(seclogname, '-') = 0;
342                 recover_cld = config_recover_log_add(obd, seclogname, cfg, sb);
343                 if (IS_ERR(recover_cld)) {
344                         config_log_put(cld);
345                         RETURN(PTR_ERR(recover_cld));
346                 }
347                 cld->cld_recover = recover_cld;
348         }
349
350         RETURN(0);
351 }
352
353 CFS_DEFINE_MUTEX(llog_process_lock);
354
355 /** Stop watching for updates on this log.
356  */
357 static int config_log_end(char *logname, struct config_llog_instance *cfg)
358 {
359         struct config_llog_data *cld;
360         struct config_llog_data *cld_sptlrpc = NULL;
361         struct config_llog_data *cld_recover = NULL;
362         int rc = 0;
363         ENTRY;
364
365         cld = config_log_find(logname, cfg);
366         if (cld == NULL)
367                 RETURN(-ENOENT);
368
369         cfs_mutex_lock(&cld->cld_lock);
370         /*
371          * if cld_stopping is set, it means we didn't start the log thus
372          * not owning the start ref. this can happen after previous umount:
373          * the cld still hanging there waiting for lock cancel, and we
374          * remount again but failed in the middle and call log_end without
375          * calling start_log.
376          */
377         if (unlikely(cld->cld_stopping)) {
378                 cfs_mutex_unlock(&cld->cld_lock);
379                 /* drop the ref from the find */
380                 config_log_put(cld);
381                 RETURN(rc);
382         }
383
384         cld->cld_stopping = 1;
385
386         cld_recover = cld->cld_recover;
387         cld->cld_recover = NULL;
388         cfs_mutex_unlock(&cld->cld_lock);
389
390         if (cld_recover) {
391                 cfs_mutex_lock(&cld_recover->cld_lock);
392                 cld_recover->cld_stopping = 1;
393                 cfs_mutex_unlock(&cld_recover->cld_lock);
394                 config_log_put(cld_recover);
395         }
396
397         cfs_spin_lock(&config_list_lock);
398         cld_sptlrpc = cld->cld_sptlrpc;
399         cld->cld_sptlrpc = NULL;
400         cfs_spin_unlock(&config_list_lock);
401
402         if (cld_sptlrpc)
403                 config_log_put(cld_sptlrpc);
404
405         /* drop the ref from the find */
406         config_log_put(cld);
407         /* drop the start ref */
408         config_log_put(cld);
409
410         CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
411                rc);
412         RETURN(rc);
413 }
414
415 int lprocfs_mgc_rd_ir_state(char *page, char **start, off_t off,
416                             int count, int *eof, void *data)
417 {
418         struct obd_device       *obd = data;
419         struct obd_import       *imp = obd->u.cli.cl_import;
420         struct obd_connect_data *ocd = &imp->imp_connect_data;
421         struct config_llog_data *cld;
422         int rc = 0;
423         ENTRY;
424
425         rc = snprintf(page, count, "imperative_recovery: %s\n",
426                       OCD_HAS_FLAG(ocd, IMP_RECOV) ? "ON" : "OFF");
427         rc += snprintf(page + rc, count - rc, "client_state:\n");
428
429         cfs_spin_lock(&config_list_lock);
430         cfs_list_for_each_entry(cld, &config_llog_list, cld_list_chain) {
431                 if (cld->cld_recover == NULL)
432                         continue;
433                 rc += snprintf(page + rc, count - rc,
434                                "    - { client: %s, nidtbl_version: %u }\n",
435                                cld->cld_logname,
436                                cld->cld_recover->cld_cfg.cfg_last_idx);
437         }
438         cfs_spin_unlock(&config_list_lock);
439
440         RETURN(rc);
441 }
442
443 /* reenqueue any lost locks */
444 #define RQ_RUNNING 0x1
445 #define RQ_NOW     0x2
446 #define RQ_LATER   0x4
447 #define RQ_STOP    0x8
448 static int                    rq_state = 0;
449 static cfs_waitq_t            rq_waitq;
450 static CFS_DECLARE_COMPLETION(rq_exit);
451
452 static void do_requeue(struct config_llog_data *cld)
453 {
454         ENTRY;
455         LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
456
457         /* Do not run mgc_process_log on a disconnected export or an
458            export which is being disconnected. Take the client
459            semaphore to make the check non-racy. */
460         cfs_down_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
461         if (cld->cld_mgcexp->exp_obd->u.cli.cl_conn_count != 0) {
462                 CDEBUG(D_MGC, "updating log %s\n", cld->cld_logname);
463                 mgc_process_log(cld->cld_mgcexp->exp_obd, cld);
464         } else {
465                 CDEBUG(D_MGC, "disconnecting, won't update log %s\n",
466                        cld->cld_logname);
467         }
468         cfs_up_read(&cld->cld_mgcexp->exp_obd->u.cli.cl_sem);
469
470         EXIT;
471 }
472
473 /* this timeout represents how many seconds MGC should wait before
474  * requeue config and recover lock to the MGS. We need to randomize this
475  * in order to not flood the MGS.
476  */
477 #define MGC_TIMEOUT_MIN_SECONDS   5
478 #define MGC_TIMEOUT_RAND_CENTISEC 0x1ff /* ~500 */
479
480 static int mgc_requeue_thread(void *data)
481 {
482         char name[] = "ll_cfg_requeue";
483         int rc = 0;
484         ENTRY;
485
486         cfs_daemonize(name);
487
488         CDEBUG(D_MGC, "Starting requeue thread\n");
489
490         /* Keep trying failed locks periodically */
491         cfs_spin_lock(&config_list_lock);
492         rq_state |= RQ_RUNNING;
493         while (1) {
494                 struct l_wait_info lwi;
495                 struct config_llog_data *cld, *cld_prev;
496                 int rand = cfs_rand() & MGC_TIMEOUT_RAND_CENTISEC;
497                 int stopped = !!(rq_state & RQ_STOP);
498                 int to;
499
500                 /* Any new or requeued lostlocks will change the state */
501                 rq_state &= ~(RQ_NOW | RQ_LATER);
502                 cfs_spin_unlock(&config_list_lock);
503
504                 /* Always wait a few seconds to allow the server who
505                    caused the lock revocation to finish its setup, plus some
506                    random so everyone doesn't try to reconnect at once. */
507                 to = MGC_TIMEOUT_MIN_SECONDS * CFS_HZ;
508                 to += rand * CFS_HZ / 100; /* rand is centi-seconds */
509                 lwi = LWI_TIMEOUT(to, NULL, NULL);
510                 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi);
511
512                 /*
513                  * iterate & processing through the list. for each cld, process
514                  * its depending sptlrpc cld firstly (if any) and then itself.
515                  *
516                  * it's guaranteed any item in the list must have
517                  * reference > 0; and if cld_lostlock is set, at
518                  * least one reference is taken by the previous enqueue.
519                  */
520                 cld_prev = NULL;
521
522                 cfs_spin_lock(&config_list_lock);
523                 cfs_list_for_each_entry(cld, &config_llog_list,
524                                         cld_list_chain) {
525                         if (!cld->cld_lostlock)
526                                 continue;
527
528                         cfs_spin_unlock(&config_list_lock);
529
530                         LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
531
532                         /* Whether we enqueued again or not in mgc_process_log,
533                          * we're done with the ref from the old enqueue */
534                         if (cld_prev)
535                                 config_log_put(cld_prev);
536                         cld_prev = cld;
537
538                         cld->cld_lostlock = 0;
539                         if (likely(!stopped))
540                                 do_requeue(cld);
541
542                         cfs_spin_lock(&config_list_lock);
543                 }
544                 cfs_spin_unlock(&config_list_lock);
545                 if (cld_prev)
546                         config_log_put(cld_prev);
547
548                 /* break after scanning the list so that we can drop
549                  * refcount to losing lock clds */
550                 if (unlikely(stopped)) {
551                         cfs_spin_lock(&config_list_lock);
552                         break;
553                 }
554
555                 /* Wait a bit to see if anyone else needs a requeue */
556                 lwi = (struct l_wait_info) { 0 };
557                 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
558                              &lwi);
559                 cfs_spin_lock(&config_list_lock);
560         }
561         /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
562         rq_state &= ~RQ_RUNNING;
563         cfs_spin_unlock(&config_list_lock);
564
565         cfs_complete(&rq_exit);
566
567         CDEBUG(D_MGC, "Ending requeue thread\n");
568         RETURN(rc);
569 }
570
571 /* Add a cld to the list to requeue.  Start the requeue thread if needed.
572    We are responsible for dropping the config log reference from here on out. */
573 static void mgc_requeue_add(struct config_llog_data *cld)
574 {
575         ENTRY;
576
577         CDEBUG(D_INFO, "log %s: requeue (r=%d sp=%d st=%x)\n",
578                cld->cld_logname, cfs_atomic_read(&cld->cld_refcount),
579                cld->cld_stopping, rq_state);
580         LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
581
582         cfs_mutex_lock(&cld->cld_lock);
583         if (cld->cld_stopping || cld->cld_lostlock) {
584                 cfs_mutex_unlock(&cld->cld_lock);
585                 RETURN_EXIT;
586         }
587         /* this refcount will be released in mgc_requeue_thread. */
588         config_log_get(cld);
589         cld->cld_lostlock = 1;
590         cfs_mutex_unlock(&cld->cld_lock);
591
592         /* Hold lock for rq_state */
593         cfs_spin_lock(&config_list_lock);
594         if (rq_state & RQ_STOP) {
595                 cfs_spin_unlock(&config_list_lock);
596                 cld->cld_lostlock = 0;
597                 config_log_put(cld);
598         } else {
599                 rq_state |= RQ_NOW;
600                 cfs_spin_unlock(&config_list_lock);
601                 cfs_waitq_signal(&rq_waitq);
602         }
603         EXIT;
604 }
605
606 /********************** class fns **********************/
607
608 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
609                         struct vfsmount *mnt)
610 {
611         struct lvfs_run_ctxt saved;
612         struct lustre_sb_info *lsi = s2lsi(sb);
613         struct client_obd *cli = &obd->u.cli;
614         struct dentry *dentry;
615         char *label;
616         int err = 0;
617         ENTRY;
618
619         LASSERT(lsi);
620         LASSERT(lsi->lsi_srv_mnt == mnt);
621
622         /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
623         cfs_down(&cli->cl_mgc_sem);
624
625         cfs_cleanup_group_info();
626
627         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
628         if (IS_ERR(obd->obd_fsops)) {
629                 cfs_up(&cli->cl_mgc_sem);
630                 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
631                        PTR_ERR(obd->obd_fsops));
632                 RETURN(PTR_ERR(obd->obd_fsops));
633         }
634
635         cli->cl_mgc_vfsmnt = mnt;
636         err = fsfilt_setup(obd, mnt->mnt_sb);
637         if (err)
638                 GOTO(err_ops, err);
639
640         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
641         obd->obd_lvfs_ctxt.pwdmnt = mnt;
642         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
643         obd->obd_lvfs_ctxt.fs = get_ds();
644
645         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
646         dentry = ll_lookup_one_len(MOUNT_CONFIGS_DIR, cfs_fs_pwd(current->fs),
647                                    strlen(MOUNT_CONFIGS_DIR));
648         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
649         if (IS_ERR(dentry)) {
650                 err = PTR_ERR(dentry);
651                 CERROR("cannot lookup %s directory: rc = %d\n",
652                        MOUNT_CONFIGS_DIR, err);
653                 GOTO(err_ops, err);
654         }
655         cli->cl_mgc_configs_dir = dentry;
656
657         /* We take an obd ref to insure that we can't get to mgc_cleanup
658            without calling mgc_fs_cleanup first. */
659         class_incref(obd, "mgc_fs", obd);
660
661         label = fsfilt_get_label(obd, mnt->mnt_sb);
662         if (label)
663                 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
664
665         /* We keep the cl_mgc_sem until mgc_fs_cleanup */
666         RETURN(0);
667
668 err_ops:
669         fsfilt_put_ops(obd->obd_fsops);
670         obd->obd_fsops = NULL;
671         cli->cl_mgc_vfsmnt = NULL;
672         cfs_up(&cli->cl_mgc_sem);
673         RETURN(err);
674 }
675
676 static int mgc_fs_cleanup(struct obd_device *obd)
677 {
678         struct client_obd *cli = &obd->u.cli;
679         int rc = 0;
680         ENTRY;
681
682         LASSERT(cli->cl_mgc_vfsmnt != NULL);
683
684         if (cli->cl_mgc_configs_dir != NULL) {
685                 struct lvfs_run_ctxt saved;
686                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
687                 l_dput(cli->cl_mgc_configs_dir);
688                 cli->cl_mgc_configs_dir = NULL;
689                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
690                 class_decref(obd, "mgc_fs", obd);
691         }
692
693         cli->cl_mgc_vfsmnt = NULL;
694         if (obd->obd_fsops)
695                 fsfilt_put_ops(obd->obd_fsops);
696
697         cfs_up(&cli->cl_mgc_sem);
698
699         RETURN(rc);
700 }
701
702 static cfs_atomic_t mgc_count = CFS_ATOMIC_INIT(0);
703 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
704 {
705         int rc = 0;
706         ENTRY;
707
708         switch (stage) {
709         case OBD_CLEANUP_EARLY:
710                 break;
711         case OBD_CLEANUP_EXPORTS:
712                 if (cfs_atomic_dec_and_test(&mgc_count)) {
713                         int running;
714                         /* stop requeue thread */
715                         cfs_spin_lock(&config_list_lock);
716                         running = rq_state & RQ_RUNNING;
717                         if (running)
718                                 rq_state |= RQ_STOP;
719                         cfs_spin_unlock(&config_list_lock);
720                         if (running) {
721                                 cfs_waitq_signal(&rq_waitq);
722                                 cfs_wait_for_completion(&rq_exit);
723                         }
724                 }
725                 obd_cleanup_client_import(obd);
726                 rc = obd_llog_finish(obd, 0);
727                 if (rc != 0)
728                         CERROR("failed to cleanup llogging subsystems\n");
729                 break;
730         }
731         RETURN(rc);
732 }
733
734 static int mgc_cleanup(struct obd_device *obd)
735 {
736         struct client_obd *cli = &obd->u.cli;
737         int rc;
738         ENTRY;
739
740         LASSERT(cli->cl_mgc_vfsmnt == NULL);
741
742         /* COMPAT_146 - old config logs may have added profiles we don't
743            know about */
744         if (obd->obd_type->typ_refcnt <= 1)
745                 /* Only for the last mgc */
746                 class_del_profiles();
747
748         lprocfs_obd_cleanup(obd);
749         ptlrpcd_decref();
750
751         rc = client_obd_cleanup(obd);
752         RETURN(rc);
753 }
754
755 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
756 {
757         struct lprocfs_static_vars lvars;
758         int rc;
759         ENTRY;
760
761         ptlrpcd_addref();
762
763         rc = client_obd_setup(obd, lcfg);
764         if (rc)
765                 GOTO(err_decref, rc);
766
767         rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
768         if (rc) {
769                 CERROR("failed to setup llogging subsystems\n");
770                 GOTO(err_cleanup, rc);
771         }
772
773         lprocfs_mgc_init_vars(&lvars);
774         lprocfs_obd_setup(obd, lvars.obd_vars);
775         sptlrpc_lprocfs_cliobd_attach(obd);
776
777         if (cfs_atomic_inc_return(&mgc_count) == 1) {
778                 rq_state = 0;
779                 cfs_waitq_init(&rq_waitq);
780
781                 /* start requeue thread */
782                 rc = cfs_create_thread(mgc_requeue_thread, NULL,
783                                        CFS_DAEMON_FLAGS);
784                 if (rc < 0) {
785                         CERROR("%s: Cannot start requeue thread (%d),"
786                                "no more log updates!\n",
787                                obd->obd_name, rc);
788                         GOTO(err_cleanup, rc);
789                 }
790                 /* rc is the pid of mgc_requeue_thread. */
791                 rc = 0;
792         }
793
794         RETURN(rc);
795
796 err_cleanup:
797         client_obd_cleanup(obd);
798 err_decref:
799         ptlrpcd_decref();
800         RETURN(rc);
801 }
802
803 /* based on ll_mdc_blocking_ast */
804 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
805                             void *data, int flag)
806 {
807         struct lustre_handle lockh;
808         struct config_llog_data *cld = (struct config_llog_data *)data;
809         int rc = 0;
810         ENTRY;
811
812         switch (flag) {
813         case LDLM_CB_BLOCKING:
814                 /* mgs wants the lock, give it up... */
815                 LDLM_DEBUG(lock, "MGC blocking CB");
816                 ldlm_lock2handle(lock, &lockh);
817                 rc = ldlm_cli_cancel(&lockh);
818                 break;
819         case LDLM_CB_CANCELING:
820                 /* We've given up the lock, prepare ourselves to update. */
821                 LDLM_DEBUG(lock, "MGC cancel CB");
822
823                 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
824                        lock->l_resource->lr_name.name[0],
825                        (char *)&lock->l_resource->lr_name.name[0]);
826
827                 if (!cld) {
828                         CDEBUG(D_INFO, "missing data, won't requeue\n");
829                         break;
830                 }
831
832                 /* held at mgc_process_log(). */
833                 LASSERT(cfs_atomic_read(&cld->cld_refcount) > 0);
834                 /* Are we done with this log? */
835                 if (cld->cld_stopping) {
836                         CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
837                                cld->cld_logname);
838                         config_log_put(cld);
839                         break;
840                 }
841                 /* Make sure not to re-enqueue when the mgc is stopping
842                    (we get called from client_disconnect_export) */
843                 if (!lock->l_conn_export ||
844                     !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
845                         CDEBUG(D_MGC, "log %.8s: disconnecting, won't requeue\n",
846                                cld->cld_logname);
847                         config_log_put(cld);
848                         break;
849                 }
850
851                 /* Re-enqueue now */
852                 mgc_requeue_add(cld);
853                 config_log_put(cld);
854                 break;
855         default:
856                 LBUG();
857         }
858
859         RETURN(rc);
860 }
861
862 /* Not sure where this should go... */
863 #define  MGC_ENQUEUE_LIMIT 50
864 #define  MGC_TARGET_REG_LIMIT 10
865 #define  MGC_SEND_PARAM_LIMIT 10
866
867 /* Send parameter to MGS*/
868 static int mgc_set_mgs_param(struct obd_export *exp,
869                              struct mgs_send_param *msp)
870 {
871         struct ptlrpc_request *req;
872         struct mgs_send_param *req_msp, *rep_msp;
873         int rc;
874         ENTRY;
875
876         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
877                                         &RQF_MGS_SET_INFO, LUSTRE_MGS_VERSION,
878                                         MGS_SET_INFO);
879         if (!req)
880                 RETURN(-ENOMEM);
881
882         req_msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
883         if (!req_msp) {
884                 ptlrpc_req_finished(req);
885                 RETURN(-ENOMEM);
886         }
887
888         memcpy(req_msp, msp, sizeof(*req_msp));
889         ptlrpc_request_set_replen(req);
890
891         /* Limit how long we will wait for the enqueue to complete */
892         req->rq_delay_limit = MGC_SEND_PARAM_LIMIT;
893         rc = ptlrpc_queue_wait(req);
894         if (!rc) {
895                 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
896                 memcpy(msp, rep_msp, sizeof(*rep_msp));
897         }
898
899         ptlrpc_req_finished(req);
900
901         RETURN(rc);
902 }
903
904 /* Take a config lock so we can get cancel notifications */
905 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
906                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
907                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
908                        void *data, __u32 lvb_len, void *lvb_swabber,
909                        struct lustre_handle *lockh)
910 {
911         struct config_llog_data *cld = (struct config_llog_data *)data;
912         struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
913                          ldlm_completion_ast, NULL, NULL, NULL };
914         struct ptlrpc_request *req;
915         int short_limit = cld_is_sptlrpc(cld);
916         int rc;
917         ENTRY;
918
919         CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
920                cld->cld_resid.name[0]);
921
922         /* We need a callback for every lockholder, so don't try to
923            ldlm_lock_match (see rev 1.1.2.11.2.47) */
924         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
925                                         &RQF_LDLM_ENQUEUE, LUSTRE_DLM_VERSION,
926                                         LDLM_ENQUEUE);
927         if (req == NULL)
928                 RETURN(-ENOMEM);
929         ptlrpc_request_set_replen(req);
930
931         /* check if this is server or client */
932         if (cld->cld_cfg.cfg_sb) {
933                 struct lustre_sb_info *lsi = s2lsi(cld->cld_cfg.cfg_sb);
934                 if (lsi && (lsi->lsi_flags & LSI_SERVER))
935                         short_limit = 1;
936         }
937         /* Limit how long we will wait for the enqueue to complete */
938         req->rq_delay_limit = short_limit ? 5 : MGC_ENQUEUE_LIMIT;
939         rc = ldlm_cli_enqueue(exp, &req, &einfo, &cld->cld_resid, NULL, flags,
940                               NULL, 0, lockh, 0);
941         /* A failed enqueue should still call the mgc_blocking_ast,
942            where it will be requeued if needed ("grant failed"). */
943         ptlrpc_req_finished(req);
944         RETURN(rc);
945 }
946
947 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
948                       __u32 mode, struct lustre_handle *lockh)
949 {
950         ENTRY;
951
952         ldlm_lock_decref(lockh, mode);
953
954         RETURN(0);
955 }
956
957 static void mgc_notify_active(struct obd_device *unused)
958 {
959         /* wakeup mgc_requeue_thread to requeue mgc lock */
960         cfs_spin_lock(&config_list_lock);
961         rq_state |= RQ_NOW;
962         cfs_spin_unlock(&config_list_lock);
963         cfs_waitq_signal(&rq_waitq);
964
965         /* TODO: Help the MGS rebuild nidtbl. -jay */
966 }
967
968 /* Send target_reg message to MGS */
969 static int mgc_target_register(struct obd_export *exp,
970                                struct mgs_target_info *mti)
971 {
972         struct ptlrpc_request  *req;
973         struct mgs_target_info *req_mti, *rep_mti;
974         int                     rc;
975         ENTRY;
976
977         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
978                                         &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
979                                         MGS_TARGET_REG);
980         if (req == NULL)
981                 RETURN(-ENOMEM);
982
983         req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
984         if (!req_mti) {
985                 ptlrpc_req_finished(req);
986                 RETURN(-ENOMEM);
987         }
988
989         memcpy(req_mti, mti, sizeof(*req_mti));
990         ptlrpc_request_set_replen(req);
991         CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
992         /* Limit how long we will wait for the enqueue to complete */
993         req->rq_delay_limit = MGC_TARGET_REG_LIMIT;
994
995         rc = ptlrpc_queue_wait(req);
996         if (!rc) {
997                 rep_mti = req_capsule_server_get(&req->rq_pill,
998                                                  &RMF_MGS_TARGET_INFO);
999                 memcpy(mti, rep_mti, sizeof(*rep_mti));
1000                 CDEBUG(D_MGC, "register %s got index = %d\n",
1001                        mti->mti_svname, mti->mti_stripe_index);
1002         }
1003         ptlrpc_req_finished(req);
1004
1005         RETURN(rc);
1006 }
1007
1008 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
1009                        void *key, obd_count vallen, void *val,
1010                        struct ptlrpc_request_set *set)
1011 {
1012         int rc = -EINVAL;
1013         ENTRY;
1014
1015         /* Turn off initial_recov after we try all backup servers once */
1016         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
1017                 struct obd_import *imp = class_exp2cliimp(exp);
1018                 int value;
1019                 if (vallen != sizeof(int))
1020                         RETURN(-EINVAL);
1021                 value = *(int *)val;
1022                 CDEBUG(D_MGC, "InitRecov %s %d/d%d:i%d:r%d:or%d:%s\n",
1023                        imp->imp_obd->obd_name, value,
1024                        imp->imp_deactive, imp->imp_invalid,
1025                        imp->imp_replayable, imp->imp_obd->obd_replayable,
1026                        ptlrpc_import_state_name(imp->imp_state));
1027                 /* Resurrect if we previously died */
1028                 if ((imp->imp_state != LUSTRE_IMP_FULL &&
1029                      imp->imp_state != LUSTRE_IMP_NEW) || value > 1)
1030                         ptlrpc_reconnect_import(imp);
1031                 RETURN(0);
1032         }
1033         /* FIXME move this to mgc_process_config */
1034         if (KEY_IS(KEY_REGISTER_TARGET)) {
1035                 struct mgs_target_info *mti;
1036                 if (vallen != sizeof(struct mgs_target_info))
1037                         RETURN(-EINVAL);
1038                 mti = (struct mgs_target_info *)val;
1039                 CDEBUG(D_MGC, "register_target %s %#x\n",
1040                        mti->mti_svname, mti->mti_flags);
1041                 rc =  mgc_target_register(exp, mti);
1042                 RETURN(rc);
1043         }
1044         if (KEY_IS(KEY_SET_FS)) {
1045                 struct super_block *sb = (struct super_block *)val;
1046                 struct lustre_sb_info *lsi;
1047                 if (vallen != sizeof(struct super_block))
1048                         RETURN(-EINVAL);
1049                 lsi = s2lsi(sb);
1050                 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
1051                 if (rc) {
1052                         CERROR("set_fs got %d\n", rc);
1053                 }
1054                 RETURN(rc);
1055         }
1056         if (KEY_IS(KEY_CLEAR_FS)) {
1057                 if (vallen != 0)
1058                         RETURN(-EINVAL);
1059                 rc = mgc_fs_cleanup(exp->exp_obd);
1060                 if (rc) {
1061                         CERROR("clear_fs got %d\n", rc);
1062                 }
1063                 RETURN(rc);
1064         }
1065         if (KEY_IS(KEY_SET_INFO)) {
1066                 struct mgs_send_param *msp;
1067
1068                 msp = (struct mgs_send_param *)val;
1069                 rc =  mgc_set_mgs_param(exp, msp);
1070                 RETURN(rc);
1071         }
1072         if (KEY_IS(KEY_MGSSEC)) {
1073                 struct client_obd     *cli = &exp->exp_obd->u.cli;
1074                 struct sptlrpc_flavor  flvr;
1075
1076                 /*
1077                  * empty string means using current flavor, if which haven't
1078                  * been set yet, set it as null.
1079                  *
1080                  * if flavor has been set previously, check the asking flavor
1081                  * must match the existing one.
1082                  */
1083                 if (vallen == 0) {
1084                         if (cli->cl_flvr_mgc.sf_rpc != SPTLRPC_FLVR_INVALID)
1085                                 RETURN(0);
1086                         val = "null";
1087                         vallen = 4;
1088                 }
1089
1090                 rc = sptlrpc_parse_flavor(val, &flvr);
1091                 if (rc) {
1092                         CERROR("invalid sptlrpc flavor %s to MGS\n",
1093                                (char *) val);
1094                         RETURN(rc);
1095                 }
1096
1097                 /*
1098                  * caller already hold a mutex
1099                  */
1100                 if (cli->cl_flvr_mgc.sf_rpc == SPTLRPC_FLVR_INVALID) {
1101                         cli->cl_flvr_mgc = flvr;
1102                 } else if (memcmp(&cli->cl_flvr_mgc, &flvr,
1103                                   sizeof(flvr)) != 0) {
1104                         char    str[20];
1105
1106                         sptlrpc_flavor2name(&cli->cl_flvr_mgc,
1107                                             str, sizeof(str));
1108                         LCONSOLE_ERROR("asking sptlrpc flavor %s to MGS but "
1109                                        "currently %s is in use\n",
1110                                        (char *) val, str);
1111                         rc = -EPERM;
1112                 }
1113                 RETURN(rc);
1114         }
1115
1116         RETURN(rc);
1117 }
1118
1119 static int mgc_get_info(struct obd_export *exp, __u32 keylen, void *key,
1120                         __u32 *vallen, void *val, struct lov_stripe_md *unused)
1121 {
1122         int rc = -EINVAL;
1123
1124         if (KEY_IS(KEY_CONN_DATA)) {
1125                 struct obd_import *imp = class_exp2cliimp(exp);
1126                 struct obd_connect_data *data = val;
1127
1128                 if (*vallen == sizeof(*data)) {
1129                         *data = imp->imp_connect_data;
1130                         rc = 0;
1131                 }
1132         }
1133
1134         return rc;
1135 }
1136
1137 static int mgc_import_event(struct obd_device *obd,
1138                             struct obd_import *imp,
1139                             enum obd_import_event event)
1140 {
1141         int rc = 0;
1142
1143         LASSERT(imp->imp_obd == obd);
1144         CDEBUG(D_MGC, "import event %#x\n", event);
1145
1146         switch (event) {
1147         case IMP_EVENT_DISCON:
1148                 /* MGC imports should not wait for recovery */
1149                 break;
1150         case IMP_EVENT_INACTIVE:
1151                 break;
1152         case IMP_EVENT_INVALIDATE: {
1153                 struct ldlm_namespace *ns = obd->obd_namespace;
1154                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
1155                 break;
1156         }
1157         case IMP_EVENT_ACTIVE:
1158                 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
1159                 /* Clearing obd_no_recov allows us to continue pinging */
1160                 obd->obd_no_recov = 0;
1161                 mgc_notify_active(obd);
1162                 break;
1163         case IMP_EVENT_OCD:
1164                 break;
1165         case IMP_EVENT_DEACTIVATE:
1166         case IMP_EVENT_ACTIVATE:
1167                 break;
1168         default:
1169                 CERROR("Unknown import event %#x\n", event);
1170                 LBUG();
1171         }
1172         RETURN(rc);
1173 }
1174
1175 static int mgc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
1176                          struct obd_device *tgt, int *index)
1177 {
1178         struct llog_ctxt *ctxt;
1179         int rc;
1180         ENTRY;
1181
1182         LASSERT(olg == &obd->obd_olg);
1183
1184         rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
1185                         &llog_lvfs_ops);
1186         if (rc)
1187                 RETURN(rc);
1188
1189         rc = llog_setup(obd, olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
1190                         &llog_client_ops);
1191         if (rc == 0) {
1192                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1193                 if (!ctxt) {
1194                         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1195                         if (ctxt)
1196                                 llog_cleanup(ctxt);
1197                         RETURN(-ENODEV);
1198                 }
1199                 llog_initiator_connect(ctxt);
1200                 llog_ctxt_put(ctxt);
1201         } else {
1202                 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1203                 if (ctxt)
1204                         llog_cleanup(ctxt);
1205         }
1206
1207         RETURN(rc);
1208 }
1209
1210 static int mgc_llog_finish(struct obd_device *obd, int count)
1211 {
1212         struct llog_ctxt *ctxt;
1213         int rc = 0, rc2 = 0;
1214         ENTRY;
1215
1216         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
1217         if (ctxt)
1218                 rc = llog_cleanup(ctxt);
1219
1220         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1221         if (ctxt)
1222                 rc2 = llog_cleanup(ctxt);
1223
1224         if (!rc)
1225                 rc = rc2;
1226
1227         RETURN(rc);
1228 }
1229
1230 enum {
1231         CONFIG_READ_NRPAGES_INIT = 1 << (20 - CFS_PAGE_SHIFT),
1232         CONFIG_READ_NRPAGES      = 4
1233 };
1234
1235 static int mgc_apply_recover_logs(struct obd_device *mgc,
1236                                   struct config_llog_data *cld,
1237                                   __u64 max_version,
1238                                   void *data, int datalen)
1239 {
1240         struct config_llog_instance *cfg = &cld->cld_cfg;
1241         struct lustre_sb_info       *lsi = s2lsi(cfg->cfg_sb);
1242         struct mgs_nidtbl_entry *entry;
1243         struct lustre_cfg       *lcfg;
1244         struct lustre_cfg_bufs   bufs;
1245         u64   prev_version = 0;
1246         char *inst;
1247         char *buf;
1248         int   bufsz;
1249         int   pos;
1250         int   rc  = 0;
1251         int   off = 0;
1252         ENTRY;
1253
1254         LASSERT(cfg->cfg_instance != NULL);
1255         LASSERT(cfg->cfg_sb == cfg->cfg_instance);
1256
1257         OBD_ALLOC(inst, CFS_PAGE_SIZE);
1258         if (inst == NULL)
1259                 RETURN(-ENOMEM);
1260
1261         if (!(lsi->lsi_flags & LSI_SERVER)) {
1262                 pos = sprintf(inst, "%p", cfg->cfg_instance);
1263         } else {
1264                 LASSERT(IS_MDT(lsi->lsi_ldd));
1265                 pos = sprintf(inst, "MDT%04x", lsi->lsi_ldd->ldd_svindex);
1266         }
1267
1268         ++pos;
1269         buf   = inst + pos;
1270         bufsz = CFS_PAGE_SIZE - pos;
1271
1272         while (datalen > 0) {
1273                 int   entry_len = sizeof(*entry);
1274                 int   is_ost;
1275                 struct obd_device *obd;
1276                 char *obdname;
1277                 char *cname;
1278                 char *params;
1279                 char *uuid;
1280
1281                 rc = -EINVAL;
1282                 if (datalen < sizeof(*entry))
1283                         break;
1284
1285                 entry = (typeof(entry))(data + off);
1286
1287                 /* sanity check */
1288                 if (entry->mne_nid_type != 0) /* only support type 0 for ipv4 */
1289                         break;
1290                 if (entry->mne_nid_count == 0) /* at least one nid entry */
1291                         break;
1292                 if (entry->mne_nid_size != sizeof(lnet_nid_t))
1293                         break;
1294
1295                 entry_len += entry->mne_nid_count * entry->mne_nid_size;
1296                 if (datalen < entry_len) /* must have entry_len at least */
1297                         break;
1298
1299                 lustre_swab_mgs_nidtbl_entry(entry);
1300                 LASSERT(entry->mne_length <= CFS_PAGE_SIZE);
1301                 if (entry->mne_length < entry_len)
1302                         break;
1303
1304                 off     += entry->mne_length;
1305                 datalen -= entry->mne_length;
1306                 if (datalen < 0)
1307                         break;
1308
1309                 if (entry->mne_version > max_version) {
1310                         CERROR("entry index(%lld) is over max_index(%lld)\n",
1311                                entry->mne_version, max_version);
1312                         break;
1313                 }
1314
1315                 if (prev_version >= entry->mne_version) {
1316                         CERROR("index unsorted, prev %lld, now %lld\n",
1317                                prev_version, entry->mne_version);
1318                         break;
1319                 }
1320                 prev_version = entry->mne_version;
1321
1322                 /*
1323                  * Write a string with format "nid::instance" to
1324                  * lustre/<osc|mdc>/<target>-<osc|mdc>-<instance>/import.
1325                  */
1326
1327                 is_ost = entry->mne_type == LDD_F_SV_TYPE_OST;
1328                 memset(buf, 0, bufsz);
1329                 obdname = buf;
1330                 pos = 0;
1331
1332                 /* lustre-OST0001-osc-<instance #> */
1333                 strcpy(obdname, cld->cld_logname);
1334                 cname = strrchr(obdname, '-');
1335                 if (cname == NULL) {
1336                         CERROR("mgc %s: invalid logname %s\n",
1337                                mgc->obd_name, obdname);
1338                         break;
1339                 }
1340
1341                 pos = cname - obdname;
1342                 obdname[pos] = 0;
1343                 pos += sprintf(obdname + pos, "-%s%04x",
1344                                   is_ost ? "OST" : "MDT", entry->mne_index);
1345
1346                 cname = is_ost ? "osc" : "mdc",
1347                 pos += sprintf(obdname + pos, "-%s-%s", cname, inst);
1348                 lustre_cfg_bufs_reset(&bufs, obdname);
1349
1350                 /* find the obd by obdname */
1351                 obd = class_name2obd(obdname);
1352                 if (obd == NULL) {
1353                         CDEBUG(D_INFO, "mgc %s: cannot find obdname %s\n",
1354                                mgc->obd_name, obdname);
1355
1356                         /* this is a safe race, when the ost is starting up...*/
1357                         continue;
1358                 }
1359
1360                 /* osc.import = "connection=<Conn UUID>::<target instance>" */
1361                 ++pos;
1362                 params = buf + pos;
1363                 pos += sprintf(params, "%s.import=%s", cname, "connection=");
1364                 uuid = buf + pos;
1365
1366                 /* TODO: iterate all nids to find one */
1367                 /* find uuid by nid */
1368                 rc = client_import_find_conn(obd->u.cli.cl_import,
1369                                              entry->u.nids[0],
1370                                              (struct obd_uuid *)uuid);
1371                 if (rc < 0) {
1372                         CERROR("mgc: cannot find uuid by nid %s\n",
1373                                libcfs_nid2str(entry->u.nids[0]));
1374                         break;
1375                 }
1376
1377                 CDEBUG(D_INFO, "Find uuid %s by nid %s\n",
1378                        uuid, libcfs_nid2str(entry->u.nids[0]));
1379
1380                 pos += strlen(uuid);
1381                 pos += sprintf(buf + pos, "::%u", entry->mne_instance);
1382                 LASSERT(pos < bufsz);
1383
1384                 lustre_cfg_bufs_set_string(&bufs, 1, params);
1385
1386                 rc = -ENOMEM;
1387                 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
1388                 if (lcfg == NULL) {
1389                         CERROR("mgc: cannot allocate memory\n");
1390                         break;
1391                 }
1392
1393                 CDEBUG(D_INFO, "ir apply logs "LPD64"/"LPD64" for %s -> %s\n",
1394                        prev_version, max_version, obdname, params);
1395
1396                 rc = class_process_config(lcfg);
1397                 lustre_cfg_free(lcfg);
1398                 if (rc)
1399                         CDEBUG(D_INFO, "process config for %s error %d\n",
1400                                obdname, rc);
1401
1402                 /* continue, even one with error */
1403         }
1404
1405         OBD_FREE(inst, CFS_PAGE_SIZE);
1406         RETURN(rc);
1407 }
1408
1409 /**
1410  * This function is called if this client was notified for target restarting
1411  * by the MGS. A CONFIG_READ RPC is going to send to fetch recovery logs.
1412  */
1413 static int mgc_process_recover_log(struct obd_device *obd,
1414                                    struct config_llog_data *cld)
1415 {
1416         struct ptlrpc_request *req = NULL;
1417         struct config_llog_instance *cfg = &cld->cld_cfg;
1418         struct mgs_config_body *body;
1419         struct mgs_config_res  *res;
1420         struct ptlrpc_bulk_desc *desc;
1421         cfs_page_t **pages;
1422         int nrpages;
1423         bool eof = true;
1424         int i;
1425         int ealen;
1426         int rc;
1427         ENTRY;
1428
1429         /* allocate buffer for bulk transfer.
1430          * if this is the first time for this mgs to read logs,
1431          * CONFIG_READ_NRPAGES_INIT will be used since it will read all logs
1432          * once; otherwise, it only reads increment of logs, this should be
1433          * small and CONFIG_READ_NRPAGES will be used.
1434          */
1435         nrpages = CONFIG_READ_NRPAGES;
1436         if (cfg->cfg_last_idx == 0) /* the first time */
1437                 nrpages = CONFIG_READ_NRPAGES_INIT;
1438
1439         OBD_ALLOC(pages, sizeof(*pages) * nrpages);
1440         if (pages == NULL)
1441                 GOTO(out, rc = -ENOMEM);
1442
1443         for (i = 0; i < nrpages; i++) {
1444                 pages[i] = cfs_alloc_page(CFS_ALLOC_STD);
1445                 if (pages[i] == NULL)
1446                         GOTO(out, rc = -ENOMEM);
1447         }
1448
1449 again:
1450         LASSERT(cld_is_recover(cld));
1451         LASSERT(cfs_mutex_is_locked(&cld->cld_lock));
1452         req = ptlrpc_request_alloc(class_exp2cliimp(cld->cld_mgcexp),
1453                                    &RQF_MGS_CONFIG_READ);
1454         if (req == NULL)
1455                 GOTO(out, rc = -ENOMEM);
1456
1457         rc = ptlrpc_request_pack(req, LUSTRE_MGS_VERSION, MGS_CONFIG_READ);
1458         if (rc)
1459                 GOTO(out, rc);
1460
1461         /* pack request */
1462         body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
1463         LASSERT(body != NULL);
1464         LASSERT(sizeof(body->mcb_name) > strlen(cld->cld_logname));
1465         strncpy(body->mcb_name, cld->cld_logname, sizeof(body->mcb_name));
1466         body->mcb_offset = cfg->cfg_last_idx + 1;
1467         body->mcb_type   = cld->cld_type;
1468         body->mcb_bits   = CFS_PAGE_SHIFT;
1469         body->mcb_units  = nrpages;
1470
1471         /* allocate bulk transfer descriptor */
1472         desc = ptlrpc_prep_bulk_imp(req, nrpages, BULK_PUT_SINK,
1473                                     MGS_BULK_PORTAL);
1474         if (desc == NULL)
1475                 GOTO(out, rc = -ENOMEM);
1476
1477         for (i = 0; i < nrpages; i++)
1478                 ptlrpc_prep_bulk_page(desc, pages[i], 0, CFS_PAGE_SIZE);
1479
1480         ptlrpc_request_set_replen(req);
1481         rc = ptlrpc_queue_wait(req);
1482         if (rc)
1483                 GOTO(out, rc);
1484
1485         res = req_capsule_server_get(&req->rq_pill, &RMF_MGS_CONFIG_RES);
1486         if (res->mcr_size < res->mcr_offset)
1487                 GOTO(out, rc = -EINVAL);
1488
1489         /* always update the index even though it might have errors with
1490          * handling the recover logs */
1491         cfg->cfg_last_idx = res->mcr_offset;
1492         eof = res->mcr_offset == res->mcr_size;
1493
1494         CDEBUG(D_INFO, "Latest version "LPD64", more %d.\n",
1495                res->mcr_offset, eof == false);
1496
1497         ealen = sptlrpc_cli_unwrap_bulk_read(req, req->rq_bulk, 0);
1498         if (ealen < 0)
1499                 GOTO(out, rc = ealen);
1500
1501         if (ealen > nrpages << CFS_PAGE_SHIFT)
1502                 GOTO(out, rc = -EINVAL);
1503
1504         if (ealen == 0) { /* no logs transferred */
1505                 if (!eof)
1506                         rc = -EINVAL;
1507                 GOTO(out, rc);
1508         }
1509
1510         for (i = 0; i < nrpages && ealen > 0; i++) {
1511                 int rc2;
1512                 void *ptr;
1513
1514                 ptr = cfs_kmap(pages[i]);
1515                 rc2 = mgc_apply_recover_logs(obd, cld, res->mcr_offset, ptr,
1516                                              min_t(int, ealen, CFS_PAGE_SIZE));
1517                 cfs_kunmap(pages[i]);
1518                 if (rc2 < 0) {
1519                         CWARN("Process recover log %s error %d\n",
1520                               cld->cld_logname, rc2);
1521                         break;
1522                 }
1523
1524                 ealen -= CFS_PAGE_SIZE;
1525         }
1526
1527 out:
1528         if (req)
1529                 ptlrpc_req_finished(req);
1530
1531         if (rc == 0 && !eof)
1532                 goto again;
1533
1534         if (pages) {
1535                 for (i = 0; i < nrpages; i++) {
1536                         if (pages[i] == NULL)
1537                                 break;
1538                         cfs_free_page(pages[i]);
1539                 }
1540                 OBD_FREE(pages, sizeof(*pages) * nrpages);
1541         }
1542         return rc;
1543 }
1544
1545 /* identical to mgs_log_is_empty */
1546 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
1547                             char *name)
1548 {
1549         struct lvfs_run_ctxt saved;
1550         struct llog_handle *llh;
1551         int rc = 0;
1552
1553         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1554         rc = llog_create(ctxt, &llh, NULL, name);
1555         if (rc == 0) {
1556                 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
1557                 rc = llog_get_size(llh);
1558                 llog_close(llh);
1559         }
1560         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1561         /* header is record 1 */
1562         return(rc <= 1);
1563 }
1564
1565 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
1566                             void *data)
1567 {
1568         struct llog_rec_hdr local_rec = *rec;
1569         struct llog_handle *local_llh = (struct llog_handle *)data;
1570         char *cfg_buf = (char*) (rec + 1);
1571         struct lustre_cfg *lcfg;
1572         int rc = 0;
1573         ENTRY;
1574
1575         /* Append all records */
1576         local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
1577         rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
1578                             (void *)cfg_buf, -1);
1579
1580         lcfg = (struct lustre_cfg *)cfg_buf;
1581         CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
1582                rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
1583                lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
1584
1585         RETURN(rc);
1586 }
1587
1588 /* Copy a remote log locally */
1589 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1590                          struct llog_ctxt *lctxt, char *logname)
1591 {
1592         struct llog_handle *local_llh, *remote_llh;
1593         struct obd_uuid *uuid;
1594         char *temp_log;
1595         int rc, rc2;
1596         ENTRY;
1597
1598         /* Write new log to a temp name, then vfs_rename over logname
1599            upon successful completion. */
1600
1601         OBD_ALLOC(temp_log, strlen(logname) + 1);
1602         if (!temp_log)
1603                 RETURN(-ENOMEM);
1604         sprintf(temp_log, "%sT", logname);
1605
1606         /* Make sure there's no old temp log */
1607         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1608         if (rc)
1609                 GOTO(out, rc);
1610         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1611         if (rc)
1612                 GOTO(out, rc);
1613         rc = llog_destroy(local_llh);
1614         llog_free_handle(local_llh);
1615         if (rc)
1616                 GOTO(out, rc);
1617
1618         /* open local log */
1619         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1620         if (rc)
1621                 GOTO(out, rc);
1622
1623         /* set the log header uuid for fun */
1624         OBD_ALLOC_PTR(uuid);
1625         obd_str2uuid(uuid, logname);
1626         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1627         OBD_FREE_PTR(uuid);
1628         if (rc)
1629                 GOTO(out_closel, rc);
1630
1631         /* open remote log */
1632         rc = llog_create(rctxt, &remote_llh, NULL, logname);
1633         if (rc)
1634                 GOTO(out_closel, rc);
1635         rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1636         if (rc)
1637                 GOTO(out_closer, rc);
1638
1639         /* Copy remote log */
1640         rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1641
1642 out_closer:
1643         rc2 = llog_close(remote_llh);
1644         if (!rc)
1645                 rc = rc2;
1646 out_closel:
1647         rc2 = llog_close(local_llh);
1648         if (!rc)
1649                 rc = rc2;
1650
1651         /* We've copied the remote log to the local temp log, now
1652            replace the old local log with the temp log. */
1653         if (!rc) {
1654                 struct client_obd *cli = &obd->u.cli;
1655                 LASSERT(cli);
1656                 LASSERT(cli->cl_mgc_configs_dir);
1657                 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1658                                    temp_log, logname);
1659         }
1660         CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1661 out:
1662         if (rc)
1663                 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1664         OBD_FREE(temp_log, strlen(logname) + 1);
1665         RETURN(rc);
1666 }
1667
1668 /* local_only means it cannot get remote llogs */
1669 static int mgc_process_cfg_log(struct obd_device *mgc,
1670                                struct config_llog_data *cld,
1671                                int local_only)
1672 {
1673         struct llog_ctxt *ctxt, *lctxt = NULL;
1674         struct client_obd *cli = &mgc->u.cli;
1675         struct lvfs_run_ctxt *saved_ctxt;
1676         struct lustre_sb_info *lsi = NULL;
1677         int rc = 0, must_pop = 0;
1678         bool sptlrpc_started = false;
1679
1680         ENTRY;
1681
1682         LASSERT(cld);
1683         LASSERT(cfs_mutex_is_locked(&cld->cld_lock));
1684
1685         /*
1686          * local copy of sptlrpc log is controlled elsewhere, don't try to
1687          * read it up here.
1688          */
1689         if (cld_is_sptlrpc(cld) && local_only)
1690                 RETURN(0);
1691
1692         if (cld->cld_cfg.cfg_sb)
1693                 lsi = s2lsi(cld->cld_cfg.cfg_sb);
1694
1695         ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1696         if (!ctxt) {
1697                 CERROR("missing llog context\n");
1698                 RETURN(-EINVAL);
1699         }
1700
1701         OBD_ALLOC_PTR(saved_ctxt);
1702         if (saved_ctxt == NULL)
1703                 RETURN(-ENOMEM);
1704
1705         lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1706
1707         /* Copy the setup log locally if we can. Don't mess around if we're
1708            running an MGS though (logs are already local). */
1709         if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1710             (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1711             !IS_MGS(lsi->lsi_ldd)) {
1712                 push_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1713                 must_pop++;
1714                 if (!local_only)
1715                         /* Only try to copy log if we have the lock. */
1716                         rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1717                 if (local_only || rc) {
1718                         if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1719                                 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1720                                                    "log %s and no local copy."
1721                                                    "\n", cld->cld_logname);
1722                                 GOTO(out_pop, rc = -ENOTCONN);
1723                         }
1724                         CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1725                                "copy for now, will try to update later.\n",
1726                                cld->cld_logname);
1727                 }
1728                 /* Now, whether we copied or not, start using the local llog.
1729                    If we failed to copy, we'll start using whatever the old
1730                    log has. */
1731                 llog_ctxt_put(ctxt);
1732                 ctxt = lctxt;
1733                 lctxt = NULL;
1734         } else if (local_only) { /* no local log at client side */
1735                 GOTO(out_pop, rc = -EIO);
1736         }
1737
1738         if (cld_is_sptlrpc(cld)) {
1739                 sptlrpc_conf_log_update_begin(cld->cld_logname);
1740                 sptlrpc_started = true;
1741         }
1742
1743         /* logname and instance info should be the same, so use our
1744            copy of the instance for the update.  The cfg_last_idx will
1745            be updated here. */
1746         rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1747         EXIT;
1748
1749 out_pop:
1750         llog_ctxt_put(ctxt);
1751         if (lctxt)
1752                 llog_ctxt_put(lctxt);
1753         if (must_pop)
1754                 pop_ctxt(saved_ctxt, &mgc->obd_lvfs_ctxt, NULL);
1755
1756         OBD_FREE_PTR(saved_ctxt);
1757         /*
1758          * update settings on existing OBDs. doing it inside
1759          * of llog_process_lock so no device is attaching/detaching
1760          * in parallel.
1761          * the logname must be <fsname>-sptlrpc
1762          */
1763         if (sptlrpc_started) {
1764                 LASSERT(cld_is_sptlrpc(cld));
1765                 sptlrpc_conf_log_update_end(cld->cld_logname);
1766                 class_notify_sptlrpc_conf(cld->cld_logname,
1767                                           strlen(cld->cld_logname) -
1768                                           strlen("-sptlrpc"));
1769         }
1770
1771         RETURN(rc);
1772 }
1773
1774 /** Get a config log from the MGS and process it.
1775  * This func is called for both clients and servers.
1776  * Copy the log locally before parsing it if appropriate (non-MGS server)
1777  */
1778 int mgc_process_log(struct obd_device *mgc, struct config_llog_data *cld)
1779 {
1780         struct lustre_handle lockh = { 0 };
1781         int rc = 0, rcl, flags = LDLM_FL_NO_LRU;
1782         ENTRY;
1783
1784         LASSERT(cld);
1785
1786         /* I don't want multiple processes running process_log at once --
1787            sounds like badness.  It actually might be fine, as long as
1788            we're not trying to update from the same log
1789            simultaneously (in which case we should use a per-log sem.) */
1790         cfs_mutex_lock(&cld->cld_lock);
1791         if (cld->cld_stopping) {
1792                 cfs_mutex_unlock(&cld->cld_lock);
1793                 RETURN(0);
1794         }
1795
1796         OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1797
1798         CDEBUG(D_MGC, "Process log %s:%p from %d\n", cld->cld_logname,
1799                cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1800
1801         /* Get the cfg lock on the llog */
1802         rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1803                           LCK_CR, &flags, NULL, NULL, NULL,
1804                           cld, 0, NULL, &lockh);
1805         if (rcl == 0) {
1806                 /* Get the cld, it will be released in mgc_blocking_ast. */
1807                 config_log_get(cld);
1808                 rc = ldlm_lock_set_data(&lockh, (void *)cld);
1809                 LASSERT(rc == 0);
1810         } else {
1811                 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1812
1813                 /* mark cld_lostlock so that it will requeue
1814                  * after MGC becomes available. */
1815                 cld->cld_lostlock = 1;
1816                 /* Get extra reference, it will be put in requeue thread */
1817                 config_log_get(cld);
1818         }
1819
1820
1821         if (cld_is_recover(cld)) {
1822                 rc = 0; /* this is not a fatal error for recover log */
1823                 if (rcl == 0)
1824                         rc = mgc_process_recover_log(mgc, cld);
1825         } else {
1826                 rc = mgc_process_cfg_log(mgc, cld, rcl != 0);
1827         }
1828
1829         CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1830                mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1831
1832         cfs_mutex_unlock(&cld->cld_lock);
1833
1834         /* Now drop the lock so MGS can revoke it */
1835         if (!rcl) {
1836                 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1837                                  LCK_CR, &lockh);
1838                 if (rcl)
1839                         CERROR("Can't drop cfg lock: %d\n", rcl);
1840         }
1841
1842         RETURN(rc);
1843 }
1844
1845
1846 /** Called from lustre_process_log.
1847  * LCFG_LOG_START gets the config log from the MGS, processes it to start
1848  * any services, and adds it to the list logs to watch (follow).
1849  */
1850 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1851 {
1852         struct lustre_cfg *lcfg = buf;
1853         struct config_llog_instance *cfg = NULL;
1854         char *logname;
1855         int rc = 0;
1856         ENTRY;
1857
1858         switch(lcfg->lcfg_command) {
1859         case LCFG_LOV_ADD_OBD: {
1860                 /* Overloading this cfg command: register a new target */
1861                 struct mgs_target_info *mti;
1862
1863                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1864                     sizeof(struct mgs_target_info))
1865                         GOTO(out, rc = -EINVAL);
1866
1867                 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1868                 CDEBUG(D_MGC, "add_target %s %#x\n",
1869                        mti->mti_svname, mti->mti_flags);
1870                 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1871                 break;
1872         }
1873         case LCFG_LOV_DEL_OBD:
1874                 /* Unregister has no meaning at the moment. */
1875                 CERROR("lov_del_obd unimplemented\n");
1876                 rc = -ENOSYS;
1877                 break;
1878         case LCFG_SPTLRPC_CONF: {
1879                 rc = sptlrpc_process_config(lcfg);
1880                 break;
1881         }
1882         case LCFG_LOG_START: {
1883                 struct config_llog_data *cld;
1884                 struct super_block *sb;
1885
1886                 logname = lustre_cfg_string(lcfg, 1);
1887                 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1888                 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1889
1890                 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1891                        cfg->cfg_last_idx);
1892
1893                 /* We're only called through here on the initial mount */
1894                 rc = config_log_add(obd, logname, cfg, sb);
1895                 if (rc)
1896                         break;
1897                 cld = config_log_find(logname, cfg);
1898                 if (cld == NULL) {
1899                         rc = -ENOENT;
1900                         break;
1901                 }
1902
1903                 /* COMPAT_146 */
1904                 /* FIXME only set this for old logs!  Right now this forces
1905                    us to always skip the "inside markers" check */
1906                 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1907
1908                 rc = mgc_process_log(obd, cld);
1909                 if (rc == 0 && cld->cld_recover != NULL) {
1910                         if (OCD_HAS_FLAG(&obd->u.cli.cl_import->
1911                                          imp_connect_data, IMP_RECOV)) {
1912                                 rc = mgc_process_log(obd, cld->cld_recover);
1913                         } else {
1914                                 struct config_llog_data *cir = cld->cld_recover;
1915                                 cld->cld_recover = NULL;
1916                                 config_log_put(cir);
1917                         }
1918                         if (rc)
1919                                 CERROR("Cannot process recover llog %d\n", rc);
1920                 }
1921                 config_log_put(cld);
1922
1923                 break;
1924         }
1925         case LCFG_LOG_END: {
1926                 logname = lustre_cfg_string(lcfg, 1);
1927
1928                 if (lcfg->lcfg_bufcount >= 2)
1929                         cfg = (struct config_llog_instance *)lustre_cfg_buf(
1930                                 lcfg, 2);
1931                 rc = config_log_end(logname, cfg);
1932                 break;
1933         }
1934         default: {
1935                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1936                 GOTO(out, rc = -EINVAL);
1937
1938         }
1939         }
1940 out:
1941         RETURN(rc);
1942 }
1943
1944 struct obd_ops mgc_obd_ops = {
1945         .o_owner        = THIS_MODULE,
1946         .o_setup        = mgc_setup,
1947         .o_precleanup   = mgc_precleanup,
1948         .o_cleanup      = mgc_cleanup,
1949         .o_add_conn     = client_import_add_conn,
1950         .o_del_conn     = client_import_del_conn,
1951         .o_connect      = client_connect_import,
1952         .o_disconnect   = client_disconnect_export,
1953         //.o_enqueue      = mgc_enqueue,
1954         .o_cancel       = mgc_cancel,
1955         //.o_iocontrol    = mgc_iocontrol,
1956         .o_set_info_async = mgc_set_info_async,
1957         .o_get_info       = mgc_get_info,
1958         .o_import_event = mgc_import_event,
1959         .o_llog_init    = mgc_llog_init,
1960         .o_llog_finish  = mgc_llog_finish,
1961         .o_process_config = mgc_process_config,
1962 };
1963
1964 int __init mgc_init(void)
1965 {
1966         return class_register_type(&mgc_obd_ops, NULL, NULL,
1967                                    LUSTRE_MGC_NAME, NULL);
1968 }
1969
1970 #ifdef __KERNEL__
1971 static void /*__exit*/ mgc_exit(void)
1972 {
1973         class_unregister_type(LUSTRE_MGC_NAME);
1974 }
1975
1976 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1977 MODULE_DESCRIPTION("Lustre Management Client");
1978 MODULE_LICENSE("GPL");
1979
1980 module_init(mgc_init);
1981 module_exit(mgc_exit);
1982 #endif