Whamcloud - gitweb
0dff68971195c80fdd7331649dedf8cd11ee46bc
[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_handler(struct ptlrpc_request *req)
53 {
54         int rc;
55         ENTRY;
56
57         switch (req->rq_reqmsg->opc) {
58         case OBD_PING:
59                 DEBUG_REQ(D_RPCTRACE, req, "ping");
60                 rc = mgmt_ping(req);
61                 break;
62         case MGMT_CONNECT:
63                 DEBUG_REQ(D_RPCTRACE, req, "connect");
64                 rc = target_handle_connect(req);
65                 break;
66         case MGMT_DISCONNECT:
67                 DEBUG_REQ(D_RPCTRACE, req, "disconnect");
68                 rc = target_handle_disconnect(req);
69                 break;
70         default:
71                 DEBUG_REQ(D_RPCTRACE, req, "UNKNOWN OP");
72                 rc = -EINVAL;
73         }
74
75         if (rc)
76                 ptlrpc_error(req);
77         else
78                 ptlrpc_reply(req);
79
80         RETURN(0);
81 }
82
83 static int mgmt_setup(struct obd_device *obd, obd_count len, void *buf)
84 {
85         int i, rc;
86         ENTRY;
87
88         if (mgmt_initialized)
89                 RETURN(-EALREADY);
90         
91         mgmt_service = 
92                 ptlrpc_init_svc(MGMT_NBUFS, MGMT_BUFSIZE, MGMT_MAXREQSIZE, 
93                                 MGMT_REQUEST_PORTAL, MGMT_REPLY_PORTAL, 
94                                 mgmt_handler, "mgmt",
95                                 obd->obd_proc_entry);
96         if (!mgmt_service) {
97                 CERROR("Failed to start mgmt service\n");
98                 RETURN(-ENOMEM);
99         }
100
101         rc = ptlrpc_start_n_threads(obd, mgmt_service, MGMT_NUM_THREADS,"mgmt");
102         if (rc)
103                 GOTO(err, rc);
104                 
105         mgmt_initialized = 1;
106         
107         RETURN(0);
108 err:
109         ptlrpc_unregister_service(mgmt_service);
110         RETURN(rc):
111 }
112
113 static int mgmt_cleanup(struct obd_device *obd, int flags)
114 {
115         ENTRY;
116         
117         if (!mgmt_initialized)
118                 RETURN(-ENOENT);
119
120         ptlrpc_stop_all_threads(mgmt_service);
121         ptlrpc_unregister_service(mgmt_service);
122         
123         mgmt_initialized = 0;
124         RETURN(0);
125 }
126
127 static struct obd_ops mgmt_obd_ops = {
128         .o_owner      = THIS_MODULE,
129         .o_setup      = mgmt_setup,
130         .o_cleanup    = mgmt_cleanup,
131         .o_connect    = class_connect,
132         .o_disconnect = class_disconnect
133 };
134
135 static int __init mgmt_init(void)
136 {
137         int rc = class_register_type(&mgmt_obd_ops, 0, MGMT_DEVICE_NAME);
138
139         return rc;
140 }
141
142 static void __exit mgmt_exit(void)
143 {
144         class_unregister_type(MGMT_DEVICE_NAME);
145 }
146
147 #ifdef __KERNEL__
148 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
149 MODULE_DESCRIPTION("Lustre monitoring service v0.1");
150 MODULE_LICENSE("GPL");
151
152 module_init(mgmt_init);
153 module_exit(mgmt_exit);
154 #endif