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