GNU Linux-libre 4.9-gnu1
[releases.git] / drivers / staging / lustre / lustre / lov / lovsub_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) 2013, 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 and cl_device_type for LOVSUB layer.
33  *
34  *   Author: Nikita Danilov <nikita.danilov@sun.com>
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOV
38
39 #include "lov_cl_internal.h"
40
41 /** \addtogroup lov
42  *  @{
43  */
44
45 /*****************************************************************************
46  *
47  * Lovsub transfer operations.
48  *
49  */
50
51 static void lovsub_req_completion(const struct lu_env *env,
52                                   const struct cl_req_slice *slice, int ioret)
53 {
54         struct lovsub_req *lsr;
55
56         lsr = cl2lovsub_req(slice);
57         kmem_cache_free(lovsub_req_kmem, lsr);
58 }
59
60 /**
61  * Implementation of struct cl_req_operations::cro_attr_set() for lovsub
62  * layer. Lov and lovsub are responsible only for struct obdo::o_stripe_idx
63  * field, which is filled there.
64  */
65 static void lovsub_req_attr_set(const struct lu_env *env,
66                                 const struct cl_req_slice *slice,
67                                 const struct cl_object *obj,
68                                 struct cl_req_attr *attr, u64 flags)
69 {
70         struct lovsub_object *subobj;
71
72         subobj = cl2lovsub(obj);
73         /*
74          * There is no OBD_MD_* flag for obdo::o_stripe_idx, so set it
75          * unconditionally. It never changes anyway.
76          */
77         attr->cra_oa->o_stripe_idx = subobj->lso_index;
78 }
79
80 static const struct cl_req_operations lovsub_req_ops = {
81         .cro_attr_set   = lovsub_req_attr_set,
82         .cro_completion = lovsub_req_completion
83 };
84
85 /*****************************************************************************
86  *
87  * Lov-sub device and device type functions.
88  *
89  */
90
91 static int lovsub_device_init(const struct lu_env *env, struct lu_device *d,
92                               const char *name, struct lu_device *next)
93 {
94         struct lovsub_device  *lsd = lu2lovsub_dev(d);
95         struct lu_device_type *ldt;
96         int rc;
97
98         next->ld_site = d->ld_site;
99         ldt = next->ld_type;
100         rc = ldt->ldt_ops->ldto_device_init(env, next, ldt->ldt_name, NULL);
101         if (rc) {
102                 next->ld_site = NULL;
103                 return rc;
104         }
105
106         lu_device_get(next);
107         lu_ref_add(&next->ld_reference, "lu-stack", &lu_site_init);
108         lsd->acid_next = lu2cl_dev(next);
109         return rc;
110 }
111
112 static struct lu_device *lovsub_device_fini(const struct lu_env *env,
113                                             struct lu_device *d)
114 {
115         struct lu_device *next;
116         struct lovsub_device *lsd;
117
118         lsd = lu2lovsub_dev(d);
119         next = cl2lu_dev(lsd->acid_next);
120         lsd->acid_super = NULL;
121         lsd->acid_next = NULL;
122         return next;
123 }
124
125 static struct lu_device *lovsub_device_free(const struct lu_env *env,
126                                             struct lu_device *d)
127 {
128         struct lovsub_device *lsd  = lu2lovsub_dev(d);
129         struct lu_device     *next = cl2lu_dev(lsd->acid_next);
130
131         if (atomic_read(&d->ld_ref) && d->ld_site) {
132                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_ERROR, NULL);
133                 lu_site_print(env, d->ld_site, &msgdata, lu_cdebug_printer);
134         }
135         cl_device_fini(lu2cl_dev(d));
136         kfree(lsd);
137         return next;
138 }
139
140 static int lovsub_req_init(const struct lu_env *env, struct cl_device *dev,
141                            struct cl_req *req)
142 {
143         struct lovsub_req *lsr;
144         int result;
145
146         lsr = kmem_cache_zalloc(lovsub_req_kmem, GFP_NOFS);
147         if (lsr) {
148                 cl_req_slice_add(req, &lsr->lsrq_cl, dev, &lovsub_req_ops);
149                 result = 0;
150         } else {
151                 result = -ENOMEM;
152         }
153         return result;
154 }
155
156 static const struct lu_device_operations lovsub_lu_ops = {
157         .ldo_object_alloc      = lovsub_object_alloc,
158         .ldo_process_config    = NULL,
159         .ldo_recovery_complete = NULL
160 };
161
162 static const struct cl_device_operations lovsub_cl_ops = {
163         .cdo_req_init = lovsub_req_init
164 };
165
166 static struct lu_device *lovsub_device_alloc(const struct lu_env *env,
167                                              struct lu_device_type *t,
168                                              struct lustre_cfg *cfg)
169 {
170         struct lu_device     *d;
171         struct lovsub_device *lsd;
172
173         lsd = kzalloc(sizeof(*lsd), GFP_NOFS);
174         if (lsd) {
175                 int result;
176
177                 result = cl_device_init(&lsd->acid_cl, t);
178                 if (result == 0) {
179                         d = lovsub2lu_dev(lsd);
180                         d->ld_ops        = &lovsub_lu_ops;
181                         lsd->acid_cl.cd_ops = &lovsub_cl_ops;
182                 } else {
183                         d = ERR_PTR(result);
184                 }
185         } else {
186                 d = ERR_PTR(-ENOMEM);
187         }
188         return d;
189 }
190
191 static const struct lu_device_type_operations lovsub_device_type_ops = {
192         .ldto_device_alloc = lovsub_device_alloc,
193         .ldto_device_free  = lovsub_device_free,
194
195         .ldto_device_init    = lovsub_device_init,
196         .ldto_device_fini    = lovsub_device_fini
197 };
198
199 #define LUSTRE_LOVSUB_NAME       "lovsub"
200
201 struct lu_device_type lovsub_device_type = {
202         .ldt_tags     = LU_DEVICE_CL,
203         .ldt_name     = LUSTRE_LOVSUB_NAME,
204         .ldt_ops      = &lovsub_device_type_ops,
205         .ldt_ctx_tags = LCT_CL_THREAD
206 };
207
208 /** @} lov */