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) 2010, 2012, Intel Corporation.
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>
39 * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40 * Author: Mikhail Pershin <tappro@whamcloud.com>
43 #define DEBUG_SUBSYSTEM S_MGS
44 #define D_MGS D_CONFIG
46 #include <obd_class.h>
47 #include <lprocfs_status.h>
48 #include <lustre_param.h>
50 #include "mgs_internal.h"
52 /* Establish a connection to the MGS.*/
53 static int mgs_connect(const struct lu_env *env,
54 struct obd_export **exp, struct obd_device *obd,
55 struct obd_uuid *cluuid, struct obd_connect_data *data,
58 struct obd_export *lexp;
59 struct lustre_handle conn = { 0 };
63 if (!exp || !obd || !cluuid)
66 rc = class_connect(&conn, obd, cluuid);
70 lexp = class_conn2export(&conn);
72 GOTO(out, rc = -EFAULT);
74 mgs_counter_incr(lexp, LPROC_MGS_CONNECT);
77 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
78 lexp->exp_connect_flags = data->ocd_connect_flags;
79 data->ocd_version = LUSTRE_VERSION_CODE;
82 rc = mgs_export_stats_init(obd, lexp, localdata);
86 class_disconnect(lexp);
94 static int mgs_reconnect(const struct lu_env *env,
95 struct obd_export *exp, struct obd_device *obd,
96 struct obd_uuid *cluuid, struct obd_connect_data *data,
101 if (exp == NULL || obd == NULL || cluuid == NULL)
104 mgs_counter_incr(exp, LPROC_MGS_CONNECT);
107 data->ocd_connect_flags &= MGS_CONNECT_SUPPORTED;
108 exp->exp_connect_flags = data->ocd_connect_flags;
109 data->ocd_version = LUSTRE_VERSION_CODE;
112 RETURN(mgs_export_stats_init(obd, exp, localdata));
115 static int mgs_disconnect(struct obd_export *exp)
122 mgs_fsc_cleanup(exp);
124 class_export_get(exp);
125 mgs_counter_incr(exp, LPROC_MGS_DISCONNECT);
127 rc = server_disconnect_export(exp);
128 class_export_put(exp);
132 static int mgs_handle(struct ptlrpc_request *req);
134 static int mgs_completion_ast_config(struct ldlm_lock *lock, __u64 flags,
139 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
140 LDLM_FL_BLOCK_CONV))) {
143 /* l_ast_data is used as a marker to avoid cancel ldlm lock
144 * twice. See LU-2317. */
145 lock_res_and_lock(lock);
146 fsdb = (struct fs_db *)lock->l_ast_data;
147 lock->l_ast_data = NULL;
148 unlock_res_and_lock(lock);
151 struct lustre_handle lockh;
153 /* clear the bit before lock put */
154 clear_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags);
156 ldlm_lock2handle(lock, &lockh);
157 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
161 RETURN(ldlm_completion_ast(lock, flags, cbdata));
164 static int mgs_completion_ast_ir(struct ldlm_lock *lock, __u64 flags,
169 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
170 LDLM_FL_BLOCK_CONV))) {
173 /* l_ast_data is used as a marker to avoid cancel ldlm lock
174 * twice. See LU-1259. */
175 lock_res_and_lock(lock);
176 fsdb = (struct fs_db *)lock->l_ast_data;
177 lock->l_ast_data = NULL;
178 unlock_res_and_lock(lock);
181 struct lustre_handle lockh;
183 mgs_ir_notify_complete(fsdb);
185 ldlm_lock2handle(lock, &lockh);
186 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
190 RETURN(ldlm_completion_ast(lock, flags, cbdata));
193 void mgs_revoke_lock(struct mgs_device *mgs, struct fs_db *fsdb, int type)
195 ldlm_completion_callback cp = NULL;
196 struct lustre_handle lockh = { 0 };
197 struct ldlm_res_id res_id;
198 __u64 flags = LDLM_FL_ATOMIC_CB;
202 LASSERT(fsdb->fsdb_name[0] != '\0');
203 rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
207 case CONFIG_T_CONFIG:
208 cp = mgs_completion_ast_config;
209 if (test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
212 case CONFIG_T_RECOVER:
213 cp = mgs_completion_ast_ir;
220 rc = ldlm_cli_enqueue_local(mgs->mgs_obd->obd_namespace,
221 &res_id, LDLM_PLAIN, NULL, LCK_EX,
222 &flags, ldlm_blocking_ast, cp,
223 NULL, fsdb, 0, LVB_T_NONE, NULL,
225 if (rc != ELDLM_OK) {
226 CERROR("can't take cfg lock for "LPX64"/"LPX64"(%d)\n",
227 le64_to_cpu(res_id.name[0]),
228 le64_to_cpu(res_id.name[1]), rc);
230 if (type == CONFIG_T_CONFIG)
231 clear_bit(FSDB_REVOKING_LOCK,
234 /* lock has been cancelled in completion_ast. */
243 static int mgs_check_target(const struct lu_env *env,
244 struct mgs_device *mgs,
245 struct mgs_target_info *mti)
250 rc = mgs_check_index(env, mgs, mti);
252 LCONSOLE_ERROR_MSG(0x13b, "%s claims to have registered, but "
253 "this MGS does not know about it, preventing "
254 "registration.\n", mti->mti_svname);
256 } else if (rc == -1) {
257 LCONSOLE_ERROR_MSG(0x13c, "Client log %s-client has "
258 "disappeared! Regenerating all logs.\n",
260 mti->mti_flags |= LDD_F_WRITECONF;
263 /* Index is correctly marked as used */
265 /* If the logs don't contain the mti_nids then add
266 them as failover nids */
267 rc = mgs_check_failnid(env, mgs, mti);
273 /* Ensure this is not a failover node that is connecting first*/
274 static int mgs_check_failover_reg(struct mgs_target_info *mti)
280 ptr = mti->mti_params;
281 while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
282 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
283 for (i = 0; i < mti->mti_nid_count; i++) {
284 if (nid == mti->mti_nids[i]) {
285 LCONSOLE_WARN("Denying initial registra"
286 "tion attempt from nid %s"
287 ", specified as failover"
288 "\n",libcfs_nid2str(nid));
289 return -EADDRNOTAVAIL;
297 /* Called whenever a target starts up. Flags indicate first connect, etc. */
298 static int mgs_handle_target_reg(struct ptlrpc_request *req)
300 struct obd_device *obd = req->rq_export->exp_obd;
301 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
302 struct lu_env *env = req->rq_svc_thread->t_env;
303 struct mgs_target_info *mti, *rep_mti;
309 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_REG);
311 mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
313 opc = mti->mti_flags & LDD_F_OPC_MASK;
314 if (opc == LDD_F_OPC_READY) {
315 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
316 mti->mti_fsname, mti->mti_stripe_index);
317 rc = mgs_ir_update(env, mgs, mti);
319 LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
320 CERROR("Update IR return with %d(ignore and IR "
323 GOTO(out_nolock, rc);
326 /* Do not support unregistering right now. */
327 if (opc != LDD_F_OPC_REG)
328 GOTO(out_nolock, rc = -EINVAL);
330 CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
331 mti->mti_fsname, mti->mti_stripe_index);
333 if (mti->mti_flags & LDD_F_NEED_INDEX)
334 mti->mti_flags |= LDD_F_WRITECONF;
336 if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPGRADE14 |
338 /* We're just here as a startup ping. */
339 CDEBUG(D_MGS, "Server %s is running on %s\n",
340 mti->mti_svname, obd_export_nid2str(req->rq_export));
341 rc = mgs_check_target(env, mgs, mti);
342 /* above will set appropriate mti flags */
344 /* Nothing wrong, or fatal error */
345 GOTO(out_nolock, rc);
346 } else if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)) {
347 rc = mgs_check_failover_reg(mti);
349 GOTO(out_nolock, rc);
352 OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
354 if (mti->mti_flags & LDD_F_WRITECONF) {
355 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
356 mti->mti_stripe_index == 0) {
357 rc = mgs_erase_logs(env, mgs, mti->mti_fsname);
358 LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
359 "request. All servers must be restarted "
360 "in order to regenerate the logs."
361 "\n", obd->obd_name, mti->mti_fsname);
362 } else if (mti->mti_flags &
363 (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
364 rc = mgs_erase_log(env, mgs, mti->mti_svname);
365 LCONSOLE_WARN("%s: Regenerating %s log by user "
367 obd->obd_name, mti->mti_svname);
369 mti->mti_flags |= LDD_F_UPDATE;
370 /* Erased logs means start from scratch. */
371 mti->mti_flags &= ~LDD_F_UPGRADE14;
373 GOTO(out_nolock, rc);
376 rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
378 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
379 GOTO(out_nolock, rc);
383 * Log writing contention is handled by the fsdb_mutex.
385 * It should be alright if someone was reading while we were
386 * updating the logs - if we revoke at the end they will just update
387 * from where they left off.
390 if (mti->mti_flags & LDD_F_UPGRADE14) {
391 CERROR("Can't upgrade from 1.4 (%d)\n", rc);
395 if (mti->mti_flags & LDD_F_UPDATE) {
396 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
397 mti->mti_stripe_index);
399 /* create or update the target log
400 and update the client/mdt logs */
401 rc = mgs_write_log_target(env, mgs, mti, fsdb);
403 CERROR("Failed to write %s log (%d)\n",
404 mti->mti_svname, rc);
408 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
409 LDD_F_NEED_INDEX | LDD_F_WRITECONF |
411 mti->mti_flags |= LDD_F_REWRITE_LDD;
415 mgs_revoke_lock(mgs, fsdb, CONFIG_T_CONFIG);
418 CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
419 mti->mti_stripe_index, rc);
422 /* we need an error flag to tell the target what's going on,
423 * instead of just doing it by error code only. */
424 mti->mti_flags |= LDD_F_ERROR;
426 rc = req_capsule_server_pack(&req->rq_pill);
430 /* send back the whole mti in the reply */
431 rep_mti = req_capsule_server_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
434 /* Flush logs to disk */
435 dt_sync(req->rq_svc_thread->t_env, mgs->mgs_bottom);
439 static int mgs_set_info_rpc(struct ptlrpc_request *req)
441 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
442 struct lu_env *env = req->rq_svc_thread->t_env;
443 struct mgs_send_param *msp, *rep_msp;
444 struct mgs_thread_info *mgi = mgs_env_info(env);
446 struct lustre_cfg *lcfg;
449 msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
452 /* Construct lustre_cfg structure to pass to function mgs_setparam */
453 lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
454 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, msp->mgs_param);
455 lcfg = lustre_cfg_new(LCFG_PARAM, &mgi->mgi_bufs);
457 GOTO(out, rc = PTR_ERR(lcfg));
458 rc = mgs_setparam(env, mgs, lcfg, mgi->mgi_fsname);
460 CERROR("Error %d in setting the parameter %s for fs %s\n",
461 rc, msp->mgs_param, mgi->mgi_fsname);
465 rc = req_capsule_server_pack(&req->rq_pill);
467 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
471 lustre_cfg_free(lcfg);
476 static int mgs_config_read(struct ptlrpc_request *req)
478 struct mgs_config_body *body;
482 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
486 switch (body->mcb_type) {
487 case CONFIG_T_RECOVER:
488 rc = mgs_get_ir_logs(req);
491 case CONFIG_T_CONFIG:
504 * similar as in ost_connect_check_sptlrpc()
506 static int mgs_connect_check_sptlrpc(struct ptlrpc_request *req)
508 struct obd_export *exp = req->rq_export;
509 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
510 struct lu_env *env = req->rq_svc_thread->t_env;
512 struct sptlrpc_flavor flvr;
515 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
516 rc = mgs_find_or_make_fsdb(env, mgs, MGSSELF_NAME, &fsdb);
520 mutex_lock(&fsdb->fsdb_mutex);
521 if (sptlrpc_rule_set_choose(&fsdb->fsdb_srpc_gen,
522 LUSTRE_SP_MGC, LUSTRE_SP_MGS,
525 /* by defualt allow any flavors */
526 flvr.sf_rpc = SPTLRPC_FLVR_ANY;
528 mutex_unlock(&fsdb->fsdb_mutex);
530 spin_lock(&exp->exp_lock);
532 exp->exp_sp_peer = req->rq_sp_from;
533 exp->exp_flvr = flvr;
535 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
536 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
537 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
538 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
539 libcfs_nid2str(req->rq_peer.nid));
543 spin_unlock(&exp->exp_lock);
545 if (exp->exp_sp_peer != req->rq_sp_from) {
546 CERROR("RPC source %s doesn't match %s\n",
547 sptlrpc_part2name(req->rq_sp_from),
548 sptlrpc_part2name(exp->exp_sp_peer));
551 rc = sptlrpc_target_export_check(exp, req);
558 /* Called whenever a target cleans up. */
559 /* XXX - Currently unused */
560 static int mgs_handle_target_del(struct ptlrpc_request *req)
563 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_DEL);
567 /* XXX - Currently unused */
568 static int mgs_handle_exception(struct ptlrpc_request *req)
571 mgs_counter_incr(req->rq_export, LPROC_MGS_EXCEPTION);
576 * For old clients there is no direct way of knowing which filesystems
577 * a client is operating at the MGS side. But we need to pick up those
578 * clients so that the MGS can mark the corresponding filesystem as
579 * non-IR capable because old clients are not ready to be notified.
581 * This is why we have this _hack_ function. We detect the filesystem's
582 * name by hacking llog operation which is currently used by the clients
583 * to fetch configuration logs. At present this is fine because this is
584 * the ONLY llog operation between mgc and the MGS.
586 * If extra llog operation is going to be added, this function needs fixing.
588 * If releases prior than 2.0 are not supported, we can remove this function.
590 static int mgs_handle_fslog_hack(struct ptlrpc_request *req)
597 /* XXX: We suppose that llog at mgs is only used for
598 * fetching file system log */
599 logname = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
600 if (logname == NULL) {
601 CERROR("No logname, is llog on MGS used for something else?\n");
605 ptr = strchr(logname, '-');
606 rc = (int)(ptr - logname);
607 if (ptr == NULL || rc >= sizeof(fsname)) {
608 CERROR("Invalid logname received: %s\n", logname);
612 strncpy(fsname, logname, rc);
614 rc = mgs_fsc_attach(req->rq_svc_thread->t_env, req->rq_export, fsname);
615 if (rc < 0 && rc != -EEXIST)
616 CERROR("add fs client %s returns %d\n", fsname, rc);
621 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
622 int mgs_handle(struct ptlrpc_request *req)
624 int fail = OBD_FAIL_MGS_ALL_REPLY_NET;
628 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
629 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_MGS_PAUSE_REQ, cfs_fail_val);
630 if (CFS_FAIL_CHECK(OBD_FAIL_MGS_ALL_REQUEST_NET))
633 LASSERT(current->journal_info == NULL);
634 opc = lustre_msg_get_opc(req->rq_reqmsg);
636 if (opc == SEC_CTX_INIT ||
637 opc == SEC_CTX_INIT_CONT ||
641 if (opc != MGS_CONNECT) {
642 if (!class_connected_export(req->rq_export)) {
643 DEBUG_REQ(D_MGS, req, "operation on unconnected MGS\n");
644 req->rq_status = -ENOTCONN;
645 GOTO(out, rc = -ENOTCONN);
651 DEBUG_REQ(D_MGS, req, "connect");
652 /* MGS and MDS have same request format for connect */
653 req_capsule_set(&req->rq_pill, &RQF_MDS_CONNECT);
654 rc = target_handle_connect(req);
656 rc = mgs_connect_check_sptlrpc(req);
658 if (!rc && (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1))
659 /* Make clients trying to reconnect after a MGS restart
660 happy; also requires obd_replayable */
661 lustre_msg_add_op_flags(req->rq_repmsg,
662 MSG_CONNECT_RECONNECT);
665 DEBUG_REQ(D_MGS, req, "disconnect");
666 /* MGS and MDS have same request format for disconnect */
667 req_capsule_set(&req->rq_pill, &RQF_MDS_DISCONNECT);
668 rc = target_handle_disconnect(req);
669 req->rq_status = rc; /* superfluous? */
672 DEBUG_REQ(D_MGS, req, "exception");
673 rc = mgs_handle_exception(req);
676 DEBUG_REQ(D_MGS, req, "target add");
677 req_capsule_set(&req->rq_pill, &RQF_MGS_TARGET_REG);
678 rc = mgs_handle_target_reg(req);
681 DEBUG_REQ(D_MGS, req, "target del");
682 rc = mgs_handle_target_del(req);
685 DEBUG_REQ(D_MGS, req, "set_info");
686 req_capsule_set(&req->rq_pill, &RQF_MGS_SET_INFO);
687 rc = mgs_set_info_rpc(req);
689 case MGS_CONFIG_READ:
690 DEBUG_REQ(D_MGS, req, "read config");
691 req_capsule_set(&req->rq_pill, &RQF_MGS_CONFIG_READ);
692 rc = mgs_config_read(req);
695 DEBUG_REQ(D_MGS, req, "enqueue");
696 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
697 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
698 ldlm_server_blocking_ast, NULL);
700 case LDLM_BL_CALLBACK:
701 case LDLM_CP_CALLBACK:
702 DEBUG_REQ(D_MGS, req, "callback");
703 CERROR("callbacks should not happen on MGS\n");
708 DEBUG_REQ(D_INFO, req, "ping");
709 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
710 rc = target_handle_ping(req);
713 DEBUG_REQ(D_MGS, req, "log cancel");
714 rc = -ENOTSUPP; /* la la la */
717 case LLOG_ORIGIN_HANDLE_CREATE:
718 DEBUG_REQ(D_MGS, req, "llog_open");
719 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
720 rc = llog_origin_handle_open(req);
722 (void)mgs_handle_fslog_hack(req);
724 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
725 DEBUG_REQ(D_MGS, req, "llog next block");
726 req_capsule_set(&req->rq_pill,
727 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
728 rc = llog_origin_handle_next_block(req);
730 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
731 DEBUG_REQ(D_MGS, req, "llog prev block");
732 req_capsule_set(&req->rq_pill,
733 &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
734 rc = llog_origin_handle_prev_block(req);
736 case LLOG_ORIGIN_HANDLE_READ_HEADER:
737 DEBUG_REQ(D_MGS, req, "llog read header");
738 req_capsule_set(&req->rq_pill,
739 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
740 rc = llog_origin_handle_read_header(req);
742 case LLOG_ORIGIN_HANDLE_CLOSE:
743 DEBUG_REQ(D_MGS, req, "llog close");
744 rc = llog_origin_handle_close(req);
750 LASSERT(current->journal_info == NULL);
752 DEBUG_REQ(D_MGS, req, "MGS fail to handle opc = %d: rc = %d\n",
755 rc = ptlrpc_error(req);
759 target_send_reply(req, rc, fail);
763 static inline int mgs_init_export(struct obd_export *exp)
765 struct mgs_export_data *data = &exp->u.eu_mgs_data;
767 /* init mgs_export_data for fsc */
768 spin_lock_init(&data->med_lock);
769 CFS_INIT_LIST_HEAD(&data->med_clients);
771 spin_lock(&exp->exp_lock);
772 exp->exp_connecting = 1;
773 spin_unlock(&exp->exp_lock);
775 /* self-export doesn't need client data and ldlm initialization */
776 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
777 &exp->exp_client_uuid)))
779 return ldlm_init_export(exp);
782 static inline int mgs_destroy_export(struct obd_export *exp)
786 target_destroy_export(exp);
787 mgs_client_free(exp);
789 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
790 &exp->exp_client_uuid)))
793 ldlm_destroy_export(exp);
798 static int mgs_extract_fs_pool(char * arg, char *fsname, char *poolname)
803 for (ptr = arg; (*ptr != '\0') && (*ptr != '.'); ptr++ ) {
811 strcpy(poolname, ptr);
816 static int mgs_iocontrol_pool(const struct lu_env *env,
817 struct mgs_device *mgs,
818 struct obd_ioctl_data *data)
820 struct mgs_thread_info *mgi = mgs_env_info(env);
822 struct lustre_cfg *lcfg = NULL;
823 char *poolname = NULL;
826 OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
827 if (poolname == NULL)
830 if (data->ioc_type != LUSTRE_CFG_TYPE) {
831 CERROR("%s: unknown cfg record type: %d\n",
832 mgs->mgs_obd->obd_name, data->ioc_type);
833 GOTO(out_pool, rc = -EINVAL);
836 if (data->ioc_plen1 > CFS_PAGE_SIZE)
837 GOTO(out_pool, rc = -E2BIG);
839 OBD_ALLOC(lcfg, data->ioc_plen1);
841 GOTO(out_pool, rc = -ENOMEM);
843 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
844 GOTO(out_lcfg, rc = -EFAULT);
846 if (lcfg->lcfg_bufcount < 2)
847 GOTO(out_lcfg, rc = -EFAULT);
849 /* first arg is always <fsname>.<poolname> */
850 rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
855 switch (lcfg->lcfg_command) {
857 if (lcfg->lcfg_bufcount != 2)
858 GOTO(out_lcfg, rc = -EINVAL);
859 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
863 if (lcfg->lcfg_bufcount != 3)
864 GOTO(out_lcfg, rc = -EINVAL);
865 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
866 poolname, lustre_cfg_string(lcfg, 2));
869 if (lcfg->lcfg_bufcount != 3)
870 GOTO(out_lcfg, rc = -EINVAL);
871 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
872 poolname, lustre_cfg_string(lcfg, 2));
875 if (lcfg->lcfg_bufcount != 2)
876 GOTO(out_lcfg, rc = -EINVAL);
877 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
885 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
886 rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
891 OBD_FREE(lcfg, data->ioc_plen1);
893 OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
897 /* from mdt_iocontrol */
898 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
899 void *karg, void *uarg)
901 struct mgs_device *mgs = exp2mgs_dev(exp);
902 struct obd_ioctl_data *data = karg;
907 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
909 rc = lu_env_init(&env, LCT_MG_THREAD);
915 case OBD_IOC_PARAM: {
916 struct mgs_thread_info *mgi = mgs_env_info(&env);
917 struct lustre_cfg *lcfg;
919 if (data->ioc_type != LUSTRE_CFG_TYPE) {
920 CERROR("%s: unknown cfg record type: %d\n",
921 mgs->mgs_obd->obd_name, data->ioc_type);
922 GOTO(out, rc = -EINVAL);
925 OBD_ALLOC(lcfg, data->ioc_plen1);
927 GOTO(out, rc = -ENOMEM);
928 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
929 GOTO(out_free, rc = -EFAULT);
931 if (lcfg->lcfg_bufcount < 1)
932 GOTO(out_free, rc = -EINVAL);
934 rc = mgs_setparam(&env, mgs, lcfg, mgi->mgi_fsname);
936 CERROR("%s: setparam err: rc = %d\n",
937 exp->exp_obd->obd_name, rc);
939 OBD_FREE(lcfg, data->ioc_plen1);
943 case OBD_IOC_REPLACE_NIDS: {
944 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
945 CERROR("No device name specified!\n");
949 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
950 CERROR("Device name is not NUL terminated!\n");
954 if (data->ioc_plen1 > MTI_NAME_MAXLEN) {
955 CERROR("Device name is too long\n");
959 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
960 CERROR("No NIDs were specified!\n");
964 if (data->ioc_inlbuf2[data->ioc_inllen2 - 1] != 0) {
965 CERROR("NID list is not NUL terminated!\n");
969 /* replace nids in llog */
970 rc = mgs_replace_nids(&env, mgs, data->ioc_inlbuf1,
973 CERROR("%s: error replacing nids: rc = %d\n",
974 exp->exp_obd->obd_name, rc);
980 rc = mgs_iocontrol_pool(&env, mgs, data);
983 case OBD_IOC_DUMP_LOG: {
984 struct llog_ctxt *ctxt;
986 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
987 rc = class_config_dump_llog(&env, ctxt, data->ioc_inlbuf1,
994 case OBD_IOC_LLOG_CANCEL:
995 case OBD_IOC_LLOG_REMOVE:
996 case OBD_IOC_LLOG_CHECK:
997 case OBD_IOC_LLOG_INFO:
998 case OBD_IOC_LLOG_PRINT: {
999 struct llog_ctxt *ctxt;
1001 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1002 rc = llog_ioctl(&env, ctxt, cmd, data);
1003 llog_ctxt_put(ctxt);
1008 CERROR("%s: unknown command %#x\n",
1009 mgs->mgs_obd->obd_name, cmd);
1018 static int mgs_connect_to_osd(struct mgs_device *m, const char *nextdev)
1020 struct obd_connect_data *data = NULL;
1021 struct obd_device *obd;
1025 OBD_ALLOC_PTR(data);
1029 obd = class_name2obd(nextdev);
1031 CERROR("can't locate next device: %s\n", nextdev);
1032 GOTO(out, rc = -ENOTCONN);
1035 data->ocd_version = LUSTRE_VERSION_CODE;
1037 rc = obd_connect(NULL, &m->mgs_bottom_exp, obd,
1038 &obd->obd_uuid, data, NULL);
1040 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1044 m->mgs_bottom = lu2dt_dev(m->mgs_bottom_exp->exp_obd->obd_lu_dev);
1045 m->mgs_dt_dev.dd_lu_dev.ld_site = m->mgs_bottom->dd_lu_dev.ld_site;
1046 LASSERT(m->mgs_dt_dev.dd_lu_dev.ld_site);
1053 static int mgs_init0(const struct lu_env *env, struct mgs_device *mgs,
1054 struct lu_device_type *ldt, struct lustre_cfg *lcfg)
1056 static struct ptlrpc_service_conf conf;
1057 struct lprocfs_static_vars lvars = { 0 };
1058 struct obd_device *obd;
1059 struct lustre_mount_info *lmi;
1060 struct llog_ctxt *ctxt;
1065 lmi = server_get_mount(lustre_cfg_string(lcfg, 0));
1069 mgs->mgs_dt_dev.dd_lu_dev.ld_ops = &mgs_lu_ops;
1071 rc = mgs_connect_to_osd(mgs, lustre_cfg_string(lcfg, 3));
1075 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1078 mgs->mgs_obd->obd_lu_dev = &mgs->mgs_dt_dev.dd_lu_dev;
1080 obd->u.obt.obt_magic = OBT_MAGIC;
1081 obd->u.obt.obt_instance = 0;
1083 /* namespace for mgs llog */
1084 obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
1085 LDLM_NAMESPACE_SERVER,
1086 LDLM_NAMESPACE_MODEST,
1088 if (obd->obd_namespace == NULL)
1089 GOTO(err_ops, rc = -ENOMEM);
1092 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1093 "mgs_ldlm_client", &obd->obd_ldlm_client);
1095 rc = mgs_fs_setup(env, mgs);
1097 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
1102 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT,
1103 obd, &llog_osd_ops);
1107 /* XXX: we need this trick till N:1 stack is supported
1108 * set "current" directory for named llogs */
1109 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1111 ctxt->loc_dir = mgs->mgs_configs_dir;
1112 llog_ctxt_put(ctxt);
1114 /* No recovery for MGC's */
1115 obd->obd_replayable = 0;
1117 /* Internal mgs setup */
1118 mgs_init_fsdb_list(mgs);
1119 mutex_init(&mgs->mgs_mutex);
1120 mgs->mgs_start_time = cfs_time_current_sec();
1121 spin_lock_init(&mgs->mgs_lock);
1124 lprocfs_mgs_init_vars(&lvars);
1125 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0) {
1126 lproc_mgs_setup(mgs, lustre_cfg_string(lcfg, 3));
1127 rc = lprocfs_alloc_md_stats(obd, LPROC_MGS_LAST);
1132 conf = (typeof(conf)) {
1133 .psc_name = LUSTRE_MGS_NAME,
1134 .psc_watchdog_factor = MGS_SERVICE_WATCHDOG_FACTOR,
1136 .bc_nbufs = MGS_NBUFS,
1137 .bc_buf_size = MGS_BUFSIZE,
1138 .bc_req_max_size = MGS_MAXREQSIZE,
1139 .bc_rep_max_size = MGS_MAXREPSIZE,
1140 .bc_req_portal = MGS_REQUEST_PORTAL,
1141 .bc_rep_portal = MGC_REPLY_PORTAL,
1144 .tc_thr_name = "ll_mgs",
1145 .tc_nthrs_init = MGS_NTHRS_INIT,
1146 .tc_nthrs_max = MGS_NTHRS_MAX,
1147 .tc_ctx_tags = LCT_MG_THREAD,
1150 .so_req_handler = mgs_handle,
1151 .so_req_printer = target_print_req,
1154 /* Start the service threads */
1155 mgs->mgs_service = ptlrpc_register_service(&conf, obd->obd_proc_entry);
1156 if (IS_ERR(mgs->mgs_service)) {
1157 rc = PTR_ERR(mgs->mgs_service);
1158 CERROR("failed to start service: %d\n", rc);
1159 GOTO(err_lproc, rc);
1162 ping_evictor_start();
1164 CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
1166 /* device stack is not yet fully setup to keep no objects behind */
1167 lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1171 lproc_mgs_cleanup(mgs);
1173 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1175 ctxt->loc_dir = NULL;
1176 llog_cleanup(env, ctxt);
1179 /* No extra cleanup needed for llog_init_commit_thread() */
1180 mgs_fs_cleanup(env, mgs);
1182 ldlm_namespace_free(obd->obd_namespace, NULL, 0);
1183 obd->obd_namespace = NULL;
1185 lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1186 if (!cfs_hash_is_empty(mgs2lu_dev(mgs)->ld_site->ls_obj_hash)) {
1187 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1188 lu_site_print(env, mgs2lu_dev(mgs)->ld_site, &msgdata,
1191 obd_disconnect(mgs->mgs_bottom_exp);
1195 static struct lu_device *mgs_device_free(const struct lu_env *env,
1196 struct lu_device *lu)
1198 struct mgs_device *mgs = lu2mgs_dev(lu);
1201 dt_device_fini(&mgs->mgs_dt_dev);
1206 static int mgs_process_config(const struct lu_env *env,
1207 struct lu_device *dev,
1208 struct lustre_cfg *lcfg)
1214 static int mgs_object_init(const struct lu_env *env, struct lu_object *o,
1215 const struct lu_object_conf *unused)
1217 struct mgs_device *d = lu2mgs_dev(o->lo_dev);
1218 struct lu_device *under;
1219 struct lu_object *below;
1223 /* do no set .do_ops as mgs calls to bottom osd directly */
1225 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
1226 PFID(lu_object_fid(o)));
1228 under = &d->mgs_bottom->dd_lu_dev;
1229 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1231 lu_object_add(o, below);
1238 static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
1240 struct mgs_object *obj = lu2mgs_obj(o);
1241 struct lu_object_header *h = o->lo_header;
1243 dt_object_fini(&obj->mgo_obj);
1244 lu_object_header_fini(h);
1248 static int mgs_object_print(const struct lu_env *env, void *cookie,
1249 lu_printer_t p, const struct lu_object *l)
1251 const struct mgs_object *o = lu2mgs_obj((struct lu_object *) l);
1253 return (*p)(env, cookie, LUSTRE_MGS_NAME"-object@%p", o);
1256 struct lu_object_operations mgs_lu_obj_ops = {
1257 .loo_object_init = mgs_object_init,
1258 .loo_object_free = mgs_object_free,
1259 .loo_object_print = mgs_object_print,
1262 struct lu_object *mgs_object_alloc(const struct lu_env *env,
1263 const struct lu_object_header *hdr,
1264 struct lu_device *d)
1266 struct lu_object_header *h;
1267 struct mgs_object *o;
1268 struct lu_object *l;
1270 LASSERT(hdr == NULL);
1274 l = &o->mgo_obj.do_lu;
1277 lu_object_header_init(h);
1278 dt_object_init(&o->mgo_obj, h, d);
1279 lu_object_add_top(h, l);
1281 l->lo_ops = &mgs_lu_obj_ops;
1289 const struct lu_device_operations mgs_lu_ops = {
1290 .ldo_object_alloc = mgs_object_alloc,
1291 .ldo_process_config = mgs_process_config,
1294 static struct lu_device *mgs_device_alloc(const struct lu_env *env,
1295 struct lu_device_type *type,
1296 struct lustre_cfg *lcfg)
1298 struct mgs_device *mgs;
1299 struct lu_device *ludev;
1303 ludev = ERR_PTR(-ENOMEM);
1307 ludev = mgs2lu_dev(mgs);
1308 dt_device_init(&mgs->mgs_dt_dev, type);
1309 rc = mgs_init0(env, mgs, type, lcfg);
1311 mgs_device_free(env, ludev);
1312 ludev = ERR_PTR(rc);
1318 static struct lu_device *mgs_device_fini(const struct lu_env *env,
1319 struct lu_device *d)
1321 struct mgs_device *mgs = lu2mgs_dev(d);
1322 struct obd_device *obd = mgs->mgs_obd;
1323 struct llog_ctxt *ctxt;
1327 LASSERT(mgs->mgs_bottom);
1329 ping_evictor_stop();
1331 ptlrpc_unregister_service(mgs->mgs_service);
1333 obd_exports_barrier(obd);
1334 obd_zombie_barrier();
1336 mgs_cleanup_fsdb_list(mgs);
1337 lproc_mgs_cleanup(mgs);
1339 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1341 ctxt->loc_dir = NULL;
1342 llog_cleanup(env, ctxt);
1345 mgs_fs_cleanup(env, mgs);
1347 ldlm_namespace_free(obd->obd_namespace, NULL, 1);
1348 obd->obd_namespace = NULL;
1350 lu_site_purge(env, d->ld_site, ~0);
1351 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
1352 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1353 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
1356 LASSERT(mgs->mgs_bottom_exp);
1357 obd_disconnect(mgs->mgs_bottom_exp);
1359 server_put_mount(obd->obd_name, NULL);
1364 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1365 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1367 LU_TYPE_INIT_FINI(mgs, &mgs_thread_key);
1369 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1371 static struct lu_device_type_operations mgs_device_type_ops = {
1372 .ldto_init = mgs_type_init,
1373 .ldto_fini = mgs_type_fini,
1375 .ldto_start = mgs_type_start,
1376 .ldto_stop = mgs_type_stop,
1378 .ldto_device_alloc = mgs_device_alloc,
1379 .ldto_device_free = mgs_device_free,
1381 .ldto_device_fini = mgs_device_fini
1384 static struct lu_device_type mgs_device_type = {
1385 .ldt_tags = LU_DEVICE_DT,
1386 .ldt_name = LUSTRE_MGS_NAME,
1387 .ldt_ops = &mgs_device_type_ops,
1388 .ldt_ctx_tags = LCT_MG_THREAD
1392 /* use obd ops to offer management infrastructure */
1393 static struct obd_ops mgs_obd_ops = {
1394 .o_owner = THIS_MODULE,
1395 .o_connect = mgs_connect,
1396 .o_reconnect = mgs_reconnect,
1397 .o_disconnect = mgs_disconnect,
1398 .o_init_export = mgs_init_export,
1399 .o_destroy_export = mgs_destroy_export,
1400 .o_iocontrol = mgs_iocontrol,
1403 static int __init mgs_init(void)
1405 struct lprocfs_static_vars lvars;
1407 lprocfs_mgs_init_vars(&lvars);
1408 class_register_type(&mgs_obd_ops, NULL, lvars.module_vars,
1409 LUSTRE_MGS_NAME, &mgs_device_type);
1414 static void /*__exit*/ mgs_exit(void)
1416 class_unregister_type(LUSTRE_MGS_NAME);
1419 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1420 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
1421 MODULE_LICENSE("GPL");
1423 module_init(mgs_init);
1424 module_exit(mgs_exit);