Whamcloud - gitweb
37cfda51e2a7c8cdfb7468c38480869065298f5c
[fs/lustre-release.git] / lustre / mgc / mgc_request.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_MGC
45 #define D_MGC D_CONFIG /*|D_WARNING*/
46
47 #ifdef __KERNEL__
48 # include <linux/module.h>
49 # include <linux/pagemap.h>
50 # include <linux/miscdevice.h>
51 # include <linux/init.h>
52 #else
53 # include <liblustre.h>
54 #endif
55
56 #include <obd_class.h>
57 #include <lustre_dlm.h>
58 #include <lprocfs_status.h>
59 #include <lustre_log.h>
60 #include <lustre_fsfilt.h>
61 #include <lustre_disk.h>
62 #include "mgc_internal.h"
63
64 static int mgc_name2resid(char *name, int len, struct ldlm_res_id *res_id)
65 {
66         __u64 resname = 0;
67
68         if (len > 8) {
69                 CERROR("name too long: %s\n", name);
70                 return -EINVAL;
71         }
72         if (len <= 0) {
73                 CERROR("missing name: %s\n", name);
74                 return -EINVAL;
75         }
76         memcpy(&resname, name, len);
77
78         memset(res_id, 0, sizeof(*res_id));
79
80         /* Always use the same endianness for the resid */
81         res_id->name[0] = cpu_to_le64(resname);
82         CDEBUG(D_MGC, "log %s to resid "LPX64"/"LPX64" (%.8s)\n", name,
83                res_id->name[0], res_id->name[1], (char *)&res_id->name[0]);
84         return 0;
85 }
86
87 int mgc_fsname2resid(char *fsname, struct ldlm_res_id *res_id)
88 {
89         /* fsname is at most 8 chars long, maybe contain "-".
90          * e.g. "lustre", "SUN-000" */
91         return mgc_name2resid(fsname, strlen(fsname), res_id);
92 }
93 EXPORT_SYMBOL(mgc_fsname2resid);
94
95 int mgc_logname2resid(char *logname, struct ldlm_res_id *res_id)
96 {
97         char *name_end;
98         int len;
99
100         /* logname consists of "fsname-nodetype".
101          * e.g. "lustre-MDT0001", "SUN-000-client" */
102         name_end = strrchr(logname, '-');
103         LASSERT(name_end);
104         len = name_end - logname;
105         return mgc_name2resid(logname, len, res_id);
106 }
107
108 /********************** config llog list **********************/
109 static CFS_LIST_HEAD(config_llog_list);
110 static spinlock_t       config_list_lock = SPIN_LOCK_UNLOCKED;
111
112 /* Take a reference to a config log */
113 static int config_log_get(struct config_llog_data *cld)
114 {
115         ENTRY;
116         if (cld->cld_stopping)
117                 RETURN(1);
118         atomic_inc(&cld->cld_refcount);
119         CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
120                atomic_read(&cld->cld_refcount));
121         RETURN(0);
122 }
123
124 /* Drop a reference to a config log.  When no longer referenced,
125    we can free the config log data */
126 static void config_log_put(struct config_llog_data *cld)
127 {
128         ENTRY;
129         CDEBUG(D_INFO, "log %s refs %d\n", cld->cld_logname,
130                atomic_read(&cld->cld_refcount));
131         if (atomic_dec_and_test(&cld->cld_refcount)) {
132                 CDEBUG(D_MGC, "dropping config log %s\n", cld->cld_logname);
133                 class_export_put(cld->cld_mgcexp);
134                 spin_lock(&config_list_lock);
135                 list_del(&cld->cld_list_chain);
136                 spin_unlock(&config_list_lock);
137                 OBD_FREE(cld->cld_logname, strlen(cld->cld_logname) + 1);
138                 if (cld->cld_cfg.cfg_instance != NULL)
139                         OBD_FREE(cld->cld_cfg.cfg_instance,
140                                  strlen(cld->cld_cfg.cfg_instance) + 1);
141                 OBD_FREE(cld, sizeof(*cld));
142         }
143         EXIT;
144 }
145
146 /* Find a config log by name */
147 static struct config_llog_data *config_log_find(char *logname,
148                                                struct config_llog_instance *cfg)
149 {
150         struct list_head *tmp;
151         struct config_llog_data *cld;
152         char *logid = logname;
153         int match_instance = 0;
154         ENTRY;
155
156         if (cfg && cfg->cfg_instance) {
157                 match_instance++;
158                 logid = cfg->cfg_instance;
159         }
160         if (!logid) {
161                 CERROR("No log specified\n");
162                 RETURN(ERR_PTR(-EINVAL));
163         }
164
165         spin_lock(&config_list_lock);
166         list_for_each(tmp, &config_llog_list) {
167                 cld = list_entry(tmp, struct config_llog_data, cld_list_chain);
168                 if (match_instance && cld->cld_cfg.cfg_instance &&
169                     strcmp(logid, cld->cld_cfg.cfg_instance) == 0)
170                         goto out_found;
171                 if (!match_instance &&
172                     strcmp(logid, cld->cld_logname) == 0)
173                         goto out_found;
174         }
175         spin_unlock(&config_list_lock);
176
177         CDEBUG(D_CONFIG, "can't get log %s\n", logid);
178         RETURN(ERR_PTR(-ENOENT));
179 out_found:
180         atomic_inc(&cld->cld_refcount);
181         spin_unlock(&config_list_lock);
182         RETURN(cld);
183 }
184
185 /* Add this log to our list of active logs.
186    We have one active log per "mount" - client instance or servername.
187    Each instance may be at a different point in the log. */
188 static int config_log_add(char *logname, struct config_llog_instance *cfg,
189                           struct super_block *sb)
190 {
191         struct config_llog_data *cld;
192         struct lustre_sb_info *lsi = s2lsi(sb);
193         int rc;
194         ENTRY;
195
196         CDEBUG(D_MGC, "adding config log %s:%s\n", logname, cfg->cfg_instance);
197
198         OBD_ALLOC(cld, sizeof(*cld));
199         if (!cld)
200                 RETURN(-ENOMEM);
201         OBD_ALLOC(cld->cld_logname, strlen(logname) + 1);
202         if (!cld->cld_logname) {
203                 OBD_FREE(cld, sizeof(*cld));
204                 RETURN(-ENOMEM);
205         }
206         strcpy(cld->cld_logname, logname);
207         cld->cld_cfg = *cfg;
208         cld->cld_cfg.cfg_last_idx = 0;
209         cld->cld_cfg.cfg_flags = 0;
210         cld->cld_cfg.cfg_sb = sb;
211         atomic_set(&cld->cld_refcount, 1);
212
213         /* Keep the mgc around until we are done */
214         cld->cld_mgcexp = class_export_get(lsi->lsi_mgc->obd_self_export);
215
216         if (cfg->cfg_instance != NULL) {
217                 OBD_ALLOC(cld->cld_cfg.cfg_instance,
218                           strlen(cfg->cfg_instance) + 1);
219                 strcpy(cld->cld_cfg.cfg_instance, cfg->cfg_instance);
220         }
221         rc = mgc_logname2resid(logname, &cld->cld_resid);
222         spin_lock(&config_list_lock);
223         list_add(&cld->cld_list_chain, &config_llog_list);
224         spin_unlock(&config_list_lock);
225
226         if (rc) {
227                 config_log_put(cld);
228                 RETURN(rc);
229         }
230
231         RETURN(rc);
232 }
233
234 DECLARE_MUTEX(llog_process_lock);
235
236 /* Stop watching for updates on this log. */
237 static int config_log_end(char *logname, struct config_llog_instance *cfg)
238 {
239         struct config_llog_data *cld;
240         int rc = 0;
241         ENTRY;
242
243         cld = config_log_find(logname, cfg);
244         if (IS_ERR(cld))
245                 RETURN(PTR_ERR(cld));
246         /* drop the ref from the find */
247         config_log_put(cld);
248
249         down(&llog_process_lock);
250         cld->cld_stopping = 1;
251         up(&llog_process_lock);
252
253         /* drop the start ref */
254         config_log_put(cld);
255         CDEBUG(D_MGC, "end config log %s (%d)\n", logname ? logname : "client",
256                rc);
257         RETURN(rc);
258 }
259
260 /* reenqueue any lost locks */
261 #define RQ_RUNNING 0x1
262 #define RQ_NOW     0x2
263 #define RQ_LATER   0x4
264 #define RQ_STOP    0x8
265 static int rq_state = 0;
266 static cfs_waitq_t rq_waitq;
267
268 static int mgc_process_log(struct obd_device *mgc,
269                            struct config_llog_data *cld);
270 static int mgc_requeue_add(struct config_llog_data *cld, int later);
271
272 static int mgc_requeue_thread(void *data)
273 {
274         struct l_wait_info lwi_now, lwi_later;
275         struct config_llog_data *cld, *n;
276         char name[] = "ll_cfg_requeue";
277         int rc = 0;
278         ENTRY;
279
280         ptlrpc_daemonize(name);
281
282         CDEBUG(D_MGC, "Starting requeue thread\n");
283
284         lwi_later = LWI_TIMEOUT(60 * HZ, NULL, NULL);
285         l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP), &lwi_later);
286
287         /* Keep trying failed locks periodically */
288         spin_lock(&config_list_lock);
289         while (rq_state & (RQ_NOW | RQ_LATER)) {
290                 /* Any new or requeued lostlocks will change the state */
291                 rq_state &= ~(RQ_NOW | RQ_LATER);
292                 spin_unlock(&config_list_lock);
293
294                 /* Always wait a few seconds to allow the server who
295                    caused the lock revocation to finish its setup, plus some
296                    random so everyone doesn't try to reconnect at once. */
297                 lwi_now = LWI_TIMEOUT(3 * HZ + (ll_rand() & 0xff) * (HZ / 100),
298                                       NULL, NULL);
299                 l_wait_event(rq_waitq, rq_state & RQ_STOP, &lwi_now);
300
301                 spin_lock(&config_list_lock);
302                 list_for_each_entry_safe(cld, n, &config_llog_list,
303                                          cld_list_chain) {
304                         spin_unlock(&config_list_lock);
305                         if (cld->cld_lostlock) {
306                                 CDEBUG(D_MGC, "updating log %s\n",
307                                        cld->cld_logname);
308                                 cld->cld_lostlock = 0;
309                                 rc = mgc_process_log(cld->cld_mgcexp->exp_obd,
310                                                      cld);
311                                 /* Whether we enqueued again or not in
312                                    mgc_process_log, we're done with the ref
313                                    from the old enqueue */
314                                 config_log_put(cld);
315                         }
316                         spin_lock(&config_list_lock);
317                 }
318                 spin_unlock(&config_list_lock);
319
320                 /* Wait a bit to see if anyone else needs a requeue */
321                 l_wait_event(rq_waitq, rq_state & (RQ_NOW | RQ_STOP),
322                              &lwi_later);
323                 spin_lock(&config_list_lock);
324         }
325         /* spinlock and while guarantee RQ_NOW and RQ_LATER are not set */
326         rq_state &= ~RQ_RUNNING;
327         spin_unlock(&config_list_lock);
328
329         CDEBUG(D_MGC, "Ending requeue thread\n");
330         RETURN(rc);
331 }
332
333 /* Add a cld to the list to requeue.  Start the requeue thread if needed.
334    We are responsible for dropping the config log reference from here on out. */
335 static int mgc_requeue_add(struct config_llog_data *cld, int later)
336 {
337         int rc = 0;
338
339         CDEBUG(D_INFO, "log %s: requeue (l=%d r=%d sp=%d st=%x)\n",
340                cld->cld_logname, later, atomic_read(&cld->cld_refcount),
341                cld->cld_stopping, rq_state);
342
343         /* Hold lock for rq_state */
344         spin_lock(&config_list_lock);
345
346         if (cld->cld_stopping || (rq_state & RQ_STOP)) {
347                 spin_unlock(&config_list_lock);
348                 config_log_put(cld);
349                 RETURN(0);
350         }
351
352         cld->cld_lostlock = 1;
353
354         if (!(rq_state & RQ_RUNNING)) {
355                 LASSERT(rq_state == 0);
356                 rq_state = RQ_RUNNING | (later ? RQ_LATER : RQ_NOW);
357                 spin_unlock(&config_list_lock);
358                 rc = cfs_kernel_thread(mgc_requeue_thread, 0,
359                                        CLONE_VM | CLONE_FILES);
360                 if (rc < 0) {
361                         CERROR("log %s: cannot start requeue thread (%d),"
362                                "no more log updates!\n", cld->cld_logname, rc);
363                         /* Drop the ref, since the rq thread won't */
364                         cld->cld_lostlock = 0;
365                         config_log_put(cld);
366                         rq_state = 0;
367                         RETURN(rc);
368                 }
369         } else {
370                 rq_state |= later ? RQ_LATER : RQ_NOW;
371                 spin_unlock(&config_list_lock);
372                 cfs_waitq_signal(&rq_waitq);
373         }
374
375         RETURN(0);
376 }
377
378 /********************** class fns **********************/
379
380 static int mgc_fs_setup(struct obd_device *obd, struct super_block *sb,
381                         struct vfsmount *mnt)
382 {
383         struct lvfs_run_ctxt saved;
384         struct lustre_sb_info *lsi = s2lsi(sb);
385         struct client_obd *cli = &obd->u.cli;
386         struct dentry *dentry;
387         char *label;
388         int err = 0;
389         ENTRY;
390
391         LASSERT(lsi);
392         LASSERT(lsi->lsi_srv_mnt == mnt);
393
394         /* The mgc fs exclusion sem. Only one fs can be setup at a time. */
395         down(&cli->cl_mgc_sem);
396
397         cleanup_group_info();
398
399         obd->obd_fsops = fsfilt_get_ops(MT_STR(lsi->lsi_ldd));
400         if (IS_ERR(obd->obd_fsops)) {
401                 up(&cli->cl_mgc_sem);
402                 CERROR("No fstype %s rc=%ld\n", MT_STR(lsi->lsi_ldd),
403                        PTR_ERR(obd->obd_fsops));
404                 RETURN(PTR_ERR(obd->obd_fsops));
405         }
406
407         cli->cl_mgc_vfsmnt = mnt;
408         fsfilt_setup(obd, mnt->mnt_sb);
409
410         OBD_SET_CTXT_MAGIC(&obd->obd_lvfs_ctxt);
411         obd->obd_lvfs_ctxt.pwdmnt = mnt;
412         obd->obd_lvfs_ctxt.pwd = mnt->mnt_root;
413         obd->obd_lvfs_ctxt.fs = get_ds();
414
415         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
416         dentry = lookup_one_len(MOUNT_CONFIGS_DIR, current->fs->pwd,
417                                 strlen(MOUNT_CONFIGS_DIR));
418         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
419         if (IS_ERR(dentry)) {
420                 err = PTR_ERR(dentry);
421                 CERROR("cannot lookup %s directory: rc = %d\n",
422                        MOUNT_CONFIGS_DIR, err);
423                 GOTO(err_ops, err);
424         }
425         cli->cl_mgc_configs_dir = dentry;
426
427         /* We take an obd ref to insure that we can't get to mgc_cleanup
428            without calling mgc_fs_cleanup first. */
429         class_incref(obd, "mgc_fs", obd);
430
431         label = fsfilt_get_label(obd, mnt->mnt_sb);
432         if (label)
433                 CDEBUG(D_MGC, "MGC using disk labelled=%s\n", label);
434
435         /* We keep the cl_mgc_sem until mgc_fs_cleanup */
436         RETURN(0);
437
438 err_ops:
439         fsfilt_put_ops(obd->obd_fsops);
440         obd->obd_fsops = NULL;
441         cli->cl_mgc_vfsmnt = NULL;
442         up(&cli->cl_mgc_sem);
443         RETURN(err);
444 }
445
446 static int mgc_fs_cleanup(struct obd_device *obd)
447 {
448         struct client_obd *cli = &obd->u.cli;
449         int rc = 0;
450         ENTRY;
451
452         LASSERT(cli->cl_mgc_vfsmnt != NULL);
453
454         if (cli->cl_mgc_configs_dir != NULL) {
455                 struct lvfs_run_ctxt saved;
456                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
457                 l_dput(cli->cl_mgc_configs_dir);
458                 cli->cl_mgc_configs_dir = NULL;
459                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
460                 class_decref(obd, "mgc_fs", obd);
461         }
462
463         cli->cl_mgc_vfsmnt = NULL;
464         if (obd->obd_fsops)
465                 fsfilt_put_ops(obd->obd_fsops);
466
467         up(&cli->cl_mgc_sem);
468
469         RETURN(rc);
470 }
471
472 static atomic_t mgc_count = ATOMIC_INIT(0);
473 static int mgc_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
474 {
475         int rc = 0;
476         ENTRY;
477
478         switch (stage) {
479         case OBD_CLEANUP_EARLY:
480                 break;
481         case OBD_CLEANUP_EXPORTS:
482                 if (atomic_dec_and_test(&mgc_count)) {
483                         /* Kick the requeue waitq - cld's should all be
484                            stopping */
485                         spin_lock(&config_list_lock);
486                         rq_state |= RQ_STOP;
487                         spin_unlock(&config_list_lock);
488                         cfs_waitq_signal(&rq_waitq);
489                 }
490                 rc = obd_llog_finish(obd, 0);
491                 if (rc != 0)
492                         CERROR("failed to cleanup llogging subsystems\n");
493                 break;
494         }
495         RETURN(rc);
496 }
497
498 static int mgc_cleanup(struct obd_device *obd)
499 {
500         struct client_obd *cli = &obd->u.cli;
501         int rc;
502         ENTRY;
503
504         LASSERT(cli->cl_mgc_vfsmnt == NULL);
505
506         /* COMPAT_146 - old config logs may have added profiles we don't
507            know about */
508         if (obd->obd_type->typ_refcnt <= 1)
509                 /* Only for the last mgc */
510                 class_del_profiles();
511
512         lprocfs_obd_cleanup(obd);
513         ptlrpcd_decref();
514
515         rc = client_obd_cleanup(obd);
516         RETURN(rc);
517 }
518
519 static int mgc_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
520 {
521         struct lprocfs_static_vars lvars;
522         int rc;
523         ENTRY;
524
525         ptlrpcd_addref();
526
527         rc = client_obd_setup(obd, lcfg);
528         if (rc)
529                 GOTO(err_decref, rc);
530
531         rc = obd_llog_init(obd, &obd->obd_olg, obd, 0, NULL, NULL);
532         if (rc) {
533                 CERROR("failed to setup llogging subsystems\n");
534                 GOTO(err_cleanup, rc);
535         }
536
537         lprocfs_mgc_init_vars(&lvars);
538         lprocfs_obd_setup(obd, lvars.obd_vars);
539
540         spin_lock(&config_list_lock);
541         atomic_inc(&mgc_count);
542         if (atomic_read(&mgc_count) == 1) {
543                 rq_state &= ~RQ_STOP;
544                 cfs_waitq_init(&rq_waitq);
545         }
546         spin_unlock(&config_list_lock);
547
548         RETURN(rc);
549
550 err_cleanup:
551         client_obd_cleanup(obd);
552 err_decref:
553         ptlrpcd_decref();
554         RETURN(rc);
555 }
556
557 /* based on ll_mdc_blocking_ast */
558 static int mgc_blocking_ast(struct ldlm_lock *lock, struct ldlm_lock_desc *desc,
559                             void *data, int flag)
560 {
561         struct lustre_handle lockh;
562         struct config_llog_data *cld = (struct config_llog_data *)data;
563         int rc = 0;
564         ENTRY;
565
566         switch (flag) {
567         case LDLM_CB_BLOCKING:
568                 /* mgs wants the lock, give it up... */
569                 LDLM_DEBUG(lock, "MGC blocking CB");
570                 ldlm_lock2handle(lock, &lockh);
571                 rc = ldlm_cli_cancel(&lockh);
572                 break;
573         case LDLM_CB_CANCELING: {
574                 /* We've given up the lock, prepare ourselves to update. */
575                 LDLM_DEBUG(lock, "MGC cancel CB");
576
577                 CDEBUG(D_MGC, "Lock res "LPX64" (%.8s)\n",
578                        lock->l_resource->lr_name.name[0],
579                        (char *)&lock->l_resource->lr_name.name[0]);
580
581                 if (!cld) {
582                         CERROR("missing data, won't requeue\n");
583                         break;
584                 }
585                 /* Are we done with this log? */
586                 if (cld->cld_stopping) {
587                         CDEBUG(D_MGC, "log %s: stopping, won't requeue\n",
588                                cld->cld_logname);
589                         config_log_put(cld);
590                         break;
591                 }
592                 /* Make sure not to re-enqueue when the mgc is stopping
593                    (we get called from client_disconnect_export) */
594                 if (!lock->l_conn_export ||
595                     !lock->l_conn_export->exp_obd->u.cli.cl_conn_count) {
596                         CDEBUG(D_MGC, "log %s: disconnecting, won't requeue\n",
597                                cld->cld_logname);
598                         config_log_put(cld);
599                         break;
600                 }
601                 /* Did we fail to get the lock? */
602                 if (lock->l_req_mode != lock->l_granted_mode) {
603                         CDEBUG(D_MGC, "log %s: original grant failed, will "
604                                "requeue later\n", cld->cld_logname);
605                         /* Try to re-enqueue later */
606                         rc = mgc_requeue_add(cld, 1);
607                         break;
608                 }
609                 /* Re-enqueue now */
610                 rc = mgc_requeue_add(cld, 0);
611                 break;
612         }
613         default:
614                 LBUG();
615         }
616
617
618         if (rc) {
619                 CERROR("%s CB failed %d:\n", flag == LDLM_CB_BLOCKING ?
620                        "blocking" : "cancel", rc);
621                 LDLM_ERROR(lock, "MGC ast");
622         }
623         RETURN(rc);
624 }
625
626 /* Send parameter to MGS*/
627 static int mgc_set_mgs_param(struct obd_export *exp,
628                              struct mgs_send_param *msp)
629 {
630         struct ptlrpc_request *req;
631         struct mgs_send_param *req_msp, *rep_msp;
632         int size[] = { sizeof(struct ptlrpc_body), sizeof(*req_msp) };
633         __u32 rep_size[] = { sizeof(struct ptlrpc_body), sizeof(*msp) };
634         int rc;
635         ENTRY;
636
637         req = ptlrpc_prep_req(class_exp2cliimp(exp), LUSTRE_MGS_VERSION,
638                               MGS_SET_INFO, 2, size, NULL);
639         if (!req)
640                 RETURN(-ENOMEM);
641
642         req_msp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*req_msp));
643         if (!req_msp)
644                 RETURN(-ENOMEM);
645
646         memcpy(req_msp, msp, sizeof(*req_msp));
647         ptlrpc_req_set_repsize(req, 2, rep_size);
648         rc = ptlrpc_queue_wait(req);
649         if (!rc) {
650                 rep_msp = lustre_swab_repbuf(req, REPLY_REC_OFF,
651                                              sizeof(*rep_msp), NULL);
652                 memcpy(msp, rep_msp, sizeof(*rep_msp));
653         }
654
655         ptlrpc_req_finished(req);
656
657         RETURN(rc);
658 }
659
660 /* Take a config lock so we can get cancel notifications */
661 static int mgc_enqueue(struct obd_export *exp, struct lov_stripe_md *lsm,
662                        __u32 type, ldlm_policy_data_t *policy, __u32 mode,
663                        int *flags, void *bl_cb, void *cp_cb, void *gl_cb,
664                        void *data, __u32 lvb_len, void *lvb_swabber,
665                        struct lustre_handle *lockh)
666 {
667         struct config_llog_data *cld = (struct config_llog_data *)data;
668         struct ldlm_enqueue_info einfo = { type, mode, mgc_blocking_ast,
669                          ldlm_completion_ast, NULL, NULL, data};
670
671         int rc;
672         ENTRY;
673
674         CDEBUG(D_MGC, "Enqueue for %s (res "LPX64")\n", cld->cld_logname,
675                cld->cld_resid.name[0]);
676
677         /* We can only drop this config log ref when we drop the lock */
678         if (config_log_get(cld))
679                 RETURN(ELDLM_LOCK_ABORTED);
680
681         /* We need a callback for every lockholder, so don't try to
682            ldlm_lock_match (see rev 1.1.2.11.2.47) */
683
684         rc = ldlm_cli_enqueue(exp, NULL, &einfo, &cld->cld_resid,
685                               NULL, flags, NULL, 0, NULL, lockh, 0);
686         /* A failed enqueue should still call the mgc_blocking_ast,
687            where it will be requeued if needed ("grant failed"). */
688
689         RETURN(rc);
690 }
691
692 static int mgc_cancel(struct obd_export *exp, struct lov_stripe_md *md,
693                       __u32 mode, struct lustre_handle *lockh)
694 {
695         ENTRY;
696
697         ldlm_lock_decref(lockh, mode);
698
699         RETURN(0);
700 }
701
702 #if 0
703 static int mgc_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
704                          void *karg, void *uarg)
705 {
706         struct obd_device *obd = exp->exp_obd;
707         struct obd_ioctl_data *data = karg;
708         struct llog_ctxt *ctxt;
709         struct lvfs_run_ctxt saved;
710         int rc;
711         ENTRY;
712
713         if (!try_module_get(THIS_MODULE)) {
714                 CERROR("Can't get module. Is it alive?");
715                 return -EINVAL;
716         }
717         switch (cmd) {
718         /* REPLicator context */
719         case OBD_IOC_PARSE: {
720                 CERROR("MGC parsing llog %s\n", data->ioc_inlbuf1);
721                 ctxt = llog_get_context(exp->exp_obd, LLOG_CONFIG_REPL_CTXT);
722                 rc = class_config_parse_llog(ctxt, data->ioc_inlbuf1, NULL);
723                 GOTO(out, rc);
724         }
725 #ifdef __KERNEL__
726         case OBD_IOC_LLOG_INFO:
727         case OBD_IOC_LLOG_PRINT: {
728                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
729                 rc = llog_ioctl(ctxt, cmd, data);
730
731                 GOTO(out, rc);
732         }
733 #endif
734         /* ORIGinator context */
735         case OBD_IOC_DUMP_LOG: {
736                 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
737                 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
738                 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
739                 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
740                 if (rc)
741                         RETURN(rc);
742
743                 GOTO(out, rc);
744         }
745         default:
746                 CERROR("mgc_ioctl(): unrecognised ioctl %#x\n", cmd);
747                 GOTO(out, rc = -ENOTTY);
748         }
749 out:
750         module_put(THIS_MODULE);
751
752         return rc;
753 }
754 #endif
755
756 /* Send target_reg message to MGS */
757 static int mgc_target_register(struct obd_export *exp,
758                                struct mgs_target_info *mti)
759 {
760         struct ptlrpc_request  *req;
761         struct mgs_target_info *req_mti, *rep_mti;
762         int                     rc;
763         ENTRY;
764
765         req = ptlrpc_request_alloc_pack(class_exp2cliimp(exp),
766                                         &RQF_MGS_TARGET_REG, LUSTRE_MGS_VERSION,
767                                         MGS_TARGET_REG);
768         if (req == NULL)
769                 RETURN(-ENOMEM);
770
771         req_mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
772         memcpy(req_mti, mti, sizeof(*req_mti));
773
774         ptlrpc_request_set_replen(req);
775
776         CDEBUG(D_MGC, "register %s\n", mti->mti_svname);
777
778         rc = ptlrpc_queue_wait(req);
779         if (!rc) {
780                 rep_mti = req_capsule_server_get(&req->rq_pill,
781                                                  &RMF_MGS_TARGET_INFO);
782                 memcpy(mti, rep_mti, sizeof(*rep_mti));
783                 CDEBUG(D_MGC, "register %s got index = %d\n",
784                        mti->mti_svname, mti->mti_stripe_index);
785         }
786         ptlrpc_req_finished(req);
787
788         RETURN(rc);
789 }
790
791 int mgc_set_info_async(struct obd_export *exp, obd_count keylen,
792                        void *key, obd_count vallen, void *val,
793                        struct ptlrpc_request_set *set)
794 {
795         struct obd_import *imp = class_exp2cliimp(exp);
796         int rc = -EINVAL;
797         ENTRY;
798
799         /* Try to "recover" the initial connection; i.e. retry */
800         if (KEY_IS(KEY_INIT_RECOV)) {
801                 if (vallen != sizeof(int))
802                         RETURN(-EINVAL);
803                 spin_lock(&imp->imp_lock);
804                 imp->imp_initial_recov = *(int *)val;
805                 spin_unlock(&imp->imp_lock);
806                 CDEBUG(D_HA, "%s: set imp_initial_recov = %d\n",
807                        exp->exp_obd->obd_name, imp->imp_initial_recov);
808                 RETURN(0);
809         }
810         /* Turn off initial_recov after we try all backup servers once */
811         if (KEY_IS(KEY_INIT_RECOV_BACKUP)) {
812                 int value;
813                 if (vallen != sizeof(int))
814                         RETURN(-EINVAL);
815                 value = *(int *)val;
816                 spin_lock(&imp->imp_lock);
817                 imp->imp_initial_recov_bk = value > 0;
818                 /* Even after the initial connection, give up all comms if
819                    nobody answers the first time. */
820                 imp->imp_recon_bk = 1;
821                 spin_unlock(&imp->imp_lock);
822                 CDEBUG(D_MGC, "InitRecov %s %d/%d:d%d:i%d:r%d:or%d:%s\n",
823                        imp->imp_obd->obd_name, value, imp->imp_initial_recov,
824                        imp->imp_deactive, imp->imp_invalid,
825                        imp->imp_replayable, imp->imp_obd->obd_replayable,
826                        ptlrpc_import_state_name(imp->imp_state));
827                 /* Resurrect if we previously died */
828                 if (imp->imp_invalid || value > 1)
829                         ptlrpc_reconnect_import(imp);
830                 RETURN(0);
831         }
832         /* FIXME move this to mgc_process_config */
833         if (KEY_IS(KEY_REGISTER_TARGET)) {
834                 struct mgs_target_info *mti;
835                 if (vallen != sizeof(struct mgs_target_info))
836                         RETURN(-EINVAL);
837                 mti = (struct mgs_target_info *)val;
838                 CDEBUG(D_MGC, "register_target %s %#x\n",
839                        mti->mti_svname, mti->mti_flags);
840                 rc =  mgc_target_register(exp, mti);
841                 RETURN(rc);
842         }
843         if (KEY_IS(KEY_SET_FS)) {
844                 struct super_block *sb = (struct super_block *)val;
845                 struct lustre_sb_info *lsi;
846                 if (vallen != sizeof(struct super_block))
847                         RETURN(-EINVAL);
848                 lsi = s2lsi(sb);
849                 rc = mgc_fs_setup(exp->exp_obd, sb, lsi->lsi_srv_mnt);
850                 if (rc) {
851                         CERROR("set_fs got %d\n", rc);
852                 }
853                 RETURN(rc);
854         }
855         if (KEY_IS(KEY_CLEAR_FS)) {
856                 if (vallen != 0)
857                         RETURN(-EINVAL);
858                 rc = mgc_fs_cleanup(exp->exp_obd);
859                 if (rc) {
860                         CERROR("clear_fs got %d\n", rc);
861                 }
862                 RETURN(rc);
863         }
864         if (KEY_IS(KEY_SET_INFO)) {
865                 struct mgs_send_param *msp;
866
867                 msp = (struct mgs_send_param *)val;
868                 rc =  mgc_set_mgs_param(exp, msp);
869                 RETURN(rc);
870         }
871
872         RETURN(rc);
873 }
874
875 static int mgc_import_event(struct obd_device *obd,
876                             struct obd_import *imp,
877                             enum obd_import_event event)
878 {
879         int rc = 0;
880
881         LASSERT(imp->imp_obd == obd);
882         CDEBUG(D_MGC, "import event %#x\n", event);
883
884         switch (event) {
885         case IMP_EVENT_DISCON:
886                 /* MGC imports should not wait for recovery */
887                 break;
888         case IMP_EVENT_INACTIVE:
889                 break;
890         case IMP_EVENT_INVALIDATE: {
891                 struct ldlm_namespace *ns = obd->obd_namespace;
892                 ldlm_namespace_cleanup(ns, LDLM_FL_LOCAL_ONLY);
893                 break;
894         }
895         case IMP_EVENT_ACTIVE:
896                 LCONSOLE_WARN("%s: Reactivating import\n", obd->obd_name);
897                 /* Clearing obd_no_recov allows us to continue pinging */
898                 obd->obd_no_recov = 0;
899                 break;
900         case IMP_EVENT_OCD:
901                 break;
902         default:
903                 CERROR("Unknown import event %#x\n", event);
904                 LBUG();
905         }
906         RETURN(rc);
907 }
908
909 static int mgc_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
910                          struct obd_device *tgt, int count,
911                          struct llog_catid *logid, struct obd_uuid *uuid)
912 {
913         struct llog_ctxt *ctxt;
914         int rc;
915         ENTRY;
916
917         LASSERT(olg == &obd->obd_olg);
918
919         rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, tgt, 0, NULL,
920                         &llog_lvfs_ops);
921         if (rc)
922                 RETURN(rc);
923
924         rc = llog_setup(obd, olg, LLOG_CONFIG_REPL_CTXT, tgt, 0, NULL,
925                         &llog_client_ops);
926         if (rc == 0) {
927                 ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
928                 llog_initiator_connect(ctxt);
929                 llog_ctxt_put(ctxt);
930         } else {
931                 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
932                 if (ctxt)
933                         llog_cleanup(ctxt);
934         }
935
936         RETURN(rc);
937 }
938
939 static int mgc_llog_finish(struct obd_device *obd, int count)
940 {
941         struct llog_ctxt *ctxt;
942         int rc = 0, rc2 = 0;
943         ENTRY;
944
945         ctxt = llog_get_context(obd, LLOG_CONFIG_REPL_CTXT);
946         if (ctxt)
947                 rc = llog_cleanup(ctxt);
948
949         ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
950         if (ctxt)
951                 rc2 = llog_cleanup(ctxt);
952
953         if (!rc)
954                 rc = rc2;
955
956         RETURN(rc);
957 }
958
959 /* identical to mgs_log_is_empty */
960 static int mgc_llog_is_empty(struct obd_device *obd, struct llog_ctxt *ctxt,
961                             char *name)
962 {
963         struct lvfs_run_ctxt saved;
964         struct llog_handle *llh;
965         int rc = 0;
966
967         push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
968         rc = llog_create(ctxt, &llh, NULL, name);
969         if (rc == 0) {
970                 llog_init_handle(llh, LLOG_F_IS_PLAIN, NULL);
971                 rc = llog_get_size(llh);
972                 llog_close(llh);
973         }
974         pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
975         /* header is record 1 */
976         return(rc <= 1);
977 }
978
979 static int mgc_copy_handler(struct llog_handle *llh, struct llog_rec_hdr *rec,
980                             void *data)
981 {
982         struct llog_rec_hdr local_rec = *rec;
983         struct llog_handle *local_llh = (struct llog_handle *)data;
984         char *cfg_buf = (char*) (rec + 1);
985         struct lustre_cfg *lcfg;
986         int rc = 0;
987         ENTRY;
988
989         /* Append all records */
990         local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
991         rc = llog_write_rec(local_llh, &local_rec, NULL, 0,
992                             (void *)cfg_buf, -1);
993
994         lcfg = (struct lustre_cfg *)cfg_buf;
995         CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
996                rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
997                lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
998
999         RETURN(rc);
1000 }
1001
1002 /* Copy a remote log locally */
1003 static int mgc_copy_llog(struct obd_device *obd, struct llog_ctxt *rctxt,
1004                          struct llog_ctxt *lctxt, char *logname)
1005 {
1006         struct llog_handle *local_llh, *remote_llh;
1007         struct obd_uuid *uuid;
1008         char *temp_log;
1009         int rc, rc2;
1010         ENTRY;
1011
1012         /* Write new log to a temp name, then vfs_rename over logname
1013            upon successful completion. */
1014
1015         OBD_ALLOC(temp_log, strlen(logname) + 1);
1016         if (!temp_log)
1017                 RETURN(-ENOMEM);
1018         sprintf(temp_log, "%sT", logname);
1019
1020         /* Make sure there's no old temp log */
1021         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1022         if (rc)
1023                 GOTO(out, rc);
1024         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, NULL);
1025         if (rc)
1026                 GOTO(out, rc);
1027         rc = llog_destroy(local_llh);
1028         llog_free_handle(local_llh);
1029         if (rc)
1030                 GOTO(out, rc);
1031
1032         /* open local log */
1033         rc = llog_create(lctxt, &local_llh, NULL, temp_log);
1034         if (rc)
1035                 GOTO(out, rc);
1036
1037         /* set the log header uuid for fun */
1038         OBD_ALLOC_PTR(uuid);
1039         obd_str2uuid(uuid, logname);
1040         rc = llog_init_handle(local_llh, LLOG_F_IS_PLAIN, uuid);
1041         OBD_FREE_PTR(uuid);
1042         if (rc)
1043                 GOTO(out_closel, rc);
1044
1045         /* open remote log */
1046         rc = llog_create(rctxt, &remote_llh, NULL, logname);
1047         if (rc)
1048                 GOTO(out_closel, rc);
1049         rc = llog_init_handle(remote_llh, LLOG_F_IS_PLAIN, NULL);
1050         if (rc)
1051                 GOTO(out_closer, rc);
1052
1053         /* Copy remote log */
1054         rc = llog_process(remote_llh, mgc_copy_handler,(void *)local_llh, NULL);
1055
1056 out_closer:
1057         rc2 = llog_close(remote_llh);
1058         if (!rc)
1059                 rc = rc2;
1060 out_closel:
1061         rc2 = llog_close(local_llh);
1062         if (!rc)
1063                 rc = rc2;
1064
1065         /* We've copied the remote log to the local temp log, now
1066            replace the old local log with the temp log. */
1067         if (!rc) {
1068                 struct client_obd *cli = &obd->u.cli;
1069                 LASSERT(cli);
1070                 LASSERT(cli->cl_mgc_configs_dir);
1071                 rc = lustre_rename(cli->cl_mgc_configs_dir, cli->cl_mgc_vfsmnt,
1072                                    temp_log, logname);
1073         }
1074         CDEBUG(D_MGC, "Copied remote log %s (%d)\n", logname, rc);
1075 out:
1076         if (rc)
1077                 CERROR("Failed to copy remote log %s (%d)\n", logname, rc);
1078         OBD_FREE(temp_log, strlen(logname) + 1);
1079         RETURN(rc);
1080 }
1081
1082 /* Get a config log from the MGS and process it.
1083    This func is called for both clients and servers. */
1084 static int mgc_process_log(struct obd_device *mgc,
1085                            struct config_llog_data *cld)
1086 {
1087         struct llog_ctxt *ctxt, *lctxt;
1088         struct lustre_handle lockh;
1089         struct client_obd *cli = &mgc->u.cli;
1090         struct lvfs_run_ctxt saved;
1091         struct lustre_sb_info *lsi;
1092         int rc = 0, rcl, flags = 0, must_pop = 0;
1093         ENTRY;
1094
1095         if (!cld || !cld->cld_cfg.cfg_sb) {
1096                 /* This should never happen */
1097                 CERROR("Missing cld, aborting log update\n");
1098                 RETURN(-EINVAL);
1099         }
1100
1101         /* I don't want mutliple processes running process_log at once --
1102            sounds like badness.  It actually might be fine, as long as
1103            we're not trying to update from the same log
1104            simultaneously (in which case we should use a per-log sem.) */
1105         down(&llog_process_lock);
1106
1107         if (cld->cld_stopping) {
1108                 up(&llog_process_lock);
1109                 RETURN(0);
1110         }
1111
1112         OBD_FAIL_TIMEOUT(OBD_FAIL_MGC_PAUSE_PROCESS_LOG, 20);
1113
1114         lsi = s2lsi(cld->cld_cfg.cfg_sb);
1115
1116         CDEBUG(D_MGC, "Process log %s:%s from %d\n", cld->cld_logname,
1117                cld->cld_cfg.cfg_instance, cld->cld_cfg.cfg_last_idx + 1);
1118
1119         ctxt = llog_get_context(mgc, LLOG_CONFIG_REPL_CTXT);
1120         if (!ctxt) {
1121                 CERROR("missing llog context\n");
1122                 up(&llog_process_lock);
1123                 RETURN(-EINVAL);
1124         }
1125
1126         /* Get the cfg lock on the llog */
1127         rcl = mgc_enqueue(mgc->u.cli.cl_mgc_mgsexp, NULL, LDLM_PLAIN, NULL,
1128                           LCK_CR, &flags, NULL, NULL, NULL,
1129                           cld, 0, NULL, &lockh);
1130         if (rcl)
1131                 CDEBUG(D_MGC, "Can't get cfg lock: %d\n", rcl);
1132
1133         lctxt = llog_get_context(mgc, LLOG_CONFIG_ORIG_CTXT);
1134
1135         /* Copy the setup log locally if we can. Don't mess around if we're
1136            running an MGS though (logs are already local). */
1137         if (lctxt && lsi && (lsi->lsi_flags & LSI_SERVER) &&
1138             (lsi->lsi_srv_mnt == cli->cl_mgc_vfsmnt) &&
1139             !IS_MGS(lsi->lsi_ldd)) {
1140                 push_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1141                 must_pop++;
1142                 if (rcl == 0)
1143                         /* Only try to copy log if we have the lock. */
1144                         rc = mgc_copy_llog(mgc, ctxt, lctxt, cld->cld_logname);
1145                 if (rcl || rc) {
1146                         if (mgc_llog_is_empty(mgc, lctxt, cld->cld_logname)) {
1147                                 LCONSOLE_ERROR_MSG(0x13a, "Failed to get MGS "
1148                                                    "log %s and no local copy."
1149                                                    "\n", cld->cld_logname);
1150                                 GOTO(out_pop, rc = -ENOTCONN);
1151                         }
1152                         CDEBUG(D_MGC, "Failed to get MGS log %s, using local "
1153                                "copy for now, will try to update later.\n",
1154                                cld->cld_logname);
1155                 }
1156                 /* Now, whether we copied or not, start using the local llog.
1157                    If we failed to copy, we'll start using whatever the old
1158                    log has. */
1159                 llog_ctxt_put(ctxt);
1160                 ctxt = lctxt;
1161         }
1162
1163         /* logname and instance info should be the same, so use our
1164            copy of the instance for the update.  The cfg_last_idx will
1165            be updated here. */
1166         rc = class_config_parse_llog(ctxt, cld->cld_logname, &cld->cld_cfg);
1167 out_pop:
1168         llog_ctxt_put(ctxt);
1169         if (ctxt != lctxt)
1170                 llog_ctxt_put(lctxt);
1171         if (must_pop)
1172                 pop_ctxt(&saved, &mgc->obd_lvfs_ctxt, NULL);
1173
1174         /* Now drop the lock so MGS can revoke it */
1175         if (!rcl) {
1176                 rcl = mgc_cancel(mgc->u.cli.cl_mgc_mgsexp, NULL,
1177                                  LCK_CR, &lockh);
1178                 if (rcl)
1179                         CERROR("Can't drop cfg lock: %d\n", rcl);
1180         }
1181
1182         CDEBUG(D_MGC, "%s: configuration from log '%s' %sed (%d).\n",
1183                mgc->obd_name, cld->cld_logname, rc ? "fail" : "succeed", rc);
1184
1185         up(&llog_process_lock);
1186
1187         RETURN(rc);
1188 }
1189
1190 static int mgc_process_config(struct obd_device *obd, obd_count len, void *buf)
1191 {
1192         struct lustre_cfg *lcfg = buf;
1193         int cmd;
1194         int rc = 0;
1195         ENTRY;
1196
1197         switch(cmd = lcfg->lcfg_command) {
1198         case LCFG_LOV_ADD_OBD: {
1199                 /* Add any new target, not just osts */
1200                 struct mgs_target_info *mti;
1201
1202                 if (LUSTRE_CFG_BUFLEN(lcfg, 1) !=
1203                     sizeof(struct mgs_target_info))
1204                         GOTO(out, rc = -EINVAL);
1205
1206                 mti = (struct mgs_target_info *)lustre_cfg_buf(lcfg, 1);
1207                 CDEBUG(D_MGC, "add_target %s %#x\n",
1208                        mti->mti_svname, mti->mti_flags);
1209                 rc = mgc_target_register(obd->u.cli.cl_mgc_mgsexp, mti);
1210                 break;
1211         }
1212         case LCFG_LOV_DEL_OBD:
1213                 /* Remove target from the fs? */
1214                 /* FIXME */
1215                 CERROR("lov_del_obd unimplemented\n");
1216                 rc = -ENOSYS;
1217                 break;
1218         case LCFG_LOG_START: {
1219                 struct config_llog_data *cld;
1220                 struct config_llog_instance *cfg;
1221                 struct super_block *sb;
1222                 char *logname = lustre_cfg_string(lcfg, 1);
1223                 cfg = (struct config_llog_instance *)lustre_cfg_buf(lcfg, 2);
1224                 sb = *(struct super_block **)lustre_cfg_buf(lcfg, 3);
1225
1226                 CDEBUG(D_MGC, "parse_log %s from %d\n", logname,
1227                        cfg->cfg_last_idx);
1228
1229                 /* We're only called through here on the initial mount */
1230                 rc = config_log_add(logname, cfg, sb);
1231                 if (rc)
1232                         break;
1233                 cld = config_log_find(logname, cfg);
1234                 if (IS_ERR(cld)) {
1235                         rc = PTR_ERR(cld);
1236                         break;
1237                 }
1238
1239                 /* COMPAT_146 */
1240                 /* FIXME only set this for old logs!  Right now this forces
1241                    us to always skip the "inside markers" check */
1242                 cld->cld_cfg.cfg_flags |= CFG_F_COMPAT146;
1243
1244                 rc = mgc_process_log(obd, cld);
1245                 config_log_put(cld);
1246
1247                 break;
1248         }
1249         case LCFG_LOG_END: {
1250                 struct config_llog_instance *cfg = NULL;
1251                 char *logname = lustre_cfg_string(lcfg, 1);
1252                 if (lcfg->lcfg_bufcount >= 2)
1253                         cfg = (struct config_llog_instance *)lustre_cfg_buf(
1254                                 lcfg, 2);
1255                 rc = config_log_end(logname, cfg);
1256                 break;
1257         }
1258         default: {
1259                 CERROR("Unknown command: %d\n", lcfg->lcfg_command);
1260                 GOTO(out, rc = -EINVAL);
1261
1262         }
1263         }
1264 out:
1265         RETURN(rc);
1266 }
1267
1268 struct obd_ops mgc_obd_ops = {
1269         .o_owner        = THIS_MODULE,
1270         .o_setup        = mgc_setup,
1271         .o_precleanup   = mgc_precleanup,
1272         .o_cleanup      = mgc_cleanup,
1273         .o_add_conn     = client_import_add_conn,
1274         .o_del_conn     = client_import_del_conn,
1275         .o_connect      = client_connect_import,
1276         .o_disconnect   = client_disconnect_export,
1277         //.o_enqueue      = mgc_enqueue,
1278         .o_cancel       = mgc_cancel,
1279         //.o_iocontrol    = mgc_iocontrol,
1280         .o_set_info_async = mgc_set_info_async,
1281         .o_import_event = mgc_import_event,
1282         .o_llog_init    = mgc_llog_init,
1283         .o_llog_finish  = mgc_llog_finish,
1284         .o_process_config = mgc_process_config,
1285 };
1286
1287 int __init mgc_init(void)
1288 {
1289         return class_register_type(&mgc_obd_ops, NULL, NULL,
1290                                    LUSTRE_MGC_NAME, NULL);
1291 }
1292
1293 #ifdef __KERNEL__
1294 static void /*__exit*/ mgc_exit(void)
1295 {
1296         class_unregister_type(LUSTRE_MGC_NAME);
1297 }
1298
1299 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1300 MODULE_DESCRIPTION("Lustre Management Client");
1301 MODULE_LICENSE("GPL");
1302
1303 module_init(mgc_init);
1304 module_exit(mgc_exit);
1305 #endif