Whamcloud - gitweb
b=7200
[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 int cmobd_init_dt_desc(struct obd_device *obd)
73 {
74         struct cm_obd *cmobd = &obd->u.cm;
75         __u32 valsize;
76         int rc = 0;
77         ENTRY;
78         
79         /* as CMOBD is stand alone device, that is has not to be connected, we
80          * have no other way to init EAs correctly but ask master device about
81          * it. Thus both, DT and MD layers should be able to answer with correct
82          * lov_desc. LOV knows it explicitly and LMV/MDC have to ask MDS server
83          * of it. */
84         valsize = sizeof(cmobd->master_desc);
85         memset(&cmobd->master_desc, 0, sizeof(cmobd->master_desc));
86
87         rc = obd_get_info(cmobd->master_exp, strlen("lovdesc") + 1,
88                           "lovdesc", &valsize, &cmobd->master_desc);
89         RETURN(rc);
90 }
91         
92 static int cmobd_init_ea_size(struct obd_device *obd)
93 {
94         int rc = 0, tgt_count, easize, cookiesize;
95         struct cm_obd *cmobd = &obd->u.cm;
96         ENTRY;
97
98         if (!cmobd->master_exp)
99                 RETURN(-EINVAL);
100
101         tgt_count = cmobd->master_desc.ld_tgt_count;
102
103         /* no EA setup is needed as there is single OST with no LOV */
104         if (tgt_count == 0)
105                 RETURN(0);
106
107         easize = lov_mds_md_size(tgt_count);
108         cookiesize = tgt_count * sizeof(struct llog_cookie);
109         rc = obd_init_ea_size(cmobd->master_exp, easize, cookiesize);
110         RETURN(rc);
111 }
112
113 static char *types[] = {
114         OBD_LMV_DEVICENAME, OBD_MDC_DEVICENAME,
115         OBD_LOV_DEVICENAME, OBD_OSC_DEVICENAME
116 };
117
118 static struct obd_device *
119 cmobd_find_obd(struct obd_device *obd, struct obd_uuid *uuid)
120 {
121         struct obd_device *res;
122         int i = 0;
123         ENTRY;
124
125         CWARN("%s: looking for client obd %s\n",
126               obd->obd_uuid.uuid, uuid->uuid);
127
128         for (i = 0; i < sizeof(types) / sizeof(char *); i++) {
129                 res = class_find_client_obd(NULL, types[i], uuid);
130                 if (res)
131                         RETURN(res);
132         }
133         RETURN(NULL);
134 }
135
136 static int cmobd_setup(struct obd_device *obd, obd_count len, void *buf)
137 {
138         struct obd_uuid master_uuid, cache_uuid;
139         struct lustre_handle conn = { 0 };
140         struct cm_obd *cmobd = &obd->u.cm;
141         struct lustre_cfg* lcfg = buf;
142         struct lustre_id mid, lid;
143         __u32 valsize;
144         int rc;
145         ENTRY;
146
147         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
148                 CERROR("%s: setup requires master device uuid\n", 
149                        obd->obd_name);
150                 RETURN(-EINVAL);
151         }
152
153         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
154                 CERROR("%s: setup requires cache device uuid\n",
155                        obd->obd_name);
156                 RETURN(-EINVAL);
157         }
158
159         obd_str2uuid(&master_uuid, lustre_cfg_string(lcfg, 1));
160         obd_str2uuid(&cache_uuid, lustre_cfg_string(lcfg, 2));
161
162         /* getting master obd */
163         cmobd->master_obd = cmobd_find_obd(obd, &master_uuid);
164         if (!cmobd->master_obd) {
165                 CERROR("can't find master client obd by uuid %s\n",
166                        master_uuid.uuid);
167                 RETURN(-EINVAL);
168         }
169
170         /* getting cache obd */
171         cmobd->cache_obd = class_uuid2obd(&cache_uuid);
172         if (cmobd->cache_obd == NULL) {
173                 CERROR("CMOBD: unable to find obd by uuid: %s\n",
174                        cache_uuid.uuid);
175                 RETURN(-EINVAL);
176         }
177
178         /* connecting master */
179         memset(&conn, 0, sizeof(conn));
180         rc = obd_connect(&conn, cmobd->master_obd, &obd->obd_uuid, 
181                          NULL, OBD_OPT_REAL_CLIENT);
182         if (rc)
183                 RETURN(rc);
184         cmobd->master_exp = class_conn2export(&conn);
185
186         /* connecting cache */
187         memset(&conn, 0, sizeof(conn));
188         rc = class_connect(&conn, cmobd->cache_obd, &obd->obd_uuid);
189         if (rc)
190                 GOTO(put_master, rc);
191         cmobd->cache_exp = class_conn2export(&conn);
192
193         /* initialing DT desc. Both, data and metadata layers should be able to
194          * serve this call. */
195         rc = cmobd_init_dt_desc(obd);
196         if (rc != 0 && rc != -EPROTO) {
197                 CERROR("cannot get DT layer desc from master device %s, "
198                        "err %d.\n", cmobd->master_exp->exp_obd->obd_name,
199                        rc);
200                 GOTO(put_cache, rc);
201         }
202
203         if (cmobd_dt_obd(cmobd->master_exp->exp_obd)) {
204                 /* for master dt device remove the recovery flag. */
205                 rc = obd_set_info(cmobd->master_exp, strlen("unrecovery"),
206                                   "unrecovery", 0, NULL); 
207                 if (rc)
208                         GOTO(put_cache, rc);
209                 
210                 rc = cmobd_init_write_srv(obd);
211                 if (rc)
212                         GOTO(put_cache, rc);
213         }
214
215         if (cmobd_md_obd(cmobd->master_exp->exp_obd)) {
216                 rc = cmobd_init_ea_size(obd);
217                 if (rc) {
218                         CERROR("can't init MD layer EA size, "
219                                "err %d\n", rc);
220                         GOTO(put_cache, rc);
221                 }
222                 cmobd->write_srv = NULL;
223
224                 /* requesting master obd to have its root inode store cookie to
225                  * be able to save it to local root inode EA. */
226                 valsize = sizeof(struct lustre_id);
227         
228                 rc = obd_get_info(cmobd->master_exp, strlen("rootid"),
229                                   "rootid", &valsize, &mid);
230                 if (rc) {
231                         CERROR("can't get rootid from master MDS %s, "
232                                "err= %d.\n", master_uuid.uuid, rc);
233                         GOTO(put_cache, rc);
234                 }
235
236                 /* getting rootid from cache MDS. It is needed to update local
237                  * (cache) root inode by rootid value from master obd. */
238                 rc = obd_get_info(cmobd->cache_exp, strlen("rootid"),
239                                   "rootid", &valsize, &lid);
240                 if (rc) {
241                         CERROR("can't get rootid from local MDS %s, "
242                                "err= %d.\n", cache_uuid.uuid, rc);
243                         GOTO(put_cache, rc);
244                 }
245
246                 /* storing master MDS rootid to local root inode EA. */
247                 CWARN("storing "DLID4" to local inode "DLID4".\n",
248                       OLID4(&mid), OLID4(&lid));
249
250                 rc = mds_update_mid(cmobd->cache_exp->exp_obd, &lid,
251                                     &mid, sizeof(mid));
252                 if (rc) {
253                         CERROR("can't update local root inode by ID "
254                                "from master MDS %s, err = %d.\n",
255                                master_uuid.uuid, rc);
256                         GOTO(put_cache, rc);
257                 }
258         }
259
260         RETURN(rc);
261 put_cache:
262         class_disconnect(cmobd->cache_exp, 0);
263 put_master:
264         obd_disconnect(cmobd->master_exp, 0);
265         return rc;
266 }
267
268 static int cmobd_cleanup(struct obd_device *obd, int flags)
269 {
270         struct cm_obd *cmobd = &obd->u.cm;
271         int rc;
272         ENTRY;
273
274         if (cmobd->write_srv)
275                 cmobd_cleanup_write_srv(obd);
276
277         rc = obd_disconnect(cmobd->master_exp, flags);
278         if (rc) {
279                 CERROR("error disconnecting master %s, err %d\n",
280                        cmobd->master_exp->exp_obd->obd_name, rc);
281         }
282         
283         rc = class_disconnect(cmobd->cache_exp, flags);
284         if (rc) {
285                 CERROR("error disconnecting cache %s, err %d\n",
286                        cmobd->cache_exp->exp_obd->obd_name, rc);
287         }
288         
289         RETURN(0);
290 }
291
292 static int cmobd_iocontrol(unsigned int cmd, struct obd_export *exp,
293                            int len, void *karg, void *uarg)
294 {
295         struct obd_device *obd = exp->exp_obd;
296         int rc = 0;
297         ENTRY;
298         
299         switch (cmd) {
300         case OBD_IOC_CMOBD_SYNC: /* trigger reintegration */
301                 rc = cmobd_reintegrate(obd);
302                 break;
303         default:
304                 CERROR("unrecognized ioctl %#x\n", cmd);
305                 rc = -EINVAL;
306                 break;
307         }
308                 
309         RETURN(rc);
310 }
311
312 static struct obd_ops cmobd_ops = {
313         .o_owner     = THIS_MODULE,
314         .o_attach    = cmobd_attach,
315         .o_detach    = cmobd_detach,
316         .o_setup     = cmobd_setup,
317         .o_cleanup   = cmobd_cleanup,
318         .o_iocontrol = cmobd_iocontrol,
319 };
320
321 kmem_cache_t *cmobd_extent_slab;
322
323 static int __init cmobd_init(void)
324 {
325         struct lprocfs_static_vars lvars;
326         int rc;
327         ENTRY;
328
329         printk(KERN_INFO "Lustre: Cache Manager OBD driver; info@clusterfs.com\n");
330
331         lprocfs_init_vars(cmobd, &lvars);
332         rc = class_register_type(&cmobd_ops, NULL, lvars.module_vars,
333                                  OBD_CMOBD_DEVICENAME);
334         if (rc)
335                 RETURN(rc);
336         cmobd_extent_slab = kmem_cache_create("cmobd_extents",
337                                                sizeof(struct cmobd_extent_info), 0,
338                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
339         if (cmobd_extent_slab == NULL) {
340                 class_unregister_type(OBD_CMOBD_DEVICENAME);
341                 RETURN(-ENOMEM);
342         }
343         RETURN(0);
344 }
345
346 static void __exit cmobd_exit(void)
347 {
348         class_unregister_type(OBD_CMOBD_DEVICENAME);
349         if (kmem_cache_destroy(cmobd_extent_slab) != 0)
350                 CERROR("couldn't free cmobd extent slab\n");
351 }
352
353 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
354 MODULE_DESCRIPTION("Lustre Cache Manager OBD driver");
355 MODULE_LICENSE("GPL");
356
357 module_init(cmobd_init);
358 module_exit(cmobd_exit);