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