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