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