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, Intel Corporation.
28 * Author: Ned Bass <bass6@llnl.gov>
37 #include <sys/xattr.h>
39 #include <libcfs/util/list.h>
40 #include <lustre/lustreapi.h>
41 #include "lustreapi_internal.h"
44 * Layout component, which contains all attributes of a plain
47 struct llapi_layout_comp {
49 uint64_t llc_stripe_size;
50 uint64_t llc_stripe_count;
51 uint64_t llc_stripe_offset;
52 /* Add 1 so user always gets back a null terminated string. */
53 char llc_pool_name[LOV_MAXPOOLNAME + 1];
54 /** Number of objects in llc_objects array if was initialized. */
55 uint32_t llc_objects_count;
56 struct lov_user_ost_data_v1 *llc_objects;
57 /* fields used only for composite layouts */
58 struct lu_extent llc_extent; /* [start, end) of component */
59 uint32_t llc_id; /* unique ID of component */
60 uint32_t llc_flags; /* LCME_FL_* flags */
61 struct list_head llc_list; /* linked to the llapi_layout
66 * An Opaque data type abstracting the layout of a Lustre file.
69 uint32_t llot_magic; /* LLAPI_LAYOUT_MAGIC */
72 bool llot_is_composite;
73 uint16_t llot_mirror_count;
74 /* Cursor pointing to one of the components in llot_comp_list */
75 struct llapi_layout_comp *llot_cur_comp;
76 struct list_head llot_comp_list;
80 * Compute the number of elements in the lmm_objects array of \a lum
81 * with size \a lum_size.
83 * \param[in] lum the struct lov_user_md to check
84 * \param[in] lum_size the number of bytes in \a lum
86 * \retval number of elements in array lum->lmm_objects
88 static int llapi_layout_objects_in_lum(struct lov_user_md *lum, size_t lum_size)
93 if (lum_size < lov_user_md_size(0, LOV_MAGIC_V1))
96 if (lum->lmm_magic == __swab32(LOV_MAGIC_V1) ||
97 lum->lmm_magic == __swab32(LOV_MAGIC_V3))
98 magic = __swab32(lum->lmm_magic);
100 magic = lum->lmm_magic;
102 base_size = lov_user_md_size(0, magic);
104 if (lum_size <= base_size)
107 return (lum_size - base_size) / sizeof(lum->lmm_objects[0]);
111 * Byte-swap the fields of struct lov_user_md.
113 * XXX Rather than duplicating swabbing code here, we should eventually
114 * refactor the needed functions in lustre/ptlrpc/pack_generic.c
115 * into a library that can be shared between kernel and user code.
118 llapi_layout_swab_lov_user_md(struct lov_user_md *lum, int lum_size)
120 int i, j, ent_count, obj_count;
121 struct lov_comp_md_v1 *comp_v1 = NULL;
122 struct lov_comp_md_entry_v1 *ent;
123 struct lov_user_ost_data *lod;
125 if (lum->lmm_magic != __swab32(LOV_MAGIC_V1) &&
126 lum->lmm_magic != __swab32(LOV_MAGIC_V3) &&
127 lum->lmm_magic != __swab32(LOV_MAGIC_COMP_V1))
130 if (lum->lmm_magic == __swab32(LOV_MAGIC_COMP_V1))
131 comp_v1 = (struct lov_comp_md_v1 *)lum;
133 if (comp_v1 != NULL) {
134 __swab32s(&comp_v1->lcm_magic);
135 __swab32s(&comp_v1->lcm_size);
136 __swab32s(&comp_v1->lcm_layout_gen);
137 __swab16s(&comp_v1->lcm_flags);
138 __swab16s(&comp_v1->lcm_entry_count);
139 ent_count = comp_v1->lcm_entry_count;
144 for (i = 0; i < ent_count; i++) {
145 if (comp_v1 != NULL) {
146 ent = &comp_v1->lcm_entries[i];
147 __swab32s(&ent->lcme_id);
148 __swab32s(&ent->lcme_flags);
149 __swab64s(&ent->lcme_extent.e_start);
150 __swab64s(&ent->lcme_extent.e_end);
151 __swab32s(&ent->lcme_offset);
152 __swab32s(&ent->lcme_size);
154 lum = (struct lov_user_md *)((char *)comp_v1 +
156 lum_size = ent->lcme_size;
158 obj_count = llapi_layout_objects_in_lum(lum, lum_size);
160 __swab32s(&lum->lmm_magic);
161 __swab32s(&lum->lmm_pattern);
162 __swab32s(&lum->lmm_stripe_size);
163 __swab16s(&lum->lmm_stripe_count);
164 __swab16s(&lum->lmm_stripe_offset);
166 if (lum->lmm_magic != LOV_MAGIC_V1) {
167 struct lov_user_md_v3 *v3;
168 v3 = (struct lov_user_md_v3 *)lum;
169 lod = v3->lmm_objects;
171 lod = lum->lmm_objects;
174 for (j = 0; j < obj_count; j++)
175 __swab32s(&lod[j].l_ost_idx);
180 * (Re-)allocate llc_objects[] to \a num_stripes stripes.
182 * Copy over existing llc_objects[], if any, to the new llc_objects[].
184 * \param[in] layout existing layout to be modified
185 * \param[in] num_stripes number of stripes in new layout
187 * \retval 0 if the objects are re-allocated successfully
188 * \retval -1 on error with errno set
190 static int __llapi_comp_objects_realloc(struct llapi_layout_comp *comp,
191 unsigned int new_stripes)
193 struct lov_user_ost_data_v1 *new_objects;
196 if (new_stripes > LOV_MAX_STRIPE_COUNT) {
201 if (new_stripes == comp->llc_objects_count)
204 if (new_stripes != 0 && new_stripes <= comp->llc_objects_count)
207 new_objects = realloc(comp->llc_objects,
208 sizeof(*new_objects) * new_stripes);
209 if (new_objects == NULL && new_stripes != 0) {
214 for (i = comp->llc_objects_count; i < new_stripes; i++)
215 new_objects[i].l_ost_idx = LLAPI_LAYOUT_IDX_MAX;
217 comp->llc_objects = new_objects;
218 comp->llc_objects_count = new_stripes;
224 * Allocate storage for a llapi_layout_comp with \a num_stripes stripes.
226 * \param[in] num_stripes number of stripes in new layout
228 * \retval valid pointer if allocation succeeds
229 * \retval NULL if allocation fails
231 static struct llapi_layout_comp *__llapi_comp_alloc(unsigned int num_stripes)
233 struct llapi_layout_comp *comp;
235 if (num_stripes > LOV_MAX_STRIPE_COUNT) {
240 comp = calloc(1, sizeof(*comp));
246 comp->llc_objects = NULL;
247 comp->llc_objects_count = 0;
249 if (__llapi_comp_objects_realloc(comp, num_stripes) < 0) {
255 comp->llc_pattern = LLAPI_LAYOUT_DEFAULT;
256 comp->llc_stripe_size = LLAPI_LAYOUT_DEFAULT;
257 comp->llc_stripe_count = LLAPI_LAYOUT_DEFAULT;
258 comp->llc_stripe_offset = LLAPI_LAYOUT_DEFAULT;
259 comp->llc_pool_name[0] = '\0';
260 comp->llc_extent.e_start = 0;
261 comp->llc_extent.e_end = LUSTRE_EOF;
264 INIT_LIST_HEAD(&comp->llc_list);
270 * Free memory allocated for \a comp
272 * \param[in] comp previously allocated by __llapi_comp_alloc()
274 static void __llapi_comp_free(struct llapi_layout_comp *comp)
276 if (comp->llc_objects != NULL)
277 free(comp->llc_objects);
282 * Free memory allocated for \a layout.
284 * \param[in] layout previously allocated by llapi_layout_alloc()
286 void llapi_layout_free(struct llapi_layout *layout)
288 struct llapi_layout_comp *comp, *n;
293 list_for_each_entry_safe(comp, n, &layout->llot_comp_list, llc_list) {
294 list_del_init(&comp->llc_list);
295 __llapi_comp_free(comp);
301 * Allocate and initialize a llapi_layout structure.
303 * \retval valid llapi_layout pointer on success
304 * \retval NULL if memory allocation fails
306 static struct llapi_layout *__llapi_layout_alloc(void)
308 struct llapi_layout *layout;
310 layout = calloc(1, sizeof(*layout));
311 if (layout == NULL) {
317 layout->llot_magic = LLAPI_LAYOUT_MAGIC;
318 layout->llot_gen = 0;
319 layout->llot_flags = 0;
320 layout->llot_is_composite = false;
321 layout->llot_mirror_count = 1;
322 layout->llot_cur_comp = NULL;
323 INIT_LIST_HEAD(&layout->llot_comp_list);
329 * Allocate and initialize a new plain layout.
331 * \retval valid llapi_layout pointer on success
332 * \retval NULL if memory allocation fails
334 struct llapi_layout *llapi_layout_alloc(void)
336 struct llapi_layout_comp *comp;
337 struct llapi_layout *layout;
339 layout = __llapi_layout_alloc();
343 comp = __llapi_comp_alloc(0);
349 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
350 layout->llot_cur_comp = comp;
356 * Convert the data from a lov_user_md to a newly allocated llapi_layout.
357 * The caller is responsible for freeing the returned pointer.
359 * \param[in] lum LOV user metadata structure to copy data from
360 * \param[in] lum_size size the the lum passed in
362 * \retval valid llapi_layout pointer on success
363 * \retval NULL if memory allocation fails
365 static struct llapi_layout *
366 llapi_layout_from_lum(const struct lov_user_md *lum, int lum_size)
368 struct lov_comp_md_v1 *comp_v1 = NULL;
369 struct lov_comp_md_entry_v1 *ent;
370 struct lov_user_md *v1;
371 struct llapi_layout *layout;
372 struct llapi_layout_comp *comp;
373 int i, ent_count = 0, obj_count;
375 layout = __llapi_layout_alloc();
379 if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
380 comp_v1 = (struct lov_comp_md_v1 *)lum;
381 ent_count = comp_v1->lcm_entry_count;
382 layout->llot_gen = comp_v1->lcm_layout_gen;
383 layout->llot_is_composite = true;
384 layout->llot_mirror_count = comp_v1->lcm_mirror_count + 1;
385 layout->llot_gen = comp_v1->lcm_layout_gen;
386 layout->llot_flags = comp_v1->lcm_flags;
387 } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
388 lum->lmm_magic == LOV_MAGIC_V3) {
390 layout->llot_is_composite = false;
393 if (ent_count == 0) {
398 v1 = (struct lov_user_md *)lum;
399 for (i = 0; i < ent_count; i++) {
400 if (comp_v1 != NULL) {
401 ent = &comp_v1->lcm_entries[i];
402 v1 = (struct lov_user_md *)((char *)comp_v1 +
404 lum_size = ent->lcme_size;
409 obj_count = llapi_layout_objects_in_lum(v1, lum_size);
410 comp = __llapi_comp_alloc(obj_count);
415 comp->llc_extent.e_start = ent->lcme_extent.e_start;
416 comp->llc_extent.e_end = ent->lcme_extent.e_end;
417 comp->llc_id = ent->lcme_id;
418 comp->llc_flags = ent->lcme_flags;
420 comp->llc_extent.e_start = 0;
421 comp->llc_extent.e_end = LUSTRE_EOF;
426 if (v1->lmm_pattern == LOV_PATTERN_RAID0)
427 comp->llc_pattern = LLAPI_LAYOUT_RAID0;
429 /* Lustre only supports RAID0 for now. */
430 comp->llc_pattern = v1->lmm_pattern;
432 if (v1->lmm_stripe_size == 0)
433 comp->llc_stripe_size = LLAPI_LAYOUT_DEFAULT;
435 comp->llc_stripe_size = v1->lmm_stripe_size;
437 if (v1->lmm_stripe_count == (typeof(v1->lmm_stripe_count))-1)
438 comp->llc_stripe_count = LLAPI_LAYOUT_WIDE;
439 else if (v1->lmm_stripe_count == 0)
440 comp->llc_stripe_count = LLAPI_LAYOUT_DEFAULT;
442 comp->llc_stripe_count = v1->lmm_stripe_count;
444 if (v1->lmm_stripe_offset ==
445 (typeof(v1->lmm_stripe_offset))-1)
446 comp->llc_stripe_offset = LLAPI_LAYOUT_DEFAULT;
448 comp->llc_stripe_offset = v1->lmm_stripe_offset;
450 if (v1->lmm_magic != LOV_USER_MAGIC_V1) {
451 const struct lov_user_md_v3 *lumv3;
452 lumv3 = (struct lov_user_md_v3 *)v1;
453 snprintf(comp->llc_pool_name,
454 sizeof(comp->llc_pool_name),
455 "%s", lumv3->lmm_pool_name);
456 memcpy(comp->llc_objects, lumv3->lmm_objects,
457 obj_count * sizeof(lumv3->lmm_objects[0]));
459 const struct lov_user_md_v1 *lumv1;
460 lumv1 = (struct lov_user_md_v1 *)v1;
461 memcpy(comp->llc_objects, lumv1->lmm_objects,
462 obj_count * sizeof(lumv1->lmm_objects[0]));
466 comp->llc_stripe_offset =
467 comp->llc_objects[0].l_ost_idx;
469 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
470 layout->llot_cur_comp = comp;
475 llapi_layout_free(layout);
480 * Convert the data from a llapi_layout to a newly allocated lov_user_md.
481 * The caller is responsible for freeing the returned pointer.
483 * \param[in] layout the layout to copy from
485 * \retval valid lov_user_md pointer on success
486 * \retval NULL if memory allocation fails or the layout is invalid
488 static struct lov_user_md *
489 llapi_layout_to_lum(const struct llapi_layout *layout)
491 struct llapi_layout_comp *comp;
492 struct lov_comp_md_v1 *comp_v1 = NULL;
493 struct lov_comp_md_entry_v1 *ent;
494 struct lov_user_md *lum = NULL;
499 if (layout == NULL ||
500 list_empty((struct list_head *)&layout->llot_comp_list)) {
505 /* Allocate header of lov_comp_md_v1 if necessary */
506 if (layout->llot_is_composite) {
509 list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
512 lum_size = sizeof(*comp_v1) + comp_cnt * sizeof(*ent);
513 lum = calloc(lum_size, 1);
518 comp_v1 = (struct lov_comp_md_v1 *)lum;
519 comp_v1->lcm_magic = LOV_USER_MAGIC_COMP_V1;
520 comp_v1->lcm_size = lum_size;
521 comp_v1->lcm_layout_gen = 0;
522 comp_v1->lcm_flags = layout->llot_flags;
523 comp_v1->lcm_entry_count = comp_cnt;
524 comp_v1->lcm_mirror_count = layout->llot_mirror_count - 1;
528 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
529 struct lov_user_md *blob;
532 int i, obj_count = 0;
533 struct lov_user_ost_data *lmm_objects;
534 uint64_t pattern = comp->llc_pattern;
536 if ((pattern & LLAPI_LAYOUT_SPECIFIC) != 0) {
537 if (comp->llc_objects_count <
538 comp->llc_stripe_count) {
542 magic = LOV_USER_MAGIC_SPECIFIC;
543 obj_count = comp->llc_stripe_count;
544 pattern &= ~LLAPI_LAYOUT_SPECIFIC;
545 } else if (strlen(comp->llc_pool_name) != 0) {
546 magic = LOV_USER_MAGIC_V3;
548 magic = LOV_USER_MAGIC_V1;
550 /* All stripes must be specified when the pattern contains
551 * LLAPI_LAYOUT_SPECIFIC */
552 for (i = 0; i < obj_count; i++) {
553 if (comp->llc_objects[i].l_ost_idx ==
554 LLAPI_LAYOUT_IDX_MAX) {
560 blob_size = lov_user_md_size(obj_count, magic);
561 blob = realloc(lum, lum_size + blob_size);
567 comp_v1 = (struct lov_comp_md_v1 *)lum;
568 blob = (struct lov_user_md *)((char *)lum + lum_size);
569 lum_size += blob_size;
572 blob->lmm_magic = magic;
573 if (pattern == LLAPI_LAYOUT_DEFAULT)
574 blob->lmm_pattern = LOV_PATTERN_RAID0;
575 else if (pattern == LLAPI_LAYOUT_MDT)
576 blob->lmm_pattern = LOV_PATTERN_MDT;
578 blob->lmm_pattern = pattern;
580 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
581 blob->lmm_stripe_size = 0;
583 blob->lmm_stripe_size = comp->llc_stripe_size;
585 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
586 blob->lmm_stripe_count = 0;
587 else if (comp->llc_stripe_count == LLAPI_LAYOUT_WIDE)
588 blob->lmm_stripe_count = LOV_ALL_STRIPES;
590 blob->lmm_stripe_count = comp->llc_stripe_count;
592 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
593 blob->lmm_stripe_offset = -1;
595 blob->lmm_stripe_offset = comp->llc_stripe_offset;
597 if (magic == LOV_USER_MAGIC_V3 ||
598 magic == LOV_USER_MAGIC_SPECIFIC) {
599 struct lov_user_md_v3 *lumv3 =
600 (struct lov_user_md_v3 *)blob;
602 if (comp->llc_pool_name[0] != '\0') {
603 strncpy(lumv3->lmm_pool_name,
605 sizeof(lumv3->lmm_pool_name));
607 memset(lumv3->lmm_pool_name, 0,
608 sizeof(lumv3->lmm_pool_name));
610 lmm_objects = lumv3->lmm_objects;
612 lmm_objects = blob->lmm_objects;
615 for (i = 0; i < obj_count; i++)
616 lmm_objects[i].l_ost_idx =
617 comp->llc_objects[i].l_ost_idx;
619 if (layout->llot_is_composite) {
620 ent = &comp_v1->lcm_entries[ent_idx];
621 ent->lcme_id = comp->llc_id;
622 ent->lcme_flags = comp->llc_flags;
623 ent->lcme_extent.e_start = comp->llc_extent.e_start;
624 ent->lcme_extent.e_end = comp->llc_extent.e_end;
625 ent->lcme_size = blob_size;
626 ent->lcme_offset = offset;
628 comp_v1->lcm_size += blob_size;
642 * Get the parent directory of a path.
644 * \param[in] path path to get parent of
645 * \param[out] buf buffer in which to store parent path
646 * \param[in] size size in bytes of buffer \a buf
648 static void get_parent_dir(const char *path, char *buf, size_t size)
652 strncpy(buf, path, size);
653 p = strrchr(buf, '/');
657 } else if (size >= 2) {
658 strncpy(buf, ".", 2);
659 buf[size - 1] = '\0';
664 * Substitute unspecified attribute values in \a layout with values
665 * from fs global settings. (lov.stripesize, lov.stripecount,
668 * \param[in] layout layout to inherit values from
669 * \param[in] path file path of the filesystem
671 static void inherit_sys_attributes(struct llapi_layout *layout,
674 struct llapi_layout_comp *comp;
675 unsigned int ssize, scount, soffset;
678 rc = sattr_cache_get_defaults(NULL, path, &scount, &ssize, &soffset);
682 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
683 if (comp->llc_pattern == LLAPI_LAYOUT_DEFAULT)
684 comp->llc_pattern = LLAPI_LAYOUT_RAID0;
685 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
686 comp->llc_stripe_size = ssize;
687 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
688 comp->llc_stripe_count = scount;
689 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
690 comp->llc_stripe_offset = soffset;
695 * Get the current component of \a layout.
697 * \param[in] layout layout to get current component
699 * \retval valid llapi_layout_comp pointer on success
700 * \retval NULL on error
702 static struct llapi_layout_comp *
703 __llapi_layout_cur_comp(const struct llapi_layout *layout)
705 struct llapi_layout_comp *comp;
707 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
711 if (layout->llot_cur_comp == NULL) {
715 /* Verify data consistency */
716 list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
717 if (comp == layout->llot_cur_comp)
724 * Test if any attributes of \a layout are specified.
726 * \param[in] layout the layout to check
728 * \retval true any attributes are specified
729 * \retval false all attributes are unspecified
731 static bool is_any_specified(const struct llapi_layout *layout)
733 struct llapi_layout_comp *comp;
735 comp = __llapi_layout_cur_comp(layout);
739 if (layout->llot_is_composite || layout->llot_mirror_count != 1)
742 return comp->llc_pattern != LLAPI_LAYOUT_DEFAULT ||
743 comp->llc_stripe_size != LLAPI_LAYOUT_DEFAULT ||
744 comp->llc_stripe_count != LLAPI_LAYOUT_DEFAULT ||
745 comp->llc_stripe_offset != LLAPI_LAYOUT_DEFAULT ||
746 strlen(comp->llc_pool_name);
750 * Check if the given \a lum_size is large enough to hold the required
753 * \param[in] lum the struct lov_user_md to check
754 * \param[in] lum_size the number of bytes in \a lum
756 * \retval true the \a lum_size is too small
757 * \retval false the \a lum_size is large enough
759 static bool llapi_layout_lum_truncated(struct lov_user_md *lum, size_t lum_size)
763 if (lum_size < sizeof(lum->lmm_magic))
766 if (lum->lmm_magic == LOV_MAGIC_V1 ||
767 lum->lmm_magic == __swab32(LOV_MAGIC_V1))
768 magic = LOV_MAGIC_V1;
769 else if (lum->lmm_magic == LOV_MAGIC_V3 ||
770 lum->lmm_magic == __swab32(LOV_MAGIC_V3))
771 magic = LOV_MAGIC_V3;
772 else if (lum->lmm_magic == LOV_MAGIC_COMP_V1 ||
773 lum->lmm_magic == __swab32(LOV_MAGIC_COMP_V1))
774 magic = LOV_MAGIC_COMP_V1;
778 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3)
779 return lum_size < lov_user_md_size(0, magic);
781 return lum_size < sizeof(struct lov_comp_md_v1);
784 /* Verify if the objects count in lum is consistent with the
785 * stripe count in lum. It applies to regular file only. */
786 static bool llapi_layout_lum_valid(struct lov_user_md *lum, int lum_size)
788 struct lov_comp_md_v1 *comp_v1 = NULL;
789 int i, ent_count, obj_count;
791 if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
792 comp_v1 = (struct lov_comp_md_v1 *)lum;
793 ent_count = comp_v1->lcm_entry_count;
794 } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
795 lum->lmm_magic == LOV_MAGIC_V3) {
801 for (i = 0; i < ent_count; i++) {
803 lum = (struct lov_user_md *)((char *)comp_v1 +
804 comp_v1->lcm_entries[i].lcme_offset);
805 lum_size = comp_v1->lcm_entries[i].lcme_size;
807 obj_count = llapi_layout_objects_in_lum(lum, lum_size);
810 if (!(comp_v1->lcm_entries[i].lcme_flags &
811 LCME_FL_INIT) && obj_count != 0)
813 } else if (obj_count != lum->lmm_stripe_count) {
821 * Get the striping layout for the file referenced by file descriptor \a fd.
823 * If the filesystem does not support the "lustre." xattr namespace, the
824 * file must be on a non-Lustre filesystem, so set errno to ENOTTY per
825 * convention. If the file has no "lustre.lov" data, the file will
826 * inherit default values, so return a default layout.
828 * If the kernel gives us back less than the expected amount of data,
829 * we fail with errno set to EINTR.
831 * \param[in] fd open file descriptor
832 * \param[in] flags open file descriptor
834 * \retval valid llapi_layout pointer on success
835 * \retval NULL if an error occurs
837 struct llapi_layout *llapi_layout_get_by_fd(int fd, uint32_t flags)
840 struct lov_user_md *lum;
841 struct llapi_layout *layout = NULL;
845 lum_len = XATTR_SIZE_MAX;
846 lum = malloc(lum_len);
850 bytes_read = fgetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_len);
851 if (bytes_read < 0) {
852 if (errno == EOPNOTSUPP)
854 else if (errno == ENODATA)
855 layout = llapi_layout_alloc();
859 /* Return an error if we got back a partial layout. */
860 if (llapi_layout_lum_truncated(lum, bytes_read)) {
865 llapi_layout_swab_lov_user_md(lum, bytes_read);
867 /* Directories may have a positive non-zero lum->lmm_stripe_count
868 * yet have an empty lum->lmm_objects array. For non-directories the
869 * amount of data returned from the kernel must be consistent
870 * with the stripe count. */
871 if (fstat(fd, &st) < 0)
874 if (!S_ISDIR(st.st_mode) && !llapi_layout_lum_valid(lum, bytes_read)) {
879 layout = llapi_layout_from_lum(lum, bytes_read);
886 * Get the expected striping layout for a file at \a path.
888 * Substitute expected inherited attribute values for unspecified
889 * attributes. Unspecified attributes may belong to directories and
890 * never-written-to files, and indicate that default values will be
891 * assigned when files are created or first written to. A default value
892 * is inherited from the parent directory if the attribute is specified
893 * there, otherwise it is inherited from the filesystem root.
894 * Unspecified attributes normally have the value LLAPI_LAYOUT_DEFAULT.
896 * The complete \a path need not refer to an existing file or directory,
897 * but some leading portion of it must reside within a lustre filesystem.
898 * A use case for this interface would be to obtain the literal striping
899 * values that would be assigned to a new file in a given directory.
901 * \param[in] path path for which to get the expected layout
903 * \retval valid llapi_layout pointer on success
904 * \retval NULL if an error occurs
906 static struct llapi_layout *llapi_layout_expected(const char *path)
908 struct llapi_layout *path_layout = NULL;
909 char donor_path[PATH_MAX];
914 fd = open(path, O_RDONLY);
915 if (fd < 0 && errno != ENOENT)
921 path_layout = llapi_layout_get_by_fd(fd, 0);
927 if (path_layout == NULL) {
928 if (errno != ENODATA && errno != ENOENT)
931 path_layout = llapi_layout_alloc();
932 if (path_layout == NULL)
936 if (is_any_specified(path_layout)) {
937 inherit_sys_attributes(path_layout, path);
941 llapi_layout_free(path_layout);
943 rc = stat(path, &st);
944 if (rc < 0 && errno != ENOENT)
947 /* If path is a not a directory or doesn't exist, inherit layout
948 * from parent directory. */
949 if ((rc == 0 && !S_ISDIR(st.st_mode)) ||
950 (rc < 0 && errno == ENOENT)) {
951 get_parent_dir(path, donor_path, sizeof(donor_path));
952 path_layout = llapi_layout_get_by_path(donor_path, 0);
953 if (path_layout != NULL) {
954 if (is_any_specified(path_layout)) {
955 inherit_sys_attributes(path_layout, donor_path);
958 llapi_layout_free(path_layout);
962 /* Inherit layout from the filesystem root. */
963 rc = llapi_search_mounts(path, 0, donor_path, NULL);
966 path_layout = llapi_layout_get_by_path(donor_path, 0);
967 if (path_layout == NULL)
970 inherit_sys_attributes(path_layout, donor_path);
975 * Get the striping layout for the file at \a path.
977 * If \a flags contains LAYOUT_GET_EXPECTED, substitute
978 * expected inherited attribute values for unspecified attributes. See
979 * llapi_layout_expected().
981 * \param[in] path path for which to get the layout
982 * \param[in] flags flags to control how layout is retrieved
984 * \retval valid llapi_layout pointer on success
985 * \retval NULL if an error occurs
987 struct llapi_layout *llapi_layout_get_by_path(const char *path, uint32_t flags)
989 struct llapi_layout *layout = NULL;
993 if (flags & LAYOUT_GET_EXPECTED)
994 return llapi_layout_expected(path);
996 fd = open(path, O_RDONLY);
1000 layout = llapi_layout_get_by_fd(fd, flags);
1009 * Get the layout for the file with FID \a fidstr in filesystem \a lustre_dir.
1011 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
1012 * \param[in] fid Lustre identifier of file to get layout for
1014 * \retval valid llapi_layout pointer on success
1015 * \retval NULL if an error occurs
1017 struct llapi_layout *llapi_layout_get_by_fid(const char *lustre_dir,
1018 const lustre_fid *fid,
1023 int saved_msg_level = llapi_msg_get_level();
1024 struct llapi_layout *layout = NULL;
1026 /* Prevent llapi internal routines from writing to console
1027 * while executing this function, then restore previous message
1029 llapi_msg_set_level(LLAPI_MSG_OFF);
1030 fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY);
1031 llapi_msg_set_level(saved_msg_level);
1036 layout = llapi_layout_get_by_fd(fd, flags);
1045 * Get the stripe count of \a layout.
1047 * \param[in] layout layout to get stripe count from
1048 * \param[out] count integer to store stripe count in
1050 * \retval 0 on success
1051 * \retval -1 if arguments are invalid
1053 int llapi_layout_stripe_count_get(const struct llapi_layout *layout,
1056 struct llapi_layout_comp *comp;
1058 comp = __llapi_layout_cur_comp(layout);
1062 if (count == NULL) {
1067 *count = comp->llc_stripe_count;
1073 * The llapi_layout API functions have these extra validity checks since
1074 * they use intuitively named macros to denote special behavior, whereas
1075 * the old API uses 0 and -1.
1078 static bool llapi_layout_stripe_count_is_valid(int64_t stripe_count)
1080 return stripe_count == LLAPI_LAYOUT_DEFAULT ||
1081 stripe_count == LLAPI_LAYOUT_WIDE ||
1082 (stripe_count != 0 && stripe_count != -1 &&
1083 llapi_stripe_count_is_valid(stripe_count));
1086 static bool llapi_layout_stripe_size_is_valid(uint64_t stripe_size)
1088 return stripe_size == LLAPI_LAYOUT_DEFAULT ||
1089 (stripe_size != 0 &&
1090 llapi_stripe_size_is_aligned(stripe_size) &&
1091 !llapi_stripe_size_is_too_big(stripe_size));
1094 static bool llapi_layout_stripe_index_is_valid(int64_t stripe_index)
1096 return stripe_index == LLAPI_LAYOUT_DEFAULT ||
1097 (stripe_index >= 0 &&
1098 llapi_stripe_index_is_valid(stripe_index));
1102 * Set the stripe count of \a layout.
1104 * \param[in] layout layout to set stripe count in
1105 * \param[in] count value to be set
1107 * \retval 0 on success
1108 * \retval -1 if arguments are invalid
1110 int llapi_layout_stripe_count_set(struct llapi_layout *layout,
1113 struct llapi_layout_comp *comp;
1115 comp = __llapi_layout_cur_comp(layout);
1119 if (!llapi_layout_stripe_count_is_valid(count)) {
1124 comp->llc_stripe_count = count;
1130 * Get the stripe size of \a layout.
1132 * \param[in] layout layout to get stripe size from
1133 * \param[out] size integer to store stripe size in
1135 * \retval 0 on success
1136 * \retval -1 if arguments are invalid
1138 int llapi_layout_stripe_size_get(const struct llapi_layout *layout,
1141 struct llapi_layout_comp *comp;
1143 comp = __llapi_layout_cur_comp(layout);
1152 *size = comp->llc_stripe_size;
1158 * Set the stripe size of \a layout.
1160 * \param[in] layout layout to set stripe size in
1161 * \param[in] size value to be set
1163 * \retval 0 on success
1164 * \retval -1 if arguments are invalid
1166 int llapi_layout_stripe_size_set(struct llapi_layout *layout,
1169 struct llapi_layout_comp *comp;
1171 comp = __llapi_layout_cur_comp(layout);
1175 if (!llapi_layout_stripe_size_is_valid(size)) {
1180 comp->llc_stripe_size = size;
1186 * Get the RAID pattern of \a layout.
1188 * \param[in] layout layout to get pattern from
1189 * \param[out] pattern integer to store pattern in
1191 * \retval 0 on success
1192 * \retval -1 if arguments are invalid
1194 int llapi_layout_pattern_get(const struct llapi_layout *layout,
1197 struct llapi_layout_comp *comp;
1199 comp = __llapi_layout_cur_comp(layout);
1203 if (pattern == NULL) {
1208 *pattern = comp->llc_pattern;
1214 * Set the pattern of \a layout.
1216 * \param[in] layout layout to set pattern in
1217 * \param[in] pattern value to be set
1219 * \retval 0 on success
1220 * \retval -1 if arguments are invalid or RAID pattern
1223 int llapi_layout_pattern_set(struct llapi_layout *layout, uint64_t pattern)
1225 struct llapi_layout_comp *comp;
1227 comp = __llapi_layout_cur_comp(layout);
1231 if (pattern != LLAPI_LAYOUT_DEFAULT &&
1232 pattern != LLAPI_LAYOUT_RAID0 && pattern != LLAPI_LAYOUT_MDT) {
1237 comp->llc_pattern = pattern |
1238 (comp->llc_pattern & LLAPI_LAYOUT_SPECIFIC);
1243 static inline int stripe_number_roundup(int stripe_number)
1245 unsigned int round_up = (stripe_number + 8) & ~7;
1246 return round_up > LOV_MAX_STRIPE_COUNT ?
1247 LOV_MAX_STRIPE_COUNT : round_up;
1251 * Set the OST index of stripe number \a stripe_number to \a ost_index.
1253 * If only the starting stripe's OST index is specified, then this can use
1254 * the normal LOV_MAGIC_{V1,V3} layout type. If multiple OST indices are
1255 * given, then allocate an array to hold the list of indices and ensure that
1256 * the LOV_USER_MAGIC_SPECIFIC layout is used when creating the file.
1258 * \param[in] layout layout to set OST index in
1259 * \param[in] stripe_number stripe number to set index for
1260 * \param[in] ost_index the index to set
1262 * \retval 0 on success
1263 * \retval -1 if arguments are invalid or an unsupported stripe number
1264 * was specified, error returned in errno
1266 int llapi_layout_ost_index_set(struct llapi_layout *layout, int stripe_number,
1269 struct llapi_layout_comp *comp;
1271 comp = __llapi_layout_cur_comp(layout);
1275 if (!llapi_layout_stripe_index_is_valid(ost_index)) {
1280 if (stripe_number == 0 && ost_index == LLAPI_LAYOUT_DEFAULT) {
1281 comp->llc_stripe_offset = ost_index;
1282 comp->llc_pattern &= ~LLAPI_LAYOUT_SPECIFIC;
1283 __llapi_comp_objects_realloc(comp, 0);
1284 } else if (stripe_number >= 0 &&
1285 stripe_number < LOV_MAX_STRIPE_COUNT) {
1286 if (ost_index >= LLAPI_LAYOUT_IDX_MAX) {
1291 /* Preallocate a few more stripes to avoid realloc() overhead.*/
1292 if (__llapi_comp_objects_realloc(comp,
1293 stripe_number_roundup(stripe_number)) < 0)
1296 comp->llc_objects[stripe_number].l_ost_idx = ost_index;
1298 if (stripe_number == 0)
1299 comp->llc_stripe_offset = ost_index;
1301 comp->llc_pattern |= LLAPI_LAYOUT_SPECIFIC;
1303 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT ||
1304 comp->llc_stripe_count <= stripe_number)
1305 comp->llc_stripe_count = stripe_number + 1;
1315 * Get the OST index associated with stripe \a stripe_number.
1317 * Stripes are indexed starting from zero.
1319 * \param[in] layout layout to get index from
1320 * \param[in] stripe_number stripe number to get index for
1321 * \param[out] index integer to store index in
1323 * \retval 0 on success
1324 * \retval -1 if arguments are invalid
1326 int llapi_layout_ost_index_get(const struct llapi_layout *layout,
1327 uint64_t stripe_number, uint64_t *index)
1329 struct llapi_layout_comp *comp;
1331 comp = __llapi_layout_cur_comp(layout);
1335 if (index == NULL) {
1340 if (stripe_number >= comp->llc_stripe_count ||
1341 stripe_number >= comp->llc_objects_count) {
1346 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
1347 *index = LLAPI_LAYOUT_DEFAULT;
1349 *index = comp->llc_objects[stripe_number].l_ost_idx;
1356 * Get the pool name of layout \a layout.
1358 * \param[in] layout layout to get pool name from
1359 * \param[out] dest buffer to store pool name in
1360 * \param[in] n size in bytes of buffer \a dest
1362 * \retval 0 on success
1363 * \retval -1 if arguments are invalid
1365 int llapi_layout_pool_name_get(const struct llapi_layout *layout, char *dest,
1368 struct llapi_layout_comp *comp;
1370 comp = __llapi_layout_cur_comp(layout);
1379 strncpy(dest, comp->llc_pool_name, n);
1385 * Set the name of the pool of layout \a layout.
1387 * \param[in] layout layout to set pool name in
1388 * \param[in] pool_name pool name to set
1390 * \retval 0 on success
1391 * \retval -1 if arguments are invalid or pool name is too long
1393 int llapi_layout_pool_name_set(struct llapi_layout *layout,
1394 const char *pool_name)
1396 struct llapi_layout_comp *comp;
1399 comp = __llapi_layout_cur_comp(layout);
1403 if (pool_name == NULL) {
1408 /* Strip off any 'fsname.' portion. */
1409 ptr = strchr(pool_name, '.');
1411 pool_name = ptr + 1;
1413 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
1418 strncpy(comp->llc_pool_name, pool_name, sizeof(comp->llc_pool_name));
1424 * Open and possibly create a file with a given \a layout.
1426 * If \a layout is NULL this function acts as a simple wrapper for
1427 * open(). By convention, ENOTTY is returned in errno if \a path
1428 * refers to a non-Lustre file.
1430 * \param[in] path name of the file to open
1431 * \param[in] open_flags open() flags
1432 * \param[in] mode permissions to create new file with
1433 * \param[in] layout layout to create new file with
1435 * \retval non-negative file descriptor on successful open
1436 * \retval -1 if an error occurred
1438 int llapi_layout_file_open(const char *path, int open_flags, mode_t mode,
1439 const struct llapi_layout *layout)
1444 struct lov_user_md *lum;
1448 (layout != NULL && layout->llot_magic != LLAPI_LAYOUT_MAGIC)) {
1453 /* Object creation must be postponed until after layout attributes
1454 * have been applied. */
1455 if (layout != NULL && (open_flags & O_CREAT))
1456 open_flags |= O_LOV_DELAY_CREATE;
1458 fd = open(path, open_flags, mode);
1460 if (layout == NULL || fd < 0)
1463 lum = llapi_layout_to_lum(layout);
1472 if (lum->lmm_magic == LOV_USER_MAGIC_COMP_V1)
1473 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
1474 else if (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)
1475 lum_size = lov_user_md_size(lum->lmm_stripe_count,
1478 lum_size = lov_user_md_size(0, lum->lmm_magic);
1480 rc = fsetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_size, 0);
1489 errno = errno == EOPNOTSUPP ? ENOTTY : errno;
1495 * Create a file with a given \a layout.
1497 * Force O_CREAT and O_EXCL flags on so caller is assured that file was
1498 * created with the given \a layout on successful function return.
1500 * \param[in] path name of the file to open
1501 * \param[in] open_flags open() flags
1502 * \param[in] mode permissions to create new file with
1503 * \param[in] layout layout to create new file with
1505 * \retval non-negative file descriptor on successful open
1506 * \retval -1 if an error occurred
1508 int llapi_layout_file_create(const char *path, int open_flags, int mode,
1509 const struct llapi_layout *layout)
1511 return llapi_layout_file_open(path, open_flags|O_CREAT|O_EXCL, mode,
1515 int llapi_layout_flags_get(struct llapi_layout *layout, uint32_t *flags)
1517 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1522 *flags = layout->llot_flags;
1527 * Set flags to the header of a component layout.
1529 int llapi_layout_flags_set(struct llapi_layout *layout, uint32_t flags)
1531 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1536 layout->llot_flags = flags;
1541 * llapi_layout_mirror_count_is_valid() - Check the validity of mirror count.
1542 * @count: Mirror count value to be checked.
1544 * This function checks the validity of mirror count.
1546 * Return: true on success or false on failure.
1548 static bool llapi_layout_mirror_count_is_valid(uint16_t count)
1550 return count >= 0 && count <= LUSTRE_MIRROR_COUNT_MAX;
1554 * llapi_layout_mirror_count_get() - Get mirror count from the header of
1556 * @layout: Layout to get mirror count from.
1557 * @count: Returned mirror count value.
1559 * This function gets mirror count from the header of a layout.
1561 * Return: 0 on success or -1 on failure.
1563 int llapi_layout_mirror_count_get(struct llapi_layout *layout,
1566 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1571 *count = layout->llot_mirror_count;
1576 * llapi_layout_mirror_count_set() - Set mirror count to the header of a layout.
1577 * @layout: Layout to set mirror count in.
1578 * @count: Mirror count value to be set.
1580 * This function sets mirror count to the header of a layout.
1582 * Return: 0 on success or -1 on failure.
1584 int llapi_layout_mirror_count_set(struct llapi_layout *layout,
1587 if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1592 if (!llapi_layout_mirror_count_is_valid(count)) {
1597 layout->llot_mirror_count = count;
1602 * Fetch the start and end offset of the current layout component.
1604 * \param[in] layout the layout component
1605 * \param[out] start extent start, inclusive
1606 * \param[out] end extent end, exclusive
1608 * \retval 0 on success
1609 * \retval <0 if error occurs
1611 int llapi_layout_comp_extent_get(const struct llapi_layout *layout,
1612 uint64_t *start, uint64_t *end)
1614 struct llapi_layout_comp *comp;
1616 comp = __llapi_layout_cur_comp(layout);
1620 if (start == NULL || end == NULL) {
1625 *start = comp->llc_extent.e_start;
1626 *end = comp->llc_extent.e_end;
1632 * Set the layout extent of a layout.
1634 * \param[in] layout the layout to be set
1635 * \param[in] start extent start, inclusive
1636 * \param[in] end extent end, exclusive
1638 * \retval 0 on success
1639 * \retval <0 if error occurs
1641 int llapi_layout_comp_extent_set(struct llapi_layout *layout,
1642 uint64_t start, uint64_t end)
1644 struct llapi_layout_comp *prev, *next, *comp;
1646 comp = __llapi_layout_cur_comp(layout);
1656 * We need to make sure the extent to be set is valid: the new
1657 * extent must be adjacent with the prev & next component.
1659 if (comp->llc_list.prev != &layout->llot_comp_list) {
1660 prev = list_entry(comp->llc_list.prev, typeof(*prev),
1662 if (start != prev->llc_extent.e_end) {
1668 if (comp->llc_list.next != &layout->llot_comp_list) {
1669 next = list_entry(comp->llc_list.next, typeof(*next),
1671 if (end != next->llc_extent.e_start) {
1677 comp->llc_extent.e_start = start;
1678 comp->llc_extent.e_end = end;
1679 layout->llot_is_composite = true;
1685 * Gets the attribute flags of the current component.
1687 * \param[in] layout the layout component
1688 * \param[out] flags stored the returned component flags
1690 * \retval 0 on success
1691 * \retval <0 if error occurs
1693 int llapi_layout_comp_flags_get(const struct llapi_layout *layout,
1696 struct llapi_layout_comp *comp;
1698 comp = __llapi_layout_cur_comp(layout);
1702 if (flags == NULL) {
1707 *flags = comp->llc_flags;
1713 * Sets the specified flags of the current component leaving other flags as-is.
1715 * \param[in] layout the layout component
1716 * \param[in] flags component flags to be set
1718 * \retval 0 on success
1719 * \retval <0 if error occurs
1721 int llapi_layout_comp_flags_set(struct llapi_layout *layout, uint32_t flags)
1723 struct llapi_layout_comp *comp;
1725 comp = __llapi_layout_cur_comp(layout);
1729 comp->llc_flags |= flags;
1735 * Clears the flags specified in the flags leaving other flags as-is.
1737 * \param[in] layout the layout component
1738 * \param[in] flags component flags to be cleared
1740 * \retval 0 on success
1741 * \retval <0 if error occurs
1743 int llapi_layout_comp_flags_clear(struct llapi_layout *layout,
1746 struct llapi_layout_comp *comp;
1748 comp = __llapi_layout_cur_comp(layout);
1752 comp->llc_flags &= ~flags;
1758 * Fetches the file-unique component ID of the current layout component.
1760 * \param[in] layout the layout component
1761 * \param[out] id stored the returned component ID
1763 * \retval 0 on success
1764 * \retval <0 if error occurs
1766 int llapi_layout_comp_id_get(const struct llapi_layout *layout, uint32_t *id)
1768 struct llapi_layout_comp *comp;
1770 comp = __llapi_layout_cur_comp(layout);
1784 * Return the mirror id of the current layout component.
1786 * \param[in] layout the layout component
1787 * \param[out] id stored the returned mirror ID
1789 * \retval 0 on success
1790 * \retval <0 if error occurs
1792 int llapi_layout_mirror_id_get(const struct llapi_layout *layout, uint32_t *id)
1794 struct llapi_layout_comp *comp;
1796 comp = __llapi_layout_cur_comp(layout);
1805 *id = mirror_id_of(comp->llc_id);
1811 * Adds a component to \a layout, the new component will be added to
1812 * the tail of components list and it'll inherit attributes of existing
1813 * ones. The \a layout will change it's current component pointer to
1814 * the newly added component, and it'll be turned into a composite
1815 * layout if it was not before the adding.
1817 * \param[in] layout existing composite or plain layout
1819 * \retval 0 on success
1820 * \retval <0 if error occurs
1822 int llapi_layout_comp_add(struct llapi_layout *layout)
1824 struct llapi_layout_comp *last, *comp, *new;
1826 comp = __llapi_layout_cur_comp(layout);
1830 new = __llapi_comp_alloc(0);
1834 last = list_entry(layout->llot_comp_list.prev, typeof(*last),
1837 if (new->llc_extent.e_end <= last->llc_extent.e_end) {
1838 __llapi_comp_free(new);
1842 new->llc_extent.e_start = last->llc_extent.e_end;
1844 list_add_tail(&new->llc_list, &layout->llot_comp_list);
1845 layout->llot_cur_comp = new;
1846 layout->llot_is_composite = true;
1852 * Deletes current component from the composite layout. The component
1853 * to be deleted must be the tail of components list, and it can't be
1854 * the only component in the layout.
1856 * \param[in] layout composite layout
1858 * \retval 0 on success
1859 * \retval <0 if error occurs
1861 int llapi_layout_comp_del(struct llapi_layout *layout)
1863 struct llapi_layout_comp *comp;
1865 comp = __llapi_layout_cur_comp(layout);
1869 if (!layout->llot_is_composite) {
1874 /* It must be the tail of the list (for PFL, can be relaxed
1875 * once we get mirrored components) */
1876 if (comp->llc_list.next != &layout->llot_comp_list) {
1880 /* It can't be the only one on the list */
1881 if (comp->llc_list.prev == &layout->llot_comp_list) {
1886 layout->llot_cur_comp =
1887 list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
1888 list_del_init(&comp->llc_list);
1889 __llapi_comp_free(comp);
1895 * Move the current component pointer to the component with
1896 * specified component ID.
1898 * \param[in] layout composite layout
1899 * \param[in] id component ID
1901 * \retval =0 : moved successfully
1902 * \retval <0 if error occurs
1904 int llapi_layout_comp_use_id(struct llapi_layout *layout, uint32_t comp_id)
1906 struct llapi_layout_comp *comp;
1908 comp = __llapi_layout_cur_comp(layout);
1910 return -1; /* use previously set errno */
1912 if (!layout->llot_is_composite) {
1917 if (comp_id == LCME_ID_INVAL) {
1922 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
1923 if (comp->llc_id == comp_id) {
1924 layout->llot_cur_comp = comp;
1933 * Move the current component pointer to a specified position.
1935 * \param[in] layout composite layout
1936 * \param[in] pos the position to be moved, it can be:
1937 * LLAPI_LAYOUT_COMP_USE_FIRST: use first component
1938 * LLAPI_LAYOUT_COMP_USE_LAST: use last component
1939 * LLAPI_LAYOUT_COMP_USE_NEXT: use component after current
1940 * LLAPI_LAYOUT_COMP_USE_PREV: use component before current
1942 * \retval =0 : moved successfully
1943 * \retval =1 : at last component with NEXT, at first component with PREV
1944 * \retval <0 if error occurs
1946 int llapi_layout_comp_use(struct llapi_layout *layout,
1947 enum llapi_layout_comp_use pos)
1949 struct llapi_layout_comp *comp, *head, *tail;
1951 comp = __llapi_layout_cur_comp(layout);
1955 if (!layout->llot_is_composite) {
1956 if (pos == LLAPI_LAYOUT_COMP_USE_FIRST ||
1957 pos == LLAPI_LAYOUT_COMP_USE_LAST)
1963 head = list_entry(layout->llot_comp_list.next, typeof(*head), llc_list);
1964 tail = list_entry(layout->llot_comp_list.prev, typeof(*tail), llc_list);
1966 case LLAPI_LAYOUT_COMP_USE_FIRST:
1967 layout->llot_cur_comp = head;
1969 case LLAPI_LAYOUT_COMP_USE_NEXT:
1974 layout->llot_cur_comp = list_entry(comp->llc_list.next,
1975 typeof(*comp), llc_list);
1977 case LLAPI_LAYOUT_COMP_USE_LAST:
1978 layout->llot_cur_comp = tail;
1980 case LLAPI_LAYOUT_COMP_USE_PREV:
1985 layout->llot_cur_comp = list_entry(comp->llc_list.prev,
1986 typeof(*comp), llc_list);
1997 * Add layout component(s) to an existing file.
1999 * \param[in] path The path name of the file
2000 * \param[in] layout The layout component(s) to be added
2002 int llapi_layout_file_comp_add(const char *path,
2003 const struct llapi_layout *layout)
2005 int rc, fd, lum_size, tmp_errno = 0;
2006 struct lov_user_md *lum;
2008 if (path == NULL || layout == NULL ||
2009 layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
2014 lum = llapi_layout_to_lum(layout);
2018 if (lum->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
2023 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
2025 fd = open(path, O_RDWR);
2032 rc = fsetxattr(fd, XATTR_LUSTRE_LOV".add", lum, lum_size, 0);
2047 * Delete component(s) by the specified component id or component flags
2048 * from an existing file.
2050 * \param[in] path path name of the file
2051 * \param[in] id unique component ID
2052 * \param[in] flags flags: LCME_FL_* or;
2053 * negative flags: (LCME_FL_NEG|LCME_FL_*)
2055 int llapi_layout_file_comp_del(const char *path, uint32_t id, uint32_t flags)
2057 int rc, fd, lum_size;
2058 struct llapi_layout *layout;
2059 struct llapi_layout_comp *comp;
2060 struct lov_user_md *lum;
2062 if (path == NULL || id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2067 /* Can only specify ID or flags, not both. */
2068 if (id != 0 && flags != 0) {
2073 layout = llapi_layout_alloc();
2077 llapi_layout_comp_extent_set(layout, 0, LUSTRE_EOF);
2078 comp = __llapi_layout_cur_comp(layout);
2080 llapi_layout_free(layout);
2085 comp->llc_flags = flags;
2087 lum = llapi_layout_to_lum(layout);
2089 llapi_layout_free(layout);
2092 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
2094 fd = open(path, O_RDWR);
2100 rc = fsetxattr(fd, XATTR_LUSTRE_LOV".del", lum, lum_size, 0);
2102 int tmp_errno = errno;
2111 llapi_layout_free(layout);
2116 * Change flags or other parameters of the component(s) by component ID of an
2117 * existing file. The component to be modified is specified by the
2118 * comp->lcme_id value, which must be an unique component ID. The new
2119 * attributes are passed in by @comp and @valid is used to specify which
2120 * attributes in the component are going to be changed.
2122 int llapi_layout_file_comp_set(const char *path,
2123 const struct llapi_layout *comp,
2131 * Check if the file layout is composite.
2133 * \param[in] layout the file layout to check
2135 * \retval true composite
2136 * \retval false not composite
2138 bool llapi_layout_is_composite(struct llapi_layout *layout)
2140 return layout->llot_is_composite;
2144 * llapi_layout_merge() - Merge a composite layout into another one.
2145 * @dst_layout: Destination composite layout.
2146 * @src_layout: Source composite layout.
2148 * This function copies all of the components from @src_layout and
2149 * appends them to @dst_layout.
2151 * Return: 0 on success or -1 on failure.
2153 int llapi_layout_merge(struct llapi_layout **dst_layout,
2154 const struct llapi_layout *src_layout)
2156 struct llapi_layout *new_layout = *dst_layout;
2157 struct llapi_layout_comp *new = NULL;
2158 struct llapi_layout_comp *comp = NULL;
2161 if (src_layout == NULL ||
2162 list_empty((struct list_head *)&src_layout->llot_comp_list))
2165 if (new_layout == NULL) {
2166 new_layout = __llapi_layout_alloc();
2167 if (new_layout == NULL) {
2173 list_for_each_entry(comp, &src_layout->llot_comp_list, llc_list) {
2174 new = __llapi_comp_alloc(0);
2180 new->llc_pattern = comp->llc_pattern;
2181 new->llc_stripe_size = comp->llc_stripe_size;
2182 new->llc_stripe_count = comp->llc_stripe_count;
2183 new->llc_stripe_offset = comp->llc_stripe_offset;
2185 if (comp->llc_pool_name[0] != '\0')
2186 strncpy(new->llc_pool_name, comp->llc_pool_name,
2187 sizeof(new->llc_pool_name));
2189 for (i = 0; i < comp->llc_objects_count; i++) {
2190 if (__llapi_comp_objects_realloc(new,
2191 stripe_number_roundup(i)) < 0) {
2193 __llapi_comp_free(new);
2196 new->llc_objects[i].l_ost_idx = \
2197 comp->llc_objects[i].l_ost_idx;
2200 new->llc_objects_count = comp->llc_objects_count;
2201 new->llc_extent.e_start = comp->llc_extent.e_start;
2202 new->llc_extent.e_end = comp->llc_extent.e_end;
2203 new->llc_id = comp->llc_id;
2204 new->llc_flags = comp->llc_flags;
2206 list_add_tail(&new->llc_list, &new_layout->llot_comp_list);
2207 new_layout->llot_cur_comp = new;
2209 new_layout->llot_is_composite = true;
2211 *dst_layout = new_layout;
2214 llapi_layout_free(new_layout);