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 <lustre/lustre_idl.h>
42 #include "lustreapi_internal.h"
45 * Layout component, which contains all attributes of a plain
48 struct llapi_layout_comp {
50 uint64_t llc_stripe_size;
51 uint64_t llc_stripe_count;
52 uint64_t llc_stripe_offset;
53 /* Add 1 so user always gets back a null terminated string. */
54 char llc_pool_name[LOV_MAXPOOLNAME + 1];
55 /** Number of objects in llc_objects array if was initialized. */
56 uint32_t llc_objects_count;
57 struct lov_user_ost_data_v1 *llc_objects;
58 /* fields used only for composite layouts */
59 struct lu_extent llc_extent; /* [start, end) of component */
60 uint32_t llc_id; /* unique ID of component */
61 uint32_t llc_flags; /* LCME_FL_* flags */
62 struct list_head llc_list; /* linked to the llapi_layout
67 * An Opaque data type abstracting the layout of a Lustre file.
70 uint32_t llot_magic; /* LLAPI_LAYOUT_MAGIC */
73 bool llot_is_composite;
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_cur_comp = NULL;
322 INIT_LIST_HEAD(&layout->llot_comp_list);
328 * Allocate and initialize a new plain layout.
330 * \retval valid llapi_layout pointer on success
331 * \retval NULL if memory allocation fails
333 struct llapi_layout *llapi_layout_alloc(void)
335 struct llapi_layout_comp *comp;
336 struct llapi_layout *layout;
338 layout = __llapi_layout_alloc();
342 comp = __llapi_comp_alloc(0);
348 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
349 layout->llot_cur_comp = comp;
355 * Convert the data from a lov_user_md to a newly allocated llapi_layout.
356 * The caller is responsible for freeing the returned pointer.
358 * \param[in] lum LOV user metadata structure to copy data from
359 * \param[in] lum_size size the the lum passed in
361 * \retval valid llapi_layout pointer on success
362 * \retval NULL if memory allocation fails
364 static struct llapi_layout *
365 llapi_layout_from_lum(const struct lov_user_md *lum, int lum_size)
367 struct lov_comp_md_v1 *comp_v1 = NULL;
368 struct lov_comp_md_entry_v1 *ent;
369 struct lov_user_md *v1;
370 struct llapi_layout *layout;
371 struct llapi_layout_comp *comp;
372 int i, ent_count = 0, obj_count;
374 layout = __llapi_layout_alloc();
378 if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
379 comp_v1 = (struct lov_comp_md_v1 *)lum;
380 ent_count = comp_v1->lcm_entry_count;
381 layout->llot_is_composite = true;
382 layout->llot_gen = comp_v1->lcm_layout_gen;
383 layout->llot_flags = comp_v1->lcm_flags;
384 } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
385 lum->lmm_magic == LOV_MAGIC_V3) {
387 layout->llot_is_composite = false;
390 if (ent_count == 0) {
395 v1 = (struct lov_user_md *)lum;
396 for (i = 0; i < ent_count; i++) {
397 if (comp_v1 != NULL) {
398 ent = &comp_v1->lcm_entries[i];
399 v1 = (struct lov_user_md *)((char *)comp_v1 +
401 lum_size = ent->lcme_size;
406 obj_count = llapi_layout_objects_in_lum(v1, lum_size);
407 comp = __llapi_comp_alloc(obj_count);
412 comp->llc_extent.e_start = ent->lcme_extent.e_start;
413 comp->llc_extent.e_end = ent->lcme_extent.e_end;
414 comp->llc_id = ent->lcme_id;
415 comp->llc_flags = ent->lcme_flags;
417 comp->llc_extent.e_start = 0;
418 comp->llc_extent.e_end = LUSTRE_EOF;
423 if (v1->lmm_pattern == LOV_PATTERN_RAID0)
424 comp->llc_pattern = LLAPI_LAYOUT_RAID0;
426 /* Lustre only supports RAID0 for now. */
427 comp->llc_pattern = v1->lmm_pattern;
429 if (v1->lmm_stripe_size == 0)
430 comp->llc_stripe_size = LLAPI_LAYOUT_DEFAULT;
432 comp->llc_stripe_size = v1->lmm_stripe_size;
434 if (v1->lmm_stripe_count == (typeof(v1->lmm_stripe_count))-1)
435 comp->llc_stripe_count = LLAPI_LAYOUT_WIDE;
436 else if (v1->lmm_stripe_count == 0)
437 comp->llc_stripe_count = LLAPI_LAYOUT_DEFAULT;
439 comp->llc_stripe_count = v1->lmm_stripe_count;
441 if (v1->lmm_stripe_offset ==
442 (typeof(v1->lmm_stripe_offset))-1)
443 comp->llc_stripe_offset = LLAPI_LAYOUT_DEFAULT;
445 comp->llc_stripe_offset = v1->lmm_stripe_offset;
447 if (v1->lmm_magic != LOV_USER_MAGIC_V1) {
448 const struct lov_user_md_v3 *lumv3;
449 lumv3 = (struct lov_user_md_v3 *)v1;
450 snprintf(comp->llc_pool_name,
451 sizeof(comp->llc_pool_name),
452 "%s", lumv3->lmm_pool_name);
453 memcpy(comp->llc_objects, lumv3->lmm_objects,
454 obj_count * sizeof(lumv3->lmm_objects[0]));
456 const struct lov_user_md_v1 *lumv1;
457 lumv1 = (struct lov_user_md_v1 *)v1;
458 memcpy(comp->llc_objects, lumv1->lmm_objects,
459 obj_count * sizeof(lumv1->lmm_objects[0]));
463 comp->llc_stripe_offset =
464 comp->llc_objects[0].l_ost_idx;
466 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
467 layout->llot_cur_comp = comp;
472 llapi_layout_free(layout);
477 * Convert the data from a llapi_layout to a newly allocated lov_user_md.
478 * The caller is responsible for freeing the returned pointer.
480 * \param[in] layout the layout to copy from
482 * \retval valid lov_user_md pointer on success
483 * \retval NULL if memory allocation fails or the layout is invalid
485 static struct lov_user_md *
486 llapi_layout_to_lum(const struct llapi_layout *layout)
488 struct llapi_layout_comp *comp;
489 struct lov_comp_md_v1 *comp_v1 = NULL;
490 struct lov_comp_md_entry_v1 *ent;
491 struct lov_user_md *lum = NULL;
496 if (layout == NULL ||
497 list_empty((struct list_head *)&layout->llot_comp_list)) {
502 /* Allocate header of lov_comp_md_v1 if necessary */
503 if (layout->llot_is_composite) {
506 list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
509 lum_size = sizeof(*comp_v1) + comp_cnt * sizeof(*ent);
510 lum = malloc(lum_size);
515 comp_v1 = (struct lov_comp_md_v1 *)lum;
516 comp_v1->lcm_magic = LOV_USER_MAGIC_COMP_V1;
517 comp_v1->lcm_size = lum_size;
518 comp_v1->lcm_layout_gen = 0;
519 comp_v1->lcm_flags = 0;
520 comp_v1->lcm_entry_count = comp_cnt;
524 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
525 struct lov_user_md *blob;
528 int i, obj_count = 0;
529 struct lov_user_ost_data *lmm_objects;
530 uint64_t pattern = comp->llc_pattern;
532 if ((pattern & LLAPI_LAYOUT_SPECIFIC) != 0) {
533 if (comp->llc_objects_count <
534 comp->llc_stripe_count) {
538 magic = LOV_USER_MAGIC_SPECIFIC;
539 obj_count = comp->llc_stripe_count;
540 pattern &= ~LLAPI_LAYOUT_SPECIFIC;
541 } else if (strlen(comp->llc_pool_name) != 0) {
542 magic = LOV_USER_MAGIC_V3;
544 magic = LOV_USER_MAGIC_V1;
546 /* All stripes must be specified when the pattern contains
547 * LLAPI_LAYOUT_SPECIFIC */
548 for (i = 0; i < obj_count; i++) {
549 if (comp->llc_objects[i].l_ost_idx ==
550 LLAPI_LAYOUT_IDX_MAX) {
556 blob_size = lov_user_md_size(obj_count, magic);
557 blob = realloc(lum, lum_size + blob_size);
563 blob = (struct lov_user_md *)((char *)lum + lum_size);
564 lum_size += blob_size;
567 blob->lmm_magic = magic;
568 if (pattern == LLAPI_LAYOUT_DEFAULT)
569 blob->lmm_pattern = 0;
570 else if (pattern == LLAPI_LAYOUT_RAID0)
571 blob->lmm_pattern = LOV_PATTERN_RAID0;
573 blob->lmm_pattern = pattern;
575 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
576 blob->lmm_stripe_size = 0;
578 blob->lmm_stripe_size = comp->llc_stripe_size;
580 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
581 blob->lmm_stripe_count = 0;
582 else if (comp->llc_stripe_count == LLAPI_LAYOUT_WIDE)
583 blob->lmm_stripe_count = LOV_ALL_STRIPES;
585 blob->lmm_stripe_count = comp->llc_stripe_count;
587 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
588 blob->lmm_stripe_offset = -1;
590 blob->lmm_stripe_offset = comp->llc_stripe_offset;
592 if (magic == LOV_USER_MAGIC_V3 ||
593 magic == LOV_USER_MAGIC_SPECIFIC) {
594 struct lov_user_md_v3 *lumv3 =
595 (struct lov_user_md_v3 *)blob;
597 if (comp->llc_pool_name[0] != '\0') {
598 strncpy(lumv3->lmm_pool_name,
600 sizeof(lumv3->lmm_pool_name));
602 memset(lumv3->lmm_pool_name, 0,
603 sizeof(lumv3->lmm_pool_name));
605 lmm_objects = lumv3->lmm_objects;
607 lmm_objects = blob->lmm_objects;
610 for (i = 0; i < obj_count; i++)
611 lmm_objects[i].l_ost_idx =
612 comp->llc_objects[i].l_ost_idx;
614 if (comp_v1 != NULL) {
615 ent = &comp_v1->lcm_entries[ent_idx];
616 ent->lcme_id = comp->llc_id;
617 ent->lcme_flags = comp->llc_flags;
618 ent->lcme_extent.e_start = comp->llc_extent.e_start;
619 ent->lcme_extent.e_end = comp->llc_extent.e_end;
620 ent->lcme_size = blob_size;
621 ent->lcme_offset = offset;
623 comp_v1->lcm_size += blob_size;
637 * Get the parent directory of a path.
639 * \param[in] path path to get parent of
640 * \param[out] buf buffer in which to store parent path
641 * \param[in] size size in bytes of buffer \a buf
643 static void get_parent_dir(const char *path, char *buf, size_t size)
647 strncpy(buf, path, size);
648 p = strrchr(buf, '/');
652 } else if (size >= 2) {
653 strncpy(buf, ".", 2);
654 buf[size - 1] = '\0';
659 * Substitute unspecified attribute values in \a layout with values
660 * from fs global settings. (lov.stripesize, lov.stripecount,
663 * \param[in] layout layout to inherit values from
664 * \param[in] path file path of the filesystem
666 static void inherit_sys_attributes(struct llapi_layout *layout,
669 struct llapi_layout_comp *comp;
670 unsigned int ssize, scount, soffset;
673 rc = sattr_cache_get_defaults(NULL, path, &scount, &ssize, &soffset);
677 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
678 if (comp->llc_pattern == LLAPI_LAYOUT_DEFAULT)
679 comp->llc_pattern = LLAPI_LAYOUT_RAID0;
680 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
681 comp->llc_stripe_size = ssize;
682 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
683 comp->llc_stripe_count = scount;
684 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
685 comp->llc_stripe_offset = soffset;
690 * Get the current component of \a layout.
692 * \param[in] layout layout to get current component
694 * \retval valid llapi_layout_comp pointer on success
695 * \retval NULL on error
697 static struct llapi_layout_comp *
698 __llapi_layout_cur_comp(const struct llapi_layout *layout)
700 struct llapi_layout_comp *comp;
702 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
706 if (layout->llot_cur_comp == NULL) {
710 /* Verify data consistency */
711 list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
712 if (comp == layout->llot_cur_comp)
719 * Test if any attributes of \a layout are specified.
721 * \param[in] layout the layout to check
723 * \retval true any attributes are specified
724 * \retval false all attributes are unspecified
726 static bool is_any_specified(const struct llapi_layout *layout)
728 struct llapi_layout_comp *comp;
730 comp = __llapi_layout_cur_comp(layout);
734 if (layout->llot_is_composite)
737 return comp->llc_pattern != LLAPI_LAYOUT_DEFAULT ||
738 comp->llc_stripe_size != LLAPI_LAYOUT_DEFAULT ||
739 comp->llc_stripe_count != LLAPI_LAYOUT_DEFAULT ||
740 comp->llc_stripe_offset != LLAPI_LAYOUT_DEFAULT ||
741 strlen(comp->llc_pool_name);
745 * Check if the given \a lum_size is large enough to hold the required
748 * \param[in] lum the struct lov_user_md to check
749 * \param[in] lum_size the number of bytes in \a lum
751 * \retval true the \a lum_size is too small
752 * \retval false the \a lum_size is large enough
754 static bool llapi_layout_lum_truncated(struct lov_user_md *lum, size_t lum_size)
758 if (lum_size < sizeof(lum->lmm_magic))
761 if (lum->lmm_magic == LOV_MAGIC_V1 ||
762 lum->lmm_magic == __swab32(LOV_MAGIC_V1))
763 magic = LOV_MAGIC_V1;
764 else if (lum->lmm_magic == LOV_MAGIC_V3 ||
765 lum->lmm_magic == __swab32(LOV_MAGIC_V3))
766 magic = LOV_MAGIC_V3;
767 else if (lum->lmm_magic == LOV_MAGIC_COMP_V1 ||
768 lum->lmm_magic == __swab32(LOV_MAGIC_COMP_V1))
769 magic = LOV_MAGIC_COMP_V1;
773 if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3)
774 return lum_size < lov_user_md_size(0, magic);
776 return lum_size < sizeof(struct lov_comp_md_v1);
779 /* Verify if the objects count in lum is consistent with the
780 * stripe count in lum. It applies to regular file only. */
781 static bool llapi_layout_lum_valid(struct lov_user_md *lum, int lum_size)
783 struct lov_comp_md_v1 *comp_v1 = NULL;
784 int i, ent_count, obj_count;
786 if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
787 comp_v1 = (struct lov_comp_md_v1 *)lum;
788 ent_count = comp_v1->lcm_entry_count;
789 } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
790 lum->lmm_magic == LOV_MAGIC_V3) {
796 for (i = 0; i < ent_count; i++) {
798 lum = (struct lov_user_md *)((char *)comp_v1 +
799 comp_v1->lcm_entries[i].lcme_offset);
800 lum_size = comp_v1->lcm_entries[i].lcme_size;
802 obj_count = llapi_layout_objects_in_lum(lum, lum_size);
805 if (!(comp_v1->lcm_entries[i].lcme_flags &
806 LCME_FL_INIT) && obj_count != 0)
808 } else if (obj_count != lum->lmm_stripe_count) {
816 * Get the striping layout for the file referenced by file descriptor \a fd.
818 * If the filesystem does not support the "lustre." xattr namespace, the
819 * file must be on a non-Lustre filesystem, so set errno to ENOTTY per
820 * convention. If the file has no "lustre.lov" data, the file will
821 * inherit default values, so return a default layout.
823 * If the kernel gives us back less than the expected amount of data,
824 * we fail with errno set to EINTR.
826 * \param[in] fd open file descriptor
827 * \param[in] flags open file descriptor
829 * \retval valid llapi_layout pointer on success
830 * \retval NULL if an error occurs
832 struct llapi_layout *llapi_layout_get_by_fd(int fd, uint32_t flags)
835 struct lov_user_md *lum;
836 struct llapi_layout *layout = NULL;
840 lum_len = XATTR_SIZE_MAX;
841 lum = malloc(lum_len);
845 bytes_read = fgetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_len);
846 if (bytes_read < 0) {
847 if (errno == EOPNOTSUPP)
849 else if (errno == ENODATA)
850 layout = llapi_layout_alloc();
854 /* Return an error if we got back a partial layout. */
855 if (llapi_layout_lum_truncated(lum, bytes_read)) {
860 llapi_layout_swab_lov_user_md(lum, bytes_read);
862 /* Directories may have a positive non-zero lum->lmm_stripe_count
863 * yet have an empty lum->lmm_objects array. For non-directories the
864 * amount of data returned from the kernel must be consistent
865 * with the stripe count. */
866 if (fstat(fd, &st) < 0)
869 if (!S_ISDIR(st.st_mode) && !llapi_layout_lum_valid(lum, bytes_read)) {
874 layout = llapi_layout_from_lum(lum, bytes_read);
881 * Get the expected striping layout for a file at \a path.
883 * Substitute expected inherited attribute values for unspecified
884 * attributes. Unspecified attributes may belong to directories and
885 * never-written-to files, and indicate that default values will be
886 * assigned when files are created or first written to. A default value
887 * is inherited from the parent directory if the attribute is specified
888 * there, otherwise it is inherited from the filesystem root.
889 * Unspecified attributes normally have the value LLAPI_LAYOUT_DEFAULT.
891 * The complete \a path need not refer to an existing file or directory,
892 * but some leading portion of it must reside within a lustre filesystem.
893 * A use case for this interface would be to obtain the literal striping
894 * values that would be assigned to a new file in a given directory.
896 * \param[in] path path for which to get the expected layout
898 * \retval valid llapi_layout pointer on success
899 * \retval NULL if an error occurs
901 static struct llapi_layout *llapi_layout_expected(const char *path)
903 struct llapi_layout *path_layout = NULL;
904 char donor_path[PATH_MAX];
909 fd = open(path, O_RDONLY);
910 if (fd < 0 && errno != ENOENT)
916 path_layout = llapi_layout_get_by_fd(fd, 0);
922 if (path_layout == NULL) {
923 if (errno != ENODATA && errno != ENOENT)
926 path_layout = llapi_layout_alloc();
927 if (path_layout == NULL)
931 if (is_any_specified(path_layout)) {
932 inherit_sys_attributes(path_layout, path);
936 llapi_layout_free(path_layout);
938 rc = stat(path, &st);
939 if (rc < 0 && errno != ENOENT)
942 /* If path is a not a directory or doesn't exist, inherit layout
943 * from parent directory. */
944 if ((rc == 0 && !S_ISDIR(st.st_mode)) ||
945 (rc < 0 && errno == ENOENT)) {
946 get_parent_dir(path, donor_path, sizeof(donor_path));
947 path_layout = llapi_layout_get_by_path(donor_path, 0);
948 if (path_layout != NULL) {
949 if (is_any_specified(path_layout)) {
950 inherit_sys_attributes(path_layout, donor_path);
953 llapi_layout_free(path_layout);
957 /* Inherit layout from the filesystem root. */
958 rc = llapi_search_mounts(path, 0, donor_path, NULL);
961 path_layout = llapi_layout_get_by_path(donor_path, 0);
962 if (path_layout == NULL)
965 inherit_sys_attributes(path_layout, donor_path);
970 * Get the striping layout for the file at \a path.
972 * If \a flags contains LAYOUT_GET_EXPECTED, substitute
973 * expected inherited attribute values for unspecified attributes. See
974 * llapi_layout_expected().
976 * \param[in] path path for which to get the layout
977 * \param[in] flags flags to control how layout is retrieved
979 * \retval valid llapi_layout pointer on success
980 * \retval NULL if an error occurs
982 struct llapi_layout *llapi_layout_get_by_path(const char *path, uint32_t flags)
984 struct llapi_layout *layout = NULL;
988 if (flags & LAYOUT_GET_EXPECTED)
989 return llapi_layout_expected(path);
991 fd = open(path, O_RDONLY);
995 layout = llapi_layout_get_by_fd(fd, flags);
1004 * Get the layout for the file with FID \a fidstr in filesystem \a lustre_dir.
1006 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
1007 * \param[in] fid Lustre identifier of file to get layout for
1009 * \retval valid llapi_layout pointer on success
1010 * \retval NULL if an error occurs
1012 struct llapi_layout *llapi_layout_get_by_fid(const char *lustre_dir,
1013 const lustre_fid *fid,
1018 int saved_msg_level = llapi_msg_get_level();
1019 struct llapi_layout *layout = NULL;
1021 /* Prevent llapi internal routines from writing to console
1022 * while executing this function, then restore previous message
1024 llapi_msg_set_level(LLAPI_MSG_OFF);
1025 fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY);
1026 llapi_msg_set_level(saved_msg_level);
1031 layout = llapi_layout_get_by_fd(fd, flags);
1040 * Get the stripe count of \a layout.
1042 * \param[in] layout layout to get stripe count from
1043 * \param[out] count integer to store stripe count in
1045 * \retval 0 on success
1046 * \retval -1 if arguments are invalid
1048 int llapi_layout_stripe_count_get(const struct llapi_layout *layout,
1051 struct llapi_layout_comp *comp;
1053 comp = __llapi_layout_cur_comp(layout);
1057 if (count == NULL) {
1062 *count = comp->llc_stripe_count;
1068 * The llapi_layout API functions have these extra validity checks since
1069 * they use intuitively named macros to denote special behavior, whereas
1070 * the old API uses 0 and -1.
1073 static bool llapi_layout_stripe_count_is_valid(int64_t stripe_count)
1075 return stripe_count == LLAPI_LAYOUT_DEFAULT ||
1076 stripe_count == LLAPI_LAYOUT_WIDE ||
1077 (stripe_count != 0 && stripe_count != -1 &&
1078 llapi_stripe_count_is_valid(stripe_count));
1081 static bool llapi_layout_stripe_size_is_valid(uint64_t stripe_size)
1083 return stripe_size == LLAPI_LAYOUT_DEFAULT ||
1084 (stripe_size != 0 &&
1085 llapi_stripe_size_is_aligned(stripe_size) &&
1086 !llapi_stripe_size_is_too_big(stripe_size));
1089 static bool llapi_layout_stripe_index_is_valid(int64_t stripe_index)
1091 return stripe_index == LLAPI_LAYOUT_DEFAULT ||
1092 (stripe_index >= 0 &&
1093 llapi_stripe_index_is_valid(stripe_index));
1097 * Set the stripe count of \a layout.
1099 * \param[in] layout layout to set stripe count in
1100 * \param[in] count value to be set
1102 * \retval 0 on success
1103 * \retval -1 if arguments are invalid
1105 int llapi_layout_stripe_count_set(struct llapi_layout *layout,
1108 struct llapi_layout_comp *comp;
1110 comp = __llapi_layout_cur_comp(layout);
1114 if (!llapi_layout_stripe_count_is_valid(count)) {
1119 comp->llc_stripe_count = count;
1125 * Get the stripe size of \a layout.
1127 * \param[in] layout layout to get stripe size from
1128 * \param[out] size integer to store stripe size in
1130 * \retval 0 on success
1131 * \retval -1 if arguments are invalid
1133 int llapi_layout_stripe_size_get(const struct llapi_layout *layout,
1136 struct llapi_layout_comp *comp;
1138 comp = __llapi_layout_cur_comp(layout);
1147 *size = comp->llc_stripe_size;
1153 * Set the stripe size of \a layout.
1155 * \param[in] layout layout to set stripe size in
1156 * \param[in] size value to be set
1158 * \retval 0 on success
1159 * \retval -1 if arguments are invalid
1161 int llapi_layout_stripe_size_set(struct llapi_layout *layout,
1164 struct llapi_layout_comp *comp;
1166 comp = __llapi_layout_cur_comp(layout);
1170 if (!llapi_layout_stripe_size_is_valid(size)) {
1175 comp->llc_stripe_size = size;
1181 * Get the RAID pattern of \a layout.
1183 * \param[in] layout layout to get pattern from
1184 * \param[out] pattern integer to store pattern in
1186 * \retval 0 on success
1187 * \retval -1 if arguments are invalid
1189 int llapi_layout_pattern_get(const struct llapi_layout *layout,
1192 struct llapi_layout_comp *comp;
1194 comp = __llapi_layout_cur_comp(layout);
1198 if (pattern == NULL) {
1203 *pattern = comp->llc_pattern;
1209 * Set the RAID pattern of \a layout.
1211 * \param[in] layout layout to set pattern in
1212 * \param[in] pattern value to be set
1214 * \retval 0 on success
1215 * \retval -1 if arguments are invalid or RAID pattern
1218 int llapi_layout_pattern_set(struct llapi_layout *layout, uint64_t pattern)
1220 struct llapi_layout_comp *comp;
1222 comp = __llapi_layout_cur_comp(layout);
1226 if (pattern != LLAPI_LAYOUT_DEFAULT &&
1227 pattern != LLAPI_LAYOUT_RAID0) {
1232 comp->llc_pattern = pattern |
1233 (comp->llc_pattern & LLAPI_LAYOUT_SPECIFIC);
1238 static inline int stripe_number_roundup(int stripe_number)
1240 unsigned int round_up = (stripe_number + 8) & ~7;
1241 return round_up > LOV_MAX_STRIPE_COUNT ?
1242 LOV_MAX_STRIPE_COUNT : round_up;
1246 * Set the OST index of stripe number \a stripe_number to \a ost_index.
1248 * If only the starting stripe's OST index is specified, then this can use
1249 * the normal LOV_MAGIC_{V1,V3} layout type. If multiple OST indices are
1250 * given, then allocate an array to hold the list of indices and ensure that
1251 * the LOV_USER_MAGIC_SPECIFIC layout is used when creating the file.
1253 * \param[in] layout layout to set OST index in
1254 * \param[in] stripe_number stripe number to set index for
1255 * \param[in] ost_index the index to set
1257 * \retval 0 on success
1258 * \retval -1 if arguments are invalid or an unsupported stripe number
1259 * was specified, error returned in errno
1261 int llapi_layout_ost_index_set(struct llapi_layout *layout, int stripe_number,
1264 struct llapi_layout_comp *comp;
1266 comp = __llapi_layout_cur_comp(layout);
1270 if (!llapi_layout_stripe_index_is_valid(ost_index)) {
1275 if (stripe_number == 0 && ost_index == LLAPI_LAYOUT_DEFAULT) {
1276 comp->llc_stripe_offset = ost_index;
1277 comp->llc_pattern &= ~LLAPI_LAYOUT_SPECIFIC;
1278 __llapi_comp_objects_realloc(comp, 0);
1279 } else if (stripe_number >= 0 &&
1280 stripe_number < LOV_MAX_STRIPE_COUNT) {
1281 if (ost_index >= LLAPI_LAYOUT_IDX_MAX) {
1286 /* Preallocate a few more stripes to avoid realloc() overhead.*/
1287 if (__llapi_comp_objects_realloc(comp,
1288 stripe_number_roundup(stripe_number)) < 0)
1291 comp->llc_objects[stripe_number].l_ost_idx = ost_index;
1293 if (stripe_number == 0)
1294 comp->llc_stripe_offset = ost_index;
1296 comp->llc_pattern |= LLAPI_LAYOUT_SPECIFIC;
1298 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT ||
1299 comp->llc_stripe_count <= stripe_number)
1300 comp->llc_stripe_count = stripe_number + 1;
1310 * Get the OST index associated with stripe \a stripe_number.
1312 * Stripes are indexed starting from zero.
1314 * \param[in] layout layout to get index from
1315 * \param[in] stripe_number stripe number to get index for
1316 * \param[out] index integer to store index in
1318 * \retval 0 on success
1319 * \retval -1 if arguments are invalid
1321 int llapi_layout_ost_index_get(const struct llapi_layout *layout,
1322 uint64_t stripe_number, uint64_t *index)
1324 struct llapi_layout_comp *comp;
1326 comp = __llapi_layout_cur_comp(layout);
1330 if (index == NULL) {
1335 if (stripe_number >= comp->llc_stripe_count ||
1336 stripe_number >= comp->llc_objects_count) {
1341 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
1342 *index = LLAPI_LAYOUT_DEFAULT;
1344 *index = comp->llc_objects[stripe_number].l_ost_idx;
1351 * Get the pool name of layout \a layout.
1353 * \param[in] layout layout to get pool name from
1354 * \param[out] dest buffer to store pool name in
1355 * \param[in] n size in bytes of buffer \a dest
1357 * \retval 0 on success
1358 * \retval -1 if arguments are invalid
1360 int llapi_layout_pool_name_get(const struct llapi_layout *layout, char *dest,
1363 struct llapi_layout_comp *comp;
1365 comp = __llapi_layout_cur_comp(layout);
1374 strncpy(dest, comp->llc_pool_name, n);
1380 * Set the name of the pool of layout \a layout.
1382 * \param[in] layout layout to set pool name in
1383 * \param[in] pool_name pool name to set
1385 * \retval 0 on success
1386 * \retval -1 if arguments are invalid or pool name is too long
1388 int llapi_layout_pool_name_set(struct llapi_layout *layout,
1389 const char *pool_name)
1391 struct llapi_layout_comp *comp;
1394 comp = __llapi_layout_cur_comp(layout);
1398 if (pool_name == NULL) {
1403 /* Strip off any 'fsname.' portion. */
1404 ptr = strchr(pool_name, '.');
1406 pool_name = ptr + 1;
1408 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
1413 strncpy(comp->llc_pool_name, pool_name, sizeof(comp->llc_pool_name));
1419 * Open and possibly create a file with a given \a layout.
1421 * If \a layout is NULL this function acts as a simple wrapper for
1422 * open(). By convention, ENOTTY is returned in errno if \a path
1423 * refers to a non-Lustre file.
1425 * \param[in] path name of the file to open
1426 * \param[in] open_flags open() flags
1427 * \param[in] mode permissions to create new file with
1428 * \param[in] layout layout to create new file with
1430 * \retval non-negative file descriptor on successful open
1431 * \retval -1 if an error occurred
1433 int llapi_layout_file_open(const char *path, int open_flags, mode_t mode,
1434 const struct llapi_layout *layout)
1439 struct lov_user_md *lum;
1443 (layout != NULL && layout->llot_magic != LLAPI_LAYOUT_MAGIC)) {
1448 /* Object creation must be postponed until after layout attributes
1449 * have been applied. */
1450 if (layout != NULL && (open_flags & O_CREAT))
1451 open_flags |= O_LOV_DELAY_CREATE;
1453 fd = open(path, open_flags, mode);
1455 if (layout == NULL || fd < 0)
1458 lum = llapi_layout_to_lum(layout);
1467 if (lum->lmm_magic == LOV_USER_MAGIC_COMP_V1)
1468 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
1469 else if (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)
1470 lum_size = lov_user_md_size(lum->lmm_stripe_count,
1473 lum_size = lov_user_md_size(0, lum->lmm_magic);
1475 rc = fsetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_size, 0);
1484 errno = errno == EOPNOTSUPP ? ENOTTY : errno;
1490 * Create a file with a given \a layout.
1492 * Force O_CREAT and O_EXCL flags on so caller is assured that file was
1493 * created with the given \a layout on successful function return.
1495 * \param[in] path name of the file to open
1496 * \param[in] open_flags open() flags
1497 * \param[in] mode permissions to create new file with
1498 * \param[in] layout layout to create new file with
1500 * \retval non-negative file descriptor on successful open
1501 * \retval -1 if an error occurred
1503 int llapi_layout_file_create(const char *path, int open_flags, int mode,
1504 const struct llapi_layout *layout)
1506 return llapi_layout_file_open(path, open_flags|O_CREAT|O_EXCL, mode,
1511 * Fetch the start and end offset of the current layout component.
1513 * \param[in] layout the layout component
1514 * \param[out] start extent start, inclusive
1515 * \param[out] end extent end, exclusive
1517 * \retval 0 on success
1518 * \retval <0 if error occurs
1520 int llapi_layout_comp_extent_get(const struct llapi_layout *layout,
1521 uint64_t *start, uint64_t *end)
1523 struct llapi_layout_comp *comp;
1525 comp = __llapi_layout_cur_comp(layout);
1529 if (start == NULL || end == NULL) {
1534 *start = comp->llc_extent.e_start;
1535 *end = comp->llc_extent.e_end;
1541 * Set the layout extent of a layout.
1543 * \param[in] layout the layout to be set
1544 * \param[in] start extent start, inclusive
1545 * \param[in] end extent end, exclusive
1547 * \retval 0 on success
1548 * \retval <0 if error occurs
1550 int llapi_layout_comp_extent_set(struct llapi_layout *layout,
1551 uint64_t start, uint64_t end)
1553 struct llapi_layout_comp *prev, *next, *comp;
1555 comp = __llapi_layout_cur_comp(layout);
1565 * We need to make sure the extent to be set is valid: the new
1566 * extent must be adjacent with the prev & next component.
1568 if (comp->llc_list.prev != &layout->llot_comp_list) {
1569 prev = list_entry(comp->llc_list.prev, typeof(*prev),
1571 if (start != prev->llc_extent.e_end) {
1577 if (comp->llc_list.next != &layout->llot_comp_list) {
1578 next = list_entry(comp->llc_list.next, typeof(*next),
1580 if (end != next->llc_extent.e_start) {
1586 comp->llc_extent.e_start = start;
1587 comp->llc_extent.e_end = end;
1588 layout->llot_is_composite = true;
1594 * Gets the attribute flags of the current component.
1596 * \param[in] layout the layout component
1597 * \param[out] flags stored the returned component flags
1599 * \retval 0 on success
1600 * \retval <0 if error occurs
1602 int llapi_layout_comp_flags_get(const struct llapi_layout *layout,
1605 struct llapi_layout_comp *comp;
1607 comp = __llapi_layout_cur_comp(layout);
1611 if (flags == NULL) {
1616 *flags = comp->llc_flags;
1622 * Sets the specified flags of the current component leaving other flags as-is.
1624 * \param[in] layout the layout component
1625 * \param[in] flags component flags to be set
1627 * \retval 0 on success
1628 * \retval <0 if error occurs
1630 int llapi_layout_comp_flags_set(struct llapi_layout *layout, uint32_t flags)
1632 struct llapi_layout_comp *comp;
1634 comp = __llapi_layout_cur_comp(layout);
1638 comp->llc_flags |= flags;
1644 * Clears the flags specified in the flags leaving other flags as-is.
1646 * \param[in] layout the layout component
1647 * \param[in] flags component flags to be cleared
1649 * \retval 0 on success
1650 * \retval <0 if error occurs
1652 int llapi_layout_comp_flags_clear(struct llapi_layout *layout,
1655 struct llapi_layout_comp *comp;
1657 comp = __llapi_layout_cur_comp(layout);
1661 comp->llc_flags &= ~flags;
1667 * Fetches the file-unique component ID of the current layout component.
1669 * \param[in] layout the layout component
1670 * \param[out] id stored the returned component ID
1672 * \retval 0 on success
1673 * \retval <0 if error occurs
1675 int llapi_layout_comp_id_get(const struct llapi_layout *layout, uint32_t *id)
1677 struct llapi_layout_comp *comp;
1679 comp = __llapi_layout_cur_comp(layout);
1693 * Adds a component to \a layout, the new component will be added to
1694 * the tail of components list and it'll inherit attributes of existing
1695 * ones. The \a layout will change it's current component pointer to
1696 * the newly added component, and it'll be turned into a composite
1697 * layout if it was not before the adding.
1699 * \param[in] layout existing composite or plain layout
1701 * \retval 0 on success
1702 * \retval <0 if error occurs
1704 int llapi_layout_comp_add(struct llapi_layout *layout)
1706 struct llapi_layout_comp *last, *comp, *new;
1708 comp = __llapi_layout_cur_comp(layout);
1712 new = __llapi_comp_alloc(0);
1716 last = list_entry(layout->llot_comp_list.prev, typeof(*last),
1719 /* Inherit some attributes from existing component */
1720 new->llc_stripe_size = comp->llc_stripe_size;
1721 new->llc_stripe_count = comp->llc_stripe_count;
1722 if (comp->llc_pool_name[0] != '\0')
1723 strncpy(new->llc_pool_name, comp->llc_pool_name,
1724 sizeof(comp->llc_pool_name));
1725 if (new->llc_extent.e_end <= last->llc_extent.e_end) {
1726 __llapi_comp_free(new);
1730 new->llc_extent.e_start = last->llc_extent.e_end;
1732 list_add_tail(&new->llc_list, &layout->llot_comp_list);
1733 layout->llot_cur_comp = new;
1734 layout->llot_is_composite = true;
1740 * Deletes current component from the composite layout. The component
1741 * to be deleted must be the tail of components list, and it can't be
1742 * the only component in the layout.
1744 * \param[in] layout composite layout
1746 * \retval 0 on success
1747 * \retval <0 if error occurs
1749 int llapi_layout_comp_del(struct llapi_layout *layout)
1751 struct llapi_layout_comp *comp;
1753 comp = __llapi_layout_cur_comp(layout);
1757 if (!layout->llot_is_composite) {
1762 /* It must be the tail of the list (for PFL, can be relaxed
1763 * once we get mirrored components) */
1764 if (comp->llc_list.next != &layout->llot_comp_list) {
1768 /* It can't be the only one on the list */
1769 if (comp->llc_list.prev == &layout->llot_comp_list) {
1774 layout->llot_cur_comp =
1775 list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
1776 list_del_init(&comp->llc_list);
1777 __llapi_comp_free(comp);
1783 * Move the current component pointer to the component with
1784 * specified component ID.
1786 * \param[in] layout composite layout
1787 * \param[in] id component ID
1789 * \retval =0 : moved successfully
1790 * \retval <0 if error occurs
1792 int llapi_layout_comp_use_id(struct llapi_layout *layout, uint32_t comp_id)
1794 struct llapi_layout_comp *comp;
1796 comp = __llapi_layout_cur_comp(layout);
1798 return -1; /* use previously set errno */
1800 if (!layout->llot_is_composite) {
1805 if (comp_id == LCME_ID_INVAL) {
1810 list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
1811 if (comp->llc_id == comp_id) {
1812 layout->llot_cur_comp = comp;
1821 * Move the current component pointer to a specified position.
1823 * \param[in] layout composite layout
1824 * \param[in] pos the position to be moved, it can be:
1825 * LLAPI_LAYOUT_COMP_USE_FIRST: use first component
1826 * LLAPI_LAYOUT_COMP_USE_LAST: use last component
1827 * LLAPI_LAYOUT_COMP_USE_NEXT: use component after current
1828 * LLAPI_LAYOUT_COMP_USE_PREV: use component before current
1830 * \retval =0 : moved successfully
1831 * \retval =1 : at last component with NEXT, at first component with PREV
1832 * \retval <0 if error occurs
1834 int llapi_layout_comp_use(struct llapi_layout *layout,
1835 enum llapi_layout_comp_use pos)
1837 struct llapi_layout_comp *comp, *head, *tail;
1839 comp = __llapi_layout_cur_comp(layout);
1843 if (!layout->llot_is_composite) {
1848 head = list_entry(layout->llot_comp_list.next, typeof(*head), llc_list);
1849 tail = list_entry(layout->llot_comp_list.prev, typeof(*tail), llc_list);
1851 case LLAPI_LAYOUT_COMP_USE_FIRST:
1852 layout->llot_cur_comp = head;
1854 case LLAPI_LAYOUT_COMP_USE_NEXT:
1859 layout->llot_cur_comp = list_entry(comp->llc_list.next,
1860 typeof(*comp), llc_list);
1862 case LLAPI_LAYOUT_COMP_USE_LAST:
1863 layout->llot_cur_comp = tail;
1865 case LLAPI_LAYOUT_COMP_USE_PREV:
1870 layout->llot_cur_comp = list_entry(comp->llc_list.prev,
1871 typeof(*comp), llc_list);
1882 * Add layout component(s) to an existing file.
1884 * \param[in] path The path name of the file
1885 * \param[in] layout The layout component(s) to be added
1887 int llapi_layout_file_comp_add(const char *path,
1888 const struct llapi_layout *layout)
1890 int rc, fd, lum_size, tmp_errno = 0;
1891 struct lov_user_md *lum;
1893 if (path == NULL || layout == NULL ||
1894 layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1899 lum = llapi_layout_to_lum(layout);
1903 if (lum->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
1908 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
1910 fd = open(path, O_RDWR);
1917 rc = fsetxattr(fd, XATTR_LUSTRE_LOV".add", lum, lum_size, 0);
1932 * Delete component(s) by the specified component id or component flags
1933 * from an existing file.
1935 * \param[in] path path name of the file
1936 * \param[in] id unique component ID
1937 * \param[in] flags flags: LCME_FL_* or;
1938 * negative flags: (LCME_FL_NEG|LCME_FL_*)
1940 int llapi_layout_file_comp_del(const char *path, uint32_t id, uint32_t flags)
1942 int rc, fd, lum_size;
1943 struct llapi_layout *layout;
1944 struct llapi_layout_comp *comp;
1945 struct lov_user_md *lum;
1947 if (path == NULL || id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
1952 /* Can only specify ID or flags, not both. */
1953 if (id != 0 && flags != 0) {
1958 layout = llapi_layout_alloc();
1962 llapi_layout_comp_extent_set(layout, 0, LUSTRE_EOF);
1963 comp = __llapi_layout_cur_comp(layout);
1965 comp->llc_flags = flags;
1967 lum = llapi_layout_to_lum(layout);
1969 llapi_layout_free(layout);
1972 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
1974 fd = open(path, O_RDWR);
1980 rc = fsetxattr(fd, XATTR_LUSTRE_LOV".del", lum, lum_size, 0);
1982 int tmp_errno = errno;
1991 llapi_layout_free(layout);
1996 * Change flags or other parameters of the component(s) by component ID of an
1997 * existing file. The component to be modified is specified by the
1998 * comp->lcme_id value, which must be an unique component ID. The new
1999 * attributes are passed in by @comp and @valid is used to specify which
2000 * attributes in the component are going to be changed.
2002 int llapi_layout_file_comp_set(const char *path,
2003 const struct llapi_layout *comp,