4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2011, 2013, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
38 * Author: Wang Di <wangdi@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_LOV
43 #include <linux/math64.h>
44 #include <libcfs/libcfs.h>
46 #include <obd_class.h>
47 #include <lustre/lustre_idl.h>
49 #include "lov_internal.h"
51 static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
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);
60 if (lmm_oi_id(&lmm->lmm_oi) == 0) {
61 CERROR("zero object id\n");
62 lov_dump_lmm_common(D_WARNING, lmm);
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);
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);
82 struct lov_stripe_md *lsm_alloc_plain(__u16 stripe_count, int *size)
84 struct lov_stripe_md *lsm;
85 struct lov_oinfo *loi;
86 int i, oinfo_ptrs_size;
88 LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
90 oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
91 *size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;
93 OBD_ALLOC_LARGE(lsm, *size);
97 for (i = 0; i < stripe_count; i++) {
98 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
101 lsm->lsm_oinfo[i] = loi;
103 lsm->lsm_stripe_count = stripe_count;
108 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
109 OBD_FREE_LARGE(lsm, *size);
113 void lsm_free_plain(struct lov_stripe_md *lsm)
115 __u16 stripe_count = lsm->lsm_stripe_count;
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 *));
125 static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
126 struct lov_mds_md *lmm)
129 * This supposes lov_mds_md_v1/v3 first fields are
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';
140 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
141 obd_off *lov_off, obd_off *swidth)
144 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
148 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
149 obd_off *lov_off, obd_off *swidth)
152 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
155 /* Find minimum stripe maxbytes value. For inactive or
156 * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES. */
157 static void lov_tgt_maxbytes(struct lov_tgt_desc *tgt, __u64 *stripe_maxbytes)
159 struct obd_import *imp = tgt->ltd_obd->u.cli.cl_import;
161 if (imp == NULL || !tgt->ltd_active) {
162 *stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
166 spin_lock(&imp->imp_lock);
167 if (imp->imp_state == LUSTRE_IMP_FULL &&
168 (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
169 imp->imp_connect_data.ocd_maxbytes > 0) {
170 if (*stripe_maxbytes > imp->imp_connect_data.ocd_maxbytes)
171 *stripe_maxbytes = imp->imp_connect_data.ocd_maxbytes;
173 *stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
175 spin_unlock(&imp->imp_lock);
178 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
181 if (lmm_bytes < sizeof(*lmm)) {
182 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
183 lmm_bytes, (int)sizeof(*lmm));
187 *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
188 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
191 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
192 CERROR("LOV EA V1 too small: %d, need %d\n",
193 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
194 lov_dump_lmm_common(D_WARNING, lmm);
198 return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
201 static int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
202 struct lov_mds_md_v1 *lmm)
204 struct lov_oinfo *loi;
207 __u64 stripe_maxbytes = OBD_OBJECT_EOF;
209 lsm_unpackmd_common(lsm, lmm);
211 stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
213 for (i = 0; i < stripe_count; i++) {
214 /* XXX LOV STACKING call down to osc_unpackmd() */
215 loi = lsm->lsm_oinfo[i];
216 ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
217 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
218 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
219 if (lov_oinfo_is_dummy(loi))
222 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
223 CERROR("OST index %d more than OST count %d\n",
224 loi->loi_ost_idx, lov->desc.ld_tgt_count);
225 lov_dump_lmm_v1(D_WARNING, lmm);
228 if (!lov->lov_tgts[loi->loi_ost_idx]) {
229 CERROR("OST index %d missing\n", loi->loi_ost_idx);
230 lov_dump_lmm_v1(D_WARNING, lmm);
233 /* calculate the minimum stripe max bytes */
234 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
238 lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
239 if (lsm->lsm_stripe_count == 0)
240 lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;
245 const struct lsm_operations lsm_v1_ops = {
246 .lsm_free = lsm_free_plain,
247 .lsm_stripe_by_index = lsm_stripe_by_index_plain,
248 .lsm_stripe_by_offset = lsm_stripe_by_offset_plain,
249 .lsm_lmm_verify = lsm_lmm_verify_v1,
250 .lsm_unpackmd = lsm_unpackmd_v1,
253 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
256 struct lov_mds_md_v3 *lmm;
258 lmm = (struct lov_mds_md_v3 *)lmmv1;
260 if (lmm_bytes < sizeof(*lmm)) {
261 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
262 lmm_bytes, (int)sizeof(*lmm));
266 *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
267 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
270 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
271 CERROR("LOV EA V3 too small: %d, need %d\n",
272 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
273 lov_dump_lmm_common(D_WARNING, lmm);
277 return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
281 static int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
282 struct lov_mds_md *lmmv1)
284 struct lov_mds_md_v3 *lmm;
285 struct lov_oinfo *loi;
288 __u64 stripe_maxbytes = OBD_OBJECT_EOF;
291 lmm = (struct lov_mds_md_v3 *)lmmv1;
293 lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);
295 stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
297 cplen = strlcpy(lsm->lsm_pool_name, lmm->lmm_pool_name,
298 sizeof(lsm->lsm_pool_name));
299 if (cplen >= sizeof(lsm->lsm_pool_name))
302 for (i = 0; i < stripe_count; i++) {
303 /* XXX LOV STACKING call down to osc_unpackmd() */
304 loi = lsm->lsm_oinfo[i];
305 ostid_le_to_cpu(&lmm->lmm_objects[i].l_ost_oi, &loi->loi_oi);
306 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
307 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
308 if (lov_oinfo_is_dummy(loi))
311 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
312 CERROR("OST index %d more than OST count %d\n",
313 loi->loi_ost_idx, lov->desc.ld_tgt_count);
314 lov_dump_lmm_v3(D_WARNING, lmm);
317 if (!lov->lov_tgts[loi->loi_ost_idx]) {
318 CERROR("OST index %d missing\n", loi->loi_ost_idx);
319 lov_dump_lmm_v3(D_WARNING, lmm);
322 /* calculate the minimum stripe max bytes */
323 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
327 lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
328 if (lsm->lsm_stripe_count == 0)
329 lsm->lsm_maxbytes = stripe_maxbytes * lov->desc.ld_tgt_count;
334 const struct lsm_operations lsm_v3_ops = {
335 .lsm_free = lsm_free_plain,
336 .lsm_stripe_by_index = lsm_stripe_by_index_plain,
337 .lsm_stripe_by_offset = lsm_stripe_by_offset_plain,
338 .lsm_lmm_verify = lsm_lmm_verify_v3,
339 .lsm_unpackmd = lsm_unpackmd_v3,
342 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
344 CDEBUG(level, "lsm %p, objid "DOSTID", maxbytes "LPX64", magic 0x%08X,"
345 " stripe_size %u, stripe_count %u, refc: %d,"
346 " layout_gen %u, pool ["LOV_POOLNAMEF"]\n", lsm,
347 POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
348 lsm->lsm_stripe_size, lsm->lsm_stripe_count,
349 atomic_read(&lsm->lsm_refc), lsm->lsm_layout_gen,