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