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>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_MGS
45 #define D_MGS D_CONFIG
48 # include <linux/module.h>
49 # include <linux/pagemap.h>
50 # include <linux/miscdevice.h>
51 # include <linux/init.h>
53 # include <liblustre.h>
56 #include <obd_class.h>
57 #include <lustre_dlm.h>
58 #include <lprocfs_status.h>
59 #include <lustre_fsfilt.h>
60 #include <lustre_disk.h>
61 #include "mgs_internal.h"
62 #include <lustre_param.h>
64 /* Establish a connection to the MGS.*/
65 static int mgs_connect(const struct lu_env *env,
66 struct obd_export **exp, struct obd_device *obd,
67 struct obd_uuid *cluuid, struct obd_connect_data *data,
70 struct obd_export *lexp;
71 struct lustre_handle conn = { 0 };
75 if (!exp || !obd || !cluuid)
78 rc = class_connect(&conn, obd, cluuid);
82 lexp = class_conn2export(&conn);
85 mgs_counter_incr(lexp, LPROC_MGS_CONNECT);
88 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
89 lexp->exp_connect_flags = data->ocd_connect_flags;
90 data->ocd_version = LUSTRE_VERSION_CODE;
93 rc = mgs_export_stats_init(obd, lexp, localdata);
96 class_disconnect(lexp);
104 static int mgs_reconnect(const struct lu_env *env,
105 struct obd_export *exp, struct obd_device *obd,
106 struct obd_uuid *cluuid, struct obd_connect_data *data,
111 if (exp == NULL || obd == NULL || cluuid == NULL)
114 mgs_counter_incr(exp, LPROC_MGS_CONNECT);
117 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
118 exp->exp_connect_flags = data->ocd_connect_flags;
119 data->ocd_version = LUSTRE_VERSION_CODE;
122 RETURN(mgs_export_stats_init(obd, exp, localdata));
125 static int mgs_disconnect(struct obd_export *exp)
132 mgs_fsc_cleanup(exp);
134 class_export_get(exp);
135 mgs_counter_incr(exp, LPROC_MGS_DISCONNECT);
137 rc = server_disconnect_export(exp);
138 class_export_put(exp);
142 static int mgs_cleanup(struct obd_device *obd);
143 static int mgs_handle(struct ptlrpc_request *req);
145 static int mgs_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
146 struct obd_device *tgt, int *index)
151 LASSERT(olg == &obd->obd_olg);
152 rc = llog_setup(obd, olg, LLOG_CONFIG_ORIG_CTXT, obd, 0, NULL,
157 static int mgs_llog_finish(struct obd_device *obd, int count)
159 struct llog_ctxt *ctxt;
163 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
165 rc = llog_cleanup(ctxt);
170 /* Start the MGS obd */
171 static int mgs_setup(struct obd_device *obd, struct lustre_cfg *lcfg)
173 struct lprocfs_static_vars lvars;
174 struct mgs_obd *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(MT_STR(lsi->lsi_ldd));
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;
203 /* namespace for mgs llog */
204 obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
205 LDLM_NAMESPACE_SERVER,
206 LDLM_NAMESPACE_MODEST,
208 if (obd->obd_namespace == NULL)
209 GOTO(err_ops, rc = -ENOMEM);
212 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
213 "mgs_ldlm_client", &obd->obd_ldlm_client);
215 rc = mgs_fs_setup(obd, mnt);
217 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
222 rc = obd_llog_init(obd, &obd->obd_olg, obd, NULL);
226 /* No recovery for MGC's */
227 obd->obd_replayable = 0;
229 /* Internal mgs setup */
230 mgs_init_fsdb_list(obd);
231 cfs_mutex_init(&mgs->mgs_mutex);
232 mgs->mgs_start_time = cfs_time_current_sec();
235 lprocfs_mgs_init_vars(&lvars);
236 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0) {
237 lproc_mgs_setup(obd);
238 rc = lprocfs_alloc_md_stats(obd, LPROC_MGS_LAST);
243 /* Start the service threads */
245 ptlrpc_init_svc(MGS_NBUFS, MGS_BUFSIZE, MGS_MAXREQSIZE,
246 MGS_MAXREPSIZE, MGS_REQUEST_PORTAL,
248 mgs_handle, LUSTRE_MGS_NAME,
249 obd->obd_proc_entry, target_print_req,
250 MGS_THREADS_AUTO_MIN, MGS_THREADS_AUTO_MAX,
251 "ll_mgs", LCT_MD_THREAD, NULL);
253 if (!mgs->mgs_service) {
254 CERROR("failed to start service\n");
255 GOTO(err_llog, rc = -ENOMEM);
258 rc = ptlrpc_start_threads(mgs->mgs_service);
260 GOTO(err_thread, rc);
262 ping_evictor_start();
264 CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
269 ptlrpc_unregister_service(mgs->mgs_service);
271 lproc_mgs_cleanup(obd);
272 obd_llog_finish(obd, 0);
274 /* No extra cleanup needed for llog_init_commit_thread() */
277 ldlm_namespace_free(obd->obd_namespace, NULL, 0);
278 obd->obd_namespace = NULL;
280 fsfilt_put_ops(obd->obd_fsops);
282 server_put_mount(obd->obd_name, mnt);
287 static int mgs_precleanup(struct obd_device *obd, enum obd_cleanup_stage stage)
289 struct mgs_obd *mgs = &obd->u.mgs;
294 case OBD_CLEANUP_EARLY:
296 case OBD_CLEANUP_EXPORTS:
298 ptlrpc_unregister_service(mgs->mgs_service);
299 mgs_cleanup_fsdb_list(obd);
300 rc = obd_llog_finish(obd, 0);
301 lproc_mgs_cleanup(obd);
308 * Performs cleanup procedures for passed \a obd given it is mgs obd.
310 static int mgs_cleanup(struct obd_device *obd)
312 struct mgs_obd *mgs = &obd->u.mgs;
315 if (mgs->mgs_sb == NULL)
320 server_put_mount(obd->obd_name, mgs->mgs_vfsmnt);
323 ldlm_namespace_free(obd->obd_namespace, NULL, 1);
324 obd->obd_namespace = NULL;
326 fsfilt_put_ops(obd->obd_fsops);
328 LCONSOLE_INFO("%s has stopped.\n", obd->obd_name);
332 static int mgs_completion_ast_config(struct ldlm_lock *lock, int flags,
337 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
338 LDLM_FL_BLOCK_CONV))) {
339 struct fs_db *fsdb = (struct fs_db *)lock->l_ast_data;
340 struct lustre_handle lockh;
342 /* clear the bit before lock put */
343 cfs_clear_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags);
345 ldlm_lock2handle(lock, &lockh);
346 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
349 RETURN(ldlm_completion_ast(lock, flags, cbdata));
352 static int mgs_completion_ast_ir(struct ldlm_lock *lock, int flags,
357 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
358 LDLM_FL_BLOCK_CONV))) {
361 /* l_ast_data is used as a marker to avoid cancel ldlm lock
362 * twice. See LU-1259. */
363 lock_res_and_lock(lock);
364 fsdb = (struct fs_db *)lock->l_ast_data;
365 lock->l_ast_data = NULL;
366 unlock_res_and_lock(lock);
369 struct lustre_handle lockh;
371 mgs_ir_notify_complete(fsdb);
373 ldlm_lock2handle(lock, &lockh);
374 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
378 RETURN(ldlm_completion_ast(lock, flags, cbdata));
381 void mgs_revoke_lock(struct obd_device *obd, struct fs_db *fsdb, int type)
383 ldlm_completion_callback cp = NULL;
384 struct lustre_handle lockh = { 0 };
385 struct ldlm_res_id res_id;
386 int flags = LDLM_FL_ATOMIC_CB;
390 LASSERT(fsdb->fsdb_name[0] != '\0');
391 rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
395 case CONFIG_T_CONFIG:
396 cp = mgs_completion_ast_config;
397 if (cfs_test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
400 case CONFIG_T_RECOVER:
401 cp = mgs_completion_ast_ir;
408 rc = ldlm_cli_enqueue_local(obd->obd_namespace, &res_id,
409 LDLM_PLAIN, NULL, LCK_EX, &flags,
410 ldlm_blocking_ast, cp, NULL,
411 fsdb, 0, NULL, &lockh);
412 if (rc != ELDLM_OK) {
413 CERROR("can't take cfg lock for "LPX64"/"LPX64"(%d)\n",
414 le64_to_cpu(res_id.name[0]),
415 le64_to_cpu(res_id.name[1]), rc);
417 if (type == CONFIG_T_CONFIG)
418 cfs_clear_bit(FSDB_REVOKING_LOCK,
421 /* lock has been cancelled in completion_ast. */
430 static int mgs_check_target(struct obd_device *obd, struct mgs_target_info *mti)
435 rc = mgs_check_index(obd, mti);
437 LCONSOLE_ERROR_MSG(0x13b, "%s claims to have registered, but "
438 "this MGS does not know about it, preventing "
439 "registration.\n", mti->mti_svname);
441 } else if (rc == -1) {
442 LCONSOLE_ERROR_MSG(0x13c, "Client log %s-client has "
443 "disappeared! Regenerating all logs.\n",
445 mti->mti_flags |= LDD_F_WRITECONF;
448 /* Index is correctly marked as used */
450 /* If the logs don't contain the mti_nids then add
451 them as failover nids */
452 rc = mgs_check_failnid(obd, mti);
458 /* Ensure this is not a failover node that is connecting first*/
459 static int mgs_check_failover_reg(struct mgs_target_info *mti)
465 ptr = mti->mti_params;
466 while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
467 while (class_parse_nid(ptr, &nid, &ptr) == 0) {
468 for (i = 0; i < mti->mti_nid_count; i++) {
469 if (nid == mti->mti_nids[i]) {
470 LCONSOLE_WARN("Denying initial registra"
471 "tion attempt from nid %s"
472 ", specified as failover"
473 "\n",libcfs_nid2str(nid));
474 return -EADDRNOTAVAIL;
482 /* Called whenever a target starts up. Flags indicate first connect, etc. */
483 static int mgs_handle_target_reg(struct ptlrpc_request *req)
485 struct obd_device *obd = req->rq_export->exp_obd;
486 struct mgs_target_info *mti, *rep_mti;
492 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_REG);
494 mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
496 opc = mti->mti_flags & LDD_F_OPC_MASK;
497 if (opc == LDD_F_OPC_READY) {
498 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
499 mti->mti_fsname, mti->mti_stripe_index);
500 rc = mgs_ir_update(obd, mti);
502 LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
503 CERROR("Update IR return with %d(ignore and IR "
506 GOTO(out_nolock, rc);
509 /* Do not support unregistering right now. */
510 if (opc != LDD_F_OPC_REG)
511 GOTO(out_nolock, rc = -EINVAL);
513 CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
514 mti->mti_fsname, mti->mti_stripe_index);
516 if (mti->mti_flags & LDD_F_NEED_INDEX)
517 mti->mti_flags |= LDD_F_WRITECONF;
519 if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPGRADE14 |
521 /* We're just here as a startup ping. */
522 CDEBUG(D_MGS, "Server %s is running on %s\n",
523 mti->mti_svname, obd_export_nid2str(req->rq_export));
524 rc = mgs_check_target(obd, mti);
525 /* above will set appropriate mti flags */
527 /* Nothing wrong, or fatal error */
528 GOTO(out_nolock, rc);
530 if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)
531 && (rc = mgs_check_failover_reg(mti)))
532 GOTO(out_nolock, rc);
535 OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
537 if (mti->mti_flags & LDD_F_WRITECONF) {
538 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
539 mti->mti_stripe_index == 0) {
540 rc = mgs_erase_logs(obd, mti->mti_fsname);
541 LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
542 "request. All servers must be restarted "
543 "in order to regenerate the logs."
544 "\n", obd->obd_name, mti->mti_fsname);
545 } else if (mti->mti_flags &
546 (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
547 rc = mgs_erase_log(obd, mti->mti_svname);
548 LCONSOLE_WARN("%s: Regenerating %s log by user "
550 obd->obd_name, mti->mti_svname);
552 mti->mti_flags |= LDD_F_UPDATE;
553 /* Erased logs means start from scratch. */
554 mti->mti_flags &= ~LDD_F_UPGRADE14;
557 rc = mgs_find_or_make_fsdb(obd, mti->mti_fsname, &fsdb);
559 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
560 GOTO(out_nolock, rc);
564 * Log writing contention is handled by the fsdb_mutex.
566 * It should be alright if someone was reading while we were
567 * updating the logs - if we revoke at the end they will just update
568 * from where they left off.
572 if (mti->mti_flags & LDD_F_UPGRADE14) {
573 rc = mgs_upgrade_sv_14(obd, mti, fsdb);
575 CERROR("Can't upgrade from 1.4 (%d)\n", rc);
579 /* We're good to go */
580 mti->mti_flags |= LDD_F_UPDATE;
584 if (mti->mti_flags & LDD_F_UPDATE) {
585 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
586 mti->mti_stripe_index);
588 /* create or update the target log
589 and update the client/mdt logs */
590 rc = mgs_write_log_target(obd, mti, fsdb);
592 CERROR("Failed to write %s log (%d)\n",
593 mti->mti_svname, rc);
597 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
598 LDD_F_NEED_INDEX | LDD_F_WRITECONF |
600 mti->mti_flags |= LDD_F_REWRITE_LDD;
604 mgs_revoke_lock(obd, fsdb, CONFIG_T_CONFIG);
607 CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
608 mti->mti_stripe_index, rc);
611 /* we need an error flag to tell the target what's going on,
612 * instead of just doing it by error code only. */
613 mti->mti_flags |= LDD_F_ERROR;
615 rc = req_capsule_server_pack(&req->rq_pill);
619 /* send back the whole mti in the reply */
620 rep_mti = req_capsule_server_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
623 /* Flush logs to disk */
624 fsfilt_sync(obd, obd->u.mgs.mgs_sb);
628 static int mgs_set_info_rpc(struct ptlrpc_request *req)
630 struct obd_device *obd = req->rq_export->exp_obd;
631 struct mgs_send_param *msp, *rep_msp;
633 struct lustre_cfg_bufs bufs;
634 struct lustre_cfg *lcfg;
635 char fsname[MTI_NAME_MAXLEN];
638 msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
641 /* Construct lustre_cfg structure to pass to function mgs_setparam */
642 lustre_cfg_bufs_reset(&bufs, NULL);
643 lustre_cfg_bufs_set_string(&bufs, 1, msp->mgs_param);
644 lcfg = lustre_cfg_new(LCFG_PARAM, &bufs);
645 rc = mgs_setparam(obd, lcfg, fsname);
647 CERROR("Error %d in setting the parameter %s for fs %s\n",
648 rc, msp->mgs_param, fsname);
652 lustre_cfg_free(lcfg);
654 rc = req_capsule_server_pack(&req->rq_pill);
656 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
662 static int mgs_config_read(struct ptlrpc_request *req)
664 struct mgs_config_body *body;
668 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
672 switch (body->mcb_type) {
673 case CONFIG_T_RECOVER:
674 rc = mgs_get_ir_logs(req);
677 case CONFIG_T_CONFIG:
690 * similar as in ost_connect_check_sptlrpc()
692 static int mgs_connect_check_sptlrpc(struct ptlrpc_request *req)
694 struct obd_export *exp = req->rq_export;
695 struct obd_device *obd = exp->exp_obd;
697 struct sptlrpc_flavor flvr;
700 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
701 rc = mgs_find_or_make_fsdb(obd, MGSSELF_NAME, &fsdb);
705 cfs_mutex_lock(&fsdb->fsdb_mutex);
706 if (sptlrpc_rule_set_choose(&fsdb->fsdb_srpc_gen,
707 LUSTRE_SP_MGC, LUSTRE_SP_MGS,
710 /* by defualt allow any flavors */
711 flvr.sf_rpc = SPTLRPC_FLVR_ANY;
713 cfs_mutex_unlock(&fsdb->fsdb_mutex);
715 cfs_spin_lock(&exp->exp_lock);
717 exp->exp_sp_peer = req->rq_sp_from;
718 exp->exp_flvr = flvr;
720 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
721 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
722 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
723 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
724 libcfs_nid2str(req->rq_peer.nid));
728 cfs_spin_unlock(&exp->exp_lock);
730 if (exp->exp_sp_peer != req->rq_sp_from) {
731 CERROR("RPC source %s doesn't match %s\n",
732 sptlrpc_part2name(req->rq_sp_from),
733 sptlrpc_part2name(exp->exp_sp_peer));
736 rc = sptlrpc_target_export_check(exp, req);
743 /* Called whenever a target cleans up. */
744 /* XXX - Currently unused */
745 static int mgs_handle_target_del(struct ptlrpc_request *req)
748 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_DEL);
752 /* XXX - Currently unused */
753 static int mgs_handle_exception(struct ptlrpc_request *req)
756 mgs_counter_incr(req->rq_export, LPROC_MGS_EXCEPTION);
761 * For old clients there is no direct way of knowing which filesystems
762 * a client is operating at the MGS side. But we need to pick up those
763 * clients so that the MGS can mark the corresponding filesystem as
764 * non-IR capable because old clients are not ready to be notified.
766 * This is why we have this _hack_ function. We detect the filesystem's
767 * name by hacking llog operation which is currently used by the clients
768 * to fetch configuration logs. At present this is fine because this is
769 * the ONLY llog operation between mgc and the MGS.
771 * If extra llog operation is going to be added, this function needs fixing.
773 * If releases prior than 2.0 are not supported, we can remove this function.
775 static int mgs_handle_fslog_hack(struct ptlrpc_request *req)
782 /* XXX: We suppose that llog at mgs is only used for
783 * fetching file system log */
784 logname = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
785 if (logname == NULL) {
786 CERROR("No logname, is llog on MGS used for something else?\n");
790 ptr = strchr(logname, '-');
791 rc = (int)(ptr - logname);
792 if (ptr == NULL || rc >= sizeof(fsname)) {
793 CERROR("Invalid logname received: %s\n", logname);
797 strncpy(fsname, logname, rc);
799 rc = mgs_fsc_attach(req->rq_export, fsname);
800 if (rc < 0 && rc != -EEXIST)
801 CERROR("add fs client %s returns %d\n", fsname, rc);
806 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
807 int mgs_handle(struct ptlrpc_request *req)
809 int fail = OBD_FAIL_MGS_ALL_REPLY_NET;
813 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
814 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_MGS_PAUSE_REQ, cfs_fail_val);
815 if (CFS_FAIL_CHECK(OBD_FAIL_MGS_ALL_REQUEST_NET))
818 LASSERT(current->journal_info == NULL);
819 opc = lustre_msg_get_opc(req->rq_reqmsg);
821 if (opc == SEC_CTX_INIT ||
822 opc == SEC_CTX_INIT_CONT ||
826 if (opc != MGS_CONNECT) {
827 if (!class_connected_export(req->rq_export)) {
828 DEBUG_REQ(D_MGS, req, "operation on unconnected MGS\n");
829 req->rq_status = -ENOTCONN;
830 GOTO(out, rc = -ENOTCONN);
836 DEBUG_REQ(D_MGS, req, "connect");
837 /* MGS and MDS have same request format for connect */
838 req_capsule_set(&req->rq_pill, &RQF_MDS_CONNECT);
839 rc = target_handle_connect(req);
841 rc = mgs_connect_check_sptlrpc(req);
843 if (!rc && (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1))
844 /* Make clients trying to reconnect after a MGS restart
845 happy; also requires obd_replayable */
846 lustre_msg_add_op_flags(req->rq_repmsg,
847 MSG_CONNECT_RECONNECT);
850 DEBUG_REQ(D_MGS, req, "disconnect");
851 /* MGS and MDS have same request format for disconnect */
852 req_capsule_set(&req->rq_pill, &RQF_MDS_DISCONNECT);
853 rc = target_handle_disconnect(req);
854 req->rq_status = rc; /* superfluous? */
857 DEBUG_REQ(D_MGS, req, "exception");
858 rc = mgs_handle_exception(req);
861 DEBUG_REQ(D_MGS, req, "target add");
862 req_capsule_set(&req->rq_pill, &RQF_MGS_TARGET_REG);
863 rc = mgs_handle_target_reg(req);
866 DEBUG_REQ(D_MGS, req, "target del");
867 rc = mgs_handle_target_del(req);
870 DEBUG_REQ(D_MGS, req, "set_info");
871 req_capsule_set(&req->rq_pill, &RQF_MGS_SET_INFO);
872 rc = mgs_set_info_rpc(req);
874 case MGS_CONFIG_READ:
875 DEBUG_REQ(D_MGS, req, "read config");
876 req_capsule_set(&req->rq_pill, &RQF_MGS_CONFIG_READ);
877 rc = mgs_config_read(req);
880 DEBUG_REQ(D_MGS, req, "enqueue");
881 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
882 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
883 ldlm_server_blocking_ast, NULL);
885 case LDLM_BL_CALLBACK:
886 case LDLM_CP_CALLBACK:
887 DEBUG_REQ(D_MGS, req, "callback");
888 CERROR("callbacks should not happen on MGS\n");
893 DEBUG_REQ(D_INFO, req, "ping");
894 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
895 rc = target_handle_ping(req);
898 DEBUG_REQ(D_MGS, req, "log cancel");
899 rc = -ENOTSUPP; /* la la la */
902 case LLOG_ORIGIN_HANDLE_CREATE:
903 DEBUG_REQ(D_MGS, req, "llog_init");
904 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
905 rc = llog_origin_handle_create(req);
907 (void)mgs_handle_fslog_hack(req);
909 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
910 DEBUG_REQ(D_MGS, req, "llog next block");
911 req_capsule_set(&req->rq_pill,
912 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
913 rc = llog_origin_handle_next_block(req);
915 case LLOG_ORIGIN_HANDLE_READ_HEADER:
916 DEBUG_REQ(D_MGS, req, "llog read header");
917 req_capsule_set(&req->rq_pill,
918 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
919 rc = llog_origin_handle_read_header(req);
921 case LLOG_ORIGIN_HANDLE_CLOSE:
922 DEBUG_REQ(D_MGS, req, "llog close");
923 rc = llog_origin_handle_close(req);
926 DEBUG_REQ(D_MGS, req, "llog catinfo");
927 req_capsule_set(&req->rq_pill, &RQF_LLOG_CATINFO);
928 rc = llog_catinfo(req);
931 req->rq_status = -ENOTSUPP;
932 rc = ptlrpc_error(req);
936 LASSERT(current->journal_info == NULL);
939 CERROR("MGS handle cmd=%d rc=%d\n", opc, rc);
942 target_send_reply(req, rc, fail);
946 static inline int mgs_init_export(struct obd_export *exp)
948 struct mgs_export_data *data = &exp->u.eu_mgs_data;
950 /* init mgs_export_data for fsc */
951 cfs_spin_lock_init(&data->med_lock);
952 CFS_INIT_LIST_HEAD(&data->med_clients);
954 cfs_spin_lock(&exp->exp_lock);
955 exp->exp_connecting = 1;
956 cfs_spin_unlock(&exp->exp_lock);
958 /* self-export doesn't need client data and ldlm initialization */
959 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
960 &exp->exp_client_uuid)))
962 return ldlm_init_export(exp);
965 static inline int mgs_destroy_export(struct obd_export *exp)
969 target_destroy_export(exp);
970 mgs_client_free(exp);
972 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
973 &exp->exp_client_uuid)))
976 ldlm_destroy_export(exp);
981 static int mgs_extract_fs_pool(char * arg, char *fsname, char *poolname)
986 for (ptr = arg; (*ptr != '\0') && (*ptr != '.'); ptr++ ) {
994 strcpy(poolname, ptr);
999 static int mgs_iocontrol_pool(struct obd_device *obd,
1000 struct obd_ioctl_data *data)
1003 struct lustre_cfg *lcfg = NULL;
1004 struct llog_rec_hdr rec;
1005 char *fsname = NULL;
1006 char *poolname = NULL;
1009 OBD_ALLOC(fsname, MTI_NAME_MAXLEN);
1013 OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
1014 if (poolname == NULL) {
1018 rec.lrh_len = llog_data_len(data->ioc_plen1);
1020 if (data->ioc_type == LUSTRE_CFG_TYPE) {
1021 rec.lrh_type = OBD_CFG_REC;
1023 CERROR("unknown cfg record type:%d \n", data->ioc_type);
1028 if (data->ioc_plen1 > CFS_PAGE_SIZE) {
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_pool, rc = -EFAULT);
1040 if (lcfg->lcfg_bufcount < 2) {
1041 GOTO(out_pool, rc = -EFAULT);
1044 /* first arg is always <fsname>.<poolname> */
1045 mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), fsname,
1048 switch (lcfg->lcfg_command) {
1049 case LCFG_POOL_NEW: {
1050 if (lcfg->lcfg_bufcount != 2)
1052 rc = mgs_pool_cmd(obd, LCFG_POOL_NEW, fsname,
1056 case LCFG_POOL_ADD: {
1057 if (lcfg->lcfg_bufcount != 3)
1059 rc = mgs_pool_cmd(obd, LCFG_POOL_ADD, fsname, poolname,
1060 lustre_cfg_string(lcfg, 2));
1063 case LCFG_POOL_REM: {
1064 if (lcfg->lcfg_bufcount != 3)
1066 rc = mgs_pool_cmd(obd, LCFG_POOL_REM, fsname, poolname,
1067 lustre_cfg_string(lcfg, 2));
1070 case LCFG_POOL_DEL: {
1071 if (lcfg->lcfg_bufcount != 2)
1073 rc = mgs_pool_cmd(obd, LCFG_POOL_DEL, fsname,
1084 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
1085 rc, lcfg->lcfg_command, fsname, poolname);
1091 OBD_FREE(lcfg, data->ioc_plen1);
1094 OBD_FREE(fsname, MTI_NAME_MAXLEN);
1096 if (poolname != NULL)
1097 OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
1102 /* from mdt_iocontrol */
1103 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
1104 void *karg, void *uarg)
1106 struct obd_device *obd = exp->exp_obd;
1107 struct obd_ioctl_data *data = karg;
1108 struct lvfs_run_ctxt saved;
1112 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
1116 case OBD_IOC_PARAM: {
1117 struct lustre_cfg *lcfg;
1118 struct llog_rec_hdr rec;
1119 char fsname[MTI_NAME_MAXLEN];
1121 rec.lrh_len = llog_data_len(data->ioc_plen1);
1123 if (data->ioc_type == LUSTRE_CFG_TYPE) {
1124 rec.lrh_type = OBD_CFG_REC;
1126 CERROR("unknown cfg record type:%d \n", data->ioc_type);
1130 OBD_ALLOC(lcfg, data->ioc_plen1);
1133 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
1134 GOTO(out_free, rc = -EFAULT);
1136 if (lcfg->lcfg_bufcount < 1)
1137 GOTO(out_free, rc = -EINVAL);
1139 rc = mgs_setparam(obd, lcfg, fsname);
1141 CERROR("setparam err %d\n", rc);
1145 OBD_FREE(lcfg, data->ioc_plen1);
1149 case OBD_IOC_POOL: {
1150 RETURN(mgs_iocontrol_pool(obd, data));
1153 case OBD_IOC_DUMP_LOG: {
1154 struct llog_ctxt *ctxt;
1155 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1156 push_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1157 rc = class_config_dump_llog(ctxt, data->ioc_inlbuf1, NULL);
1158 pop_ctxt(&saved, &obd->obd_lvfs_ctxt, NULL);
1159 llog_ctxt_put(ctxt);
1164 case OBD_IOC_LLOG_CHECK:
1165 case OBD_IOC_LLOG_INFO:
1166 case OBD_IOC_LLOG_PRINT: {
1167 struct llog_ctxt *ctxt;
1168 ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
1170 push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
1171 rc = llog_ioctl(ctxt, cmd, data);
1172 pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
1173 llog_ctxt_put(ctxt);
1179 CDEBUG(D_INFO, "unknown command %x\n", cmd);
1185 /* use obd ops to offer management infrastructure */
1186 static struct obd_ops mgs_obd_ops = {
1187 .o_owner = THIS_MODULE,
1188 .o_connect = mgs_connect,
1189 .o_reconnect = mgs_reconnect,
1190 .o_disconnect = mgs_disconnect,
1191 .o_setup = mgs_setup,
1192 .o_precleanup = mgs_precleanup,
1193 .o_cleanup = mgs_cleanup,
1194 .o_init_export = mgs_init_export,
1195 .o_destroy_export = mgs_destroy_export,
1196 .o_iocontrol = mgs_iocontrol,
1197 .o_llog_init = mgs_llog_init,
1198 .o_llog_finish = mgs_llog_finish
1201 static int __init mgs_init(void)
1203 struct lprocfs_static_vars lvars;
1205 lprocfs_mgs_init_vars(&lvars);
1206 class_register_type(&mgs_obd_ops, NULL,
1207 lvars.module_vars, LUSTRE_MGS_NAME, NULL);
1212 static void /*__exit*/ mgs_exit(void)
1214 class_unregister_type(LUSTRE_MGS_NAME);
1217 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1218 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
1219 MODULE_LICENSE("GPL");
1221 module_init(mgs_init);
1222 module_exit(mgs_exit);