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