Whamcloud - gitweb
land b_hd_sec onto HEAD:
[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_lmv.h>
36
37 #include "cm_internal.h"
38
39 static int cmobd_attach(struct obd_device *obd,
40                         obd_count len, void *data)
41 {
42         struct lprocfs_static_vars lvars;
43
44         lprocfs_init_vars(cmobd, &lvars);
45         return lprocfs_obd_attach(obd, lvars.obd_vars);
46 }
47
48 static int cmobd_detach(struct obd_device *obd)
49 {
50         return lprocfs_obd_detach(obd);
51 }
52
53 static inline int cmobd_lmv_obd(struct obd_device *obd)
54 {
55         if (!strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) ||
56             !strcmp(obd->obd_type->typ_name, OBD_LMV_DEVICENAME))
57                 return 1;
58
59         return 0;
60 }
61
62 static inline int cmobd_lov_obd(struct obd_device *obd)
63 {
64         if (!strcmp(obd->obd_type->typ_name, OBD_LOV_DEVICENAME))
65                 return 1;
66
67         return 0;
68 }
69
70 static void cmobd_init_ea_size(struct obd_device *obd)
71 {
72         int ost_count = 1, easize, cookiesize;
73         struct cm_obd *cmobd = &obd->u.cm;
74         ENTRY;
75
76         /*
77          * here we should also take into account that there is possible to have
78          * few OSTs. --umka
79          */
80         easize = lov_mds_md_size(ost_count);
81         cookiesize = ost_count * sizeof(struct llog_cookie);
82
83         obd_init_ea_size(cmobd->master_exp, easize, cookiesize);
84
85         cmobd->master_exp->exp_obd->u.cli.cl_max_mds_easize = easize;
86         cmobd->master_exp->exp_obd->u.cli.cl_max_mds_cookiesize = cookiesize;
87         
88         EXIT;
89 }
90
91 static struct obd_device *
92 find_master_obd(struct obd_device *obd, struct obd_uuid *uuid)
93 {
94         struct obd_device *master;
95
96         CWARN("%s: looking for client obd %s\n",
97               obd->obd_uuid.uuid, uuid->uuid);
98
99         master = class_find_client_obd(NULL, OBD_LOV_DEVICENAME,
100                                        uuid);
101         if (master)
102                 return master;
103
104         master = class_find_client_obd(NULL, OBD_LMV_DEVICENAME,
105                                        uuid);
106         if (master)
107                 return master;
108
109         return class_find_client_obd(NULL, LUSTRE_MDC_NAME,
110                                      uuid);
111 }
112
113 static int cmobd_setup(struct obd_device *obd, obd_count len, void *buf)
114 {
115         struct obd_uuid master_uuid, cache_uuid;
116         struct lustre_handle conn = { 0 };
117         struct cm_obd *cmobd = &obd->u.cm;
118         struct lustre_cfg* lcfg = buf;
119         struct lustre_id mid, lid;
120         int valsize, rc;
121         ENTRY;
122
123         if (lcfg->lcfg_inllen1 == 0 || lcfg->lcfg_inlbuf1 == NULL) {
124                 CERROR("%s: setup requires master device uuid\n", 
125                        obd->obd_name);
126                 RETURN(-EINVAL);
127         }
128
129         if (lcfg->lcfg_inllen2 == 0 || lcfg->lcfg_inlbuf2 == NULL) {
130                 CERROR("%s: setup requires cache device uuid\n",
131                        obd->obd_name);
132                 RETURN(-EINVAL);
133         }
134
135         obd_str2uuid(&master_uuid, lcfg->lcfg_inlbuf1);
136         obd_str2uuid(&cache_uuid, lcfg->lcfg_inlbuf2);
137
138         /* getting master obd */
139         cmobd->master_obd = find_master_obd(obd, &master_uuid);
140         if (!cmobd->master_obd) {
141                 CERROR("can't find master obd by uuid %s\n",
142                        master_uuid.uuid);
143                 RETURN(-EINVAL);
144         }
145
146         /* getting cache obd */
147         cmobd->cache_obd = class_uuid2obd(&cache_uuid);
148         if (cmobd->cache_obd == NULL) {
149                 CERROR("CMOBD: unable to find obd by uuid: %s\n",
150                        cache_uuid.uuid);
151                 RETURN(-EINVAL);
152         }
153
154         /* connecting master */
155         memset(&conn, 0, sizeof(conn));
156         rc = obd_connect(&conn, cmobd->master_obd, &obd->obd_uuid, NULL, 0);
157         if (rc)
158                 RETURN(rc);
159         cmobd->master_exp = class_conn2export(&conn);
160
161         /* connecting cache */
162         memset(&conn, 0, sizeof(conn));
163         rc = class_connect(&conn, cmobd->cache_obd, &obd->obd_uuid);
164         if (rc)
165                 GOTO(put_master, rc);
166         cmobd->cache_exp = class_conn2export(&conn);
167         
168         if (cmobd_lov_obd(cmobd->master_exp->exp_obd)) {
169                 /* for master osc remove the recovery flag. */
170                 rc = obd_set_info(cmobd->master_exp, strlen("unrecovery"),
171                                   "unrecovery", 0, NULL); 
172                 if (rc)
173                         GOTO(put_cache, rc);
174                 
175                 rc = cmobd_init_write_srv(obd);
176                 if (rc)
177                         GOTO(put_cache, rc);
178         } else {
179                 cmobd_init_ea_size(obd);
180                 cmobd->write_srv = NULL;
181         }
182
183         if (cmobd_lmv_obd(cmobd->master_exp->exp_obd)) {
184                 /*
185                  * making sure, that both obds are ready. This is especially
186                  * important in the case of using LMV as master.
187                  */
188                 rc = obd_getready(cmobd->master_exp);
189                 if (rc) {
190                         CERROR("can't get %s obd ready.\n",
191                                master_uuid.uuid);
192                         GOTO(put_cache, rc);
193                 }
194         
195                 rc = obd_getready(cmobd->cache_exp);
196                 if (rc) {
197                         CERROR("can't get %s obd ready.\n",
198                                cache_uuid.uuid);
199                         GOTO(put_cache, rc);
200                 }
201         
202                 /*
203                  * requesting master obd to have its root inode store cookie to
204                  * be able to save it to local root inode EA.
205                  */
206                 valsize = sizeof(struct lustre_id);
207         
208                 rc = obd_get_info(cmobd->master_exp, strlen("rootid"),
209                                   "rootid", &valsize, &mid);
210                 if (rc) {
211                         CERROR("can't get rootid from master MDS %s, "
212                                "err= %d.\n", master_uuid.uuid, rc);
213                         GOTO(put_cache, rc);
214                 }
215
216                 /*
217                  * getting rootid from cache MDS. It is needed to update local
218                  * (cache) root inode by rootid value from master obd.
219                  */
220                 rc = obd_get_info(cmobd->cache_exp, strlen("rootid"),
221                                   "rootid", &valsize, &lid);
222                 if (rc) {
223                         CERROR("can't get rootid from local MDS %s, "
224                                "err= %d.\n", cache_uuid.uuid, rc);
225                         GOTO(put_cache, rc);
226                 }
227
228                 /* storing master MDS rootid to local root inode EA. */
229                 CWARN("storing "DLID4" to local inode "DLID4".\n",
230                       OLID4(&mid), OLID4(&lid));
231
232                 rc = mds_update_mid(cmobd->cache_exp->exp_obd, &lid,
233                                     &mid, sizeof(mid));
234                 if (rc) {
235                         CERROR("can't update local root inode by ID "
236                                "from master MDS %s, err = %d.\n",
237                                master_uuid.uuid, rc);
238                         GOTO(put_cache, rc);
239                 }
240         }
241
242         RETURN(rc);
243 put_cache:
244         class_disconnect(cmobd->cache_exp, 0);
245 put_master:
246         obd_disconnect(cmobd->master_exp, 0);
247         return rc;
248 }
249
250 static int cmobd_cleanup(struct obd_device *obd, int flags)
251 {
252         struct cm_obd *cmobd = &obd->u.cm;
253         int rc;
254         ENTRY;
255
256         if (cmobd->write_srv)
257                 cmobd_cleanup_write_srv(obd);
258
259         rc = obd_disconnect(cmobd->master_exp, flags);
260         if (rc) {
261                 CERROR("error disconnecting master, err %d\n",
262                        rc);
263         }
264         
265         rc = class_disconnect(cmobd->cache_exp, flags);
266         if (rc) {
267                 CERROR("error disconnecting cache, err %d\n",
268                        rc);
269         }
270         
271         RETURN(0);
272 }
273
274 static int cmobd_iocontrol(unsigned int cmd, struct obd_export *exp,
275                            int len, void *karg, void *uarg)
276 {
277         struct obd_device *obd = exp->exp_obd;
278         int rc = 0;
279         ENTRY;
280         
281         switch (cmd) {
282         case OBD_IOC_CMOBD_SYNC: /* trigger reintegration */
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                                  LUSTRE_CMOBD_NAME);
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(LUSTRE_CMOBD_NAME);
323                 RETURN(-ENOMEM);
324         }
325         RETURN(0);
326 }
327
328 static void __exit cmobd_exit(void)
329 {
330         class_unregister_type(LUSTRE_CMOBD_NAME);
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);