Whamcloud - gitweb
Branch: HEAD
[fs/lustre-release.git] / lustre / mgmt / mgmt_svc.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Implementation of the management/health monitoring service.
5  *
6  *  Copyright (c) 2003 Cluster File Systems, Inc.
7  *   Author: Mike Shaver <shaver@clusterfs.com>
8  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MGMT
29 #include <linux/module.h>
30 #include <linux/init.h>
31
32 #include <linux/obd_class.h>
33 #include <linux/lustre_net.h>
34
35 #define MGMT_NBUFS       128UL
36 #define MGMT_BUFSIZE     8192
37 #define MGMT_MAXREQSIZE  512
38 #define MGMT_NUM_THREADS 4
39 #define MGMT_DEVICE_NAME "mgmt"
40
41 static int mgmt_initialized;
42 static struct ptlrpc_service *mgmt_service;
43
44 static int mgmt_ping(struct ptlrpc_request *req)
45 {
46         /* handle_incoming_request will have already updated the export's
47          * last_request_time, so we don't need to do anything else.
48          */
49         return lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
50 }
51
52 static int mgmt_msg_check_version(struct lustre_msg *msg)
53 {
54         if (lustre_msg_check_version(msg, LUSTRE_OBD_VERSION)) {
55                 CERROR("bad opc %u version %08x, expecting %08x\n",
56                        msg->opc, msg->version, LUSTRE_OBD_VERSION);
57                 return -EINVAL;
58         }
59
60         return 0;
61 }
62
63 static int mgmt_handler(struct ptlrpc_request *req)
64 {
65         int rc;
66         ENTRY;
67
68         rc = mgmt_msg_check_version(req->rq_reqmsg);
69         if (rc) {
70                 CERROR("MGMT drop mal-formed request\n");
71                 RETURN(rc);
72         }
73
74         switch (req->rq_reqmsg->opc) {
75         case OBD_PING:
76                 DEBUG_REQ(D_RPCTRACE, req, "ping");
77                 rc = mgmt_ping(req);
78                 break;
79         case MGMT_CONNECT:
80                 DEBUG_REQ(D_RPCTRACE, req, "connect");
81                 rc = target_handle_connect(req);
82                 break;
83         case MGMT_DISCONNECT:
84                 DEBUG_REQ(D_RPCTRACE, req, "disconnect");
85                 rc = target_handle_disconnect(req);
86                 break;
87         default:
88                 DEBUG_REQ(D_RPCTRACE, req, "UNKNOWN OP");
89                 rc = -EINVAL;
90         }
91
92         if (rc)
93                 ptlrpc_error(req);
94         else
95                 ptlrpc_reply(req);
96
97         RETURN(0);
98 }
99
100 static int mgmt_setup(struct obd_device *obd, obd_count len, void *buf)
101 {
102         int i, rc;
103         ENTRY;
104
105         if (mgmt_initialized)
106                 RETURN(-EALREADY);
107         
108         mgmt_service = 
109                 ptlrpc_init_svc(MGMT_NBUFS, MGMT_BUFSIZE, MGMT_MAXREQSIZE,
110                                 MGMT_REQUEST_PORTAL, MGMT_REPLY_PORTAL, 30000,
111                                 mgmt_handler, "mgmt", obd->obd_proc_entry);
112         if (!mgmt_service) {
113                 CERROR("Failed to start mgmt service\n");
114                 RETURN(-ENOMEM);
115         }
116
117         rc = ptlrpc_start_n_threads(obd, mgmt_service, MGMT_NUM_THREADS,"mgmt");
118         if (rc)
119                 GOTO(err, rc);
120                 
121         mgmt_initialized = 1;
122         
123         RETURN(0);
124 err:
125         ptlrpc_unregister_service(mgmt_service);
126         RETURN(rc):
127 }
128
129 static int mgmt_cleanup(struct obd_device *obd, int flags)
130 {
131         ENTRY;
132         
133         if (!mgmt_initialized)
134                 RETURN(-ENOENT);
135
136         ptlrpc_stop_all_threads(mgmt_service);
137         ptlrpc_unregister_service(mgmt_service);
138         
139         mgmt_initialized = 0;
140         RETURN(0);
141 }
142
143 static struct obd_ops mgmt_obd_ops = {
144         .o_owner      = THIS_MODULE,
145         .o_setup      = mgmt_setup,
146         .o_cleanup    = mgmt_cleanup,
147         .o_connect    = class_connect,
148         .o_disconnect = class_disconnect
149 };
150
151 static int __init mgmt_init(void)
152 {
153         int rc = class_register_type(&mgmt_obd_ops, NULL, 
154                                      0, MGMT_DEVICE_NAME);
155
156         return rc;
157 }
158
159 static void __exit mgmt_exit(void)
160 {
161         class_unregister_type(MGMT_DEVICE_NAME);
162 }
163
164 #ifdef __KERNEL__
165 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
166 MODULE_DESCRIPTION("Lustre monitoring service v0.1");
167 MODULE_LICENSE("GPL");
168
169 module_init(mgmt_init);
170 module_exit(mgmt_exit);
171 #endif