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 <linux/sort.h>
41 #include <libcfs/libcfs.h>
43 #include <obd_class.h>
44 #include <lustre/lustre_idl.h>
45 #include <lustre/lustre_user.h>
47 #include "lov_internal.h"
50 lu_extent_le_to_cpu(struct lu_extent *dst, const struct lu_extent *src)
52 dst->e_start = le64_to_cpu(src->e_start);
53 dst->e_end = le64_to_cpu(src->e_end);
56 /* Find minimum stripe maxbytes value. For inactive or
57 * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES. */
58 static loff_t lov_tgt_maxbytes(struct lov_tgt_desc *tgt)
60 struct obd_import *imp;
61 loff_t maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
66 imp = tgt->ltd_obd->u.cli.cl_import;
70 spin_lock(&imp->imp_lock);
71 if (imp->imp_state == LUSTRE_IMP_FULL &&
72 (imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_MAXBYTES) &&
73 imp->imp_connect_data.ocd_maxbytes > 0)
74 maxbytes = imp->imp_connect_data.ocd_maxbytes;
76 spin_unlock(&imp->imp_lock);
81 static int lsm_lmm_verify_v1v3(struct lov_mds_md *lmm, size_t lmm_size,
84 if (stripe_count > LOV_V1_INSANE_STRIPE_COUNT) {
85 CERROR("bad stripe count %d\n", stripe_count);
86 lov_dump_lmm_common(D_WARNING, lmm);
90 if (lmm_oi_id(&lmm->lmm_oi) == 0) {
91 CERROR("zero object id\n");
92 lov_dump_lmm_common(D_WARNING, lmm);
96 if (lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_RAID0) {
97 CERROR("bad striping pattern\n");
98 lov_dump_lmm_common(D_WARNING, lmm);
102 if (lmm->lmm_stripe_size == 0 ||
103 (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
104 CERROR("bad stripe size %u\n",
105 le32_to_cpu(lmm->lmm_stripe_size));
106 lov_dump_lmm_common(D_WARNING, lmm);
112 static void lsme_free(struct lov_stripe_md_entry *lsme)
114 unsigned int stripe_count = lsme->lsme_stripe_count;
118 if (!lsme_inited(lsme) ||
119 lsme->lsme_pattern & LOV_PATTERN_F_RELEASED)
121 for (i = 0; i < stripe_count; i++)
122 OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
124 lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
125 OBD_FREE_LARGE(lsme, lsme_size);
128 void lsm_free(struct lov_stripe_md *lsm)
130 unsigned int entry_count = lsm->lsm_entry_count;
134 for (i = 0; i < entry_count; i++)
135 lsme_free(lsm->lsm_entries[i]);
137 lsm_size = offsetof(typeof(*lsm), lsm_entries[entry_count]);
138 OBD_FREE(lsm, lsm_size);
142 * Unpack a struct lov_mds_md into a struct lov_stripe_md_entry.
144 * The caller should set id and extent.
146 static struct lov_stripe_md_entry *
147 lsme_unpack(struct lov_obd *lov, struct lov_mds_md *lmm, size_t buf_size,
148 const char *pool_name, bool inited, struct lov_ost_data_v1 *objects,
151 struct lov_stripe_md_entry *lsme;
153 loff_t min_stripe_maxbytes = 0;
157 unsigned int stripe_count;
161 magic = le32_to_cpu(lmm->lmm_magic);
162 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
163 RETURN(ERR_PTR(-EINVAL));
165 pattern = le32_to_cpu(lmm->lmm_pattern);
166 if (pattern & LOV_PATTERN_F_RELEASED || !inited)
169 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
171 if (buf_size < (magic == LOV_MAGIC_V1 ? sizeof(struct lov_mds_md_v1) :
172 sizeof(struct lov_mds_md_v3))) {
173 CERROR("LOV EA %s too small: %zu, need %u\n",
174 magic == LOV_MAGIC_V1 ? "V1" : "V3", buf_size,
175 lov_mds_md_size(stripe_count, magic == LOV_MAGIC_V1 ?
176 LOV_MAGIC_V1 : LOV_MAGIC_V3));
177 lov_dump_lmm_common(D_WARNING, lmm);
178 return ERR_PTR(-EINVAL);
181 rc = lsm_lmm_verify_v1v3(lmm, buf_size, stripe_count);
185 lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
186 OBD_ALLOC_LARGE(lsme, lsme_size);
188 RETURN(ERR_PTR(-ENOMEM));
190 lsme->lsme_magic = magic;
191 lsme->lsme_pattern = pattern;
192 lsme->lsme_flags = 0;
193 lsme->lsme_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
194 /* preserve the possible -1 stripe count for uninstantiated component */
195 lsme->lsme_stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
196 lsme->lsme_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
198 if (pool_name != NULL) {
199 size_t pool_name_len;
201 pool_name_len = strlcpy(lsme->lsme_pool_name, pool_name,
202 sizeof(lsme->lsme_pool_name));
203 if (pool_name_len >= sizeof(lsme->lsme_pool_name))
204 GOTO(out_lsme, rc = -E2BIG);
207 for (i = 0; i < stripe_count; i++) {
208 struct lov_oinfo *loi;
209 struct lov_tgt_desc *ltd;
211 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
213 GOTO(out_lsme, rc = -ENOMEM);
215 lsme->lsme_oinfo[i] = loi;
217 ostid_le_to_cpu(&objects[i].l_ost_oi, &loi->loi_oi);
218 loi->loi_ost_idx = le32_to_cpu(objects[i].l_ost_idx);
219 loi->loi_ost_gen = le32_to_cpu(objects[i].l_ost_gen);
220 if (lov_oinfo_is_dummy(loi))
223 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count &&
224 !lov2obd(lov)->obd_process_conf) {
225 CERROR("%s: OST index %d more than OST count %d\n",
226 (char*)lov->desc.ld_uuid.uuid,
227 loi->loi_ost_idx, lov->desc.ld_tgt_count);
228 lov_dump_lmm_v1(D_WARNING, lmm);
229 GOTO(out_lsme, rc = -EINVAL);
232 ltd = lov->lov_tgts[loi->loi_ost_idx];
234 CERROR("%s: OST index %d missing\n",
235 (char*)lov->desc.ld_uuid.uuid, loi->loi_ost_idx);
236 lov_dump_lmm_v1(D_WARNING, lmm);
240 lov_bytes = lov_tgt_maxbytes(ltd);
241 if (min_stripe_maxbytes == 0 || lov_bytes < min_stripe_maxbytes)
242 min_stripe_maxbytes = lov_bytes;
245 if (min_stripe_maxbytes == 0)
246 min_stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
248 lov_bytes = min_stripe_maxbytes * stripe_count;
250 if (maxbytes != NULL) {
251 if (lov_bytes < min_stripe_maxbytes) /* handle overflow */
252 *maxbytes = MAX_LFS_FILESIZE;
254 *maxbytes = lov_bytes;
260 for (i = 0; i < stripe_count; i++) {
261 struct lov_oinfo *loi = lsme->lsme_oinfo[i];
264 OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
266 OBD_FREE_LARGE(lsme, lsme_size);
271 static struct lov_stripe_md *
272 lsm_unpackmd_v1v3(struct lov_obd *lov,
273 struct lov_mds_md *lmm, size_t buf_size,
274 const char *pool_name,
275 struct lov_ost_data_v1 *objects)
277 struct lov_stripe_md *lsm;
278 struct lov_stripe_md_entry *lsme;
284 pattern = le32_to_cpu(lmm->lmm_pattern);
286 lsme = lsme_unpack(lov, lmm, buf_size, pool_name, true, objects,
289 RETURN(ERR_CAST(lsme));
291 lsme->lsme_flags = LCME_FL_INIT;
292 lsme->lsme_extent.e_start = 0;
293 lsme->lsme_extent.e_end = LUSTRE_EOF;
295 lsm_size = offsetof(typeof(*lsm), lsm_entries[1]);
296 OBD_ALLOC(lsm, lsm_size);
298 GOTO(out_lsme, rc = -ENOMEM);
300 atomic_set(&lsm->lsm_refc, 1);
301 spin_lock_init(&lsm->lsm_lock);
302 lsm->lsm_maxbytes = maxbytes;
303 lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
304 lsm->lsm_magic = le32_to_cpu(lmm->lmm_magic);
305 lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
306 lsm->lsm_entry_count = 1;
307 lsm->lsm_is_released = pattern & LOV_PATTERN_F_RELEASED;
308 lsm->lsm_entries[0] = lsme;
318 static inline struct lov_stripe_md *
319 lsm_unpackmd_v1(struct lov_obd *lov, void *buf, size_t buf_size)
321 struct lov_mds_md_v1 *lmm = buf;
323 return lsm_unpackmd_v1v3(lov, buf, buf_size, NULL, lmm->lmm_objects);
326 const struct lsm_operations lsm_v1_ops = {
327 .lsm_unpackmd = lsm_unpackmd_v1,
330 static inline struct lov_stripe_md *
331 lsm_unpackmd_v3(struct lov_obd *lov, void *buf, size_t buf_size)
333 struct lov_mds_md_v3 *lmm = buf;
335 return lsm_unpackmd_v1v3(lov, buf, buf_size, lmm->lmm_pool_name,
339 const struct lsm_operations lsm_v3_ops = {
340 .lsm_unpackmd = lsm_unpackmd_v3,
343 static int lsm_verify_comp_md_v1(struct lov_comp_md_v1 *lcm,
346 unsigned int entry_count;
350 lcm_size = le32_to_cpu(lcm->lcm_size);
351 if (lcm_buf_size < lcm_size) {
352 CERROR("bad LCM buffer size %zu, expected %zu\n",
353 lcm_buf_size, lcm_size);
357 entry_count = le16_to_cpu(lcm->lcm_entry_count);
358 for (i = 0; i < entry_count; i++) {
359 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
363 blob_offset = le32_to_cpu(lcme->lcme_offset);
364 blob_size = le32_to_cpu(lcme->lcme_size);
366 if (lcm_size < blob_offset || lcm_size < blob_size ||
367 lcm_size < blob_offset + blob_size) {
368 CERROR("LCM entry %u has invalid blob: "
369 "LCM size = %zu, offset = %zu, size = %zu\n",
370 le32_to_cpu(lcme->lcme_id),
371 lcm_size, blob_offset, blob_size);
379 static struct lov_stripe_md_entry *
380 lsme_unpack_comp(struct lov_obd *lov, struct lov_mds_md *lmm,
381 size_t lmm_buf_size, bool inited, loff_t *maxbytes)
384 unsigned int stripe_count;
386 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
387 if (stripe_count == 0)
388 RETURN(ERR_PTR(-EINVAL));
389 /* un-instantiated lmm contains no ost id info, i.e. lov_ost_data_v1 */
393 magic = le32_to_cpu(lmm->lmm_magic);
394 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
395 RETURN(ERR_PTR(-EINVAL));
397 if (lmm_buf_size < lov_mds_md_size(stripe_count, magic))
398 RETURN(ERR_PTR(-EINVAL));
400 if (magic == LOV_MAGIC_V1) {
401 return lsme_unpack(lov, lmm, lmm_buf_size, NULL,
402 inited, lmm->lmm_objects, maxbytes);
404 struct lov_mds_md_v3 *lmm3 = (struct lov_mds_md_v3 *)lmm;
406 return lsme_unpack(lov, lmm, lmm_buf_size, lmm3->lmm_pool_name,
407 inited, lmm3->lmm_objects, maxbytes);
411 static struct lov_stripe_md *
412 lsm_unpackmd_comp_md_v1(struct lov_obd *lov, void *buf, size_t buf_size)
414 struct lov_comp_md_v1 *lcm = buf;
415 struct lov_stripe_md *lsm;
417 unsigned int entry_count = 0;
422 rc = lsm_verify_comp_md_v1(buf, buf_size);
426 entry_count = le16_to_cpu(lcm->lcm_entry_count);
428 lsm_size = offsetof(typeof(*lsm), lsm_entries[entry_count]);
429 OBD_ALLOC(lsm, lsm_size);
431 return ERR_PTR(-ENOMEM);
433 atomic_set(&lsm->lsm_refc, 1);
434 spin_lock_init(&lsm->lsm_lock);
435 lsm->lsm_magic = le32_to_cpu(lcm->lcm_magic);
436 lsm->lsm_layout_gen = le32_to_cpu(lcm->lcm_layout_gen);
437 lsm->lsm_entry_count = entry_count;
438 lsm->lsm_is_released = true;
439 lsm->lsm_maxbytes = LLONG_MIN;
441 for (i = 0; i < entry_count; i++) {
442 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
443 struct lov_stripe_md_entry *lsme;
448 blob_offset = le32_to_cpu(lcme->lcme_offset);
449 blob_size = le32_to_cpu(lcme->lcme_size);
450 blob = (char *)lcm + blob_offset;
452 lsme = lsme_unpack_comp(lov, blob, blob_size,
453 le32_to_cpu(lcme->lcme_flags) &
455 (i == entry_count - 1) ? &maxbytes :
458 GOTO(out_lsm, rc = PTR_ERR(lsme));
460 if (!(lsme->lsme_pattern & LOV_PATTERN_F_RELEASED))
461 lsm->lsm_is_released = false;
463 lsm->lsm_entries[i] = lsme;
464 lsme->lsme_id = le32_to_cpu(lcme->lcme_id);
465 lsme->lsme_flags = le32_to_cpu(lcme->lcme_flags);
466 lu_extent_le_to_cpu(&lsme->lsme_extent, &lcme->lcme_extent);
468 if (i == entry_count - 1) {
469 lsm->lsm_maxbytes = (loff_t)lsme->lsme_extent.e_start +
471 /* the last component hasn't been defined, or
472 * lsm_maxbytes overflowed. */
473 if (lsme->lsme_extent.e_end != LUSTRE_EOF ||
475 (loff_t)lsme->lsme_extent.e_start)
476 lsm->lsm_maxbytes = MAX_LFS_FILESIZE;
483 for (i = 0; i < entry_count; i++)
484 if (lsm->lsm_entries[i] != NULL)
485 lsme_free(lsm->lsm_entries[i]);
487 OBD_FREE(lsm, lsm_size);
492 const struct lsm_operations lsm_comp_md_v1_ops = {
493 .lsm_unpackmd = lsm_unpackmd_comp_md_v1,
496 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
500 CDEBUG(level, "lsm %p, objid "DOSTID", maxbytes %#llx, magic 0x%08X, "
501 "refc: %d, entry: %u, layout_gen %u\n",
502 lsm, POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
503 atomic_read(&lsm->lsm_refc), lsm->lsm_entry_count,
504 lsm->lsm_layout_gen);
506 for (i = 0; i < lsm->lsm_entry_count; i++) {
507 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
509 CDEBUG(level, DEXT ": id: %u, flags: %x, "
510 "magic 0x%08X, layout_gen %u, "
511 "stripe count %u, sstripe size %u, "
512 "pool: ["LOV_POOLNAMEF"]\n",
513 PEXT(&lse->lsme_extent), lse->lsme_id, lse->lsme_flags,
514 lse->lsme_magic, lse->lsme_layout_gen,
515 lse->lsme_stripe_count, lse->lsme_stripe_size,
516 lse->lsme_pool_name);
517 if (!lsme_inited(lse) ||
518 lse->lsme_pattern & LOV_PATTERN_F_RELEASED)
520 for (j = 0; j < lse->lsme_stripe_count; j++) {
521 CDEBUG(level, " oinfo:%p: ostid: "DOSTID
522 " ost idx: %d gen: %d\n",
524 POSTID(&lse->lsme_oinfo[j]->loi_oi),
525 lse->lsme_oinfo[j]->loi_ost_idx,
526 lse->lsme_oinfo[j]->loi_ost_gen);
531 int lov_lsm_entry(const struct lov_stripe_md *lsm, __u64 offset)
535 for (i = 0; i < lsm->lsm_entry_count; i++) {
536 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
538 if ((offset >= lse->lsme_extent.e_start &&
539 offset < lse->lsme_extent.e_end) ||
540 (offset == OBD_OBJECT_EOF &&
541 lse->lsme_extent.e_end == OBD_OBJECT_EOF))