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