Whamcloud - gitweb
068f0d517c5f3e9d4d6aa93e46a053be00aace61
[fs/lustre-release.git] / lustre / osc / osc_dev.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * Implementation of cl_device, cl_req for OSC layer.
37  *
38  *   Author: Nikita Danilov <nikita.danilov@sun.com>
39  */
40
41 /** \addtogroup osc osc @{ */
42
43 #define DEBUG_SUBSYSTEM S_OSC
44
45 /* class_name2obd() */
46 #include <obd_class.h>
47
48 #include "osc_cl_internal.h"
49
50 cfs_mem_cache_t *osc_page_kmem;
51 cfs_mem_cache_t *osc_lock_kmem;
52 cfs_mem_cache_t *osc_object_kmem;
53 cfs_mem_cache_t *osc_thread_kmem;
54 cfs_mem_cache_t *osc_session_kmem;
55 cfs_mem_cache_t *osc_req_kmem;
56
57 struct lu_kmem_descr osc_caches[] = {
58         {
59                 .ckd_cache = &osc_page_kmem,
60                 .ckd_name  = "osc_page_kmem",
61                 .ckd_size  = sizeof (struct osc_page)
62         },
63         {
64                 .ckd_cache = &osc_lock_kmem,
65                 .ckd_name  = "osc_lock_kmem",
66                 .ckd_size  = sizeof (struct osc_lock)
67         },
68         {
69                 .ckd_cache = &osc_object_kmem,
70                 .ckd_name  = "osc_object_kmem",
71                 .ckd_size  = sizeof (struct osc_object)
72         },
73         {
74                 .ckd_cache = &osc_thread_kmem,
75                 .ckd_name  = "osc_thread_kmem",
76                 .ckd_size  = sizeof (struct osc_thread_info)
77         },
78         {
79                 .ckd_cache = &osc_session_kmem,
80                 .ckd_name  = "osc_session_kmem",
81                 .ckd_size  = sizeof (struct osc_session)
82         },
83         {
84                 .ckd_cache = &osc_req_kmem,
85                 .ckd_name  = "osc_req_kmem",
86                 .ckd_size  = sizeof (struct osc_req)
87         },
88         {
89                 .ckd_cache = NULL
90         }
91 };
92
93 struct lock_class_key osc_ast_guard_class;
94
95 /*****************************************************************************
96  *
97  * Type conversions.
98  *
99  */
100
101 static struct lu_device *osc2lu_dev(struct osc_device *osc)
102 {
103         return &osc->od_cl.cd_lu_dev;
104 }
105
106 /*****************************************************************************
107  *
108  * Osc device and device type functions.
109  *
110  */
111
112 static void *osc_key_init(const struct lu_context *ctx,
113                          struct lu_context_key *key)
114 {
115         struct osc_thread_info *info;
116
117         OBD_SLAB_ALLOC_PTR_GFP(info, osc_thread_kmem, CFS_ALLOC_IO);
118         if (info == NULL)
119                 info = ERR_PTR(-ENOMEM);
120         return info;
121 }
122
123 static void osc_key_fini(const struct lu_context *ctx,
124                          struct lu_context_key *key, void *data)
125 {
126         struct osc_thread_info *info = data;
127         OBD_SLAB_FREE_PTR(info, osc_thread_kmem);
128 }
129
130 struct lu_context_key osc_key = {
131         .lct_tags = LCT_CL_THREAD,
132         .lct_init = osc_key_init,
133         .lct_fini = osc_key_fini
134 };
135
136 static void *osc_session_init(const struct lu_context *ctx,
137                               struct lu_context_key *key)
138 {
139         struct osc_session *info;
140
141         OBD_SLAB_ALLOC_PTR_GFP(info, osc_session_kmem, CFS_ALLOC_IO);
142         if (info == NULL)
143                 info = ERR_PTR(-ENOMEM);
144         return info;
145 }
146
147 static void osc_session_fini(const struct lu_context *ctx,
148                              struct lu_context_key *key, void *data)
149 {
150         struct osc_session *info = data;
151         OBD_SLAB_FREE_PTR(info, osc_session_kmem);
152 }
153
154 struct lu_context_key osc_session_key = {
155         .lct_tags = LCT_SESSION,
156         .lct_init = osc_session_init,
157         .lct_fini = osc_session_fini
158 };
159
160 /* type constructor/destructor: osc_type_{init,fini,start,stop}(). */
161 LU_TYPE_INIT_FINI(osc, &osc_key, &osc_session_key);
162
163 static int osc_cl_process_config(const struct lu_env *env,
164                                  struct lu_device *d, struct lustre_cfg *cfg)
165 {
166         ENTRY;
167         RETURN(osc_process_config_base(d->ld_obd, cfg));
168 }
169
170 static const struct lu_device_operations osc_lu_ops = {
171         .ldo_object_alloc      = osc_object_alloc,
172         .ldo_process_config    = osc_cl_process_config,
173         .ldo_recovery_complete = NULL
174 };
175
176 static const struct cl_device_operations osc_cl_ops = {
177         .cdo_req_init = osc_req_init
178 };
179
180 static int osc_device_init(const struct lu_env *env, struct lu_device *d,
181                            const char *name, struct lu_device *next)
182 {
183         RETURN(0);
184 }
185
186 static struct lu_device *osc_device_fini(const struct lu_env *env,
187                                          struct lu_device *d)
188 {
189         return 0;
190 }
191
192 static struct lu_device *osc_device_free(const struct lu_env *env,
193                                          struct lu_device *d)
194 {
195         struct osc_device *od = lu2osc_dev(d);
196
197         cl_device_fini(lu2cl_dev(d));
198         OBD_FREE_PTR(od);
199         return NULL;
200 }
201
202 static struct lu_device *osc_device_alloc(const struct lu_env *env,
203                                           struct lu_device_type *t,
204                                           struct lustre_cfg *cfg)
205 {
206         struct lu_device *d;
207         struct osc_device *od;
208         struct obd_device *obd;
209         int rc;
210
211         OBD_ALLOC_PTR(od);
212         if (od == NULL)
213                 RETURN(ERR_PTR(-ENOMEM));
214
215         cl_device_init(&od->od_cl, t);
216         d = osc2lu_dev(od);
217         d->ld_ops = &osc_lu_ops;
218         od->od_cl.cd_ops = &osc_cl_ops;
219
220         /* Setup OSC OBD */
221         obd = class_name2obd(lustre_cfg_string(cfg, 0));
222         LASSERT(obd != NULL);
223         rc = osc_setup(obd, cfg);
224         if (rc) {
225                 osc_device_free(env, d);
226                 RETURN(ERR_PTR(rc));
227         }
228         od->od_exp = obd->obd_self_export;
229         RETURN(d);
230 }
231
232 static const struct lu_device_type_operations osc_device_type_ops = {
233         .ldto_init = osc_type_init,
234         .ldto_fini = osc_type_fini,
235
236         .ldto_start = osc_type_start,
237         .ldto_stop  = osc_type_stop,
238
239         .ldto_device_alloc = osc_device_alloc,
240         .ldto_device_free  = osc_device_free,
241
242         .ldto_device_init    = osc_device_init,
243         .ldto_device_fini    = osc_device_fini
244 };
245
246 struct lu_device_type osc_device_type = {
247         .ldt_tags     = LU_DEVICE_CL,
248         .ldt_name     = LUSTRE_OSC_NAME,
249         .ldt_ops      = &osc_device_type_ops,
250         .ldt_ctx_tags = LCT_CL_THREAD
251 };
252
253 /** @} osc */