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, 2013, 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 data->ocd_version = LUSTRE_VERSION_CODE;
79 lexp->exp_connect_data = *data;
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 data->ocd_version = LUSTRE_VERSION_CODE;
109 exp->exp_connect_data = *data;
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 if (OCD_HAS_FLAG(&req->rq_export->exp_connect_data, IMP_RECOV))
314 opc = mti->mti_flags & LDD_F_OPC_MASK;
318 if (opc == LDD_F_OPC_READY) {
319 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
320 mti->mti_fsname, mti->mti_stripe_index);
321 rc = mgs_ir_update(env, mgs, mti);
323 LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
324 CERROR("Update IR return with %d(ignore and IR "
327 GOTO(out_nolock, rc);
330 /* Do not support unregistering right now. */
331 if (opc != LDD_F_OPC_REG)
332 GOTO(out_nolock, rc = -EINVAL);
334 CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
335 mti->mti_fsname, mti->mti_stripe_index);
337 if (mti->mti_flags & LDD_F_NEED_INDEX)
338 mti->mti_flags |= LDD_F_WRITECONF;
340 if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPGRADE14 |
342 /* We're just here as a startup ping. */
343 CDEBUG(D_MGS, "Server %s is running on %s\n",
344 mti->mti_svname, obd_export_nid2str(req->rq_export));
345 rc = mgs_check_target(env, mgs, mti);
346 /* above will set appropriate mti flags */
348 /* Nothing wrong, or fatal error */
349 GOTO(out_nolock, rc);
350 } else if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)) {
351 rc = mgs_check_failover_reg(mti);
353 GOTO(out_nolock, rc);
356 OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
358 if (mti->mti_flags & LDD_F_WRITECONF) {
359 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
360 mti->mti_stripe_index == 0) {
361 rc = mgs_erase_logs(env, mgs, mti->mti_fsname);
362 LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
363 "request. All servers must be restarted "
364 "in order to regenerate the logs."
365 "\n", obd->obd_name, mti->mti_fsname);
366 } else if (mti->mti_flags &
367 (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
368 rc = mgs_erase_log(env, mgs, mti->mti_svname);
369 LCONSOLE_WARN("%s: Regenerating %s log by user "
371 obd->obd_name, mti->mti_svname);
373 mti->mti_flags |= LDD_F_UPDATE;
374 /* Erased logs means start from scratch. */
375 mti->mti_flags &= ~LDD_F_UPGRADE14;
377 GOTO(out_nolock, rc);
380 rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
382 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
383 GOTO(out_nolock, rc);
387 * Log writing contention is handled by the fsdb_mutex.
389 * It should be alright if someone was reading while we were
390 * updating the logs - if we revoke at the end they will just update
391 * from where they left off.
394 if (mti->mti_flags & LDD_F_UPGRADE14) {
395 CERROR("Can't upgrade from 1.4 (%d)\n", rc);
399 if (mti->mti_flags & LDD_F_UPDATE) {
400 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
401 mti->mti_stripe_index);
403 /* create or update the target log
404 and update the client/mdt logs */
405 rc = mgs_write_log_target(env, mgs, mti, fsdb);
407 CERROR("Failed to write %s log (%d)\n",
408 mti->mti_svname, rc);
412 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
413 LDD_F_NEED_INDEX | LDD_F_WRITECONF |
415 mti->mti_flags |= LDD_F_REWRITE_LDD;
419 mgs_revoke_lock(mgs, fsdb, CONFIG_T_CONFIG);
422 CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
423 mti->mti_stripe_index, rc);
426 /* we need an error flag to tell the target what's going on,
427 * instead of just doing it by error code only. */
428 mti->mti_flags |= LDD_F_ERROR;
430 rc = req_capsule_server_pack(&req->rq_pill);
434 /* send back the whole mti in the reply */
435 rep_mti = req_capsule_server_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
438 /* Flush logs to disk */
439 dt_sync(req->rq_svc_thread->t_env, mgs->mgs_bottom);
443 static int mgs_set_info_rpc(struct ptlrpc_request *req)
445 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
446 struct lu_env *env = req->rq_svc_thread->t_env;
447 struct mgs_send_param *msp, *rep_msp;
448 struct mgs_thread_info *mgi = mgs_env_info(env);
450 struct lustre_cfg *lcfg;
453 msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
456 /* Construct lustre_cfg structure to pass to function mgs_setparam */
457 lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
458 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, msp->mgs_param);
459 lcfg = lustre_cfg_new(LCFG_PARAM, &mgi->mgi_bufs);
461 GOTO(out, rc = PTR_ERR(lcfg));
462 rc = mgs_setparam(env, mgs, lcfg, mgi->mgi_fsname);
464 CERROR("Error %d in setting the parameter %s for fs %s\n",
465 rc, msp->mgs_param, mgi->mgi_fsname);
469 rc = req_capsule_server_pack(&req->rq_pill);
471 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
475 lustre_cfg_free(lcfg);
480 static int mgs_config_read(struct ptlrpc_request *req)
482 struct mgs_config_body *body;
486 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
490 switch (body->mcb_type) {
491 case CONFIG_T_RECOVER:
492 rc = mgs_get_ir_logs(req);
495 case CONFIG_T_CONFIG:
508 * similar as in ost_connect_check_sptlrpc()
510 static int mgs_connect_check_sptlrpc(struct ptlrpc_request *req)
512 struct obd_export *exp = req->rq_export;
513 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
514 struct lu_env *env = req->rq_svc_thread->t_env;
516 struct sptlrpc_flavor flvr;
519 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
520 rc = mgs_find_or_make_fsdb(env, mgs, MGSSELF_NAME, &fsdb);
524 mutex_lock(&fsdb->fsdb_mutex);
525 if (sptlrpc_rule_set_choose(&fsdb->fsdb_srpc_gen,
526 LUSTRE_SP_MGC, LUSTRE_SP_MGS,
529 /* by defualt allow any flavors */
530 flvr.sf_rpc = SPTLRPC_FLVR_ANY;
532 mutex_unlock(&fsdb->fsdb_mutex);
534 spin_lock(&exp->exp_lock);
536 exp->exp_sp_peer = req->rq_sp_from;
537 exp->exp_flvr = flvr;
539 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
540 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
541 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
542 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
543 libcfs_nid2str(req->rq_peer.nid));
547 spin_unlock(&exp->exp_lock);
549 if (exp->exp_sp_peer != req->rq_sp_from) {
550 CERROR("RPC source %s doesn't match %s\n",
551 sptlrpc_part2name(req->rq_sp_from),
552 sptlrpc_part2name(exp->exp_sp_peer));
555 rc = sptlrpc_target_export_check(exp, req);
562 /* Called whenever a target cleans up. */
563 /* XXX - Currently unused */
564 static int mgs_handle_target_del(struct ptlrpc_request *req)
567 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_DEL);
571 /* XXX - Currently unused */
572 static int mgs_handle_exception(struct ptlrpc_request *req)
575 mgs_counter_incr(req->rq_export, LPROC_MGS_EXCEPTION);
580 * For old clients there is no direct way of knowing which filesystems
581 * a client is operating at the MGS side. But we need to pick up those
582 * clients so that the MGS can mark the corresponding filesystem as
583 * non-IR capable because old clients are not ready to be notified.
585 * This is why we have this _hack_ function. We detect the filesystem's
586 * name by hacking llog operation which is currently used by the clients
587 * to fetch configuration logs. At present this is fine because this is
588 * the ONLY llog operation between mgc and the MGS.
590 * If extra llog operation is going to be added, this function needs fixing.
592 * If releases prior than 2.0 are not supported, we can remove this function.
594 static int mgs_handle_fslog_hack(struct ptlrpc_request *req)
601 /* XXX: We suppose that llog at mgs is only used for
602 * fetching file system log */
603 logname = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
604 if (logname == NULL) {
605 CERROR("No logname, is llog on MGS used for something else?\n");
609 ptr = strchr(logname, '-');
610 rc = (int)(ptr - logname);
611 if (ptr == NULL || rc >= sizeof(fsname)) {
612 CERROR("Invalid logname received: %s\n", logname);
616 strncpy(fsname, logname, rc);
618 rc = mgs_fsc_attach(req->rq_svc_thread->t_env, req->rq_export, fsname);
619 if (rc < 0 && rc != -EEXIST)
620 CERROR("add fs client %s returns %d\n", fsname, rc);
625 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
626 int mgs_handle(struct ptlrpc_request *req)
628 int fail = OBD_FAIL_MGS_ALL_REPLY_NET;
632 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
633 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_MGS_PAUSE_REQ, cfs_fail_val);
634 if (CFS_FAIL_CHECK(OBD_FAIL_MGS_ALL_REQUEST_NET))
637 LASSERT(current->journal_info == NULL);
638 opc = lustre_msg_get_opc(req->rq_reqmsg);
640 if (opc == SEC_CTX_INIT ||
641 opc == SEC_CTX_INIT_CONT ||
645 if (opc != MGS_CONNECT) {
646 if (!class_connected_export(req->rq_export)) {
647 DEBUG_REQ(D_MGS, req, "operation on unconnected MGS\n");
648 req->rq_status = -ENOTCONN;
649 GOTO(out, rc = -ENOTCONN);
655 DEBUG_REQ(D_MGS, req, "connect");
656 /* MGS and MDS have same request format for connect */
657 req_capsule_set(&req->rq_pill, &RQF_MDS_CONNECT);
658 rc = target_handle_connect(req);
660 rc = mgs_connect_check_sptlrpc(req);
662 if (!rc && (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1))
663 /* Make clients trying to reconnect after a MGS restart
664 happy; also requires obd_replayable */
665 lustre_msg_add_op_flags(req->rq_repmsg,
666 MSG_CONNECT_RECONNECT);
669 DEBUG_REQ(D_MGS, req, "disconnect");
670 /* MGS and MDS have same request format for disconnect */
671 req_capsule_set(&req->rq_pill, &RQF_MDS_DISCONNECT);
672 rc = target_handle_disconnect(req);
673 req->rq_status = rc; /* superfluous? */
676 DEBUG_REQ(D_MGS, req, "exception");
677 rc = mgs_handle_exception(req);
680 DEBUG_REQ(D_MGS, req, "target add");
681 req_capsule_set(&req->rq_pill, &RQF_MGS_TARGET_REG);
682 rc = mgs_handle_target_reg(req);
685 DEBUG_REQ(D_MGS, req, "target del");
686 rc = mgs_handle_target_del(req);
689 DEBUG_REQ(D_MGS, req, "set_info");
690 req_capsule_set(&req->rq_pill, &RQF_MGS_SET_INFO);
691 rc = mgs_set_info_rpc(req);
693 case MGS_CONFIG_READ:
694 DEBUG_REQ(D_MGS, req, "read config");
695 req_capsule_set(&req->rq_pill, &RQF_MGS_CONFIG_READ);
696 rc = mgs_config_read(req);
699 DEBUG_REQ(D_MGS, req, "enqueue");
700 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
701 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
702 ldlm_server_blocking_ast, NULL);
704 case LDLM_BL_CALLBACK:
705 case LDLM_CP_CALLBACK:
706 DEBUG_REQ(D_MGS, req, "callback");
707 CERROR("callbacks should not happen on MGS\n");
712 DEBUG_REQ(D_INFO, req, "ping");
713 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
714 rc = target_handle_ping(req);
717 DEBUG_REQ(D_MGS, req, "log cancel");
718 rc = -ENOTSUPP; /* la la la */
721 case LLOG_ORIGIN_HANDLE_CREATE:
722 DEBUG_REQ(D_MGS, req, "llog_open");
723 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
724 rc = llog_origin_handle_open(req);
726 (void)mgs_handle_fslog_hack(req);
728 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
729 DEBUG_REQ(D_MGS, req, "llog next block");
730 req_capsule_set(&req->rq_pill,
731 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
732 rc = llog_origin_handle_next_block(req);
734 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
735 DEBUG_REQ(D_MGS, req, "llog prev block");
736 req_capsule_set(&req->rq_pill,
737 &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
738 rc = llog_origin_handle_prev_block(req);
740 case LLOG_ORIGIN_HANDLE_READ_HEADER:
741 DEBUG_REQ(D_MGS, req, "llog read header");
742 req_capsule_set(&req->rq_pill,
743 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
744 rc = llog_origin_handle_read_header(req);
746 case LLOG_ORIGIN_HANDLE_CLOSE:
747 DEBUG_REQ(D_MGS, req, "llog close");
748 rc = llog_origin_handle_close(req);
754 LASSERT(current->journal_info == NULL);
756 DEBUG_REQ(D_MGS, req, "MGS fail to handle opc = %d: rc = %d\n",
759 rc = ptlrpc_error(req);
763 target_send_reply(req, rc, fail);
767 static inline int mgs_init_export(struct obd_export *exp)
769 struct mgs_export_data *data = &exp->u.eu_mgs_data;
771 /* init mgs_export_data for fsc */
772 spin_lock_init(&data->med_lock);
773 CFS_INIT_LIST_HEAD(&data->med_clients);
775 spin_lock(&exp->exp_lock);
776 exp->exp_connecting = 1;
777 spin_unlock(&exp->exp_lock);
779 /* self-export doesn't need client data and ldlm initialization */
780 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
781 &exp->exp_client_uuid)))
783 return ldlm_init_export(exp);
786 static inline int mgs_destroy_export(struct obd_export *exp)
790 target_destroy_export(exp);
791 mgs_client_free(exp);
793 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
794 &exp->exp_client_uuid)))
797 ldlm_destroy_export(exp);
802 static int mgs_extract_fs_pool(char * arg, char *fsname, char *poolname)
807 for (ptr = arg; (*ptr != '\0') && (*ptr != '.'); ptr++ ) {
815 strcpy(poolname, ptr);
820 static int mgs_iocontrol_pool(const struct lu_env *env,
821 struct mgs_device *mgs,
822 struct obd_ioctl_data *data)
824 struct mgs_thread_info *mgi = mgs_env_info(env);
826 struct lustre_cfg *lcfg = NULL;
827 char *poolname = NULL;
830 OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
831 if (poolname == NULL)
834 if (data->ioc_type != LUSTRE_CFG_TYPE) {
835 CERROR("%s: unknown cfg record type: %d\n",
836 mgs->mgs_obd->obd_name, data->ioc_type);
837 GOTO(out_pool, rc = -EINVAL);
840 if (data->ioc_plen1 > PAGE_CACHE_SIZE)
841 GOTO(out_pool, rc = -E2BIG);
843 OBD_ALLOC(lcfg, data->ioc_plen1);
845 GOTO(out_pool, rc = -ENOMEM);
847 if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
848 GOTO(out_lcfg, rc = -EFAULT);
850 if (lcfg->lcfg_bufcount < 2)
851 GOTO(out_lcfg, rc = -EFAULT);
853 /* first arg is always <fsname>.<poolname> */
854 rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
859 switch (lcfg->lcfg_command) {
861 if (lcfg->lcfg_bufcount != 2)
862 GOTO(out_lcfg, rc = -EINVAL);
863 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
867 if (lcfg->lcfg_bufcount != 3)
868 GOTO(out_lcfg, rc = -EINVAL);
869 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
870 poolname, lustre_cfg_string(lcfg, 2));
873 if (lcfg->lcfg_bufcount != 3)
874 GOTO(out_lcfg, rc = -EINVAL);
875 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
876 poolname, lustre_cfg_string(lcfg, 2));
879 if (lcfg->lcfg_bufcount != 2)
880 GOTO(out_lcfg, rc = -EINVAL);
881 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
889 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
890 rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
895 OBD_FREE(lcfg, data->ioc_plen1);
897 OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
901 /* from mdt_iocontrol */
902 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
903 void *karg, void *uarg)
905 struct mgs_device *mgs = exp2mgs_dev(exp);
906 struct obd_ioctl_data *data = karg;
911 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
913 rc = lu_env_init(&env, LCT_MG_THREAD);
919 case OBD_IOC_PARAM: {
920 struct mgs_thread_info *mgi = mgs_env_info(&env);
921 struct lustre_cfg *lcfg;
923 if (data->ioc_type != LUSTRE_CFG_TYPE) {
924 CERROR("%s: unknown cfg record type: %d\n",
925 mgs->mgs_obd->obd_name, data->ioc_type);
926 GOTO(out, rc = -EINVAL);
929 OBD_ALLOC(lcfg, data->ioc_plen1);
931 GOTO(out, rc = -ENOMEM);
932 if (copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
933 GOTO(out_free, rc = -EFAULT);
935 if (lcfg->lcfg_bufcount < 1)
936 GOTO(out_free, rc = -EINVAL);
938 rc = mgs_setparam(&env, mgs, lcfg, mgi->mgi_fsname);
940 CERROR("%s: setparam err: rc = %d\n",
941 exp->exp_obd->obd_name, rc);
943 OBD_FREE(lcfg, data->ioc_plen1);
947 case OBD_IOC_REPLACE_NIDS: {
948 if (!data->ioc_inllen1 || !data->ioc_inlbuf1) {
949 CERROR("No device name specified!\n");
954 if (data->ioc_inlbuf1[data->ioc_inllen1 - 1] != 0) {
955 CERROR("Device name is not NUL terminated!\n");
960 if (data->ioc_plen1 > MTI_NAME_MAXLEN) {
961 CERROR("Device name is too long\n");
966 if (!data->ioc_inllen2 || !data->ioc_inlbuf2) {
967 CERROR("No NIDs were specified!\n");
972 if (data->ioc_inlbuf2[data->ioc_inllen2 - 1] != 0) {
973 CERROR("NID list is not NUL terminated!\n");
978 /* replace nids in llog */
979 rc = mgs_replace_nids(&env, mgs, data->ioc_inlbuf1,
982 CERROR("%s: error replacing nids: rc = %d\n",
983 exp->exp_obd->obd_name, rc);
989 rc = mgs_iocontrol_pool(&env, mgs, data);
992 case OBD_IOC_DUMP_LOG: {
993 struct llog_ctxt *ctxt;
995 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
996 rc = class_config_dump_llog(&env, ctxt, data->ioc_inlbuf1,
1003 case OBD_IOC_LLOG_CANCEL:
1004 case OBD_IOC_LLOG_REMOVE:
1005 case OBD_IOC_LLOG_CHECK:
1006 case OBD_IOC_LLOG_INFO:
1007 case OBD_IOC_LLOG_PRINT: {
1008 struct llog_ctxt *ctxt;
1010 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1011 rc = llog_ioctl(&env, ctxt, cmd, data);
1012 llog_ctxt_put(ctxt);
1017 CERROR("%s: unknown command %#x\n",
1018 mgs->mgs_obd->obd_name, cmd);
1027 static int mgs_connect_to_osd(struct mgs_device *m, const char *nextdev)
1029 struct obd_connect_data *data = NULL;
1030 struct obd_device *obd;
1034 OBD_ALLOC_PTR(data);
1038 obd = class_name2obd(nextdev);
1040 CERROR("can't locate next device: %s\n", nextdev);
1041 GOTO(out, rc = -ENOTCONN);
1044 data->ocd_version = LUSTRE_VERSION_CODE;
1046 rc = obd_connect(NULL, &m->mgs_bottom_exp, obd,
1047 &obd->obd_uuid, data, NULL);
1049 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
1053 m->mgs_bottom = lu2dt_dev(m->mgs_bottom_exp->exp_obd->obd_lu_dev);
1054 m->mgs_dt_dev.dd_lu_dev.ld_site = m->mgs_bottom->dd_lu_dev.ld_site;
1055 LASSERT(m->mgs_dt_dev.dd_lu_dev.ld_site);
1062 static int mgs_init0(const struct lu_env *env, struct mgs_device *mgs,
1063 struct lu_device_type *ldt, struct lustre_cfg *lcfg)
1065 static struct ptlrpc_service_conf conf;
1066 struct obd_device *obd;
1067 struct lustre_mount_info *lmi;
1068 struct llog_ctxt *ctxt;
1073 lmi = server_get_mount(lustre_cfg_string(lcfg, 0));
1077 mgs->mgs_dt_dev.dd_lu_dev.ld_ops = &mgs_lu_ops;
1079 rc = mgs_connect_to_osd(mgs, lustre_cfg_string(lcfg, 3));
1083 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1086 mgs->mgs_obd->obd_lu_dev = &mgs->mgs_dt_dev.dd_lu_dev;
1088 obd->u.obt.obt_magic = OBT_MAGIC;
1089 obd->u.obt.obt_instance = 0;
1091 /* namespace for mgs llog */
1092 obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
1093 LDLM_NAMESPACE_SERVER,
1094 LDLM_NAMESPACE_MODEST,
1096 if (obd->obd_namespace == NULL)
1097 GOTO(err_ops, rc = -ENOMEM);
1100 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1101 "mgs_ldlm_client", &obd->obd_ldlm_client);
1103 rc = mgs_fs_setup(env, mgs);
1105 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
1110 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT,
1111 obd, &llog_osd_ops);
1115 /* XXX: we need this trick till N:1 stack is supported
1116 * set "current" directory for named llogs */
1117 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1119 ctxt->loc_dir = mgs->mgs_configs_dir;
1120 llog_ctxt_put(ctxt);
1122 /* No recovery for MGC's */
1123 obd->obd_replayable = 0;
1125 /* Internal mgs setup */
1126 mgs_init_fsdb_list(mgs);
1127 mutex_init(&mgs->mgs_mutex);
1128 mgs->mgs_start_time = cfs_time_current_sec();
1129 spin_lock_init(&mgs->mgs_lock);
1131 rc = lproc_mgs_setup(mgs, lustre_cfg_string(lcfg, 3));
1133 CERROR("%s: cannot initialize proc entry: rc = %d\n",
1138 conf = (typeof(conf)) {
1139 .psc_name = LUSTRE_MGS_NAME,
1140 .psc_watchdog_factor = MGS_SERVICE_WATCHDOG_FACTOR,
1142 .bc_nbufs = MGS_NBUFS,
1143 .bc_buf_size = MGS_BUFSIZE,
1144 .bc_req_max_size = MGS_MAXREQSIZE,
1145 .bc_rep_max_size = MGS_MAXREPSIZE,
1146 .bc_req_portal = MGS_REQUEST_PORTAL,
1147 .bc_rep_portal = MGC_REPLY_PORTAL,
1150 .tc_thr_name = "ll_mgs",
1151 .tc_nthrs_init = MGS_NTHRS_INIT,
1152 .tc_nthrs_max = MGS_NTHRS_MAX,
1153 .tc_ctx_tags = LCT_MG_THREAD,
1156 .so_req_handler = mgs_handle,
1157 .so_req_printer = target_print_req,
1160 /* Start the service threads */
1161 mgs->mgs_service = ptlrpc_register_service(&conf, obd->obd_proc_entry);
1162 if (IS_ERR(mgs->mgs_service)) {
1163 rc = PTR_ERR(mgs->mgs_service);
1164 CERROR("failed to start service: %d\n", rc);
1165 GOTO(err_lproc, rc);
1168 ping_evictor_start();
1170 CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
1172 /* device stack is not yet fully setup to keep no objects behind */
1173 lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1177 lproc_mgs_cleanup(mgs);
1179 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1181 ctxt->loc_dir = NULL;
1182 llog_cleanup(env, ctxt);
1185 /* No extra cleanup needed for llog_init_commit_thread() */
1186 mgs_fs_cleanup(env, mgs);
1188 ldlm_namespace_free(obd->obd_namespace, NULL, 0);
1189 obd->obd_namespace = NULL;
1191 lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1192 if (!cfs_hash_is_empty(mgs2lu_dev(mgs)->ld_site->ls_obj_hash)) {
1193 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1194 lu_site_print(env, mgs2lu_dev(mgs)->ld_site, &msgdata,
1197 obd_disconnect(mgs->mgs_bottom_exp);
1200 server_put_mount(lustre_cfg_string(lcfg, 0), lmi->lmi_mnt);
1204 static struct lu_device *mgs_device_free(const struct lu_env *env,
1205 struct lu_device *lu)
1207 struct mgs_device *mgs = lu2mgs_dev(lu);
1210 dt_device_fini(&mgs->mgs_dt_dev);
1215 static int mgs_process_config(const struct lu_env *env,
1216 struct lu_device *dev,
1217 struct lustre_cfg *lcfg)
1223 static int mgs_object_init(const struct lu_env *env, struct lu_object *o,
1224 const struct lu_object_conf *unused)
1226 struct mgs_device *d = lu2mgs_dev(o->lo_dev);
1227 struct lu_device *under;
1228 struct lu_object *below;
1232 /* do no set .do_ops as mgs calls to bottom osd directly */
1234 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
1235 PFID(lu_object_fid(o)));
1237 under = &d->mgs_bottom->dd_lu_dev;
1238 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1240 lu_object_add(o, below);
1247 static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
1249 struct mgs_object *obj = lu2mgs_obj(o);
1250 struct lu_object_header *h = o->lo_header;
1252 dt_object_fini(&obj->mgo_obj);
1253 lu_object_header_fini(h);
1257 static int mgs_object_print(const struct lu_env *env, void *cookie,
1258 lu_printer_t p, const struct lu_object *l)
1260 const struct mgs_object *o = lu2mgs_obj((struct lu_object *) l);
1262 return (*p)(env, cookie, LUSTRE_MGS_NAME"-object@%p", o);
1265 struct lu_object_operations mgs_lu_obj_ops = {
1266 .loo_object_init = mgs_object_init,
1267 .loo_object_free = mgs_object_free,
1268 .loo_object_print = mgs_object_print,
1271 struct lu_object *mgs_object_alloc(const struct lu_env *env,
1272 const struct lu_object_header *hdr,
1273 struct lu_device *d)
1275 struct lu_object_header *h;
1276 struct mgs_object *o;
1277 struct lu_object *l;
1279 LASSERT(hdr == NULL);
1283 l = &o->mgo_obj.do_lu;
1286 lu_object_header_init(h);
1287 dt_object_init(&o->mgo_obj, h, d);
1288 lu_object_add_top(h, l);
1290 l->lo_ops = &mgs_lu_obj_ops;
1298 const struct lu_device_operations mgs_lu_ops = {
1299 .ldo_object_alloc = mgs_object_alloc,
1300 .ldo_process_config = mgs_process_config,
1303 static struct lu_device *mgs_device_alloc(const struct lu_env *env,
1304 struct lu_device_type *type,
1305 struct lustre_cfg *lcfg)
1307 struct mgs_device *mgs;
1308 struct lu_device *ludev;
1312 ludev = ERR_PTR(-ENOMEM);
1316 ludev = mgs2lu_dev(mgs);
1317 dt_device_init(&mgs->mgs_dt_dev, type);
1318 rc = mgs_init0(env, mgs, type, lcfg);
1320 mgs_device_free(env, ludev);
1321 ludev = ERR_PTR(rc);
1327 static struct lu_device *mgs_device_fini(const struct lu_env *env,
1328 struct lu_device *d)
1330 struct mgs_device *mgs = lu2mgs_dev(d);
1331 struct obd_device *obd = mgs->mgs_obd;
1332 struct llog_ctxt *ctxt;
1336 LASSERT(mgs->mgs_bottom);
1338 ping_evictor_stop();
1340 ptlrpc_unregister_service(mgs->mgs_service);
1342 obd_exports_barrier(obd);
1343 obd_zombie_barrier();
1345 mgs_cleanup_fsdb_list(mgs);
1346 lproc_mgs_cleanup(mgs);
1348 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1350 ctxt->loc_dir = NULL;
1351 llog_cleanup(env, ctxt);
1354 mgs_fs_cleanup(env, mgs);
1356 ldlm_namespace_free(obd->obd_namespace, NULL, 1);
1357 obd->obd_namespace = NULL;
1359 lu_site_purge(env, d->ld_site, ~0);
1360 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
1361 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1362 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
1365 LASSERT(mgs->mgs_bottom_exp);
1366 obd_disconnect(mgs->mgs_bottom_exp);
1368 server_put_mount(obd->obd_name, NULL);
1373 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1374 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1376 LU_TYPE_INIT_FINI(mgs, &mgs_thread_key);
1378 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1380 static struct lu_device_type_operations mgs_device_type_ops = {
1381 .ldto_init = mgs_type_init,
1382 .ldto_fini = mgs_type_fini,
1384 .ldto_start = mgs_type_start,
1385 .ldto_stop = mgs_type_stop,
1387 .ldto_device_alloc = mgs_device_alloc,
1388 .ldto_device_free = mgs_device_free,
1390 .ldto_device_fini = mgs_device_fini
1393 static struct lu_device_type mgs_device_type = {
1394 .ldt_tags = LU_DEVICE_DT,
1395 .ldt_name = LUSTRE_MGS_NAME,
1396 .ldt_ops = &mgs_device_type_ops,
1397 .ldt_ctx_tags = LCT_MG_THREAD
1401 /* use obd ops to offer management infrastructure */
1402 static struct obd_ops mgs_obd_ops = {
1403 .o_owner = THIS_MODULE,
1404 .o_connect = mgs_connect,
1405 .o_reconnect = mgs_reconnect,
1406 .o_disconnect = mgs_disconnect,
1407 .o_init_export = mgs_init_export,
1408 .o_destroy_export = mgs_destroy_export,
1409 .o_iocontrol = mgs_iocontrol,
1412 static int __init mgs_init(void)
1414 struct lprocfs_static_vars lvars;
1416 lprocfs_mgs_init_vars(&lvars);
1417 class_register_type(&mgs_obd_ops, NULL, lvars.module_vars,
1418 LUSTRE_MGS_NAME, &mgs_device_type);
1423 static void /*__exit*/ mgs_exit(void)
1425 class_unregister_type(LUSTRE_MGS_NAME);
1428 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1429 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
1430 MODULE_LICENSE("GPL");
1432 module_init(mgs_init);
1433 module_exit(mgs_exit);