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 * Author: Ned Bass <bass6@llnl.gov>
35 #include <sys/xattr.h>
37 #include <lustre/lustreapi.h>
38 #include <lustre/lustre_idl.h>
39 #include "lustreapi_internal.h"
42 * An Opaque data type abstracting the layout of a Lustre file.
44 * Duplicate the fields we care about from struct lov_user_md_v3.
45 * Deal with v1 versus v3 format issues only when we read or write
50 uint64_t llot_pattern;
51 uint64_t llot_stripe_size;
52 uint64_t llot_stripe_count;
53 uint64_t llot_stripe_offset;
54 /* Add 1 so user always gets back a null terminated string. */
55 char llot_pool_name[LOV_MAXPOOLNAME + 1];
56 /** Number of objects in llot_objects array if was initialized. */
57 uint32_t llot_objects_count;
58 struct lov_user_ost_data_v1 *llot_objects;
62 * Byte-swap the fields of struct lov_user_md.
64 * XXX Rather than duplicating swabbing code here, we should eventually
65 * refactor the needed functions in lustre/ptlrpc/pack_generic.c
66 * into a library that can be shared between kernel and user code.
69 llapi_layout_swab_lov_user_md(struct lov_user_md *lum, int object_count)
72 struct lov_user_md_v3 *lumv3 = (struct lov_user_md_v3 *)lum;
73 struct lov_user_ost_data *lod;
75 __swab32s(&lum->lmm_magic);
76 __swab32s(&lum->lmm_pattern);
77 __swab32s(&lum->lmm_stripe_size);
78 __swab16s(&lum->lmm_stripe_count);
79 __swab16s(&lum->lmm_stripe_offset);
81 if (lum->lmm_magic != LOV_MAGIC_V1)
82 lod = lumv3->lmm_objects;
84 lod = lum->lmm_objects;
86 for (i = 0; i < object_count; i++)
87 __swab32s(&lod[i].l_ost_idx);
91 * (Re-)allocate llot_objects[] to \a num_stripes stripes.
93 * Copy over existing llot_objects[], if any, to the new llot_objects[].
95 * \param[in] layout existing layout to be modified
96 * \param[in] num_stripes number of stripes in new layout
98 * \retval 0 if the objects are re-allocated successfully
99 * \retval -1 on error with errno set
101 static int __llapi_layout_objects_realloc(struct llapi_layout *layout,
102 unsigned int new_stripes)
104 struct lov_user_ost_data_v1 *new_objects;
107 if (new_stripes > LOV_MAX_STRIPE_COUNT) {
112 if (new_stripes == 0 && layout->llot_objects_count == 0)
115 if (new_stripes != 0 && new_stripes <= layout->llot_objects_count)
118 new_objects = realloc(layout->llot_objects,
119 sizeof(*new_objects) * new_stripes);
120 if (new_objects == NULL && new_stripes != 0) {
125 for (i = layout->llot_objects_count; i < new_stripes; i++)
126 new_objects[i].l_ost_idx = LLAPI_LAYOUT_IDX_MAX;
128 if (new_stripes == 0)
129 layout->llot_objects = NULL;
131 layout->llot_objects = new_objects;
132 layout->llot_objects_count = new_stripes;
138 * Allocate storage for a llapi_layout with \a num_stripes stripes.
140 * \param[in] num_stripes number of stripes in new layout
142 * \retval valid pointer if allocation succeeds
143 * \retval NULL if allocation fails
145 static struct llapi_layout *__llapi_layout_alloc(unsigned int num_stripes)
147 struct llapi_layout *layout;
149 if (num_stripes > LOV_MAX_STRIPE_COUNT) {
154 layout = calloc(1, sizeof(*layout));
155 if (layout == NULL) {
160 layout->llot_objects = NULL;
161 layout->llot_objects_count = 0;
163 if (__llapi_layout_objects_realloc(layout, num_stripes) < 0) {
172 * Copy the data from a lov_user_md to a newly allocated llapi_layout.
174 * The caller is responsible for freeing the returned pointer.
176 * \param[in] lum LOV user metadata structure to copy data from
178 * \retval valid llapi_layout pointer on success
179 * \retval NULL if memory allocation fails
181 static struct llapi_layout *
182 llapi_layout_from_lum(const struct lov_user_md *lum, size_t object_count)
184 struct llapi_layout *layout;
187 objects_sz = object_count * sizeof(lum->lmm_objects[0]);
189 layout = __llapi_layout_alloc(object_count);
193 layout->llot_magic = LLAPI_LAYOUT_MAGIC;
195 if (lum->lmm_pattern == LOV_PATTERN_RAID0)
196 layout->llot_pattern = LLAPI_LAYOUT_RAID0;
198 /* Lustre only supports RAID0 for now. */
199 layout->llot_pattern = lum->lmm_pattern;
201 if (lum->lmm_stripe_size == 0)
202 layout->llot_stripe_size = LLAPI_LAYOUT_DEFAULT;
204 layout->llot_stripe_size = lum->lmm_stripe_size;
206 if (lum->lmm_stripe_count == (typeof(lum->lmm_stripe_count))-1)
207 layout->llot_stripe_count = LLAPI_LAYOUT_WIDE;
208 else if (lum->lmm_stripe_count == 0)
209 layout->llot_stripe_count = LLAPI_LAYOUT_DEFAULT;
211 layout->llot_stripe_count = lum->lmm_stripe_count;
213 if (lum->lmm_stripe_offset == (typeof(lum->lmm_stripe_offset))-1)
214 layout->llot_stripe_offset = LLAPI_LAYOUT_DEFAULT;
216 layout->llot_stripe_offset = lum->lmm_stripe_offset;
218 if (lum->lmm_magic != LOV_USER_MAGIC_V1) {
219 const struct lov_user_md_v3 *lumv3;
220 lumv3 = (struct lov_user_md_v3 *)lum;
221 snprintf(layout->llot_pool_name, sizeof(layout->llot_pool_name),
222 "%s", lumv3->lmm_pool_name);
223 memcpy(layout->llot_objects, lumv3->lmm_objects, objects_sz);
225 const struct lov_user_md_v1 *lumv1;
226 lumv1 = (struct lov_user_md_v1 *)lum;
227 memcpy(layout->llot_objects, lumv1->lmm_objects, objects_sz);
230 if (object_count != 0)
231 layout->llot_stripe_offset = layout->llot_objects[0].l_ost_idx;
237 * Copy the data from a llapi_layout to a newly allocated lov_user_md.
239 * The caller is responsible for freeing the returned pointer.
241 * The current version of this API doesn't support specifying the OST
242 * index of arbitrary stripes, only stripe 0 via lmm_stripe_offset.
243 * There is therefore no need to copy the lmm_objects array.
245 * \param[in] layout the layout to copy from
247 * \retval valid lov_user_md pointer on success
248 * \retval NULL if memory allocation fails or the layout is invalid
250 static struct lov_user_md *
251 llapi_layout_to_lum(const struct llapi_layout *layout)
253 struct lov_user_md *lum;
255 uint32_t magic = LOV_USER_MAGIC_V1;
256 uint64_t pattern = layout->llot_pattern;
257 struct lov_user_ost_data *lmm_objects;
258 int obj_count = 0, i;
260 if ((pattern & LLAPI_LAYOUT_SPECIFIC) != 0) {
261 if (layout->llot_objects_count < layout->llot_stripe_count) {
265 magic = LOV_USER_MAGIC_SPECIFIC;
266 obj_count = layout->llot_stripe_count;
267 pattern &= ~LLAPI_LAYOUT_SPECIFIC;
268 } else if (strlen(layout->llot_pool_name) != 0) {
269 magic = LOV_USER_MAGIC_V3;
273 * All stripes must be specified when the pattern contains
274 * LLAPI_LAYOUT_SPECIFIC
276 for (i = 0; i < obj_count; i++) {
277 if (layout->llot_objects[i].l_ost_idx ==
278 LLAPI_LAYOUT_IDX_MAX) {
284 lum_size = lov_user_md_size(obj_count, magic);
285 lum = malloc(lum_size);
289 lum->lmm_magic = magic;
291 if (pattern == LLAPI_LAYOUT_DEFAULT)
292 lum->lmm_pattern = 0;
293 else if (pattern == LLAPI_LAYOUT_RAID0)
294 lum->lmm_pattern = LOV_PATTERN_RAID0;
296 lum->lmm_pattern = pattern;
298 if (layout->llot_stripe_size == LLAPI_LAYOUT_DEFAULT)
299 lum->lmm_stripe_size = 0;
301 lum->lmm_stripe_size = layout->llot_stripe_size;
303 if (layout->llot_stripe_count == LLAPI_LAYOUT_DEFAULT)
304 lum->lmm_stripe_count = 0;
305 else if (layout->llot_stripe_count == LLAPI_LAYOUT_WIDE)
306 lum->lmm_stripe_count = LOV_ALL_STRIPES;
308 lum->lmm_stripe_count = layout->llot_stripe_count;
310 if (layout->llot_stripe_offset == LLAPI_LAYOUT_DEFAULT)
311 lum->lmm_stripe_offset = -1;
313 lum->lmm_stripe_offset = layout->llot_stripe_offset;
315 if (magic == LOV_USER_MAGIC_V3 || magic == LOV_USER_MAGIC_SPECIFIC) {
316 struct lov_user_md_v3 *lumv3 = (struct lov_user_md_v3 *)lum;
318 if (strlen(layout->llot_pool_name)) {
319 strncpy(lumv3->lmm_pool_name, layout->llot_pool_name,
320 sizeof(lumv3->lmm_pool_name));
322 memset(lumv3->lmm_pool_name, 0, LOV_MAXPOOLNAME);
324 lmm_objects = lumv3->lmm_objects;
326 lmm_objects = lum->lmm_objects;
329 for (i = 0; i < obj_count; i++)
330 lmm_objects[i].l_ost_idx = layout->llot_objects[i].l_ost_idx;
336 * Get the parent directory of a path.
338 * \param[in] path path to get parent of
339 * \param[out] buf buffer in which to store parent path
340 * \param[in] size size in bytes of buffer \a buf
342 static void get_parent_dir(const char *path, char *buf, size_t size)
346 strncpy(buf, path, size);
347 p = strrchr(buf, '/');
352 strncpy(buf, ".", 2);
356 * Substitute unspecified attribute values in \a dest with
357 * values from \a src.
359 * \param[in] src layout to inherit values from
360 * \param[in] dest layout to receive inherited values
362 static void inherit_layout_attributes(const struct llapi_layout *src,
363 struct llapi_layout *dest)
365 if (dest->llot_pattern == LLAPI_LAYOUT_DEFAULT)
366 dest->llot_pattern = src->llot_pattern;
368 if (dest->llot_stripe_size == LLAPI_LAYOUT_DEFAULT)
369 dest->llot_stripe_size = src->llot_stripe_size;
371 if (dest->llot_stripe_count == LLAPI_LAYOUT_DEFAULT)
372 dest->llot_stripe_count = src->llot_stripe_count;
374 if (dest->llot_stripe_offset == LLAPI_LAYOUT_DEFAULT)
375 dest->llot_stripe_offset = src->llot_stripe_offset;
379 * Test if all attributes of \a layout are specified.
381 * \param[in] layout the layout to check
383 * \retval true all attributes are specified
384 * \retval false at least one attribute is unspecified
386 static bool is_fully_specified(const struct llapi_layout *layout)
388 return layout->llot_pattern != LLAPI_LAYOUT_DEFAULT &&
389 layout->llot_stripe_size != LLAPI_LAYOUT_DEFAULT &&
390 layout->llot_stripe_count != LLAPI_LAYOUT_DEFAULT;
394 * Allocate and initialize a new layout.
396 * \retval valid llapi_layout pointer on success
397 * \retval NULL if memory allocation fails
399 struct llapi_layout *llapi_layout_alloc(void)
401 struct llapi_layout *layout;
403 layout = __llapi_layout_alloc(0);
408 layout->llot_magic = LLAPI_LAYOUT_MAGIC;
409 layout->llot_pattern = LLAPI_LAYOUT_DEFAULT;
410 layout->llot_stripe_size = LLAPI_LAYOUT_DEFAULT;
411 layout->llot_stripe_count = LLAPI_LAYOUT_DEFAULT;
412 layout->llot_stripe_offset = LLAPI_LAYOUT_DEFAULT;
413 layout->llot_pool_name[0] = '\0';
419 * Check if the given \a lum_size is large enough to hold the required
422 * \param[in] lum the struct lov_user_md to check
423 * \param[in] lum_size the number of bytes in \a lum
425 * \retval true the \a lum_size is too small
426 * \retval false the \a lum_size is large enough
428 static bool llapi_layout_lum_truncated(struct lov_user_md *lum, size_t lum_size)
432 if (lum_size < lov_user_md_size(0, LOV_MAGIC_V1))
435 if (lum->lmm_magic == __swab32(LOV_MAGIC_V1) ||
436 lum->lmm_magic == __swab32(LOV_MAGIC_V3))
437 magic = __swab32(lum->lmm_magic);
439 magic = lum->lmm_magic;
441 return lum_size < lov_user_md_size(0, magic);
445 * Compute the number of elements in the lmm_objects array of \a lum
446 * with size \a lum_size.
448 * \param[in] lum the struct lov_user_md to check
449 * \param[in] lum_size the number of bytes in \a lum
451 * \retval number of elements in array lum->lmm_objects
453 static int llapi_layout_objects_in_lum(struct lov_user_md *lum, size_t lum_size)
458 if (lum_size < lov_user_md_size(0, LOV_MAGIC_V1))
461 if (lum->lmm_magic == __swab32(LOV_MAGIC_V1) ||
462 lum->lmm_magic == __swab32(LOV_MAGIC_V3))
463 magic = __swab32(lum->lmm_magic);
465 magic = lum->lmm_magic;
467 base_size = lov_user_md_size(0, magic);
469 if (lum_size <= base_size)
472 return (lum_size - base_size) / sizeof(lum->lmm_objects[0]);
476 * Get the striping layout for the file referenced by file descriptor \a fd.
478 * If the filesystem does not support the "lustre." xattr namespace, the
479 * file must be on a non-Lustre filesystem, so set errno to ENOTTY per
480 * convention. If the file has no "lustre.lov" data, the file will
481 * inherit default values, so return a default layout.
483 * If the kernel gives us back less than the expected amount of data,
484 * we fail with errno set to EINTR.
486 * \param[in] fd open file descriptor
487 * \param[in] flags open file descriptor
489 * \retval valid llapi_layout pointer on success
490 * \retval NULL if an error occurs
492 struct llapi_layout *llapi_layout_get_by_fd(int fd, uint32_t flags)
495 struct lov_user_md *lum;
496 struct llapi_layout *layout = NULL;
499 int lum_stripe_count;
503 lum_len = XATTR_SIZE_MAX;
504 lum = malloc(lum_len);
508 bytes_read = fgetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_len);
509 if (bytes_read < 0) {
510 if (errno == EOPNOTSUPP)
512 else if (errno == ENODATA)
513 layout = llapi_layout_alloc();
517 /* Return an error if we got back a partial layout. */
518 if (llapi_layout_lum_truncated(lum, bytes_read)) {
523 object_count = llapi_layout_objects_in_lum(lum, bytes_read);
525 need_swab = lum->lmm_magic == __swab32(LOV_MAGIC_V1) ||
526 lum->lmm_magic == __swab32(LOV_MAGIC_V3);
529 lum_stripe_count = __swab16(lum->lmm_stripe_count);
531 lum_stripe_count = lum->lmm_stripe_count;
533 /* Directories may have a positive non-zero lum->lmm_stripe_count
534 * yet have an empty lum->lmm_objects array. For non-directories the
535 * amount of data returned from the kernel must be consistent
536 * with the stripe count. */
537 if (fstat(fd, &st) < 0)
540 if (!S_ISDIR(st.st_mode) && object_count != lum_stripe_count) {
546 llapi_layout_swab_lov_user_md(lum, object_count);
548 layout = llapi_layout_from_lum(lum, object_count);
556 * Get the expected striping layout for a file at \a path.
558 * Substitute expected inherited attribute values for unspecified
559 * attributes. Unspecified attributes may belong to directories and
560 * never-written-to files, and indicate that default values will be
561 * assigned when files are created or first written to. A default value
562 * is inherited from the parent directory if the attribute is specified
563 * there, otherwise it is inherited from the filesystem root.
564 * Unspecified attributes normally have the value LLAPI_LAYOUT_DEFAULT.
566 * The complete \a path need not refer to an existing file or directory,
567 * but some leading portion of it must reside within a lustre filesystem.
568 * A use case for this interface would be to obtain the literal striping
569 * values that would be assigned to a new file in a given directory.
571 * \param[in] path path for which to get the expected layout
573 * \retval valid llapi_layout pointer on success
574 * \retval NULL if an error occurs
576 static struct llapi_layout *llapi_layout_expected(const char *path)
578 struct llapi_layout *path_layout = NULL;
579 struct llapi_layout *donor_layout;
580 char donor_path[PATH_MAX];
585 fd = open(path, O_RDONLY);
586 if (fd < 0 && errno != ENOENT)
592 path_layout = llapi_layout_get_by_fd(fd, 0);
598 if (path_layout == NULL) {
599 if (errno != ENODATA && errno != ENOENT)
602 path_layout = llapi_layout_alloc();
603 if (path_layout == NULL)
607 if (is_fully_specified(path_layout))
610 rc = stat(path, &st);
611 if (rc < 0 && errno != ENOENT) {
612 llapi_layout_free(path_layout);
616 /* If path is a not a directory or doesn't exist, inherit unspecified
617 * attributes from parent directory. */
618 if ((rc == 0 && !S_ISDIR(st.st_mode)) ||
619 (rc < 0 && errno == ENOENT)) {
620 get_parent_dir(path, donor_path, sizeof(donor_path));
621 donor_layout = llapi_layout_get_by_path(donor_path, 0);
622 if (donor_layout != NULL) {
623 inherit_layout_attributes(donor_layout, path_layout);
624 llapi_layout_free(donor_layout);
625 if (is_fully_specified(path_layout))
630 /* Inherit remaining unspecified attributes from the filesystem root. */
631 rc = llapi_search_mounts(path, 0, donor_path, NULL);
633 llapi_layout_free(path_layout);
636 donor_layout = llapi_layout_get_by_path(donor_path, 0);
637 if (donor_layout == NULL) {
638 llapi_layout_free(path_layout);
642 inherit_layout_attributes(donor_layout, path_layout);
643 llapi_layout_free(donor_layout);
649 * Get the striping layout for the file at \a path.
651 * If \a flags contains LAYOUT_GET_EXPECTED, substitute
652 * expected inherited attribute values for unspecified attributes. See
653 * llapi_layout_expected().
655 * \param[in] path path for which to get the layout
656 * \param[in] flags flags to control how layout is retrieved
658 * \retval valid llapi_layout pointer on success
659 * \retval NULL if an error occurs
661 struct llapi_layout *llapi_layout_get_by_path(const char *path, uint32_t flags)
663 struct llapi_layout *layout = NULL;
667 if (flags & LAYOUT_GET_EXPECTED)
668 return llapi_layout_expected(path);
670 fd = open(path, O_RDONLY);
674 layout = llapi_layout_get_by_fd(fd, flags);
683 * Get the layout for the file with FID \a fidstr in filesystem \a lustre_dir.
685 * \param[in] lustre_dir path within Lustre filesystem containing \a fid
686 * \param[in] fid Lustre identifier of file to get layout for
688 * \retval valid llapi_layout pointer on success
689 * \retval NULL if an error occurs
691 struct llapi_layout *llapi_layout_get_by_fid(const char *lustre_dir,
692 const lustre_fid *fid,
697 int saved_msg_level = llapi_msg_get_level();
698 struct llapi_layout *layout = NULL;
700 /* Prevent llapi internal routines from writing to console
701 * while executing this function, then restore previous message
703 llapi_msg_set_level(LLAPI_MSG_OFF);
704 fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY);
705 llapi_msg_set_level(saved_msg_level);
710 layout = llapi_layout_get_by_fd(fd, flags);
719 * Free memory allocated for \a layout.
721 * \param[in] layout previously allocated by llapi_layout_alloc()
723 void llapi_layout_free(struct llapi_layout *layout)
725 if (layout->llot_objects != NULL)
726 free(layout->llot_objects);
731 * Get the stripe count of \a layout.
733 * \param[in] layout layout to get stripe count from
734 * \param[out] count integer to store stripe count in
736 * \retval 0 on success
737 * \retval -1 if arguments are invalid
739 int llapi_layout_stripe_count_get(const struct llapi_layout *layout,
742 if (layout == NULL || count == NULL ||
743 layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
747 *count = layout->llot_stripe_count;
752 * The llapi_layout API functions have these extra validity checks since
753 * they use intuitively named macros to denote special behavior, whereas
754 * the old API uses 0 and -1.
757 static bool llapi_layout_stripe_count_is_valid(int64_t stripe_count)
759 return stripe_count == LLAPI_LAYOUT_DEFAULT ||
760 stripe_count == LLAPI_LAYOUT_WIDE ||
761 (stripe_count != 0 && stripe_count != -1 &&
762 llapi_stripe_count_is_valid(stripe_count));
765 static bool llapi_layout_stripe_size_is_valid(uint64_t stripe_size)
767 return stripe_size == LLAPI_LAYOUT_DEFAULT ||
769 llapi_stripe_size_is_aligned(stripe_size) &&
770 !llapi_stripe_size_is_too_big(stripe_size));
773 static bool llapi_layout_stripe_index_is_valid(int64_t stripe_index)
775 return stripe_index == LLAPI_LAYOUT_DEFAULT ||
776 (stripe_index >= 0 &&
777 llapi_stripe_index_is_valid(stripe_index));
781 * Set the stripe count of \a layout.
783 * \param[in] layout layout to set stripe count in
784 * \param[in] count value to be set
786 * \retval 0 on success
787 * \retval -1 if arguments are invalid
789 int llapi_layout_stripe_count_set(struct llapi_layout *layout,
792 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC ||
793 !llapi_layout_stripe_count_is_valid(count)) {
798 layout->llot_stripe_count = count;
804 * Get the stripe size of \a layout.
806 * \param[in] layout layout to get stripe size from
807 * \param[out] size integer to store stripe size in
809 * \retval 0 on success
810 * \retval -1 if arguments are invalid
812 int llapi_layout_stripe_size_get(const struct llapi_layout *layout,
815 if (layout == NULL || size == NULL ||
816 layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
821 *size = layout->llot_stripe_size;
827 * Set the stripe size of \a layout.
829 * \param[in] layout layout to set stripe size in
830 * \param[in] size value to be set
832 * \retval 0 on success
833 * \retval -1 if arguments are invalid
835 int llapi_layout_stripe_size_set(struct llapi_layout *layout,
838 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC ||
839 !llapi_layout_stripe_size_is_valid(size)) {
844 layout->llot_stripe_size = size;
850 * Get the RAID pattern of \a layout.
852 * \param[in] layout layout to get pattern from
853 * \param[out] pattern integer to store pattern in
855 * \retval 0 on success
856 * \retval -1 if arguments are invalid
858 int llapi_layout_pattern_get(const struct llapi_layout *layout,
861 if (layout == NULL || pattern == NULL ||
862 layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
867 *pattern = layout->llot_pattern;
873 * Set the RAID pattern of \a layout.
875 * \param[in] layout layout to set pattern in
876 * \param[in] pattern value to be set
878 * \retval 0 on success
879 * \retval -1 if arguments are invalid or RAID pattern
882 int llapi_layout_pattern_set(struct llapi_layout *layout, uint64_t pattern)
884 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
889 if (pattern != LLAPI_LAYOUT_DEFAULT &&
890 pattern != LLAPI_LAYOUT_RAID0) {
895 layout->llot_pattern = pattern |
896 (layout->llot_pattern & LLAPI_LAYOUT_SPECIFIC);
901 static inline int stripe_number_roundup(int stripe_number)
903 unsigned int round_up = (stripe_number + 8) & ~7;
904 return round_up > LOV_MAX_STRIPE_COUNT ?
905 LOV_MAX_STRIPE_COUNT : round_up;
909 * Set the OST index of stripe number \a stripe_number to \a ost_index.
911 * If only the starting stripe's OST index is specified, then this can use
912 * the normal LOV_MAGIC_{V1,V3} layout type. If multiple OST indices are
913 * given, then allocate an array to hold the list of indices and ensure that
914 * the LOV_USER_MAGIC_SPECIFIC layout is used when creating the file.
916 * \param[in] layout layout to set OST index in
917 * \param[in] stripe_number stripe number to set index for
918 * \param[in] ost_index the index to set
920 * \retval 0 on success
921 * \retval -1 if arguments are invalid or an unsupported stripe number
922 * was specified, error returned in errno
924 int llapi_layout_ost_index_set(struct llapi_layout *layout, int stripe_number,
927 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
931 if (!llapi_layout_stripe_index_is_valid(ost_index)) {
936 if (stripe_number == 0 && ost_index == LLAPI_LAYOUT_DEFAULT) {
937 layout->llot_stripe_offset = ost_index;
938 layout->llot_pattern &= ~LLAPI_LAYOUT_SPECIFIC;
939 __llapi_layout_objects_realloc(layout, 0);
940 } else if (stripe_number >= 0 &&
941 stripe_number < LOV_MAX_STRIPE_COUNT) {
942 if (ost_index >= LLAPI_LAYOUT_IDX_MAX) {
947 /* Preallocate a few more stripes to avoid realloc() overhead.*/
948 if (__llapi_layout_objects_realloc(layout,
949 stripe_number_roundup(stripe_number)) < 0)
952 layout->llot_objects[stripe_number].l_ost_idx = ost_index;
954 if (stripe_number == 0)
955 layout->llot_stripe_offset = ost_index;
957 layout->llot_pattern |= LLAPI_LAYOUT_SPECIFIC;
959 if (layout->llot_stripe_count == LLAPI_LAYOUT_DEFAULT ||
960 layout->llot_stripe_count <= stripe_number)
961 layout->llot_stripe_count = stripe_number + 1;
971 * Get the OST index associated with stripe \a stripe_number.
973 * Stripes are indexed starting from zero.
975 * \param[in] layout layout to get index from
976 * \param[in] stripe_number stripe number to get index for
977 * \param[out] index integer to store index in
979 * \retval 0 on success
980 * \retval -1 if arguments are invalid
982 int llapi_layout_ost_index_get(const struct llapi_layout *layout,
983 uint64_t stripe_number, uint64_t *index)
985 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC ||
986 stripe_number >= layout->llot_stripe_count ||
987 stripe_number >= layout->llot_objects_count || index == NULL) {
992 if (layout->llot_stripe_offset == LLAPI_LAYOUT_DEFAULT)
993 *index = LLAPI_LAYOUT_DEFAULT;
995 *index = layout->llot_objects[stripe_number].l_ost_idx;
1002 * Get the pool name of layout \a layout.
1004 * \param[in] layout layout to get pool name from
1005 * \param[out] dest buffer to store pool name in
1006 * \param[in] n size in bytes of buffer \a dest
1008 * \retval 0 on success
1009 * \retval -1 if arguments are invalid
1011 int llapi_layout_pool_name_get(const struct llapi_layout *layout, char *dest,
1014 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC ||
1020 strncpy(dest, layout->llot_pool_name, n);
1026 * Set the name of the pool of layout \a layout.
1028 * \param[in] layout layout to set pool name in
1029 * \param[in] pool_name pool name to set
1031 * \retval 0 on success
1032 * \retval -1 if arguments are invalid or pool name is too long
1034 int llapi_layout_pool_name_set(struct llapi_layout *layout,
1035 const char *pool_name)
1039 if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC ||
1040 pool_name == NULL) {
1045 /* Strip off any 'fsname.' portion. */
1046 ptr = strchr(pool_name, '.');
1048 pool_name = ptr + 1;
1050 if (strlen(pool_name) > LOV_MAXPOOLNAME) {
1055 strncpy(layout->llot_pool_name, pool_name,
1056 sizeof(layout->llot_pool_name));
1062 * Open and possibly create a file with a given \a layout.
1064 * If \a layout is NULL this function acts as a simple wrapper for
1065 * open(). By convention, ENOTTY is returned in errno if \a path
1066 * refers to a non-Lustre file.
1068 * \param[in] path name of the file to open
1069 * \param[in] open_flags open() flags
1070 * \param[in] mode permissions to create new file with
1071 * \param[in] layout layout to create new file with
1073 * \retval non-negative file descriptor on successful open
1074 * \retval -1 if an error occurred
1076 int llapi_layout_file_open(const char *path, int open_flags, mode_t mode,
1077 const struct llapi_layout *layout)
1082 struct lov_user_md *lum;
1086 (layout != NULL && layout->llot_magic != LLAPI_LAYOUT_MAGIC)) {
1091 /* Object creation must be postponed until after layout attributes
1092 * have been applied. */
1093 if (layout != NULL && (open_flags & O_CREAT))
1094 open_flags |= O_LOV_DELAY_CREATE;
1096 fd = open(path, open_flags, mode);
1098 if (layout == NULL || fd < 0)
1101 lum = llapi_layout_to_lum(layout);
1110 if (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)
1111 lum_size = lov_user_md_size(lum->lmm_stripe_count,
1114 lum_size = lov_user_md_size(0, lum->lmm_magic);
1116 rc = fsetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_size, 0);
1125 errno = errno == EOPNOTSUPP ? ENOTTY : errno;
1131 * Create a file with a given \a layout.
1133 * Force O_CREAT and O_EXCL flags on so caller is assured that file was
1134 * created with the given \a layout on successful function return.
1136 * \param[in] path name of the file to open
1137 * \param[in] open_flags open() flags
1138 * \param[in] mode permissions to create new file with
1139 * \param[in] layout layout to create new file with
1141 * \retval non-negative file descriptor on successful open
1142 * \retval -1 if an error occurred
1144 int llapi_layout_file_create(const char *path, int open_flags, int mode,
1145 const struct llapi_layout *layout)
1147 return llapi_layout_file_open(path, open_flags|O_CREAT|O_EXCL, mode,