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