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, 2017, 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 "lov_internal.h"
47 lu_extent_le_to_cpu(struct lu_extent *dst, const struct lu_extent *src)
49 dst->e_start = le64_to_cpu(src->e_start);
50 dst->e_end = le64_to_cpu(src->e_end);
54 * Find minimum stripe maxbytes value. For inactive or
55 * reconnecting targets use LUSTRE_EXT3_STRIPE_MAXBYTES.
57 static loff_t lov_tgt_maxbytes(struct lov_tgt_desc *tgt)
59 struct obd_import *imp;
60 loff_t maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
65 imp = tgt->ltd_obd->u.cli.cl_import;
69 spin_lock(&imp->imp_lock);
70 if ((imp->imp_state == LUSTRE_IMP_FULL ||
71 imp->imp_state == LUSTRE_IMP_IDLE) &&
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_MDT &&
97 lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_RAID0) {
98 CERROR("bad striping pattern\n");
99 lov_dump_lmm_common(D_WARNING, lmm);
103 if (lmm->lmm_stripe_size == 0 ||
104 (le32_to_cpu(lmm->lmm_stripe_size)&(LOV_MIN_STRIPE_SIZE-1)) != 0) {
105 CERROR("bad stripe size %u\n",
106 le32_to_cpu(lmm->lmm_stripe_size));
107 lov_dump_lmm_common(D_WARNING, lmm);
113 static void lsme_free(struct lov_stripe_md_entry *lsme)
115 unsigned int stripe_count = lsme->lsme_stripe_count;
119 if (!lsme_inited(lsme) ||
120 lsme->lsme_pattern & LOV_PATTERN_F_RELEASED)
122 for (i = 0; i < stripe_count; i++)
123 OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
125 lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
126 OBD_FREE_LARGE(lsme, lsme_size);
129 void lsm_free(struct lov_stripe_md *lsm)
131 unsigned int entry_count = lsm->lsm_entry_count;
135 for (i = 0; i < entry_count; i++)
136 lsme_free(lsm->lsm_entries[i]);
138 lsm_size = offsetof(typeof(*lsm), lsm_entries[entry_count]);
139 OBD_FREE(lsm, lsm_size);
143 * Unpack a struct lov_mds_md into a struct lov_stripe_md_entry.
145 * The caller should set id and extent.
147 static struct lov_stripe_md_entry *
148 lsme_unpack(struct lov_obd *lov, struct lov_mds_md *lmm, size_t buf_size,
149 const char *pool_name, bool inited, struct lov_ost_data_v1 *objects,
152 struct lov_stripe_md_entry *lsme;
154 loff_t min_stripe_maxbytes = 0;
158 unsigned int stripe_count;
162 magic = le32_to_cpu(lmm->lmm_magic);
163 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
164 RETURN(ERR_PTR(-EINVAL));
166 pattern = le32_to_cpu(lmm->lmm_pattern);
167 if (pattern & LOV_PATTERN_F_RELEASED || !inited)
170 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
172 if (buf_size < (magic == LOV_MAGIC_V1 ? sizeof(struct lov_mds_md_v1) :
173 sizeof(struct lov_mds_md_v3))) {
174 CERROR("LOV EA %s too small: %zu, need %u\n",
175 magic == LOV_MAGIC_V1 ? "V1" : "V3", buf_size,
176 lov_mds_md_size(stripe_count, magic == LOV_MAGIC_V1 ?
177 LOV_MAGIC_V1 : LOV_MAGIC_V3));
178 lov_dump_lmm_common(D_WARNING, lmm);
179 return ERR_PTR(-EINVAL);
182 rc = lsm_lmm_verify_v1v3(lmm, buf_size, stripe_count);
186 lsme_size = offsetof(typeof(*lsme), lsme_oinfo[stripe_count]);
187 OBD_ALLOC_LARGE(lsme, lsme_size);
189 RETURN(ERR_PTR(-ENOMEM));
191 lsme->lsme_magic = magic;
192 lsme->lsme_pattern = pattern;
193 lsme->lsme_flags = 0;
194 lsme->lsme_stripe_size = le32_to_cpu(lmm->lmm_stripe_size);
195 /* preserve the possible -1 stripe count for uninstantiated component */
196 lsme->lsme_stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
197 lsme->lsme_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
200 size_t pool_name_len;
202 pool_name_len = strlcpy(lsme->lsme_pool_name, pool_name,
203 sizeof(lsme->lsme_pool_name));
204 if (pool_name_len >= sizeof(lsme->lsme_pool_name))
205 GOTO(out_lsme, rc = -E2BIG);
208 /* with Data-on-MDT set maxbytes to stripe size */
209 if (lsme_is_dom(lsme)) {
210 lov_bytes = lsme->lsme_stripe_size;
214 for (i = 0; i < stripe_count; i++) {
215 struct lov_oinfo *loi;
216 struct lov_tgt_desc *ltd;
218 OBD_SLAB_ALLOC_PTR_GFP(loi, lov_oinfo_slab, GFP_NOFS);
220 GOTO(out_lsme, rc = -ENOMEM);
222 lsme->lsme_oinfo[i] = loi;
224 ostid_le_to_cpu(&objects[i].l_ost_oi, &loi->loi_oi);
225 loi->loi_ost_idx = le32_to_cpu(objects[i].l_ost_idx);
226 loi->loi_ost_gen = le32_to_cpu(objects[i].l_ost_gen);
227 if (lov_oinfo_is_dummy(loi))
230 if (loi->loi_ost_idx >= lov->desc.ld_tgt_count &&
231 !lov2obd(lov)->obd_process_conf) {
232 CERROR("%s: OST index %d more than OST count %d\n",
233 (char*)lov->desc.ld_uuid.uuid,
234 loi->loi_ost_idx, lov->desc.ld_tgt_count);
235 lov_dump_lmm_v1(D_WARNING, lmm);
236 GOTO(out_lsme, rc = -EINVAL);
239 ltd = lov->lov_tgts[loi->loi_ost_idx];
241 CERROR("%s: OST index %d missing\n",
242 (char*)lov->desc.ld_uuid.uuid, loi->loi_ost_idx);
243 lov_dump_lmm_v1(D_WARNING, lmm);
247 lov_bytes = lov_tgt_maxbytes(ltd);
248 if (min_stripe_maxbytes == 0 || lov_bytes < min_stripe_maxbytes)
249 min_stripe_maxbytes = lov_bytes;
252 if (min_stripe_maxbytes == 0)
253 min_stripe_maxbytes = LUSTRE_EXT3_STRIPE_MAXBYTES;
255 lov_bytes = min_stripe_maxbytes * stripe_count;
259 if (lov_bytes < min_stripe_maxbytes) /* handle overflow */
260 *maxbytes = MAX_LFS_FILESIZE;
262 *maxbytes = lov_bytes;
268 for (i = 0; i < stripe_count; i++) {
269 struct lov_oinfo *loi = lsme->lsme_oinfo[i];
272 OBD_SLAB_FREE_PTR(lsme->lsme_oinfo[i], lov_oinfo_slab);
274 OBD_FREE_LARGE(lsme, lsme_size);
280 lov_stripe_md *lsm_unpackmd_v1v3(struct lov_obd *lov, struct lov_mds_md *lmm,
281 size_t buf_size, const char *pool_name,
282 struct lov_ost_data_v1 *objects)
284 struct lov_stripe_md *lsm;
285 struct lov_stripe_md_entry *lsme;
291 pattern = le32_to_cpu(lmm->lmm_pattern);
293 lsme = lsme_unpack(lov, lmm, buf_size, pool_name, true, objects,
296 RETURN(ERR_CAST(lsme));
298 lsme->lsme_flags = LCME_FL_INIT;
299 lsme->lsme_extent.e_start = 0;
300 lsme->lsme_extent.e_end = LUSTRE_EOF;
302 lsm_size = offsetof(typeof(*lsm), lsm_entries[1]);
303 OBD_ALLOC(lsm, lsm_size);
305 GOTO(out_lsme, rc = -ENOMEM);
307 atomic_set(&lsm->lsm_refc, 1);
308 spin_lock_init(&lsm->lsm_lock);
309 lsm->lsm_maxbytes = maxbytes;
310 lmm_oi_le_to_cpu(&lsm->lsm_oi, &lmm->lmm_oi);
311 lsm->lsm_magic = le32_to_cpu(lmm->lmm_magic);
312 lsm->lsm_layout_gen = le16_to_cpu(lmm->lmm_layout_gen);
313 lsm->lsm_entry_count = 1;
314 lsm->lsm_is_released = pattern & LOV_PATTERN_F_RELEASED;
315 lsm->lsm_entries[0] = lsme;
325 static inline struct lov_stripe_md *
326 lsm_unpackmd_v1(struct lov_obd *lov, void *buf, size_t buf_size)
328 struct lov_mds_md_v1 *lmm = buf;
330 return lsm_unpackmd_v1v3(lov, buf, buf_size, NULL, lmm->lmm_objects);
333 const struct lsm_operations lsm_v1_ops = {
334 .lsm_unpackmd = lsm_unpackmd_v1,
338 struct lov_stripe_md *lsm_unpackmd_v3(struct lov_obd *lov, void *buf,
341 struct lov_mds_md_v3 *lmm = buf;
343 return lsm_unpackmd_v1v3(lov, buf, buf_size, lmm->lmm_pool_name,
347 const struct lsm_operations lsm_v3_ops = {
348 .lsm_unpackmd = lsm_unpackmd_v3,
351 static int lsm_verify_comp_md_v1(struct lov_comp_md_v1 *lcm,
354 unsigned int entry_count;
358 lcm_size = le32_to_cpu(lcm->lcm_size);
359 if (lcm_buf_size < lcm_size) {
360 CERROR("bad LCM buffer size %zu, expected %zu\n",
361 lcm_buf_size, lcm_size);
365 entry_count = le16_to_cpu(lcm->lcm_entry_count);
366 for (i = 0; i < entry_count; i++) {
367 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
371 blob_offset = le32_to_cpu(lcme->lcme_offset);
372 blob_size = le32_to_cpu(lcme->lcme_size);
374 if (lcm_size < blob_offset || lcm_size < blob_size ||
375 lcm_size < blob_offset + blob_size) {
376 CERROR("LCM entry %u has invalid blob: "
377 "LCM size = %zu, offset = %zu, size = %zu\n",
378 le32_to_cpu(lcme->lcme_id),
379 lcm_size, blob_offset, blob_size);
387 static struct lov_stripe_md_entry *
388 lsme_unpack_comp(struct lov_obd *lov, struct lov_mds_md *lmm,
389 size_t lmm_buf_size, bool inited, loff_t *maxbytes)
392 unsigned int stripe_count;
394 stripe_count = le16_to_cpu(lmm->lmm_stripe_count);
395 if (stripe_count == 0 &&
396 lov_pattern(le32_to_cpu(lmm->lmm_pattern)) != LOV_PATTERN_MDT)
397 RETURN(ERR_PTR(-EINVAL));
398 /* un-instantiated lmm contains no ost id info, i.e. lov_ost_data_v1 */
402 magic = le32_to_cpu(lmm->lmm_magic);
403 if (magic != LOV_MAGIC_V1 && magic != LOV_MAGIC_V3)
404 RETURN(ERR_PTR(-EINVAL));
406 if (lmm_buf_size < lov_mds_md_size(stripe_count, magic))
407 RETURN(ERR_PTR(-EINVAL));
409 if (magic == LOV_MAGIC_V1) {
410 return lsme_unpack(lov, lmm, lmm_buf_size, NULL,
411 inited, lmm->lmm_objects, maxbytes);
413 struct lov_mds_md_v3 *lmm3 = (struct lov_mds_md_v3 *)lmm;
415 return lsme_unpack(lov, lmm, lmm_buf_size, lmm3->lmm_pool_name,
416 inited, lmm3->lmm_objects, maxbytes);
420 static struct lov_stripe_md *
421 lsm_unpackmd_comp_md_v1(struct lov_obd *lov, void *buf, size_t buf_size)
423 struct lov_comp_md_v1 *lcm = buf;
424 struct lov_stripe_md *lsm;
426 unsigned int entry_count = 0;
431 rc = lsm_verify_comp_md_v1(buf, buf_size);
435 entry_count = le16_to_cpu(lcm->lcm_entry_count);
437 lsm_size = offsetof(typeof(*lsm), lsm_entries[entry_count]);
438 OBD_ALLOC(lsm, lsm_size);
440 return ERR_PTR(-ENOMEM);
442 atomic_set(&lsm->lsm_refc, 1);
443 spin_lock_init(&lsm->lsm_lock);
444 lsm->lsm_magic = le32_to_cpu(lcm->lcm_magic);
445 lsm->lsm_layout_gen = le32_to_cpu(lcm->lcm_layout_gen);
446 lsm->lsm_entry_count = entry_count;
447 lsm->lsm_mirror_count = le16_to_cpu(lcm->lcm_mirror_count);
448 lsm->lsm_flags = le16_to_cpu(lcm->lcm_flags);
449 lsm->lsm_is_released = true;
450 lsm->lsm_maxbytes = LLONG_MIN;
452 for (i = 0; i < entry_count; i++) {
453 struct lov_comp_md_entry_v1 *lcme = &lcm->lcm_entries[i];
454 struct lov_stripe_md_entry *lsme;
459 blob_offset = le32_to_cpu(lcme->lcme_offset);
460 blob_size = le32_to_cpu(lcme->lcme_size);
461 blob = (char *)lcm + blob_offset;
463 lsme = lsme_unpack_comp(lov, blob, blob_size,
464 le32_to_cpu(lcme->lcme_flags) &
466 (i == entry_count - 1) ? &maxbytes :
469 GOTO(out_lsm, rc = PTR_ERR(lsme));
471 if (!(lsme->lsme_pattern & LOV_PATTERN_F_RELEASED))
472 lsm->lsm_is_released = false;
474 lsm->lsm_entries[i] = lsme;
475 lsme->lsme_id = le32_to_cpu(lcme->lcme_id);
476 lsme->lsme_flags = le32_to_cpu(lcme->lcme_flags);
477 if (lsme->lsme_flags & LCME_FL_NOSYNC)
478 lsme->lsme_timestamp =
479 le64_to_cpu(lcme->lcme_timestamp);
480 lu_extent_le_to_cpu(&lsme->lsme_extent, &lcme->lcme_extent);
482 if (i == entry_count - 1) {
483 lsm->lsm_maxbytes = (loff_t)lsme->lsme_extent.e_start +
486 * the last component hasn't been defined, or
487 * lsm_maxbytes overflowed.
489 if (!lsme_is_dom(lsme) &&
490 (lsme->lsme_extent.e_end != LUSTRE_EOF ||
492 (loff_t)lsme->lsme_extent.e_start))
493 lsm->lsm_maxbytes = MAX_LFS_FILESIZE;
500 for (i = 0; i < entry_count; i++)
501 if (lsm->lsm_entries[i])
502 lsme_free(lsm->lsm_entries[i]);
504 OBD_FREE(lsm, lsm_size);
509 const struct lsm_operations lsm_comp_md_v1_ops = {
510 .lsm_unpackmd = lsm_unpackmd_comp_md_v1,
513 void dump_lsm(unsigned int level, const struct lov_stripe_md *lsm)
517 CDEBUG(level, "lsm %p, objid "DOSTID", maxbytes %#llx, magic 0x%08X, "
518 "refc: %d, entry: %u, layout_gen %u\n",
519 lsm, POSTID(&lsm->lsm_oi), lsm->lsm_maxbytes, lsm->lsm_magic,
520 atomic_read(&lsm->lsm_refc), lsm->lsm_entry_count,
521 lsm->lsm_layout_gen);
523 for (i = 0; i < lsm->lsm_entry_count; i++) {
524 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
526 CDEBUG(level, DEXT ": id: %u, flags: %x, "
527 "magic 0x%08X, layout_gen %u, "
528 "stripe count %u, sstripe size %u, "
529 "pool: ["LOV_POOLNAMEF"]\n",
530 PEXT(&lse->lsme_extent), lse->lsme_id, lse->lsme_flags,
531 lse->lsme_magic, lse->lsme_layout_gen,
532 lse->lsme_stripe_count, lse->lsme_stripe_size,
533 lse->lsme_pool_name);
534 if (!lsme_inited(lse) ||
535 lse->lsme_pattern & LOV_PATTERN_F_RELEASED)
537 for (j = 0; j < lse->lsme_stripe_count; j++) {
538 CDEBUG(level, " oinfo:%p: ostid: "DOSTID
539 " ost idx: %d gen: %d\n",
541 POSTID(&lse->lsme_oinfo[j]->loi_oi),
542 lse->lsme_oinfo[j]->loi_ost_idx,
543 lse->lsme_oinfo[j]->loi_ost_gen);
548 int lov_lsm_entry(const struct lov_stripe_md *lsm, __u64 offset)
552 for (i = 0; i < lsm->lsm_entry_count; i++) {
553 struct lov_stripe_md_entry *lse = lsm->lsm_entries[i];
555 if ((offset >= lse->lsme_extent.e_start &&
556 offset < lse->lsme_extent.e_end) ||
557 (offset == OBD_OBJECT_EOF &&
558 lse->lsme_extent.e_end == OBD_OBJECT_EOF))