blob: 34c1346f0dc76e9c1f05d3651676cd2098ddea23 [file] [log] [blame]
Peng Taod7e09d02013-05-02 16:46:55 +08001/*
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, 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 <asm/div64.h>
Greg Kroah-Hartman9fdaf8c2014-07-11 20:51:16 -070044#include "../../include/linux/libcfs/libcfs.h"
Peng Taod7e09d02013-05-02 16:46:55 +080045
Greg Kroah-Hartman0cf0f7a2014-07-11 22:01:58 -070046#include "../include/obd_class.h"
47#include "../include/lustre/lustre_idl.h"
Peng Taod7e09d02013-05-02 16:46:55 +080048
49#include "lov_internal.h"
50
51struct lovea_unpack_args {
52 struct lov_stripe_md *lsm;
53 int cursor;
54};
55
56static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
57 __u16 stripe_count)
58{
Jinshan Xiong5dd16412013-07-23 00:06:39 +080059 if (stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
Peng Taod7e09d02013-05-02 16:46:55 +080060 CERROR("bad stripe count %d\n", stripe_count);
Andreas Dilger53b78532013-06-03 21:40:47 +080061 lov_dump_lmm_common(D_WARNING, lmm);
Peng Taod7e09d02013-05-02 16:46:55 +080062 return -EINVAL;
63 }
64
65 if (lmm_oi_id(&lmm->lmm_oi) == 0) {
66 CERROR("zero object id\n");
Andreas Dilger53b78532013-06-03 21:40:47 +080067 lov_dump_lmm_common(D_WARNING, lmm);
Peng Taod7e09d02013-05-02 16:46:55 +080068 return -EINVAL;
69 }
70
Jinshan Xiong5dd16412013-07-23 00:06:39 +080071 if (lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_RAID0) {
Peng Taod7e09d02013-05-02 16:46:55 +080072 CERROR("bad striping pattern\n");
Andreas Dilger53b78532013-06-03 21:40:47 +080073 lov_dump_lmm_common(D_WARNING, lmm);
Peng Taod7e09d02013-05-02 16:46:55 +080074 return -EINVAL;
75 }
76
77 if (lmm->lmm_stripe_size == 0 ||
Andreas Dilger53b78532013-06-03 21:40:47 +080078 (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
Peng Taod7e09d02013-05-02 16:46:55 +080079 CERROR("bad stripe size %u\n",
80 le32_to_cpu(lmm->lmm_stripe_size));
Andreas Dilger53b78532013-06-03 21:40:47 +080081 lov_dump_lmm_common(D_WARNING, lmm);
Peng Taod7e09d02013-05-02 16:46:55 +080082 return -EINVAL;
83 }
84 return 0;
85}
86
87struct lov_stripe_md *lsm_alloc_plain(__u16 stripe_count, int *size)
88{
89 struct lov_stripe_md *lsm;
90 struct lov_oinfo *loi;
91 int i, oinfo_ptrs_size;
92
93 LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
94
95 oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
96 *size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;
97
Julia Lawall3d0ba712015-06-11 14:02:54 +020098 lsm = libcfs_kvzalloc(*size, GFP_NOFS);
Peng Taod7e09d02013-05-02 16:46:55 +080099 if (!lsm)
Monam Agarwal76b8f532014-03-01 17:03:33 +0530100 return NULL;
Peng Taod7e09d02013-05-02 16:46:55 +0800101
102 for (i = 0; i < stripe_count; i++) {
Mike Rapoportccaabce2015-10-20 12:39:49 +0300103 loi = kmem_cache_alloc(lov_oinfo_slab, GFP_NOFS | __GFP_ZERO);
Peng Taod7e09d02013-05-02 16:46:55 +0800104 if (loi == NULL)
105 goto err;
106 lsm->lsm_oinfo[i] = loi;
107 }
108 lsm->lsm_stripe_count = stripe_count;
109 return lsm;
110
111err:
112 while (--i >= 0)
Mike Rapoport5c4d8ed2015-10-20 12:39:52 +0300113 kmem_cache_free(lov_oinfo_slab, lsm->lsm_oinfo[i]);
Julia Lawall3d0ba712015-06-11 14:02:54 +0200114 kvfree(lsm);
Peng Taod7e09d02013-05-02 16:46:55 +0800115 return NULL;
116}
117
118void lsm_free_plain(struct lov_stripe_md *lsm)
119{
120 __u16 stripe_count = lsm->lsm_stripe_count;
121 int i;
122
123 for (i = 0; i < stripe_count; i++)
Mike Rapoport5c4d8ed2015-10-20 12:39:52 +0300124 kmem_cache_free(lov_oinfo_slab, lsm->lsm_oinfo[i]);
Julia Lawall3d0ba712015-06-11 14:02:54 +0200125 kvfree(lsm);
Peng Taod7e09d02013-05-02 16:46:55 +0800126}
127
128static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
129 struct lov_mds_md *lmm)
130{
131 /*
132 * This supposes lov_mds_md_v1/v3 first fields are
133 * are the same
134 */
135 lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
136 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
137 lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
138 lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
139 lsm->lsm_pool_name[0] = '\0';
140}
141
142static void
143lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
Oleg Drokin21aef7d2014-08-15 12:55:56 -0400144 u64 *lov_off, u64 *swidth)
Peng Taod7e09d02013-05-02 16:46:55 +0800145{
146 if (swidth)
Oleg Drokin21aef7d2014-08-15 12:55:56 -0400147 *swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
Peng Taod7e09d02013-05-02 16:46:55 +0800148}
149
150static void
151lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
Oleg Drokin21aef7d2014-08-15 12:55:56 -0400152 u64 *lov_off, u64 *swidth)
Peng Taod7e09d02013-05-02 16:46:55 +0800153{
154 if (swidth)
Oleg Drokin21aef7d2014-08-15 12:55:56 -0400155 *swidth = (u64)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
Peng Taod7e09d02013-05-02 16:46:55 +0800156}
157
158static int lsm_destroy_plain(struct lov_stripe_md *lsm, struct obdo *oa,
159 struct obd_export *md_exp)
160{
161 return 0;
162}
163
164/* Find minimum stripe maxbytes value. For inactive or
165 * reconnecting targets use LUSTRE_STRIPE_MAXBYTES. */
166static void lov_tgt_maxbytes(struct lov_tgt_desc *tgt, __u64 *stripe_maxbytes)
167{
168 struct obd_import *imp = tgt->ltd_obd->u.cli.cl_import;
169
170 if (imp == NULL || !tgt->ltd_active) {
171 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
172 return;
173 }
174
175 spin_lock(&imp->imp_lock);
176 if (imp->imp_state == LUSTRE_IMP_FULL &&
177 (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
178 imp->imp_connect_data.ocd_maxbytes > 0) {
179 if (*stripe_maxbytes > imp->imp_connect_data.ocd_maxbytes)
180 *stripe_maxbytes = imp->imp_connect_data.ocd_maxbytes;
181 } else {
182 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
183 }
184 spin_unlock(&imp->imp_lock);
185}
186
187static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
188 __u16 *stripe_count)
189{
190 if (lmm_bytes < sizeof(*lmm)) {
191 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
192 lmm_bytes, (int)sizeof(*lmm));
193 return -EINVAL;
194 }
195
196 *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800197 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
198 *stripe_count = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800199
200 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
201 CERROR("LOV EA V1 too small: %d, need %d\n",
202 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
Andreas Dilger53b78532013-06-03 21:40:47 +0800203 lov_dump_lmm_common(D_WARNING, lmm);
Peng Taod7e09d02013-05-02 16:46:55 +0800204 return -EINVAL;
205 }
206
207 return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
208}
209
Le Tan12e397c2015-02-11 12:13:14 +0800210static int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
211 struct lov_mds_md_v1 *lmm)
Peng Taod7e09d02013-05-02 16:46:55 +0800212{
213 struct lov_oinfo *loi;
214 int i;
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800215 int stripe_count;
Peng Taod7e09d02013-05-02 16:46:55 +0800216 __u64 stripe_maxbytes = OBD_OBJECT_EOF;
217
218 lsm_unpackmd_common(lsm, lmm);
219
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800220 stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
221
222 for (i = 0; i < stripe_count; i++) {
Peng Taod7e09d02013-05-02 16:46:55 +0800223 /* XXX LOV STACKING call down to osc_unpackmd() */
224 loi = lsm->lsm_oinfo[i];
225 ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
226 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
227 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
Yang Sheng397632e2015-03-25 21:53:22 -0400228 if (lov_oinfo_is_dummy(loi))
229 continue;
230
Peng Taod7e09d02013-05-02 16:46:55 +0800231 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
232 CERROR("OST index %d more than OST count %d\n",
233 loi->loi_ost_idx, lov->desc.ld_tgt_count);
234 lov_dump_lmm_v1(D_WARNING, lmm);
235 return -EINVAL;
236 }
237 if (!lov->lov_tgts[loi->loi_ost_idx]) {
238 CERROR("OST index %d missing\n", loi->loi_ost_idx);
239 lov_dump_lmm_v1(D_WARNING, lmm);
240 return -EINVAL;
241 }
242 /* calculate the minimum stripe max bytes */
243 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
244 &stripe_maxbytes);
245 }
246
247 lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800248 if (lsm->lsm_stripe_count == 0)
249 lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;
Peng Taod7e09d02013-05-02 16:46:55 +0800250
251 return 0;
252}
253
254const struct lsm_operations lsm_v1_ops = {
255 .lsm_free = lsm_free_plain,
256 .lsm_destroy = lsm_destroy_plain,
257 .lsm_stripe_by_index = lsm_stripe_by_index_plain,
258 .lsm_stripe_by_offset = lsm_stripe_by_offset_plain,
259 .lsm_lmm_verify = lsm_lmm_verify_v1,
260 .lsm_unpackmd = lsm_unpackmd_v1,
261};
262
263static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
264 __u16 *stripe_count)
265{
266 struct lov_mds_md_v3 *lmm;
267
268 lmm = (struct lov_mds_md_v3 *)lmmv1;
269
270 if (lmm_bytes < sizeof(*lmm)) {
271 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
272 lmm_bytes, (int)sizeof(*lmm));
273 return -EINVAL;
274 }
275
276 *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800277 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
278 *stripe_count = 0;
Peng Taod7e09d02013-05-02 16:46:55 +0800279
280 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
281 CERROR("LOV EA V3 too small: %d, need %d\n",
282 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
Andreas Dilger53b78532013-06-03 21:40:47 +0800283 lov_dump_lmm_common(D_WARNING, lmm);
Peng Taod7e09d02013-05-02 16:46:55 +0800284 return -EINVAL;
285 }
286
287 return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
288 *stripe_count);
289}
290
Le Tan12e397c2015-02-11 12:13:14 +0800291static int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
292 struct lov_mds_md *lmmv1)
Peng Taod7e09d02013-05-02 16:46:55 +0800293{
294 struct lov_mds_md_v3 *lmm;
295 struct lov_oinfo *loi;
296 int i;
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800297 int stripe_count;
Peng Taod7e09d02013-05-02 16:46:55 +0800298 __u64 stripe_maxbytes = OBD_OBJECT_EOF;
299 int cplen = 0;
300
301 lmm = (struct lov_mds_md_v3 *)lmmv1;
302
303 lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800304
305 stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
306
Peng Taod7e09d02013-05-02 16:46:55 +0800307 cplen = strlcpy(lsm->lsm_pool_name, lmm->lmm_pool_name,
308 sizeof(lsm->lsm_pool_name));
309 if (cplen >= sizeof(lsm->lsm_pool_name))
310 return -E2BIG;
311
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800312 for (i = 0; i < stripe_count; i++) {
Peng Taod7e09d02013-05-02 16:46:55 +0800313 /* XXX LOV STACKING call down to osc_unpackmd() */
314 loi = lsm->lsm_oinfo[i];
315 ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
316 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
317 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
Yang Sheng397632e2015-03-25 21:53:22 -0400318 if (lov_oinfo_is_dummy(loi))
319 continue;
320
Peng Taod7e09d02013-05-02 16:46:55 +0800321 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
322 CERROR("OST index %d more than OST count %d\n",
323 loi->loi_ost_idx, lov->desc.ld_tgt_count);
324 lov_dump_lmm_v3(D_WARNING, lmm);
325 return -EINVAL;
326 }
327 if (!lov->lov_tgts[loi->loi_ost_idx]) {
328 CERROR("OST index %d missing\n", loi->loi_ost_idx);
329 lov_dump_lmm_v3(D_WARNING, lmm);
330 return -EINVAL;
331 }
332 /* calculate the minimum stripe max bytes */
333 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
334 &stripe_maxbytes);
335 }
336
337 lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
Jinshan Xiong5dd16412013-07-23 00:06:39 +0800338 if (lsm->lsm_stripe_count == 0)
339 lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;
Peng Taod7e09d02013-05-02 16:46:55 +0800340
341 return 0;
342}
343
344const struct lsm_operations lsm_v3_ops = {
345 .lsm_free = lsm_free_plain,
346 .lsm_destroy = lsm_destroy_plain,
347 .lsm_stripe_by_index = lsm_stripe_by_index_plain,
348 .lsm_stripe_by_offset = lsm_stripe_by_offset_plain,
349 .lsm_lmm_verify = lsm_lmm_verify_v3,
350 .lsm_unpackmd = lsm_unpackmd_v3,
351};
John L. Hammond081b7262014-04-27 13:06:40 -0400352
353void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
354{
Joe Perches2d00bd12014-11-23 11:28:50 -0800355 CDEBUG(level, "lsm %p, objid " DOSTID ", maxbytes %#llx, magic 0x%08X, stripe_size %u, stripe_count %u, refc: %d, layout_gen %u, pool [" LOV_POOLNAMEF "]\n",
356 lsm,
John L. Hammond081b7262014-04-27 13:06:40 -0400357 POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
358 lsm->lsm_stripe_size, lsm->lsm_stripe_count,
359 atomic_read(&lsm->lsm_refc), lsm->lsm_layout_gen,
360 lsm->lsm_pool_name);
361}