Whamcloud - gitweb
a1bc262dfec3c4c1e205c0be26c55c567b20b1a7
[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 static int ofd_stack_init(const struct lu_env *env,
65                           struct ofd_device *m, struct lustre_cfg *cfg)
66 {
67         struct lu_device        *ofd_lu = &m->ofd_dt_dev.dd_lu_dev;
68         const char              *dev = lustre_cfg_string(cfg, 0);
69         struct obd_type         *type;
70         struct lu_device_type   *ldt;
71         struct lu_device        *d;
72         struct ofd_thread_info  *info = ofd_info(env);
73         int                      rc;
74
75         ENTRY;
76
77         /* XXX: we should be able to use different OSDs here */
78         type = class_get_type(LUSTRE_OSD_NAME);
79         if (!type) {
80                 CERROR("Unknown type: '%s'\n", LUSTRE_OSD_NAME);
81                 RETURN(-ENODEV);
82         }
83
84         rc = lu_env_refill((struct lu_env *)env);
85         if (rc != 0) {
86                 CERROR("Failure to refill session: '%d'\n", rc);
87                 GOTO(out_type, rc);
88         }
89
90         ldt = type->typ_lu;
91         if (ldt == NULL) {
92                 CERROR("type: '%s'\n", LUSTRE_OSD_NAME);
93                 GOTO(out_type, rc = -EINVAL);
94         }
95
96         ldt->ldt_obd_type = type;
97         d = ldt->ldt_ops->ldto_device_alloc(env, ldt, cfg);
98         if (IS_ERR(d)) {
99                 CERROR("Cannot allocate device: '%s'\n", LUSTRE_OSD_NAME);
100                 GOTO(out_type, rc = -ENODEV);
101         }
102
103         LASSERT(ofd_lu->ld_site);
104         d->ld_site = ofd_lu->ld_site;
105
106         snprintf(info->fti_u.name, sizeof(info->fti_u.name),
107                  "%s-osd", lustre_cfg_string(cfg, 0));
108
109         type->typ_refcnt++;
110         rc = ldt->ldt_ops->ldto_device_init(env, d, dev, NULL);
111         if (rc) {
112                 CERROR("can't init device '%s', rc %d\n", LUSTRE_OSD_NAME, rc);
113                 GOTO(out_free, rc);
114         }
115         lu_device_get(d);
116         lu_ref_add(&d->ld_reference, "lu-stack", &lu_site_init);
117
118         m->ofd_osd = lu2dt_dev(d);
119
120         /* process setup config */
121         rc = d->ld_ops->ldo_process_config(env, d, cfg);
122         if (rc)
123                 GOTO(out_fini, rc);
124
125         RETURN(rc);
126
127 out_fini:
128         ldt->ldt_ops->ldto_device_fini(env, d);
129 out_free:
130         type->typ_refcnt--;
131         ldt->ldt_ops->ldto_device_free(env, d);
132 out_type:
133         class_put_type(type);
134         RETURN(rc);
135 }
136
137 static void ofd_stack_fini(const struct lu_env *env, struct ofd_device *m,
138                            struct lu_device *top)
139 {
140         struct obd_device       *obd = ofd_obd(m);
141         struct lustre_cfg_bufs   bufs;
142         struct lustre_cfg       *lcfg;
143         char                     flags[3] = "";
144
145         ENTRY;
146
147         lu_site_purge(env, top->ld_site, ~0);
148
149         /* process cleanup, pass mdt obd name to get obd umount flags */
150         lustre_cfg_bufs_reset(&bufs, obd->obd_name);
151         if (obd->obd_force)
152                 strcat(flags, "F");
153         if (obd->obd_fail)
154                 strcat(flags, "A");
155         lustre_cfg_bufs_set_string(&bufs, 1, flags);
156         lcfg = lustre_cfg_new(LCFG_CLEANUP, &bufs);
157         if (!lcfg) {
158                 CERROR("Cannot alloc lcfg!\n");
159                 RETURN_EXIT;
160         }
161
162         LASSERT(top);
163         top->ld_ops->ldo_process_config(env, top, lcfg);
164         lustre_cfg_free(lcfg);
165
166         lu_stack_fini(env, &m->ofd_osd->dd_lu_dev);
167         m->ofd_osd = NULL;
168
169         EXIT;
170 }
171
172 /* used by MGS to process specific configurations */
173 static int ofd_process_config(const struct lu_env *env, struct lu_device *d,
174                               struct lustre_cfg *cfg)
175 {
176         struct ofd_device       *m = ofd_dev(d);
177         struct dt_device        *dt_next = m->ofd_osd;
178         struct lu_device        *next = &dt_next->dd_lu_dev;
179         int                      rc;
180
181         ENTRY;
182
183         switch (cfg->lcfg_command) {
184         case LCFG_PARAM: {
185                 struct lprocfs_static_vars lvars;
186
187                 lprocfs_ofd_init_vars(&lvars);
188                 rc = class_process_proc_param(PARAM_OST, lvars.obd_vars, cfg,
189                                               d->ld_obd);
190                 if (rc > 0 || rc == -ENOSYS)
191                         /* we don't understand; pass it on */
192                         rc = next->ld_ops->ldo_process_config(env, next, cfg);
193                 break;
194         }
195         case LCFG_SPTLRPC_CONF: {
196                 rc = -ENOTSUPP;
197                 break;
198         }
199         default:
200                 /* others are passed further */
201                 rc = next->ld_ops->ldo_process_config(env, next, cfg);
202                 break;
203         }
204         RETURN(rc);
205 }
206
207 struct lu_object_operations ofd_obj_ops = {
208 };
209
210 static struct lu_object *ofd_object_alloc(const struct lu_env *env,
211                                           const struct lu_object_header *hdr,
212                                           struct lu_device *d)
213 {
214         struct ofd_object *of;
215
216         ENTRY;
217
218         OBD_SLAB_ALLOC_PTR_GFP(of, ofd_object_kmem, CFS_ALLOC_IO);
219         if (of != NULL) {
220                 struct lu_object        *o;
221                 struct lu_object_header *h;
222
223                 o = &of->ofo_obj.do_lu;
224                 h = &of->ofo_header;
225                 lu_object_header_init(h);
226                 lu_object_init(o, h, d);
227                 lu_object_add_top(h, o);
228                 o->lo_ops = &ofd_obj_ops;
229                 RETURN(o);
230         } else {
231                 RETURN(NULL);
232         }
233 }
234
235 static int ofd_start(const struct lu_env *env, struct lu_device *dev)
236 {
237         struct ofd_device       *ofd = ofd_dev(dev);
238         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
239         int                      rc;
240
241         ENTRY;
242
243         /* initialize lower device */
244         rc = next->ld_ops->ldo_prepare(env, dev, next);
245
246         RETURN(rc);
247 }
248
249 static int ofd_recovery_complete(const struct lu_env *env,
250                                  struct lu_device *dev)
251 {
252         struct ofd_device       *ofd = ofd_dev(dev);
253         struct lu_device        *next = &ofd->ofd_osd->dd_lu_dev;
254         int                      rc = 0;
255
256         ENTRY;
257
258         rc = next->ld_ops->ldo_recovery_complete(env, next);
259         RETURN(rc);
260 }
261
262 static struct lu_device_operations ofd_lu_ops = {
263         .ldo_object_alloc       = ofd_object_alloc,
264         .ldo_process_config     = ofd_process_config,
265         .ldo_recovery_complete  = ofd_recovery_complete,
266 };
267
268 extern int ost_handle(struct ptlrpc_request *req);
269
270 static int ofd_init0(const struct lu_env *env, struct ofd_device *m,
271                      struct lu_device_type *ldt, struct lustre_cfg *cfg)
272 {
273         const char              *dev = lustre_cfg_string(cfg, 0);
274         struct ofd_thread_info  *info = NULL;
275         struct obd_device       *obd;
276         int                      rc;
277
278         ENTRY;
279
280         obd = class_name2obd(dev);
281         if (obd == NULL) {
282                 CERROR("Cannot find obd with name %s\n", dev);
283                 RETURN(-ENODEV);
284         }
285
286         rc = lu_env_refill((struct lu_env *)env);
287         if (rc != 0)
288                 RETURN(rc);
289
290         obd->u.obt.obt_magic = OBT_MAGIC;
291
292         m->ofd_dt_dev.dd_lu_dev.ld_ops = &ofd_lu_ops;
293         m->ofd_dt_dev.dd_lu_dev.ld_obd = obd;
294         /* set this lu_device to obd, because error handling need it */
295         obd->obd_lu_dev = &m->ofd_dt_dev.dd_lu_dev;
296
297         /* No connection accepted until configurations will finish */
298         obd->obd_no_conn = 1;
299         obd->obd_replayable = 1;
300         if (cfg->lcfg_bufcount > 4 && LUSTRE_CFG_BUFLEN(cfg, 4) > 0) {
301                 char *str = lustre_cfg_string(cfg, 4);
302
303                 if (strchr(str, 'n')) {
304                         CWARN("%s: recovery disabled\n", obd->obd_name);
305                         obd->obd_replayable = 0;
306                 }
307         }
308
309         info = ofd_info_init(env, NULL);
310         if (info == NULL)
311                 RETURN(-EFAULT);
312
313         rc = lu_site_init(&m->ofd_site, &m->ofd_dt_dev.dd_lu_dev);
314         if (rc)
315                 GOTO(err_out, rc);
316         m->ofd_site.ls_top_dev = &m->ofd_dt_dev.dd_lu_dev;
317
318         rc = ofd_stack_init(env, m, cfg);
319         if (rc) {
320                 CERROR("Can't init device stack, rc %d\n", rc);
321                 GOTO(err_lu_site, rc);
322         }
323
324         dt_conf_get(env, m->ofd_osd, &m->ofd_dt_conf);
325
326         rc = ofd_start(env, &m->ofd_dt_dev.dd_lu_dev);
327         if (rc)
328                 GOTO(err_fini_stack, rc);
329
330         rc = lu_site_init_finish(&m->ofd_site);
331         if (rc)
332                 GOTO(err_fini_stack, rc);
333
334         RETURN(0);
335
336 err_fini_stack:
337         ofd_stack_fini(env, m, &m->ofd_osd->dd_lu_dev);
338 err_lu_site:
339         lu_site_fini(&m->ofd_site);
340 err_out:
341         return rc;
342 }
343
344 static void ofd_fini(const struct lu_env *env, struct ofd_device *m)
345 {
346         struct lu_device  *d = &m->ofd_dt_dev.dd_lu_dev;
347
348         ofd_stack_fini(env, m, m->ofd_site.ls_top_dev);
349         lu_site_fini(&m->ofd_site);
350         LASSERT(cfs_atomic_read(&d->ld_ref) == 0);
351         EXIT;
352 }
353
354 static struct lu_device *ofd_device_fini(const struct lu_env *env,
355                                          struct lu_device *d)
356 {
357         ENTRY;
358         ofd_fini(env, ofd_dev(d));
359         RETURN(NULL);
360 }
361
362 static struct lu_device *ofd_device_free(const struct lu_env *env,
363                                          struct lu_device *d)
364 {
365         struct ofd_device *m = ofd_dev(d);
366
367         dt_device_fini(&m->ofd_dt_dev);
368         OBD_FREE_PTR(m);
369         RETURN(NULL);
370 }
371
372 static struct lu_device *ofd_device_alloc(const struct lu_env *env,
373                                           struct lu_device_type *t,
374                                           struct lustre_cfg *cfg)
375 {
376         struct ofd_device *m;
377         struct lu_device  *l;
378         int                rc;
379
380         OBD_ALLOC_PTR(m);
381         if (m == NULL)
382                 return ERR_PTR(-ENOMEM);
383
384         l = &m->ofd_dt_dev.dd_lu_dev;
385         dt_device_init(&m->ofd_dt_dev, t);
386         rc = ofd_init0(env, m, t, cfg);
387         if (rc != 0) {
388                 ofd_device_free(env, l);
389                 l = ERR_PTR(rc);
390         }
391
392         return l;
393 }
394
395 /* thread context key constructor/destructor */
396 LU_KEY_INIT_FINI(ofd, struct ofd_thread_info);
397
398 static void ofd_key_exit(const struct lu_context *ctx,
399                          struct lu_context_key *key, void *data)
400 {
401         struct ofd_thread_info *info = data;
402
403         info->fti_env = NULL;
404 }
405
406 struct lu_context_key ofd_thread_key = {
407         .lct_tags = LCT_DT_THREAD,
408         .lct_init = ofd_key_init,
409         .lct_fini = ofd_key_fini,
410         .lct_exit = ofd_key_exit
411 };
412
413 /* type constructor/destructor: mdt_type_init, mdt_type_fini */
414 LU_TYPE_INIT_FINI(ofd, &ofd_thread_key);
415
416 static struct lu_device_type_operations ofd_device_type_ops = {
417         .ldto_init              = ofd_type_init,
418         .ldto_fini              = ofd_type_fini,
419
420         .ldto_start             = ofd_type_start,
421         .ldto_stop              = ofd_type_stop,
422
423         .ldto_device_alloc      = ofd_device_alloc,
424         .ldto_device_free       = ofd_device_free,
425         .ldto_device_fini       = ofd_device_fini
426 };
427
428 static struct lu_device_type ofd_device_type = {
429         .ldt_tags       = LU_DEVICE_DT,
430         .ldt_name       = LUSTRE_OST_NAME,
431         .ldt_ops        = &ofd_device_type_ops,
432         .ldt_ctx_tags   = LCT_DT_THREAD
433 };
434
435 int __init ofd_init(void)
436 {
437         struct lprocfs_static_vars      lvars;
438         int                             rc;
439
440         rc = lu_kmem_init(ofd_caches);
441         if (rc)
442                 return rc;
443
444         lprocfs_ofd_init_vars(&lvars);
445
446         rc = class_register_type(&ofd_obd_ops, NULL, lvars.module_vars,
447                                  LUSTRE_OST_NAME, &ofd_device_type);
448         return rc;
449 }
450
451 void __exit ofd_exit(void)
452 {
453         class_unregister_type(LUSTRE_OST_NAME);
454         lu_kmem_fini(ofd_caches);
455 }
456
457 MODULE_AUTHOR("Whamcloud, Inc. <http://www.whamcloud.com/>");
458 MODULE_DESCRIPTION("Lustre Object Filtering Device");
459 MODULE_LICENSE("GPL");
460
461 module_init(ofd_init);
462 module_exit(ofd_exit);