1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30 * Use is subject to license terms.
33 * Copyright (c) 2011 Whamcloud, Inc.
36 * This file is part of Lustre, http://www.lustre.org/
37 * Lustre is a trademark of Sun Microsystems, Inc.
41 * Author: Wang Di <wangdi@clusterfs.com>
45 # define EXPORT_SYMTAB
47 #define DEBUG_SUBSYSTEM S_LOV
50 #include <asm/div64.h>
51 #include <libcfs/libcfs.h>
53 #include <liblustre.h>
56 #include <obd_class.h>
58 #include <lustre/lustre_idl.h>
59 #include <lustre_log.h>
61 #include "lov_internal.h"
63 struct lovea_unpack_args {
64 struct lov_stripe_md *lsm;
68 static int lsm_lmm_verify_common(struct lov_mds_md *lmm, int lmm_bytes,
72 if (stripe_count == 0 || stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
73 CERROR("bad stripe count %d\n", stripe_count);
74 lov_dump_lmm(D_WARNING, lmm);
78 if (lmm->lmm_object_id == 0) {
79 CERROR("zero object id\n");
80 lov_dump_lmm(D_WARNING, lmm);
84 if (lmm->lmm_pattern != cpu_to_le32(LOV_PATTERN_RAID0)) {
85 CERROR("bad striping pattern\n");
86 lov_dump_lmm(D_WARNING, lmm);
90 if (lmm->lmm_stripe_size == 0 ||
91 (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
92 CERROR("bad stripe size %u\n",
93 le32_to_cpu(lmm->lmm_stripe_size));
94 lov_dump_lmm(D_WARNING, lmm);
100 struct lov_stripe_md *lsm_alloc_plain(int stripe_count, int *size)
102 struct lov_stripe_md *lsm;
103 int i, oinfo_ptrs_size;
104 struct lov_oinfo *loi;
106 LASSERT(stripe_count > 0);
108 oinfo_ptrs_size = sizeof(struct lov_oinfo *) * stripe_count;
109 *size = sizeof(struct lov_stripe_md) + oinfo_ptrs_size;
111 OBD_ALLOC_LARGE(lsm, *size);
115 for (i = 0; i < stripe_count; i++) {
116 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, CFS_ALLOC_IO);
119 lsm->lsm_oinfo[i] = loi;
121 lsm->lsm_stripe_count = stripe_count;
122 lsm->lsm_pool_name[0] = '\0';
127 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab, sizeof(*loi));
128 OBD_FREE_LARGE(lsm, *size);
132 void lsm_free_plain(struct lov_stripe_md *lsm)
134 int stripe_count = lsm->lsm_stripe_count;
137 for (i = 0; i < stripe_count; i++)
138 OBD_SLAB_FREE(lsm->lsm_oinfo[i], lov_oinfo_slab,
139 sizeof(struct lov_oinfo));
140 OBD_FREE_LARGE(lsm, sizeof(struct lov_stripe_md) +
141 stripe_count * sizeof(struct lov_oinfo *));
144 static void lsm_unpackmd_common(struct lov_stripe_md *lsm,
145 struct lov_mds_md *lmm)
148 * This supposes lov_mds_md_v1/v3 first fields are
151 lsm->lsm_object_id = le64_to_cpu(lmm->lmm_object_id);
152 lsm->lsm_object_seq = le64_to_cpu(lmm->lmm_object_seq);
153 lsm->lsm_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
154 lsm->lsm_pattern = le32_to_cpu(lmm->lmm_pattern);
155 lsm->lsm_pool_name[0] = '\0';
159 lsm_stripe_by_index_plain(struct lov_stripe_md *lsm, int *stripeno,
160 obd_off *lov_off, obd_off *swidth)
163 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
167 lsm_stripe_by_offset_plain(struct lov_stripe_md *lsm, int *stripeno,
168 obd_off *lov_off, obd_off *swidth)
171 *swidth = (obd_off)lsm->lsm_stripe_size * lsm->lsm_stripe_count;
174 static int lsm_destroy_plain(struct lov_stripe_md *lsm, struct obdo *oa,
175 struct obd_export *md_exp)
180 /* Find minimum stripe maxbytes value. For inactive or
181 * reconnecting targets use LUSTRE_STRIPE_MAXBYTES. */
182 static void lov_tgt_maxbytes(struct lov_tgt_desc *tgt, __u64 *stripe_maxbytes)
184 struct obd_import *imp = tgt->ltd_obd->u.cli.cl_import;
186 if (imp == NULL || !tgt->ltd_active) {
187 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
191 cfs_spin_lock(&imp->imp_lock);
192 if (imp->imp_state == LUSTRE_IMP_FULL &&
193 (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
194 imp->imp_connect_data.ocd_maxbytes > 0) {
195 if (*stripe_maxbytes > imp->imp_connect_data.ocd_maxbytes)
196 *stripe_maxbytes = imp->imp_connect_data.ocd_maxbytes;
198 *stripe_maxbytes = LUSTRE_STRIPE_MAXBYTES;
200 cfs_spin_unlock(&imp->imp_lock);
203 static int lsm_lmm_verify_v1(struct lov_mds_md_v1 *lmm, int lmm_bytes,
206 if (lmm_bytes < sizeof(*lmm)) {
207 CERROR("lov_mds_md_v1 too small: %d, need at least %d\n",
208 lmm_bytes, (int)sizeof(*lmm));
212 *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
214 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V1)) {
215 CERROR("LOV EA V1 too small: %d, need %d\n",
216 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V1));
217 lov_dump_lmm_v1(D_WARNING, lmm);
221 return lsm_lmm_verify_common(lmm, lmm_bytes, *stripe_count);
224 int lsm_unpackmd_v1(struct lov_obd *lov, struct lov_stripe_md *lsm,
225 struct lov_mds_md_v1 *lmm)
227 struct lov_oinfo *loi;
229 __u64 stripe_maxbytes = OBD_OBJECT_EOF;
231 lsm_unpackmd_common(lsm, lmm);
233 for (i = 0; i < lsm->lsm_stripe_count; i++) {
234 /* XXX LOV STACKING call down to osc_unpackmd() */
235 loi = lsm->lsm_oinfo[i];
236 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
237 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
238 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
239 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
240 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
241 CERROR("OST index %d more than OST count %d\n",
242 loi->loi_ost_idx, lov->desc.ld_tgt_count);
243 lov_dump_lmm_v1(D_WARNING, lmm);
246 if (!lov->lov_tgts[loi->loi_ost_idx]) {
247 CERROR("OST index %d missing\n", loi->loi_ost_idx);
248 lov_dump_lmm_v1(D_WARNING, lmm);
251 /* calculate the minimum stripe max bytes */
252 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
256 lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
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,
270 static int lsm_lmm_verify_v3(struct lov_mds_md *lmmv1, int lmm_bytes,
273 struct lov_mds_md_v3 *lmm;
275 lmm = (struct lov_mds_md_v3 *)lmmv1;
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));
283 *stripe_count = le32_to_cpu(lmm->lmm_stripe_count);
285 if (lmm_bytes < lov_mds_md_size(*stripe_count, LOV_MAGIC_V3)) {
286 CERROR("LOV EA V3 too small: %d, need %d\n",
287 lmm_bytes, lov_mds_md_size(*stripe_count, LOV_MAGIC_V3));
288 lov_dump_lmm_v3(D_WARNING, lmm);
292 return lsm_lmm_verify_common((struct lov_mds_md_v1 *)lmm, lmm_bytes,
296 int lsm_unpackmd_v3(struct lov_obd *lov, struct lov_stripe_md *lsm,
297 struct lov_mds_md *lmmv1)
299 struct lov_mds_md_v3 *lmm;
300 struct lov_oinfo *loi;
302 __u64 stripe_maxbytes = OBD_OBJECT_EOF;
304 lmm = (struct lov_mds_md_v3 *)lmmv1;
306 lsm_unpackmd_common(lsm, (struct lov_mds_md_v1 *)lmm);
307 strncpy(lsm->lsm_pool_name, lmm->lmm_pool_name, LOV_MAXPOOLNAME);
309 for (i = 0; i < lsm->lsm_stripe_count; i++) {
310 /* XXX LOV STACKING call down to osc_unpackmd() */
311 loi = lsm->lsm_oinfo[i];
312 loi->loi_id = le64_to_cpu(lmm->lmm_objects[i].l_object_id);
313 loi->loi_seq = le64_to_cpu(lmm->lmm_objects[i].l_object_seq);
314 loi->loi_ost_idx = le32_to_cpu(lmm->lmm_objects[i].l_ost_idx);
315 loi->loi_ost_gen = le32_to_cpu(lmm->lmm_objects[i].l_ost_gen);
316 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count) {
317 CERROR("OST index %d more than OST count %d\n",
318 loi->loi_ost_idx, lov->desc.ld_tgt_count);
319 lov_dump_lmm_v3(D_WARNING, lmm);
322 if (!lov->lov_tgts[loi->loi_ost_idx]) {
323 CERROR("OST index %d missing\n", loi->loi_ost_idx);
324 lov_dump_lmm_v3(D_WARNING, lmm);
327 /* calculate the minimum stripe max bytes */
328 lov_tgt_maxbytes(lov->lov_tgts[loi->loi_ost_idx],
332 lsm->lsm_maxbytes = stripe_maxbytes * lsm->lsm_stripe_count;
337 const struct lsm_operations lsm_v3_ops = {
338 .lsm_free = lsm_free_plain,
339 .lsm_destroy = lsm_destroy_plain,
340 .lsm_stripe_by_index = lsm_stripe_by_index_plain,
341 .lsm_stripe_by_offset = lsm_stripe_by_offset_plain,
342 .lsm_lmm_verify = lsm_lmm_verify_v3,
343 .lsm_unpackmd = lsm_unpackmd_v3,