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