Whamcloud - gitweb
LU-3285 mdc: add cl_device to the MDC
[fs/lustre-release.git] / lustre / osc / osc_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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * Implementation of cl_device, for OSC layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_OSC
38
39 /* class_name2obd() */
40 #include <obd_class.h>
41 #include <lustre_osc.h>
42
43 #include "osc_internal.h"
44
45 /** \addtogroup osc
46  * @{
47  */
48
49 struct kmem_cache *osc_lock_kmem;
50 EXPORT_SYMBOL(osc_lock_kmem);
51 struct kmem_cache *osc_object_kmem;
52 EXPORT_SYMBOL(osc_object_kmem);
53
54 struct kmem_cache *osc_thread_kmem;
55 struct kmem_cache *osc_session_kmem;
56 struct kmem_cache *osc_extent_kmem;
57 EXPORT_SYMBOL(osc_extent_kmem);
58 struct kmem_cache *osc_quota_kmem;
59 EXPORT_SYMBOL(osc_quota_kmem);
60
61 struct lu_kmem_descr osc_caches[] = {
62         {
63                 .ckd_cache = &osc_lock_kmem,
64                 .ckd_name  = "osc_lock_kmem",
65                 .ckd_size  = sizeof (struct osc_lock)
66         },
67         {
68                 .ckd_cache = &osc_object_kmem,
69                 .ckd_name  = "osc_object_kmem",
70                 .ckd_size  = sizeof (struct osc_object)
71         },
72         {
73                 .ckd_cache = &osc_thread_kmem,
74                 .ckd_name  = "osc_thread_kmem",
75                 .ckd_size  = sizeof (struct osc_thread_info)
76         },
77         {
78                 .ckd_cache = &osc_session_kmem,
79                 .ckd_name  = "osc_session_kmem",
80                 .ckd_size  = sizeof (struct osc_session)
81         },
82         {
83                 .ckd_cache = &osc_extent_kmem,
84                 .ckd_name  = "osc_extent_kmem",
85                 .ckd_size  = sizeof (struct osc_extent)
86         },
87         {
88                 .ckd_cache = &osc_quota_kmem,
89                 .ckd_name  = "osc_quota_kmem",
90                 .ckd_size  = sizeof(struct osc_quota_info)
91         },
92         {
93                 .ckd_cache = NULL
94         }
95 };
96
97 /*****************************************************************************
98  *
99  * Osc device and device type functions.
100  *
101  */
102
103 static void *osc_key_init(const struct lu_context *ctx,
104                           struct lu_context_key *key)
105 {
106         struct osc_thread_info *info;
107
108         OBD_SLAB_ALLOC_PTR_GFP(info, osc_thread_kmem, GFP_NOFS);
109         if (info == NULL)
110                 info = ERR_PTR(-ENOMEM);
111         return info;
112 }
113
114 static void osc_key_fini(const struct lu_context *ctx,
115                          struct lu_context_key *key, void *data)
116 {
117         struct osc_thread_info *info = data;
118
119         lu_buf_free(&info->oti_ladvise_buf);
120         OBD_SLAB_FREE_PTR(info, osc_thread_kmem);
121 }
122
123 struct lu_context_key osc_key = {
124         .lct_tags = LCT_CL_THREAD,
125         .lct_init = osc_key_init,
126         .lct_fini = osc_key_fini
127 };
128
129 static void *osc_session_init(const struct lu_context *ctx,
130                               struct lu_context_key *key)
131 {
132         struct osc_session *info;
133
134         OBD_SLAB_ALLOC_PTR_GFP(info, osc_session_kmem, GFP_NOFS);
135         if (info == NULL)
136                 info = ERR_PTR(-ENOMEM);
137         return info;
138 }
139
140 static void osc_session_fini(const struct lu_context *ctx,
141                              struct lu_context_key *key, void *data)
142 {
143         struct osc_session *info = data;
144         OBD_SLAB_FREE_PTR(info, osc_session_kmem);
145 }
146
147 struct lu_context_key osc_session_key = {
148         .lct_tags = LCT_SESSION,
149         .lct_init = osc_session_init,
150         .lct_fini = osc_session_fini
151 };
152
153 /* type constructor/destructor: osc_type_{init,fini,start,stop}(). */
154 LU_TYPE_INIT_FINI(osc, &osc_key, &osc_session_key);
155
156 static int osc_cl_process_config(const struct lu_env *env,
157                                  struct lu_device *d, struct lustre_cfg *cfg)
158 {
159         ENTRY;
160         RETURN(osc_process_config_base(d->ld_obd, cfg));
161 }
162
163 static const struct lu_device_operations osc_lu_ops = {
164         .ldo_object_alloc      = osc_object_alloc,
165         .ldo_process_config    = osc_cl_process_config,
166         .ldo_recovery_complete = NULL
167 };
168
169 int osc_device_init(const struct lu_env *env, struct lu_device *d,
170                     const char *name, struct lu_device *next)
171 {
172         RETURN(0);
173 }
174 EXPORT_SYMBOL(osc_device_init);
175
176 struct lu_device *osc_device_fini(const struct lu_env *env,
177                                   struct lu_device *d)
178 {
179         return NULL;
180 }
181 EXPORT_SYMBOL(osc_device_fini);
182
183 struct lu_device *osc_device_free(const struct lu_env *env,
184                                   struct lu_device *d)
185 {
186         struct osc_device *od = lu2osc_dev(d);
187
188         cl_device_fini(lu2cl_dev(d));
189         OBD_FREE_PTR(od);
190         return NULL;
191 }
192 EXPORT_SYMBOL(osc_device_free);
193
194 static struct lu_device *osc_device_alloc(const struct lu_env *env,
195                                           struct lu_device_type *t,
196                                           struct lustre_cfg *cfg)
197 {
198         struct lu_device *d;
199         struct osc_device *od;
200         struct obd_device *obd;
201         int rc;
202
203         OBD_ALLOC_PTR(od);
204         if (od == NULL)
205                 RETURN(ERR_PTR(-ENOMEM));
206
207         cl_device_init(&od->od_cl, t);
208         d = osc2lu_dev(od);
209         d->ld_ops = &osc_lu_ops;
210
211         /* Setup OSC OBD */
212         obd = class_name2obd(lustre_cfg_string(cfg, 0));
213         LASSERT(obd != NULL);
214         rc = osc_setup(obd, cfg);
215         if (rc) {
216                 osc_device_free(env, d);
217                 RETURN(ERR_PTR(rc));
218         }
219         od->od_exp = obd->obd_self_export;
220         RETURN(d);
221 }
222
223 static const struct lu_device_type_operations osc_device_type_ops = {
224         .ldto_init = osc_type_init,
225         .ldto_fini = osc_type_fini,
226
227         .ldto_start = osc_type_start,
228         .ldto_stop  = osc_type_stop,
229
230         .ldto_device_alloc = osc_device_alloc,
231         .ldto_device_free  = osc_device_free,
232
233         .ldto_device_init    = osc_device_init,
234         .ldto_device_fini    = osc_device_fini
235 };
236
237 struct lu_device_type osc_device_type = {
238         .ldt_tags     = LU_DEVICE_CL,
239         .ldt_name     = LUSTRE_OSC_NAME,
240         .ldt_ops      = &osc_device_type_ops,
241         .ldt_ctx_tags = LCT_CL_THREAD
242 };
243
244 /** @} osc */