Whamcloud - gitweb
- fixed possible access to of bounds in cmobd
[fs/lustre-release.git] / lustre / cmobd / cm_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #define DEBUG_SUBSYSTEM S_CMOBD
23
24 #include <linux/version.h>
25 #include <linux/init.h>
26 #include <linux/obd_support.h>
27 #include <linux/lustre_lib.h>
28 #include <linux/lustre_net.h>
29 #include <linux/lustre_idl.h>
30 #include <linux/obd_class.h>
31 #include <linux/lustre_mds.h>
32 #include <linux/lustre_cmobd.h>
33 #include <linux/lprocfs_status.h>
34 #include <linux/obd_lov.h>
35 #include <linux/obd_ost.h>
36 #include <linux/obd_lmv.h>
37
38 #include "cm_internal.h"
39
40 static int cmobd_attach(struct obd_device *obd,
41                         obd_count len, void *data)
42 {
43         struct lprocfs_static_vars lvars;
44
45         lprocfs_init_vars(cmobd, &lvars);
46         return lprocfs_obd_attach(obd, lvars.obd_vars);
47 }
48
49 static int cmobd_detach(struct obd_device *obd)
50 {
51         return lprocfs_obd_detach(obd);
52 }
53
54 static inline int cmobd_md_obd(struct obd_device *obd)
55 {
56         if (!strcmp(obd->obd_type->typ_name, OBD_MDC_DEVICENAME) ||
57             !strcmp(obd->obd_type->typ_name, OBD_LMV_DEVICENAME))
58                 return 1;
59
60         return 0;
61 }
62
63 static inline int cmobd_dt_obd(struct obd_device *obd)
64 {
65         if (!strcmp(obd->obd_type->typ_name, OBD_LOV_DEVICENAME) ||
66             !strcmp(obd->obd_type->typ_name, OBD_OSC_DEVICENAME))
67                 return 1;
68
69         return 0;
70 }
71
72 static void cmobd_init_ea_size(struct obd_device *obd)
73 {
74         int ost_count = 1, easize, cookiesize;
75         struct cm_obd *cmobd = &obd->u.cm;
76         ENTRY;
77
78         /*
79          * here we should also take into account that there is possible to have
80          * few OSTs. --umka
81          */
82         easize = lov_mds_md_size(ost_count);
83         cookiesize = ost_count * sizeof(struct llog_cookie);
84
85         obd_init_ea_size(cmobd->master_exp, easize, cookiesize);
86
87         cmobd->master_exp->exp_obd->u.cli.cl_max_mds_easize = easize;
88         cmobd->master_exp->exp_obd->u.cli.cl_max_mds_cookiesize = cookiesize;
89         
90         EXIT;
91 }
92
93 static char *types[] = {
94         OBD_LMV_DEVICENAME, OBD_MDC_DEVICENAME,
95         OBD_LOV_DEVICENAME, OBD_OSC_DEVICENAME
96 };
97
98 static struct obd_device *
99 cmobd_find_obd(struct obd_device *obd, struct obd_uuid *uuid)
100 {
101         struct obd_device *res;
102         int i = 0;
103         ENTRY;
104
105         CWARN("%s: looking for client obd %s\n",
106               obd->obd_uuid.uuid, uuid->uuid);
107
108         for (i = 0; i < sizeof(types) / sizeof(char *); i++) {
109                 res = class_find_client_obd(NULL, types[i], uuid);
110                 if (res)
111                         RETURN(res);
112         }
113         RETURN(NULL);
114 }
115
116 static int cmobd_setup(struct obd_device *obd, obd_count len, void *buf)
117 {
118         struct obd_uuid master_uuid, cache_uuid;
119         struct lustre_handle conn = { 0 };
120         struct cm_obd *cmobd = &obd->u.cm;
121         struct lustre_cfg* lcfg = buf;
122         struct lustre_id mid, lid;
123         __u32 valsize;
124         int rc;
125         ENTRY;
126
127         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
128                 CERROR("%s: setup requires master device uuid\n", 
129                        obd->obd_name);
130                 RETURN(-EINVAL);
131         }
132
133         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
134                 CERROR("%s: setup requires cache device uuid\n",
135                        obd->obd_name);
136                 RETURN(-EINVAL);
137         }
138
139         obd_str2uuid(&master_uuid, lustre_cfg_string(lcfg, 1));
140         obd_str2uuid(&cache_uuid, lustre_cfg_string(lcfg, 2));
141
142         /* getting master obd */
143         cmobd->master_obd = cmobd_find_obd(obd, &master_uuid);
144         if (!cmobd->master_obd) {
145                 CERROR("can't find master client obd by uuid %s\n",
146                        master_uuid.uuid);
147                 RETURN(-EINVAL);
148         }
149
150         /* getting cache obd */
151         cmobd->cache_obd = class_uuid2obd(&cache_uuid);
152         if (cmobd->cache_obd == NULL) {
153                 CERROR("CMOBD: unable to find obd by uuid: %s\n",
154                        cache_uuid.uuid);
155                 RETURN(-EINVAL);
156         }
157
158         /* connecting master */
159         memset(&conn, 0, sizeof(conn));
160         rc = obd_connect(&conn, cmobd->master_obd, &obd->obd_uuid, 
161                          NULL, OBD_OPT_REAL_CLIENT);
162         if (rc)
163                 RETURN(rc);
164         cmobd->master_exp = class_conn2export(&conn);
165
166         /* connecting cache */
167         memset(&conn, 0, sizeof(conn));
168         rc = class_connect(&conn, cmobd->cache_obd, &obd->obd_uuid);
169         if (rc)
170                 GOTO(put_master, rc);
171         cmobd->cache_exp = class_conn2export(&conn);
172         
173         if (cmobd_dt_obd(cmobd->master_exp->exp_obd)) {
174                 /* for master dt device remove the recovery flag. */
175                 rc = obd_set_info(cmobd->master_exp, strlen("unrecovery"),
176                                   "unrecovery", 0, NULL); 
177                 if (rc)
178                         GOTO(put_cache, rc);
179                 
180                 rc = cmobd_init_write_srv(obd);
181                 if (rc)
182                         GOTO(put_cache, rc);
183         } else {
184                 cmobd_init_ea_size(obd);
185                 cmobd->write_srv = NULL;
186         }
187
188         if (cmobd_md_obd(cmobd->master_exp->exp_obd)) {
189                 /*
190                  * making sure, that both obds are ready. This is especially
191                  * important in the case of using LMV as master.
192                  */
193                 rc = obd_getready(cmobd->master_exp);
194                 if (rc) {
195                         CERROR("can't get %s obd ready.\n",
196                                master_uuid.uuid);
197                         GOTO(put_cache, rc);
198                 }
199         
200                 rc = obd_getready(cmobd->cache_exp);
201                 if (rc) {
202                         CERROR("can't get %s obd ready.\n",
203                                cache_uuid.uuid);
204                         GOTO(put_cache, rc);
205                 }
206         
207                 /*
208                  * requesting master obd to have its root inode store cookie to
209                  * be able to save it to local root inode EA.
210                  */
211                 valsize = sizeof(struct lustre_id);
212         
213                 rc = obd_get_info(cmobd->master_exp, strlen("rootid"),
214                                   "rootid", &valsize, &mid);
215                 if (rc) {
216                         CERROR("can't get rootid from master MDS %s, "
217                                "err= %d.\n", master_uuid.uuid, rc);
218                         GOTO(put_cache, rc);
219                 }
220
221                 /*
222                  * getting rootid from cache MDS. It is needed to update local
223                  * (cache) root inode by rootid value from master obd.
224                  */
225                 rc = obd_get_info(cmobd->cache_exp, strlen("rootid"),
226                                   "rootid", &valsize, &lid);
227                 if (rc) {
228                         CERROR("can't get rootid from local MDS %s, "
229                                "err= %d.\n", cache_uuid.uuid, rc);
230                         GOTO(put_cache, rc);
231                 }
232
233                 /* storing master MDS rootid to local root inode EA. */
234                 CWARN("storing "DLID4" to local inode "DLID4".\n",
235                       OLID4(&mid), OLID4(&lid));
236
237                 rc = mds_update_mid(cmobd->cache_exp->exp_obd, &lid,
238                                     &mid, sizeof(mid));
239                 if (rc) {
240                         CERROR("can't update local root inode by ID "
241                                "from master MDS %s, err = %d.\n",
242                                master_uuid.uuid, rc);
243                         GOTO(put_cache, rc);
244                 }
245         }
246
247         RETURN(rc);
248 put_cache:
249         class_disconnect(cmobd->cache_exp, 0);
250 put_master:
251         obd_disconnect(cmobd->master_exp, 0);
252         return rc;
253 }
254
255 static int cmobd_cleanup(struct obd_device *obd, int flags)
256 {
257         struct cm_obd *cmobd = &obd->u.cm;
258         int rc;
259         ENTRY;
260
261         if (cmobd->write_srv)
262                 cmobd_cleanup_write_srv(obd);
263
264         rc = obd_disconnect(cmobd->master_exp, flags);
265         if (rc) {
266                 CERROR("error disconnecting master, err %d\n",
267                        rc);
268         }
269         
270         rc = class_disconnect(cmobd->cache_exp, flags);
271         if (rc) {
272                 CERROR("error disconnecting cache, err %d\n",
273                        rc);
274         }
275         
276         RETURN(0);
277 }
278
279 static int cmobd_iocontrol(unsigned int cmd, struct obd_export *exp,
280                            int len, void *karg, void *uarg)
281 {
282         struct obd_device *obd = exp->exp_obd;
283         int rc = 0;
284         ENTRY;
285         
286         switch (cmd) {
287         case OBD_IOC_CMOBD_SYNC: /* trigger reintegration */
288                 rc = cmobd_reintegrate(obd);
289                 break;
290         default:
291                 CERROR("unrecognized ioctl %#x\n", cmd);
292                 rc = -EINVAL;
293                 break;
294         }
295                 
296         RETURN(rc);
297 }
298
299 static struct obd_ops cmobd_ops = {
300         .o_owner     = THIS_MODULE,
301         .o_attach    = cmobd_attach,
302         .o_detach    = cmobd_detach,
303         .o_setup     = cmobd_setup,
304         .o_cleanup   = cmobd_cleanup,
305         .o_iocontrol = cmobd_iocontrol,
306 };
307
308 kmem_cache_t *cmobd_extent_slab;
309
310 static int __init cmobd_init(void)
311 {
312         struct lprocfs_static_vars lvars;
313         int rc;
314         ENTRY;
315
316         printk(KERN_INFO "Lustre: Cache Manager OBD driver; info@clusterfs.com\n");
317
318         lprocfs_init_vars(cmobd, &lvars);
319         rc = class_register_type(&cmobd_ops, NULL, lvars.module_vars,
320                                  OBD_CMOBD_DEVICENAME);
321         if (rc)
322                 RETURN(rc);
323         cmobd_extent_slab = kmem_cache_create("cmobd_extents",
324                                                sizeof(struct cmobd_extent_info), 0,
325                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
326         if (cmobd_extent_slab == NULL) {
327                 class_unregister_type(OBD_CMOBD_DEVICENAME);
328                 RETURN(-ENOMEM);
329         }
330         RETURN(0);
331 }
332
333 static void __exit cmobd_exit(void)
334 {
335         class_unregister_type(OBD_CMOBD_DEVICENAME);
336         if (kmem_cache_destroy(cmobd_extent_slab) != 0)
337                 CERROR("couldn't free cmobd extent slab\n");
338 }
339
340 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
341 MODULE_DESCRIPTION("Lustre Cache Manager OBD driver");
342 MODULE_LICENSE("GPL");
343
344 module_init(cmobd_init);
345 module_exit(cmobd_exit);