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