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