Whamcloud - gitweb
LU-1406 ofd: OFD module framework
[fs/lustre-release.git] / lustre / ofd / ofd_dev.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ofd/ofd.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mike Pershin <tappro@whamcloud.com>
40  * Author: Johann Lombardi <johann@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_FILTER
44
45 #include <obd_class.h>
46 #include <lustre_param.h>
47
48 #include "ofd_internal.h"
49
50 /* Slab for OFD object allocation */
51 static cfs_mem_cache_t *ofd_object_kmem;
52
53 static struct lu_kmem_descr ofd_caches[] = {
54         {
55                 .ckd_cache = &ofd_object_kmem,
56                 .ckd_name  = "ofd_obj",
57                 .ckd_size  = sizeof(struct ofd_object)
58         },
59         {
60                 .ckd_cache = NULL
61         }
62 };
63
64 /* used by MGS to process specific configurations */
65 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
66                               struct lustre_cfg *cfg)
67 {
68         int rc = 0;
69
70         ENTRY;
71
72         switch (cfg->lcfg_command) {
73         case LCFG_PARAM: {
74                 struct lprocfs_static_vars lvars;
75
76                 lprocfs_ofd_init_vars(&lvars);
77                 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
78                                               d->ld_obd);
79                 break;
80         }
81         case LCFG_SPTLRPC_CONF: {
82                 rc = -ENOTSUPP;
83                 break;
84         }
85         default:
86                 break;
87         }
88         RETURN(rc);
89 }
90
91 struct lu_object_operations ofd_obj_ops = {
92 };
93
94 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
95                                           const struct lu_object_header *hdr,
96                                           struct lu_device *d)
97 {
98         struct ofd_object *of;
99
100         ENTRY;
101
102         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
103         if (of != NULL) {
104                 struct lu_object        *o;
105                 struct lu_object_header *h;
106
107                 o = &of->ofo_obj.do_lu;
108                 h = &of->ofo_header;
109                 lu_object_header_init(h);
110                 lu_object_init(o, h, d);
111                 lu_object_add_top(h, o);
112                 o->lo_ops = &ofd_obj_ops;
113                 RETURN(o);
114         } else {
115                 RETURN(NULL);
116         }
117 }
118
119 static int ofd_start(const struct lu_env *env, struct lu_device *parent,
120                      struct lu_device *dev)
121 {
122         struct obd_device *obd = dev->ld_obd;
123         int                rc = 0;
124
125         ENTRY;
126
127         LASSERT(obd->obd_no_conn);
128         cfs_spin_lock(&obd->obd_dev_lock);
129         obd->obd_no_conn = 0;
130         cfs_spin_unlock(&obd->obd_dev_lock);
131
132         RETURN(rc);
133 }
134
135 static int ofd_recovery_complete(const struct lu_env *env,
136                                  struct lu_device *dev)
137 {
138         int                rc = 0;
139
140         ENTRY;
141
142         RETURN(rc);
143 }
144
145 static struct lu_device_operations ofd_lu_ops = {
146         .ldo_object_alloc       = ofd_object_alloc,
147         .ldo_process_config     = ofd_process_config,
148         .ldo_prepare            = ofd_start,
149         .ldo_recovery_complete  = ofd_recovery_complete,
150 };
151
152 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
153                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
154 {
155         const char              *dev = lustre_cfg_string(cfg, 0);
156         struct obd_device       *obd;
157         int                      rc;
158
159         ENTRY;
160
161         obd = class_name2obd(dev);
162         if (obd == NULL) {
163                 CERROR("Cannot find obd with name %s\n", dev);
164                 RETURN(-ENODEV);
165         }
166
167         rc = lu_env_refill((struct lu_env *)env);
168         if (rc != 0)
169                 RETURN(rc);
170
171         obd->u.obt.obt_magic = OBT_MAGIC;
172
173         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
174         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
175         /* set this lu_device to obd, because error handling need it */
176         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
177
178         /* No connection accepted until configurations will finish */
179         obd->obd_no_conn = 1;
180         obd->obd_replayable = 1;
181         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
182                 char *str = lustre_cfg_string(cfg, 4);
183
184                 if (strchr(str, 'n')) {
185                         CWARN("%s: recovery disabled\n", obd->obd_name);
186                         obd->obd_replayable = 0;
187                 }
188         }
189
190         RETURN(0);
191 }
192
193 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
194 {
195         struct obd_device *obd = ofd_obd(m);
196         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
197
198         obd_exports_barrier(obd);
199         obd_zombie_barrier();
200
201         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
202         EXIT;
203 }
204
205 static struct lu_device* ofd_device_fini(const struct lu_env *env,
206                                          struct lu_device *d)
207 {
208         ENTRY;
209         ofd_fini(env, ofd_dev(d));
210         RETURN(NULL);
211 }
212
213 static struct lu_device *ofd_device_free(const struct lu_env *env,
214                                          struct lu_device *d)
215 {
216         struct ofd_device *m = ofd_dev(d);
217
218         dt_device_fini(&m->ofd_dt_dev);
219         OBD_FREE_PTR(m);
220         RETURN(NULL);
221 }
222
223 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
224                                           struct lu_device_type *t,
225                                           struct lustre_cfg *cfg)
226 {
227         struct ofd_device *m;
228         struct lu_device  *l;
229         int                rc;
230
231         OBD_ALLOC_PTR(m);
232         if (m == NULL)
233                 return ERR_PTR(-ENOMEM);
234
235         l = &m->ofd_dt_dev.dd_lu_dev;
236         dt_device_init(&m->ofd_dt_dev, t);
237         rc = ofd_init0(env, m, t, cfg);
238         if (rc != 0) {
239                 ofd_device_free(env, l);
240                 l = ERR_PTR(rc);
241         }
242
243         return l;
244 }
245
246 /* thread context key constructor/destructor */
247 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
248
249 static void ofd_key_exit(const struct lu_context *ctx,
250                          struct lu_context_key *key, void *data)
251 {
252         struct ofd_thread_info *info = data;
253
254         info->fti_env = NULL;
255 }
256
257 struct lu_context_key ofd_thread_key = {
258         .lct_tags = LCT_DT_THREAD,
259         .lct_init = ofd_key_init,
260         .lct_fini = ofd_key_fini,
261         .lct_exit = ofd_key_exit
262 };
263
264 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
265 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
266
267 static struct lu_device_type_operations ofd_device_type_ops = {
268         .ldto_init              = ofd_type_init,
269         .ldto_fini              = ofd_type_fini,
270
271         .ldto_start             = ofd_type_start,
272         .ldto_stop              = ofd_type_stop,
273
274         .ldto_device_alloc      = ofd_device_alloc,
275         .ldto_device_free       = ofd_device_free,
276         .ldto_device_fini       = ofd_device_fini
277 };
278
279 static struct lu_device_type ofd_device_type = {
280         .ldt_tags       = LU_DEVICE_DT,
281         .ldt_name       = LUSTRE_OST_NAME,
282         .ldt_ops        = &ofd_device_type_ops,
283         .ldt_ctx_tags   = LCT_DT_THREAD
284 };
285
286 int __init ofd_init(void)
287 {
288         struct lprocfs_static_vars      lvars;
289         int                             rc;
290
291         rc = lu_kmem_init(ofd_caches);
292         if (rc)
293                 return rc;
294
295         lprocfs_ofd_init_vars(&lvars);
296
297         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
298                                  LUSTRE_OST_NAME, &ofd_device_type);
299         return rc;
300 }
301
302 void __exit ofd_exit(void)
303 {
304         class_unregister_type(LUSTRE_OST_NAME);
305         lu_kmem_fini(ofd_caches);
306 }
307
308 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
309 MODULE_DESCRIPTION("Lustre Object Filtering Device");
310 MODULE_LICENSE("GPL");
311
312 module_init(ofd_init);
313 module_exit(ofd_exit);