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.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2011, 2016, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
34 * Author: Wang Di <wangdi@clusterfs.com>
37 #define DEBUG_SUBSYSTEM S_LOV
39 #include <linux/math64.h>
40 #include <libcfs/libcfs.h>
42 #include <obd_class.h>
43 #include <lustre/lustre_idl.h>
45 #include "lov_internal.h"
47 static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
50 if (stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
51 CERROR("bad stripe count %d\n", stripe_count);
52 lov_dump_lmm_common(D_WARNING, lmm);
56 if (lmm_oi_id(&lmm->lmm_oi) == 0) {
57 CERROR("zero object id\n");
58 lov_dump_lmm_common(D_WARNING, lmm);
62 if (lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_RAID0) {
63 CERROR("bad striping pattern\n");
64 lov_dump_lmm_common(D_WARNING, lmm);
68 if (lmm->lmm_stripe_size == 0 ||
69 (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
70 CERROR("bad stripe size %u\n",
71 le32_to_cpu(lmm->lmm_stripe_size));
72 lov_dump_lmm_common(D_WARNING, lmm);
78 struct lov_stripe_md *lsm_alloc_plain(u16 stripe_count)
80 struct lov_stripe_md *lsm;
81 struct lov_oinfo *loi;
83 size_t oinfo_ptrs_size;
86 LASSERT(stripe_count <= LOV_MAX_STRIPE_COUNT);
88 oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
89 lsm_size = sizeof(*lsm) + oinfo_ptrs_size;
91 OBD_ALLOC_LARGE(lsm, lsm_size);
95 for (i = 0; i < stripe_count; i++) {
96 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
99 lsm->lsm_oinfo[i] = loi;
101 lsm->lsm_stripe_count = stripe_count;
106 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
108 OBD_FREE_LARGE(lsm, 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 /* Find minimum stripe maxbytes value. For inactive or
126 * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES. */
127 static loff_t lov_tgt_maxbytes(struct lov_tgt_desc *tgt)
129 struct obd_import *imp;
130 loff_t maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
132 if (!tgt->ltd_active)
135 imp = tgt->ltd_obd->u.cli.cl_import;
139 spin_lock(&imp->imp_lock);
140 if (imp->imp_state == LUSTRE_IMP_FULL &&
141 (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
142 imp->imp_connect_data.ocd_maxbytes > 0)
143 maxbytes = imp->imp_connect_data.ocd_maxbytes;
145 spin_unlock(&imp->imp_lock);
150 static int lsm_unpackmd_common(struct lov_obd *lov,
151 struct lov_stripe_md *lsm,
152 struct lov_mds_md *lmm,
153 struct lov_ost_data_v1 *objects)
155 struct lov_oinfo *loi;
156 loff_t min_stripe_maxbytes = 0;
158 unsigned int stripe_count;
162 * This supposes lov_mds_md_v1/v3 first fields are
165 lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
166 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
167 lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
168 lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
169 lsm->lsm_pool_name[0] = '\0';
171 stripe_count = lsm_is_released(lsm) ? 0 : lsm->lsm_stripe_count;
173 for (i = 0; i < stripe_count; i++) {
174 loi = lsm->lsm_oinfo[i];
175 ostid_le_to_cpu(&objects[i].l_ost_oi, &loi->loi_oi);
176 loi->loi_ost_idx = le32_to_cpu(objects[i].l_ost_idx);
177 loi->loi_ost_gen = le32_to_cpu(objects[i].l_ost_gen);
178 if (lov_oinfo_is_dummy(loi))
181 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count &&
182 !lov2obd(lov)->obd_process_conf) {
183 CERROR("%s: OST index %d more than OST count %d\n",
184 (char*)lov->desc.ld_uuid.uuid,
185 loi->loi_ost_idx, lov->desc.ld_tgt_count);
186 lov_dump_lmm_v1(D_WARNING, lmm);
190 if (lov->lov_tgts[loi->loi_ost_idx] == NULL) {
191 CERROR("%s: OST index %d missing\n",
192 (char*)lov->desc.ld_uuid.uuid, loi->loi_ost_idx);
193 lov_dump_lmm_v1(D_WARNING, lmm);
197 lov_bytes = lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx]);
198 if (min_stripe_maxbytes == 0 || lov_bytes < min_stripe_maxbytes)
199 min_stripe_maxbytes = lov_bytes;
202 if (min_stripe_maxbytes == 0)
203 min_stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
205 stripe_count = lsm->lsm_stripe_count ?: lov->desc.ld_tgt_count;
206 lov_bytes = min_stripe_maxbytes * stripe_count;
208 if (lov_bytes < min_stripe_maxbytes) /* handle overflow */
209 lsm->lsm_maxbytes = MAX_LFS_FILESIZE;
211 lsm->lsm_maxbytes = lov_bytes;
217 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
218 loff_t *lov_off, loff_t *swidth)
221 *swidth = (loff_t)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
225 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
226 loff_t *lov_off, loff_t *swidth)
229 *swidth = (loff_t)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
232 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
235 if (lmm_bytes < sizeof(*lmm)) {
236 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
237 lmm_bytes, (int)sizeof(*lmm));
241 *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
242 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
245 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
246 CERROR("LOV EA V1 too small: %d, need %d\n",
247 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
248 lov_dump_lmm_common(D_WARNING, lmm);
252 return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
255 static int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
256 struct lov_mds_md_v1 *lmm)
258 return lsm_unpackmd_common(lov, lsm, lmm, lmm->lmm_objects);
261 const struct lsm_operations lsm_v1_ops = {
262 .lsm_free = lsm_free_plain,
263 .lsm_stripe_by_index = lsm_stripe_by_index_plain,
264 .lsm_stripe_by_offset = lsm_stripe_by_offset_plain,
265 .lsm_lmm_verify = lsm_lmm_verify_v1,
266 .lsm_unpackmd = lsm_unpackmd_v1,
269 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
272 struct lov_mds_md_v3 *lmm;
274 lmm = (struct lov_mds_md_v3 *)lmmv1;
276 if (lmm_bytes < sizeof(*lmm)) {
277 CERROR("lov_mds_md_v3 too small: %d, need at least %d\n",
278 lmm_bytes, (int)sizeof(*lmm));
282 *stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
283 if (le32_to_cpu(lmm->lmm_pattern) & LOV_PATTERN_F_RELEASED)
286 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
287 CERROR("LOV EA V3 too small: %d, need %d\n",
288 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
289 lov_dump_lmm_common(D_WARNING, lmm);
293 return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
297 static int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
298 struct lov_mds_md *lmm)
300 struct lov_mds_md_v3 *lmm_v3 = (struct lov_mds_md_v3 *)lmm;
304 rc = lsm_unpackmd_common(lov, lsm, lmm, lmm_v3->lmm_objects);
308 cplen = strlcpy(lsm->lsm_pool_name, lmm_v3->lmm_pool_name,
309 sizeof(lsm->lsm_pool_name));
310 if (cplen >= sizeof(lsm->lsm_pool_name))
316 const struct lsm_operations lsm_v3_ops = {
317 .lsm_free = lsm_free_plain,
318 .lsm_stripe_by_index = lsm_stripe_by_index_plain,
319 .lsm_stripe_by_offset = lsm_stripe_by_offset_plain,
320 .lsm_lmm_verify = lsm_lmm_verify_v3,
321 .lsm_unpackmd = lsm_unpackmd_v3,
324 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
326 CDEBUG(level, "lsm %p, objid "DOSTID", maxbytes %#llx, magic 0x%08X,"
327 " stripe_size %u, stripe_count %u, refc: %d,"
328 " layout_gen %u, pool ["LOV_POOLNAMEF"]\n", lsm,
329 POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
330 lsm->lsm_stripe_size, lsm->lsm_stripe_count,
331 atomic_read(&lsm->lsm_refc), lsm->lsm_layout_gen,