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