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