4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mgs/mgs_handler.c
38 * Author: Nathan Rutman <nathan@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_MGS
42 #define D_MGS D_CONFIG
45 # include <linux/module.h>
46 # include <linux/pagemap.h>
47 # include <linux/miscdevice.h>
48 # include <linux/init.h>
50 # include <liblustre.h>
53 #include <obd_class.h>
54 #include <lustre_dlm.h>
55 #include <lprocfs_status.h>
56 #include <lustre_fsfilt.h>
57 #include <lustre_disk.h>
58 #include "mgs_internal.h"
59 #include <lustre_param.h>
61 /* Establish a connection to the MGS.*/
62 static int mgs_connect(const struct lu_env *env,
63 struct obd_export **exp, struct obd_device *obd,
64 struct obd_uuid *cluuid, struct obd_connect_data *data,
67 struct obd_export *lexp;
68 struct lustre_handle conn = { 0 };
72 if (!exp || !obd || !cluuid)
75 rc = class_connect(&conn, obd, cluuid);
79 lexp = class_conn2export(&conn);
81 GOTO(out, rc = -EFAULT);
83 mgs_counter_incr(lexp, LPROC_MGS_CONNECT);
86 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
87 lexp->exp_connect_flags = data->ocd_connect_flags;
88 data->ocd_version = LUSTRE_VERSION_CODE;
91 rc = mgs_export_stats_init(obd, lexp, localdata);
95 class_disconnect(lexp);
103 static int mgs_reconnect(const struct lu_env *env,
104 struct obd_export *exp, struct obd_device *obd,
105 struct obd_uuid *cluuid, struct obd_connect_data *data,
110 if (exp == NULL || obd == NULL || cluuid == NULL)
113 mgs_counter_incr(exp, LPROC_MGS_CONNECT);
116 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
117 exp->exp_connect_flags = data->ocd_connect_flags;
118 data->ocd_version = LUSTRE_VERSION_CODE;
121 RETURN(mgs_export_stats_init(obd, exp, localdata));
124 static int mgs_disconnect(struct obd_export *exp)
131 mgs_fsc_cleanup(exp);
133 class_export_get(exp);
134 mgs_counter_incr(exp, LPROC_MGS_DISCONNECT);
136 rc = server_disconnect_export(exp);
137 class_export_put(exp);
141 static int mgs_cleanup(struct obd_device *obd);
142 static int mgs_handle(struct ptlrpc_request *req);
144 static int mgs_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
145 struct obd_device *tgt, int *index)
150 LASSERT(olg == &obd->obd_olg);
151 rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
156 static int mgs_llog_finish(struct obd_device *obd, int count)
158 struct llog_ctxt *ctxt;
162 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
164 rc = llog_cleanup(ctxt);
169 /* Start the MGS obd */
170 static int mgs_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
172 static struct ptlrpc_service_conf conf;
173 struct lprocfs_static_vars lvars;
174 struct mgs_device *mgs = &obd->u.mgs;
175 struct lustre_mount_info *lmi;
176 struct lustre_sb_info *lsi;
177 struct vfsmount *mnt;
181 CDEBUG(D_CONFIG, "Starting MGS\n");
184 lmi = server_get_mount(obd->obd_name);
186 RETURN(rc = -EINVAL);
189 lsi = s2lsi(lmi->lmi_sb);
190 obd->obd_fsops = fsfilt_get_ops(lsi->lsi_fstype);
191 if (IS_ERR(obd->obd_fsops))
192 GOTO(err_put, rc = PTR_ERR(obd->obd_fsops));
194 if (lvfs_check_rdonly(lvfs_sbdev(mnt->mnt_sb))) {
195 CERROR("%s: Underlying device is marked as read-only. "
196 "Setup failed\n", obd->obd_name);
197 GOTO(err_ops, rc = -EROFS);
200 obd->u.obt.obt_magic = OBT_MAGIC;
201 obd->u.obt.obt_instance = 0;
204 /* namespace for mgs llog */
205 obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
206 LDLM_NAMESPACE_SERVER,
207 LDLM_NAMESPACE_MODEST,
209 if (obd->obd_namespace == NULL)
210 GOTO(err_ops, rc = -ENOMEM);
213 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
214 "mgs_ldlm_client", &obd->obd_ldlm_client);
216 rc = mgs_fs_setup(obd, mnt);
218 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
223 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
227 /* No recovery for MGC's */
228 obd->obd_replayable = 0;
230 /* Internal mgs setup */
231 mgs_init_fsdb_list(mgs);
232 cfs_mutex_init(&mgs->mgs_mutex);
233 mgs->mgs_start_time = cfs_time_current_sec();
236 lprocfs_mgs_init_vars(&lvars);
237 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0) {
238 lproc_mgs_setup(mgs);
239 rc = lprocfs_alloc_md_stats(obd, LPROC_MGS_LAST);
244 conf = (typeof(conf)) {
245 .psc_name = LUSTRE_MGS_NAME,
246 .psc_watchdog_factor = MGS_SERVICE_WATCHDOG_FACTOR,
248 .bc_nbufs = MGS_NBUFS,
249 .bc_buf_size = MGS_BUFSIZE,
250 .bc_req_max_size = MGS_MAXREQSIZE,
251 .bc_rep_max_size = MGS_MAXREPSIZE,
252 .bc_req_portal = MGS_REQUEST_PORTAL,
253 .bc_rep_portal = MGC_REPLY_PORTAL,
256 .tc_thr_name = "ll_mgs",
257 .tc_nthrs_init = MGS_NTHRS_INIT,
258 .tc_nthrs_max = MGS_NTHRS_MAX,
259 .tc_ctx_tags = LCT_MG_THREAD,
262 .so_req_handler = mgs_handle,
263 .so_req_printer = target_print_req,
266 /* Start the service threads */
267 mgs->mgs_service = ptlrpc_register_service(&conf, obd->obd_proc_entry);
268 if (IS_ERR(mgs->mgs_service)) {
269 rc = PTR_ERR(mgs->mgs_service);
270 CERROR("failed to start service: %d\n", rc);
274 ping_evictor_start();
276 CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
281 lproc_mgs_cleanup(mgs);
282 obd_llog_finish(obd, 0);
284 /* No extra cleanup needed for llog_init_commit_thread() */
287 ldlm_namespace_free(obd->obd_namespace, NULL, 0);
288 obd->obd_namespace = NULL;
290 fsfilt_put_ops(obd->obd_fsops);
292 server_put_mount(obd->obd_name, mnt);
297 static int mgs_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
299 struct mgs_device *mgs = &obd->u.mgs;
304 case OBD_CLEANUP_EARLY:
306 case OBD_CLEANUP_EXPORTS:
308 ptlrpc_unregister_service(mgs->mgs_service);
309 mgs_cleanup_fsdb_list(mgs);
310 rc = obd_llog_finish(obd, 0);
311 lproc_mgs_cleanup(mgs);
318 * Performs cleanup procedures for passed \a obd given it is mgs obd.
320 static int mgs_cleanup(struct obd_device *obd)
322 struct mgs_device *mgs = &obd->u.mgs;
325 if (mgs->mgs_sb == NULL)
330 server_put_mount(obd->obd_name, mgs->mgs_vfsmnt);
333 ldlm_namespace_free(obd->obd_namespace, NULL, 1);
334 obd->obd_namespace = NULL;
336 fsfilt_put_ops(obd->obd_fsops);
338 LCONSOLE_INFO("%s has stopped.\n", obd->obd_name);
342 static int mgs_completion_ast_config(struct ldlm_lock *lock, int flags,
347 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
348 LDLM_FL_BLOCK_CONV))) {
349 struct fs_db *fsdb = (struct fs_db *)lock->l_ast_data;
350 struct lustre_handle lockh;
352 /* clear the bit before lock put */
353 cfs_clear_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags);
355 ldlm_lock2handle(lock, &lockh);
356 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
359 RETURN(ldlm_completion_ast(lock, flags, cbdata));
362 static int mgs_completion_ast_ir(struct ldlm_lock *lock, int flags,
367 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
368 LDLM_FL_BLOCK_CONV))) {
371 /* l_ast_data is used as a marker to avoid cancel ldlm lock
372 * twice. See LU-1259. */
373 lock_res_and_lock(lock);
374 fsdb = (struct fs_db *)lock->l_ast_data;
375 lock->l_ast_data = NULL;
376 unlock_res_and_lock(lock);
379 struct lustre_handle lockh;
381 mgs_ir_notify_complete(fsdb);
383 ldlm_lock2handle(lock, &lockh);
384 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
388 RETURN(ldlm_completion_ast(lock, flags, cbdata));
391 void mgs_revoke_lock(struct mgs_device *mgs, struct fs_db *fsdb, int type)
393 ldlm_completion_callback cp = NULL;
394 struct lustre_handle lockh = { 0 };
395 struct ldlm_res_id res_id;
396 int flags = LDLM_FL_ATOMIC_CB;
400 LASSERT(fsdb->fsdb_name[0] != '\0');
401 rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
405 case CONFIG_T_CONFIG:
406 cp = mgs_completion_ast_config;
407 if (cfs_test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
410 case CONFIG_T_RECOVER:
411 cp = mgs_completion_ast_ir;
418 rc = ldlm_cli_enqueue_local(mgs->mgs_obd->obd_namespace,
419 &res_id, LDLM_PLAIN, NULL, LCK_EX,
420 &flags, ldlm_blocking_ast, cp,
421 NULL, fsdb, 0, NULL, &lockh);
422 if (rc != ELDLM_OK) {
423 CERROR("can't take cfg lock for "LPX64"/"LPX64"(%d)\n",
424 le64_to_cpu(res_id.name[0]),
425 le64_to_cpu(res_id.name[1]), rc);
427 if (type == CONFIG_T_CONFIG)
428 cfs_clear_bit(FSDB_REVOKING_LOCK,
431 /* lock has been cancelled in completion_ast. */
440 static int mgs_check_target(const struct lu_env *env,
441 struct mgs_device *mgs,
442 struct mgs_target_info *mti)
447 rc = mgs_check_index(env, mgs, mti);
449 LCONSOLE_ERROR_MSG(0x13b, "%s claims to have registered, but "
450 "this MGS does not know about it, preventing "
451 "registration.\n", mti->mti_svname);
453 } else if (rc == -1) {
454 LCONSOLE_ERROR_MSG(0x13c, "Client log %s-client has "
455 "disappeared! Regenerating all logs.\n",
457 mti->mti_flags |= LDD_F_WRITECONF;
460 /* Index is correctly marked as used */
462 /* If the logs don't contain the mti_nids then add
463 them as failover nids */
464 rc = mgs_check_failnid(env, mgs, mti);
470 /* Ensure this is not a failover node that is connecting first*/
471 static int mgs_check_failover_reg(struct mgs_target_info *mti)
477 ptr = mti->mti_params;
478 while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
479 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
480 for (i = 0; i < mti->mti_nid_count; i++) {
481 if (nid == mti->mti_nids[i]) {
482 LCONSOLE_WARN("Denying initial registra"
483 "tion attempt from nid %s"
484 ", specified as failover"
485 "\n",libcfs_nid2str(nid));
486 return -EADDRNOTAVAIL;
494 /* Called whenever a target starts up. Flags indicate first connect, etc. */
495 static int mgs_handle_target_reg(struct ptlrpc_request *req)
497 struct obd_device *obd = req->rq_export->exp_obd;
498 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
499 struct lu_env *env = req->rq_svc_thread->t_env;
500 struct mgs_target_info *mti, *rep_mti;
506 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_REG);
508 mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
510 opc = mti->mti_flags & LDD_F_OPC_MASK;
511 if (opc == LDD_F_OPC_READY) {
512 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
513 mti->mti_fsname, mti->mti_stripe_index);
514 rc = mgs_ir_update(env, mgs, mti);
516 LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
517 CERROR("Update IR return with %d(ignore and IR "
520 GOTO(out_nolock, rc);
523 /* Do not support unregistering right now. */
524 if (opc != LDD_F_OPC_REG)
525 GOTO(out_nolock, rc = -EINVAL);
527 CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
528 mti->mti_fsname, mti->mti_stripe_index);
530 if (mti->mti_flags & LDD_F_NEED_INDEX)
531 mti->mti_flags |= LDD_F_WRITECONF;
533 if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPGRADE14 |
535 /* We're just here as a startup ping. */
536 CDEBUG(D_MGS, "Server %s is running on %s\n",
537 mti->mti_svname, obd_export_nid2str(req->rq_export));
538 rc = mgs_check_target(env, mgs, mti);
539 /* above will set appropriate mti flags */
541 /* Nothing wrong, or fatal error */
542 GOTO(out_nolock, rc);
544 if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)
545 && (rc = mgs_check_failover_reg(mti)))
546 GOTO(out_nolock, rc);
549 OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
551 if (mti->mti_flags & LDD_F_WRITECONF) {
552 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
553 mti->mti_stripe_index == 0) {
554 rc = mgs_erase_logs(env, mgs, mti->mti_fsname);
555 LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
556 "request. All servers must be restarted "
557 "in order to regenerate the logs."
558 "\n", obd->obd_name, mti->mti_fsname);
559 } else if (mti->mti_flags &
560 (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
561 rc = mgs_erase_log(env, mgs, mti->mti_svname);
562 LCONSOLE_WARN("%s: Regenerating %s log by user "
564 obd->obd_name, mti->mti_svname);
566 mti->mti_flags |= LDD_F_UPDATE;
567 /* Erased logs means start from scratch. */
568 mti->mti_flags &= ~LDD_F_UPGRADE14;
570 GOTO(out_nolock, rc);
573 rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
575 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
576 GOTO(out_nolock, rc);
580 * Log writing contention is handled by the fsdb_mutex.
582 * It should be alright if someone was reading while we were
583 * updating the logs - if we revoke at the end they will just update
584 * from where they left off.
587 if (mti->mti_flags & LDD_F_UPGRADE14) {
588 CERROR("Can't upgrade from 1.4 (%d)\n", rc);
592 if (mti->mti_flags & LDD_F_UPDATE) {
593 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
594 mti->mti_stripe_index);
596 /* create or update the target log
597 and update the client/mdt logs */
598 rc = mgs_write_log_target(env, mgs, mti, fsdb);
600 CERROR("Failed to write %s log (%d)\n",
601 mti->mti_svname, rc);
605 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
606 LDD_F_NEED_INDEX | LDD_F_WRITECONF |
608 mti->mti_flags |= LDD_F_REWRITE_LDD;
612 mgs_revoke_lock(mgs, fsdb, CONFIG_T_CONFIG);
615 CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
616 mti->mti_stripe_index, rc);
619 /* we need an error flag to tell the target what's going on,
620 * instead of just doing it by error code only. */
621 mti->mti_flags |= LDD_F_ERROR;
623 rc = req_capsule_server_pack(&req->rq_pill);
627 /* send back the whole mti in the reply */
628 rep_mti = req_capsule_server_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
631 /* Flush logs to disk */
632 fsfilt_sync(obd, obd->u.mgs.mgs_sb);
636 static int mgs_set_info_rpc(struct ptlrpc_request *req)
638 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
639 struct lu_env *env = req->rq_svc_thread->t_env;
640 struct mgs_send_param *msp, *rep_msp;
641 struct mgs_thread_info *mgi = mgs_env_info(env);
643 struct lustre_cfg *lcfg;
646 msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
649 /* Construct lustre_cfg structure to pass to function mgs_setparam */
650 lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
651 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, msp->mgs_param);
652 lcfg = lustre_cfg_new(LCFG_PARAM, &mgi->mgi_bufs);
654 GOTO(out, rc = PTR_ERR(lcfg));
655 rc = mgs_setparam(env, mgs, lcfg, mgi->mgi_fsname);
657 CERROR("Error %d in setting the parameter %s for fs %s\n",
658 rc, msp->mgs_param, mgi->mgi_fsname);
662 rc = req_capsule_server_pack(&req->rq_pill);
664 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
668 lustre_cfg_free(lcfg);
673 static int mgs_config_read(struct ptlrpc_request *req)
675 struct mgs_config_body *body;
679 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
683 switch (body->mcb_type) {
684 case CONFIG_T_RECOVER:
685 rc = mgs_get_ir_logs(req);
688 case CONFIG_T_CONFIG:
701 * similar as in ost_connect_check_sptlrpc()
703 static int mgs_connect_check_sptlrpc(struct ptlrpc_request *req)
705 struct obd_export *exp = req->rq_export;
706 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
707 struct lu_env *env = req->rq_svc_thread->t_env;
709 struct sptlrpc_flavor flvr;
712 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
713 rc = mgs_find_or_make_fsdb(env, mgs, MGSSELF_NAME, &fsdb);
717 cfs_mutex_lock(&fsdb->fsdb_mutex);
718 if (sptlrpc_rule_set_choose(&fsdb->fsdb_srpc_gen,
719 LUSTRE_SP_MGC, LUSTRE_SP_MGS,
722 /* by defualt allow any flavors */
723 flvr.sf_rpc = SPTLRPC_FLVR_ANY;
725 cfs_mutex_unlock(&fsdb->fsdb_mutex);
727 cfs_spin_lock(&exp->exp_lock);
729 exp->exp_sp_peer = req->rq_sp_from;
730 exp->exp_flvr = flvr;
732 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
733 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
734 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
735 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
736 libcfs_nid2str(req->rq_peer.nid));
740 cfs_spin_unlock(&exp->exp_lock);
742 if (exp->exp_sp_peer != req->rq_sp_from) {
743 CERROR("RPC source %s doesn't match %s\n",
744 sptlrpc_part2name(req->rq_sp_from),
745 sptlrpc_part2name(exp->exp_sp_peer));
748 rc = sptlrpc_target_export_check(exp, req);
755 /* Called whenever a target cleans up. */
756 /* XXX - Currently unused */
757 static int mgs_handle_target_del(struct ptlrpc_request *req)
760 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_DEL);
764 /* XXX - Currently unused */
765 static int mgs_handle_exception(struct ptlrpc_request *req)
768 mgs_counter_incr(req->rq_export, LPROC_MGS_EXCEPTION);
773 * For old clients there is no direct way of knowing which filesystems
774 * a client is operating at the MGS side. But we need to pick up those
775 * clients so that the MGS can mark the corresponding filesystem as
776 * non-IR capable because old clients are not ready to be notified.
778 * This is why we have this _hack_ function. We detect the filesystem's
779 * name by hacking llog operation which is currently used by the clients
780 * to fetch configuration logs. At present this is fine because this is
781 * the ONLY llog operation between mgc and the MGS.
783 * If extra llog operation is going to be added, this function needs fixing.
785 * If releases prior than 2.0 are not supported, we can remove this function.
787 static int mgs_handle_fslog_hack(struct ptlrpc_request *req)
794 /* XXX: We suppose that llog at mgs is only used for
795 * fetching file system log */
796 logname = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
797 if (logname == NULL) {
798 CERROR("No logname, is llog on MGS used for something else?\n");
802 ptr = strchr(logname, '-');
803 rc = (int)(ptr - logname);
804 if (ptr == NULL || rc >= sizeof(fsname)) {
805 CERROR("Invalid logname received: %s\n", logname);
809 strncpy(fsname, logname, rc);
811 rc = mgs_fsc_attach(req->rq_svc_thread->t_env, req->rq_export, fsname);
812 if (rc < 0 && rc != -EEXIST)
813 CERROR("add fs client %s returns %d\n", fsname, rc);
818 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
819 int mgs_handle(struct ptlrpc_request *req)
821 int fail = OBD_FAIL_MGS_ALL_REPLY_NET;
825 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
826 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_MGS_PAUSE_REQ, cfs_fail_val);
827 if (CFS_FAIL_CHECK(OBD_FAIL_MGS_ALL_REQUEST_NET))
830 LASSERT(current->journal_info == NULL);
831 opc = lustre_msg_get_opc(req->rq_reqmsg);
833 if (opc == SEC_CTX_INIT ||
834 opc == SEC_CTX_INIT_CONT ||
838 if (opc != MGS_CONNECT) {
839 if (!class_connected_export(req->rq_export)) {
840 DEBUG_REQ(D_MGS, req, "operation on unconnected MGS\n");
841 req->rq_status = -ENOTCONN;
842 GOTO(out, rc = -ENOTCONN);
848 DEBUG_REQ(D_MGS, req, "connect");
849 /* MGS and MDS have same request format for connect */
850 req_capsule_set(&req->rq_pill, &RQF_MDS_CONNECT);
851 rc = target_handle_connect(req);
853 rc = mgs_connect_check_sptlrpc(req);
855 if (!rc && (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1))
856 /* Make clients trying to reconnect after a MGS restart
857 happy; also requires obd_replayable */
858 lustre_msg_add_op_flags(req->rq_repmsg,
859 MSG_CONNECT_RECONNECT);
862 DEBUG_REQ(D_MGS, req, "disconnect");
863 /* MGS and MDS have same request format for disconnect */
864 req_capsule_set(&req->rq_pill, &RQF_MDS_DISCONNECT);
865 rc = target_handle_disconnect(req);
866 req->rq_status = rc; /* superfluous? */
869 DEBUG_REQ(D_MGS, req, "exception");
870 rc = mgs_handle_exception(req);
873 DEBUG_REQ(D_MGS, req, "target add");
874 req_capsule_set(&req->rq_pill, &RQF_MGS_TARGET_REG);
875 rc = mgs_handle_target_reg(req);
878 DEBUG_REQ(D_MGS, req, "target del");
879 rc = mgs_handle_target_del(req);
882 DEBUG_REQ(D_MGS, req, "set_info");
883 req_capsule_set(&req->rq_pill, &RQF_MGS_SET_INFO);
884 rc = mgs_set_info_rpc(req);
886 case MGS_CONFIG_READ:
887 DEBUG_REQ(D_MGS, req, "read config");
888 req_capsule_set(&req->rq_pill, &RQF_MGS_CONFIG_READ);
889 rc = mgs_config_read(req);
892 DEBUG_REQ(D_MGS, req, "enqueue");
893 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
894 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
895 ldlm_server_blocking_ast, NULL);
897 case LDLM_BL_CALLBACK:
898 case LDLM_CP_CALLBACK:
899 DEBUG_REQ(D_MGS, req, "callback");
900 CERROR("callbacks should not happen on MGS\n");
905 DEBUG_REQ(D_INFO, req, "ping");
906 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
907 rc = target_handle_ping(req);
910 DEBUG_REQ(D_MGS, req, "log cancel");
911 rc = -ENOTSUPP; /* la la la */
914 case LLOG_ORIGIN_HANDLE_CREATE:
915 DEBUG_REQ(D_MGS, req, "llog_open");
916 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
917 rc = llog_origin_handle_open(req);
919 (void)mgs_handle_fslog_hack(req);
921 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
922 DEBUG_REQ(D_MGS, req, "llog next block");
923 req_capsule_set(&req->rq_pill,
924 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
925 rc = llog_origin_handle_next_block(req);
927 case LLOG_ORIGIN_HANDLE_READ_HEADER:
928 DEBUG_REQ(D_MGS, req, "llog read header");
929 req_capsule_set(&req->rq_pill,
930 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
931 rc = llog_origin_handle_read_header(req);
933 case LLOG_ORIGIN_HANDLE_CLOSE:
934 DEBUG_REQ(D_MGS, req, "llog close");
935 rc = llog_origin_handle_close(req);
941 LASSERT(current->journal_info == NULL);
943 DEBUG_REQ(D_MGS, req, "MGS fail to handle opc = %d: rc = %d\n",
946 rc = ptlrpc_error(req);
950 target_send_reply(req, rc, fail);
954 static inline int mgs_init_export(struct obd_export *exp)
956 struct mgs_export_data *data = &exp->u.eu_mgs_data;
958 /* init mgs_export_data for fsc */
959 cfs_spin_lock_init(&data->med_lock);
960 CFS_INIT_LIST_HEAD(&data->med_clients);
962 cfs_spin_lock(&exp->exp_lock);
963 exp->exp_connecting = 1;
964 cfs_spin_unlock(&exp->exp_lock);
966 /* self-export doesn't need client data and ldlm initialization */
967 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
968 &exp->exp_client_uuid)))
970 return ldlm_init_export(exp);
973 static inline int mgs_destroy_export(struct obd_export *exp)
977 target_destroy_export(exp);
978 mgs_client_free(exp);
980 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
981 &exp->exp_client_uuid)))
984 ldlm_destroy_export(exp);
989 static int mgs_extract_fs_pool(char * arg, char *fsname, char *poolname)
994 for (ptr = arg; (*ptr != '\0') && (*ptr != '.'); ptr++ ) {
1002 strcpy(poolname, ptr);
1007 static int mgs_iocontrol_pool(const struct lu_env *env,
1008 struct mgs_device *mgs,
1009 struct obd_ioctl_data *data)
1011 struct mgs_thread_info *mgi = mgs_env_info(env);
1013 struct lustre_cfg *lcfg = NULL;
1014 struct llog_rec_hdr rec;
1015 char *poolname = NULL;
1018 OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
1019 if (poolname == NULL)
1021 rec.lrh_len = llog_data_len(data->ioc_plen1);
1023 if (data->ioc_type == LUSTRE_CFG_TYPE) {
1024 rec.lrh_type = OBD_CFG_REC;
1026 CERROR("unknown cfg record type:%d \n", data->ioc_type);
1027 GOTO(out_pool, rc = -EINVAL);
1030 if (data->ioc_plen1 > CFS_PAGE_SIZE)
1031 GOTO(out_pool, rc = -E2BIG);
1033 OBD_ALLOC(lcfg, data->ioc_plen1);
1035 GOTO(out_pool, rc = -ENOMEM);
1037 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
1038 GOTO(out_lcfg, rc = -EFAULT);
1040 if (lcfg->lcfg_bufcount < 2)
1041 GOTO(out_lcfg, rc = -EFAULT);
1043 /* first arg is always <fsname>.<poolname> */
1044 rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
1049 switch (lcfg->lcfg_command) {
1051 if (lcfg->lcfg_bufcount != 2)
1052 GOTO(out_lcfg, rc = -EINVAL);
1053 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
1057 if (lcfg->lcfg_bufcount != 3)
1058 GOTO(out_lcfg, rc = -EINVAL);
1059 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
1060 poolname, lustre_cfg_string(lcfg, 2));
1063 if (lcfg->lcfg_bufcount != 3)
1064 GOTO(out_lcfg, rc = -EINVAL);
1065 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
1066 poolname, lustre_cfg_string(lcfg, 2));
1069 if (lcfg->lcfg_bufcount != 2)
1070 GOTO(out_lcfg, rc = -EINVAL);
1071 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
1079 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
1080 rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
1085 OBD_FREE(lcfg, data->ioc_plen1);
1087 OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
1091 /* from mdt_iocontrol */
1092 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1093 void *karg, void *uarg)
1095 struct mgs_device *mgs = exp2mgs_dev(exp);
1096 struct obd_ioctl_data *data = karg;
1097 struct lvfs_run_ctxt saved;
1102 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1104 rc = lu_env_init(&env, LCT_MG_THREAD);
1110 case OBD_IOC_PARAM: {
1111 struct mgs_thread_info *mgi = mgs_env_info(&env);
1112 struct lustre_cfg *lcfg;
1113 struct llog_rec_hdr rec;
1115 rec.lrh_len = llog_data_len(data->ioc_plen1);
1117 if (data->ioc_type == LUSTRE_CFG_TYPE) {
1118 rec.lrh_type = OBD_CFG_REC;
1120 CERROR("unknown cfg record type:%d \n", data->ioc_type);
1121 GOTO(out, rc = -EINVAL);
1124 OBD_ALLOC(lcfg, data->ioc_plen1);
1126 GOTO(out, rc = -ENOMEM);
1127 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
1128 GOTO(out_free, rc = -EFAULT);
1130 if (lcfg->lcfg_bufcount < 1)
1131 GOTO(out_free, rc = -EINVAL);
1133 rc = mgs_setparam(&env, mgs, lcfg, mgi->mgi_fsname);
1135 CERROR("%s: setparam err: rc = %d\n",
1136 exp->exp_obd->obd_name, rc);
1138 OBD_FREE(lcfg, data->ioc_plen1);
1143 rc = mgs_iocontrol_pool(&env, mgs, data);
1146 case OBD_IOC_DUMP_LOG: {
1147 struct llog_ctxt *ctxt;
1148 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1149 push_ctxt(&saved, &mgs->mgs_obd->obd_lvfs_ctxt, NULL);
1150 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
1151 pop_ctxt(&saved, &mgs->mgs_obd->obd_lvfs_ctxt, NULL);
1152 llog_ctxt_put(ctxt);
1157 case OBD_IOC_LLOG_CHECK:
1158 case OBD_IOC_LLOG_INFO:
1159 case OBD_IOC_LLOG_PRINT: {
1160 struct llog_ctxt *ctxt;
1161 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1163 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
1164 rc = llog_ioctl(ctxt, cmd, data);
1165 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
1166 llog_ctxt_put(ctxt);
1172 CDEBUG(D_INFO, "unknown command %x\n", cmd);
1181 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1182 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1184 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1186 /* use obd ops to offer management infrastructure */
1187 static struct obd_ops mgs_obd_ops = {
1188 .o_owner = THIS_MODULE,
1189 .o_connect = mgs_connect,
1190 .o_reconnect = mgs_reconnect,
1191 .o_disconnect = mgs_disconnect,
1192 .o_setup = mgs_setup,
1193 .o_precleanup = mgs_precleanup,
1194 .o_cleanup = mgs_cleanup,
1195 .o_init_export = mgs_init_export,
1196 .o_destroy_export = mgs_destroy_export,
1197 .o_iocontrol = mgs_iocontrol,
1198 .o_llog_init = mgs_llog_init,
1199 .o_llog_finish = mgs_llog_finish
1202 static int __init mgs_init(void)
1204 struct lprocfs_static_vars lvars;
1207 lprocfs_mgs_init_vars(&lvars);
1208 class_register_type(&mgs_obd_ops, NULL,
1209 lvars.module_vars, LUSTRE_MGS_NAME, NULL);
1211 LU_CONTEXT_KEY_INIT(&mgs_thread_key);
1212 rc = lu_context_key_register(&mgs_thread_key);
1214 class_unregister_type(LUSTRE_MGS_NAME);
1219 static void /*__exit*/ mgs_exit(void)
1221 class_unregister_type(LUSTRE_MGS_NAME);
1222 lu_context_key_degister(&mgs_thread_key);
1225 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1226 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
1227 MODULE_LICENSE("GPL");
1229 module_init(mgs_init);
1230 module_exit(mgs_exit);