Whamcloud - gitweb
b=4738
[fs/lustre-release.git] / lustre / cmobd / cache_manager_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
36 #include "cmobd_internal.h"
37
38 static int cmobd_attach(struct obd_device *obd, obd_count len, void *data)
39 {
40         struct lprocfs_static_vars lvars;
41
42         lprocfs_init_vars(cmobd, &lvars);
43         return lprocfs_obd_attach(obd, lvars.obd_vars);
44 }
45
46 static int cmobd_detach(struct obd_device *obd)
47 {
48         return lprocfs_obd_detach(obd);
49 }
50
51 static void cmobd_find_master_client_obd(struct obd_device *obd,
52                                         struct obd_uuid *uuid)
53 {             
54         struct cache_manager_obd *cmobd = &obd->u.cmobd;
55          
56         cmobd->cm_master_obd = class_find_client_obd(NULL, OBD_LOV_DEVICENAME,
57                                                      uuid);
58         if (cmobd->cm_master_obd == NULL) 
59                cmobd->cm_master_obd = class_find_client_obd(NULL, 
60                                                             LUSTRE_MDC_NAME, 
61                                                             uuid);
62 }        
63
64 static int cmobd_setup(struct obd_device *obd, obd_count len, void *buf)
65 {
66         struct cache_manager_obd *cmobd = &obd->u.cmobd;
67         struct lustre_cfg* lcfg = buf;
68         struct obd_uuid master_uuid, cache_uuid;
69         struct lustre_handle conn = { 0 };
70         int rc;
71         ENTRY;
72
73         if (lcfg->lcfg_inllen1 < 1 || !lcfg->lcfg_inlbuf1) {
74                 CERROR("CMOBD setup requires master uuid\n");
75                 RETURN(-EINVAL);
76         }
77         if (lcfg->lcfg_inllen2 < 1 || !lcfg->lcfg_inlbuf2) {
78                 CERROR("CMOBD setup requires cache uuid\n");
79                 RETURN(-EINVAL);
80         }
81         
82         obd_str2uuid(&master_uuid, lcfg->lcfg_inlbuf1);
83         obd_str2uuid(&cache_uuid, lcfg->lcfg_inlbuf2);
84
85         cmobd_find_master_client_obd(obd, &master_uuid);
86         if (cmobd->cm_master_obd == NULL) {
87                 CERROR("Can't find master obd %s\n", &master_uuid.uuid[0]);
88                 RETURN(-EINVAL);
89         }
90         cmobd->cm_cache_obd = class_uuid2obd(&cache_uuid);
91         if (cmobd->cm_cache_obd == NULL) {
92                 CERROR("Can't find cache obd %s\n", &cache_uuid.uuid[0]);
93                 RETURN(-EINVAL);
94         }
95
96         /* master lov connects to master ost here */
97         rc = obd_connect(&conn, cmobd->cm_master_obd, &obd->obd_uuid, 0); 
98         if (rc)
99                 RETURN(rc);
100         cmobd->cm_master_exp = class_conn2export(&conn);
101
102         memset(&conn, 0, sizeof(conn));
103         rc = class_connect(&conn, cmobd->cm_cache_obd, &obd->obd_uuid);
104         if (rc)
105                 GOTO(put_master, rc);
106         cmobd->cm_cache_exp = class_conn2export(&conn);
107         if (!strcmp(cmobd->cm_master_obd->obd_type->typ_name, 
108                     OBD_LOV_DEVICENAME)){
109                 /* for master osc remove the recovery flag of oscc */
110                 rc = obd_set_info(cmobd->cm_master_exp, strlen("unrecovery"),
111                                   "unrecovery", 0, NULL); 
112                 if (rc)
113                         GOTO(put_master, rc);
114                 
115                 rc = cmobd_init_write_srv(obd);
116                 if (rc)
117                         GOTO(put_cache, rc);
118
119         } else {
120                 /*FIXME later temp fix here 
121                  *Assumation:cache mds only have one ost*/
122                 cmobd->cm_master_obd->u.cli.cl_max_mds_easize = 
123                                                 lov_mds_md_size(1);
124         }
125         /* start n threads for write replay */
126         RETURN(0);
127 put_cache:
128         class_disconnect(cmobd->cm_cache_exp, 0);
129 put_master:
130         obd_disconnect(cmobd->cm_master_exp, 0);
131         RETURN(rc);
132 }
133
134 static int cmobd_cleanup(struct obd_device *obd, int flags)
135 {
136         struct cache_manager_obd *cmobd = &obd->u.cmobd;
137         ENTRY;
138         
139         if (!strcmp(cmobd->cm_master_obd->obd_type->typ_name,
140                     OBD_LOV_DEVICENAME)) {
141                 cmobd_cleanup_write_srv(obd);
142         }
143         class_disconnect(cmobd->cm_cache_exp, 0);
144         obd_disconnect(cmobd->cm_master_exp, 0);
145         
146         RETURN(0);
147 }
148
149 static int cmobd_iocontrol(unsigned int cmd, struct obd_export *exp, int len,
150                            void *karg, void *uarg)
151 {
152         struct obd_device *obd = exp->exp_obd;
153         int rc = 0;
154         ENTRY;
155         
156         switch (cmd) {
157         case OBD_IOC_CMOBD_SYNC: /* trigger reintegration */
158                 rc = cmobd_reintegrate(obd);
159                 break;
160         default:
161                 CERROR("unrecognized ioctl %#x\n", cmd);
162                 rc = -EINVAL;
163                 break;
164         }
165                 
166         RETURN(rc);
167 }
168
169 static struct obd_ops cmobd_ops = {
170         o_owner:                THIS_MODULE,
171         o_attach:               cmobd_attach,
172         o_detach:               cmobd_detach,
173         o_setup:                cmobd_setup,
174         o_cleanup:              cmobd_cleanup,
175         o_iocontrol:            cmobd_iocontrol,
176 };
177
178 kmem_cache_t *cmobd_extent_slab;
179
180 static int __init cmobd_init(void)
181 {
182         struct lprocfs_static_vars lvars;
183         int rc;
184         ENTRY;
185
186         printk(KERN_INFO "Lustre: Cache Manager OBD driver; info@clusterfs.com\n");
187
188         lprocfs_init_vars(cmobd, &lvars);
189         rc = class_register_type(&cmobd_ops, NULL, lvars.module_vars,
190                                  LUSTRE_CMOBD_NAME);
191         if (rc)
192                 RETURN(rc);
193         cmobd_extent_slab = kmem_cache_create("cmobd_extents",
194                                                sizeof(struct cmobd_extent_info), 0,
195                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
196         if (cmobd_extent_slab == NULL) {
197                 class_unregister_type(LUSTRE_CMOBD_NAME);
198                 RETURN(-ENOMEM);
199         }
200         RETURN(0);
201 }
202
203 static void /*__exit*/ cmobd_exit(void)
204 {
205         class_unregister_type(LUSTRE_CMOBD_NAME);
206         if (kmem_cache_destroy(cmobd_extent_slab) != 0)
207                 CERROR("couldn't free cmobd extent slab\n");
208 }
209
210 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
211 MODULE_DESCRIPTION("Lustre Cache Manager OBD driver");
212 MODULE_LICENSE("GPL");
213
214 module_init(cmobd_init);
215 module_exit(cmobd_exit);