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>
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))) {
141 struct fs_db *fsdb = (struct fs_db *)lock->l_ast_data;
142 struct lustre_handle lockh;
144 /* clear the bit before lock put */
145 clear_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags);
147 ldlm_lock2handle(lock, &lockh);
148 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
151 RETURN(ldlm_completion_ast(lock, flags, cbdata));
154 static int mgs_completion_ast_ir(struct ldlm_lock *lock, __u64 flags,
159 if (!(flags & (LDLM_FL_BLOCK_WAIT | LDLM_FL_BLOCK_GRANTED |
160 LDLM_FL_BLOCK_CONV))) {
163 /* l_ast_data is used as a marker to avoid cancel ldlm lock
164 * twice. See LU-1259. */
165 lock_res_and_lock(lock);
166 fsdb = (struct fs_db *)lock->l_ast_data;
167 lock->l_ast_data = NULL;
168 unlock_res_and_lock(lock);
171 struct lustre_handle lockh;
173 mgs_ir_notify_complete(fsdb);
175 ldlm_lock2handle(lock, &lockh);
176 ldlm_lock_decref_and_cancel(&lockh, LCK_EX);
180 RETURN(ldlm_completion_ast(lock, flags, cbdata));
183 void mgs_revoke_lock(struct mgs_device *mgs, struct fs_db *fsdb, int type)
185 ldlm_completion_callback cp = NULL;
186 struct lustre_handle lockh = { 0 };
187 struct ldlm_res_id res_id;
188 __u64 flags = LDLM_FL_ATOMIC_CB;
192 LASSERT(fsdb->fsdb_name[0] != '\0');
193 rc = mgc_fsname2resid(fsdb->fsdb_name, &res_id, type);
197 case CONFIG_T_CONFIG:
198 cp = mgs_completion_ast_config;
199 if (test_and_set_bit(FSDB_REVOKING_LOCK, &fsdb->fsdb_flags))
202 case CONFIG_T_RECOVER:
203 cp = mgs_completion_ast_ir;
210 rc = ldlm_cli_enqueue_local(mgs->mgs_obd->obd_namespace,
211 &res_id, LDLM_PLAIN, NULL, LCK_EX,
212 &flags, ldlm_blocking_ast, cp,
213 NULL, fsdb, 0, LVB_T_NONE, NULL,
215 if (rc != ELDLM_OK) {
216 CERROR("can't take cfg lock for "LPX64"/"LPX64"(%d)\n",
217 le64_to_cpu(res_id.name[0]),
218 le64_to_cpu(res_id.name[1]), rc);
220 if (type == CONFIG_T_CONFIG)
221 clear_bit(FSDB_REVOKING_LOCK,
224 /* lock has been cancelled in completion_ast. */
233 static int mgs_check_target(const struct lu_env *env,
234 struct mgs_device *mgs,
235 struct mgs_target_info *mti)
240 rc = mgs_check_index(env, mgs, mti);
242 LCONSOLE_ERROR_MSG(0x13b, "%s claims to have registered, but "
243 "this MGS does not know about it, preventing "
244 "registration.\n", mti->mti_svname);
246 } else if (rc == -1) {
247 LCONSOLE_ERROR_MSG(0x13c, "Client log %s-client has "
248 "disappeared! Regenerating all logs.\n",
250 mti->mti_flags |= LDD_F_WRITECONF;
253 /* Index is correctly marked as used */
255 /* If the logs don't contain the mti_nids then add
256 them as failover nids */
257 rc = mgs_check_failnid(env, mgs, mti);
263 /* Ensure this is not a failover node that is connecting first*/
264 static int mgs_check_failover_reg(struct mgs_target_info *mti)
270 ptr = mti->mti_params;
271 while (class_find_param(ptr, PARAM_FAILNODE, &ptr) == 0) {
272 while (class_parse_nid_quiet(ptr, &nid, &ptr) == 0) {
273 for (i = 0; i < mti->mti_nid_count; i++) {
274 if (nid == mti->mti_nids[i]) {
275 LCONSOLE_WARN("Denying initial registra"
276 "tion attempt from nid %s"
277 ", specified as failover"
278 "\n",libcfs_nid2str(nid));
279 return -EADDRNOTAVAIL;
287 /* Called whenever a target starts up. Flags indicate first connect, etc. */
288 static int mgs_handle_target_reg(struct ptlrpc_request *req)
290 struct obd_device *obd = req->rq_export->exp_obd;
291 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
292 struct lu_env *env = req->rq_svc_thread->t_env;
293 struct mgs_target_info *mti, *rep_mti;
299 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_REG);
301 mti = req_capsule_client_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
303 opc = mti->mti_flags & LDD_F_OPC_MASK;
304 if (opc == LDD_F_OPC_READY) {
305 CDEBUG(D_MGS, "fs: %s index: %d is ready to reconnect.\n",
306 mti->mti_fsname, mti->mti_stripe_index);
307 rc = mgs_ir_update(env, mgs, mti);
309 LASSERT(!(mti->mti_flags & LDD_F_IR_CAPABLE));
310 CERROR("Update IR return with %d(ignore and IR "
313 GOTO(out_nolock, rc);
316 /* Do not support unregistering right now. */
317 if (opc != LDD_F_OPC_REG)
318 GOTO(out_nolock, rc = -EINVAL);
320 CDEBUG(D_MGS, "fs: %s index: %d is registered to MGS.\n",
321 mti->mti_fsname, mti->mti_stripe_index);
323 if (mti->mti_flags & LDD_F_NEED_INDEX)
324 mti->mti_flags |= LDD_F_WRITECONF;
326 if (!(mti->mti_flags & (LDD_F_WRITECONF | LDD_F_UPGRADE14 |
328 /* We're just here as a startup ping. */
329 CDEBUG(D_MGS, "Server %s is running on %s\n",
330 mti->mti_svname, obd_export_nid2str(req->rq_export));
331 rc = mgs_check_target(env, mgs, mti);
332 /* above will set appropriate mti flags */
334 /* Nothing wrong, or fatal error */
335 GOTO(out_nolock, rc);
336 } else if (!(mti->mti_flags & LDD_F_NO_PRIMNODE)) {
337 rc = mgs_check_failover_reg(mti);
339 GOTO(out_nolock, rc);
342 OBD_FAIL_TIMEOUT(OBD_FAIL_MGS_PAUSE_TARGET_REG, 10);
344 if (mti->mti_flags & LDD_F_WRITECONF) {
345 if (mti->mti_flags & LDD_F_SV_TYPE_MDT &&
346 mti->mti_stripe_index == 0) {
347 rc = mgs_erase_logs(env, mgs, mti->mti_fsname);
348 LCONSOLE_WARN("%s: Logs for fs %s were removed by user "
349 "request. All servers must be restarted "
350 "in order to regenerate the logs."
351 "\n", obd->obd_name, mti->mti_fsname);
352 } else if (mti->mti_flags &
353 (LDD_F_SV_TYPE_OST | LDD_F_SV_TYPE_MDT)) {
354 rc = mgs_erase_log(env, mgs, mti->mti_svname);
355 LCONSOLE_WARN("%s: Regenerating %s log by user "
357 obd->obd_name, mti->mti_svname);
359 mti->mti_flags |= LDD_F_UPDATE;
360 /* Erased logs means start from scratch. */
361 mti->mti_flags &= ~LDD_F_UPGRADE14;
363 GOTO(out_nolock, rc);
366 rc = mgs_find_or_make_fsdb(env, mgs, mti->mti_fsname, &fsdb);
368 CERROR("Can't get db for %s: %d\n", mti->mti_fsname, rc);
369 GOTO(out_nolock, rc);
373 * Log writing contention is handled by the fsdb_mutex.
375 * It should be alright if someone was reading while we were
376 * updating the logs - if we revoke at the end they will just update
377 * from where they left off.
380 if (mti->mti_flags & LDD_F_UPGRADE14) {
381 CERROR("Can't upgrade from 1.4 (%d)\n", rc);
385 if (mti->mti_flags & LDD_F_UPDATE) {
386 CDEBUG(D_MGS, "updating %s, index=%d\n", mti->mti_svname,
387 mti->mti_stripe_index);
389 /* create or update the target log
390 and update the client/mdt logs */
391 rc = mgs_write_log_target(env, mgs, mti, fsdb);
393 CERROR("Failed to write %s log (%d)\n",
394 mti->mti_svname, rc);
398 mti->mti_flags &= ~(LDD_F_VIRGIN | LDD_F_UPDATE |
399 LDD_F_NEED_INDEX | LDD_F_WRITECONF |
401 mti->mti_flags |= LDD_F_REWRITE_LDD;
405 mgs_revoke_lock(mgs, fsdb, CONFIG_T_CONFIG);
408 CDEBUG(D_MGS, "replying with %s, index=%d, rc=%d\n", mti->mti_svname,
409 mti->mti_stripe_index, rc);
412 /* we need an error flag to tell the target what's going on,
413 * instead of just doing it by error code only. */
414 mti->mti_flags |= LDD_F_ERROR;
416 rc = req_capsule_server_pack(&req->rq_pill);
420 /* send back the whole mti in the reply */
421 rep_mti = req_capsule_server_get(&req->rq_pill, &RMF_MGS_TARGET_INFO);
424 /* Flush logs to disk */
425 dt_sync(req->rq_svc_thread->t_env, mgs->mgs_bottom);
429 static int mgs_set_info_rpc(struct ptlrpc_request *req)
431 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
432 struct lu_env *env = req->rq_svc_thread->t_env;
433 struct mgs_send_param *msp, *rep_msp;
434 struct mgs_thread_info *mgi = mgs_env_info(env);
436 struct lustre_cfg *lcfg;
439 msp = req_capsule_client_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
442 /* Construct lustre_cfg structure to pass to function mgs_setparam */
443 lustre_cfg_bufs_reset(&mgi->mgi_bufs, NULL);
444 lustre_cfg_bufs_set_string(&mgi->mgi_bufs, 1, msp->mgs_param);
445 lcfg = lustre_cfg_new(LCFG_PARAM, &mgi->mgi_bufs);
447 GOTO(out, rc = PTR_ERR(lcfg));
448 rc = mgs_setparam(env, mgs, lcfg, mgi->mgi_fsname);
450 CERROR("Error %d in setting the parameter %s for fs %s\n",
451 rc, msp->mgs_param, mgi->mgi_fsname);
455 rc = req_capsule_server_pack(&req->rq_pill);
457 rep_msp = req_capsule_server_get(&req->rq_pill, &RMF_MGS_SEND_PARAM);
461 lustre_cfg_free(lcfg);
466 static int mgs_config_read(struct ptlrpc_request *req)
468 struct mgs_config_body *body;
472 body = req_capsule_client_get(&req->rq_pill, &RMF_MGS_CONFIG_BODY);
476 switch (body->mcb_type) {
477 case CONFIG_T_RECOVER:
478 rc = mgs_get_ir_logs(req);
481 case CONFIG_T_CONFIG:
494 * similar as in ost_connect_check_sptlrpc()
496 static int mgs_connect_check_sptlrpc(struct ptlrpc_request *req)
498 struct obd_export *exp = req->rq_export;
499 struct mgs_device *mgs = exp2mgs_dev(req->rq_export);
500 struct lu_env *env = req->rq_svc_thread->t_env;
502 struct sptlrpc_flavor flvr;
505 if (exp->exp_flvr.sf_rpc == SPTLRPC_FLVR_INVALID) {
506 rc = mgs_find_or_make_fsdb(env, mgs, MGSSELF_NAME, &fsdb);
510 mutex_lock(&fsdb->fsdb_mutex);
511 if (sptlrpc_rule_set_choose(&fsdb->fsdb_srpc_gen,
512 LUSTRE_SP_MGC, LUSTRE_SP_MGS,
515 /* by defualt allow any flavors */
516 flvr.sf_rpc = SPTLRPC_FLVR_ANY;
518 mutex_unlock(&fsdb->fsdb_mutex);
520 spin_lock(&exp->exp_lock);
522 exp->exp_sp_peer = req->rq_sp_from;
523 exp->exp_flvr = flvr;
525 if (exp->exp_flvr.sf_rpc != SPTLRPC_FLVR_ANY &&
526 exp->exp_flvr.sf_rpc != req->rq_flvr.sf_rpc) {
527 CERROR("invalid rpc flavor %x, expect %x, from %s\n",
528 req->rq_flvr.sf_rpc, exp->exp_flvr.sf_rpc,
529 libcfs_nid2str(req->rq_peer.nid));
533 spin_unlock(&exp->exp_lock);
535 if (exp->exp_sp_peer != req->rq_sp_from) {
536 CERROR("RPC source %s doesn't match %s\n",
537 sptlrpc_part2name(req->rq_sp_from),
538 sptlrpc_part2name(exp->exp_sp_peer));
541 rc = sptlrpc_target_export_check(exp, req);
548 /* Called whenever a target cleans up. */
549 /* XXX - Currently unused */
550 static int mgs_handle_target_del(struct ptlrpc_request *req)
553 mgs_counter_incr(req->rq_export, LPROC_MGS_TARGET_DEL);
557 /* XXX - Currently unused */
558 static int mgs_handle_exception(struct ptlrpc_request *req)
561 mgs_counter_incr(req->rq_export, LPROC_MGS_EXCEPTION);
566 * For old clients there is no direct way of knowing which filesystems
567 * a client is operating at the MGS side. But we need to pick up those
568 * clients so that the MGS can mark the corresponding filesystem as
569 * non-IR capable because old clients are not ready to be notified.
571 * This is why we have this _hack_ function. We detect the filesystem's
572 * name by hacking llog operation which is currently used by the clients
573 * to fetch configuration logs. At present this is fine because this is
574 * the ONLY llog operation between mgc and the MGS.
576 * If extra llog operation is going to be added, this function needs fixing.
578 * If releases prior than 2.0 are not supported, we can remove this function.
580 static int mgs_handle_fslog_hack(struct ptlrpc_request *req)
587 /* XXX: We suppose that llog at mgs is only used for
588 * fetching file system log */
589 logname = req_capsule_client_get(&req->rq_pill, &RMF_NAME);
590 if (logname == NULL) {
591 CERROR("No logname, is llog on MGS used for something else?\n");
595 ptr = strchr(logname, '-');
596 rc = (int)(ptr - logname);
597 if (ptr == NULL || rc >= sizeof(fsname)) {
598 CERROR("Invalid logname received: %s\n", logname);
602 strncpy(fsname, logname, rc);
604 rc = mgs_fsc_attach(req->rq_svc_thread->t_env, req->rq_export, fsname);
605 if (rc < 0 && rc != -EEXIST)
606 CERROR("add fs client %s returns %d\n", fsname, rc);
611 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
612 int mgs_handle(struct ptlrpc_request *req)
614 int fail = OBD_FAIL_MGS_ALL_REPLY_NET;
618 req_capsule_init(&req->rq_pill, req, RCL_SERVER);
619 CFS_FAIL_TIMEOUT_MS(OBD_FAIL_MGS_PAUSE_REQ, cfs_fail_val);
620 if (CFS_FAIL_CHECK(OBD_FAIL_MGS_ALL_REQUEST_NET))
623 LASSERT(current->journal_info == NULL);
624 opc = lustre_msg_get_opc(req->rq_reqmsg);
626 if (opc == SEC_CTX_INIT ||
627 opc == SEC_CTX_INIT_CONT ||
631 if (opc != MGS_CONNECT) {
632 if (!class_connected_export(req->rq_export)) {
633 DEBUG_REQ(D_MGS, req, "operation on unconnected MGS\n");
634 req->rq_status = -ENOTCONN;
635 GOTO(out, rc = -ENOTCONN);
641 DEBUG_REQ(D_MGS, req, "connect");
642 /* MGS and MDS have same request format for connect */
643 req_capsule_set(&req->rq_pill, &RQF_MDS_CONNECT);
644 rc = target_handle_connect(req);
646 rc = mgs_connect_check_sptlrpc(req);
648 if (!rc && (lustre_msg_get_conn_cnt(req->rq_reqmsg) > 1))
649 /* Make clients trying to reconnect after a MGS restart
650 happy; also requires obd_replayable */
651 lustre_msg_add_op_flags(req->rq_repmsg,
652 MSG_CONNECT_RECONNECT);
655 DEBUG_REQ(D_MGS, req, "disconnect");
656 /* MGS and MDS have same request format for disconnect */
657 req_capsule_set(&req->rq_pill, &RQF_MDS_DISCONNECT);
658 rc = target_handle_disconnect(req);
659 req->rq_status = rc; /* superfluous? */
662 DEBUG_REQ(D_MGS, req, "exception");
663 rc = mgs_handle_exception(req);
666 DEBUG_REQ(D_MGS, req, "target add");
667 req_capsule_set(&req->rq_pill, &RQF_MGS_TARGET_REG);
668 rc = mgs_handle_target_reg(req);
671 DEBUG_REQ(D_MGS, req, "target del");
672 rc = mgs_handle_target_del(req);
675 DEBUG_REQ(D_MGS, req, "set_info");
676 req_capsule_set(&req->rq_pill, &RQF_MGS_SET_INFO);
677 rc = mgs_set_info_rpc(req);
679 case MGS_CONFIG_READ:
680 DEBUG_REQ(D_MGS, req, "read config");
681 req_capsule_set(&req->rq_pill, &RQF_MGS_CONFIG_READ);
682 rc = mgs_config_read(req);
685 DEBUG_REQ(D_MGS, req, "enqueue");
686 req_capsule_set(&req->rq_pill, &RQF_LDLM_ENQUEUE);
687 rc = ldlm_handle_enqueue(req, ldlm_server_completion_ast,
688 ldlm_server_blocking_ast, NULL);
690 case LDLM_BL_CALLBACK:
691 case LDLM_CP_CALLBACK:
692 DEBUG_REQ(D_MGS, req, "callback");
693 CERROR("callbacks should not happen on MGS\n");
698 DEBUG_REQ(D_INFO, req, "ping");
699 req_capsule_set(&req->rq_pill, &RQF_OBD_PING);
700 rc = target_handle_ping(req);
703 DEBUG_REQ(D_MGS, req, "log cancel");
704 rc = -ENOTSUPP; /* la la la */
707 case LLOG_ORIGIN_HANDLE_CREATE:
708 DEBUG_REQ(D_MGS, req, "llog_open");
709 req_capsule_set(&req->rq_pill, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
710 rc = llog_origin_handle_open(req);
712 (void)mgs_handle_fslog_hack(req);
714 case LLOG_ORIGIN_HANDLE_NEXT_BLOCK:
715 DEBUG_REQ(D_MGS, req, "llog next block");
716 req_capsule_set(&req->rq_pill,
717 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
718 rc = llog_origin_handle_next_block(req);
720 case LLOG_ORIGIN_HANDLE_PREV_BLOCK:
721 DEBUG_REQ(D_MGS, req, "llog prev block");
722 req_capsule_set(&req->rq_pill,
723 &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK);
724 rc = llog_origin_handle_prev_block(req);
726 case LLOG_ORIGIN_HANDLE_READ_HEADER:
727 DEBUG_REQ(D_MGS, req, "llog read header");
728 req_capsule_set(&req->rq_pill,
729 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER);
730 rc = llog_origin_handle_read_header(req);
732 case LLOG_ORIGIN_HANDLE_CLOSE:
733 DEBUG_REQ(D_MGS, req, "llog close");
734 rc = llog_origin_handle_close(req);
740 LASSERT(current->journal_info == NULL);
742 DEBUG_REQ(D_MGS, req, "MGS fail to handle opc = %d: rc = %d\n",
745 rc = ptlrpc_error(req);
749 target_send_reply(req, rc, fail);
753 static inline int mgs_init_export(struct obd_export *exp)
755 struct mgs_export_data *data = &exp->u.eu_mgs_data;
757 /* init mgs_export_data for fsc */
758 spin_lock_init(&data->med_lock);
759 CFS_INIT_LIST_HEAD(&data->med_clients);
761 spin_lock(&exp->exp_lock);
762 exp->exp_connecting = 1;
763 spin_unlock(&exp->exp_lock);
765 /* self-export doesn't need client data and ldlm initialization */
766 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
767 &exp->exp_client_uuid)))
769 return ldlm_init_export(exp);
772 static inline int mgs_destroy_export(struct obd_export *exp)
776 target_destroy_export(exp);
777 mgs_client_free(exp);
779 if (unlikely(obd_uuid_equals(&exp->exp_obd->obd_uuid,
780 &exp->exp_client_uuid)))
783 ldlm_destroy_export(exp);
788 static int mgs_extract_fs_pool(char * arg, char *fsname, char *poolname)
793 for (ptr = arg; (*ptr != '\0') && (*ptr != '.'); ptr++ ) {
801 strcpy(poolname, ptr);
806 static int mgs_iocontrol_pool(const struct lu_env *env,
807 struct mgs_device *mgs,
808 struct obd_ioctl_data *data)
810 struct mgs_thread_info *mgi = mgs_env_info(env);
812 struct lustre_cfg *lcfg = NULL;
813 char *poolname = NULL;
816 OBD_ALLOC(poolname, LOV_MAXPOOLNAME + 1);
817 if (poolname == NULL)
820 if (data->ioc_type != LUSTRE_CFG_TYPE) {
821 CERROR("%s: unknown cfg record type: %d\n",
822 mgs->mgs_obd->obd_name, data->ioc_type);
823 GOTO(out_pool, rc = -EINVAL);
826 if (data->ioc_plen1 > CFS_PAGE_SIZE)
827 GOTO(out_pool, rc = -E2BIG);
829 OBD_ALLOC(lcfg, data->ioc_plen1);
831 GOTO(out_pool, rc = -ENOMEM);
833 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
834 GOTO(out_lcfg, rc = -EFAULT);
836 if (lcfg->lcfg_bufcount < 2)
837 GOTO(out_lcfg, rc = -EFAULT);
839 /* first arg is always <fsname>.<poolname> */
840 rc = mgs_extract_fs_pool(lustre_cfg_string(lcfg, 1), mgi->mgi_fsname,
845 switch (lcfg->lcfg_command) {
847 if (lcfg->lcfg_bufcount != 2)
848 GOTO(out_lcfg, rc = -EINVAL);
849 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_NEW, mgi->mgi_fsname,
853 if (lcfg->lcfg_bufcount != 3)
854 GOTO(out_lcfg, rc = -EINVAL);
855 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_ADD, mgi->mgi_fsname,
856 poolname, lustre_cfg_string(lcfg, 2));
859 if (lcfg->lcfg_bufcount != 3)
860 GOTO(out_lcfg, rc = -EINVAL);
861 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_REM, mgi->mgi_fsname,
862 poolname, lustre_cfg_string(lcfg, 2));
865 if (lcfg->lcfg_bufcount != 2)
866 GOTO(out_lcfg, rc = -EINVAL);
867 rc = mgs_pool_cmd(env, mgs, LCFG_POOL_DEL, mgi->mgi_fsname,
875 CERROR("OBD_IOC_POOL err %d, cmd %X for pool %s.%s\n",
876 rc, lcfg->lcfg_command, mgi->mgi_fsname, poolname);
881 OBD_FREE(lcfg, data->ioc_plen1);
883 OBD_FREE(poolname, LOV_MAXPOOLNAME + 1);
887 /* from mdt_iocontrol */
888 int mgs_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
889 void *karg, void *uarg)
891 struct mgs_device *mgs = exp2mgs_dev(exp);
892 struct obd_ioctl_data *data = karg;
897 CDEBUG(D_IOCTL, "handling ioctl cmd %#x\n", cmd);
899 rc = lu_env_init(&env, LCT_MG_THREAD);
905 case OBD_IOC_PARAM: {
906 struct mgs_thread_info *mgi = mgs_env_info(&env);
907 struct lustre_cfg *lcfg;
909 if (data->ioc_type != LUSTRE_CFG_TYPE) {
910 CERROR("%s: unknown cfg record type: %d\n",
911 mgs->mgs_obd->obd_name, data->ioc_type);
912 GOTO(out, rc = -EINVAL);
915 OBD_ALLOC(lcfg, data->ioc_plen1);
917 GOTO(out, rc = -ENOMEM);
918 if (cfs_copy_from_user(lcfg, data->ioc_pbuf1, data->ioc_plen1))
919 GOTO(out_free, rc = -EFAULT);
921 if (lcfg->lcfg_bufcount < 1)
922 GOTO(out_free, rc = -EINVAL);
924 rc = mgs_setparam(&env, mgs, lcfg, mgi->mgi_fsname);
926 CERROR("%s: setparam err: rc = %d\n",
927 exp->exp_obd->obd_name, rc);
929 OBD_FREE(lcfg, data->ioc_plen1);
934 rc = mgs_iocontrol_pool(&env, mgs, data);
937 case OBD_IOC_DUMP_LOG: {
938 struct llog_ctxt *ctxt;
940 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
941 rc = class_config_dump_llog(&env, ctxt, data->ioc_inlbuf1,
948 case OBD_IOC_LLOG_CANCEL:
949 case OBD_IOC_LLOG_REMOVE:
950 case OBD_IOC_LLOG_CHECK:
951 case OBD_IOC_LLOG_INFO:
952 case OBD_IOC_LLOG_PRINT: {
953 struct llog_ctxt *ctxt;
955 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
956 rc = llog_ioctl(&env, ctxt, cmd, data);
962 CERROR("%s: unknown command %#x\n",
963 mgs->mgs_obd->obd_name, cmd);
972 static int mgs_connect_to_osd(struct mgs_device *m, const char *nextdev)
974 struct obd_connect_data *data = NULL;
975 struct obd_device *obd;
983 obd = class_name2obd(nextdev);
985 CERROR("can't locate next device: %s\n", nextdev);
986 GOTO(out, rc = -ENOTCONN);
989 data->ocd_version = LUSTRE_VERSION_CODE;
991 rc = obd_connect(NULL, &m->mgs_bottom_exp, obd,
992 &obd->obd_uuid, data, NULL);
994 CERROR("cannot connect to next dev %s (%d)\n", nextdev, rc);
998 m->mgs_bottom = lu2dt_dev(m->mgs_bottom_exp->exp_obd->obd_lu_dev);
999 m->mgs_dt_dev.dd_lu_dev.ld_site = m->mgs_bottom->dd_lu_dev.ld_site;
1000 LASSERT(m->mgs_dt_dev.dd_lu_dev.ld_site);
1007 static int mgs_init0(const struct lu_env *env, struct mgs_device *mgs,
1008 struct lu_device_type *ldt, struct lustre_cfg *lcfg)
1010 static struct ptlrpc_service_conf conf;
1011 struct lprocfs_static_vars lvars = { 0 };
1012 struct obd_device *obd;
1013 struct lustre_mount_info *lmi;
1014 struct llog_ctxt *ctxt;
1019 lmi = server_get_mount(lustre_cfg_string(lcfg, 0));
1023 mgs->mgs_dt_dev.dd_lu_dev.ld_ops = &mgs_lu_ops;
1025 rc = mgs_connect_to_osd(mgs, lustre_cfg_string(lcfg, 3));
1029 obd = class_name2obd(lustre_cfg_string(lcfg, 0));
1032 mgs->mgs_obd->obd_lu_dev = &mgs->mgs_dt_dev.dd_lu_dev;
1034 obd->u.obt.obt_magic = OBT_MAGIC;
1035 obd->u.obt.obt_instance = 0;
1037 /* namespace for mgs llog */
1038 obd->obd_namespace = ldlm_namespace_new(obd ,"MGS",
1039 LDLM_NAMESPACE_SERVER,
1040 LDLM_NAMESPACE_MODEST,
1042 if (obd->obd_namespace == NULL)
1043 GOTO(err_ops, rc = -ENOMEM);
1046 ptlrpc_init_client(LDLM_CB_REQUEST_PORTAL, LDLM_CB_REPLY_PORTAL,
1047 "mgs_ldlm_client", &obd->obd_ldlm_client);
1049 rc = mgs_fs_setup(env, mgs);
1051 CERROR("%s: MGS filesystem method init failed: rc = %d\n",
1056 rc = llog_setup(env, obd, &obd->obd_olg, LLOG_CONFIG_ORIG_CTXT,
1057 obd, &llog_osd_ops);
1061 /* XXX: we need this trick till N:1 stack is supported
1062 * set "current" directory for named llogs */
1063 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1065 ctxt->loc_dir = mgs->mgs_configs_dir;
1066 llog_ctxt_put(ctxt);
1068 /* No recovery for MGC's */
1069 obd->obd_replayable = 0;
1071 /* Internal mgs setup */
1072 mgs_init_fsdb_list(mgs);
1073 mutex_init(&mgs->mgs_mutex);
1074 mgs->mgs_start_time = cfs_time_current_sec();
1075 spin_lock_init(&mgs->mgs_lock);
1078 lprocfs_mgs_init_vars(&lvars);
1079 if (lprocfs_obd_setup(obd, lvars.obd_vars) == 0) {
1080 lproc_mgs_setup(mgs, lustre_cfg_string(lcfg, 3));
1081 rc = lprocfs_alloc_md_stats(obd, LPROC_MGS_LAST);
1086 conf = (typeof(conf)) {
1087 .psc_name = LUSTRE_MGS_NAME,
1088 .psc_watchdog_factor = MGS_SERVICE_WATCHDOG_FACTOR,
1090 .bc_nbufs = MGS_NBUFS,
1091 .bc_buf_size = MGS_BUFSIZE,
1092 .bc_req_max_size = MGS_MAXREQSIZE,
1093 .bc_rep_max_size = MGS_MAXREPSIZE,
1094 .bc_req_portal = MGS_REQUEST_PORTAL,
1095 .bc_rep_portal = MGC_REPLY_PORTAL,
1098 .tc_thr_name = "ll_mgs",
1099 .tc_nthrs_init = MGS_NTHRS_INIT,
1100 .tc_nthrs_max = MGS_NTHRS_MAX,
1101 .tc_ctx_tags = LCT_MG_THREAD,
1104 .so_req_handler = mgs_handle,
1105 .so_req_printer = target_print_req,
1108 /* Start the service threads */
1109 mgs->mgs_service = ptlrpc_register_service(&conf, obd->obd_proc_entry);
1110 if (IS_ERR(mgs->mgs_service)) {
1111 rc = PTR_ERR(mgs->mgs_service);
1112 CERROR("failed to start service: %d\n", rc);
1113 GOTO(err_lproc, rc);
1116 ping_evictor_start();
1118 CDEBUG(D_INFO, "MGS %s started\n", obd->obd_name);
1120 /* device stack is not yet fully setup to keep no objects behind */
1121 lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1125 lproc_mgs_cleanup(mgs);
1127 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1129 ctxt->loc_dir = NULL;
1130 llog_cleanup(env, ctxt);
1133 /* No extra cleanup needed for llog_init_commit_thread() */
1134 mgs_fs_cleanup(env, mgs);
1136 ldlm_namespace_free(obd->obd_namespace, NULL, 0);
1137 obd->obd_namespace = NULL;
1139 lu_site_purge(env, mgs2lu_dev(mgs)->ld_site, ~0);
1140 if (!cfs_hash_is_empty(mgs2lu_dev(mgs)->ld_site->ls_obj_hash)) {
1141 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1142 lu_site_print(env, mgs2lu_dev(mgs)->ld_site, &msgdata,
1145 obd_disconnect(mgs->mgs_bottom_exp);
1149 static struct lu_device *mgs_device_free(const struct lu_env *env,
1150 struct lu_device *lu)
1152 struct mgs_device *mgs = lu2mgs_dev(lu);
1155 dt_device_fini(&mgs->mgs_dt_dev);
1160 static int mgs_process_config(const struct lu_env *env,
1161 struct lu_device *dev,
1162 struct lustre_cfg *lcfg)
1168 static int mgs_object_init(const struct lu_env *env, struct lu_object *o,
1169 const struct lu_object_conf *unused)
1171 struct mgs_device *d = lu2mgs_dev(o->lo_dev);
1172 struct lu_device *under;
1173 struct lu_object *below;
1177 /* do no set .do_ops as mgs calls to bottom osd directly */
1179 CDEBUG(D_INFO, "object init, fid = "DFID"\n",
1180 PFID(lu_object_fid(o)));
1182 under = &d->mgs_bottom->dd_lu_dev;
1183 below = under->ld_ops->ldo_object_alloc(env, o->lo_header, under);
1185 lu_object_add(o, below);
1192 static void mgs_object_free(const struct lu_env *env, struct lu_object *o)
1194 struct mgs_object *obj = lu2mgs_obj(o);
1195 struct lu_object_header *h = o->lo_header;
1197 dt_object_fini(&obj->mgo_obj);
1198 lu_object_header_fini(h);
1202 static int mgs_object_print(const struct lu_env *env, void *cookie,
1203 lu_printer_t p, const struct lu_object *l)
1205 const struct mgs_object *o = lu2mgs_obj((struct lu_object *) l);
1207 return (*p)(env, cookie, LUSTRE_MGS_NAME"-object@%p", o);
1210 struct lu_object_operations mgs_lu_obj_ops = {
1211 .loo_object_init = mgs_object_init,
1212 .loo_object_free = mgs_object_free,
1213 .loo_object_print = mgs_object_print,
1216 struct lu_object *mgs_object_alloc(const struct lu_env *env,
1217 const struct lu_object_header *hdr,
1218 struct lu_device *d)
1220 struct lu_object_header *h;
1221 struct mgs_object *o;
1222 struct lu_object *l;
1224 LASSERT(hdr == NULL);
1228 l = &o->mgo_obj.do_lu;
1231 lu_object_header_init(h);
1232 dt_object_init(&o->mgo_obj, h, d);
1233 lu_object_add_top(h, l);
1235 l->lo_ops = &mgs_lu_obj_ops;
1243 const struct lu_device_operations mgs_lu_ops = {
1244 .ldo_object_alloc = mgs_object_alloc,
1245 .ldo_process_config = mgs_process_config,
1248 static struct lu_device *mgs_device_alloc(const struct lu_env *env,
1249 struct lu_device_type *type,
1250 struct lustre_cfg *lcfg)
1252 struct mgs_device *mgs;
1253 struct lu_device *ludev;
1257 ludev = ERR_PTR(-ENOMEM);
1261 ludev = mgs2lu_dev(mgs);
1262 dt_device_init(&mgs->mgs_dt_dev, type);
1263 rc = mgs_init0(env, mgs, type, lcfg);
1265 mgs_device_free(env, ludev);
1266 ludev = ERR_PTR(rc);
1272 static struct lu_device *mgs_device_fini(const struct lu_env *env,
1273 struct lu_device *d)
1275 struct mgs_device *mgs = lu2mgs_dev(d);
1276 struct obd_device *obd = mgs->mgs_obd;
1277 struct llog_ctxt *ctxt;
1281 LASSERT(mgs->mgs_bottom);
1283 ping_evictor_stop();
1285 ptlrpc_unregister_service(mgs->mgs_service);
1287 obd_exports_barrier(obd);
1288 obd_zombie_barrier();
1290 mgs_cleanup_fsdb_list(mgs);
1291 lproc_mgs_cleanup(mgs);
1293 ctxt = llog_get_context(mgs->mgs_obd, LLOG_CONFIG_ORIG_CTXT);
1295 ctxt->loc_dir = NULL;
1296 llog_cleanup(env, ctxt);
1299 mgs_fs_cleanup(env, mgs);
1301 ldlm_namespace_free(obd->obd_namespace, NULL, 1);
1302 obd->obd_namespace = NULL;
1304 lu_site_purge(env, d->ld_site, ~0);
1305 if (!cfs_hash_is_empty(d->ld_site->ls_obj_hash)) {
1306 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
1307 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
1310 LASSERT(mgs->mgs_bottom_exp);
1311 obd_disconnect(mgs->mgs_bottom_exp);
1313 server_put_mount(obd->obd_name, NULL);
1318 /* context key constructor/destructor: mgs_key_init, mgs_key_fini */
1319 LU_KEY_INIT_FINI(mgs, struct mgs_thread_info);
1321 LU_TYPE_INIT_FINI(mgs, &mgs_thread_key);
1323 LU_CONTEXT_KEY_DEFINE(mgs, LCT_MG_THREAD);
1325 static struct lu_device_type_operations mgs_device_type_ops = {
1326 .ldto_init = mgs_type_init,
1327 .ldto_fini = mgs_type_fini,
1329 .ldto_start = mgs_type_start,
1330 .ldto_stop = mgs_type_stop,
1332 .ldto_device_alloc = mgs_device_alloc,
1333 .ldto_device_free = mgs_device_free,
1335 .ldto_device_fini = mgs_device_fini
1338 static struct lu_device_type mgs_device_type = {
1339 .ldt_tags = LU_DEVICE_DT,
1340 .ldt_name = LUSTRE_MGS_NAME,
1341 .ldt_ops = &mgs_device_type_ops,
1342 .ldt_ctx_tags = LCT_MG_THREAD
1346 /* use obd ops to offer management infrastructure */
1347 static struct obd_ops mgs_obd_ops = {
1348 .o_owner = THIS_MODULE,
1349 .o_connect = mgs_connect,
1350 .o_reconnect = mgs_reconnect,
1351 .o_disconnect = mgs_disconnect,
1352 .o_init_export = mgs_init_export,
1353 .o_destroy_export = mgs_destroy_export,
1354 .o_iocontrol = mgs_iocontrol,
1357 static int __init mgs_init(void)
1359 struct lprocfs_static_vars lvars;
1361 lprocfs_mgs_init_vars(&lvars);
1362 class_register_type(&mgs_obd_ops, NULL, lvars.module_vars,
1363 LUSTRE_MGS_NAME, &mgs_device_type);
1368 static void /*__exit*/ mgs_exit(void)
1370 class_unregister_type(LUSTRE_MGS_NAME);
1373 MODULE_AUTHOR("Sun Microsystems, Inc. <http://www.lustre.org/>");
1374 MODULE_DESCRIPTION("Lustre Management Server (MGS)");
1375 MODULE_LICENSE("GPL");
1377 module_init(mgs_init);
1378 module_exit(mgs_exit);