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