GNU Linux-libre 4.14.266-gnu1
[releases.git] / drivers / staging / lustre / 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
42 #include "osc_cl_internal.h"
43
44 /** \addtogroup osc
45  * @{
46  */
47
48 struct kmem_cache *osc_lock_kmem;
49 struct kmem_cache *osc_object_kmem;
50 struct kmem_cache *osc_thread_kmem;
51 struct kmem_cache *osc_session_kmem;
52 struct kmem_cache *osc_extent_kmem;
53 struct kmem_cache *osc_quota_kmem;
54
55 struct lu_kmem_descr osc_caches[] = {
56         {
57                 .ckd_cache = &osc_lock_kmem,
58                 .ckd_name  = "osc_lock_kmem",
59                 .ckd_size  = sizeof(struct osc_lock)
60         },
61         {
62                 .ckd_cache = &osc_object_kmem,
63                 .ckd_name  = "osc_object_kmem",
64                 .ckd_size  = sizeof(struct osc_object)
65         },
66         {
67                 .ckd_cache = &osc_thread_kmem,
68                 .ckd_name  = "osc_thread_kmem",
69                 .ckd_size  = sizeof(struct osc_thread_info)
70         },
71         {
72                 .ckd_cache = &osc_session_kmem,
73                 .ckd_name  = "osc_session_kmem",
74                 .ckd_size  = sizeof(struct osc_session)
75         },
76         {
77                 .ckd_cache = &osc_extent_kmem,
78                 .ckd_name  = "osc_extent_kmem",
79                 .ckd_size  = sizeof(struct osc_extent)
80         },
81         {
82                 .ckd_cache = &osc_quota_kmem,
83                 .ckd_name  = "osc_quota_kmem",
84                 .ckd_size  = sizeof(struct osc_quota_info)
85         },
86         {
87                 .ckd_cache = NULL
88         }
89 };
90
91 /*****************************************************************************
92  *
93  * Type conversions.
94  *
95  */
96
97 static struct lu_device *osc2lu_dev(struct osc_device *osc)
98 {
99         return &osc->od_cl.cd_lu_dev;
100 }
101
102 /*****************************************************************************
103  *
104  * Osc device and device type functions.
105  *
106  */
107
108 static void *osc_key_init(const struct lu_context *ctx,
109                           struct lu_context_key *key)
110 {
111         struct osc_thread_info *info;
112
113         info = kmem_cache_zalloc(osc_thread_kmem, GFP_NOFS);
114         if (!info)
115                 info = ERR_PTR(-ENOMEM);
116         return info;
117 }
118
119 static void osc_key_fini(const struct lu_context *ctx,
120                          struct lu_context_key *key, void *data)
121 {
122         struct osc_thread_info *info = data;
123
124         kmem_cache_free(osc_thread_kmem, info);
125 }
126
127 struct lu_context_key osc_key = {
128         .lct_tags = LCT_CL_THREAD,
129         .lct_init = osc_key_init,
130         .lct_fini = osc_key_fini
131 };
132
133 static void *osc_session_init(const struct lu_context *ctx,
134                               struct lu_context_key *key)
135 {
136         struct osc_session *info;
137
138         info = kmem_cache_zalloc(osc_session_kmem, GFP_NOFS);
139         if (!info)
140                 info = ERR_PTR(-ENOMEM);
141         return info;
142 }
143
144 static void osc_session_fini(const struct lu_context *ctx,
145                              struct lu_context_key *key, void *data)
146 {
147         struct osc_session *info = data;
148
149         kmem_cache_free(osc_session_kmem, info);
150 }
151
152 struct lu_context_key osc_session_key = {
153         .lct_tags = LCT_SESSION,
154         .lct_init = osc_session_init,
155         .lct_fini = osc_session_fini
156 };
157
158 /* type constructor/destructor: osc_type_{init,fini,start,stop}(). */
159 LU_TYPE_INIT_FINI(osc, &osc_key, &osc_session_key);
160
161 static int osc_cl_process_config(const struct lu_env *env,
162                                  struct lu_device *d, struct lustre_cfg *cfg)
163 {
164         return osc_process_config_base(d->ld_obd, cfg);
165 }
166
167 static const struct lu_device_operations osc_lu_ops = {
168         .ldo_object_alloc      = osc_object_alloc,
169         .ldo_process_config    = osc_cl_process_config,
170         .ldo_recovery_complete = NULL
171 };
172
173 static int osc_device_init(const struct lu_env *env, struct lu_device *d,
174                            const char *name, struct lu_device *next)
175 {
176         return 0;
177 }
178
179 static struct lu_device *osc_device_fini(const struct lu_env *env,
180                                          struct lu_device *d)
181 {
182         return NULL;
183 }
184
185 static struct lu_device *osc_device_free(const struct lu_env *env,
186                                          struct lu_device *d)
187 {
188         struct osc_device *od = lu2osc_dev(d);
189
190         cl_device_fini(lu2cl_dev(d));
191         kfree(od);
192         return NULL;
193 }
194
195 static struct lu_device *osc_device_alloc(const struct lu_env *env,
196                                           struct lu_device_type *t,
197                                           struct lustre_cfg *cfg)
198 {
199         struct lu_device *d;
200         struct osc_device *od;
201         struct obd_device *obd;
202         int rc;
203
204         od = kzalloc(sizeof(*od), GFP_NOFS);
205         if (!od)
206                 return ERR_PTR(-ENOMEM);
207
208         cl_device_init(&od->od_cl, t);
209         d = osc2lu_dev(od);
210         d->ld_ops = &osc_lu_ops;
211
212         /* Setup OSC OBD */
213         obd = class_name2obd(lustre_cfg_string(cfg, 0));
214         LASSERT(obd);
215         rc = osc_setup(obd, cfg);
216         if (rc) {
217                 osc_device_free(env, d);
218                 return ERR_PTR(rc);
219         }
220         od->od_exp = obd->obd_self_export;
221         return d;
222 }
223
224 static const struct lu_device_type_operations osc_device_type_ops = {
225         .ldto_init = osc_type_init,
226         .ldto_fini = osc_type_fini,
227
228         .ldto_start = osc_type_start,
229         .ldto_stop  = osc_type_stop,
230
231         .ldto_device_alloc = osc_device_alloc,
232         .ldto_device_free  = osc_device_free,
233
234         .ldto_device_init = osc_device_init,
235         .ldto_device_fini = osc_device_fini
236 };
237
238 struct lu_device_type osc_device_type = {
239         .ldt_tags = LU_DEVICE_CL,
240         .ldt_name = LUSTRE_OSC_NAME,
241         .ldt_ops = &osc_device_type_ops,
242         .ldt_ctx_tags = LCT_CL_THREAD
243 };
244
245 /** @} osc */