Whamcloud - gitweb
Branch b1_4
[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 int cmobd_init_dt_desc(struct obd_device *obd)
55 {
56         struct cm_obd *cmobd = &obd->u.cm;
57         __u32 valsize;
58         int rc = 0;
59         ENTRY;
60         
61         /* as CMOBD is stand alone device, that is has not to be connected, we
62          * have no other way to init EAs correctly but ask master device about
63          * it. Thus both, DT and MD layers should be able to answer with correct
64          * lov_desc. LOV knows it explicitly and LMV/MDC have to ask MDS server
65          * of it. */
66         valsize = sizeof(cmobd->master_desc);
67         memset(&cmobd->master_desc, 0, sizeof(cmobd->master_desc));
68
69         rc = obd_get_info(cmobd->master_exp, strlen("lovdesc") + 1,
70                           "lovdesc", &valsize, &cmobd->master_desc);
71         RETURN(rc);
72 }
73         
74 static int cmobd_init_ea_size(struct obd_device *obd)
75 {
76         int rc = 0, tgt_count, easize, cookiesize;
77         struct cm_obd *cmobd = &obd->u.cm;
78         ENTRY;
79
80         if (!cmobd->master_exp)
81                 RETURN(-EINVAL);
82
83         tgt_count = cmobd->master_desc.ld_tgt_count;
84
85         /* no EA setup is needed as there is single OST with no LOV */
86         if (tgt_count == 0)
87                 RETURN(0);
88
89         easize = lov_mds_md_size(tgt_count);
90         cookiesize = tgt_count * sizeof(struct llog_cookie);
91         rc = obd_init_ea_size(cmobd->master_exp, easize, cookiesize);
92         RETURN(rc);
93 }
94
95 static char *types[] = {
96         OBD_LMV_DEVICENAME, OBD_MDC_DEVICENAME,
97         OBD_LOV_DEVICENAME, OBD_OSC_DEVICENAME
98 };
99
100 static struct obd_device *
101 cmobd_find_obd(struct obd_device *obd, struct obd_uuid *uuid)
102 {
103         struct obd_device *res;
104         int i = 0;
105         ENTRY;
106
107         CWARN("%s: looking for client obd %s\n",
108               obd->obd_uuid.uuid, uuid->uuid);
109
110         for (i = 0; i < sizeof(types) / sizeof(char *); i++) {
111                 res = class_find_client_obd(NULL, types[i], uuid);
112                 if (res)
113                         RETURN(res);
114         }
115         RETURN(NULL);
116 }
117
118 static int cmobd_setup(struct obd_device *obd, obd_count len, void *buf)
119 {
120         struct obd_uuid master_uuid, cache_uuid;
121         struct lustre_handle conn = { 0 };
122         struct cm_obd *cmobd = &obd->u.cm;
123         struct lustre_cfg* lcfg = buf;
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         /* initialing DT desc. Both, data and metadata layers should be able to
174          * serve this call. */
175         rc = cmobd_init_dt_desc(obd);
176         if (rc != 0 && rc != -EPROTO) {
177                 CERROR("cannot get DT layer desc from master device %s, "
178                        "err %d.\n", cmobd->master_exp->exp_obd->obd_name,
179                        rc);
180                 GOTO(put_cache, rc);
181         }
182
183         if (obd_dt_type(cmobd->master_exp->exp_obd)) {
184                 /* for master dt device remove the recovery flag. */
185                 rc = obd_set_info(cmobd->master_exp, strlen("unrecovery"),
186                                   "unrecovery", 0, NULL); 
187                 if (rc)
188                         GOTO(put_cache, rc);
189                 
190                 rc = cmobd_init_write_srv(obd);
191                 if (rc)
192                         GOTO(put_cache, rc);
193         }
194
195         if (obd_md_type(cmobd->master_exp->exp_obd)) {
196                 __u32 size = sizeof(struct fid_extent);
197                 struct fid_extent ext;
198                 
199                 rc = cmobd_init_ea_size(obd);
200                 if (rc) {
201                         CERROR("can't init MD layer EA size, "
202                                "err %d\n", rc);
203                         GOTO(put_cache, rc);
204                 }
205                 cmobd->write_srv = NULL;
206
207                 /* getting fid pool from master to set it on cache */
208                 rc = obd_get_info(cmobd->master_exp, strlen("getext"),
209                                   "getext", &size, &ext);
210                 if (rc) {
211                         CERROR("can't get fids extent from master, "
212                                "err %d\n", rc);
213                         GOTO(put_cache, rc);
214                 }
215
216                 /* simple checks for validness */
217                 if (!ext.fe_start || !ext.fe_width || ext.fe_start == ext.fe_width) {
218                         CERROR("invalid fids extent from master, ["LPD64"-"LPD64"]\n", 
219                                ext.fe_start, ext.fe_width);
220                         GOTO(put_cache, rc = -EINVAL);
221                 }
222
223                 CWARN("setting master fids extent ["LPD64"-"LPD64
224                       "] -> %s\n", ext.fe_start, ext.fe_width,
225                       cmobd->cache_exp->exp_obd->obd_name);
226                 
227                 rc = obd_set_info(cmobd->cache_exp, strlen("setext"),
228                                   "setext", size, &ext); 
229                 if (rc) {
230                         CERROR("can't set fids extent to cache, "
231                                "err %d\n", rc);
232                         GOTO(put_cache, rc);
233                 }
234         }
235
236         RETURN(rc);
237 put_cache:
238         class_disconnect(cmobd->cache_exp, 0);
239 put_master:
240         obd_disconnect(cmobd->master_exp, 0);
241         return rc;
242 }
243
244 static int cmobd_cleanup(struct obd_device *obd, int flags)
245 {
246         struct cm_obd *cmobd = &obd->u.cm;
247         int rc;
248         ENTRY;
249
250         if (cmobd->write_srv) {
251                 cmobd_cleanup_write_srv(obd);
252                 cmobd->write_srv = NULL;
253         }
254
255         rc = obd_disconnect(cmobd->master_exp, flags);
256         if (rc) {
257                 CERROR("error disconnecting master %s, err %d\n",
258                        cmobd->master_exp->exp_obd->obd_name, rc);
259         }
260         
261         rc = class_disconnect(cmobd->cache_exp, flags);
262         if (rc) {
263                 CERROR("error disconnecting cache %s, err %d\n",
264                        cmobd->cache_exp->exp_obd->obd_name, rc);
265         }
266         
267         RETURN(0);
268 }
269
270 static int cmobd_iocontrol(unsigned int cmd, struct obd_export *exp,
271                            int len, void *karg, void *uarg)
272 {
273         struct obd_device *obd = exp->exp_obd;
274         int rc = 0;
275         ENTRY;
276         
277         switch (cmd) {
278         case OBD_IOC_CMOBD_SYNC:
279                 /* here would be nice to make sure somehow that all data is in
280                  * cache and there are no outstanding requests, as otherwise
281                  * cache is not coherent. But how to check that from CMOBD? I do
282                  * not know. --umka */
283                 rc = cmobd_reintegrate(obd);
284                 break;
285         default:
286                 CERROR("unrecognized ioctl %#x\n", cmd);
287                 rc = -EINVAL;
288                 break;
289         }
290                 
291         RETURN(rc);
292 }
293
294 static struct obd_ops cmobd_ops = {
295         .o_owner     = THIS_MODULE,
296         .o_attach    = cmobd_attach,
297         .o_detach    = cmobd_detach,
298         .o_setup     = cmobd_setup,
299         .o_cleanup   = cmobd_cleanup,
300         .o_iocontrol = cmobd_iocontrol,
301 };
302
303 kmem_cache_t *cmobd_extent_slab;
304
305 static int __init cmobd_init(void)
306 {
307         struct lprocfs_static_vars lvars;
308         int rc;
309         ENTRY;
310
311         printk(KERN_INFO "Lustre: Cache Manager OBD driver; info@clusterfs.com\n");
312
313         lprocfs_init_vars(cmobd, &lvars);
314         rc = class_register_type(&cmobd_ops, NULL, lvars.module_vars,
315                                  OBD_CMOBD_DEVICENAME);
316         if (rc)
317                 RETURN(rc);
318         cmobd_extent_slab = kmem_cache_create("cmobd_extents",
319                                               sizeof(struct cmobd_extent_info), 0,
320                                               SLAB_HWCACHE_ALIGN, NULL, NULL);
321         if (cmobd_extent_slab == NULL) {
322                 class_unregister_type(OBD_CMOBD_DEVICENAME);
323                 RETURN(-ENOMEM);
324         }
325         RETURN(0);
326 }
327
328 static void __exit cmobd_exit(void)
329 {
330         class_unregister_type(OBD_CMOBD_DEVICENAME);
331         if (kmem_cache_destroy(cmobd_extent_slab) != 0)
332                 CERROR("couldn't free cmobd extent slab\n");
333 }
334
335 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
336 MODULE_DESCRIPTION("Lustre Cache Manager OBD driver");
337 MODULE_LICENSE("GPL");
338
339 module_init(cmobd_init);
340 module_exit(cmobd_exit);