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