Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[fs/lustre-release.git] / lustre / lov / lov_ea.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lov/lov_ea.c
37  *
38  * Author: Wang Di <wangdi@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LOV
42
43 #ifdef __KERNEL__
44 #include <asm/div64.h>
45 #include <libcfs/libcfs.h>
46 #else
47 #include <liblustre.h>
48 #endif
49
50 #include <obd_class.h>
51 #include <obd_lov.h>
52 #include <lustre/lustre_idl.h>
53 #include <lustre_log.h>
54
55 #include "lov_internal.h"
56
57 struct lovea_unpack_args {
58         struct lov_stripe_md *lsm;
59         int                   cursor;
60 };
61
62 static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
63                                  __u16 stripe_count)
64 {
65
66         if (stripe_count == 0 || stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
67                 CERROR("bad stripe count %d\n", stripe_count);
68                 lov_dump_lmm(D_WARNING, lmm);
69                 return -EINVAL;
70         }
71
72         if (lmm->lmm_object_id == 0) {
73                 CERROR("zero object id\n");
74                 lov_dump_lmm(D_WARNING, lmm);
75                 return -EINVAL;
76         }
77
78         if (lmm->lmm_pattern != cpu_to_le32(LOV_PATTERN_RAID0)) {
79                 CERROR("bad striping pattern\n");
80                 lov_dump_lmm(D_WARNING, lmm);
81                 return -EINVAL;
82         }
83
84         if (lmm->lmm_stripe_size == 0 ||
85              (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
86                 CERROR("bad stripe size %u\n",
87                        le32_to_cpu(lmm->lmm_stripe_size));
88                 lov_dump_lmm(D_WARNING, lmm);
89                 return -EINVAL;
90         }
91         return 0;
92 }
93
94 struct lov_stripe_md *lsm_alloc_plain(__u16 stripe_count, int *size)
95 {
96         struct lov_stripe_md *lsm;
97         struct lov_oinfo     *loi;
98         int                   i, oinfo_ptrs_size;
99
100         LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
101
102         oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
103         *size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;
104
105         OBD_ALLOC_LARGE(lsm, *size);
106         if (!lsm)
107                 return NULL;;
108
109         for (i = 0; i < stripe_count; i++) {
110                 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, CFS_ALLOC_IO);
111                 if (loi == NULL)
112                         goto err;
113                 lsm->lsm_oinfo[i] = loi;
114         }
115         lsm->lsm_stripe_count = stripe_count;
116         return lsm;
117
118 err:
119         while (--i >= 0)
120                 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
121         OBD_FREE_LARGE(lsm, *size);
122         return NULL;
123 }
124
125 void lsm_free_plain(struct lov_stripe_md *lsm)
126 {
127         __u16 stripe_count = lsm->lsm_stripe_count;
128         int i;
129
130         for (i = 0; i < stripe_count; i++)
131                 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
132                               sizeof(struct lov_oinfo));
133         OBD_FREE_LARGE(lsm, sizeof(struct lov_stripe_md) +
134                        stripe_count * sizeof(struct lov_oinfo *));
135 }
136
137 static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
138                                 struct lov_mds_md *lmm)
139 {
140         /*
141          * This supposes lov_mds_md_v1/v3 first fields are
142          * are the same
143          */
144         lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
145         lsm->lsm_object_seq = le64_to_cpu(lmm->lmm_object_seq);
146         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
147         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
148         lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
149         lsm->lsm_pool_name[0] = '\0';
150 }
151
152 static void
153 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
154                            obd_off *lov_off, obd_off *swidth)
155 {
156         if (swidth)
157                 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
158 }
159
160 static void
161 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
162                            obd_off *lov_off, obd_off *swidth)
163 {
164         if (swidth)
165                 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
166 }
167
168 static int lsm_destroy_plain(struct lov_stripe_md *lsm, struct obdo *oa,
169                              struct obd_export *md_exp)
170 {
171         return 0;
172 }
173
174 /* Find minimum stripe maxbytes value.  For inactive or
175  * reconnecting targets use LUSTRE_STRIPE_MAXBYTES. */
176 static void lov_tgt_maxbytes(struct lov_tgt_desc *tgt, __u64 *stripe_maxbytes)
177 {
178         struct obd_import *imp = tgt->ltd_obd->u.cli.cl_import;
179
180         if (imp == NULL || !tgt->ltd_active) {
181                 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
182                 return;
183         }
184
185         cfs_spin_lock(&imp->imp_lock);
186         if (imp->imp_state == LUSTRE_IMP_FULL &&
187            (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
188            imp->imp_connect_data.ocd_maxbytes > 0) {
189                 if (*stripe_maxbytes > imp->imp_connect_data.ocd_maxbytes)
190                         *stripe_maxbytes = imp->imp_connect_data.ocd_maxbytes;
191         } else {
192                 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
193         }
194         cfs_spin_unlock(&imp->imp_lock);
195 }
196
197 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
198                              __u16 *stripe_count)
199 {
200         if (lmm_bytes < sizeof(*lmm)) {
201                 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
202                        lmm_bytes, (int)sizeof(*lmm));
203                 return -EINVAL;
204         }
205
206         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
207
208         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
209                 CERROR("LOV EA V1 too small: %d, need %d\n",
210                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
211                 lov_dump_lmm_v1(D_WARNING, lmm);
212                 return -EINVAL;
213         }
214
215         return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
216 }
217
218 int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
219                     struct lov_mds_md_v1 *lmm)
220 {
221         struct lov_oinfo *loi;
222         int i;
223         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
224
225         lsm_unpackmd_common(lsm, lmm);
226
227         for (i = 0; i < lsm->lsm_stripe_count; i++) {
228                 /* XXX LOV STACKING call down to osc_unpackmd() */
229                 loi = lsm->lsm_oinfo[i];
230                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
231                 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
232                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
233                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
234                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
235                         CERROR("OST index %d more than OST count %d\n",
236                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
237                         lov_dump_lmm_v1(D_WARNING, lmm);
238                         return -EINVAL;
239                 }
240                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
241                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
242                         lov_dump_lmm_v1(D_WARNING, lmm);
243                         return -EINVAL;
244                 }
245                 /* calculate the minimum stripe max bytes */
246                 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
247                                  &stripe_maxbytes);
248         }
249
250         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
251
252         return 0;
253 }
254
255 const struct lsm_operations lsm_v1_ops = {
256         .lsm_free            = lsm_free_plain,
257         .lsm_destroy         = lsm_destroy_plain,
258         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
259         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
260         .lsm_lmm_verify         = lsm_lmm_verify_v1,
261         .lsm_unpackmd           = lsm_unpackmd_v1,
262 };
263
264 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
265                              __u16 *stripe_count)
266 {
267         struct lov_mds_md_v3 *lmm;
268
269         lmm = (struct lov_mds_md_v3 *)lmmv1;
270
271         if (lmm_bytes < sizeof(*lmm)) {
272                 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
273                        lmm_bytes, (int)sizeof(*lmm));
274                 return -EINVAL;
275         }
276
277         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
278
279         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
280                 CERROR("LOV EA V3 too small: %d, need %d\n",
281                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
282                 lov_dump_lmm_v3(D_WARNING, lmm);
283                 return -EINVAL;
284         }
285
286         return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
287                                      *stripe_count);
288 }
289
290 int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
291                     struct lov_mds_md *lmmv1)
292 {
293         struct lov_mds_md_v3 *lmm;
294         struct lov_oinfo *loi;
295         int i;
296         __u64 stripe_maxbytes = OBD_OBJECT_EOF;
297
298         lmm = (struct lov_mds_md_v3 *)lmmv1;
299
300         lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);
301         strncpy(lsm->lsm_pool_name, lmm->lmm_pool_name, LOV_MAXPOOLNAME);
302
303         for (i = 0; i < lsm->lsm_stripe_count; i++) {
304                 /* XXX LOV STACKING call down to osc_unpackmd() */
305                 loi = lsm->lsm_oinfo[i];
306                 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
307                 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
308                 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
309                 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
310                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
311                         CERROR("OST index %d more than OST count %d\n",
312                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
313                         lov_dump_lmm_v3(D_WARNING, lmm);
314                         return -EINVAL;
315                 }
316                 if (!lov->lov_tgts[loi->loi_ost_idx]) {
317                         CERROR("OST index %d missing\n", loi->loi_ost_idx);
318                         lov_dump_lmm_v3(D_WARNING, lmm);
319                         return -EINVAL;
320                 }
321                 /* calculate the minimum stripe max bytes */
322                 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
323                                  &stripe_maxbytes);
324         }
325
326         lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
327
328         return 0;
329 }
330
331 const struct lsm_operations lsm_v3_ops = {
332         .lsm_free            = lsm_free_plain,
333         .lsm_destroy         = lsm_destroy_plain,
334         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
335         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
336         .lsm_lmm_verify         = lsm_lmm_verify_v3,
337         .lsm_unpackmd           = lsm_unpackmd_v3,
338 };
339