4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * All rights reserved. This program and the accompanying materials
7 * are made available under the terms of the GNU Lesser General Public License
8 * (LGPL) version 2.1 or (at your discretion) any later version.
9 * (LGPL) version 2.1 accompanies this distribution, and is available at
10 * http://www.gnu.org/licenses/lgpl-2.1.html
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
20 * lustre/utils/liblustreapi_layout.c
22 * lustreapi library for layout calls for interacting with the layout of
23 * Lustre files while hiding details of the internal data structures
26 * Copyright (c) 2016, 2017, Intel Corporation.
28 * Author: Ned Bass <bass6@llnl.gov>
38 #include <sys/xattr.h>
39 #include <sys/param.h>
41 #include <libcfs/util/list.h>
42 #include <lustre/lustreapi.h>
43 #include "lustreapi_internal.h"
46 * Layout component, which contains all attributes of a plain
49 struct llapi_layout_comp {
51 uint64_t llc_stripe_size;
52 uint64_t llc_stripe_count;
53 uint64_t llc_stripe_offset;
54 /* Add 1 so user always gets back a null terminated string. */
55 char llc_pool_name[LOV_MAXPOOLNAME + 1];
56 /** Number of objects in llc_objects array if was initialized. */
57 uint32_t llc_objects_count;
58 struct lov_user_ost_data_v1 *llc_objects;
59 /* fields used only for composite layouts */
60 struct lu_extent llc_extent; /* [start, end) of component */
61 uint32_t llc_id; /* unique ID of component */
62 uint32_t llc_flags; /* LCME_FL_* flags */
63 uint64_t llc_timestamp; /* snapshot timestamp */
64 struct list_head llc_list; /* linked to the llapi_layout
70 * An Opaque data type abstracting the layout of a Lustre file.
73 uint32_t llot_magic; /* LLAPI_LAYOUT_MAGIC */
76 bool llot_is_composite;
77 uint16_t llot_mirror_count;
78 /* Cursor pointing to one of the components in llot_comp_list */
79 struct llapi_layout_comp *llot_cur_comp;
80 struct list_head llot_comp_list;
84 * Compute the number of elements in the lmm_objects array of \a lum
85 * with size \a lum_size.
87 * \param[in] lum the struct lov_user_md to check
88 * \param[in] lum_size the number of bytes in \a lum
90 * \retval number of elements in array lum->lmm_objects
92 static int llapi_layout_objects_in_lum(struct lov_user_md *lum, size_t lum_size)
97 if (lum_size < lov_user_md_size(0, LOV_MAGIC_V1))
100 if (lum->lmm_magic == __swab32(LOV_MAGIC_V1) ||
101 lum->lmm_magic == __swab32(LOV_MAGIC_V3))
102 magic = __swab32(lum->lmm_magic);
104 magic = lum->lmm_magic;
106 base_size = lov_user_md_size(0, magic);
108 if (lum_size <= base_size)
111 return (lum_size - base_size) / sizeof(lum->lmm_objects[0]);
115 * Byte-swap the fields of struct lov_user_md.
117 * XXX Rather than duplicating swabbing code here, we should eventually
118 * refactor the needed functions in lustre/ptlrpc/pack_generic.c
119 * into a library that can be shared between kernel and user code.
122 llapi_layout_swab_lov_user_md(struct lov_user_md *lum, int lum_size)
124 int i, j, ent_count, obj_count;
125 struct lov_comp_md_v1 *comp_v1 = NULL;
126 struct lov_comp_md_entry_v1 *ent;
127 struct lov_user_ost_data *lod;
129 if (lum->lmm_magic != __swab32(LOV_MAGIC_V1) &&
130 lum->lmm_magic != __swab32(LOV_MAGIC_V3) &&
131 lum->lmm_magic != __swab32(LOV_MAGIC_COMP_V1))
134 if (lum->lmm_magic == __swab32(LOV_MAGIC_COMP_V1))
135 comp_v1 = (struct lov_comp_md_v1 *)lum;
137 if (comp_v1 != NULL) {
138 comp_v1->lcm_magic = __swab32(comp_v1->lcm_magic);
139 comp_v1->lcm_size = __swab32(comp_v1->lcm_size);
140 comp_v1->lcm_layout_gen = __swab32(comp_v1->lcm_layout_gen);
141 comp_v1->lcm_flags = __swab16(comp_v1->lcm_flags);
142 comp_v1->lcm_entry_count = __swab16(comp_v1->lcm_entry_count);
143 ent_count = comp_v1->lcm_entry_count;
148 for (i = 0; i < ent_count; i++) {
149 if (comp_v1 != NULL) {
150 ent = &comp_v1->lcm_entries[i];
151 ent->lcme_id = __swab32(ent->lcme_id);
152 ent->lcme_flags = __swab32(ent->lcme_flags);
153 ent->lcme_timestamp = __swab64(ent->lcme_timestamp);
154 ent->lcme_extent.e_start = __swab64(ent->lcme_extent.e_start);
155 ent->lcme_extent.e_end = __swab64(ent->lcme_extent.e_end);
156 ent->lcme_offset = __swab32(ent->lcme_offset);
157 ent->lcme_size = __swab32(ent->lcme_size);
159 lum = (struct lov_user_md *)((char *)comp_v1 +
161 lum_size = ent->lcme_size;
163 obj_count = llapi_layout_objects_in_lum(lum, lum_size);
165 lum->lmm_magic = __swab32(lum->lmm_magic);
166 lum->lmm_pattern = __swab32(lum->lmm_pattern);
167 lum->lmm_stripe_size = __swab32(lum->lmm_stripe_size);
168 lum->lmm_stripe_count = __swab16(lum->lmm_stripe_count);
169 lum->lmm_stripe_offset = __swab16(lum->lmm_stripe_offset);
171 if (lum->lmm_magic != LOV_MAGIC_V1) {
172 struct lov_user_md_v3 *v3;
173 v3 = (struct lov_user_md_v3 *)lum;
174 lod = v3->lmm_objects;
176 lod = lum->lmm_objects;
179 for (j = 0; j < obj_count; j++)
180 lod[j].l_ost_idx = __swab32(lod[j].l_ost_idx);
185 * (Re-)allocate llc_objects[] to \a num_stripes stripes.
187 * Copy over existing llc_objects[], if any, to the new llc_objects[].
189 * \param[in] layout existing layout to be modified
190 * \param[in] num_stripes number of stripes in new layout
192 * \retval 0 if the objects are re-allocated successfully
193 * \retval -1 on error with errno set
195 static int __llapi_comp_objects_realloc(struct llapi_layout_comp *comp,
196 unsigned int new_stripes)
198 struct lov_user_ost_data_v1 *new_objects;
201 if (new_stripes > LOV_MAX_STRIPE_COUNT) {
206 if (new_stripes == comp->llc_objects_count)
209 if (new_stripes != 0 && new_stripes <= comp->llc_objects_count)
212 new_objects = realloc(comp->llc_objects,
213 sizeof(*new_objects) * new_stripes);
214 if (new_objects == NULL && new_stripes != 0) {
219 for (i = comp->llc_objects_count; i < new_stripes; i++)
220 new_objects[i].l_ost_idx = LLAPI_LAYOUT_IDX_MAX;
222 comp->llc_objects = new_objects;
223 comp->llc_objects_count = new_stripes;
229 * Allocate storage for a llapi_layout_comp with \a num_stripes stripes.
231 * \param[in] num_stripes number of stripes in new layout
233 * \retval valid pointer if allocation succeeds
234 * \retval NULL if allocation fails
236 static struct llapi_layout_comp *__llapi_comp_alloc(unsigned int num_stripes)
238 struct llapi_layout_comp *comp;
240 if (num_stripes > LOV_MAX_STRIPE_COUNT) {
245 comp = calloc(1, sizeof(*comp));
251 comp->llc_objects = NULL;
252 comp->llc_objects_count = 0;
254 if (__llapi_comp_objects_realloc(comp, num_stripes) < 0) {
260 comp->llc_pattern = LLAPI_LAYOUT_DEFAULT;
261 comp->llc_stripe_size = LLAPI_LAYOUT_DEFAULT;
262 comp->llc_stripe_count = LLAPI_LAYOUT_DEFAULT;
263 comp->llc_stripe_offset = LLAPI_LAYOUT_DEFAULT;
264 comp->llc_pool_name[0] = '\0';
265 comp->llc_extent.e_start = 0;
266 comp->llc_extent.e_end = LUSTRE_EOF;
269 INIT_LIST_HEAD(&comp->llc_list);
275 * Free memory allocated for \a comp
277 * \param[in] comp previously allocated by __llapi_comp_alloc()
279 static void __llapi_comp_free(struct llapi_layout_comp *comp)
281 if (comp->llc_objects != NULL)
282 free(comp->llc_objects);
287 * Free memory allocated for \a layout.
289 * \param[in] layout previously allocated by llapi_layout_alloc()
291 void llapi_layout_free(struct llapi_layout *layout)
293 struct llapi_layout_comp *comp, *n;
298 list_for_each_entry_safe(comp, n, &layout->llot_comp_list, llc_list) {
299 list_del_init(&comp->llc_list);
300 __llapi_comp_free(comp);
306 * Allocate and initialize a llapi_layout structure.
308 * \retval valid llapi_layout pointer on success
309 * \retval NULL if memory allocation fails
311 static struct llapi_layout *__llapi_layout_alloc(void)
313 struct llapi_layout *layout;
315 layout = calloc(1, sizeof(*layout));
316 if (layout == NULL) {
322 layout->llot_magic = LLAPI_LAYOUT_MAGIC;
323 layout->llot_gen = 0;
324 layout->llot_flags = 0;
325 layout->llot_is_composite = false;
326 layout->llot_mirror_count = 1;
327 layout->llot_cur_comp = NULL;
328 INIT_LIST_HEAD(&layout->llot_comp_list);
334 * Allocate and initialize a new plain layout.
336 * \retval valid llapi_layout pointer on success
337 * \retval NULL if memory allocation fails
339 struct llapi_layout *llapi_layout_alloc(void)
341 struct llapi_layout_comp *comp;
342 struct llapi_layout *layout;
344 layout = __llapi_layout_alloc();
348 comp = __llapi_comp_alloc(0);
354 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
355 layout->llot_cur_comp = comp;
361 * Check if the given \a lum_size is large enough to hold the required
364 * \param[in] lum the struct lov_user_md to check
365 * \param[in] lum_size the number of bytes in \a lum
367 * \retval true the \a lum_size is too small
368 * \retval false the \a lum_size is large enough
370 static bool llapi_layout_lum_truncated(struct lov_user_md *lum, size_t lum_size)
374 if (lum_size < sizeof(lum->lmm_magic))
377 if (lum->lmm_magic == LOV_MAGIC_V1 ||
378 lum->lmm_magic == __swab32(LOV_MAGIC_V1))
379 magic = LOV_MAGIC_V1;
380 else if (lum->lmm_magic == LOV_MAGIC_V3 ||
381 lum->lmm_magic == __swab32(LOV_MAGIC_V3))
382 magic = LOV_MAGIC_V3;
383 else if (lum->lmm_magic == LOV_MAGIC_COMP_V1 ||
384 lum->lmm_magic == __swab32(LOV_MAGIC_COMP_V1))
385 magic = LOV_MAGIC_COMP_V1;
389 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3)
390 return lum_size < lov_user_md_size(0, magic);
392 return lum_size < sizeof(struct lov_comp_md_v1);
395 /* Verify if the objects count in lum is consistent with the
396 * stripe count in lum. It applies to regular file only. */
397 static bool llapi_layout_lum_valid(struct lov_user_md *lum, int lum_size)
399 struct lov_comp_md_v1 *comp_v1 = NULL;
400 int i, ent_count, obj_count;
402 if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
403 comp_v1 = (struct lov_comp_md_v1 *)lum;
404 ent_count = comp_v1->lcm_entry_count;
405 } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
406 lum->lmm_magic == LOV_MAGIC_V3) {
412 for (i = 0; i < ent_count; i++) {
414 lum = (struct lov_user_md *)((char *)comp_v1 +
415 comp_v1->lcm_entries[i].lcme_offset);
416 lum_size = comp_v1->lcm_entries[i].lcme_size;
418 obj_count = llapi_layout_objects_in_lum(lum, lum_size);
421 if (!(comp_v1->lcm_entries[i].lcme_flags &
422 LCME_FL_INIT) && obj_count != 0)
424 } else if (obj_count != lum->lmm_stripe_count) {
432 * Convert the data from a lov_user_md to a newly allocated llapi_layout.
433 * The caller is responsible for freeing the returned pointer.
435 * \param[in] lov_xattr LOV user metadata xattr to copy data from
436 * \param[in] lov_xattr_size size the lov_xattr_size passed in
437 * \param[in] flags bitwise-or'd flags to control the behavior
439 * \retval valid llapi_layout pointer on success
440 * \retval NULL if memory allocation fails
442 struct llapi_layout *llapi_layout_get_by_xattr(void *lov_xattr,
443 ssize_t lov_xattr_size,
446 struct lov_user_md *lum = lov_xattr;
447 struct lov_comp_md_v1 *comp_v1 = NULL;
448 struct lov_comp_md_entry_v1 *ent;
449 struct lov_user_md *v1;
450 struct llapi_layout *layout = NULL;
451 struct llapi_layout_comp *comp;
452 int i, ent_count = 0, obj_count;
454 if (lov_xattr == NULL || lov_xattr_size <= 0) {
459 /* Return an error if we got back a partial layout. */
460 if (llapi_layout_lum_truncated(lov_xattr, lov_xattr_size)) {
465 #if __BYTE_ORDER == __BIG_ENDIAN
466 if (flags & LLAPI_LXF_COPY) {
467 lum = malloc(lov_xattr_size);
472 memcpy(lum, lov_xattr, lov_xattr_size);
476 llapi_layout_swab_lov_user_md(lum, lov_xattr_size);
478 if ((flags & LLAPI_LXF_CHECK) &&
479 !llapi_layout_lum_valid(lum, lov_xattr_size)) {
484 layout = __llapi_layout_alloc();
485 if (layout == NULL) {
490 if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
491 comp_v1 = (struct lov_comp_md_v1 *)lum;
492 ent_count = comp_v1->lcm_entry_count;
493 layout->llot_gen = comp_v1->lcm_layout_gen;
494 layout->llot_is_composite = true;
495 layout->llot_mirror_count = comp_v1->lcm_mirror_count + 1;
496 layout->llot_gen = comp_v1->lcm_layout_gen;
497 layout->llot_flags = comp_v1->lcm_flags;
498 } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
499 lum->lmm_magic == LOV_MAGIC_V3) {
501 layout->llot_is_composite = false;
503 if (lov_xattr_size <= 0) {
512 if (ent_count == 0) {
517 v1 = (struct lov_user_md *)lum;
518 for (i = 0; i < ent_count; i++) {
519 if (comp_v1 != NULL) {
520 ent = &comp_v1->lcm_entries[i];
521 v1 = (struct lov_user_md *)((char *)comp_v1 +
523 lov_xattr_size = ent->lcme_size;
528 obj_count = llapi_layout_objects_in_lum(v1, lov_xattr_size);
529 comp = __llapi_comp_alloc(obj_count);
534 comp->llc_extent.e_start = ent->lcme_extent.e_start;
535 comp->llc_extent.e_end = ent->lcme_extent.e_end;
536 comp->llc_id = ent->lcme_id;
537 comp->llc_flags = ent->lcme_flags;
538 if (comp->llc_flags & LCME_FL_NOSYNC)
539 comp->llc_timestamp = ent->lcme_timestamp;
541 comp->llc_extent.e_start = 0;
542 comp->llc_extent.e_end = LUSTRE_EOF;
547 if (v1->lmm_pattern == LOV_PATTERN_RAID0)
548 comp->llc_pattern = LLAPI_LAYOUT_RAID0;
549 else if (v1->lmm_pattern == (LOV_PATTERN_RAID0 |
550 LOV_PATTERN_OVERSTRIPING))
551 comp->llc_pattern = LLAPI_LAYOUT_OVERSTRIPING;
552 else if (v1->lmm_pattern == LOV_PATTERN_MDT)
553 comp->llc_pattern = LLAPI_LAYOUT_MDT;
555 /* Lustre only supports RAID0, overstripping
558 comp->llc_pattern = v1->lmm_pattern;
560 if (v1->lmm_stripe_size == 0)
561 comp->llc_stripe_size = LLAPI_LAYOUT_DEFAULT;
563 comp->llc_stripe_size = v1->lmm_stripe_size;
565 if (v1->lmm_stripe_count == (typeof(v1->lmm_stripe_count))-1)
566 comp->llc_stripe_count = LLAPI_LAYOUT_WIDE;
567 else if (v1->lmm_stripe_count == 0)
568 comp->llc_stripe_count = LLAPI_LAYOUT_DEFAULT;
570 comp->llc_stripe_count = v1->lmm_stripe_count;
572 if (v1->lmm_stripe_offset ==
573 (typeof(v1->lmm_stripe_offset))-1)
574 comp->llc_stripe_offset = LLAPI_LAYOUT_DEFAULT;
576 comp->llc_stripe_offset = v1->lmm_stripe_offset;
578 if (v1->lmm_magic != LOV_USER_MAGIC_V1) {
579 const struct lov_user_md_v3 *lumv3;
580 lumv3 = (struct lov_user_md_v3 *)v1;
581 snprintf(comp->llc_pool_name,
582 sizeof(comp->llc_pool_name),
583 "%s", lumv3->lmm_pool_name);
584 memcpy(comp->llc_objects, lumv3->lmm_objects,
585 obj_count * sizeof(lumv3->lmm_objects[0]));
587 const struct lov_user_md_v1 *lumv1;
588 lumv1 = (struct lov_user_md_v1 *)v1;
589 memcpy(comp->llc_objects, lumv1->lmm_objects,
590 obj_count * sizeof(lumv1->lmm_objects[0]));
594 comp->llc_stripe_offset =
595 comp->llc_objects[0].l_ost_idx;
597 comp->llc_ondisk = true;
598 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
599 layout->llot_cur_comp = comp;
603 if (lum != lov_xattr)
607 llapi_layout_free(layout);
612 __u32 llapi_pattern_to_lov(uint64_t pattern)
617 case LLAPI_LAYOUT_DEFAULT:
618 lov_pattern = LOV_PATTERN_RAID0;
620 case LLAPI_LAYOUT_RAID0:
621 lov_pattern = LOV_PATTERN_RAID0;
623 case LLAPI_LAYOUT_MDT:
624 lov_pattern = LOV_PATTERN_MDT;
626 case LLAPI_LAYOUT_OVERSTRIPING:
627 lov_pattern = LOV_PATTERN_OVERSTRIPING | LOV_PATTERN_RAID0;
630 lov_pattern = EINVAL;
637 * Convert the data from a llapi_layout to a newly allocated lov_user_md.
638 * The caller is responsible for freeing the returned pointer.
640 * \param[in] layout the layout to copy from
642 * \retval valid lov_user_md pointer on success
643 * \retval NULL if memory allocation fails or the layout is invalid
645 static struct lov_user_md *
646 llapi_layout_to_lum(const struct llapi_layout *layout)
648 struct llapi_layout_comp *comp;
649 struct lov_comp_md_v1 *comp_v1 = NULL;
650 struct lov_comp_md_entry_v1 *ent;
651 struct lov_user_md *lum = NULL;
656 if (layout == NULL ||
657 list_empty((struct list_head *)&layout->llot_comp_list)) {
662 /* Allocate header of lov_comp_md_v1 if necessary */
663 if (layout->llot_is_composite) {
666 list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
669 lum_size = sizeof(*comp_v1) + comp_cnt * sizeof(*ent);
670 lum = calloc(lum_size, 1);
675 comp_v1 = (struct lov_comp_md_v1 *)lum;
676 comp_v1->lcm_magic = LOV_USER_MAGIC_COMP_V1;
677 comp_v1->lcm_size = lum_size;
678 comp_v1->lcm_layout_gen = 0;
679 comp_v1->lcm_flags = layout->llot_flags;
680 comp_v1->lcm_entry_count = comp_cnt;
681 comp_v1->lcm_mirror_count = layout->llot_mirror_count - 1;
685 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
686 struct lov_user_md *blob;
689 int i, obj_count = 0;
690 struct lov_user_ost_data *lmm_objects;
691 uint64_t pattern = comp->llc_pattern;
693 if ((pattern & LLAPI_LAYOUT_SPECIFIC) != 0) {
694 if (comp->llc_objects_count <
695 comp->llc_stripe_count) {
699 magic = LOV_USER_MAGIC_SPECIFIC;
700 obj_count = comp->llc_stripe_count;
701 pattern &= ~LLAPI_LAYOUT_SPECIFIC;
702 } else if (strlen(comp->llc_pool_name) != 0) {
703 magic = LOV_USER_MAGIC_V3;
705 magic = LOV_USER_MAGIC_V1;
707 /* All stripes must be specified when the pattern contains
708 * LLAPI_LAYOUT_SPECIFIC */
709 for (i = 0; i < obj_count; i++) {
710 if (comp->llc_objects[i].l_ost_idx ==
711 LLAPI_LAYOUT_IDX_MAX) {
717 blob_size = lov_user_md_size(obj_count, magic);
718 blob = realloc(lum, lum_size + blob_size);
724 comp_v1 = (struct lov_comp_md_v1 *)lum;
725 blob = (struct lov_user_md *)((char *)lum + lum_size);
726 lum_size += blob_size;
729 blob->lmm_magic = magic;
730 blob->lmm_pattern = llapi_pattern_to_lov(pattern);
731 if (blob->lmm_pattern == EINVAL) {
736 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
737 blob->lmm_stripe_size = 0;
739 blob->lmm_stripe_size = comp->llc_stripe_size;
741 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
742 blob->lmm_stripe_count = 0;
743 else if (comp->llc_stripe_count == LLAPI_LAYOUT_WIDE)
744 blob->lmm_stripe_count = LOV_ALL_STRIPES;
746 blob->lmm_stripe_count = comp->llc_stripe_count;
748 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
749 blob->lmm_stripe_offset = -1;
751 blob->lmm_stripe_offset = comp->llc_stripe_offset;
753 if (magic == LOV_USER_MAGIC_V3 ||
754 magic == LOV_USER_MAGIC_SPECIFIC) {
755 struct lov_user_md_v3 *lumv3 =
756 (struct lov_user_md_v3 *)blob;
758 if (comp->llc_pool_name[0] != '\0') {
759 strncpy(lumv3->lmm_pool_name,
761 sizeof(lumv3->lmm_pool_name));
763 memset(lumv3->lmm_pool_name, 0,
764 sizeof(lumv3->lmm_pool_name));
766 lmm_objects = lumv3->lmm_objects;
768 lmm_objects = blob->lmm_objects;
771 for (i = 0; i < obj_count; i++)
772 lmm_objects[i].l_ost_idx =
773 comp->llc_objects[i].l_ost_idx;
775 if (layout->llot_is_composite) {
776 ent = &comp_v1->lcm_entries[ent_idx];
777 ent->lcme_id = comp->llc_id;
778 ent->lcme_flags = comp->llc_flags;
779 if (ent->lcme_flags & LCME_FL_NOSYNC)
780 ent->lcme_timestamp = comp->llc_timestamp;
781 ent->lcme_extent.e_start = comp->llc_extent.e_start;
782 ent->lcme_extent.e_end = comp->llc_extent.e_end;
783 ent->lcme_size = blob_size;
784 ent->lcme_offset = offset;
786 comp_v1->lcm_size += blob_size;
800 * Get the parent directory of a path.
802 * \param[in] path path to get parent of
803 * \param[out] buf buffer in which to store parent path
804 * \param[in] size size in bytes of buffer \a buf
806 static void get_parent_dir(const char *path, char *buf, size_t size)
810 strncpy(buf, path, size - 1);
811 p = strrchr(buf, '/');
815 } else if (size >= 2) {
816 strncpy(buf, ".", 2);
817 buf[size - 1] = '\0';
822 * Substitute unspecified attribute values in \a layout with values
823 * from fs global settings. (lov.stripesize, lov.stripecount,
826 * \param[in] layout layout to inherit values from
827 * \param[in] path file path of the filesystem
829 static void inherit_sys_attributes(struct llapi_layout *layout,
832 struct llapi_layout_comp *comp;
833 unsigned int ssize, scount, soffset;
836 rc = sattr_cache_get_defaults(NULL, path, &scount, &ssize, &soffset);
840 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
841 if (comp->llc_pattern == LLAPI_LAYOUT_DEFAULT)
842 comp->llc_pattern = LLAPI_LAYOUT_RAID0;
843 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
844 comp->llc_stripe_size = ssize;
845 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
846 comp->llc_stripe_count = scount;
847 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
848 comp->llc_stripe_offset = soffset;
853 * Get the current component of \a layout.
855 * \param[in] layout layout to get current component
857 * \retval valid llapi_layout_comp pointer on success
858 * \retval NULL on error
860 static struct llapi_layout_comp *
861 __llapi_layout_cur_comp(const struct llapi_layout *layout)
863 struct llapi_layout_comp *comp;
865 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
869 if (layout->llot_cur_comp == NULL) {
873 /* Verify data consistency */
874 list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
875 if (comp == layout->llot_cur_comp)
882 * Test if any attributes of \a layout are specified.
884 * \param[in] layout the layout to check
886 * \retval true any attributes are specified
887 * \retval false all attributes are unspecified
889 static bool is_any_specified(const struct llapi_layout *layout)
891 struct llapi_layout_comp *comp;
893 comp = __llapi_layout_cur_comp(layout);
897 if (layout->llot_is_composite || layout->llot_mirror_count != 1)
900 return comp->llc_pattern != LLAPI_LAYOUT_DEFAULT ||
901 comp->llc_stripe_size != LLAPI_LAYOUT_DEFAULT ||
902 comp->llc_stripe_count != LLAPI_LAYOUT_DEFAULT ||
903 comp->llc_stripe_offset != LLAPI_LAYOUT_DEFAULT ||
904 strlen(comp->llc_pool_name);
908 * Get the striping layout for the file referenced by file descriptor \a fd.
910 * If the filesystem does not support the "lustre." xattr namespace, the
911 * file must be on a non-Lustre filesystem, so set errno to ENOTTY per
912 * convention. If the file has no "lustre.lov" data, the file will
913 * inherit default values, so return a default layout.
915 * If the kernel gives us back less than the expected amount of data,
916 * we fail with errno set to EINTR.
918 * \param[in] fd open file descriptor
919 * \param[in] flags open file descriptor
921 * \retval valid llapi_layout pointer on success
922 * \retval NULL if an error occurs
924 struct llapi_layout *llapi_layout_get_by_fd(int fd, uint32_t flags)
927 struct lov_user_md *lum;
928 struct llapi_layout *layout = NULL;
932 lum_len = XATTR_SIZE_MAX;
933 lum = malloc(lum_len);
937 bytes_read = fgetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_len);
938 if (bytes_read < 0) {
939 if (errno == EOPNOTSUPP)
941 else if (errno == ENODATA)
942 layout = llapi_layout_alloc();
946 /* Directories may have a positive non-zero lum->lmm_stripe_count
947 * yet have an empty lum->lmm_objects array. For non-directories the
948 * amount of data returned from the kernel must be consistent
949 * with the stripe count. */
950 if (fstat(fd, &st) < 0)
953 layout = llapi_layout_get_by_xattr(lum, bytes_read,
954 S_ISDIR(st.st_mode) ? 0 : LLAPI_LXF_CHECK);
961 * Get the expected striping layout for a file at \a path.
963 * Substitute expected inherited attribute values for unspecified
964 * attributes. Unspecified attributes may belong to directories and
965 * never-written-to files, and indicate that default values will be
966 * assigned when files are created or first written to. A default value
967 * is inherited from the parent directory if the attribute is specified
968 * there, otherwise it is inherited from the filesystem root.
969 * Unspecified attributes normally have the value LLAPI_LAYOUT_DEFAULT.
971 * The complete \a path need not refer to an existing file or directory,
972 * but some leading portion of it must reside within a lustre filesystem.
973 * A use case for this interface would be to obtain the literal striping
974 * values that would be assigned to a new file in a given directory.
976 * \param[in] path path for which to get the expected layout
978 * \retval valid llapi_layout pointer on success
979 * \retval NULL if an error occurs
981 static struct llapi_layout *llapi_layout_expected(const char *path)
983 struct llapi_layout *path_layout = NULL;
984 char donor_path[PATH_MAX];
989 fd = open(path, O_RDONLY);
990 if (fd < 0 && errno != ENOENT)
996 path_layout = llapi_layout_get_by_fd(fd, 0);
1002 if (path_layout == NULL) {
1003 if (errno != ENODATA && errno != ENOENT)
1006 path_layout = llapi_layout_alloc();
1007 if (path_layout == NULL)
1011 if (is_any_specified(path_layout)) {
1012 inherit_sys_attributes(path_layout, path);
1016 llapi_layout_free(path_layout);
1018 rc = stat(path, &st);
1019 if (rc < 0 && errno != ENOENT)
1022 /* If path is a not a directory or doesn't exist, inherit layout
1023 * from parent directory. */
1024 if ((rc == 0 && !S_ISDIR(st.st_mode)) ||
1025 (rc < 0 && errno == ENOENT)) {
1026 get_parent_dir(path, donor_path, sizeof(donor_path));
1027 path_layout = llapi_layout_get_by_path(donor_path, 0);
1028 if (path_layout != NULL) {
1029 if (is_any_specified(path_layout)) {
1030 inherit_sys_attributes(path_layout, donor_path);
1033 llapi_layout_free(path_layout);
1037 /* Inherit layout from the filesystem root. */
1038 rc = llapi_search_mounts(path, 0, donor_path, NULL);
1041 path_layout = llapi_layout_get_by_path(donor_path, 0);
1042 if (path_layout == NULL)
1045 inherit_sys_attributes(path_layout, donor_path);
1050 * Get the striping layout for the file at \a path.
1052 * If \a flags contains LAYOUT_GET_EXPECTED, substitute
1053 * expected inherited attribute values for unspecified attributes. See
1054 * llapi_layout_expected().
1056 * \param[in] path path for which to get the layout
1057 * \param[in] flags flags to control how layout is retrieved
1059 * \retval valid llapi_layout pointer on success
1060 * \retval NULL if an error occurs
1062 struct llapi_layout *llapi_layout_get_by_path(const char *path, uint32_t flags)
1064 struct llapi_layout *layout = NULL;
1068 if (flags & LAYOUT_GET_EXPECTED)
1069 return llapi_layout_expected(path);
1071 fd = open(path, O_RDONLY);
1075 layout = llapi_layout_get_by_fd(fd, flags);
1084 * Get the layout for the file with FID \a fidstr in filesystem \a lustre_dir.
1086 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
1087 * \param[in] fid Lustre identifier of file to get layout for
1089 * \retval valid llapi_layout pointer on success
1090 * \retval NULL if an error occurs
1092 struct llapi_layout *llapi_layout_get_by_fid(const char *lustre_dir,
1093 const struct lu_fid *fid,
1098 int saved_msg_level = llapi_msg_get_level();
1099 struct llapi_layout *layout = NULL;
1101 /* Prevent llapi internal routines from writing to console
1102 * while executing this function, then restore previous message
1104 llapi_msg_set_level(LLAPI_MSG_OFF);
1105 fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY);
1106 llapi_msg_set_level(saved_msg_level);
1111 layout = llapi_layout_get_by_fd(fd, flags);
1120 * Get the stripe count of \a layout.
1122 * \param[in] layout layout to get stripe count from
1123 * \param[out] count integer to store stripe count in
1125 * \retval 0 on success
1126 * \retval -1 if arguments are invalid
1128 int llapi_layout_stripe_count_get(const struct llapi_layout *layout,
1131 struct llapi_layout_comp *comp;
1133 comp = __llapi_layout_cur_comp(layout);
1137 if (count == NULL) {
1142 *count = comp->llc_stripe_count;
1148 * The llapi_layout API functions have these extra validity checks since
1149 * they use intuitively named macros to denote special behavior, whereas
1150 * the old API uses 0 and -1.
1153 static bool llapi_layout_stripe_count_is_valid(int64_t stripe_count)
1155 return stripe_count == LLAPI_LAYOUT_DEFAULT ||
1156 stripe_count == LLAPI_LAYOUT_WIDE ||
1157 (stripe_count != 0 && stripe_count != -1 &&
1158 llapi_stripe_count_is_valid(stripe_count));
1161 static bool llapi_layout_extension_size_is_valid(uint64_t ext_size)
1163 return (ext_size != 0 &&
1164 llapi_stripe_size_is_aligned(ext_size) &&
1165 !llapi_stripe_size_is_too_big(ext_size));
1168 static bool llapi_layout_stripe_size_is_valid(uint64_t stripe_size)
1170 return stripe_size == LLAPI_LAYOUT_DEFAULT ||
1171 (stripe_size != 0 &&
1172 llapi_stripe_size_is_aligned(stripe_size) &&
1173 !llapi_stripe_size_is_too_big(stripe_size));
1176 static bool llapi_layout_stripe_index_is_valid(int64_t stripe_index)
1178 return stripe_index == LLAPI_LAYOUT_DEFAULT ||
1179 (stripe_index >= 0 &&
1180 llapi_stripe_index_is_valid(stripe_index));
1184 * Set the stripe count of \a layout.
1186 * \param[in] layout layout to set stripe count in
1187 * \param[in] count value to be set
1189 * \retval 0 on success
1190 * \retval -1 if arguments are invalid
1192 int llapi_layout_stripe_count_set(struct llapi_layout *layout,
1195 struct llapi_layout_comp *comp;
1197 comp = __llapi_layout_cur_comp(layout);
1201 if (!llapi_layout_stripe_count_is_valid(count)) {
1206 comp->llc_stripe_count = count;
1212 * Get the stripe/extension size of \a layout.
1214 * \param[in] layout layout to get stripe size from
1215 * \param[out] size integer to store stripe size in
1216 * \param[in] extension flag if extenion size is requested
1218 * \retval 0 on success
1219 * \retval -1 if arguments are invalid
1221 static int layout_stripe_size_get(const struct llapi_layout *layout,
1222 uint64_t *size, bool extension)
1224 struct llapi_layout_comp *comp;
1227 comp = __llapi_layout_cur_comp(layout);
1236 comp_ext = comp->llc_flags & LCME_FL_EXTENSION;
1237 if ((comp_ext && !extension) || (!comp_ext && extension)) {
1242 *size = comp->llc_stripe_size;
1243 if (comp->llc_flags & LCME_FL_EXTENSION)
1244 *size *= SEL_UNIT_SIZE;
1249 int llapi_layout_stripe_size_get(const struct llapi_layout *layout,
1252 return layout_stripe_size_get(layout, size, false);
1255 int llapi_layout_extension_size_get(const struct llapi_layout *layout,
1258 return layout_stripe_size_get(layout, size, true);
1262 * Set the stripe/extension size of \a layout.
1264 * \param[in] layout layout to set stripe size in
1265 * \param[in] size value to be set
1266 * \param[in] extension flag if extenion size is passed
1268 * \retval 0 on success
1269 * \retval -1 if arguments are invalid
1271 static int layout_stripe_size_set(struct llapi_layout *layout,
1272 uint64_t size, bool extension)
1274 struct llapi_layout_comp *comp;
1277 comp = __llapi_layout_cur_comp(layout);
1281 comp_ext = comp->llc_flags & LCME_FL_EXTENSION;
1282 if ((comp_ext && !extension) || (!comp_ext && extension)) {
1288 size /= SEL_UNIT_SIZE;
1290 if ((comp_ext && !llapi_layout_extension_size_is_valid(size)) ||
1291 (!comp_ext && !llapi_layout_stripe_size_is_valid(size))) {
1296 comp->llc_stripe_size = size;
1300 int llapi_layout_stripe_size_set(struct llapi_layout *layout,
1303 return layout_stripe_size_set(layout, size, false);
1306 int llapi_layout_extension_size_set(struct llapi_layout *layout,
1309 return layout_stripe_size_set(layout, size, true);
1313 * Get the RAID pattern of \a layout.
1315 * \param[in] layout layout to get pattern from
1316 * \param[out] pattern integer to store pattern in
1318 * \retval 0 on success
1319 * \retval -1 if arguments are invalid
1321 int llapi_layout_pattern_get(const struct llapi_layout *layout,
1324 struct llapi_layout_comp *comp;
1326 comp = __llapi_layout_cur_comp(layout);
1330 if (pattern == NULL) {
1335 *pattern = comp->llc_pattern;
1341 * Set the pattern of \a layout.
1343 * \param[in] layout layout to set pattern in
1344 * \param[in] pattern value to be set
1346 * \retval 0 on success
1347 * \retval -1 if arguments are invalid or RAID pattern
1350 int llapi_layout_pattern_set(struct llapi_layout *layout, uint64_t pattern)
1352 struct llapi_layout_comp *comp;
1354 comp = __llapi_layout_cur_comp(layout);
1358 if (pattern != LLAPI_LAYOUT_DEFAULT &&
1359 pattern != LLAPI_LAYOUT_RAID0 && pattern != LLAPI_LAYOUT_MDT
1360 && pattern != LLAPI_LAYOUT_OVERSTRIPING) {
1365 comp->llc_pattern = pattern |
1366 (comp->llc_pattern & LLAPI_LAYOUT_SPECIFIC);
1371 static inline int stripe_number_roundup(int stripe_number)
1373 unsigned int round_up = (stripe_number + 8) & ~7;
1374 return round_up > LOV_MAX_STRIPE_COUNT ?
1375 LOV_MAX_STRIPE_COUNT : round_up;
1379 * Set the OST index of stripe number \a stripe_number to \a ost_index.
1381 * If only the starting stripe's OST index is specified, then this can use
1382 * the normal LOV_MAGIC_{V1,V3} layout type. If multiple OST indices are
1383 * given, then allocate an array to hold the list of indices and ensure that
1384 * the LOV_USER_MAGIC_SPECIFIC layout is used when creating the file.
1386 * \param[in] layout layout to set OST index in
1387 * \param[in] stripe_number stripe number to set index for
1388 * \param[in] ost_index the index to set
1390 * \retval 0 on success
1391 * \retval -1 if arguments are invalid or an unsupported stripe number
1392 * was specified, error returned in errno
1394 int llapi_layout_ost_index_set(struct llapi_layout *layout, int stripe_number,
1397 struct llapi_layout_comp *comp;
1399 comp = __llapi_layout_cur_comp(layout);
1403 if (!llapi_layout_stripe_index_is_valid(ost_index)) {
1408 if (stripe_number == 0 && ost_index == LLAPI_LAYOUT_DEFAULT) {
1409 comp->llc_stripe_offset = ost_index;
1410 comp->llc_pattern &= ~LLAPI_LAYOUT_SPECIFIC;
1411 __llapi_comp_objects_realloc(comp, 0);
1412 } else if (stripe_number >= 0 &&
1413 stripe_number < LOV_MAX_STRIPE_COUNT) {
1414 if (ost_index >= LLAPI_LAYOUT_IDX_MAX) {
1419 /* Preallocate a few more stripes to avoid realloc() overhead.*/
1420 if (__llapi_comp_objects_realloc(comp,
1421 stripe_number_roundup(stripe_number)) < 0)
1424 comp->llc_objects[stripe_number].l_ost_idx = ost_index;
1426 if (stripe_number == 0)
1427 comp->llc_stripe_offset = ost_index;
1429 comp->llc_pattern |= LLAPI_LAYOUT_SPECIFIC;
1431 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT ||
1432 comp->llc_stripe_count <= stripe_number)
1433 comp->llc_stripe_count = stripe_number + 1;
1443 * Get the OST index associated with stripe \a stripe_number.
1445 * Stripes are indexed starting from zero.
1447 * \param[in] layout layout to get index from
1448 * \param[in] stripe_number stripe number to get index for
1449 * \param[out] index integer to store index in
1451 * \retval 0 on success
1452 * \retval -1 if arguments are invalid
1454 int llapi_layout_ost_index_get(const struct llapi_layout *layout,
1455 uint64_t stripe_number, uint64_t *index)
1457 struct llapi_layout_comp *comp;
1459 comp = __llapi_layout_cur_comp(layout);
1463 if (index == NULL) {
1468 if (stripe_number >= comp->llc_stripe_count ||
1469 stripe_number >= comp->llc_objects_count) {
1474 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
1475 *index = LLAPI_LAYOUT_DEFAULT;
1477 *index = comp->llc_objects[stripe_number].l_ost_idx;
1484 * Get the pool name of layout \a layout.
1486 * \param[in] layout layout to get pool name from
1487 * \param[out] dest buffer to store pool name in
1488 * \param[in] n size in bytes of buffer \a dest
1490 * \retval 0 on success
1491 * \retval -1 if arguments are invalid
1493 int llapi_layout_pool_name_get(const struct llapi_layout *layout, char *dest,
1496 struct llapi_layout_comp *comp;
1498 comp = __llapi_layout_cur_comp(layout);
1507 strncpy(dest, comp->llc_pool_name, n);
1513 * Set the name of the pool of layout \a layout.
1515 * \param[in] layout layout to set pool name in
1516 * \param[in] pool_name pool name to set
1518 * \retval 0 on success
1519 * \retval -1 if arguments are invalid or pool name is too long
1521 int llapi_layout_pool_name_set(struct llapi_layout *layout,
1522 const char *pool_name)
1524 struct llapi_layout_comp *comp;
1527 comp = __llapi_layout_cur_comp(layout);
1531 if (pool_name == NULL) {
1536 /* Strip off any 'fsname.' portion. */
1537 ptr = strchr(pool_name, '.');
1539 pool_name = ptr + 1;
1541 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
1546 strncpy(comp->llc_pool_name, pool_name, sizeof(comp->llc_pool_name));
1552 * Open and possibly create a file with a given \a layout.
1554 * If \a layout is NULL this function acts as a simple wrapper for
1555 * open(). By convention, ENOTTY is returned in errno if \a path
1556 * refers to a non-Lustre file.
1558 * \param[in] path name of the file to open
1559 * \param[in] open_flags open() flags
1560 * \param[in] mode permissions to create file, filtered by umask
1561 * \param[in] layout layout to create new file with
1563 * \retval non-negative file descriptor on successful open
1564 * \retval -1 if an error occurred
1566 int llapi_layout_file_open(const char *path, int open_flags, mode_t mode,
1567 const struct llapi_layout *layout)
1572 struct lov_user_md *lum;
1576 (layout != NULL && layout->llot_magic != LLAPI_LAYOUT_MAGIC)) {
1582 rc = llapi_layout_sanity((struct llapi_layout *)layout, false,
1583 !!(layout->llot_mirror_count > 1));
1585 llapi_layout_sanity_perror(rc);
1590 /* Object creation must be postponed until after layout attributes
1591 * have been applied. */
1592 if (layout != NULL && (open_flags & O_CREAT))
1593 open_flags |= O_LOV_DELAY_CREATE;
1595 fd = open(path, open_flags, mode);
1597 if (layout == NULL || fd < 0)
1600 lum = llapi_layout_to_lum(layout);
1609 if (lum->lmm_magic == LOV_USER_MAGIC_COMP_V1)
1610 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
1611 else if (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)
1612 lum_size = lov_user_md_size(lum->lmm_stripe_count,
1615 lum_size = lov_user_md_size(0, lum->lmm_magic);
1617 rc = fsetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_size, 0);
1626 errno = errno == EOPNOTSUPP ? ENOTTY : errno;
1632 * Create a file with a given \a layout.
1634 * Force O_CREAT and O_EXCL flags on so caller is assured that file was
1635 * created with the given \a layout on successful function return.
1637 * \param[in] path name of the file to open
1638 * \param[in] open_flags open() flags
1639 * \param[in] mode permissions to create new file with
1640 * \param[in] layout layout to create new file with
1642 * \retval non-negative file descriptor on successful open
1643 * \retval -1 if an error occurred
1645 int llapi_layout_file_create(const char *path, int open_flags, int mode,
1646 const struct llapi_layout *layout)
1648 return llapi_layout_file_open(path, open_flags|O_CREAT|O_EXCL, mode,
1652 int llapi_layout_flags_get(struct llapi_layout *layout, uint32_t *flags)
1654 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1659 *flags = layout->llot_flags;
1664 * Set flags to the header of a component layout.
1666 int llapi_layout_flags_set(struct llapi_layout *layout, uint32_t flags)
1668 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1673 layout->llot_flags = flags;
1677 const char *llapi_layout_flags_string(uint32_t flags)
1679 switch (flags & LCM_FL_FLR_MASK) {
1682 case LCM_FL_WRITE_PENDING:
1684 case LCM_FL_SYNC_PENDING:
1691 const __u16 llapi_layout_string_flags(char *string)
1693 if (strncmp(string, "ro", strlen(string)) == 0)
1694 return LCM_FL_RDONLY;
1695 if (strncmp(string, "wp", strlen(string)) == 0)
1696 return LCM_FL_WRITE_PENDING;
1697 if (strncmp(string, "sp", strlen(string)) == 0)
1698 return LCM_FL_SYNC_PENDING;
1704 * llapi_layout_mirror_count_is_valid() - Check the validity of mirror count.
1705 * @count: Mirror count value to be checked.
1707 * This function checks the validity of mirror count.
1709 * Return: true on success or false on failure.
1711 static bool llapi_layout_mirror_count_is_valid(uint16_t count)
1713 return count >= 0 && count <= LUSTRE_MIRROR_COUNT_MAX;
1717 * llapi_layout_mirror_count_get() - Get mirror count from the header of
1719 * @layout: Layout to get mirror count from.
1720 * @count: Returned mirror count value.
1722 * This function gets mirror count from the header of a layout.
1724 * Return: 0 on success or -1 on failure.
1726 int llapi_layout_mirror_count_get(struct llapi_layout *layout,
1729 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1734 *count = layout->llot_mirror_count;
1739 * llapi_layout_mirror_count_set() - Set mirror count to the header of a layout.
1740 * @layout: Layout to set mirror count in.
1741 * @count: Mirror count value to be set.
1743 * This function sets mirror count to the header of a layout.
1745 * Return: 0 on success or -1 on failure.
1747 int llapi_layout_mirror_count_set(struct llapi_layout *layout,
1750 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1755 if (!llapi_layout_mirror_count_is_valid(count)) {
1760 layout->llot_mirror_count = count;
1765 * Fetch the start and end offset of the current layout component.
1767 * \param[in] layout the layout component
1768 * \param[out] start extent start, inclusive
1769 * \param[out] end extent end, exclusive
1771 * \retval 0 on success
1772 * \retval <0 if error occurs
1774 int llapi_layout_comp_extent_get(const struct llapi_layout *layout,
1775 uint64_t *start, uint64_t *end)
1777 struct llapi_layout_comp *comp;
1779 comp = __llapi_layout_cur_comp(layout);
1783 if (start == NULL || end == NULL) {
1788 *start = comp->llc_extent.e_start;
1789 *end = comp->llc_extent.e_end;
1795 * Set the layout extent of a layout.
1797 * \param[in] layout the layout to be set
1798 * \param[in] start extent start, inclusive
1799 * \param[in] end extent end, exclusive
1801 * \retval 0 on success
1802 * \retval <0 if error occurs
1804 int llapi_layout_comp_extent_set(struct llapi_layout *layout,
1805 uint64_t start, uint64_t end)
1807 struct llapi_layout_comp *comp;
1809 comp = __llapi_layout_cur_comp(layout);
1818 comp->llc_extent.e_start = start;
1819 comp->llc_extent.e_end = end;
1820 layout->llot_is_composite = true;
1826 * Gets the attribute flags of the current component.
1828 * \param[in] layout the layout component
1829 * \param[out] flags stored the returned component flags
1831 * \retval 0 on success
1832 * \retval <0 if error occurs
1834 int llapi_layout_comp_flags_get(const struct llapi_layout *layout,
1837 struct llapi_layout_comp *comp;
1839 comp = __llapi_layout_cur_comp(layout);
1843 if (flags == NULL) {
1848 *flags = comp->llc_flags;
1854 * Sets the specified flags of the current component leaving other flags as-is.
1856 * \param[in] layout the layout component
1857 * \param[in] flags component flags to be set
1859 * \retval 0 on success
1860 * \retval <0 if error occurs
1862 int llapi_layout_comp_flags_set(struct llapi_layout *layout, uint32_t flags)
1864 struct llapi_layout_comp *comp;
1866 comp = __llapi_layout_cur_comp(layout);
1870 comp->llc_flags |= flags;
1876 * Clears the flags specified in the flags leaving other flags as-is.
1878 * \param[in] layout the layout component
1879 * \param[in] flags component flags to be cleared
1881 * \retval 0 on success
1882 * \retval <0 if error occurs
1884 int llapi_layout_comp_flags_clear(struct llapi_layout *layout,
1887 struct llapi_layout_comp *comp;
1889 comp = __llapi_layout_cur_comp(layout);
1893 comp->llc_flags &= ~flags;
1899 * Fetches the file-unique component ID of the current layout component.
1901 * \param[in] layout the layout component
1902 * \param[out] id stored the returned component ID
1904 * \retval 0 on success
1905 * \retval <0 if error occurs
1907 int llapi_layout_comp_id_get(const struct llapi_layout *layout, uint32_t *id)
1909 struct llapi_layout_comp *comp;
1911 comp = __llapi_layout_cur_comp(layout);
1925 * Return the mirror id of the current layout component.
1927 * \param[in] layout the layout component
1928 * \param[out] id stored the returned mirror ID
1930 * \retval 0 on success
1931 * \retval <0 if error occurs
1933 int llapi_layout_mirror_id_get(const struct llapi_layout *layout, uint32_t *id)
1935 struct llapi_layout_comp *comp;
1937 comp = __llapi_layout_cur_comp(layout);
1946 *id = mirror_id_of(comp->llc_id);
1952 * Adds a component to \a layout, the new component will be added to
1953 * the tail of components list and it'll inherit attributes of existing
1954 * ones. The \a layout will change it's current component pointer to
1955 * the newly added component, and it'll be turned into a composite
1956 * layout if it was not before the adding.
1958 * \param[in] layout existing composite or plain layout
1960 * \retval 0 on success
1961 * \retval <0 if error occurs
1963 int llapi_layout_comp_add(struct llapi_layout *layout)
1965 struct llapi_layout_comp *last, *comp, *new;
1966 bool composite = layout->llot_is_composite;
1968 comp = __llapi_layout_cur_comp(layout);
1972 new = __llapi_comp_alloc(0);
1976 last = list_entry(layout->llot_comp_list.prev, typeof(*last),
1979 list_add_tail(&new->llc_list, &layout->llot_comp_list);
1981 /* We must mark the layout composite for the sanity check, but it may
1982 * not stay that way if the check fails */
1983 layout->llot_is_composite = true;
1984 layout->llot_cur_comp = new;
1986 /* We need to set a temporary non-zero value for "end" when we call
1987 * comp_extent_set, so we use LUSTRE_EOF-1, which is > all allowed
1988 * for the end of the previous component. (If we're adding this
1989 * component, the end of the previous component cannot be EOF.) */
1990 if (llapi_layout_comp_extent_set(layout, last->llc_extent.e_end,
1992 llapi_layout_comp_del(layout);
1993 layout->llot_is_composite = composite;
2000 * Adds a first component of a mirror to \a layout.
2001 * The \a layout will change it's current component pointer to
2002 * the newly added component, and it'll be turned into a composite
2003 * layout if it was not before the adding.
2005 * \param[in] layout existing composite or plain layout
2007 * \retval 0 on success
2008 * \retval <0 if error occurs
2010 int llapi_layout_add_first_comp(struct llapi_layout *layout)
2012 struct llapi_layout_comp *comp, *new;
2014 comp = __llapi_layout_cur_comp(layout);
2018 new = __llapi_comp_alloc(0);
2022 new->llc_extent.e_start = 0;
2024 list_add_tail(&new->llc_list, &layout->llot_comp_list);
2025 layout->llot_cur_comp = new;
2026 layout->llot_is_composite = true;
2032 * Deletes current component from the composite layout. The component
2033 * to be deleted must be the tail of components list, and it can't be
2034 * the only component in the layout.
2036 * \param[in] layout composite layout
2038 * \retval 0 on success
2039 * \retval <0 if error occurs
2041 int llapi_layout_comp_del(struct llapi_layout *layout)
2043 struct llapi_layout_comp *comp;
2045 comp = __llapi_layout_cur_comp(layout);
2049 if (!layout->llot_is_composite) {
2054 /* It must be the tail of the list (for PFL, can be relaxed
2055 * once we get mirrored components) */
2056 if (comp->llc_list.next != &layout->llot_comp_list) {
2060 layout->llot_cur_comp =
2061 list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
2062 if (comp->llc_list.prev == &layout->llot_comp_list)
2063 layout->llot_cur_comp = NULL;
2065 list_del_init(&comp->llc_list);
2066 __llapi_comp_free(comp);
2072 * Move the current component pointer to the component with
2073 * specified component ID.
2075 * \param[in] layout composite layout
2076 * \param[in] id component ID
2078 * \retval =0 : moved successfully
2079 * \retval <0 if error occurs
2081 int llapi_layout_comp_use_id(struct llapi_layout *layout, uint32_t comp_id)
2083 struct llapi_layout_comp *comp;
2085 comp = __llapi_layout_cur_comp(layout);
2087 return -1; /* use previously set errno */
2089 if (!layout->llot_is_composite) {
2094 if (comp_id == LCME_ID_INVAL) {
2099 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
2100 if (comp->llc_id == comp_id) {
2101 layout->llot_cur_comp = comp;
2110 * Move the current component pointer to a specified position.
2112 * \param[in] layout composite layout
2113 * \param[in] pos the position to be moved, it can be:
2114 * LLAPI_LAYOUT_COMP_USE_FIRST: use first component
2115 * LLAPI_LAYOUT_COMP_USE_LAST: use last component
2116 * LLAPI_LAYOUT_COMP_USE_NEXT: use component after current
2117 * LLAPI_LAYOUT_COMP_USE_PREV: use component before current
2119 * \retval =0 : moved successfully
2120 * \retval =1 : at last component with NEXT, at first component with PREV
2121 * \retval <0 if error occurs
2123 int llapi_layout_comp_use(struct llapi_layout *layout,
2124 enum llapi_layout_comp_use pos)
2126 struct llapi_layout_comp *comp, *head, *tail;
2128 comp = __llapi_layout_cur_comp(layout);
2132 if (!layout->llot_is_composite) {
2133 if (pos == LLAPI_LAYOUT_COMP_USE_FIRST ||
2134 pos == LLAPI_LAYOUT_COMP_USE_LAST)
2140 head = list_entry(layout->llot_comp_list.next, typeof(*head), llc_list);
2141 tail = list_entry(layout->llot_comp_list.prev, typeof(*tail), llc_list);
2143 case LLAPI_LAYOUT_COMP_USE_FIRST:
2144 layout->llot_cur_comp = head;
2146 case LLAPI_LAYOUT_COMP_USE_NEXT:
2151 layout->llot_cur_comp = list_entry(comp->llc_list.next,
2152 typeof(*comp), llc_list);
2154 case LLAPI_LAYOUT_COMP_USE_LAST:
2155 layout->llot_cur_comp = tail;
2157 case LLAPI_LAYOUT_COMP_USE_PREV:
2162 layout->llot_cur_comp = list_entry(comp->llc_list.prev,
2163 typeof(*comp), llc_list);
2174 * Add layout component(s) to an existing file.
2176 * \param[in] path The path name of the file
2177 * \param[in] layout The layout component(s) to be added
2179 int llapi_layout_file_comp_add(const char *path,
2180 const struct llapi_layout *layout)
2182 int rc, fd = -1, lum_size, tmp_errno = 0;
2183 struct llapi_layout *existing_layout = NULL;
2184 struct lov_user_md *lum = NULL;
2186 if (path == NULL || layout == NULL ||
2187 layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
2192 fd = open(path, O_RDWR);
2199 existing_layout = llapi_layout_get_by_fd(fd, 0);
2200 if (existing_layout == NULL) {
2206 rc = llapi_layout_merge(&existing_layout, layout);
2213 rc = llapi_layout_sanity(existing_layout, false, false);
2216 llapi_layout_sanity_perror(rc);
2221 lum = llapi_layout_to_lum(layout);
2228 if (lum->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
2233 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
2235 rc = fsetxattr(fd, XATTR_LUSTRE_LOV".add", lum, lum_size, 0);
2245 llapi_layout_free(existing_layout);
2251 * Delete component(s) by the specified component id or component flags
2252 * from an existing file.
2254 * \param[in] path path name of the file
2255 * \param[in] id unique component ID
2256 * \param[in] flags flags: LCME_FL_* or;
2257 * negative flags: (LCME_FL_NEG|LCME_FL_*)
2259 int llapi_layout_file_comp_del(const char *path, uint32_t id, uint32_t flags)
2261 int rc = 0, fd = -1, lum_size, tmp_errno = 0;
2262 struct llapi_layout *layout;
2263 struct llapi_layout_comp *comp, *next;
2264 struct llapi_layout *existing_layout = NULL;
2265 struct lov_user_md *lum = NULL;
2267 if (path == NULL || id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2272 /* Can only specify ID or flags, not both, not none. */
2273 if ((id != LCME_ID_INVAL && flags != 0) ||
2274 (id == LCME_ID_INVAL && flags == 0)) {
2279 layout = llapi_layout_alloc();
2283 llapi_layout_comp_extent_set(layout, 0, LUSTRE_EOF);
2284 comp = __llapi_layout_cur_comp(layout);
2292 comp->llc_flags = flags;
2294 lum = llapi_layout_to_lum(layout);
2300 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
2302 fd = open(path, O_RDWR);
2309 existing_layout = llapi_layout_get_by_fd(fd, 0);
2310 if (existing_layout == NULL) {
2318 while (rc == 0 && existing_layout->llot_cur_comp != NULL) {
2319 rc = llapi_layout_comp_use(existing_layout, comp ?
2320 LLAPI_LAYOUT_COMP_USE_PREV :
2321 LLAPI_LAYOUT_COMP_USE_LAST);
2326 comp = __llapi_layout_cur_comp(existing_layout);
2332 if (id != LCME_ID_INVAL && id != comp->llc_id)
2334 else if ((flags & LCME_FL_NEG) && (flags & comp->llc_flags))
2336 else if (flags && !(flags & comp->llc_flags))
2339 rc = llapi_layout_comp_del(existing_layout);
2340 /* the layout position is moved to previous one, adjust */
2348 rc = llapi_layout_sanity(existing_layout, false, false);
2351 llapi_layout_sanity_perror(rc);
2356 rc = fsetxattr(fd, XATTR_LUSTRE_LOV".del", lum, lum_size, 0);
2367 llapi_layout_free(layout);
2368 llapi_layout_free(existing_layout);
2374 /* Internal utility function to apply flags for sanity checking */
2375 static void llapi_layout_comp_apply_flags(struct llapi_layout_comp *comp,
2378 if (flags & LCME_FL_NEG)
2379 comp->llc_flags &= ~flags;
2381 comp->llc_flags |= flags;
2384 struct llapi_layout_apply_flags_args {
2386 uint32_t *lfa_flags;
2392 static int llapi_layout_apply_flags_cb(struct llapi_layout *layout,
2395 struct llapi_layout_apply_flags_args *args = arg;
2396 struct llapi_layout_comp *comp;
2399 comp = __llapi_layout_cur_comp(layout);
2402 return LLAPI_LAYOUT_ITER_STOP;
2405 for (i = 0; i < args->lfa_count; i++) {
2406 if (comp->llc_id == args->lfa_ids[i])
2407 llapi_layout_comp_apply_flags(comp, args->lfa_flags[i]);
2410 return LLAPI_LAYOUT_ITER_CONT;
2413 /* Apply flags to the layout for sanity checking */
2414 static int llapi_layout_apply_flags(struct llapi_layout *layout, uint32_t *ids,
2415 uint32_t *flags, int count)
2417 struct llapi_layout_apply_flags_args args;
2420 if (!ids || !flags || count == 0) {
2426 args.lfa_flags = flags;
2427 args.lfa_count = count;
2430 rc = llapi_layout_comp_iterate(layout,
2431 llapi_layout_apply_flags_cb,
2433 if (errno == ENOENT)
2436 if (rc != LLAPI_LAYOUT_ITER_CONT)
2442 * Change flags by component ID of components of an existing file.
2443 * The component to be modified is specified by the comp->lcme_id value,
2444 * which must be a unique component ID.
2446 * \param[in] path path name of the file
2447 * \param[in] ids An array of component IDs
2448 * \param[in] flags flags: LCME_FL_* or;
2449 * negative flags: (LCME_FL_NEG|LCME_FL_*)
2450 * \param[in] count Number of elements in ids and flags array
2452 int llapi_layout_file_comp_set(const char *path, uint32_t *ids, uint32_t *flags,
2455 int rc = -1, fd = -1, i, tmp_errno = 0;
2457 struct llapi_layout *existing_layout = NULL;
2458 struct llapi_layout *layout = NULL;
2459 struct llapi_layout_comp *comp;
2460 struct lov_user_md *lum = NULL;
2470 for (i = 0; i < count; i++) {
2471 if (!ids[i] || !flags[i]) {
2476 if (ids[i] > LCME_ID_MAX || (flags[i] & ~LCME_KNOWN_FLAGS)) {
2481 /* do not allow to set or clear INIT flag */
2482 if (flags[i] & LCME_FL_INIT) {
2488 fd = open(path, O_RDWR);
2495 existing_layout = llapi_layout_get_by_fd(fd, 0);
2496 if (existing_layout == NULL) {
2502 if (llapi_layout_apply_flags(existing_layout, ids, flags, count)) {
2508 rc = llapi_layout_sanity(existing_layout, false, false);
2511 llapi_layout_sanity_perror(rc);
2516 layout = __llapi_layout_alloc();
2517 if (layout == NULL) {
2523 layout->llot_is_composite = true;
2524 for (i = 0; i < count; i++) {
2525 comp = __llapi_comp_alloc(0);
2532 comp->llc_id = ids[i];
2533 comp->llc_flags = flags[i];
2535 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
2536 layout->llot_cur_comp = comp;
2539 lum = llapi_layout_to_lum(layout);
2546 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
2548 /* flush cached pages from clients */
2549 rc = llapi_file_flush(fd);
2556 rc = fsetxattr(fd, XATTR_LUSTRE_LOV".set.flags", lum, lum_size, 0);
2569 llapi_layout_free(existing_layout);
2570 llapi_layout_free(layout);
2576 * Check if the file layout is composite.
2578 * \param[in] layout the file layout to check
2580 * \retval true composite
2581 * \retval false not composite
2583 bool llapi_layout_is_composite(struct llapi_layout *layout)
2585 return layout->llot_is_composite;
2589 * Iterate every components in the @layout and call callback function @cb.
2591 * \param[in] layout component layout list.
2592 * \param[in] cb callback for each component
2593 * \param[in] cbdata callback data
2595 * \retval < 0 error happens during the iteration
2596 * \retval LLAPI_LAYOUT_ITER_CONT finished the iteration w/o error
2597 * \retval LLAPI_LAYOUT_ITER_STOP got something, stop the iteration
2599 int llapi_layout_comp_iterate(struct llapi_layout *layout,
2600 llapi_layout_iter_cb cb, void *cbdata)
2604 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2609 * make sure on success llapi_layout_comp_use() API returns 0 with
2615 rc = cb(layout, cbdata);
2616 if (rc != LLAPI_LAYOUT_ITER_CONT)
2619 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2622 else if (rc == 1) /* reached the last comp */
2623 return LLAPI_LAYOUT_ITER_CONT;
2630 * llapi_layout_merge() - Merge a composite layout into another one.
2631 * @dst_layout: Destination composite layout.
2632 * @src_layout: Source composite layout.
2634 * This function copies all of the components from @src_layout and
2635 * appends them to @dst_layout.
2637 * Return: 0 on success or -1 on failure.
2639 int llapi_layout_merge(struct llapi_layout **dst_layout,
2640 const struct llapi_layout *src_layout)
2642 struct llapi_layout *new_layout = *dst_layout;
2643 struct llapi_layout_comp *new = NULL;
2644 struct llapi_layout_comp *comp = NULL;
2647 if (src_layout == NULL ||
2648 list_empty((struct list_head *)&src_layout->llot_comp_list))
2651 if (new_layout == NULL) {
2652 new_layout = __llapi_layout_alloc();
2653 if (new_layout == NULL) {
2659 list_for_each_entry(comp, &src_layout->llot_comp_list, llc_list) {
2660 new = __llapi_comp_alloc(0);
2666 new->llc_pattern = comp->llc_pattern;
2667 new->llc_stripe_size = comp->llc_stripe_size;
2668 new->llc_stripe_count = comp->llc_stripe_count;
2669 new->llc_stripe_offset = comp->llc_stripe_offset;
2671 if (comp->llc_pool_name[0] != '\0')
2672 strncpy(new->llc_pool_name, comp->llc_pool_name,
2673 sizeof(new->llc_pool_name));
2675 for (i = 0; i < comp->llc_objects_count; i++) {
2676 if (__llapi_comp_objects_realloc(new,
2677 stripe_number_roundup(i)) < 0) {
2679 __llapi_comp_free(new);
2682 new->llc_objects[i].l_ost_idx = \
2683 comp->llc_objects[i].l_ost_idx;
2686 new->llc_objects_count = comp->llc_objects_count;
2687 new->llc_extent.e_start = comp->llc_extent.e_start;
2688 new->llc_extent.e_end = comp->llc_extent.e_end;
2689 new->llc_id = comp->llc_id;
2690 new->llc_flags = comp->llc_flags;
2692 list_add_tail(&new->llc_list, &new_layout->llot_comp_list);
2693 new_layout->llot_cur_comp = new;
2695 new_layout->llot_is_composite = true;
2697 *dst_layout = new_layout;
2700 llapi_layout_free(new_layout);
2705 * Get the last initialized component
2707 * \param[in] layout component layout list.
2710 * \retval -EINVAL not found
2711 * \retval -EISDIR directory layout
2713 int llapi_layout_get_last_init_comp(struct llapi_layout *layout)
2715 struct llapi_layout_comp *comp = NULL, *head = NULL;
2717 if (!layout->llot_is_composite)
2720 head = list_entry(layout->llot_comp_list.next, typeof(*comp), llc_list);
2723 if (head->llc_id == 0 && !(head->llc_flags & LCME_FL_INIT))
2727 /* traverse the components from the tail to find the last init one */
2728 comp = list_entry(layout->llot_comp_list.prev, typeof(*comp), llc_list);
2729 while (comp != head) {
2730 if (comp->llc_flags & LCME_FL_INIT)
2732 comp = list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
2735 layout->llot_cur_comp = comp;
2737 return comp->llc_flags & LCME_FL_INIT ? 0 : -EINVAL;
2741 * Interit stripe info from the file's component to the mirror
2743 * \param[in] layout file component layout list.
2744 * \param[in] layout mirro component layout list.
2746 * \retval 0 on success
2747 * \retval -EINVAL on error
2749 int llapi_layout_mirror_inherit(struct llapi_layout *f_layout,
2750 struct llapi_layout *m_layout)
2752 struct llapi_layout_comp *m_comp = NULL;
2753 struct llapi_layout_comp *f_comp = NULL;
2756 f_comp = __llapi_layout_cur_comp(f_layout);
2759 m_comp = __llapi_layout_cur_comp(m_layout);
2763 m_comp->llc_stripe_size = f_comp->llc_stripe_size;
2764 m_comp->llc_stripe_count = f_comp->llc_stripe_count;
2770 * Find all stale components.
2772 * \param[in] layout component layout list.
2773 * \param[out] comp array of stale component info.
2774 * \param[in] comp_size array size of @comp.
2775 * \param[in] mirror_ids array of mirror id that only components
2776 * belonging to these mirror will be collected.
2777 * \param[in] ids_nr number of mirror ids array.
2779 * \retval number of component info collected on sucess or
2780 * an error code on failure.
2782 int llapi_mirror_find_stale(struct llapi_layout *layout,
2783 struct llapi_resync_comp *comp, size_t comp_size,
2784 __u16 *mirror_ids, int ids_nr)
2789 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2797 uint64_t start, end;
2799 rc = llapi_layout_comp_flags_get(layout, &flags);
2803 if (!(flags & LCME_FL_STALE))
2806 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
2810 /* the caller only wants stale components from specific
2815 for (j = 0; j < ids_nr; j++) {
2816 if (mirror_ids[j] == mirror_id)
2820 /* not in the specified mirror */
2823 } else if (flags & LCME_FL_NOSYNC) {
2824 /* if not specified mirrors, do not resync "nosync"
2829 rc = llapi_layout_comp_id_get(layout, &id);
2833 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2837 /* pack this component into @comp array */
2838 comp[idx].lrc_id = id;
2839 comp[idx].lrc_mirror_id = mirror_id;
2840 comp[idx].lrc_start = start;
2841 comp[idx].lrc_end = end;
2844 if (idx >= comp_size) {
2850 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2857 return rc < 0 ? rc : idx;
2860 /* locate @layout to a valid component covering file [file_start, file_end) */
2861 uint32_t llapi_mirror_find(struct llapi_layout *layout,
2862 uint64_t file_start, uint64_t file_end,
2865 uint32_t mirror_id = 0;
2868 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2874 uint64_t start, end;
2875 uint32_t flags, id, rid;
2877 rc = llapi_layout_comp_flags_get(layout, &flags);
2881 if (flags & LCME_FL_STALE)
2884 rc = llapi_layout_mirror_id_get(layout, &rid);
2888 rc = llapi_layout_comp_id_get(layout, &id);
2892 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2896 if (file_start >= start && file_start < end) {
2899 else if (mirror_id != rid || *endp != start)
2902 file_start = *endp = end;
2903 if (end >= file_end)
2908 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2916 int llapi_mirror_resync_many(int fd, struct llapi_layout *layout,
2917 struct llapi_resync_comp *comp_array,
2918 int comp_size, uint64_t start, uint64_t end)
2921 size_t page_size = sysconf(_SC_PAGESIZE);
2922 const size_t buflen = 4 << 20; /* 4M */
2924 uint64_t pos = start;
2929 rc = posix_memalign(&buf, page_size, buflen);
2933 if (end == OBD_OBJECT_EOF)
2934 count = OBD_OBJECT_EOF;
2936 count = end - start;
2940 uint64_t mirror_end = 0;
2941 uint64_t bytes_left;
2946 src = llapi_mirror_find(layout, pos, end, &mirror_end);
2950 if (mirror_end == OBD_OBJECT_EOF) {
2953 bytes_left = MIN(count, mirror_end - pos);
2954 bytes_left = ((bytes_left - 1) | (page_size - 1)) + 1;
2956 to_read = MIN(buflen, bytes_left);
2958 bytes_read = llapi_mirror_read(fd, src, buf, to_read, pos);
2959 if (bytes_read == 0) {
2963 if (bytes_read < 0) {
2968 /* round up to page align to make direct IO happy. */
2969 to_write = ((bytes_read - 1) | (page_size - 1)) + 1;
2971 for (i = 0; i < comp_size; i++) {
2974 size_t to_write2 = to_write;
2976 /* skip non-overlapped component */
2977 if (pos >= comp_array[i].lrc_end ||
2978 pos + to_write <= comp_array[i].lrc_start)
2981 if (pos < comp_array[i].lrc_start)
2982 pos2 = comp_array[i].lrc_start;
2984 to_write2 -= pos2 - pos;
2986 if ((pos + to_write) > comp_array[i].lrc_end)
2987 to_write2 -= pos + to_write -
2988 comp_array[i].lrc_end;
2990 written = llapi_mirror_write(fd,
2991 comp_array[i].lrc_mirror_id,
2996 * this component is not written successfully,
2997 * mark it using its lrc_synced, it is supposed
2998 * to be false before getting here.
3000 * And before this function returns, all
3001 * elements of comp_array will reverse their
3002 * lrc_synced flag to reflect their true
3005 comp_array[i].lrc_synced = true;
3006 llapi_error(LLAPI_MSG_ERROR, written,
3007 "component %u not synced",
3008 comp_array[i].lrc_id);
3013 assert(written == to_write2);
3017 count -= bytes_read;
3023 /* fatal error happens */
3024 for (i = 0; i < comp_size; i++)
3025 comp_array[i].lrc_synced = false;
3030 * no fatal error happens, each lrc_synced tells whether the component
3031 * has been resync successfully (note: we'd reverse the value to
3032 * reflect its true meaning.
3034 for (i = 0; i < comp_size; i++) {
3035 comp_array[i].lrc_synced = !comp_array[i].lrc_synced;
3036 if (comp_array[i].lrc_synced && pos & (page_size - 1)) {
3037 rc = llapi_mirror_truncate(fd,
3038 comp_array[i].lrc_mirror_id, pos);
3040 comp_array[i].lrc_synced = false;
3045 * returns the first error code for partially successful resync if
3051 enum llapi_layout_comp_sanity_error {
3053 LSE_INCOMPLETE_MIRROR,
3054 LSE_ADJACENT_EXTENSION,
3058 LSE_DOM_EXTENSION_FOLLOWING,
3061 LSE_NOT_ZERO_LENGTH_EXTENDABLE,
3062 LSE_END_NOT_GREATER,
3063 LSE_ZERO_LENGTH_NORMAL,
3064 LSE_NOT_ADJACENT_PREV,
3071 const char *llapi_layout_strerror[] =
3074 [LSE_INCOMPLETE_MIRROR] =
3075 "Incomplete mirror - must go to EOF",
3076 [LSE_ADJACENT_EXTENSION] =
3077 "No adjacent extension space components",
3078 [LSE_INIT_EXTENSION] =
3079 "Cannot apply extension flag to init components",
3082 [LSE_DOM_EXTENSION] =
3083 "DoM components can't be extension space",
3084 [LSE_DOM_EXTENSION_FOLLOWING] =
3085 "DoM components cannot be followed by extension space",
3087 "DoM component should be the first one in a file/mirror",
3088 [LSE_SET_COMP_START] =
3089 "Must set previous component extent before adding next",
3090 [LSE_NOT_ZERO_LENGTH_EXTENDABLE] =
3091 "Extendable component must start out zero-length",
3092 [LSE_END_NOT_GREATER] =
3093 "Component end is before end of previous component",
3094 [LSE_ZERO_LENGTH_NORMAL] =
3095 "Zero length components must be followed by extension",
3096 [LSE_NOT_ADJACENT_PREV] =
3097 "Components not adjacent (end != next->start",
3098 [LSE_START_GT_END] =
3099 "Component start is > end",
3101 "The component end must be aligned by the stripe size",
3103 "The extension size must be aligned by the stripe size",
3106 struct llapi_layout_sanity_args {
3107 bool lsa_incomplete;
3113 /* The component flags can be set by users at creation/modification time. */
3114 #define LCME_USER_COMP_FLAGS (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
3117 static int llapi_layout_sanity_cb(struct llapi_layout *layout,
3120 struct llapi_layout_comp *comp, *next, *prev;
3121 struct llapi_layout_sanity_args *args = arg;
3122 bool first_comp = false;
3124 comp = __llapi_layout_cur_comp(layout);
3130 if (comp->llc_list.prev != &layout->llot_comp_list)
3131 prev = list_entry(comp->llc_list.prev, typeof(*prev),
3136 if (comp->llc_list.next != &layout->llot_comp_list)
3137 next = list_entry(comp->llc_list.next, typeof(*next),
3142 /* Start of zero implies a new mirror */
3143 if (comp->llc_extent.e_start == 0) {
3145 /* Most checks apply only within one mirror, this is an
3147 if (prev && prev->llc_extent.e_end != LUSTRE_EOF) {
3148 args->lsa_rc = LSE_INCOMPLETE_MIRROR;
3155 if (next && next->llc_extent.e_start == 0)
3158 /* Flag sanity checks */
3159 /* No adjacent extension components */
3160 if ((comp->llc_flags & LCME_FL_EXTENSION) && next &&
3161 (next->llc_flags & LCME_FL_EXTENSION)) {
3162 args->lsa_rc = LSE_ADJACENT_EXTENSION;
3166 /* Extension flag cannot be applied to init components and the first
3167 * component of each mirror is automatically init */
3168 if ((comp->llc_flags & LCME_FL_EXTENSION) &&
3169 (comp->llc_flags & LCME_FL_INIT || first_comp)) {
3170 args->lsa_rc = LSE_INIT_EXTENSION;
3174 if (comp->llc_ondisk) {
3175 if (comp->llc_flags & LCME_FL_NEG)
3176 args->lsa_rc = LSE_FLAGS;
3177 } else if (!args->lsa_incomplete) {
3178 if (args->lsa_flr) {
3179 if (comp->llc_flags & ~LCME_USER_COMP_FLAGS)
3180 args->lsa_rc = LSE_FLAGS;
3182 if (comp->llc_flags &
3183 ~(LCME_FL_EXTENSION | LCME_FL_PREF_RW))
3184 args->lsa_rc = LSE_FLAGS;
3190 /* DoM sanity checks */
3191 if (comp->llc_pattern == LLAPI_LAYOUT_MDT ||
3192 comp->llc_pattern == LOV_PATTERN_MDT) {
3193 /* DoM components can't be extension components */
3194 if (comp->llc_flags & LCME_FL_EXTENSION) {
3195 args->lsa_rc = LSE_DOM_EXTENSION;
3198 /* DoM components cannot be followed by an extension comp */
3199 if (next && (next->llc_flags & LCME_FL_EXTENSION)) {
3200 args->lsa_rc = LSE_DOM_EXTENSION_FOLLOWING;
3204 /* DoM should be the first component in a mirror */
3206 args->lsa_rc = LSE_DOM_FIRST;
3212 /* Extent sanity checks */
3213 /* Must set previous component extent before adding another */
3214 if (prev && prev->llc_extent.e_start == 0 &&
3215 prev->llc_extent.e_end == 0) {
3216 args->lsa_rc = LSE_SET_COMP_START;
3220 if (!args->lsa_incomplete) {
3221 /* Components followed by extension space (extendable
3222 * components) must be zero length before initialization.
3223 * (Except for first comp, which will be initialized on
3225 if (next && (next->llc_flags & LCME_FL_EXTENSION) &&
3226 !first_comp && !(comp->llc_flags & LCME_FL_INIT) &&
3227 comp->llc_extent.e_start != comp->llc_extent.e_end) {
3228 args->lsa_rc = LSE_NOT_ZERO_LENGTH_EXTENDABLE;
3232 /* End must come after end of previous comp */
3233 if (prev && comp->llc_extent.e_end < prev->llc_extent.e_end) {
3234 args->lsa_rc = LSE_END_NOT_GREATER;
3238 /* Components not followed by ext space must have length > 0. */
3239 if (comp->llc_extent.e_start == comp->llc_extent.e_end &&
3240 (next == NULL || !(next->llc_flags & LCME_FL_EXTENSION))) {
3241 args->lsa_rc = LSE_ZERO_LENGTH_NORMAL;
3245 /* The component end must be aligned by the stripe size */
3246 if ((comp->llc_flags & LCME_FL_EXTENSION) &&
3247 (prev->llc_stripe_size != LLAPI_LAYOUT_DEFAULT)) {
3248 if (comp->llc_extent.e_end != LUSTRE_EOF &&
3249 comp->llc_extent.e_end % prev->llc_stripe_size) {
3250 args->lsa_rc = LSE_ALIGN_END;
3253 if ((comp->llc_stripe_size * SEL_UNIT_SIZE) %
3254 prev->llc_stripe_size) {
3255 args->lsa_rc = LSE_ALIGN_EXT;
3258 } else if (!(comp->llc_flags & LCME_FL_EXTENSION) &&
3259 (comp->llc_stripe_size != LLAPI_LAYOUT_DEFAULT)) {
3260 if (comp->llc_extent.e_end != LUSTRE_EOF &&
3261 comp->llc_extent.e_end % comp->llc_stripe_size) {
3262 args->lsa_rc = LSE_ALIGN_END;
3268 /* Components must have start == prev->end */
3269 if (prev && comp->llc_extent.e_start != 0 &&
3270 comp->llc_extent.e_start != prev->llc_extent.e_end) {
3271 args->lsa_rc = LSE_NOT_ADJACENT_PREV;
3275 /* Components must have start <= end */
3276 if (comp->llc_extent.e_start > comp->llc_extent.e_end) {
3277 args->lsa_rc = LSE_START_GT_END;
3281 return LLAPI_LAYOUT_ITER_CONT;
3284 errno = errno ? errno : EINVAL;
3285 return LLAPI_LAYOUT_ITER_STOP;
3288 /* Print explanation of layout error */
3289 void llapi_layout_sanity_perror(int error)
3291 if (error >= LSE_LAST || error < 0) {
3292 fprintf(stdout, "Invalid layout, unrecognized error: %d\n",
3295 fprintf(stdout, "Invalid layout: %s\n",
3296 llapi_layout_strerror[error]);
3300 /* Walk a layout and enforce sanity checks that apply to > 1 component
3302 * The core idea here is that of sanity checking individual tokens vs semantic
3304 * We cannot check everything at the individual component level ('token'),
3305 * instead we must check whether or not the full layout has a valid meaning.
3307 * An example of a component level check is "is stripe size valid?". That is
3308 * handled when setting stripe size.
3310 * An example of a layout level check is "are the extents of these components
3311 * valid when adjacent to one another", or "can we set these flags on adjacent
3314 * \param[in] layout component layout list.
3315 * \param[in] incomplete if layout is complete or not - some checks can
3316 * only be done on complete layouts.
3317 * \param[in] flr set when this is called from FLR mirror create
3319 * \retval 0, success, positive: various errors, see
3320 * llapi_layout_sanity_perror, -1, failure
3322 int llapi_layout_sanity(struct llapi_layout *layout, bool incomplete, bool flr)
3324 struct llapi_layout_sanity_args args;
3325 struct llapi_layout_comp *curr;
3331 curr = layout->llot_cur_comp;
3338 args.lsa_incomplete = incomplete;
3340 /* When we modify an existing layout, this tells us if it's FLR */
3341 if (mirror_id_of(curr->llc_id) > 0)
3342 args.lsa_flr = true;
3345 rc = llapi_layout_comp_iterate(layout,
3346 llapi_layout_sanity_cb,
3348 if (errno == ENOENT)
3351 if (rc != LLAPI_LAYOUT_ITER_CONT)
3354 layout->llot_cur_comp = curr;
3359 int llapi_layout_dom_size(struct llapi_layout *layout, uint64_t *size)
3361 uint64_t pattern, start;
3364 if (!layout || !llapi_layout_is_composite(layout)) {
3369 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3373 rc = llapi_layout_pattern_get(layout, &pattern);
3377 if (pattern != LOV_PATTERN_MDT && pattern != LLAPI_LAYOUT_MDT) {
3382 rc = llapi_layout_comp_extent_get(layout, &start, size);
3390 int lov_comp_md_size(struct lov_comp_md_v1 *lcm)
3392 if (lcm->lcm_magic == LOV_MAGIC_V1 || lcm->lcm_magic == LOV_MAGIC_V3) {
3393 struct lov_user_md *lum = (void *)lcm;
3395 return lov_user_md_size(lum->lmm_stripe_count, lum->lmm_magic);
3398 if (lcm->lcm_magic == LOV_MAGIC_FOREIGN) {
3399 struct lov_foreign_md *lfm = (void *)lcm;
3401 return lfm->lfm_length;
3404 if (lcm->lcm_magic != LOV_MAGIC_COMP_V1)
3407 return lcm->lcm_size;
3410 int llapi_get_lum_file_fd(int dir_fd, const char *fname, __u64 *valid,
3411 lstatx_t *statx, struct lov_user_md *lum,
3414 struct lov_user_mds_data *lmd;
3415 char buf[65536 + offsetof(typeof(*lmd), lmd_lmm)];
3419 if (lum && lumsize < sizeof(*lum))
3422 /* If a file name is provided, it is relative to the parent directory */
3428 lmd = (struct lov_user_mds_data *)buf;
3429 rc = get_lmd_info_fd(fname, parent_fd, dir_fd, buf, sizeof(buf),
3435 *valid = lmd->lmd_flags;
3438 memcpy(statx, &lmd->lmd_stx, sizeof(*statx));
3441 if (lmd->lmd_lmmsize > lumsize)
3443 memcpy(lum, &lmd->lmd_lmm, lmd->lmd_lmmsize);
3449 int llapi_get_lum_dir_fd(int dir_fd, __u64 *valid, lstatx_t *statx,
3450 struct lov_user_md *lum, size_t lumsize)
3452 return llapi_get_lum_file_fd(dir_fd, NULL, valid, statx, lum, lumsize);
3455 int llapi_get_lum_file(const char *path, __u64 *valid, lstatx_t *statx,
3456 struct lov_user_md *lum, size_t lumsize)
3458 char parent[PATH_MAX];
3465 tmp = strrchr(path, '/');
3467 strncpy(parent, ".", sizeof(parent) - 1);
3470 strncpy(parent, path, tmp - path);
3471 offset = tmp - path - 1;
3472 parent[tmp - path] = 0;
3477 fname += offset + 2;
3479 dir_fd = open(parent, O_RDONLY);
3482 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
3486 rc = llapi_get_lum_file_fd(dir_fd, fname, valid, statx, lum, lumsize);
3491 int llapi_get_lum_dir(const char *path, __u64 *valid, lstatx_t *statx,
3492 struct lov_user_md *lum, size_t lumsize)
3497 dir_fd = open(path, O_RDONLY);
3500 llapi_error(LLAPI_MSG_ERROR, rc, "cannot open '%s'", path);
3504 rc = llapi_get_lum_dir_fd(dir_fd, valid, statx, lum, lumsize);