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