Whamcloud - gitweb
LU-10308 misc: update Intel copyright messages for 2017
[fs/lustre-release.git] / lustre / utils / liblustreapi_layout.c
1 /*
2  * LGPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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
11  *
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.
16  *
17  * LGPL HEADER END
18  */
19 /*
20  * lustre/utils/liblustreapi_layout.c
21  *
22  * lustreapi library for layout calls for interacting with the layout of
23  * Lustre files while hiding details of the internal data structures
24  * from the user.
25  *
26  * Copyright (c) 2016, 2017, Intel Corporation.
27  *
28  * Author: Ned Bass <bass6@llnl.gov>
29  */
30
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <errno.h>
36 #include <limits.h>
37 #include <sys/xattr.h>
38 #include <sys/param.h>
39
40 #include <libcfs/util/list.h>
41 #include <lustre/lustreapi.h>
42 #include "lustreapi_internal.h"
43
44 /**
45  * Layout component, which contains all attributes of a plain
46  * V1/V3 layout.
47  */
48 struct llapi_layout_comp {
49         uint64_t        llc_pattern;
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
63                                                    components list */
64 };
65
66 /**
67  * An Opaque data type abstracting the layout of a Lustre file.
68  */
69 struct llapi_layout {
70         uint32_t        llot_magic; /* LLAPI_LAYOUT_MAGIC */
71         uint32_t        llot_gen;
72         uint32_t        llot_flags;
73         bool            llot_is_composite;
74         uint16_t        llot_mirror_count;
75         /* Cursor pointing to one of the components in llot_comp_list */
76         struct llapi_layout_comp *llot_cur_comp;
77         struct list_head          llot_comp_list;
78 };
79
80 /**
81  * Compute the number of elements in the lmm_objects array of \a lum
82  * with size \a lum_size.
83  *
84  * \param[in] lum       the struct lov_user_md to check
85  * \param[in] lum_size  the number of bytes in \a lum
86  *
87  * \retval              number of elements in array lum->lmm_objects
88  */
89 static int llapi_layout_objects_in_lum(struct lov_user_md *lum, size_t lum_size)
90 {
91         uint32_t magic;
92         size_t base_size;
93
94         if (lum_size < lov_user_md_size(0, LOV_MAGIC_V1))
95                 return 0;
96
97         if (lum->lmm_magic == __swab32(LOV_MAGIC_V1) ||
98             lum->lmm_magic == __swab32(LOV_MAGIC_V3))
99                 magic = __swab32(lum->lmm_magic);
100         else
101                 magic = lum->lmm_magic;
102
103         base_size = lov_user_md_size(0, magic);
104
105         if (lum_size <= base_size)
106                 return 0;
107         else
108                 return (lum_size - base_size) / sizeof(lum->lmm_objects[0]);
109 }
110
111 /**
112  * Byte-swap the fields of struct lov_user_md.
113  *
114  * XXX Rather than duplicating swabbing code here, we should eventually
115  * refactor the needed functions in lustre/ptlrpc/pack_generic.c
116  * into a library that can be shared between kernel and user code.
117  */
118 static void
119 llapi_layout_swab_lov_user_md(struct lov_user_md *lum, int lum_size)
120 {
121         int i, j, ent_count, obj_count;
122         struct lov_comp_md_v1 *comp_v1 = NULL;
123         struct lov_comp_md_entry_v1 *ent;
124         struct lov_user_ost_data *lod;
125
126         if (lum->lmm_magic != __swab32(LOV_MAGIC_V1) &&
127             lum->lmm_magic != __swab32(LOV_MAGIC_V3) &&
128             lum->lmm_magic != __swab32(LOV_MAGIC_COMP_V1))
129                 return;
130
131         if (lum->lmm_magic == __swab32(LOV_MAGIC_COMP_V1))
132                 comp_v1 = (struct lov_comp_md_v1 *)lum;
133
134         if (comp_v1 != NULL) {
135                 __swab32s(&comp_v1->lcm_magic);
136                 __swab32s(&comp_v1->lcm_size);
137                 __swab32s(&comp_v1->lcm_layout_gen);
138                 __swab16s(&comp_v1->lcm_flags);
139                 __swab16s(&comp_v1->lcm_entry_count);
140                 ent_count = comp_v1->lcm_entry_count;
141         } else {
142                 ent_count = 1;
143         }
144
145         for (i = 0; i < ent_count; i++) {
146                 if (comp_v1 != NULL) {
147                         ent = &comp_v1->lcm_entries[i];
148                         __swab32s(&ent->lcme_id);
149                         __swab32s(&ent->lcme_flags);
150                         __swab64s(&ent->lcme_extent.e_start);
151                         __swab64s(&ent->lcme_extent.e_end);
152                         __swab32s(&ent->lcme_offset);
153                         __swab32s(&ent->lcme_size);
154
155                         lum = (struct lov_user_md *)((char *)comp_v1 +
156                                         ent->lcme_offset);
157                         lum_size = ent->lcme_size;
158                 }
159                 obj_count = llapi_layout_objects_in_lum(lum, lum_size);
160
161                 __swab32s(&lum->lmm_magic);
162                 __swab32s(&lum->lmm_pattern);
163                 __swab32s(&lum->lmm_stripe_size);
164                 __swab16s(&lum->lmm_stripe_count);
165                 __swab16s(&lum->lmm_stripe_offset);
166
167                 if (lum->lmm_magic != LOV_MAGIC_V1) {
168                         struct lov_user_md_v3 *v3;
169                         v3 = (struct lov_user_md_v3 *)lum;
170                         lod = v3->lmm_objects;
171                 } else {
172                         lod = lum->lmm_objects;
173                 }
174
175                 for (j = 0; j < obj_count; j++)
176                         __swab32s(&lod[j].l_ost_idx);
177         }
178 }
179
180 /**
181  * (Re-)allocate llc_objects[] to \a num_stripes stripes.
182  *
183  * Copy over existing llc_objects[], if any, to the new llc_objects[].
184  *
185  * \param[in] layout            existing layout to be modified
186  * \param[in] num_stripes       number of stripes in new layout
187  *
188  * \retval      0 if the objects are re-allocated successfully
189  * \retval      -1 on error with errno set
190  */
191 static int __llapi_comp_objects_realloc(struct llapi_layout_comp *comp,
192                                         unsigned int new_stripes)
193 {
194         struct lov_user_ost_data_v1 *new_objects;
195         unsigned int i;
196
197         if (new_stripes > LOV_MAX_STRIPE_COUNT) {
198                 errno = EINVAL;
199                 return -1;
200         }
201
202         if (new_stripes == comp->llc_objects_count)
203                 return 0;
204
205         if (new_stripes != 0 && new_stripes <= comp->llc_objects_count)
206                 return 0;
207
208         new_objects = realloc(comp->llc_objects,
209                               sizeof(*new_objects) * new_stripes);
210         if (new_objects == NULL && new_stripes != 0) {
211                 errno = ENOMEM;
212                 return -1;
213         }
214
215         for (i = comp->llc_objects_count; i < new_stripes; i++)
216                 new_objects[i].l_ost_idx = LLAPI_LAYOUT_IDX_MAX;
217
218         comp->llc_objects = new_objects;
219         comp->llc_objects_count = new_stripes;
220
221         return 0;
222 }
223
224 /**
225  * Allocate storage for a llapi_layout_comp with \a num_stripes stripes.
226  *
227  * \param[in] num_stripes       number of stripes in new layout
228  *
229  * \retval      valid pointer if allocation succeeds
230  * \retval      NULL if allocation fails
231  */
232 static struct llapi_layout_comp *__llapi_comp_alloc(unsigned int num_stripes)
233 {
234         struct llapi_layout_comp *comp;
235
236         if (num_stripes > LOV_MAX_STRIPE_COUNT) {
237                 errno = EINVAL;
238                 return NULL;
239         }
240
241         comp = calloc(1, sizeof(*comp));
242         if (comp == NULL) {
243                 errno = ENOMEM;
244                 return NULL;
245         }
246
247         comp->llc_objects = NULL;
248         comp->llc_objects_count = 0;
249
250         if (__llapi_comp_objects_realloc(comp, num_stripes) < 0) {
251                 free(comp);
252                 return NULL;
253         }
254
255         /* Set defaults. */
256         comp->llc_pattern = LLAPI_LAYOUT_DEFAULT;
257         comp->llc_stripe_size = LLAPI_LAYOUT_DEFAULT;
258         comp->llc_stripe_count = LLAPI_LAYOUT_DEFAULT;
259         comp->llc_stripe_offset = LLAPI_LAYOUT_DEFAULT;
260         comp->llc_pool_name[0] = '\0';
261         comp->llc_extent.e_start = 0;
262         comp->llc_extent.e_end = LUSTRE_EOF;
263         comp->llc_flags = 0;
264         comp->llc_id = 0;
265         INIT_LIST_HEAD(&comp->llc_list);
266
267         return comp;
268 }
269
270 /**
271  * Free memory allocated for \a comp
272  *
273  * \param[in] comp      previously allocated by __llapi_comp_alloc()
274  */
275 static void __llapi_comp_free(struct llapi_layout_comp *comp)
276 {
277         if (comp->llc_objects != NULL)
278                 free(comp->llc_objects);
279         free(comp);
280 }
281
282 /**
283  * Free memory allocated for \a layout.
284  *
285  * \param[in] layout    previously allocated by llapi_layout_alloc()
286  */
287 void llapi_layout_free(struct llapi_layout *layout)
288 {
289         struct llapi_layout_comp *comp, *n;
290
291         if (layout == NULL)
292                 return;
293
294         list_for_each_entry_safe(comp, n, &layout->llot_comp_list, llc_list) {
295                 list_del_init(&comp->llc_list);
296                 __llapi_comp_free(comp);
297         }
298         free(layout);
299 }
300
301 /**
302  * Allocate and initialize a llapi_layout structure.
303  *
304  * \retval      valid llapi_layout pointer on success
305  * \retval      NULL if memory allocation fails
306  */
307 static struct llapi_layout *__llapi_layout_alloc(void)
308 {
309         struct llapi_layout *layout;
310
311         layout = calloc(1, sizeof(*layout));
312         if (layout == NULL) {
313                 errno = ENOMEM;
314                 return NULL;
315         }
316
317         /* Set defaults. */
318         layout->llot_magic = LLAPI_LAYOUT_MAGIC;
319         layout->llot_gen = 0;
320         layout->llot_flags = 0;
321         layout->llot_is_composite = false;
322         layout->llot_mirror_count = 1;
323         layout->llot_cur_comp = NULL;
324         INIT_LIST_HEAD(&layout->llot_comp_list);
325
326         return layout;
327 }
328
329 /**
330  * Allocate and initialize a new plain layout.
331  *
332  * \retval      valid llapi_layout pointer on success
333  * \retval      NULL if memory allocation fails
334  */
335 struct llapi_layout *llapi_layout_alloc(void)
336 {
337         struct llapi_layout_comp *comp;
338         struct llapi_layout *layout;
339
340         layout = __llapi_layout_alloc();
341         if (layout == NULL)
342                 return NULL;
343
344         comp = __llapi_comp_alloc(0);
345         if (comp == NULL) {
346                 free(layout);
347                 return NULL;
348         }
349
350         list_add_tail(&comp->llc_list, &layout->llot_comp_list);
351         layout->llot_cur_comp = comp;
352
353         return layout;
354 }
355
356 /**
357  * Convert the data from a lov_user_md to a newly allocated llapi_layout.
358  * The caller is responsible for freeing the returned pointer.
359  *
360  * \param[in] lum       LOV user metadata structure to copy data from
361  * \param[in] lum_size  size the the lum passed in
362  *
363  * \retval              valid llapi_layout pointer on success
364  * \retval              NULL if memory allocation fails
365  */
366 static struct llapi_layout *
367 llapi_layout_from_lum(const struct lov_user_md *lum, int lum_size)
368 {
369         struct lov_comp_md_v1 *comp_v1 = NULL;
370         struct lov_comp_md_entry_v1 *ent;
371         struct lov_user_md *v1;
372         struct llapi_layout *layout;
373         struct llapi_layout_comp *comp;
374         int i, ent_count = 0, obj_count;
375
376         layout = __llapi_layout_alloc();
377         if (layout == NULL)
378                 return NULL;
379
380         if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
381                 comp_v1 = (struct lov_comp_md_v1 *)lum;
382                 ent_count = comp_v1->lcm_entry_count;
383                 layout->llot_gen = comp_v1->lcm_layout_gen;
384                 layout->llot_is_composite = true;
385                 layout->llot_mirror_count = comp_v1->lcm_mirror_count + 1;
386                 layout->llot_gen = comp_v1->lcm_layout_gen;
387                 layout->llot_flags = comp_v1->lcm_flags;
388         } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
389                    lum->lmm_magic == LOV_MAGIC_V3) {
390                 ent_count = 1;
391                 layout->llot_is_composite = false;
392         }
393
394         if (ent_count == 0) {
395                 errno = EINVAL;
396                 goto error;
397         }
398
399         v1 = (struct lov_user_md *)lum;
400         for (i = 0; i < ent_count; i++) {
401                 if (comp_v1 != NULL) {
402                         ent = &comp_v1->lcm_entries[i];
403                         v1 = (struct lov_user_md *)((char *)comp_v1 +
404                                 ent->lcme_offset);
405                         lum_size = ent->lcme_size;
406                 } else {
407                         ent = NULL;
408                 }
409
410                 obj_count = llapi_layout_objects_in_lum(v1, lum_size);
411                 comp = __llapi_comp_alloc(obj_count);
412                 if (comp == NULL)
413                         goto error;
414
415                 if (ent != NULL) {
416                         comp->llc_extent.e_start = ent->lcme_extent.e_start;
417                         comp->llc_extent.e_end = ent->lcme_extent.e_end;
418                         comp->llc_id = ent->lcme_id;
419                         comp->llc_flags = ent->lcme_flags;
420                 } else {
421                         comp->llc_extent.e_start = 0;
422                         comp->llc_extent.e_end = LUSTRE_EOF;
423                         comp->llc_id = 0;
424                         comp->llc_flags = 0;
425                 }
426
427                 if (v1->lmm_pattern == LOV_PATTERN_RAID0)
428                         comp->llc_pattern = LLAPI_LAYOUT_RAID0;
429                 else
430                         /* Lustre only supports RAID0 for now. */
431                         comp->llc_pattern = v1->lmm_pattern;
432
433                 if (v1->lmm_stripe_size == 0)
434                         comp->llc_stripe_size = LLAPI_LAYOUT_DEFAULT;
435                 else
436                         comp->llc_stripe_size = v1->lmm_stripe_size;
437
438                 if (v1->lmm_stripe_count == (typeof(v1->lmm_stripe_count))-1)
439                         comp->llc_stripe_count = LLAPI_LAYOUT_WIDE;
440                 else if (v1->lmm_stripe_count == 0)
441                         comp->llc_stripe_count = LLAPI_LAYOUT_DEFAULT;
442                 else
443                         comp->llc_stripe_count = v1->lmm_stripe_count;
444
445                 if (v1->lmm_stripe_offset ==
446                     (typeof(v1->lmm_stripe_offset))-1)
447                         comp->llc_stripe_offset = LLAPI_LAYOUT_DEFAULT;
448                 else
449                         comp->llc_stripe_offset = v1->lmm_stripe_offset;
450
451                 if (v1->lmm_magic != LOV_USER_MAGIC_V1) {
452                         const struct lov_user_md_v3 *lumv3;
453                         lumv3 = (struct lov_user_md_v3 *)v1;
454                         snprintf(comp->llc_pool_name,
455                                  sizeof(comp->llc_pool_name),
456                                  "%s", lumv3->lmm_pool_name);
457                         memcpy(comp->llc_objects, lumv3->lmm_objects,
458                                obj_count * sizeof(lumv3->lmm_objects[0]));
459                 } else {
460                         const struct lov_user_md_v1 *lumv1;
461                         lumv1 = (struct lov_user_md_v1 *)v1;
462                         memcpy(comp->llc_objects, lumv1->lmm_objects,
463                                obj_count * sizeof(lumv1->lmm_objects[0]));
464                 }
465
466                 if (obj_count != 0)
467                         comp->llc_stripe_offset =
468                                 comp->llc_objects[0].l_ost_idx;
469
470                 list_add_tail(&comp->llc_list, &layout->llot_comp_list);
471                 layout->llot_cur_comp = comp;
472         }
473
474         return layout;
475 error:
476         llapi_layout_free(layout);
477         return NULL;
478 }
479
480 /**
481  * Convert the data from a llapi_layout to a newly allocated lov_user_md.
482  * The caller is responsible for freeing the returned pointer.
483  *
484  * \param[in] layout    the layout to copy from
485  *
486  * \retval      valid lov_user_md pointer on success
487  * \retval      NULL if memory allocation fails or the layout is invalid
488  */
489 static struct lov_user_md *
490 llapi_layout_to_lum(const struct llapi_layout *layout)
491 {
492         struct llapi_layout_comp *comp;
493         struct lov_comp_md_v1 *comp_v1 = NULL;
494         struct lov_comp_md_entry_v1 *ent;
495         struct lov_user_md *lum = NULL;
496         size_t lum_size = 0;
497         int ent_idx = 0;
498         uint32_t offset = 0;
499
500         if (layout == NULL ||
501             list_empty((struct list_head *)&layout->llot_comp_list)) {
502                 errno = EINVAL;
503                 return NULL;
504         }
505
506         /* Allocate header of lov_comp_md_v1 if necessary */
507         if (layout->llot_is_composite) {
508                 int comp_cnt = 0;
509
510                 list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
511                         comp_cnt++;
512
513                 lum_size = sizeof(*comp_v1) + comp_cnt * sizeof(*ent);
514                 lum = calloc(lum_size, 1);
515                 if (lum == NULL) {
516                         errno = ENOMEM;
517                         return NULL;
518                 }
519                 comp_v1 = (struct lov_comp_md_v1 *)lum;
520                 comp_v1->lcm_magic = LOV_USER_MAGIC_COMP_V1;
521                 comp_v1->lcm_size = lum_size;
522                 comp_v1->lcm_layout_gen = 0;
523                 comp_v1->lcm_flags = layout->llot_flags;
524                 comp_v1->lcm_entry_count = comp_cnt;
525                 comp_v1->lcm_mirror_count = layout->llot_mirror_count - 1;
526                 offset += lum_size;
527         }
528
529         list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
530                 struct lov_user_md *blob;
531                 size_t blob_size;
532                 uint32_t magic;
533                 int i, obj_count = 0;
534                 struct lov_user_ost_data *lmm_objects;
535                 uint64_t pattern = comp->llc_pattern;
536
537                 if ((pattern & LLAPI_LAYOUT_SPECIFIC) != 0) {
538                         if (comp->llc_objects_count <
539                             comp->llc_stripe_count) {
540                                 errno = EINVAL;
541                                 goto error;
542                         }
543                         magic = LOV_USER_MAGIC_SPECIFIC;
544                         obj_count = comp->llc_stripe_count;
545                         pattern &= ~LLAPI_LAYOUT_SPECIFIC;
546                 } else if (strlen(comp->llc_pool_name) != 0) {
547                         magic = LOV_USER_MAGIC_V3;
548                 } else {
549                         magic = LOV_USER_MAGIC_V1;
550                 }
551                 /* All stripes must be specified when the pattern contains
552                  * LLAPI_LAYOUT_SPECIFIC */
553                 for (i = 0; i < obj_count; i++) {
554                         if (comp->llc_objects[i].l_ost_idx ==
555                             LLAPI_LAYOUT_IDX_MAX) {
556                                 errno = EINVAL;
557                                 goto error;
558                         }
559                 }
560
561                 blob_size = lov_user_md_size(obj_count, magic);
562                 blob = realloc(lum, lum_size + blob_size);
563                 if (blob == NULL) {
564                         errno = ENOMEM;
565                         goto error;
566                 } else {
567                         lum = blob;
568                         comp_v1 = (struct lov_comp_md_v1 *)lum;
569                         blob = (struct lov_user_md *)((char *)lum + lum_size);
570                         lum_size += blob_size;
571                 }
572
573                 blob->lmm_magic = magic;
574                 if (pattern == LLAPI_LAYOUT_DEFAULT)
575                         blob->lmm_pattern = LOV_PATTERN_RAID0;
576                 else if (pattern == LLAPI_LAYOUT_MDT)
577                         blob->lmm_pattern = LOV_PATTERN_MDT;
578                 else
579                         blob->lmm_pattern = pattern;
580
581                 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
582                         blob->lmm_stripe_size = 0;
583                 else
584                         blob->lmm_stripe_size = comp->llc_stripe_size;
585
586                 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
587                         blob->lmm_stripe_count = 0;
588                 else if (comp->llc_stripe_count == LLAPI_LAYOUT_WIDE)
589                         blob->lmm_stripe_count = LOV_ALL_STRIPES;
590                 else
591                         blob->lmm_stripe_count = comp->llc_stripe_count;
592
593                 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
594                         blob->lmm_stripe_offset = -1;
595                 else
596                         blob->lmm_stripe_offset = comp->llc_stripe_offset;
597
598                 if (magic == LOV_USER_MAGIC_V3 ||
599                     magic == LOV_USER_MAGIC_SPECIFIC) {
600                         struct lov_user_md_v3 *lumv3 =
601                                 (struct lov_user_md_v3 *)blob;
602
603                         if (comp->llc_pool_name[0] != '\0') {
604                                 strncpy(lumv3->lmm_pool_name,
605                                         comp->llc_pool_name,
606                                         sizeof(lumv3->lmm_pool_name));
607                         } else {
608                                 memset(lumv3->lmm_pool_name, 0,
609                                        sizeof(lumv3->lmm_pool_name));
610                         }
611                         lmm_objects = lumv3->lmm_objects;
612                 } else {
613                         lmm_objects = blob->lmm_objects;
614                 }
615
616                 for (i = 0; i < obj_count; i++)
617                         lmm_objects[i].l_ost_idx =
618                                 comp->llc_objects[i].l_ost_idx;
619
620                 if (layout->llot_is_composite) {
621                         ent = &comp_v1->lcm_entries[ent_idx];
622                         ent->lcme_id = comp->llc_id;
623                         ent->lcme_flags = comp->llc_flags;
624                         ent->lcme_extent.e_start = comp->llc_extent.e_start;
625                         ent->lcme_extent.e_end = comp->llc_extent.e_end;
626                         ent->lcme_size = blob_size;
627                         ent->lcme_offset = offset;
628                         offset += blob_size;
629                         comp_v1->lcm_size += blob_size;
630                         ent_idx++;
631                 } else {
632                         break;
633                 }
634         }
635
636         return lum;
637 error:
638         free(lum);
639         return NULL;
640 }
641
642 /**
643  * Get the parent directory of a path.
644  *
645  * \param[in] path      path to get parent of
646  * \param[out] buf      buffer in which to store parent path
647  * \param[in] size      size in bytes of buffer \a buf
648  */
649 static void get_parent_dir(const char *path, char *buf, size_t size)
650 {
651         char *p;
652
653         strncpy(buf, path, size);
654         p = strrchr(buf, '/');
655
656         if (p != NULL) {
657                 *p = '\0';
658         } else if (size >= 2) {
659                 strncpy(buf, ".", 2);
660                 buf[size - 1] = '\0';
661         }
662 }
663
664 /**
665  * Substitute unspecified attribute values in \a layout with values
666  * from fs global settings. (lov.stripesize, lov.stripecount,
667  * lov.stripeoffset)
668  *
669  * \param[in] layout    layout to inherit values from
670  * \param[in] path      file path of the filesystem
671  */
672 static void inherit_sys_attributes(struct llapi_layout *layout,
673                                    const char *path)
674 {
675         struct llapi_layout_comp *comp;
676         unsigned int ssize, scount, soffset;
677         int rc;
678
679         rc = sattr_cache_get_defaults(NULL, path, &scount, &ssize, &soffset);
680         if (rc)
681                 return;
682
683         list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
684                 if (comp->llc_pattern == LLAPI_LAYOUT_DEFAULT)
685                         comp->llc_pattern = LLAPI_LAYOUT_RAID0;
686                 if (comp->llc_stripe_size == LLAPI_LAYOUT_DEFAULT)
687                         comp->llc_stripe_size = ssize;
688                 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT)
689                         comp->llc_stripe_count = scount;
690                 if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
691                         comp->llc_stripe_offset = soffset;
692         }
693 }
694
695 /**
696  * Get the current component of \a layout.
697  *
698  * \param[in] layout    layout to get current component
699  *
700  * \retval      valid llapi_layout_comp pointer on success
701  * \retval      NULL on error
702  */
703 static struct llapi_layout_comp *
704 __llapi_layout_cur_comp(const struct llapi_layout *layout)
705 {
706         struct llapi_layout_comp *comp;
707
708         if (layout == NULL || layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
709                 errno = EINVAL;
710                 return NULL;
711         }
712         if (layout->llot_cur_comp == NULL) {
713                 errno = EINVAL;
714                 return NULL;
715         }
716         /* Verify data consistency */
717         list_for_each_entry(comp, &layout->llot_comp_list, llc_list)
718                 if (comp == layout->llot_cur_comp)
719                         return comp;
720         errno = EFAULT;
721         return NULL;
722 }
723
724 /**
725  * Test if any attributes of \a layout are specified.
726  *
727  * \param[in] layout    the layout to check
728  *
729  * \retval true         any attributes are specified
730  * \retval false        all attributes are unspecified
731  */
732 static bool is_any_specified(const struct llapi_layout *layout)
733 {
734         struct llapi_layout_comp *comp;
735
736         comp = __llapi_layout_cur_comp(layout);
737         if (comp == NULL)
738                 return false;
739
740         if (layout->llot_is_composite || layout->llot_mirror_count != 1)
741                 return true;
742
743         return comp->llc_pattern != LLAPI_LAYOUT_DEFAULT ||
744                comp->llc_stripe_size != LLAPI_LAYOUT_DEFAULT ||
745                comp->llc_stripe_count != LLAPI_LAYOUT_DEFAULT ||
746                comp->llc_stripe_offset != LLAPI_LAYOUT_DEFAULT ||
747                strlen(comp->llc_pool_name);
748 }
749
750 /**
751  * Check if the given \a lum_size is large enough to hold the required
752  * fields in \a lum.
753  *
754  * \param[in] lum       the struct lov_user_md to check
755  * \param[in] lum_size  the number of bytes in \a lum
756  *
757  * \retval true         the \a lum_size is too small
758  * \retval false        the \a lum_size is large enough
759  */
760 static bool llapi_layout_lum_truncated(struct lov_user_md *lum, size_t lum_size)
761 {
762         uint32_t magic;
763
764         if (lum_size < sizeof(lum->lmm_magic))
765                 return true;
766
767         if (lum->lmm_magic == LOV_MAGIC_V1 ||
768             lum->lmm_magic == __swab32(LOV_MAGIC_V1))
769                 magic = LOV_MAGIC_V1;
770         else if (lum->lmm_magic == LOV_MAGIC_V3 ||
771                  lum->lmm_magic == __swab32(LOV_MAGIC_V3))
772                 magic = LOV_MAGIC_V3;
773         else if (lum->lmm_magic == LOV_MAGIC_COMP_V1 ||
774                  lum->lmm_magic == __swab32(LOV_MAGIC_COMP_V1))
775                 magic = LOV_MAGIC_COMP_V1;
776         else
777                 return true;
778
779         if (magic == LOV_MAGIC_V1 || magic == LOV_MAGIC_V3)
780                 return lum_size < lov_user_md_size(0, magic);
781         else
782                 return lum_size < sizeof(struct lov_comp_md_v1);
783 }
784
785 /* Verify if the objects count in lum is consistent with the
786  * stripe count in lum. It applies to regular file only. */
787 static bool llapi_layout_lum_valid(struct lov_user_md *lum, int lum_size)
788 {
789         struct lov_comp_md_v1 *comp_v1 = NULL;
790         int i, ent_count, obj_count;
791
792         if (lum->lmm_magic == LOV_MAGIC_COMP_V1) {
793                 comp_v1 = (struct lov_comp_md_v1 *)lum;
794                 ent_count = comp_v1->lcm_entry_count;
795         } else if (lum->lmm_magic == LOV_MAGIC_V1 ||
796                    lum->lmm_magic == LOV_MAGIC_V3) {
797                 ent_count = 1;
798         } else {
799                 return false;
800         }
801
802         for (i = 0; i < ent_count; i++) {
803                 if (comp_v1) {
804                         lum = (struct lov_user_md *)((char *)comp_v1 +
805                                 comp_v1->lcm_entries[i].lcme_offset);
806                         lum_size = comp_v1->lcm_entries[i].lcme_size;
807                 }
808                 obj_count = llapi_layout_objects_in_lum(lum, lum_size);
809
810                 if (comp_v1) {
811                         if (!(comp_v1->lcm_entries[i].lcme_flags &
812                                  LCME_FL_INIT) && obj_count != 0)
813                                 return false;
814                 } else if (obj_count != lum->lmm_stripe_count) {
815                         return false;
816                 }
817         }
818         return true;
819 }
820
821 /**
822  * Get the striping layout for the file referenced by file descriptor \a fd.
823  *
824  * If the filesystem does not support the "lustre." xattr namespace, the
825  * file must be on a non-Lustre filesystem, so set errno to ENOTTY per
826  * convention.  If the file has no "lustre.lov" data, the file will
827  * inherit default values, so return a default layout.
828  *
829  * If the kernel gives us back less than the expected amount of data,
830  * we fail with errno set to EINTR.
831  *
832  * \param[in] fd        open file descriptor
833  * \param[in] flags     open file descriptor
834  *
835  * \retval      valid llapi_layout pointer on success
836  * \retval      NULL if an error occurs
837  */
838 struct llapi_layout *llapi_layout_get_by_fd(int fd, uint32_t flags)
839 {
840         size_t lum_len;
841         struct lov_user_md *lum;
842         struct llapi_layout *layout = NULL;
843         ssize_t bytes_read;
844         struct stat st;
845
846         lum_len = XATTR_SIZE_MAX;
847         lum = malloc(lum_len);
848         if (lum == NULL)
849                 return NULL;
850
851         bytes_read = fgetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_len);
852         if (bytes_read < 0) {
853                 if (errno == EOPNOTSUPP)
854                         errno = ENOTTY;
855                 else if (errno == ENODATA)
856                         layout = llapi_layout_alloc();
857                 goto out;
858         }
859
860         /* Return an error if we got back a partial layout. */
861         if (llapi_layout_lum_truncated(lum, bytes_read)) {
862                 errno = EINTR;
863                 goto out;
864         }
865
866         llapi_layout_swab_lov_user_md(lum, bytes_read);
867
868         /* Directories may have a positive non-zero lum->lmm_stripe_count
869          * yet have an empty lum->lmm_objects array. For non-directories the
870          * amount of data returned from the kernel must be consistent
871          * with the stripe count. */
872         if (fstat(fd, &st) < 0)
873                 goto out;
874
875         if (!S_ISDIR(st.st_mode) && !llapi_layout_lum_valid(lum, bytes_read)) {
876                 errno = EINTR;
877                 goto out;
878         }
879
880         layout = llapi_layout_from_lum(lum, bytes_read);
881 out:
882         free(lum);
883         return layout;
884 }
885
886 /**
887  * Get the expected striping layout for a file at \a path.
888  *
889  * Substitute expected inherited attribute values for unspecified
890  * attributes.  Unspecified attributes may belong to directories and
891  * never-written-to files, and indicate that default values will be
892  * assigned when files are created or first written to.  A default value
893  * is inherited from the parent directory if the attribute is specified
894  * there, otherwise it is inherited from the filesystem root.
895  * Unspecified attributes normally have the value LLAPI_LAYOUT_DEFAULT.
896  *
897  * The complete \a path need not refer to an existing file or directory,
898  * but some leading portion of it must reside within a lustre filesystem.
899  * A use case for this interface would be to obtain the literal striping
900  * values that would be assigned to a new file in a given directory.
901  *
902  * \param[in] path      path for which to get the expected layout
903  *
904  * \retval      valid llapi_layout pointer on success
905  * \retval      NULL if an error occurs
906  */
907 static struct llapi_layout *llapi_layout_expected(const char *path)
908 {
909         struct llapi_layout     *path_layout = NULL;
910         char                    donor_path[PATH_MAX];
911         struct stat st;
912         int fd;
913         int rc;
914
915         fd = open(path, O_RDONLY);
916         if (fd < 0 && errno != ENOENT)
917                 return NULL;
918
919         if (fd >= 0) {
920                 int tmp;
921
922                 path_layout = llapi_layout_get_by_fd(fd, 0);
923                 tmp = errno;
924                 close(fd);
925                 errno = tmp;
926         }
927
928         if (path_layout == NULL) {
929                 if (errno != ENODATA && errno != ENOENT)
930                         return NULL;
931
932                 path_layout = llapi_layout_alloc();
933                 if (path_layout == NULL)
934                         return NULL;
935         }
936
937         if (is_any_specified(path_layout)) {
938                 inherit_sys_attributes(path_layout, path);
939                 return path_layout;
940         }
941
942         llapi_layout_free(path_layout);
943
944         rc = stat(path, &st);
945         if (rc < 0 && errno != ENOENT)
946                 return NULL;
947
948         /* If path is a not a directory or doesn't exist, inherit layout
949          * from parent directory. */
950         if ((rc == 0 && !S_ISDIR(st.st_mode)) ||
951             (rc < 0 && errno == ENOENT)) {
952                 get_parent_dir(path, donor_path, sizeof(donor_path));
953                 path_layout = llapi_layout_get_by_path(donor_path, 0);
954                 if (path_layout != NULL) {
955                         if (is_any_specified(path_layout)) {
956                                 inherit_sys_attributes(path_layout, donor_path);
957                                 return path_layout;
958                         }
959                         llapi_layout_free(path_layout);
960                 }
961         }
962
963         /* Inherit layout from the filesystem root. */
964         rc = llapi_search_mounts(path, 0, donor_path, NULL);
965         if (rc < 0)
966                 return NULL;
967         path_layout = llapi_layout_get_by_path(donor_path, 0);
968         if (path_layout == NULL)
969                 return NULL;
970
971         inherit_sys_attributes(path_layout, donor_path);
972         return path_layout;
973 }
974
975 /**
976  * Get the striping layout for the file at \a path.
977  *
978  * If \a flags contains LAYOUT_GET_EXPECTED, substitute
979  * expected inherited attribute values for unspecified attributes. See
980  * llapi_layout_expected().
981  *
982  * \param[in] path      path for which to get the layout
983  * \param[in] flags     flags to control how layout is retrieved
984  *
985  * \retval      valid llapi_layout pointer on success
986  * \retval      NULL if an error occurs
987  */
988 struct llapi_layout *llapi_layout_get_by_path(const char *path, uint32_t flags)
989 {
990         struct llapi_layout *layout = NULL;
991         int fd;
992         int tmp;
993
994         if (flags & LAYOUT_GET_EXPECTED)
995                 return llapi_layout_expected(path);
996
997         fd = open(path, O_RDONLY);
998         if (fd < 0)
999                 return layout;
1000
1001         layout = llapi_layout_get_by_fd(fd, flags);
1002         tmp = errno;
1003         close(fd);
1004         errno = tmp;
1005
1006         return layout;
1007 }
1008
1009 /**
1010  * Get the layout for the file with FID \a fidstr in filesystem \a lustre_dir.
1011  *
1012  * \param[in] lustre_dir        path within Lustre filesystem containing \a fid
1013  * \param[in] fid               Lustre identifier of file to get layout for
1014  *
1015  * \retval      valid llapi_layout pointer on success
1016  * \retval      NULL if an error occurs
1017  */
1018 struct llapi_layout *llapi_layout_get_by_fid(const char *lustre_dir,
1019                                              const struct lu_fid *fid,
1020                                              uint32_t flags)
1021 {
1022         int fd;
1023         int tmp;
1024         int saved_msg_level = llapi_msg_get_level();
1025         struct llapi_layout *layout = NULL;
1026
1027         /* Prevent llapi internal routines from writing to console
1028          * while executing this function, then restore previous message
1029          * level. */
1030         llapi_msg_set_level(LLAPI_MSG_OFF);
1031         fd = llapi_open_by_fid(lustre_dir, fid, O_RDONLY);
1032         llapi_msg_set_level(saved_msg_level);
1033
1034         if (fd < 0)
1035                 return NULL;
1036
1037         layout = llapi_layout_get_by_fd(fd, flags);
1038         tmp = errno;
1039         close(fd);
1040         errno = tmp;
1041
1042         return layout;
1043 }
1044
1045 /**
1046  * Get the stripe count of \a layout.
1047  *
1048  * \param[in] layout    layout to get stripe count from
1049  * \param[out] count    integer to store stripe count in
1050  *
1051  * \retval      0 on success
1052  * \retval      -1 if arguments are invalid
1053  */
1054 int llapi_layout_stripe_count_get(const struct llapi_layout *layout,
1055                                   uint64_t *count)
1056 {
1057         struct llapi_layout_comp *comp;
1058
1059         comp = __llapi_layout_cur_comp(layout);
1060         if (comp == NULL)
1061                 return -1;
1062
1063         if (count == NULL) {
1064                 errno = EINVAL;
1065                 return -1;
1066         }
1067
1068         *count = comp->llc_stripe_count;
1069
1070         return 0;
1071 }
1072
1073 /*
1074  * The llapi_layout API functions have these extra validity checks since
1075  * they use intuitively named macros to denote special behavior, whereas
1076  * the old API uses 0 and -1.
1077  */
1078
1079 static bool llapi_layout_stripe_count_is_valid(int64_t stripe_count)
1080 {
1081         return stripe_count == LLAPI_LAYOUT_DEFAULT ||
1082                 stripe_count == LLAPI_LAYOUT_WIDE ||
1083                 (stripe_count != 0 && stripe_count != -1 &&
1084                  llapi_stripe_count_is_valid(stripe_count));
1085 }
1086
1087 static bool llapi_layout_stripe_size_is_valid(uint64_t stripe_size)
1088 {
1089         return stripe_size == LLAPI_LAYOUT_DEFAULT ||
1090                 (stripe_size != 0 &&
1091                  llapi_stripe_size_is_aligned(stripe_size) &&
1092                  !llapi_stripe_size_is_too_big(stripe_size));
1093 }
1094
1095 static bool llapi_layout_stripe_index_is_valid(int64_t stripe_index)
1096 {
1097         return stripe_index == LLAPI_LAYOUT_DEFAULT ||
1098                 (stripe_index >= 0 &&
1099                 llapi_stripe_index_is_valid(stripe_index));
1100 }
1101
1102 /**
1103  * Set the stripe count of \a layout.
1104  *
1105  * \param[in] layout    layout to set stripe count in
1106  * \param[in] count     value to be set
1107  *
1108  * \retval      0 on success
1109  * \retval      -1 if arguments are invalid
1110  */
1111 int llapi_layout_stripe_count_set(struct llapi_layout *layout,
1112                                   uint64_t count)
1113 {
1114         struct llapi_layout_comp *comp;
1115
1116         comp = __llapi_layout_cur_comp(layout);
1117         if (comp == NULL)
1118                 return -1;
1119
1120         if (!llapi_layout_stripe_count_is_valid(count)) {
1121                 errno = EINVAL;
1122                 return -1;
1123         }
1124
1125         comp->llc_stripe_count = count;
1126
1127         return 0;
1128 }
1129
1130 /**
1131  * Get the stripe size of \a layout.
1132  *
1133  * \param[in] layout    layout to get stripe size from
1134  * \param[out] size     integer to store stripe size in
1135  *
1136  * \retval      0 on success
1137  * \retval      -1 if arguments are invalid
1138  */
1139 int llapi_layout_stripe_size_get(const struct llapi_layout *layout,
1140                                  uint64_t *size)
1141 {
1142         struct llapi_layout_comp *comp;
1143
1144         comp = __llapi_layout_cur_comp(layout);
1145         if (comp == NULL)
1146                 return -1;
1147
1148         if (size == NULL) {
1149                 errno = EINVAL;
1150                 return -1;
1151         }
1152
1153         *size = comp->llc_stripe_size;
1154
1155         return 0;
1156 }
1157
1158 /**
1159  * Set the stripe size of \a layout.
1160  *
1161  * \param[in] layout    layout to set stripe size in
1162  * \param[in] size      value to be set
1163  *
1164  * \retval      0 on success
1165  * \retval      -1 if arguments are invalid
1166  */
1167 int llapi_layout_stripe_size_set(struct llapi_layout *layout,
1168                                  uint64_t size)
1169 {
1170         struct llapi_layout_comp *comp;
1171
1172         comp = __llapi_layout_cur_comp(layout);
1173         if (comp == NULL)
1174                 return -1;
1175
1176         if (!llapi_layout_stripe_size_is_valid(size)) {
1177                 errno = EINVAL;
1178                 return -1;
1179         }
1180
1181         comp->llc_stripe_size = size;
1182
1183         return 0;
1184 }
1185
1186 /**
1187  * Get the RAID pattern of \a layout.
1188  *
1189  * \param[in] layout    layout to get pattern from
1190  * \param[out] pattern  integer to store pattern in
1191  *
1192  * \retval      0 on success
1193  * \retval      -1 if arguments are invalid
1194  */
1195 int llapi_layout_pattern_get(const struct llapi_layout *layout,
1196                              uint64_t *pattern)
1197 {
1198         struct llapi_layout_comp *comp;
1199
1200         comp = __llapi_layout_cur_comp(layout);
1201         if (comp == NULL)
1202                 return -1;
1203
1204         if (pattern == NULL) {
1205                 errno = EINVAL;
1206                 return -1;
1207         }
1208
1209         *pattern = comp->llc_pattern;
1210
1211         return 0;
1212 }
1213
1214 /**
1215  * Set the pattern of \a layout.
1216  *
1217  * \param[in] layout    layout to set pattern in
1218  * \param[in] pattern   value to be set
1219  *
1220  * \retval      0 on success
1221  * \retval      -1 if arguments are invalid or RAID pattern
1222  *              is unsupported
1223  */
1224 int llapi_layout_pattern_set(struct llapi_layout *layout, uint64_t pattern)
1225 {
1226         struct llapi_layout_comp *comp;
1227
1228         comp = __llapi_layout_cur_comp(layout);
1229         if (comp == NULL)
1230                 return -1;
1231
1232         if (pattern != LLAPI_LAYOUT_DEFAULT &&
1233             pattern != LLAPI_LAYOUT_RAID0 && pattern != LLAPI_LAYOUT_MDT) {
1234                 errno = EOPNOTSUPP;
1235                 return -1;
1236         }
1237
1238         comp->llc_pattern = pattern |
1239                             (comp->llc_pattern & LLAPI_LAYOUT_SPECIFIC);
1240
1241         return 0;
1242 }
1243
1244 static inline int stripe_number_roundup(int stripe_number)
1245 {
1246         unsigned int round_up = (stripe_number + 8) & ~7;
1247         return round_up > LOV_MAX_STRIPE_COUNT ?
1248                 LOV_MAX_STRIPE_COUNT : round_up;
1249 }
1250
1251 /**
1252  * Set the OST index of stripe number \a stripe_number to \a ost_index.
1253  *
1254  * If only the starting stripe's OST index is specified, then this can use
1255  * the normal LOV_MAGIC_{V1,V3} layout type.  If multiple OST indices are
1256  * given, then allocate an array to hold the list of indices and ensure that
1257  * the LOV_USER_MAGIC_SPECIFIC layout is used when creating the file.
1258  *
1259  * \param[in] layout            layout to set OST index in
1260  * \param[in] stripe_number     stripe number to set index for
1261  * \param[in] ost_index         the index to set
1262  *
1263  * \retval      0 on success
1264  * \retval      -1 if arguments are invalid or an unsupported stripe number
1265  *              was specified, error returned in errno
1266  */
1267 int llapi_layout_ost_index_set(struct llapi_layout *layout, int stripe_number,
1268                                uint64_t ost_index)
1269 {
1270         struct llapi_layout_comp *comp;
1271
1272         comp = __llapi_layout_cur_comp(layout);
1273         if (comp == NULL)
1274                 return -1;
1275
1276         if (!llapi_layout_stripe_index_is_valid(ost_index)) {
1277                 errno = EINVAL;
1278                 return -1;
1279         }
1280
1281         if (stripe_number == 0 && ost_index == LLAPI_LAYOUT_DEFAULT) {
1282                 comp->llc_stripe_offset = ost_index;
1283                 comp->llc_pattern &= ~LLAPI_LAYOUT_SPECIFIC;
1284                 __llapi_comp_objects_realloc(comp, 0);
1285         } else if (stripe_number >= 0 &&
1286                    stripe_number < LOV_MAX_STRIPE_COUNT) {
1287                 if (ost_index >= LLAPI_LAYOUT_IDX_MAX) {
1288                         errno = EINVAL;
1289                         return -1;
1290                 }
1291
1292                 /* Preallocate a few more stripes to avoid realloc() overhead.*/
1293                 if (__llapi_comp_objects_realloc(comp,
1294                                 stripe_number_roundup(stripe_number)) < 0)
1295                         return -1;
1296
1297                 comp->llc_objects[stripe_number].l_ost_idx = ost_index;
1298
1299                 if (stripe_number == 0)
1300                         comp->llc_stripe_offset = ost_index;
1301                 else
1302                         comp->llc_pattern |= LLAPI_LAYOUT_SPECIFIC;
1303
1304                 if (comp->llc_stripe_count == LLAPI_LAYOUT_DEFAULT ||
1305                     comp->llc_stripe_count <= stripe_number)
1306                         comp->llc_stripe_count = stripe_number + 1;
1307         } else {
1308                 errno = EINVAL;
1309                 return -1;
1310         }
1311
1312         return 0;
1313 }
1314
1315 /**
1316  * Get the OST index associated with stripe \a stripe_number.
1317  *
1318  * Stripes are indexed starting from zero.
1319  *
1320  * \param[in] layout            layout to get index from
1321  * \param[in] stripe_number     stripe number to get index for
1322  * \param[out] index            integer to store index in
1323  *
1324  * \retval      0 on success
1325  * \retval      -1 if arguments are invalid
1326  */
1327 int llapi_layout_ost_index_get(const struct llapi_layout *layout,
1328                                uint64_t stripe_number, uint64_t *index)
1329 {
1330         struct llapi_layout_comp *comp;
1331
1332         comp = __llapi_layout_cur_comp(layout);
1333         if (comp == NULL)
1334                 return -1;
1335
1336         if (index == NULL) {
1337                 errno = EINVAL;
1338                 return -1;
1339         }
1340
1341         if (stripe_number >= comp->llc_stripe_count ||
1342             stripe_number >= comp->llc_objects_count) {
1343                 errno = EINVAL;
1344                 return -1;
1345         }
1346
1347         if (comp->llc_stripe_offset == LLAPI_LAYOUT_DEFAULT)
1348                 *index = LLAPI_LAYOUT_DEFAULT;
1349         else
1350                 *index = comp->llc_objects[stripe_number].l_ost_idx;
1351
1352         return 0;
1353 }
1354
1355 /**
1356  *
1357  * Get the pool name of layout \a layout.
1358  *
1359  * \param[in] layout    layout to get pool name from
1360  * \param[out] dest     buffer to store pool name in
1361  * \param[in] n         size in bytes of buffer \a dest
1362  *
1363  * \retval      0 on success
1364  * \retval      -1 if arguments are invalid
1365  */
1366 int llapi_layout_pool_name_get(const struct llapi_layout *layout, char *dest,
1367                                size_t n)
1368 {
1369         struct llapi_layout_comp *comp;
1370
1371         comp = __llapi_layout_cur_comp(layout);
1372         if (comp == NULL)
1373                 return -1;
1374
1375         if (dest == NULL) {
1376                 errno = EINVAL;
1377                 return -1;
1378         }
1379
1380         strncpy(dest, comp->llc_pool_name, n);
1381
1382         return 0;
1383 }
1384
1385 /**
1386  * Set the name of the pool of layout \a layout.
1387  *
1388  * \param[in] layout    layout to set pool name in
1389  * \param[in] pool_name pool name to set
1390  *
1391  * \retval      0 on success
1392  * \retval      -1 if arguments are invalid or pool name is too long
1393  */
1394 int llapi_layout_pool_name_set(struct llapi_layout *layout,
1395                                const char *pool_name)
1396 {
1397         struct llapi_layout_comp *comp;
1398         char *ptr;
1399
1400         comp = __llapi_layout_cur_comp(layout);
1401         if (comp == NULL)
1402                 return -1;
1403
1404         if (pool_name == NULL) {
1405                 errno = EINVAL;
1406                 return -1;
1407         }
1408
1409         /* Strip off any 'fsname.' portion. */
1410         ptr = strchr(pool_name, '.');
1411         if (ptr != NULL)
1412                 pool_name = ptr + 1;
1413
1414         if (strlen(pool_name) > LOV_MAXPOOLNAME) {
1415                 errno = EINVAL;
1416                 return -1;
1417         }
1418
1419         strncpy(comp->llc_pool_name, pool_name, sizeof(comp->llc_pool_name));
1420
1421         return 0;
1422 }
1423
1424 /**
1425  * Open and possibly create a file with a given \a layout.
1426  *
1427  * If \a layout is NULL this function acts as a simple wrapper for
1428  * open().  By convention, ENOTTY is returned in errno if \a path
1429  * refers to a non-Lustre file.
1430  *
1431  * \param[in] path              name of the file to open
1432  * \param[in] open_flags        open() flags
1433  * \param[in] mode              permissions to create new file with
1434  * \param[in] layout            layout to create new file with
1435  *
1436  * \retval              non-negative file descriptor on successful open
1437  * \retval              -1 if an error occurred
1438  */
1439 int llapi_layout_file_open(const char *path, int open_flags, mode_t mode,
1440                            const struct llapi_layout *layout)
1441 {
1442         int fd;
1443         int rc;
1444         int tmp;
1445         struct lov_user_md *lum;
1446         size_t lum_size;
1447
1448         if (path == NULL ||
1449             (layout != NULL && layout->llot_magic != LLAPI_LAYOUT_MAGIC)) {
1450                 errno = EINVAL;
1451                 return -1;
1452         }
1453
1454         /* Object creation must be postponed until after layout attributes
1455          * have been applied. */
1456         if (layout != NULL && (open_flags & O_CREAT))
1457                 open_flags |= O_LOV_DELAY_CREATE;
1458
1459         fd = open(path, open_flags, mode);
1460
1461         if (layout == NULL || fd < 0)
1462                 return fd;
1463
1464         lum = llapi_layout_to_lum(layout);
1465
1466         if (lum == NULL) {
1467                 tmp = errno;
1468                 close(fd);
1469                 errno = tmp;
1470                 return -1;
1471         }
1472
1473         if (lum->lmm_magic == LOV_USER_MAGIC_COMP_V1)
1474                 lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
1475         else if (lum->lmm_magic == LOV_USER_MAGIC_SPECIFIC)
1476                 lum_size = lov_user_md_size(lum->lmm_stripe_count,
1477                                             lum->lmm_magic);
1478         else
1479                 lum_size = lov_user_md_size(0, lum->lmm_magic);
1480
1481         rc = fsetxattr(fd, XATTR_LUSTRE_LOV, lum, lum_size, 0);
1482         if (rc < 0) {
1483                 tmp = errno;
1484                 close(fd);
1485                 errno = tmp;
1486                 fd = -1;
1487         }
1488
1489         free(lum);
1490         errno = errno == EOPNOTSUPP ? ENOTTY : errno;
1491
1492         return fd;
1493 }
1494
1495 /**
1496  * Create a file with a given \a layout.
1497  *
1498  * Force O_CREAT and O_EXCL flags on so caller is assured that file was
1499  * created with the given \a layout on successful function return.
1500  *
1501  * \param[in] path              name of the file to open
1502  * \param[in] open_flags        open() flags
1503  * \param[in] mode              permissions to create new file with
1504  * \param[in] layout            layout to create new file with
1505  *
1506  * \retval              non-negative file descriptor on successful open
1507  * \retval              -1 if an error occurred
1508  */
1509 int llapi_layout_file_create(const char *path, int open_flags, int mode,
1510                              const struct llapi_layout *layout)
1511 {
1512         return llapi_layout_file_open(path, open_flags|O_CREAT|O_EXCL, mode,
1513                                       layout);
1514 }
1515
1516 int llapi_layout_flags_get(struct llapi_layout *layout, uint32_t *flags)
1517 {
1518         if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1519                 errno = EINVAL;
1520                 return -1;
1521         }
1522
1523         *flags = layout->llot_flags;
1524         return 0;
1525 }
1526
1527 /**
1528  * Set flags to the header of a component layout.
1529  */
1530 int llapi_layout_flags_set(struct llapi_layout *layout, uint32_t flags)
1531 {
1532         if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1533                 errno = EINVAL;
1534                 return -1;
1535         }
1536
1537         layout->llot_flags = flags;
1538         return 0;
1539 }
1540
1541 /**
1542  * llapi_layout_mirror_count_is_valid() - Check the validity of mirror count.
1543  * @count: Mirror count value to be checked.
1544  *
1545  * This function checks the validity of mirror count.
1546  *
1547  * Return: true on success or false on failure.
1548  */
1549 static bool llapi_layout_mirror_count_is_valid(uint16_t count)
1550 {
1551         return count >= 0 && count <= LUSTRE_MIRROR_COUNT_MAX;
1552 }
1553
1554 /**
1555  * llapi_layout_mirror_count_get() - Get mirror count from the header of
1556  *                                   a layout.
1557  * @layout: Layout to get mirror count from.
1558  * @count:  Returned mirror count value.
1559  *
1560  * This function gets mirror count from the header of a layout.
1561  *
1562  * Return: 0 on success or -1 on failure.
1563  */
1564 int llapi_layout_mirror_count_get(struct llapi_layout *layout,
1565                                   uint16_t *count)
1566 {
1567         if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1568                 errno = EINVAL;
1569                 return -1;
1570         }
1571
1572         *count = layout->llot_mirror_count;
1573         return 0;
1574 }
1575
1576 /**
1577  * llapi_layout_mirror_count_set() - Set mirror count to the header of a layout.
1578  * @layout: Layout to set mirror count in.
1579  * @count:  Mirror count value to be set.
1580  *
1581  * This function sets mirror count to the header of a layout.
1582  *
1583  * Return: 0 on success or -1 on failure.
1584  */
1585 int llapi_layout_mirror_count_set(struct llapi_layout *layout,
1586                                   uint16_t count)
1587 {
1588         if (layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
1589                 errno = EINVAL;
1590                 return -1;
1591         }
1592
1593         if (!llapi_layout_mirror_count_is_valid(count)) {
1594                 errno = EINVAL;
1595                 return -1;
1596         }
1597
1598         layout->llot_mirror_count = count;
1599         return 0;
1600 }
1601
1602 /**
1603  * Fetch the start and end offset of the current layout component.
1604  *
1605  * \param[in] layout    the layout component
1606  * \param[out] start    extent start, inclusive
1607  * \param[out] end      extent end, exclusive
1608  *
1609  * \retval      0 on success
1610  * \retval      <0 if error occurs
1611  */
1612 int llapi_layout_comp_extent_get(const struct llapi_layout *layout,
1613                                  uint64_t *start, uint64_t *end)
1614 {
1615         struct llapi_layout_comp *comp;
1616
1617         comp = __llapi_layout_cur_comp(layout);
1618         if (comp == NULL)
1619                 return -1;
1620
1621         if (start == NULL || end == NULL) {
1622                 errno = EINVAL;
1623                 return -1;
1624         }
1625
1626         *start = comp->llc_extent.e_start;
1627         *end = comp->llc_extent.e_end;
1628
1629         return 0;
1630 }
1631
1632 /**
1633  * Set the layout extent of a layout.
1634  *
1635  * \param[in] layout    the layout to be set
1636  * \param[in] start     extent start, inclusive
1637  * \param[in] end       extent end, exclusive
1638  *
1639  * \retval      0 on success
1640  * \retval      <0 if error occurs
1641  */
1642 int llapi_layout_comp_extent_set(struct llapi_layout *layout,
1643                                  uint64_t start, uint64_t end)
1644 {
1645         struct llapi_layout_comp *prev, *next, *comp;
1646
1647         comp = __llapi_layout_cur_comp(layout);
1648         if (comp == NULL)
1649                 return -1;
1650
1651         if (start >= end) {
1652                 errno = EINVAL;
1653                 return -1;
1654         }
1655
1656         /*
1657          * We need to make sure the extent to be set is valid: the new
1658          * extent must be adjacent with the prev & next component.
1659          */
1660         if (comp->llc_list.prev != &layout->llot_comp_list) {
1661                 prev = list_entry(comp->llc_list.prev, typeof(*prev),
1662                                   llc_list);
1663                 if (start != prev->llc_extent.e_end) {
1664                         errno = EINVAL;
1665                         return -1;
1666                 }
1667         }
1668
1669         if (comp->llc_list.next != &layout->llot_comp_list) {
1670                 next = list_entry(comp->llc_list.next, typeof(*next),
1671                                   llc_list);
1672                 if (end != next->llc_extent.e_start) {
1673                         errno = EINVAL;
1674                         return -1;
1675                 }
1676         }
1677
1678         comp->llc_extent.e_start = start;
1679         comp->llc_extent.e_end = end;
1680         layout->llot_is_composite = true;
1681
1682         return 0;
1683 }
1684
1685 /**
1686  * Gets the attribute flags of the current component.
1687  *
1688  * \param[in] layout    the layout component
1689  * \param[out] flags    stored the returned component flags
1690  *
1691  * \retval      0 on success
1692  * \retval      <0 if error occurs
1693  */
1694 int llapi_layout_comp_flags_get(const struct llapi_layout *layout,
1695                                 uint32_t *flags)
1696 {
1697         struct llapi_layout_comp *comp;
1698
1699         comp = __llapi_layout_cur_comp(layout);
1700         if (comp == NULL)
1701                 return -1;
1702
1703         if (flags == NULL) {
1704                 errno = EINVAL;
1705                 return -1;
1706         }
1707
1708         *flags = comp->llc_flags;
1709
1710         return 0;
1711 }
1712
1713 /**
1714  * Sets the specified flags of the current component leaving other flags as-is.
1715  *
1716  * \param[in] layout    the layout component
1717  * \param[in] flags     component flags to be set
1718  *
1719  * \retval      0 on success
1720  * \retval      <0 if error occurs
1721  */
1722 int llapi_layout_comp_flags_set(struct llapi_layout *layout, uint32_t flags)
1723 {
1724         struct llapi_layout_comp *comp;
1725
1726         comp = __llapi_layout_cur_comp(layout);
1727         if (comp == NULL)
1728                 return -1;
1729
1730         comp->llc_flags |= flags;
1731
1732         return 0;
1733 }
1734
1735 /**
1736  * Clears the flags specified in the flags leaving other flags as-is.
1737  *
1738  * \param[in] layout    the layout component
1739  * \param[in] flags     component flags to be cleared
1740  *
1741  * \retval      0 on success
1742  * \retval      <0 if error occurs
1743  */
1744 int llapi_layout_comp_flags_clear(struct llapi_layout *layout,
1745                                   uint32_t flags)
1746 {
1747         struct llapi_layout_comp *comp;
1748
1749         comp = __llapi_layout_cur_comp(layout);
1750         if (comp == NULL)
1751                 return -1;
1752
1753         comp->llc_flags &= ~flags;
1754
1755         return 0;
1756 }
1757
1758 /**
1759  * Fetches the file-unique component ID of the current layout component.
1760  *
1761  * \param[in] layout    the layout component
1762  * \param[out] id       stored the returned component ID
1763  *
1764  * \retval      0 on success
1765  * \retval      <0 if error occurs
1766  */
1767 int llapi_layout_comp_id_get(const struct llapi_layout *layout, uint32_t *id)
1768 {
1769         struct llapi_layout_comp *comp;
1770
1771         comp = __llapi_layout_cur_comp(layout);
1772         if (comp == NULL)
1773                 return -1;
1774
1775         if (id == NULL) {
1776                 errno = EINVAL;
1777                 return -1;
1778         }
1779         *id = comp->llc_id;
1780
1781         return 0;
1782 }
1783
1784 /**
1785  * Return the mirror id of the current layout component.
1786  *
1787  * \param[in] layout    the layout component
1788  * \param[out] id       stored the returned mirror ID
1789  *
1790  * \retval      0 on success
1791  * \retval      <0 if error occurs
1792  */
1793 int llapi_layout_mirror_id_get(const struct llapi_layout *layout, uint32_t *id)
1794 {
1795         struct llapi_layout_comp *comp;
1796
1797         comp = __llapi_layout_cur_comp(layout);
1798         if (comp == NULL)
1799                 return -1;
1800
1801         if (id == NULL) {
1802                 errno = EINVAL;
1803                 return -1;
1804         }
1805
1806         *id = mirror_id_of(comp->llc_id);
1807
1808         return 0;
1809 }
1810
1811 /**
1812  * Adds a component to \a layout, the new component will be added to
1813  * the tail of components list and it'll inherit attributes of existing
1814  * ones. The \a layout will change it's current component pointer to
1815  * the newly added component, and it'll be turned into a composite
1816  * layout if it was not before the adding.
1817  *
1818  * \param[in] layout    existing composite or plain layout
1819  *
1820  * \retval      0 on success
1821  * \retval      <0 if error occurs
1822  */
1823 int llapi_layout_comp_add(struct llapi_layout *layout)
1824 {
1825         struct llapi_layout_comp *last, *comp, *new;
1826
1827         comp = __llapi_layout_cur_comp(layout);
1828         if (comp == NULL)
1829                 return -1;
1830
1831         new = __llapi_comp_alloc(0);
1832         if (new == NULL)
1833                 return -1;
1834
1835         last = list_entry(layout->llot_comp_list.prev, typeof(*last),
1836                           llc_list);
1837
1838         if (new->llc_extent.e_end <= last->llc_extent.e_end) {
1839                 __llapi_comp_free(new);
1840                 errno = EINVAL;
1841                 return -1;
1842         }
1843         new->llc_extent.e_start = last->llc_extent.e_end;
1844
1845         list_add_tail(&new->llc_list, &layout->llot_comp_list);
1846         layout->llot_cur_comp = new;
1847         layout->llot_is_composite = true;
1848
1849         return 0;
1850 }
1851
1852 /**
1853  * Deletes current component from the composite layout. The component
1854  * to be deleted must be the tail of components list, and it can't be
1855  * the only component in the layout.
1856  *
1857  * \param[in] layout    composite layout
1858  *
1859  * \retval      0 on success
1860  * \retval      <0 if error occurs
1861  */
1862 int llapi_layout_comp_del(struct llapi_layout *layout)
1863 {
1864         struct llapi_layout_comp *comp;
1865
1866         comp = __llapi_layout_cur_comp(layout);
1867         if (comp == NULL)
1868                 return -1;
1869
1870         if (!layout->llot_is_composite) {
1871                 errno = EINVAL;
1872                 return -1;
1873         }
1874
1875         /* It must be the tail of the list (for PFL, can be relaxed
1876          * once we get mirrored components) */
1877         if (comp->llc_list.next != &layout->llot_comp_list) {
1878                 errno = EINVAL;
1879                 return -1;
1880         }
1881         /* It can't be the only one on the list */
1882         if (comp->llc_list.prev == &layout->llot_comp_list) {
1883                 errno = EINVAL;
1884                 return -1;
1885         }
1886
1887         layout->llot_cur_comp =
1888                 list_entry(comp->llc_list.prev, typeof(*comp), llc_list);
1889         list_del_init(&comp->llc_list);
1890         __llapi_comp_free(comp);
1891
1892         return 0;
1893 }
1894
1895 /**
1896  * Move the current component pointer to the component with
1897  * specified component ID.
1898  *
1899  * \param[in] layout    composite layout
1900  * \param[in] id        component ID
1901  *
1902  * \retval      =0 : moved successfully
1903  * \retval      <0 if error occurs
1904  */
1905 int llapi_layout_comp_use_id(struct llapi_layout *layout, uint32_t comp_id)
1906 {
1907         struct llapi_layout_comp *comp;
1908
1909         comp = __llapi_layout_cur_comp(layout);
1910         if (comp == NULL)
1911                 return -1; /* use previously set errno */
1912
1913         if (!layout->llot_is_composite) {
1914                 errno = EINVAL;
1915                 return -1;
1916         }
1917
1918         if (comp_id == LCME_ID_INVAL) {
1919                 errno = EINVAL;
1920                 return -1;
1921         }
1922
1923         list_for_each_entry(comp, &layout->llot_comp_list, llc_list) {
1924                 if (comp->llc_id == comp_id) {
1925                         layout->llot_cur_comp = comp;
1926                         return 0;
1927                 }
1928         }
1929         errno = ENOENT;
1930         return -1;
1931 }
1932
1933 /**
1934  * Move the current component pointer to a specified position.
1935  *
1936  * \param[in] layout    composite layout
1937  * \param[in] pos       the position to be moved, it can be:
1938  *                      LLAPI_LAYOUT_COMP_USE_FIRST: use first component
1939  *                      LLAPI_LAYOUT_COMP_USE_LAST: use last component
1940  *                      LLAPI_LAYOUT_COMP_USE_NEXT: use component after current
1941  *                      LLAPI_LAYOUT_COMP_USE_PREV: use component before current
1942  *
1943  * \retval      =0 : moved successfully
1944  * \retval      =1 : at last component with NEXT, at first component with PREV
1945  * \retval      <0 if error occurs
1946  */
1947 int llapi_layout_comp_use(struct llapi_layout *layout,
1948                           enum llapi_layout_comp_use pos)
1949 {
1950         struct llapi_layout_comp *comp, *head, *tail;
1951
1952         comp = __llapi_layout_cur_comp(layout);
1953         if (comp == NULL)
1954                 return -1;
1955
1956         if (!layout->llot_is_composite) {
1957                 if (pos == LLAPI_LAYOUT_COMP_USE_FIRST ||
1958                     pos == LLAPI_LAYOUT_COMP_USE_LAST)
1959                         return 0;
1960                 errno = ENOENT;
1961                 return 1;
1962         }
1963
1964         head = list_entry(layout->llot_comp_list.next, typeof(*head), llc_list);
1965         tail = list_entry(layout->llot_comp_list.prev, typeof(*tail), llc_list);
1966         switch (pos) {
1967         case LLAPI_LAYOUT_COMP_USE_FIRST:
1968                 layout->llot_cur_comp = head;
1969                 break;
1970         case LLAPI_LAYOUT_COMP_USE_NEXT:
1971                 if (comp == tail) {
1972                         errno = ENOENT;
1973                         return 1;
1974                 }
1975                 layout->llot_cur_comp = list_entry(comp->llc_list.next,
1976                                                    typeof(*comp), llc_list);
1977                 break;
1978         case LLAPI_LAYOUT_COMP_USE_LAST:
1979                 layout->llot_cur_comp = tail;
1980                 break;
1981         case LLAPI_LAYOUT_COMP_USE_PREV:
1982                 if (comp == head) {
1983                         errno = ENOENT;
1984                         return 1;
1985                 }
1986                 layout->llot_cur_comp = list_entry(comp->llc_list.prev,
1987                                                    typeof(*comp), llc_list);
1988                 break;
1989         default:
1990                 errno = EINVAL;
1991                 return -1;
1992         }
1993
1994         return 0;
1995 }
1996
1997 /**
1998  * Add layout component(s) to an existing file.
1999  *
2000  * \param[in] path      The path name of the file
2001  * \param[in] layout    The layout component(s) to be added
2002  */
2003 int llapi_layout_file_comp_add(const char *path,
2004                                const struct llapi_layout *layout)
2005 {
2006         int rc, fd, lum_size, tmp_errno = 0;
2007         struct lov_user_md *lum;
2008
2009         if (path == NULL || layout == NULL ||
2010             layout->llot_magic != LLAPI_LAYOUT_MAGIC) {
2011                 errno = EINVAL;
2012                 return -1;
2013         }
2014
2015         lum = llapi_layout_to_lum(layout);
2016         if (lum == NULL)
2017                 return -1;
2018
2019         if (lum->lmm_magic != LOV_USER_MAGIC_COMP_V1) {
2020                 free(lum);
2021                 errno = EINVAL;
2022                 return -1;
2023         }
2024         lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
2025
2026         fd = open(path, O_RDWR);
2027         if (fd < 0) {
2028                 tmp_errno = errno;
2029                 rc = -1;
2030                 goto out;
2031         }
2032
2033         rc = fsetxattr(fd, XATTR_LUSTRE_LOV".add", lum, lum_size, 0);
2034         if (rc < 0) {
2035                 tmp_errno = errno;
2036                 close(fd);
2037                 rc = -1;
2038                 goto out;
2039         }
2040         close(fd);
2041 out:
2042         free(lum);
2043         errno = tmp_errno;
2044         return rc;
2045 }
2046
2047 /**
2048  * Delete component(s) by the specified component id or component flags
2049  * from an existing file.
2050  *
2051  * \param[in] path      path name of the file
2052  * \param[in] id        unique component ID
2053  * \param[in] flags     flags: LCME_FL_* or;
2054  *                      negative flags: (LCME_FL_NEG|LCME_FL_*)
2055  */
2056 int llapi_layout_file_comp_del(const char *path, uint32_t id, uint32_t flags)
2057 {
2058         int rc, fd, lum_size;
2059         struct llapi_layout *layout;
2060         struct llapi_layout_comp *comp;
2061         struct lov_user_md *lum;
2062
2063         if (path == NULL || id > LCME_ID_MAX || (flags & ~LCME_KNOWN_FLAGS)) {
2064                 errno = EINVAL;
2065                 return -1;
2066         }
2067
2068         /* Can only specify ID or flags, not both. */
2069         if (id != 0 && flags != 0) {
2070                 errno = EINVAL;
2071                 return -1;
2072         }
2073
2074         layout = llapi_layout_alloc();
2075         if (layout == NULL)
2076                 return -1;
2077
2078         llapi_layout_comp_extent_set(layout, 0, LUSTRE_EOF);
2079         comp = __llapi_layout_cur_comp(layout);
2080         if (comp == NULL) {
2081                 llapi_layout_free(layout);
2082                 return -1;
2083         }
2084
2085         comp->llc_id = id;
2086         comp->llc_flags = flags;
2087
2088         lum = llapi_layout_to_lum(layout);
2089         if (lum == NULL) {
2090                 llapi_layout_free(layout);
2091                 return -1;
2092         }
2093         lum_size = ((struct lov_comp_md_v1 *)lum)->lcm_size;
2094
2095         fd = open(path, O_RDWR);
2096         if (fd < 0) {
2097                 rc = -1;
2098                 goto out;
2099         }
2100
2101         rc = fsetxattr(fd, XATTR_LUSTRE_LOV".del", lum, lum_size, 0);
2102         if (rc < 0) {
2103                 int tmp_errno = errno;
2104                 close(fd);
2105                 errno = tmp_errno;
2106                 rc = -1;
2107                 goto out;
2108         }
2109         close(fd);
2110 out:
2111         free(lum);
2112         llapi_layout_free(layout);
2113         return rc;
2114 }
2115
2116 /**
2117  * Change flags or other parameters of the component(s) by component ID of an
2118  * existing file. The component to be modified is specified by the
2119  * comp->lcme_id value, which must be an unique component ID. The new
2120  * attributes are passed in by @comp and @valid is used to specify which
2121  * attributes in the component are going to be changed.
2122  */
2123 int llapi_layout_file_comp_set(const char *path,
2124                                const struct llapi_layout *comp,
2125                                uint32_t valid)
2126 {
2127         errno = EOPNOTSUPP;
2128         return -1;
2129 }
2130
2131 /**
2132  * Check if the file layout is composite.
2133  *
2134  * \param[in] layout    the file layout to check
2135  *
2136  * \retval true         composite
2137  * \retval false        not composite
2138  */
2139 bool llapi_layout_is_composite(struct llapi_layout *layout)
2140 {
2141         return layout->llot_is_composite;
2142 }
2143
2144 /**
2145  * llapi_layout_merge() - Merge a composite layout into another one.
2146  * @dst_layout: Destination composite layout.
2147  * @src_layout: Source composite layout.
2148  *
2149  * This function copies all of the components from @src_layout and
2150  * appends them to @dst_layout.
2151  *
2152  * Return: 0 on success or -1 on failure.
2153  */
2154 int llapi_layout_merge(struct llapi_layout **dst_layout,
2155                        const struct llapi_layout *src_layout)
2156 {
2157         struct llapi_layout *new_layout = *dst_layout;
2158         struct llapi_layout_comp *new = NULL;
2159         struct llapi_layout_comp *comp = NULL;
2160         int i = 0;
2161
2162         if (src_layout == NULL ||
2163             list_empty((struct list_head *)&src_layout->llot_comp_list))
2164                 return 0;
2165
2166         if (new_layout == NULL) {
2167                 new_layout = __llapi_layout_alloc();
2168                 if (new_layout == NULL) {
2169                         errno = ENOMEM;
2170                         return -1;
2171                 }
2172         }
2173
2174         list_for_each_entry(comp, &src_layout->llot_comp_list, llc_list) {
2175                 new = __llapi_comp_alloc(0);
2176                 if (new == NULL) {
2177                         errno = ENOMEM;
2178                         goto error;
2179                 }
2180
2181                 new->llc_pattern = comp->llc_pattern;
2182                 new->llc_stripe_size = comp->llc_stripe_size;
2183                 new->llc_stripe_count = comp->llc_stripe_count;
2184                 new->llc_stripe_offset = comp->llc_stripe_offset;
2185
2186                 if (comp->llc_pool_name[0] != '\0')
2187                         strncpy(new->llc_pool_name, comp->llc_pool_name,
2188                                 sizeof(new->llc_pool_name));
2189
2190                 for (i = 0; i < comp->llc_objects_count; i++) {
2191                         if (__llapi_comp_objects_realloc(new,
2192                             stripe_number_roundup(i)) < 0) {
2193                                 errno = EINVAL;
2194                                 __llapi_comp_free(new);
2195                                 goto error;
2196                         }
2197                         new->llc_objects[i].l_ost_idx = \
2198                                 comp->llc_objects[i].l_ost_idx;
2199                 }
2200
2201                 new->llc_objects_count = comp->llc_objects_count;
2202                 new->llc_extent.e_start = comp->llc_extent.e_start;
2203                 new->llc_extent.e_end = comp->llc_extent.e_end;
2204                 new->llc_id = comp->llc_id;
2205                 new->llc_flags = comp->llc_flags;
2206
2207                 list_add_tail(&new->llc_list, &new_layout->llot_comp_list);
2208                 new_layout->llot_cur_comp = new;
2209         }
2210         new_layout->llot_is_composite = true;
2211
2212         *dst_layout = new_layout;
2213         return 0;
2214 error:
2215         llapi_layout_free(new_layout);
2216         return -1;
2217 }
2218
2219 /**
2220  * Find all stale components.
2221  *
2222  * \param[in] layout            component layout list.
2223  * \param[out] comp             array of stale component info.
2224  * \param[in] comp_size         array size of @comp.
2225  * \param[in] mirror_ids        array of mirror id that only components
2226  *                              belonging to these mirror will be collected.
2227  * \param[in] ids_nr            number of mirror ids array.
2228  *
2229  * \retval              number of component info collected on sucess or
2230  *                      an error code on failure.
2231  */
2232 int llapi_mirror_find_stale(struct llapi_layout *layout,
2233                 struct llapi_resync_comp *comp, size_t comp_size,
2234                 __u16 *mirror_ids, int ids_nr)
2235 {
2236         int idx = 0;
2237         int rc;
2238
2239         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2240         if (rc < 0) {
2241                 fprintf(stderr, "%s: move to the first layout component: %s.\n",
2242                         __func__, strerror(errno));
2243                 goto error;
2244         }
2245
2246         while (rc == 0) {
2247                 uint32_t id;
2248                 uint32_t mirror_id;
2249                 uint32_t flags;
2250                 uint64_t start, end;
2251
2252                 rc = llapi_layout_comp_flags_get(layout, &flags);
2253                 if (rc < 0) {
2254                         fprintf(stderr, "llapi_layout_comp_flags_get: %s.\n",
2255                                 strerror(errno));
2256                         goto error;
2257                 }
2258
2259                 if (!(flags & LCME_FL_STALE))
2260                         goto next;
2261
2262                 rc = llapi_layout_mirror_id_get(layout, &mirror_id);
2263                 if (rc < 0) {
2264                         fprintf(stderr, "llapi_layout_mirror_id_get: %s.\n",
2265                                 strerror(errno));
2266                         goto error;
2267                 }
2268
2269                 /* the caller only wants stale components from specific
2270                  * mirrors */
2271                 if (ids_nr > 0) {
2272                         int j;
2273
2274                         for (j = 0; j < ids_nr; j++) {
2275                                 if (mirror_ids[j] == mirror_id)
2276                                         break;
2277                         }
2278
2279                         /* not in the specified mirror */
2280                         if (j == ids_nr)
2281                                 goto next;
2282                 }
2283
2284                 rc = llapi_layout_comp_id_get(layout, &id);
2285                 if (rc < 0) {
2286                         fprintf(stderr, "llapi_layout_comp_id_get: %s.\n",
2287                                 strerror(errno));
2288                         goto error;
2289                 }
2290
2291                 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2292                 if (rc < 0) {
2293                         fprintf(stderr, "llapi_layout_comp_extent_get: %s.\n",
2294                                 strerror(errno));
2295                         goto error;
2296                 }
2297
2298                 /* pack this component into @comp array */
2299                 comp[idx].lrc_id = id;
2300                 comp[idx].lrc_mirror_id = mirror_id;
2301                 comp[idx].lrc_start = start;
2302                 comp[idx].lrc_end = end;
2303                 idx++;
2304
2305                 if (idx >= comp_size) {
2306                         fprintf(stderr, "%s: resync_comp array too small.\n",
2307                                 __func__);
2308                         rc = -EINVAL;
2309                         goto error;
2310                 }
2311
2312         next:
2313                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2314                 if (rc < 0) {
2315                         fprintf(stderr, "%s: move to the next layout "
2316                                 "component: %s.\n", __func__, strerror(errno));
2317                         rc = -EINVAL;
2318                         goto error;
2319                 }
2320         }
2321 error:
2322         return rc < 0 ? rc : idx;
2323 }
2324
2325 /* locate @layout to a valid component covering file [file_start, file_end) */
2326 static uint32_t llapi_mirror_find(struct llapi_layout *layout,
2327                                   uint64_t file_start, uint64_t file_end,
2328                                   uint64_t *endp)
2329 {
2330         uint32_t mirror_id = 0;
2331         int rc;
2332
2333         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2334         if (rc < 0)
2335                 return rc;
2336
2337         *endp = 0;
2338         while (rc == 0) {
2339                 uint64_t start, end;
2340                 uint32_t flags, id, rid;
2341
2342                 rc = llapi_layout_comp_flags_get(layout, &flags);
2343                 if (rc < 0)
2344                         return rc;
2345
2346                 if (flags & LCME_FL_STALE)
2347                         goto next;
2348
2349                 rc = llapi_layout_mirror_id_get(layout, &rid);
2350                 if (rc < 0)
2351                         return rc;
2352
2353                 rc = llapi_layout_comp_id_get(layout, &id);
2354                 if (rc < 0)
2355                         return rc;
2356
2357                 rc = llapi_layout_comp_extent_get(layout, &start, &end);
2358                 if (rc < 0)
2359                         return rc;
2360
2361                 if (file_start >= start && file_start < end) {
2362                         if (!mirror_id)
2363                                 mirror_id = rid;
2364                         else if (mirror_id != rid || *endp != start)
2365                                 break;
2366
2367                         file_start = *endp = end;
2368                         if (end >= file_end)
2369                                 break;
2370                 }
2371
2372         next:
2373                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
2374                 if (rc < 0)
2375                         return rc;
2376         }
2377
2378         return mirror_id;
2379 }
2380
2381 ssize_t llapi_mirror_resync_one(int fd, struct llapi_layout *layout,
2382                                 uint32_t dst, uint64_t start, uint64_t end)
2383 {
2384         uint64_t mirror_end = 0;
2385         ssize_t result = 0;
2386         size_t count;
2387
2388         if (end == OBD_OBJECT_EOF)
2389                 count = OBD_OBJECT_EOF;
2390         else
2391                 count = end - start;
2392
2393         while (count > 0) {
2394                 uint32_t src;
2395                 size_t to_copy;
2396                 ssize_t copied;
2397
2398                 src = llapi_mirror_find(layout, start, end, &mirror_end);
2399                 if (src == 0) {
2400                         fprintf(stderr, "llapi_mirror_find cannot find "
2401                                 "component covering %lu.\n", start);
2402                         return -ENOENT;
2403                 }
2404
2405                 if (mirror_end == OBD_OBJECT_EOF)
2406                         to_copy = count;
2407                 else
2408                         to_copy = MIN(count, mirror_end - start);
2409
2410                 copied = llapi_mirror_copy(fd, src, dst, start, to_copy);
2411                 if (copied < 0) {
2412                         fprintf(stderr, "llapi_mirror_copy returned %zd.\n",
2413                                 copied);
2414                         return copied;
2415                 }
2416
2417                 result += copied;
2418                 if (copied < to_copy) /* end of file */
2419                         break;
2420
2421                 if (count != OBD_OBJECT_EOF)
2422                         count -= copied;
2423                 start += copied;
2424         }
2425
2426         return result;
2427 }