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