Whamcloud - gitweb
land v0.9.1 on HEAD, in preparation for a 1.0.x branch
[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_NEVENTS     1024UL
36 #define MGMT_NBUFS       128UL
37 #define MGMT_BUFSIZE     8192
38 #define MGMT_MAXREQSIZE  512
39 #define MGMT_NUM_THREADS 4
40 #define MGMT_DEVICE_NAME "mgmt"
41
42 static int mgmt_initialized;
43 static struct ptlrpc_service *mgmt_service;
44
45 static int mgmt_ping(struct ptlrpc_request *req)
46 {
47         /* handle_incoming_request will have already updated the export's
48          * last_request_time, so we don't need to do anything else.
49          */
50         return lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
51 }
52
53 static int mgmt_handler(struct ptlrpc_request *req)
54 {
55         int rc;
56         ENTRY;
57
58         switch (req->rq_reqmsg->opc) {
59         case OBD_PING:
60                 DEBUG_REQ(D_RPCTRACE, req, "ping");
61                 rc = mgmt_ping(req);
62                 break;
63         case MGMT_CONNECT:
64                 DEBUG_REQ(D_RPCTRACE, req, "connect");
65                 rc = target_handle_connect(req, NULL /* no recovery handler */);
66                 break;
67         case MGMT_DISCONNECT:
68                 DEBUG_REQ(D_RPCTRACE, req, "disconnect");
69                 rc = target_handle_disconnect(req);
70                 break;
71         default:
72                 DEBUG_REQ(D_RPCTRACE, req, "UNKNOWN OP");
73                 rc = -EINVAL;
74         }
75
76         if (rc)
77                 ptlrpc_error(req);
78         else
79                 ptlrpc_reply(req);
80
81         RETURN(0);
82 }
83
84 static int mgmt_setup(struct obd_device *obd, obd_count len, void *buf)
85 {
86         int i, rc;
87         ENTRY;
88
89         if (mgmt_initialized)
90                 RETURN(-EALREADY);
91         
92         mgmt_service = ptlrpc_init_svc(MGMT_NEVENTS, MGMT_NBUFS, MGMT_BUFSIZE,
93                                        MGMT_MAXREQSIZE, MGMT_REQUEST_PORTAL,
94                                        MGMT_REPLY_PORTAL, mgmt_handler,
95                                        "mgmt", 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