Whamcloud - gitweb
LU-7890 lov: Ensure correct operation for large object sizes
[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, 2015, 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)
83 {
84         struct lov_stripe_md *lsm;
85         struct lov_oinfo     *loi;
86         size_t lsm_size;
87         size_t oinfo_ptrs_size;
88         int i;
89
90         LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
91
92         oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
93         lsm_size = sizeof(*lsm) + oinfo_ptrs_size;
94
95         OBD_ALLOC_LARGE(lsm, lsm_size);
96         if (!lsm)
97                 return NULL;
98
99         for (i = 0; i < stripe_count; i++) {
100                 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
101                 if (loi == NULL)
102                         goto err;
103                 lsm->lsm_oinfo[i] = loi;
104         }
105         lsm->lsm_stripe_count = stripe_count;
106         return lsm;
107
108 err:
109         while (--i >= 0)
110                 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
111
112         OBD_FREE_LARGE(lsm, lsm_size);
113
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 /* Find minimum stripe maxbytes value.  For inactive or
130  * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES. */
131 static loff_t lov_tgt_maxbytes(struct lov_tgt_desc *tgt)
132 {
133         struct obd_import *imp;
134         loff_t maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
135
136         if (!tgt->ltd_active)
137                 return maxbytes;
138
139         imp = tgt->ltd_obd->u.cli.cl_import;
140         if (imp == NULL)
141                 return maxbytes;
142
143         spin_lock(&imp->imp_lock);
144         if (imp->imp_state == LUSTRE_IMP_FULL &&
145             (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
146             imp->imp_connect_data.ocd_maxbytes > 0)
147                 maxbytes = imp->imp_connect_data.ocd_maxbytes;
148
149         spin_unlock(&imp->imp_lock);
150
151         return maxbytes;
152 }
153
154 static int lsm_unpackmd_common(struct lov_obd *lov,
155                                struct lov_stripe_md *lsm,
156                                struct lov_mds_md *lmm,
157                                struct lov_ost_data_v1 *objects)
158 {
159         struct lov_oinfo *loi;
160         loff_t min_stripe_maxbytes = 0;
161         loff_t lov_bytes;
162         unsigned int stripe_count;
163         unsigned int i;
164
165         /*
166          * This supposes lov_mds_md_v1/v3 first fields are
167          * are the same
168          */
169         lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
170         lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
171         lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
172         lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
173         lsm->lsm_pool_name[0] = '\0';
174
175         stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
176
177         for (i = 0; i < stripe_count; i++) {
178                 loi = lsm->lsm_oinfo[i];
179                 ostid_le_to_cpu(&objects[i].l_ost_oi, &loi->loi_oi);
180                 loi->loi_ost_idx = le32_to_cpu(objects[i].l_ost_idx);
181                 loi->loi_ost_gen = le32_to_cpu(objects[i].l_ost_gen);
182                 if (lov_oinfo_is_dummy(loi))
183                         continue;
184
185                 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count &&
186                     !lov2obd(lov)->obd_process_conf) {
187                         CERROR("%s: OST index %d more than OST count %d\n",
188                                (char*)lov->desc.ld_uuid.uuid,
189                                loi->loi_ost_idx, lov->desc.ld_tgt_count);
190                         lov_dump_lmm_v1(D_WARNING, lmm);
191                         return -EINVAL;
192                 }
193
194                 if (lov->lov_tgts[loi->loi_ost_idx] == NULL) {
195                         CERROR("%s: OST index %d missing\n",
196                                (char*)lov->desc.ld_uuid.uuid, loi->loi_ost_idx);
197                         lov_dump_lmm_v1(D_WARNING, lmm);
198                         continue;
199                 }
200
201                 lov_bytes = lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx]);
202                 if (min_stripe_maxbytes == 0 || lov_bytes < min_stripe_maxbytes)
203                         min_stripe_maxbytes = lov_bytes;
204         }
205
206         if (min_stripe_maxbytes == 0)
207                 min_stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
208
209         stripe_count = lsm->lsm_stripe_count ?: lov->desc.ld_tgt_count;
210         lov_bytes = min_stripe_maxbytes * stripe_count;
211
212         if (lov_bytes < min_stripe_maxbytes) /* handle overflow */
213                 lsm->lsm_maxbytes = MAX_LFS_FILESIZE;
214         else
215                 lsm->lsm_maxbytes = lov_bytes;
216
217         return 0;
218 }
219
220 static void
221 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
222                           loff_t *lov_off, loff_t *swidth)
223 {
224         if (swidth != NULL)
225                 *swidth = (loff_t)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
226 }
227
228 static void
229 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
230                            loff_t *lov_off, loff_t *swidth)
231 {
232         if (swidth != NULL)
233                 *swidth = (loff_t)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
234 }
235
236 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
237                              __u16 *stripe_count)
238 {
239         if (lmm_bytes < sizeof(*lmm)) {
240                 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
241                        lmm_bytes, (int)sizeof(*lmm));
242                 return -EINVAL;
243         }
244
245         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
246         if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
247                 *stripe_count = 0;
248
249         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
250                 CERROR("LOV EA V1 too small: %d, need %d\n",
251                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
252                 lov_dump_lmm_common(D_WARNING, lmm);
253                 return -EINVAL;
254         }
255
256         return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
257 }
258
259 static int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
260                            struct lov_mds_md_v1 *lmm)
261 {
262         return lsm_unpackmd_common(lov, lsm, lmm, lmm->lmm_objects);
263 }
264
265 const struct lsm_operations lsm_v1_ops = {
266         .lsm_free            = lsm_free_plain,
267         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
268         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
269         .lsm_lmm_verify         = lsm_lmm_verify_v1,
270         .lsm_unpackmd           = lsm_unpackmd_v1,
271 };
272
273 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
274                              __u16 *stripe_count)
275 {
276         struct lov_mds_md_v3 *lmm;
277
278         lmm = (struct lov_mds_md_v3 *)lmmv1;
279
280         if (lmm_bytes < sizeof(*lmm)) {
281                 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
282                        lmm_bytes, (int)sizeof(*lmm));
283                 return -EINVAL;
284         }
285
286         *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
287         if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
288                 *stripe_count = 0;
289
290         if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
291                 CERROR("LOV EA V3 too small: %d, need %d\n",
292                        lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
293                 lov_dump_lmm_common(D_WARNING, lmm);
294                 return -EINVAL;
295         }
296
297         return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
298                                      *stripe_count);
299 }
300
301 static int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
302                            struct lov_mds_md *lmm)
303 {
304         struct lov_mds_md_v3 *lmm_v3 = (struct lov_mds_md_v3 *)lmm;
305         size_t cplen;
306         int rc;
307
308         rc = lsm_unpackmd_common(lov, lsm, lmm, lmm_v3->lmm_objects);
309         if (rc != 0)
310                 return rc;
311
312         cplen = strlcpy(lsm->lsm_pool_name, lmm_v3->lmm_pool_name,
313                         sizeof(lsm->lsm_pool_name));
314         if (cplen >= sizeof(lsm->lsm_pool_name))
315                 return -E2BIG;
316
317         return 0;
318 }
319
320 const struct lsm_operations lsm_v3_ops = {
321         .lsm_free            = lsm_free_plain,
322         .lsm_stripe_by_index    = lsm_stripe_by_index_plain,
323         .lsm_stripe_by_offset   = lsm_stripe_by_offset_plain,
324         .lsm_lmm_verify         = lsm_lmm_verify_v3,
325         .lsm_unpackmd           = lsm_unpackmd_v3,
326 };
327
328 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
329 {
330         CDEBUG(level, "lsm %p, objid "DOSTID", maxbytes "LPX64", magic 0x%08X,"
331                " stripe_size %u, stripe_count %u, refc: %d,"
332                " layout_gen %u, pool ["LOV_POOLNAMEF"]\n", lsm,
333                POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
334                lsm->lsm_stripe_size, lsm->lsm_stripe_count,
335                atomic_read(&lsm->lsm_refc), lsm->lsm_layout_gen,
336                lsm->lsm_pool_name);
337 }