Whamcloud - gitweb
LU-14677 sec: migrate/extend/split on encrypted file
[fs/lustre-release.git] / lustre / utils / lfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/lfs.c
32  *
33  * Author: Peter J. Braam <braam@clusterfs.com>
34  * Author: Phil Schwan <phil@clusterfs.com>
35  * Author: Robert Read <rread@clusterfs.com>
36  */
37
38 /* for O_DIRECTORY */
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <inttypes.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <mntent.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
61 #include <fcntl.h>
62 #include <dirent.h>
63 #include <time.h>
64 #include <ctype.h>
65 #include <zlib.h>
66 #include <libgen.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
69
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <lnetconfig/cyaml.h>
79 #include "lstddef.h"
80
81 /* all functions */
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
97 #endif
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137                               __u64 migration_flags,
138                               struct llapi_stripe_param *param,
139                               struct llapi_layout *layout);
140
141 struct pool_to_id_cbdata {
142         const char *pool;
143         __u32 id;
144 };
145 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
147
148 enum setstripe_origin {
149         SO_SETSTRIPE,
150         SO_MIGRATE,
151         SO_MIGRATE_MDT,
152         SO_MIRROR_CREATE,
153         SO_MIRROR_EXTEND,
154         SO_MIRROR_SPLIT,
155         SO_MIRROR_DELETE,
156 };
157
158 static int lfs_setstripe_internal(int argc, char **argv,
159                                   enum setstripe_origin opc);
160
161 static inline int lfs_setstripe(int argc, char **argv)
162 {
163         return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
164 }
165
166 static inline int lfs_setstripe_migrate(int argc, char **argv)
167 {
168         return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
169 }
170
171 static inline int lfs_mirror_create(int argc, char **argv)
172 {
173         return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
174 }
175
176 static inline int lfs_mirror_extend(int argc, char **argv)
177 {
178         return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
179 }
180
181 static inline int lfs_mirror_split(int argc, char **argv)
182 {
183         return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
184 }
185
186 static inline int lfs_mirror_delete(int argc, char **argv)
187 {
188         return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
189 }
190
191 /* Setstripe and migrate share mostly the same parameters */
192 #define SSM_CMD_COMMON(cmd) \
193         "usage: "cmd" [--component-end|-E COMP_END]\n"                  \
194         "                 [--stripe-count|-c STRIPE_COUNT]\n"           \
195         "                 [--overstripe-count|-C STRIPE_COUNT]\n"       \
196         "                 [--stripe-index|-i START_OST_IDX]\n"          \
197         "                 [--stripe-size|-S STRIPE_SIZE]\n"             \
198         "                 [--extension-size|--ext-size|-z]\n"           \
199         "                 [--help|-h] [--layout|-L PATTERN]\n"          \
200         "                 [--mirror_count|-N[MIRROR_COUNT]]\n"          \
201         "                 [--ost|-o OST_INDICES]\n"                     \
202         "                 [--pool|-p POOL_NAME]\n"                      \
203         "                 [--yaml|-y YAML_TEMPLATE_FILE]\n"             \
204         "                 [--copy=LUSTRE_SRC]\n"
205
206 #define SSM_HELP_COMMON \
207         "\tSTRIPE_COUNT: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
208         "\t              Using -C instead of -c allows overstriping, which\n"  \
209         "\t              will place more than one stripe per OST if\n"         \
210         "\t              stripe_count is greater than the number of OSTs.\n"   \
211         "\tSTART_OST_IDX: OST index of first stripe (-1=default round robin)\n"\
212         "\tSTRIPE_SIZE:  Number of bytes on each OST (0=fs default)\n"         \
213         "\t              Optional K, M, or G suffix (for KB, MB, GB\n"         \
214         "\t              respectively).  Must be a multiple of 64KiB.\n"       \
215         "\tEXTENSION_SIZE:\n"                                                  \
216         "\t              Number of bytes the previous component is extended\n" \
217         "\t              each time. Optional K, M, or G suffix (for KB,\n"     \
218         "\t              MB, GB respectively)\n"                               \
219         "\tPOOL_NAME:    Name of OST pool to use (default none)\n"             \
220         "\tLAYOUT:       stripe pattern type: raid0, mdt (default raid0)\n"    \
221         "\tOST_INDICES:  List of OST indices, can be repeated multiple times\n"\
222         "\t              Indices be specified in a format of:\n"               \
223         "\t                -o OST_1,OST_I-OST_J,OST_N\n"                       \
224         "\t              Or:\n"                                                \
225         "\t                -o OST_1 -o OST_I-OST_J -o OST_N\n"                 \
226         "\t              If --pool is set with --ost then the OSTs\n"          \
227         "\t              must be the members of the pool.\n"                   \
228         "\tCOMP_END:     Extent end of component, start after previous end.\n" \
229         "\t              Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
230         "\t              -1 or 'eof' for max file size). Must be a multiple\n" \
231         "\t              of stripe_size and a multiple of 64KiB.\n"            \
232         "\tYAML_TEMPLATE_FILE:\n"                                              \
233         "\t              YAML layout template file, can't be used with -c,\n"  \
234         "\t              -i, -S, -p, -o, or -E arguments.\n"                   \
235         "\tLUSTRE_SRC:   Lustre file/dir whose layout info is used to set\n"   \
236         "\t              another lustre file or directory, can't used with\n"  \
237         "\t              -c, -i, -S, -p, -o, or -E arguments.\n"
238
239 #define MIRROR_CREATE_HELP                                                     \
240         "\tMIRROR_COUNT: Number of mirrors to be created with the upcoming\n"  \
241         "\t              setstripe layout options\n"                           \
242         "\t              It defaults to 1 if not specified; if specified,\n"   \
243         "\t              it must follow the option without a space.\n"         \
244         "\t              The option can also be repeated multiple times to\n"  \
245         "\t              separate mirrors that have different layouts.\n"      \
246         "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n"             \
247         "\t              It can be a plain layout or a composite layout.\n"    \
248         "\t              If not specified, the stripe options inherited\n"     \
249         "\t              from the previous component will be used.\n"          \
250         "\tFLAGS:        set flags to the component of the current mirror.\n"  \
251         "\t              Only \"prefer\" flag is supported so far.\n"
252
253 #define MIRROR_EXTEND_HELP                                                     \
254         MIRROR_CREATE_HELP                                                     \
255         "\tVICTIM_FILE:  The layout of victim_file will be split and used\n"   \
256         "\t              as a mirror added to the mirrored file.\n"            \
257         "\tno-verify:    This option indicates not to verify the mirror(s)\n"  \
258         "\t              from victim file(s) in case the victim file(s)\n"     \
259         "\t              contains the same data as the original mirrored\n"    \
260         "\t              file.\n"
261
262 #define MIRROR_EXTEND_USAGE                                                    \
263         "                 {--mirror-count|-N[mirror_count]}\n"                 \
264         "                 [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n"         \
265         "                 [--no-verify]\n"
266
267 #define SETSTRIPE_USAGE                                                 \
268         SSM_CMD_COMMON("setstripe")                                     \
269         MIRROR_EXTEND_USAGE                                             \
270         "                 <directory|filename>\n"                       \
271         SSM_HELP_COMMON                                                 \
272         MIRROR_EXTEND_HELP
273
274 #define MIGRATE_USAGE                                                   \
275         SSM_CMD_COMMON("migrate  ")                                     \
276         "                 [--block|-b] [--non-block|-n]\n"              \
277         "                 [--non-direct|-D] [--verbose|-v]\n"           \
278         "                 <filename>\n"                                 \
279         SSM_HELP_COMMON                                                 \
280         "\n"                                                            \
281         "\tblock:        Block file access during data migration (default)\n" \
282         "\tnon-block:    Abort migrations if concurrent access is detected\n" \
283         "\tnon-direct:   Do not use direct I/O to copy file contents\n" \
284         "\tverbose:      Print each filename as it is migrated\n"       \
285
286 #define SETDIRSTRIPE_USAGE                                              \
287         "               [--mdt-count|-c stripe_count>\n"                \
288         "               [--help|-h] [--mdt-hash|-H mdt_hash]\n"         \
289         "               [--mdt-index|-i mdt_index[,mdt_index,...]\n"    \
290         "               [--default|-D] [--mode|-o mode]\n"              \
291         "               [--max-inherit|-X max_inherit]\n"               \
292         "               [--max-inherit-rr max_inherit_rr] <dir>\n"      \
293         "\tstripe_count: stripe count of the striped directory\n"       \
294         "\tmdt_index: MDT index of first stripe\n"                      \
295         "\tmdt_hash:  hash type of the striped directory. mdt types:\n" \
296         "       crush     CRUSH hash algorithm (default)\n" \
297         "       fnv_1a_64 FNV-1a hash algorithm\n"              \
298         "       all_char  sum of characters % MDT_COUNT (not recommended)\n" \
299         "\tdefault_stripe: set default dirstripe of the directory\n"    \
300         "\tmode: the file access permission of the directory (octal)\n" \
301         "To create dir with a foreign (free format) layout :\n"         \
302         "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
303                 "[--mode|-o MODE] [--flags HEX] <dir>\n" \
304         "\tmode: the file access permissions of the directory\n"        \
305         "\tforeign_type: none, daos, symlink, or numeric value\n"
306
307 /**
308  * command_t mirror_cmdlist - lfs mirror commands.
309  */
310 command_t mirror_cmdlist[] = {
311         { .pc_name = "create", .pc_func = lfs_mirror_create,
312           .pc_help = "Create a mirrored file.\n"
313                 "usage: lfs mirror create {--mirror-count|-N[MIRROR_COUNT]}\n"
314                 "\t\t[SETSTRIPE_OPTIONS] <filename|directory> ...\n"
315           MIRROR_CREATE_HELP },
316         { .pc_name = "delete", .pc_func = lfs_mirror_delete,
317           .pc_help = "Delete a mirror from a file.\n"
318         "usage: lfs mirror delete {--mirror-id MIRROR_ID|-p POOL|\n"
319         "\t               --component-id|--comp-id|-I COMP_ID} <mirror_file>\n"
320         },
321         { .pc_name = "extend", .pc_func = lfs_mirror_extend,
322           .pc_help = "Extend a mirrored file.\n"
323                 "usage: lfs mirror extend {--mirror-count|-N[MIRROR_COUNT]}\n"
324                 "\t\t[--no-verify] [SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... <filename> ...\n"
325           MIRROR_EXTEND_HELP },
326         { .pc_name = "split", .pc_func = lfs_mirror_split,
327           .pc_help = "Split a mirrored file.\n"
328         "usage: lfs mirror split [--destroy|-d] [-f NEW_FILE] [--help|-h]\n"
329         "\t\t{--mirror-id MIRROR_ID|--component-id|-I COMP_ID|-p POOL}\n"
330         "\t\t<mirrored file> ...\n"
331         "\tMIRROR_ID: The numerical unique identifier for a mirror.\n"
332         "\t           It can be fetched by the 'lfs getstripe' command.\n"
333         "\tCOMP_ID:   Unique component ID within a mirror.\n"
334         "\tPOOL:      Components using specified pool.\n"
335         "\tNEW_FILE:  This option indicates the layout of the split\n"
336         "\t           mirror will be stored into. If not specified,\n"
337         "\t           a new file named <mirrored_file>.mirror~MIRROR_ID\n"
338         "\t           will be used.\n" },
339         { .pc_name = "read", .pc_func = lfs_mirror_read,
340           .pc_help = "Read the content of a specified mirror of a file.\n"
341                 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
342                 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
343         { .pc_name = "write", .pc_func = lfs_mirror_write,
344           .pc_help = "Write to a specified mirror of a file.\n"
345                 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
346                 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
347         { .pc_name = "copy", .pc_func = lfs_mirror_copy,
348           .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
349                 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
350                 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
351         { .pc_name = "resync", .pc_func = lfs_mirror_resync,
352           .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
353                 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]\n"
354                 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
355         { .pc_name = "verify", .pc_func = lfs_mirror_verify,
356           .pc_help = "Verify mirrored file(s).\n"
357                 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
358                 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
359         { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
360           .pc_help = "list commands supported by lfs mirror"},
361         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
362         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
363         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
364         { .pc_help = NULL }
365 };
366
367 /**
368  * command_t pcc_cmdlist - lfs pcc commands.
369  */
370 command_t pcc_cmdlist[] = {
371         { .pc_name = "attach", .pc_func = lfs_pcc_attach,
372           .pc_help = "Attach given files to the Persistent Client Cache.\n"
373                 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
374                 "\t-i: archive id for RW-PCC\n" },
375         { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
376           .pc_help = "Attach given files into PCC by FID(s).\n"
377                 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
378                 "\t-i: archive id for RW-PCC\n"
379                 "\t-m: Lustre mount point\n" },
380         { .pc_name = "state", .pc_func = lfs_pcc_state,
381           .pc_help = "Display the PCC state for given files.\n"
382                 "usage: lfs pcc state <file> ...\n" },
383         { .pc_name = "detach", .pc_func = lfs_pcc_detach,
384           .pc_help = "Detach given files from the Persistent Client Cache.\n"
385                 "usage: lfs pcc detach <file> ...\n" },
386         { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
387           .pc_help = "Detach given files from PCC by FID(s).\n"
388                 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
389         { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
390           .pc_help = "list commands supported by lfs pcc"},
391         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
392         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
393         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
394         { .pc_help = NULL }
395 };
396
397 /* all available commands */
398 command_t cmdlist[] = {
399         {"setstripe", lfs_setstripe, 0,
400          "To create a file with specified striping/composite layout, or\n"
401          "create/replace the default layout on an existing directory:\n"
402          SSM_CMD_COMMON("setstripe")
403          "                 [--mode MODE]\n"
404          "                 <directory|filename>\n"
405          " or\n"
406          "To add component(s) to an existing composite file:\n"
407          SSM_CMD_COMMON("setstripe --component-add")
408          SSM_HELP_COMMON
409          "To totally delete the default striping from an existing directory:\n"
410          "usage: setstripe [--delete|-d] <directory>\n"
411          " or\n"
412          "To create a mirrored file or set s default mirror layout on a directory:\n"
413          "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
414          " or\n"
415          "To delete the last component(s) from an existing composite file\n"
416          "(note that this will also delete any data in those components):\n"
417          "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
418          "                               [--component-flags|-F COMP_FLAGS]\n"
419          "                               <filename>\n"
420          "\tCOMP_ID:     Unique component ID to delete\n"
421          "\tCOMP_FLAGS:  'init' indicating all instantiated components\n"
422          "\t             '^init' indicating all uninstantiated components\n"
423          "\t-I and -F cannot be specified at the same time\n"
424          " or\n"
425          "To set or clear flags on a specific component\n"
426          "(note that this command can only be applied to mirrored files:\n"
427          "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
428          "                            <filename>\n"
429          " or\n"
430          "To create a file with a foreign (free format) layout:\n"
431          "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
432          "                 --xattr|-x LAYOUT_STRING [--flags HEX]\n"
433          "                 [--mode MODE] <filename>\n"},
434         {"getstripe", lfs_getstripe, 0,
435          "To list the layout pattern for a given file or files in a\n"
436          "directory or recursively for all files in a directory tree.\n"
437          "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
438          "                 [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
439          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
440          "                 [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
441          "                 [--layout|-L] [--generation|-g] [--yaml|-y]\n"
442          "                 [--help|-h] [--component-id|-I[=COMP_ID]]\n"
443          "                 [--component-flags[=COMP_FLAGS]]\n"
444          "                 [--component-count]\n"
445          "                 [--extension-size|--ext-size|-z]\n"
446          "                 [--component-start[=[+-]COMP_START]]\n"
447          "                 [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
448          "                 [[!] --mirror-index=[+-]INDEX |\n"
449          "                 [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
450          "                 <directory|filename> ..."},
451         {"setdirstripe", lfs_setdirstripe, 0,
452          "Create striped directory on specified MDT, same as mkdir.\n"
453          "May be restricted to root or group users, depending on settings.\n"
454          "usage: setdirstripe [OPTION] <directory>\n"
455          SETDIRSTRIPE_USAGE},
456         {"getdirstripe", lfs_getdirstripe, 0,
457          "To list the layout pattern info for a given directory\n"
458          "or recursively for all directories in a directory tree.\n"
459          "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
460          "                    [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
461          "                    [--recursive|-r] [--yaml|-y]\n"
462          "                    [--verbose|-v] [--default|-D]\n"
463          "                    [--max-inherit|-X]\n"
464          "                    [--max-inherit-rr] <dir> ..."},
465         {"mkdir", lfs_setdirstripe, 0,
466          "Create striped directory on specified MDT, same as setdirstripe.\n"
467          "usage: mkdir [OPTION] <directory>\n"
468          SETDIRSTRIPE_USAGE},
469         {"rm_entry", lfs_rmentry, 0,
470          "To remove the name entry of the remote directory. Note: This\n"
471          "command will only delete the name entry, i.e. the remote directory\n"
472          "will become inaccessable after this command. This can only be done\n"
473          "by the administrator\n"
474          "usage: rm_entry <dir>\n"},
475         {"unlink_foreign", lfs_unlink_foreign, 0,
476          "To remove the foreign file/dir.\n"
477          "Note: This is for files/dirs prevented to be removed using\n"
478          "unlink/rmdir, but works also for regular ones\n"
479          "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
480         {"pool_list", lfs_poollist, 0,
481          "List pools or pool OSTs\n"
482          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
483         {"find", lfs_find, 0,
484          "find files matching given attributes recursively in directory tree.\n"
485          "usage: find <directory|filename> ...\n"
486          "     [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
487          "     [[!] --mtime|-M [+-]N[smhdwy]]\n"
488          "     [[!] --btime|--Btime|-B [+-]N[smhdwy]] [--help|-h]\n"
489          "     [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
490          "     [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
491          "     [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
492          "     [[!] --perm [/-]mode] [[!] --pool <pool>] [--print|-P]\n"
493          "     [--print0|-0] [[!] --projid <projid>]\n"
494          "     [[!] --size|-s [+-]N[bkMGTPE]]\n"
495          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
496          "     [[!] --stripe-index|-i <index,...>]\n"
497          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
498          "     [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
499          "     [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
500          "     [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
501          "     [[!] --layout|-L released,raid0,mdt]\n"
502          "     [[!] --foreign[=<foreign_type>]]\n"
503          "     [[!] --component-count [+-]<comp_cnt>]\n"
504          "     [[!] --component-start [+-]N[kMGTPE]]\n"
505          "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
506          "     [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
507          "     [[!] --mirror-count|-N [+-]<n>]\n"
508          "     [[!] --mirror-state <[^]state>]\n"
509          "     [[!] --mdt-count|-T [+-]<stripes>]\n"
510          "     [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
511          "     [[!] --mdt-index|-m <uuid|index,...>]\n"
512          "\t !: used before an option indicates 'NOT' requested attribute\n"
513          "\t -: used before a value indicates less than requested value\n"
514          "\t +: used before a value indicates more than requested value\n"
515          "\t ^: used before a flag indicates to exclude it\n"},
516         {"check", lfs_check, 0,
517          "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
518          "or all the servers (MGTs, MDTs and OSTs).\n"
519          "usage: check {mgts|osts|mdts|all}"},
520         {"osts", lfs_osts, 0, "list OSTs connected to client "
521          "[for specified path only]\n" "usage: osts [path]"},
522         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
523          "[for specified path only]\n" "usage: mdts [path]"},
524         {"df", lfs_df, 0,
525          "report filesystem disk space usage or inodes usage "
526          "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
527          "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
528          "          [--pool|-p <fsname>[.<pool>]] [path]"},
529         {"getname", lfs_getname, 0,
530          "list instances and specified mount points [for specified path only]\n"
531          "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
532 #ifdef HAVE_SYS_QUOTA_H
533         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
534          "usage: setquota {-u|-g|-p} UNAME|UID|GNAME|GID|PROJID\n"
535          "                -b BLOCK_SOFTLIMIT -B BLOCK_HARDLIMIT\n"
536          "                -i INODE_SOFTLIMIT -I INODE_HARDLIMIT <filesystem>\n"
537          "       setquota {-u|--user USER|UID|-g|--group GROUP|GID|-p|--projid PROJID}\n"
538          "                [--block-softlimit BLOCK_SOFTLIMIT]\n"
539          "                [--block-hardlimit BLOCK_HARDLIMIT]\n"
540          "                [--inode-softlimit INODE_SOFTLIMIT]\n"
541          "                [--inode-hardlimit INODE_HARDLIMIT] <filesystem>\n"
542          "       setquota [-t] {-h|--help|-u|--user|-g|--group|-p|--projid}\n"
543          "                [--block-grace 'notify'|BLOCK_GRACE]\n"
544          "                [--inode-grace 'notify'|INODE_GRACE] <filesystem>\n"
545          "       setquota {-U|-G|-P}\n"
546          "                -b BLOCK_SOFTLIMIT -B BLOCK_HARDLIMIT\n"
547          "                -i INODE_SOFTLIMIT -I INODE_HARDLIMIT <filesystem>\n"
548          "       setquota {-U|--default-usr|-G|--default-grp|-P|--default-prj}\n"
549          "                [--block-softlimit BLOCK_SOFTLIMIT]\n"
550          "                [--block-hardlimit BLOCK_HARDLIMIT]\n"
551          "                [--inode-softlimit INODE_SOFTLIMIT]\n"
552          "                [--inode-hardlimit INODE_HARDLIMIT] <filesystem>\n"
553          "       setquota {-u|-g|-p} UNAME|UID|GNAME|GID|PROJID\n"
554          "                {-d|--default}\n"
555          "       -b can be used instead of --block-softlimit/--block-grace\n"
556          "       -B can be used instead of --block-hardlimit\n"
557          "       -i can be used instead of --inode-softlimit/--inode-grace\n"
558          "       -I can be used instead of --inode-hardlimit\n"
559          "       -d can be used instead of --default\n\n"
560          "Note: The total quota space will be split into many qunits and\n"
561          "      balanced over all server targets, the minimal qunit size is\n"
562          "      1M bytes for block space and 1K inodes for inode space.\n\n"
563          "      The maximum quota grace time is 2^48 - 1 seconds.\n\n"
564          "      Quota space rebalancing process will stop when this mininum\n"
565          "      value is reached. As a result, quota exceeded can be returned\n"
566          "      while many targets still have 1MB or 1K inodes of spare\n"
567          "      quota space.\n\n"
568          "      When setting the grace time, 'notify' can be used as grace to\n"
569          "      be notified after the quota is over soft limit but prevents\n"
570          "      the soft limit from becoming the hard limit."},
571         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
572          "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
573          "             [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID] <filesystem>\n"
574          "       quota [-o OBD_UUID|-i MDT_IDX|-I OST_IDX] -t {-u|-g|-p} <filesystem>\n"
575         "        quota [-q] [-v] [h] {-U|-G|-P} <filesystem>"},
576         {"project", lfs_project, 0,
577          "Change or list project attribute for specified file or directory.\n"
578          "usage: project [-d|-r] <file|directory...>\n"
579          "         list project ID and flags on file(s) or directories\n"
580          "       project [-p id] [-s] [-r] <file|directory...>\n"
581          "         set project ID and/or inherit flag for specified file(s) or directories\n"
582          "       project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
583          "         check project ID and flags on file(s) or directories, print outliers\n"
584          "       project -C [-r] [-k] <file|directory...>\n"
585          "         clear the project inherit flag and ID on the file or directory\n"
586         },
587 #endif
588         {"flushctx", lfs_flushctx, 0,
589          "Flush security context for current user.\n"
590          "usage: flushctx [-k] [-r] [mountpoint...]"},
591         {"changelog", lfs_changelog, 0,
592          "Show the metadata changes on an MDT."
593          "\nusage: changelog <mdtname> [startrec [endrec]]"},
594         {"changelog_clear", lfs_changelog_clear, 0,
595          "Indicate that old changelog records up to <endrec> are no longer of "
596          "interest to consumer <id>, allowing the system to free up space.\n"
597          "An <endrec> of 0 means all records.\n"
598          "usage: changelog_clear <mdtname> <id> <endrec>"},
599         {"fid2path", lfs_fid2path, 0,
600          "Resolve the full path(s) for given FID(s). For a specific hardlink "
601          "specify link number <linkno>.\n"
602          "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
603          "<fsname|root> <fid>..."},
604         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
605          "usage: path2fid [--parents] <path> ..."},
606         {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
607          "usage: rmfid <fsname|rootpath> <fid> ..."},
608         {"data_version", lfs_data_version, 0, "Display file data version for "
609          "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
610         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
611          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
612         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
613          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
614          "[--archived] [--lost] [--archive-id NUM] <file> ..."},
615         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
616          "files.\n"
617          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
618          "[--archived] [--lost] <file> ..."},
619         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
620          "given files.\n" "usage: hsm_action <file> ..."},
621         {"hsm_archive", lfs_hsm_archive, 0,
622          "Archive file to external storage.\n"
623          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
624          "<file> ..."},
625         {"hsm_restore", lfs_hsm_restore, 0,
626          "Restore file from external storage.\n"
627          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
628         {"hsm_release", lfs_hsm_release, 0,
629          "Release files from Lustre.\n"
630          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
631         {"hsm_remove", lfs_hsm_remove, 0,
632          "Remove file copy from external storage.\n"
633          "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
634          "[--archive NUM]\n"
635          "                  (FILE [FILE ...] | "
636          "--mntpath MOUNTPATH FID [FID ...])\n"
637          "\n"
638          "Note: To remove an archived copy of a file already deleted from a "
639          "Lustre FS, the\n"
640          "--mntpath option and a list of FIDs must be specified"
641         },
642         {"hsm_cancel", lfs_hsm_cancel, 0,
643          "Cancel requests related to specified files.\n"
644          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
645         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
646          "usage: swap_layouts <path1> <path2>"},
647         {"migrate", lfs_setstripe_migrate, 0,
648          "migrate a directory between MDTs.\n"
649          "usage: migrate [--mdt-count|-c STRIPE_COUNT]\n"
650          "               [--mdt-hash|-H HASH_TYPE]\n"
651          "               [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
652          "               <directory>\n"
653          "\tmdt:        MDTs to stripe over, if only one MDT is specified\n"
654          "                      it's the MDT index of first stripe\n"
655          "\tmdt_count:  number of MDTs to stripe a directory over\n"
656          "\tmdt_hash:   hash type of the striped directory. mdt types:\n"
657          "              all_char  (type 1)sum of characters % MDT_COUNT\n"
658          "              fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
659          "              crush     (type 3)CRUSH hash algorithm\n"
660          "\n"
661          "migrate file objects from one OST "
662          "layout\nto another (may be not safe with concurent writes).\n"
663          "usage: migrate [--stripe-count|-c STRIPE_COUNT]\n"
664          "               [--overstripe-count|-C STRIPE_COUNT]\n"
665          "               [--stripe-index|-i START_OST_INDEX]\n"
666          "               [--stripe-size|-S STRIPE_SIZE]\n"
667          "               [--pool|-p <pool_name>] [--ost|-o OST_INDICES]\n"
668          "               [--block|-b] [--non-block|-n] [--non-direct|-D]\n"
669          "               <file|directory>\n"
670          "\tstripe_count:   number of OSTs to stripe a file over\n"
671          "\t                Using -C instead of -c allows overstriping, which\n"
672          "\t                will place more than one stripe per OST if\n"
673          "\t                stripe_count is greater than the number of OSTs\n"
674          "\tstripe_ost_index: index of the first OST to stripe a file over\n"
675          "\tstripe_size:      number of bytes to store before moving to the next OST\n"
676          "\tpool_name:        name of the predefined pool of OSTs\n"
677          "\tost_indices:      OSTs to stripe over, in order\n"
678          "\tblock:        Block file access during data migration (default)\n"
679          "\tnon-block:    Abort migrations if concurrent access is detected\n"
680          "\tnon-direct:       do not use direct I/O to copy file contents.\n"},
681         {"mv", lfs_mv, 0,
682          "To move directories between MDTs. This command is deprecated, "
683          "use \"migrate\" instead.\n"
684          "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
685          "[--verbose|-v]\n"},
686         {"ladvise", lfs_ladvise, 0,
687          "Provide servers with advice about access patterns for a file.\n"
688          "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
689          "               [--background|-b] [--unset|-u]\n\n"
690          "               {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
691          "               {[--mode|-m [READ,WRITE]}\n"
692          "               <file> ...\n"},
693         {"mirror", lfs_mirror, mirror_cmdlist,
694          "lfs commands used to manage files with mirrored components:\n"
695          "lfs mirror create - create a mirrored file or directory\n"
696          "lfs mirror extend - add mirror(s) to an existing file\n"
697          "lfs mirror split  - split a mirror from an existing mirrored file\n"
698          "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
699          "lfs mirror read   - read a mirror content of a mirrored file\n"
700          "lfs mirror write  - write to a mirror of a mirrored file\n"
701          "lfs mirror copy   - copy a mirror to other mirror(s) of a file\n"
702          "lfs mirror verify - verify mirrored file(s)\n"},
703         {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
704          "usage: getsom [-s] [-b] [-f] <path>\n"
705          "\t-s: Only show the size value of the SOM data for a given file\n"
706          "\t-b: Only show the blocks value of the SOM data for a given file\n"
707          "\t-f: Only show the flags value of the SOM data for a given file\n"},
708         {"heat_get", lfs_heat_get, 0,
709          "To get heat of files.\n"
710          "usage: heat_get <file> ...\n"},
711         {"heat_set", lfs_heat_set, 0,
712          "To set heat flags of files.\n"
713          "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
714          "\t--clear|-c: Clear file heat for given files\n"
715          "\t--off|-o:   Turn off file heat for given files\n"
716          "\t--on|-O:    Turn on file heat for given files\n"},
717         {"pcc", lfs_pcc, pcc_cmdlist,
718          "lfs commands used to interact with PCC features:\n"
719          "lfs pcc attach - attach given files to Persistent Client Cache\n"
720          "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
721          "lfs pcc state  - display the PCC state for given files\n"
722          "lfs pcc detach - detach given files from Persistent Client Cache\n"
723          "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
724         {"help", Parser_help, 0, "help"},
725         {"exit", Parser_quit, 0, "quit"},
726         {"quit", Parser_quit, 0, "quit"},
727         {"--version", Parser_version, 0,
728          "output build version of the utility and exit"},
729         {"--list-commands", lfs_list_commands, 0,
730          "list commands supported by the utility and exit"},
731         { 0, 0, 0, NULL }
732 };
733
734 static int check_hashtype(const char *hashtype)
735 {
736         int type_num = atoi(hashtype);
737         int i;
738
739         /* numeric hash type */
740         if (hashtype && strlen(hashtype) == 1 &&
741             (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
742                 return type_num;
743         /* string hash type */
744         for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
745                 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
746                         return i;
747
748         return 0;
749 }
750
751 static uint32_t check_foreign_type_name(const char *foreign_type_name)
752 {
753         uint32_t i;
754
755         for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
756                 if (!lu_foreign_types[i].lft_name)
757                         break;
758                 if (strcmp(foreign_type_name,
759                            lu_foreign_types[i].lft_name) == 0)
760                         return lu_foreign_types[i].lft_type;
761         }
762
763         return LU_FOREIGN_TYPE_UNKNOWN;
764 }
765
766 static const char *error_loc = "syserror";
767
768 enum {
769         MIGRATION_NONBLOCK      = 0x0001,
770         MIGRATION_MIRROR        = 0x0002,
771         MIGRATION_NONDIRECT     = 0x0004,
772         MIGRATION_VERBOSE       = 0x0008,
773 };
774
775 static int
776 migrate_open_files(const char *name, __u64 migration_flags,
777                    const struct llapi_stripe_param *param,
778                    struct llapi_layout *layout, int *fd_src, int *fd_tgt)
779 {
780         int                      fd = -1;
781         int                      fdv = -1;
782         int                      rflags;
783         int                      mdt_index;
784         int                      random_value;
785         char                     parent[PATH_MAX];
786         char                     volatile_file[PATH_MAX];
787         char                    *ptr;
788         int                      rc;
789         struct stat              st;
790         struct stat              stv;
791
792         if (!param && !layout) {
793                 error_loc = "layout information";
794                 return -EINVAL;
795         }
796
797         /* search for file directory pathname */
798         if (strlen(name) > sizeof(parent) - 1) {
799                 error_loc = "source file name";
800                 return -ERANGE;
801         }
802
803         strncpy(parent, name, sizeof(parent));
804         ptr = strrchr(parent, '/');
805         if (!ptr) {
806                 if (!getcwd(parent, sizeof(parent))) {
807                         error_loc = "getcwd";
808                         return -errno;
809                 }
810         } else {
811                 if (ptr == parent) /* leading '/' */
812                         ptr = parent + 1;
813                 *ptr = '\0';
814         }
815
816         /* open file, direct io */
817         /* even if the file is only read, WR mode is nedeed to allow
818          * layout swap on fd
819          */
820         rflags = O_RDWR;
821         if (!(migration_flags & MIGRATION_NONDIRECT))
822                 rflags |= O_DIRECT;
823         fd = open(name, rflags);
824         if (fd < 0) {
825                 rc = -errno;
826                 error_loc = "cannot open source file";
827                 return rc;
828         }
829
830         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
831         if (rc < 0) {
832                 error_loc = "cannot get MDT index";
833                 goto out;
834         }
835
836         do {
837                 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
838                 mode_t open_mode = S_IRUSR | S_IWUSR;
839
840                 random_value = random();
841                 rc = snprintf(volatile_file, sizeof(volatile_file),
842                               "%s/%s:%.4X:%.4X:fd=%.2d", parent,
843                               LUSTRE_VOLATILE_HDR, mdt_index,
844                               random_value, fd);
845                 if (rc >= sizeof(volatile_file)) {
846                         rc = -ENAMETOOLONG;
847                         break;
848                 }
849
850                 /* create, open a volatile file, use caching (ie no directio) */
851                 if (layout) {
852                         /* Returns -1 and sets errno on error: */
853                         fdv = llapi_layout_file_open(volatile_file, open_flags,
854                                                      open_mode, layout);
855                         if (fdv < 0)
856                                 fdv = -errno;
857                 } else {
858                         /* Does the right thing on error: */
859                         fdv = llapi_file_open_param(volatile_file, open_flags,
860                                                     open_mode, param);
861                 }
862         } while (fdv < 0 && (rc = fdv) == -EEXIST);
863
864         if (rc < 0) {
865                 error_loc = "cannot create volatile file";
866                 goto out;
867         }
868
869         /*
870          * In case the MDT does not support creation of volatile files
871          * we should try to unlink it.
872          */
873         (void)unlink(volatile_file);
874
875         /*
876          * Not-owner (root?) special case.
877          * Need to set owner/group of volatile file like original.
878          * This will allow to pass related check during layout_swap.
879          */
880         rc = fstat(fd, &st);
881         if (rc != 0) {
882                 rc = -errno;
883                 error_loc = "cannot stat source file";
884                 goto out;
885         }
886
887         rc = fstat(fdv, &stv);
888         if (rc != 0) {
889                 rc = -errno;
890                 error_loc = "cannot stat volatile";
891                 goto out;
892         }
893
894         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
895                 rc = fchown(fdv, st.st_uid, st.st_gid);
896                 if (rc != 0) {
897                         rc = -errno;
898                         error_loc = "cannot change ownwership of volatile";
899                         goto out;
900                 }
901         }
902
903 out:
904         if (rc < 0) {
905                 if (fd > 0)
906                         close(fd);
907                 if (fdv > 0)
908                         close(fdv);
909         } else {
910                 *fd_src = fd;
911                 *fd_tgt = fdv;
912                 error_loc = NULL;
913         }
914         return rc;
915 }
916
917 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
918 {
919         struct llapi_layout *layout;
920         size_t buf_size = 4 * 1024 * 1024;
921         void *buf = NULL;
922         off_t pos = 0;
923         off_t data_end = 0;
924         size_t page_size = sysconf(_SC_PAGESIZE);
925         bool sparse;
926         int rc;
927
928         layout = llapi_layout_get_by_fd(fd_src, 0);
929         if (layout) {
930                 uint64_t stripe_size;
931
932                 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
933                 if (rc == 0)
934                         buf_size = stripe_size;
935
936                 llapi_layout_free(layout);
937         }
938
939         /* Use a page-aligned buffer for direct I/O */
940         rc = posix_memalign(&buf, page_size, buf_size);
941         if (rc != 0)
942                 return -rc;
943
944         sparse = llapi_file_is_sparse(fd_src);
945         if (sparse) {
946                 rc = ftruncate(fd_dst, pos);
947                 if (rc < 0) {
948                         rc = -errno;
949                         return rc;
950                 }
951         }
952
953         while (1) {
954                 off_t data_off;
955                 size_t to_read, to_write;
956                 ssize_t rsize;
957
958                 if (sparse && pos >= data_end) {
959                         size_t data_size;
960
961                         data_off = llapi_data_seek(fd_src, pos, &data_size);
962                         if (data_off < 0) {
963                                 /* Non-fatal, switch to full copy */
964                                 sparse = false;
965                                 continue;
966                         }
967                         /* hole at the end of file, truncate up to it */
968                         if (!data_size) {
969                                 rc = ftruncate(fd_dst, data_off);
970                                 if (rc < 0)
971                                         goto out;
972                         }
973                         pos = data_off & ~(page_size - 1);
974                         data_end = data_off + data_size;
975                         to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
976                         to_read = MIN(to_read, buf_size);
977                 } else {
978                         to_read = buf_size;
979                 }
980
981                 if (check_file) {
982                         rc = check_file(fd_src);
983                         if (rc < 0)
984                                 goto out;
985                 }
986
987                 rsize = pread(fd_src, buf, to_read, pos);
988                 if (rsize < 0) {
989                         rc = -errno;
990                         goto out;
991                 }
992                 /* EOF */
993                 if (rsize == 0)
994                         break;
995
996                 to_write = rsize;
997                 while (to_write > 0) {
998                         ssize_t written;
999
1000                         written = pwrite(fd_dst, buf, to_write, pos);
1001                         if (written < 0) {
1002                                 rc = -errno;
1003                                 goto out;
1004                         }
1005                         pos += written;
1006                         to_write -= written;
1007                 }
1008                 if (rc || rsize < to_read)
1009                         break;
1010         }
1011
1012         rc = fsync(fd_dst);
1013         if (rc < 0)
1014                 rc = -errno;
1015 out:
1016         /* Try to avoid page cache pollution after migration. */
1017         (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1018         (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1019
1020         free(buf);
1021         return rc;
1022 }
1023
1024 static int migrate_copy_timestamps(int fd, int fdv)
1025 {
1026         struct stat st;
1027
1028         if (fstat(fd, &st) == 0) {
1029                 struct timeval tv[2] = {
1030                         {.tv_sec = st.st_atime},
1031                         {.tv_sec = st.st_mtime}
1032                 };
1033
1034                 return futimes(fdv, tv);
1035         }
1036
1037         return -errno;
1038 }
1039
1040 static int migrate_block(int fd, int fdv)
1041 {
1042         __u64   dv1;
1043         int     gid;
1044         int     rc;
1045         int     rc2;
1046
1047         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1048         if (rc < 0) {
1049                 error_loc = "cannot get dataversion";
1050                 return rc;
1051         }
1052
1053         do
1054                 gid = random();
1055         while (gid == 0);
1056
1057         /*
1058          * The grouplock blocks all concurrent accesses to the file.
1059          * It has to be taken after llapi_get_data_version as it would
1060          * block it too.
1061          */
1062         rc = llapi_group_lock(fd, gid);
1063         if (rc < 0) {
1064                 error_loc = "cannot get group lock";
1065                 return rc;
1066         }
1067
1068         rc = migrate_copy_data(fd, fdv, NULL);
1069         if (rc < 0) {
1070                 error_loc = "data copy failed";
1071                 goto out_unlock;
1072         }
1073
1074         /* Make sure we keep original atime/mtime values */
1075         rc = migrate_copy_timestamps(fd, fdv);
1076         if (rc < 0) {
1077                 error_loc = "timestamp copy failed";
1078                 goto out_unlock;
1079         }
1080
1081         /*
1082          * swap layouts
1083          * for a migration we need to check data version on file did
1084          * not change.
1085          *
1086          * Pass in gid=0 since we already own grouplock.
1087          */
1088         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1089                                            SWAP_LAYOUTS_CHECK_DV1);
1090         if (rc == -EAGAIN) {
1091                 error_loc = "file changed";
1092                 goto out_unlock;
1093         } else if (rc < 0) {
1094                 error_loc = "cannot swap layout";
1095                 goto out_unlock;
1096         }
1097
1098 out_unlock:
1099         rc2 = llapi_group_unlock(fd, gid);
1100         if (rc2 < 0 && rc == 0) {
1101                 error_loc = "unlock group lock";
1102                 rc = rc2;
1103         }
1104
1105         return rc;
1106 }
1107
1108 /**
1109  * Internal helper for migrate_copy_data(). Check lease and report error if
1110  * need be.
1111  *
1112  * \param[in]  fd           File descriptor on which to check the lease.
1113  *
1114  * \retval 0       Migration can keep on going.
1115  * \retval -errno  Error occurred, abort migration.
1116  */
1117 static int check_lease(int fd)
1118 {
1119         int rc;
1120
1121         rc = llapi_lease_check(fd);
1122         if (rc > 0)
1123                 return 0; /* llapi_check_lease returns > 0 on success. */
1124
1125         return -EBUSY;
1126 }
1127
1128 static int migrate_nonblock(int fd, int fdv)
1129 {
1130         __u64   dv1;
1131         __u64   dv2;
1132         int     rc;
1133
1134         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1135         if (rc < 0) {
1136                 error_loc = "cannot get data version";
1137                 return rc;
1138         }
1139
1140         rc = migrate_copy_data(fd, fdv, check_lease);
1141         if (rc < 0) {
1142                 error_loc = "data copy failed";
1143                 return rc;
1144         }
1145
1146         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1147         if (rc != 0) {
1148                 error_loc = "cannot get data version";
1149                 return rc;
1150         }
1151
1152         if (dv1 != dv2) {
1153                 rc = -EAGAIN;
1154                 error_loc = "source file changed";
1155                 return rc;
1156         }
1157
1158         /* Make sure we keep original atime/mtime values */
1159         rc = migrate_copy_timestamps(fd, fdv);
1160         if (rc < 0) {
1161                 error_loc = "timestamp copy failed";
1162                 return rc;
1163         }
1164
1165         return 0;
1166 }
1167
1168 static
1169 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1170 {
1171         struct pool_to_id_cbdata data = { .pool = pool };
1172         struct llapi_layout *layout = NULL;
1173         int rc;
1174
1175         layout = llapi_layout_get_by_path(fname, 0);
1176         if (!layout) {
1177                 fprintf(stderr,
1178                         "error %s: file '%s' couldn't get layout: rc=%d\n",
1179                         progname, fname, errno);
1180                 rc = -errno;
1181                 goto free_layout;
1182         }
1183         rc = llapi_layout_sanity(layout, fname, false, true);
1184         if (rc < 0) {
1185                 llapi_layout_sanity_perror(errno);
1186                 goto free_layout;
1187         }
1188         rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1189         if (rc < 0)
1190                 goto free_layout;
1191
1192         *comp_id = data.id;
1193         rc = 0;
1194
1195 free_layout:
1196         if (layout)
1197                 llapi_layout_free(layout);
1198         return rc;
1199 }
1200
1201 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1202                              __u32 flags, __u32 neg_flags)
1203 {
1204         __u32 ids[2];
1205         __u32 flags_array[2];
1206         size_t count = 0;
1207         int rc;
1208
1209         if (!comp_id) {
1210                 if (pool == NULL) {
1211                         fprintf(stderr,
1212                                 "error %s: neither component id nor pool is specified\n",
1213                                 progname);
1214                         return -EINVAL;
1215                 }
1216                 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1217                 if (rc)
1218                         return rc;
1219         }
1220
1221         if (flags) {
1222                 ids[count] = comp_id;
1223                 flags_array[count] = flags;
1224                 ++count;
1225         }
1226
1227         if (neg_flags) {
1228                 if (neg_flags & LCME_FL_STALE) {
1229                         fprintf(stderr,
1230                                 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1231                                 progname);
1232                         return -EINVAL;
1233                 }
1234
1235                 ids[count] = comp_id;
1236                 flags_array[count] = neg_flags | LCME_FL_NEG;
1237                 ++count;
1238         }
1239
1240         rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1241         if (rc) {
1242                 if (errno == EUCLEAN) {
1243                         rc = -errno;
1244                         fprintf(stderr,
1245                                 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1246                                 progname, comp_id, fname);
1247                 } else {
1248                         fprintf(stderr,
1249                                 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1250                                 progname, comp_id, fname, flags, neg_flags);
1251                 }
1252         }
1253
1254         return rc;
1255 }
1256
1257 static int lfs_component_del(char *fname, __u32 comp_id,
1258                              __u32 flags, __u32 neg_flags)
1259 {
1260         int     rc = 0;
1261
1262         if (flags && neg_flags)
1263                 return -EINVAL;
1264
1265         if (!flags && neg_flags)
1266                 flags = neg_flags | LCME_FL_NEG;
1267
1268         if ((flags && comp_id) || (!flags && !comp_id))
1269                 return -EINVAL;
1270
1271         if (flags) {
1272                 if (flags & ~LCME_KNOWN_FLAGS) {
1273                         fprintf(stderr,
1274                                 "%s setstripe: unknown flags %#x\n",
1275                                 progname, flags);
1276                         return -EINVAL;
1277                 }
1278         } else if (comp_id > LCME_ID_MAX) {
1279                 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1280                         progname, comp_id);
1281                 return -EINVAL;
1282         }
1283
1284         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1285         if (rc)
1286                 fprintf(stderr,
1287                         "%s setstripe: cannot delete component %#x from '%s': %s\n",
1288                         progname, comp_id, fname, strerror(errno));
1289         return rc;
1290 }
1291
1292 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1293 {
1294         int     rc;
1295
1296         if (!layout)
1297                 return -EINVAL;
1298
1299         rc = llapi_layout_file_comp_add(fname, layout);
1300         if (rc)
1301                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1302                         fname, strerror(errno));
1303         return rc;
1304 }
1305
1306 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1307                                 struct llapi_layout *layout)
1308 {
1309         struct stat     st;
1310         int     fd;
1311
1312         if (!layout)
1313                 return -EINVAL;
1314
1315         fd = lstat(fname, &st);
1316         if (fd == 0 && S_ISDIR(st.st_mode))
1317                 open_flags = O_DIRECTORY | O_RDONLY;
1318
1319         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1320         if (fd < 0)
1321                 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1322                         S_ISDIR(st.st_mode) ?
1323                                 "set default composite layout for" :
1324                                 "create composite file",
1325                         fname, strerror(errno));
1326         return fd;
1327 }
1328
1329 static int lfs_migrate(char *name, __u64 migration_flags,
1330                        struct llapi_stripe_param *param,
1331                        struct llapi_layout *layout)
1332 {
1333         struct llapi_layout *existing;
1334         uint64_t dom_new, dom_cur;
1335         int fd = -1;
1336         int fdv = -1;
1337         int rc;
1338
1339         rc = migrate_open_files(name, migration_flags, param, layout,
1340                                 &fd, &fdv);
1341         if (rc < 0)
1342                 goto out;
1343
1344         rc = llapi_layout_dom_size(layout, &dom_new);
1345         if (rc) {
1346                 error_loc = "cannot get new layout DoM size";
1347                 goto out;
1348         }
1349         /* special case for migration to DOM layout*/
1350         existing = llapi_layout_get_by_fd(fd, 0);
1351         if (!existing) {
1352                 error_loc = "cannot get existing layout";
1353                 goto out;
1354         }
1355
1356         rc = llapi_layout_dom_size(existing, &dom_cur);
1357         if (rc) {
1358                 error_loc = "cannot get current layout DoM size";
1359                 goto out;
1360         }
1361
1362         /*
1363          * if file has DoM layout already then migration is possible to
1364          * the new layout with the same DoM component via swap layout,
1365          * if new layout used bigger DOM size, then mirroring is used
1366          */
1367         if (dom_new > dom_cur) {
1368                 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1369                                         layout);
1370                 if (rc)
1371                         error_loc = "cannot migrate to DOM layout";
1372                 goto out_closed;
1373         }
1374
1375         if (!(migration_flags & MIGRATION_NONBLOCK)) {
1376                 /*
1377                  * Blocking mode (forced if servers do not support file lease).
1378                  * It is also the default mode, since we cannot distinguish
1379                  * between a broken lease and a server that does not support
1380                  * atomic swap/close (LU-6785)
1381                  */
1382                 rc = migrate_block(fd, fdv);
1383                 goto out;
1384         }
1385
1386         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1387         if (rc < 0) {
1388                 error_loc = "cannot get lease";
1389                 goto out;
1390         }
1391
1392         rc = migrate_nonblock(fd, fdv);
1393         if (rc < 0) {
1394                 llapi_lease_release(fd);
1395                 goto out;
1396         }
1397
1398         /*
1399          * Atomically put lease, swap layouts and close.
1400          * for a migration we need to check data version on file did
1401          * not change.
1402          */
1403         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1404         if (rc < 0) {
1405                 error_loc = "cannot swap layout";
1406                 goto out;
1407         }
1408
1409 out:
1410         if (fd >= 0)
1411                 close(fd);
1412
1413         if (fdv >= 0)
1414                 close(fdv);
1415 out_closed:
1416         if (rc < 0)
1417                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1418                         progname, name, error_loc, strerror(-rc));
1419         else if (migration_flags & MIGRATION_VERBOSE)
1420                 printf("%s\n", name);
1421
1422         return rc;
1423 }
1424
1425 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1426 {
1427         char *name;
1428         char *dup_string = NULL;
1429         int rc = 0;
1430
1431         *flags = 0;
1432         *neg_flags = 0;
1433
1434         if (!string || !string[0])
1435                 return -EINVAL;
1436
1437         dup_string = strdup(string);
1438         if (!dup_string) {
1439                 llapi_printf(LLAPI_MSG_ERROR,
1440                              "%s: insufficient memory\n",
1441                              progname);
1442                 return -ENOMEM;
1443         }
1444
1445         for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1446                 bool found = false;
1447                 int i;
1448
1449                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1450                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1451                         const char *comp_name = comp_flags_table[i].cfn_name;
1452
1453                         if (strcmp(name, comp_name) == 0) {
1454                                 *flags |= comp_flag;
1455                                 found = true;
1456                         } else if (strncmp(name, "^", 1) == 0 &&
1457                                    strcmp(name + 1, comp_name) == 0) {
1458                                 *neg_flags |= comp_flag;
1459                                 found = true;
1460                         }
1461                 }
1462                 if (!found) {
1463                         llapi_printf(LLAPI_MSG_ERROR,
1464                                      "%s: component flag '%s' not supported\n",
1465                                      progname, name);
1466                         rc = -EINVAL;
1467                         goto out_free;
1468                 }
1469         }
1470
1471         if (!*flags && !*neg_flags)
1472                 rc = -EINVAL;
1473
1474         /* don't allow to set and exclude the same flag */
1475         if (*flags & *neg_flags)
1476                 rc = -EINVAL;
1477
1478 out_free:
1479         free(dup_string);
1480         return rc;
1481 }
1482
1483 static int mdthash_input(char *string, __u32 *inflags,
1484                          __u32 *exflags, __u32 *type)
1485 {
1486         char *name;
1487         struct mhf_list {
1488                 char *name;
1489                 __u32 flag;
1490         } mhflist[] = {
1491                 {"migrating", LMV_HASH_FLAG_MIGRATION},
1492                 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1493                 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1494         };
1495
1496         if (string == NULL)
1497                 return -EINVAL;
1498
1499         *inflags = 0;
1500         *exflags = 0;
1501         *type = 0;
1502         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1503                 bool found = false;
1504                 int i;
1505
1506                 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1507                         if (strcmp(name, mhflist[i].name) == 0 ||
1508                             name[0] == mhflist[i].name[0]) {
1509                                 *inflags |= mhflist[i].flag;
1510                                 found = true;
1511                         } else if (name[0] == '^' &&
1512                                    (strcmp(name + 1, mhflist[i].name) == 0 ||
1513                                     name[1] == mhflist[i].name[0])) {
1514                                 *exflags |= mhflist[i].flag;
1515                                 found = true;
1516                         }
1517                 }
1518                 if (!found) {
1519                         i = check_hashtype(name);
1520                         if (i > 0) {
1521                                 *type |= 1 << i;
1522                                 continue;
1523                         }
1524                         llapi_printf(LLAPI_MSG_ERROR,
1525                                      "%s: invalid mdt_hash value '%s'\n",
1526                                      progname, name);
1527                         return -EINVAL;
1528                 }
1529         }
1530
1531         /* don't allow to include and exclude the same flag */
1532         if (*inflags & *exflags) {
1533                 llapi_printf(LLAPI_MSG_ERROR,
1534                              "%s: include and exclude same flag '%s'\n",
1535                              progname, string);
1536                 return -EINVAL;
1537         }
1538
1539         return 0;
1540 }
1541
1542 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1543 {
1544         if (!string)
1545                 return -EINVAL;
1546
1547         *state = 0;
1548         *neg_state = 0;
1549
1550         if (strncmp(string, "^", 1) == 0) {
1551                 *neg_state = llapi_layout_string_flags(string + 1);
1552                 if (*neg_state != 0)
1553                         return 0;
1554         } else {
1555                 *state = llapi_layout_string_flags(string);
1556                 if (*state != 0)
1557                         return 0;
1558         }
1559
1560         llapi_printf(LLAPI_MSG_ERROR,
1561                      "%s: mirrored file state '%s' not supported\n",
1562                      progname, string);
1563         return -EINVAL;
1564 }
1565
1566 /**
1567  * struct mirror_args - Command-line arguments for mirror(s).
1568  * @m_count:  Number of mirrors to be created with this layout.
1569  * @m_flags:  Mirror level flags, only 'prefer' is supported.
1570  * @m_layout: Mirror layout.
1571  * @m_file:   A victim file. Its layout will be split and used as a mirror.
1572  * @m_next:   Point to the next node of the list.
1573  *
1574  * Command-line arguments for mirror(s) will be parsed and stored in
1575  * a linked list that consists of this structure.
1576  */
1577 struct mirror_args {
1578         __u32                   m_count;
1579         __u32                   m_flags;
1580         struct llapi_layout     *m_layout;
1581         const char              *m_file;
1582         struct mirror_args      *m_next;
1583         bool                    m_inherit;
1584 };
1585
1586 /**
1587  * enum mirror_flags - Flags for extending a mirrored file.
1588  * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1589  *             in case the victim file(s) contains the same data as the
1590  *             original mirrored file.
1591  * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1592  * @MF_COMP_ID: specified component id instead of mirror id
1593  *
1594  * Flags for extending a mirrored file.
1595  */
1596 enum mirror_flags {
1597         MF_NO_VERIFY    = 0x1,
1598         MF_DESTROY      = 0x2,
1599         MF_COMP_ID      = 0x4,
1600         MF_COMP_POOL    = 0x8,
1601 };
1602
1603 /**
1604  * mirror_create_sanity_check() - Check mirror list.
1605  * @list:  A linked list that stores the mirror arguments.
1606  *
1607  * This function does a sanity check on @list for creating
1608  * a mirrored file.
1609  *
1610  * Return: 0 on success or a negative error code on failure.
1611  */
1612 static int mirror_create_sanity_check(const char *fname,
1613                                       struct mirror_args *list,
1614                                       bool check_fname)
1615 {
1616         int rc = 0;
1617         bool has_m_file = false;
1618         bool has_m_layout = false;
1619
1620         if (!list)
1621                 return -EINVAL;
1622
1623         if (fname && check_fname) {
1624                 struct llapi_layout *layout;
1625
1626                 layout = llapi_layout_get_by_path(fname, 0);
1627                 if (!layout) {
1628                         fprintf(stderr,
1629                                 "error: %s: file '%s' couldn't get layout\n",
1630                                 progname, fname);
1631                         return -ENODATA;
1632                 }
1633
1634                 rc = llapi_layout_sanity(layout, fname, false, true);
1635
1636                 llapi_layout_free(layout);
1637
1638                 if (rc) {
1639                         llapi_layout_sanity_perror(rc);
1640                         return rc;
1641                 }
1642         }
1643
1644         while (list) {
1645                 if (list->m_file) {
1646                         has_m_file = true;
1647                         llapi_layout_free(list->m_layout);
1648
1649                         list->m_layout =
1650                                 llapi_layout_get_by_path(list->m_file, 0);
1651                         if (!list->m_layout) {
1652                                 fprintf(stderr,
1653                                         "error: %s: file '%s' has no layout\n",
1654                                         progname, list->m_file);
1655                                 return -ENODATA;
1656                         }
1657                 } else {
1658                         has_m_layout = true;
1659                         if (!list->m_layout) {
1660                                 fprintf(stderr, "error: %s: no mirror layout\n",
1661                                         progname);
1662                                 return -EINVAL;
1663                         }
1664                 }
1665
1666                 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1667                 if (rc) {
1668                         llapi_layout_sanity_perror(rc);
1669                         return rc;
1670                 }
1671
1672                 list = list->m_next;
1673         }
1674
1675         if (has_m_file && has_m_layout) {
1676                 fprintf(stderr,
1677                         "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1678                         progname);
1679                 return -EINVAL;
1680         }
1681
1682         return 0;
1683 }
1684
1685 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1686 {
1687         __u32 mirror_flags = *(__u32 *)cbdata;
1688         uint32_t flags;
1689         int rc;
1690
1691         rc = llapi_layout_comp_flags_get(layout, &flags);
1692         if (rc < 0)
1693                 return rc;
1694
1695         if (!flags) {
1696                 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1697                 if (rc)
1698                         return rc;
1699         }
1700
1701         return LLAPI_LAYOUT_ITER_CONT;
1702 }
1703
1704 /**
1705  * mirror_create() - Create a mirrored file.
1706  * @fname:        The file to be created.
1707  * @mirror_list:  A linked list that stores the mirror arguments.
1708  *
1709  * This function creates a mirrored file @fname with the mirror(s)
1710  * from @mirror_list.
1711  *
1712  * Return: 0 on success or a negative error code on failure.
1713  */
1714 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1715 {
1716         struct llapi_layout *layout = NULL;
1717         struct mirror_args *cur_mirror = NULL;
1718         uint16_t mirror_count = 0;
1719         int i = 0;
1720         int rc = 0;
1721
1722         rc = mirror_create_sanity_check(fname, mirror_list, false);
1723         if (rc)
1724                 return rc;
1725
1726         cur_mirror = mirror_list;
1727         while (cur_mirror) {
1728                 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1729                                                mirror_set_flags,
1730                                                &cur_mirror->m_flags);
1731                 if (rc) {
1732                         rc = -errno;
1733                         fprintf(stderr, "%s: failed to set mirror flags\n",
1734                                 progname);
1735                         goto error;
1736                 }
1737
1738                 for (i = 0; i < cur_mirror->m_count; i++) {
1739                         rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1740                         if (rc) {
1741                                 rc = -errno;
1742                                 fprintf(stderr,
1743                                         "error: %s: merge layout failed: %s\n",
1744                                         progname, strerror(errno));
1745                                 goto error;
1746                         }
1747                 }
1748                 mirror_count += cur_mirror->m_count;
1749                 cur_mirror = cur_mirror->m_next;
1750         }
1751
1752         if (!layout) {
1753                 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1754                 return -EINVAL;
1755         }
1756
1757         rc = llapi_layout_mirror_count_set(layout, mirror_count);
1758         if (rc) {
1759                 rc = -errno;
1760                 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1761                         progname, strerror(errno));
1762                 goto error;
1763         }
1764
1765         rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1766                                   layout);
1767         if (rc >= 0) {
1768                 close(rc);
1769                 rc = 0;
1770         }
1771
1772 error:
1773         llapi_layout_free(layout);
1774         return rc;
1775 }
1776
1777 /**
1778  * Compare files and check lease on @fd.
1779  *
1780  * \retval bytes number of bytes are the same
1781  */
1782 static ssize_t mirror_file_compare(int fd, int fdv)
1783 {
1784         const size_t buflen = 4 * 1024 * 1024; /* 4M */
1785         void *buf;
1786         ssize_t bytes_done = 0;
1787         ssize_t bytes_read = 0;
1788
1789         buf = malloc(buflen * 2);
1790         if (!buf)
1791                 return -ENOMEM;
1792
1793         while (1) {
1794                 if (!llapi_lease_check(fd)) {
1795                         bytes_done = -EBUSY;
1796                         break;
1797                 }
1798
1799                 bytes_read = read(fd, buf, buflen);
1800                 if (bytes_read <= 0)
1801                         break;
1802
1803                 if (bytes_read != read(fdv, buf + buflen, buflen))
1804                         break;
1805
1806                 /*
1807                  * XXX: should compute the checksum on each buffer and then
1808                  * compare checksum to avoid cache collision
1809                  */
1810                 if (memcmp(buf, buf + buflen, bytes_read))
1811                         break;
1812
1813                 bytes_done += bytes_read;
1814         }
1815
1816         free(buf);
1817
1818         return bytes_done;
1819 }
1820
1821 static int mirror_extend_file(const char *fname, const char *victim_file,
1822                               enum mirror_flags mirror_flags)
1823 {
1824         int fd = -1;
1825         int fdv = -1;
1826         struct stat stbuf;
1827         struct stat stbuf_v;
1828         struct ll_ioc_lease *data = NULL;
1829         int rc;
1830
1831         fd = open(fname, O_RDWR);
1832         if (fd < 0) {
1833                 error_loc = "open source file";
1834                 rc = -errno;
1835                 goto out;
1836         }
1837
1838         fdv = open(victim_file, O_RDWR);
1839         if (fdv < 0) {
1840                 error_loc = "open target file";
1841                 rc = -errno;
1842                 goto out;
1843         }
1844
1845         if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1846                 error_loc = "stat source or target file";
1847                 rc = -errno;
1848                 goto out;
1849         }
1850
1851         if (stbuf.st_dev != stbuf_v.st_dev) {
1852                 error_loc = "stat source and target file";
1853                 rc = -EXDEV;
1854                 goto out;
1855         }
1856
1857         /* mirrors should be of the same size */
1858         if (stbuf.st_size != stbuf_v.st_size) {
1859                 error_loc = "file sizes don't match";
1860                 rc = -EINVAL;
1861                 goto out;
1862         }
1863
1864         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1865         if (rc < 0) {
1866                 error_loc = "cannot get lease";
1867                 goto out;
1868         }
1869
1870         if (!(mirror_flags & MF_NO_VERIFY)) {
1871                 ssize_t ret;
1872                 /* mirrors should have the same contents */
1873                 ret = mirror_file_compare(fd, fdv);
1874                 if (ret != stbuf.st_size) {
1875                         error_loc = "file busy or contents don't match";
1876                         rc = ret < 0 ? ret : -EINVAL;
1877                         goto out;
1878                 }
1879         }
1880
1881         /* Get rid of caching pages from clients */
1882         rc = llapi_file_flush(fd);
1883         if (rc < 0) {
1884                 error_loc = "cannot get data version";
1885                 goto out;
1886         }
1887
1888         rc = llapi_file_flush(fdv);
1889         if (rc < 0) {
1890                 error_loc = "cannot get data version";
1891                 goto out;
1892         }
1893
1894         /* Make sure we keep original atime/mtime values */
1895         rc = migrate_copy_timestamps(fd, fdv);
1896         if (rc < 0) {
1897                 error_loc = "cannot copy timestamp";
1898                 goto out;
1899         }
1900
1901         /* Atomically put lease, merge layouts and close. */
1902         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1903         if (!data) {
1904                 error_loc = "memory allocation";
1905                 goto out;
1906         }
1907         data->lil_mode = LL_LEASE_UNLCK;
1908         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1909         data->lil_count = 1;
1910         data->lil_ids[0] = fdv;
1911         rc = llapi_lease_set(fd, data);
1912         if (rc < 0) {
1913                 error_loc = "cannot merge layout";
1914                 goto out;
1915         } else if (rc == 0) {
1916                 rc = -EBUSY;
1917                 error_loc = "lost lease lock";
1918                 goto out;
1919         }
1920         rc = 0;
1921
1922 out:
1923         if (data)
1924                 free(data);
1925         if (fd >= 0)
1926                 close(fd);
1927         if (fdv >= 0)
1928                 close(fdv);
1929         if (!rc)
1930                 (void) unlink(victim_file);
1931         if (rc < 0)
1932                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1933                         progname, fname, error_loc, strerror(-rc));
1934         return rc;
1935 }
1936
1937 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1938                                 bool inherit, uint32_t flags)
1939 {
1940         struct llapi_layout *f_layout = NULL;
1941         struct ll_ioc_lease *data = NULL;
1942         int fd = -1;
1943         int fdv = -1;
1944         int rc = 0;
1945
1946         if (inherit) {
1947                 f_layout = llapi_layout_get_by_path(name, 0);
1948                 if (!f_layout) {
1949                         fprintf(stderr, "%s: cannot get layout\n", progname);
1950                         goto out;
1951                 }
1952                 rc = llapi_layout_get_last_init_comp(f_layout);
1953                 if (rc) {
1954                         fprintf(stderr, "%s: cannot get the last init comp\n",
1955                                 progname);
1956                         goto out;
1957                 }
1958                 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1959                 if (rc) {
1960                         fprintf(stderr,
1961                                 "%s: cannot inherit from the last init comp\n",
1962                                 progname);
1963                         goto out;
1964                 }
1965         }
1966         llapi_layout_comp_flags_set(m_layout, flags);
1967         rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1968                                 &fdv);
1969         if (rc < 0)
1970                 goto out;
1971
1972         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1973         if (rc < 0) {
1974                 error_loc = "cannot get lease";
1975                 goto out;
1976         }
1977
1978         rc = migrate_nonblock(fd, fdv);
1979         if (rc < 0) {
1980                 llapi_lease_release(fd);
1981                 goto out;
1982         }
1983
1984         /* Atomically put lease, merge layouts and close. */
1985         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1986         if (!data) {
1987                 error_loc = "memory allocation";
1988                 goto out;
1989         }
1990         data->lil_mode = LL_LEASE_UNLCK;
1991         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1992         data->lil_count = 1;
1993         data->lil_ids[0] = fdv;
1994         rc = llapi_lease_set(fd, data);
1995         if (rc < 0) {
1996                 error_loc = "cannot merge layout";
1997                 goto out;
1998         } else if (rc == 0) {
1999                 rc = -EBUSY;
2000                 error_loc = "lost lease lock";
2001                 goto out;
2002         }
2003         rc = 0;
2004
2005 out:
2006         if (data)
2007                 free(data);
2008         if (fd >= 0)
2009                 close(fd);
2010         if (fdv >= 0)
2011                 close(fdv);
2012         if (rc < 0)
2013                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2014                         progname, name, error_loc, strerror(-rc));
2015         return rc;
2016 }
2017
2018 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2019                          enum mirror_flags mirror_flags)
2020 {
2021         int rc;
2022
2023         rc = mirror_create_sanity_check(fname, mirror_list, true);
2024         if (rc)
2025                 return rc;
2026
2027         while (mirror_list) {
2028                 if (mirror_list->m_file) {
2029                         rc = mirror_extend_file(fname, mirror_list->m_file,
2030                                                 mirror_flags);
2031                 } else {
2032                         __u32 mirror_count = mirror_list->m_count;
2033
2034                         while (mirror_count > 0) {
2035                                 rc = mirror_extend_layout(fname,
2036                                                         mirror_list->m_layout,
2037                                                         mirror_list->m_inherit,
2038                                                         mirror_list->m_flags);
2039                                 if (rc)
2040                                         break;
2041
2042                                 --mirror_count;
2043                         }
2044                 }
2045                 if (rc)
2046                         break;
2047
2048                 mirror_list = mirror_list->m_next;
2049         }
2050
2051         return rc;
2052 }
2053
2054 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2055 {
2056         uint32_t id;
2057         int rc;
2058
2059         rc = llapi_layout_mirror_id_get(layout, &id);
2060         if (rc < 0)
2061                 return rc;
2062
2063         if ((__u16)id == *(__u16 *)cbdata)
2064                 return LLAPI_LAYOUT_ITER_STOP;
2065
2066         return LLAPI_LAYOUT_ITER_CONT;
2067 }
2068
2069 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2070 {
2071         uint32_t id;
2072         int rc;
2073
2074         rc = llapi_layout_comp_id_get(layout, &id);
2075         if (rc < 0)
2076                 return rc;
2077
2078         if (id == *(__u32 *)cbdata)
2079                 return LLAPI_LAYOUT_ITER_STOP;
2080
2081         return LLAPI_LAYOUT_ITER_CONT;
2082 }
2083
2084 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2085 {
2086         char buf[LOV_MAXPOOLNAME + 1];
2087         struct pool_to_id_cbdata *d = (void *)cbdata;
2088         uint32_t id;
2089         int rc;
2090
2091         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2092         if (rc < 0)
2093                 return rc;
2094         if (strcmp(d->pool, buf))
2095                 return LLAPI_LAYOUT_ITER_CONT;
2096
2097         rc = llapi_layout_mirror_id_get(layout, &id);
2098         if (rc < 0)
2099                 return rc;
2100         d->id = id;
2101
2102         return LLAPI_LAYOUT_ITER_STOP;
2103 }
2104
2105 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2106 {
2107         char buf[LOV_MAXPOOLNAME + 1];
2108         struct pool_to_id_cbdata *d = (void *)cbdata;
2109         uint32_t id;
2110         int rc;
2111
2112         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2113         if (rc < 0)
2114                 return rc;
2115         if (strcmp(d->pool, buf))
2116                 return LLAPI_LAYOUT_ITER_CONT;
2117
2118         rc = llapi_layout_comp_id_get(layout, &id);
2119         if (rc < 0)
2120                 return rc;
2121         d->id = id;
2122
2123         return LLAPI_LAYOUT_ITER_STOP;
2124 }
2125
2126 struct collect_ids_data {
2127         __u16   *cid_ids;
2128         int     cid_count;
2129         __u16   cid_exclude;
2130 };
2131
2132 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2133 {
2134         struct collect_ids_data *cid = cbdata;
2135         uint32_t id;
2136         int rc;
2137
2138         rc = llapi_layout_mirror_id_get(layout, &id);
2139         if (rc < 0)
2140                 return rc;
2141
2142         if ((__u16)id != cid->cid_exclude) {
2143                 int i;
2144
2145                 for (i = 0; i < cid->cid_count; i++) {
2146                         /* already collected the mirror id */
2147                         if (id == cid->cid_ids[i])
2148                                 return LLAPI_LAYOUT_ITER_CONT;
2149                 }
2150                 cid->cid_ids[cid->cid_count] = id;
2151                 cid->cid_count++;
2152         }
2153
2154         return LLAPI_LAYOUT_ITER_CONT;
2155 }
2156
2157 /**
2158  * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2159  * @mirror_id: Mirror id to be checked.
2160  * @layout:    Mirror component list.
2161  *
2162  * This function checks if a mirror with specified @mirror_id is the last
2163  * non-stale mirror of a layout @layout.
2164  *
2165  * Return: true or false.
2166  */
2167 static inline
2168 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2169 {
2170         __u16 mirror_ids[128] = { 0 };
2171         struct collect_ids_data cid = { .cid_ids = mirror_ids,
2172                                         .cid_count = 0,
2173                                         .cid_exclude = mirror_id, };
2174         int i;
2175
2176         llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2177
2178         for (i = 0; i < cid.cid_count; i++) {
2179                 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2180                 int comp_size = 0;
2181
2182                 comp_size = llapi_mirror_find_stale(layout, comp_array,
2183                                                     ARRAY_SIZE(comp_array),
2184                                                     &mirror_ids[i], 1);
2185                 if (comp_size == 0)
2186                         return false;
2187         }
2188
2189         return true;
2190 }
2191
2192 static int mirror_split(const char *fname, __u32 id, const char *pool,
2193                         enum mirror_flags mflags, const char *victim_file)
2194 {
2195         struct llapi_layout *layout;
2196         char parent[PATH_MAX];
2197         char victim[PATH_MAX];
2198         int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2199         char *ptr;
2200         struct ll_ioc_lease *data;
2201         uint16_t mirror_count;
2202         __u32 mirror_id;
2203         int mdt_index;
2204         int fd, fdv;
2205         bool purge = true; /* delete mirror by setting fdv=fd */
2206         bool is_encrypted;
2207         int rc;
2208
2209         if (victim_file && (strcmp(fname, victim_file) == 0)) {
2210                 fprintf(stderr,
2211                         "error %s: the source file '%s' and -f file are the same\n",
2212                         progname, fname);
2213                 return -EINVAL;
2214         }
2215
2216         /* check fname contains mirror with mirror_id/comp_id */
2217         layout = llapi_layout_get_by_path(fname, 0);
2218         if (!layout) {
2219                 fprintf(stderr,
2220                         "error %s: file '%s' couldn't get layout\n",
2221                         progname, fname);
2222                 return -EINVAL;
2223         }
2224
2225         rc = llapi_layout_sanity(layout, fname, false, true);
2226         if (rc) {
2227                 llapi_layout_sanity_perror(rc);
2228                 goto free_layout;
2229         }
2230
2231         rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2232         if (rc) {
2233                 fprintf(stderr,
2234                         "error %s: file '%s' couldn't get mirror count\n",
2235                         progname, fname);
2236                 goto free_layout;
2237         }
2238         if (mirror_count < 2) {
2239                 fprintf(stderr,
2240                         "error %s: file '%s' has %d component, cannot split\n",
2241                         progname, fname, mirror_count);
2242                 goto free_layout;
2243         }
2244
2245         if (mflags & MF_COMP_POOL) {
2246                 struct pool_to_id_cbdata data = { .pool = pool };
2247
2248                 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2249                                                &data);
2250                 mirror_id = data.id;
2251         } else if (mflags & MF_COMP_ID) {
2252                 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2253                 mirror_id = mirror_id_of(id);
2254         } else {
2255                 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2256                 mirror_id = id;
2257         }
2258         if (rc < 0) {
2259                 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2260                         progname, fname);
2261                 goto free_layout;
2262         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2263                 if (mflags & MF_COMP_POOL) {
2264                         fprintf(stderr,
2265                                 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2266                                 progname, fname, pool);
2267                         goto free_layout;
2268                 } else if (mflags & MF_COMP_ID) {
2269                         fprintf(stderr,
2270                                 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2271                                 progname, fname, id);
2272                         goto free_layout;
2273                 } else {
2274                         fprintf(stderr,
2275                                 "error %s: file '%s' does not contain mirror with id %u\n",
2276                                 progname, fname, id);
2277                         goto free_layout;
2278                 }
2279         }
2280
2281         fd = open(fname, O_RDWR);
2282         if (fd < 0) {
2283                 fprintf(stderr,
2284                         "error %s: open file '%s' failed: %s\n",
2285                         progname, fname, strerror(errno));
2286                 goto free_layout;
2287         }
2288
2289         /* get victim file directory pathname */
2290         if (strlen(fname) > sizeof(parent) - 1) {
2291                 fprintf(stderr, "error %s: file name of '%s' too long\n",
2292                         progname, fname);
2293                 rc = -ERANGE;
2294                 goto close_fd;
2295         }
2296         strncpy(parent, fname, sizeof(parent));
2297         ptr = strrchr(parent, '/');
2298         if (!ptr) {
2299                 if (!getcwd(parent, sizeof(parent))) {
2300                         fprintf(stderr, "error %s: getcwd failed: %s\n",
2301                                 progname, strerror(errno));
2302                         rc = -errno;
2303                         goto close_fd;
2304                 }
2305         } else {
2306                 if (ptr == parent)
2307                         ptr = parent + 1;
2308                 *ptr = '\0';
2309         }
2310
2311         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2312         if (rc < 0) {
2313                 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2314                         progname, fname);
2315                 goto close_fd;
2316         }
2317
2318         rc = llapi_file_is_encrypted(fd);
2319         if (rc < 0) {
2320                 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2321                         progname, fname, rc);
2322                 goto close_fd;
2323         }
2324         is_encrypted = rc;
2325
2326 again:
2327         if (!victim_file) {
2328                 /* use a temp file to store the splitted layout */
2329                 if (mflags & MF_DESTROY) {
2330                         char file_path[PATH_MAX];
2331                         unsigned int rnumber;
2332                         int open_flags;
2333
2334                         if (last_non_stale_mirror(mirror_id, layout)) {
2335                                 rc = -EUCLEAN;
2336                                 fprintf(stderr,
2337                                         "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2338                                         progname, fname);
2339                                 goto close_fd;
2340                         }
2341
2342                         if (purge) {
2343                                 /* don't use volatile file for mirror destroy */
2344                                 fdv = fd;
2345                         } else {
2346                                 /**
2347                                  * try the old way to delete mirror using
2348                                  * volatile file.
2349                                  */
2350                                 do {
2351                                         rnumber = random();
2352                                         rc = snprintf(file_path,
2353                                                       sizeof(file_path),
2354                                                       "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2355                                                       parent, mdt_index,
2356                                                       rnumber, fd);
2357                                         if (rc < 0 ||
2358                                             rc >= sizeof(file_path)) {
2359                                                 fdv = -ENAMETOOLONG;
2360                                                 break;
2361                                         }
2362
2363                                         open_flags = O_RDWR |
2364                                              (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2365                                              O_CREAT | O_EXCL | O_NOFOLLOW;
2366                                         fdv = open(file_path, open_flags,
2367                                                    S_IRUSR | S_IWUSR);
2368                                         if (fdv < 0)
2369                                                 rc = -errno;
2370                                 } while (fdv < 0 && rc == -EEXIST);
2371                         }
2372                 } else {
2373                         if (is_encrypted) {
2374                                 rc = -1;
2375                                 fprintf(stderr,
2376                                         "error %s: not permitted on encrypted file '%s': %d\n",
2377                                         progname, fname, rc);
2378                                 goto close_fd;
2379                         }
2380
2381                         snprintf(victim, sizeof(victim), "%s.mirror~%u",
2382                                  fname, mirror_id);
2383                         fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2384                 }
2385         } else {
2386                 /* user specified victim file */
2387                 if (is_encrypted) {
2388                         rc = -1;
2389                         fprintf(stderr,
2390                                 "error %s: not permitted on encrypted file '%s': %d\n",
2391                                 progname, fname, rc);
2392                         goto close_fd;
2393                 }
2394                 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2395         }
2396
2397         if (fdv < 0) {
2398                 fprintf(stderr,
2399                         "error %s: create victim file failed: %s\n",
2400                         progname, strerror(errno));
2401                 goto close_fd;
2402         }
2403
2404         /* get lease lock of fname */
2405         rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2406         if (rc < 0) {
2407                 fprintf(stderr,
2408                         "error %s: cannot get lease of file '%s': %d\n",
2409                         progname, fname, rc);
2410                 goto close_victim;
2411         }
2412
2413         /* Atomatically put lease, split layouts and close. */
2414         data = malloc(offsetof(typeof(*data), lil_ids[2]));
2415         if (!data) {
2416                 rc = -ENOMEM;
2417                 goto close_victim;
2418         }
2419
2420         data->lil_mode = LL_LEASE_UNLCK;
2421         data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2422         data->lil_count = 2;
2423         data->lil_ids[0] = fdv;
2424         data->lil_ids[1] = mirror_id;
2425         rc = llapi_lease_set(fd, data);
2426         if (rc <= 0) {
2427                 if (rc == -EINVAL && purge) {
2428                         /* could be old MDS which prohibit fd==fdv */
2429                         purge = false;
2430                         goto again;
2431
2432                 }
2433                 if (rc == 0) /* lost lease lock */
2434                         rc = -EBUSY;
2435                 fprintf(stderr,
2436                         "error %s: cannot split '%s': %s\n",
2437                         progname, fname, strerror(-rc));
2438         } else {
2439                 rc = 0;
2440         }
2441         free(data);
2442
2443 close_victim:
2444         if (!purge)
2445                 close(fdv);
2446 close_fd:
2447         close(fd);
2448 free_layout:
2449         llapi_layout_free(layout);
2450         return rc;
2451 }
2452
2453 static inline
2454 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2455                            __u16 *mirror_ids, int ids_nr);
2456
2457 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2458                               __u64 migration_flags,
2459                               struct llapi_stripe_param *param,
2460                               struct llapi_layout *layout)
2461 {
2462         struct ll_ioc_lease *data = NULL;
2463         int rc;
2464
2465         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2466         if (rc < 0) {
2467                 error_loc = "cannot get lease";
2468                 goto out_close;
2469         }
2470
2471         /* Atomically put lease, merge layouts, resync and close. */
2472         data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2473         if (!data) {
2474                 error_loc = "memory allocation";
2475                 goto out_close;
2476         }
2477         data->lil_mode = LL_LEASE_UNLCK;
2478         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2479         data->lil_count = 1;
2480         data->lil_ids[0] = fdv;
2481         rc = llapi_lease_set(fd, data);
2482         if (rc < 0) {
2483                 error_loc = "cannot merge layout";
2484                 goto out_close;
2485         } else if (rc == 0) {
2486                 rc = -EBUSY;
2487                 error_loc = "lost lease lock";
2488                 goto out_close;
2489         }
2490         close(fd);
2491         close(fdv);
2492
2493         rc = lfs_mirror_resync_file(name, data, NULL, 0);
2494         if (rc) {
2495                 error_loc = "cannot resync file";
2496                 goto out;
2497         }
2498
2499         /* delete first mirror now */
2500         rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2501         if (rc < 0)
2502                 error_loc = "cannot delete old layout";
2503         goto out;
2504
2505 out_close:
2506         close(fd);
2507         close(fdv);
2508 out:
2509         if (rc < 0)
2510                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2511                         progname, name, error_loc, strerror(-rc));
2512         else if (migration_flags & MIGRATION_VERBOSE)
2513                 printf("%s\n", name);
2514         if (data)
2515                 free(data);
2516         return rc;
2517 }
2518
2519 /**
2520  * Parse a string containing an target index list into an array of integers.
2521  *
2522  * The input string contains a comma delimited list of individual
2523  * indices and ranges, for example "1,2-4,7". Add the indices into the
2524  * \a tgts array and remove duplicates.
2525  *
2526  * \param[out] tgts             array to store indices in
2527  * \param[in] size              size of \a tgts array
2528  * \param[in] offset            starting index in \a tgts
2529  * \param[in] arg               string containing OST index list
2530  * \param[in/out] overstriping  index list may contain duplicates
2531  *
2532  * \retval positive    number of indices in \a tgts
2533  * \retval -EINVAL     unable to parse \a arg
2534  */
2535 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2536                          unsigned long long *pattern)
2537 {
2538         int rc;
2539         int nr = offset;
2540         int slots = size - offset;
2541         char *ptr = NULL;
2542         bool overstriped = false;
2543         bool end_of_loop;
2544
2545         if (!arg)
2546                 return -EINVAL;
2547
2548         end_of_loop = false;
2549         while (!end_of_loop) {
2550                 int start_index = 0;
2551                 int end_index = 0;
2552                 int i;
2553                 char *endptr = NULL;
2554
2555                 rc = -EINVAL;
2556
2557                 ptr = strchrnul(arg, ',');
2558
2559                 end_of_loop = *ptr == '\0';
2560                 *ptr = '\0';
2561
2562                 errno = 0;
2563                 start_index = strtol(arg, &endptr, 0);
2564                 if (endptr == arg) /* no data at all */
2565                         break;
2566                 if (errno != 0 || start_index < -1 ||
2567                     (*endptr != '-' && *endptr != '\0'))
2568                         break;
2569
2570                 end_index = start_index;
2571                 if (*endptr == '-') {
2572                         errno = 0;
2573                         end_index = strtol(endptr + 1, &endptr, 0);
2574                         if (errno != 0 || *endptr != '\0' || end_index < -1)
2575                                 break;
2576                         if (end_index < start_index)
2577                                 break;
2578                 }
2579
2580                 for (i = start_index; i <= end_index && slots > 0; i++) {
2581                         int j;
2582
2583                         /* remove duplicate */
2584                         for (j = 0; j < offset; j++) {
2585                                 if (tgts[j] == i && pattern &&
2586                                     *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2587                                         overstriped = true;
2588                                 else if (tgts[j] == i)
2589                                         return -EINVAL;
2590                         }
2591
2592                         j = offset;
2593
2594                         if (j == offset) { /* check complete */
2595                                 tgts[nr++] = i;
2596                                 --slots;
2597                         }
2598                 }
2599
2600                 if (slots == 0 && i < end_index)
2601                         break;
2602
2603                 *ptr = ',';
2604                 arg = ++ptr;
2605                 offset = nr;
2606                 rc = 0;
2607         }
2608         if (!end_of_loop && ptr)
2609                 *ptr = ',';
2610
2611         if (!overstriped && pattern)
2612                 *pattern = LLAPI_LAYOUT_DEFAULT;
2613
2614         return rc < 0 ? rc : nr;
2615 }
2616
2617 struct lfs_setstripe_args {
2618         unsigned long long       lsa_comp_end;
2619         unsigned long long       lsa_stripe_size;
2620         unsigned long long       lsa_extension_size;
2621         long long                lsa_stripe_count;
2622         long long                lsa_stripe_off;
2623         __u32                    lsa_comp_flags;
2624         __u32                    lsa_comp_neg_flags;
2625         unsigned long long       lsa_pattern;
2626         unsigned int             lsa_mirror_count;
2627         int                      lsa_nr_tgts;
2628         bool                     lsa_first_comp;
2629         bool                     lsa_extension_comp;
2630         __u32                   *lsa_tgts;
2631         char                    *lsa_pool_name;
2632 };
2633
2634 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2635 {
2636         unsigned int mirror_count = lsa->lsa_mirror_count;
2637         bool first_comp = lsa->lsa_first_comp;
2638
2639         memset(lsa, 0, sizeof(*lsa));
2640
2641         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2642         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2643         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2644         lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2645         lsa->lsa_pool_name = NULL;
2646
2647         lsa->lsa_mirror_count = mirror_count;
2648         lsa->lsa_first_comp = first_comp;
2649 }
2650
2651 /**
2652  * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2653  * @lsa: Stripe options to be initialized and inherited.
2654  *
2655  * This function initializes stripe options in @lsa and inherit
2656  * stripe_size, stripe_count and OST pool_name options.
2657  *
2658  * Return: void.
2659  */
2660 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2661 {
2662         unsigned long long stripe_size;
2663         long long stripe_count;
2664         char *pool_name = NULL;
2665
2666         stripe_size = lsa->lsa_stripe_size;
2667         stripe_count = lsa->lsa_stripe_count;
2668         pool_name = lsa->lsa_pool_name;
2669
2670         setstripe_args_init(lsa);
2671
2672         lsa->lsa_stripe_size = stripe_size;
2673         lsa->lsa_stripe_count = stripe_count;
2674         lsa->lsa_pool_name = pool_name;
2675 }
2676
2677 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2678 {
2679         return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2680                 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2681                 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2682                 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2683                 lsa->lsa_comp_end != 0);
2684 }
2685
2686 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2687 {
2688         if (lsa->lsa_nr_tgts) {
2689                 if (lsa->lsa_nr_tgts < 0 ||
2690                     lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2691                         fprintf(stderr, "Invalid nr_tgts(%d)\n",
2692                                 lsa->lsa_nr_tgts);
2693                         errno = EINVAL;
2694                         return -1;
2695                 }
2696
2697                 if (lsa->lsa_stripe_count > 0 &&
2698                     lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2699                     lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2700                     lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2701                         fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2702                                 lsa->lsa_stripe_count,
2703                                 lsa->lsa_nr_tgts);
2704                         errno = EINVAL;
2705                         return -1;
2706                 }
2707         }
2708
2709         return 0;
2710
2711 }
2712
2713 /**
2714  * comp_args_to_layout() - Create or extend a composite layout.
2715  * @composite:       Pointer to the composite layout.
2716  * @lsa:             Stripe options for the new component.
2717  *
2718  * This function creates or extends a composite layout by adding a new
2719  * component with stripe options from @lsa.
2720  *
2721  * When modified, adjust llapi_stripe_param_verify() if needed as well.
2722  *
2723  * Return: 0 on success or an error code on failure.
2724  */
2725 static int comp_args_to_layout(struct llapi_layout **composite,
2726                                struct lfs_setstripe_args *lsa,
2727                                bool set_extent)
2728 {
2729         struct llapi_layout *layout = *composite;
2730         uint64_t prev_end = 0;
2731         uint64_t size;
2732         int i = 0, rc;
2733
2734 new_comp:
2735         if (!layout) {
2736                 layout = llapi_layout_alloc();
2737                 if (!layout) {
2738                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2739                                 strerror(errno));
2740                         errno = ENOMEM;
2741                         return -1;
2742                 }
2743                 *composite = layout;
2744                 lsa->lsa_first_comp = true;
2745         } else {
2746                 uint64_t start;
2747
2748                 /*
2749                  * Get current component extent, current component
2750                  * must be the tail component.
2751                  */
2752                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2753                 if (rc) {
2754                         fprintf(stderr, "Get comp extent failed. %s\n",
2755                                 strerror(errno));
2756                         return rc;
2757                 }
2758
2759                 if (lsa->lsa_first_comp) {
2760                         prev_end = 0;
2761                         rc = llapi_layout_add_first_comp(layout);
2762                 } else {
2763                         rc = llapi_layout_comp_add(layout);
2764                 }
2765                 if (rc) {
2766                         fprintf(stderr, "Add component failed. %s\n",
2767                                 strerror(errno));
2768                         return rc;
2769                 }
2770         }
2771
2772         rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2773         if (rc) {
2774                 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2775                         lsa->lsa_comp_flags, strerror(errno));
2776                 return rc;
2777         }
2778
2779         if (set_extent) {
2780                 uint64_t comp_end = lsa->lsa_comp_end;
2781
2782                 /*
2783                  * The extendable component is 0-length, so it can be removed
2784                  * if there is insufficient space to extend it.
2785                  */
2786                 if (lsa->lsa_extension_comp)
2787                         comp_end = prev_end;
2788
2789                 rc = llapi_layout_comp_extent_set(layout, prev_end,
2790                                                   comp_end);
2791                 if (rc) {
2792                         fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2793                                 prev_end, comp_end, strerror(errno));
2794                         return rc;
2795                 }
2796         }
2797         /* reset lsa_first_comp */
2798         lsa->lsa_first_comp = false;
2799
2800         /* Data-on-MDT component setting */
2801         if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2802                 /*
2803                  * In case of Data-on-MDT patterns the only extra option
2804                  * applicable is stripe size option.
2805                  */
2806                 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2807                         fprintf(stderr,
2808                                 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2809                                 lsa->lsa_stripe_count);
2810                         errno = EINVAL;
2811                         return -1;
2812                 }
2813                 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2814                         fprintf(stderr,
2815                                 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2816                                 lsa->lsa_stripe_size);
2817                         errno = EINVAL;
2818                         return -1;
2819                 }
2820                 if (lsa->lsa_nr_tgts != 0) {
2821                         fprintf(stderr,
2822                                 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2823                                 lsa->lsa_nr_tgts);
2824                         errno = EINVAL;
2825                         return -1;
2826                 }
2827                 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2828                         fprintf(stderr,
2829                                 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2830                                 lsa->lsa_stripe_off);
2831                         errno = EINVAL;
2832                         return -1;
2833                 }
2834                 if (lsa->lsa_pool_name != 0) {
2835                         fprintf(stderr,
2836                                 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2837                                 lsa->lsa_pool_name);
2838                         errno = EINVAL;
2839                         return -1;
2840                 }
2841
2842                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2843                 if (rc) {
2844                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2845                                 lsa->lsa_pattern,
2846                                 strerror(errno));
2847                         return rc;
2848                 }
2849                 /* Data-on-MDT component has always single stripe up to end */
2850                 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2851         } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2852                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2853                 if (rc) {
2854                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2855                                 lsa->lsa_pattern,
2856                                 strerror(errno));
2857                         return rc;
2858                 }
2859         }
2860
2861         size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2862                 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2863
2864         if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2865                 rc = llapi_layout_extension_size_set(layout, size);
2866         else
2867                 rc = llapi_layout_stripe_size_set(layout, size);
2868
2869         if (rc) {
2870                 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2871                         size, strerror(errno));
2872                 return rc;
2873         }
2874
2875         rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2876         if (rc) {
2877                 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2878                         lsa->lsa_stripe_count, strerror(errno));
2879                 return rc;
2880         }
2881
2882         if (lsa->lsa_pool_name) {
2883                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2884                 if (rc) {
2885                         fprintf(stderr, "Set pool name: %s failed. %s\n",
2886                                 lsa->lsa_pool_name, strerror(errno));
2887                         return rc;
2888                 }
2889         } else {
2890                 rc = llapi_layout_pool_name_set(layout, "");
2891                 if (rc) {
2892                         fprintf(stderr, "Clear pool name failed: %s\n",
2893                                 strerror(errno));
2894                         return rc;
2895                 }
2896         }
2897
2898         rc = lsa_args_stripe_count_check(lsa);
2899         if (rc)
2900                 return rc;
2901
2902         if (lsa->lsa_nr_tgts > 0) {
2903                 bool found = false;
2904
2905                 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2906                         rc = llapi_layout_ost_index_set(layout, i,
2907                                                         lsa->lsa_tgts[i]);
2908                         if (rc)
2909                                 break;
2910
2911                         /* Make sure stripe offset is in OST list. */
2912                         if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2913                                 found = true;
2914                 }
2915                 if (!found) {
2916                         fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2917                                 lsa->lsa_stripe_off);
2918                         errno = EINVAL;
2919                         return -1;
2920                 }
2921         } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2922                    lsa->lsa_stripe_off != -1) {
2923                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2924         }
2925         if (rc) {
2926                 fprintf(stderr, "Set ost index %d failed. %s\n",
2927                         i, strerror(errno));
2928                 return rc;
2929         }
2930
2931         /* Create the second, virtual component of extension space */
2932         if (lsa->lsa_extension_comp) {
2933                 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2934                 lsa->lsa_extension_comp = false;
2935                 goto new_comp;
2936         }
2937
2938         return rc;
2939 }
2940
2941 static int build_component(struct llapi_layout **layout,
2942                            struct lfs_setstripe_args *lsa, bool set_extent)
2943 {
2944         int rc;
2945
2946         rc = comp_args_to_layout(layout, lsa, set_extent);
2947         if (rc)
2948                 return rc;
2949
2950         if (lsa->lsa_mirror_count > 0) {
2951                 rc = llapi_layout_mirror_count_set(*layout,
2952                                                    lsa->lsa_mirror_count);
2953                 if (rc)
2954                         return rc;
2955
2956                 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2957                 if (rc)
2958                         return rc;
2959                 lsa->lsa_mirror_count = 0;
2960         }
2961
2962         return rc;
2963 }
2964
2965 static int build_prev_component(struct llapi_layout **layout,
2966                                 struct lfs_setstripe_args *prev,
2967                                 struct lfs_setstripe_args *lsa,
2968                                 bool set_extent)
2969 {
2970         int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2971         int rc;
2972
2973         if (prev->lsa_stripe_size) {
2974                 if (extension) {
2975                         prev->lsa_comp_end = lsa->lsa_comp_end;
2976                         prev->lsa_extension_size = lsa->lsa_extension_size;
2977                         prev->lsa_extension_comp = true;
2978                 }
2979
2980                 rc = build_component(layout, prev, true);
2981                 if (rc)
2982                         return rc;
2983         }
2984
2985         /*
2986          * Copy lsa to previous lsa;
2987          * if this is an extension component, make the previous invalid;
2988          */
2989         if (extension)
2990                 prev->lsa_stripe_size = 0;
2991         else
2992                 *prev = *lsa;
2993
2994         return 0;
2995 }
2996
2997 static int build_layout_from_yaml_node(struct cYAML *node,
2998                                        struct llapi_layout **layout,
2999                                        struct lfs_setstripe_args *lsa,
3000                                        struct lfs_setstripe_args *prevp)
3001 {
3002         struct lfs_setstripe_args prev = { 0 };
3003         __u32 *osts = lsa->lsa_tgts;
3004         char *string;
3005         int rc = 0;
3006
3007         if (!prevp)
3008                 prevp = &prev;
3009
3010         while (node) {
3011                 string = node->cy_string;
3012
3013                 if (node->cy_type == CYAML_TYPE_OBJECT) {
3014                         /* go deep to sub blocks */
3015                         if (string && !strncmp(string, "component", 9) &&
3016                             strncmp(string, "component0", 10) &&
3017                             strncmp(string, "components", 10)) {
3018                                 rc = build_prev_component(layout, prevp, lsa,
3019                                                           true);
3020                                 if (rc)
3021                                         return rc;
3022
3023                                 /* initialize lsa. */
3024                                 setstripe_args_init(lsa);
3025                                 lsa->lsa_first_comp = false;
3026                                 lsa->lsa_tgts = osts;
3027                         }
3028
3029                         rc = build_layout_from_yaml_node(node->cy_child, layout,
3030                                                          lsa, prevp);
3031                         if (rc)
3032                                 return rc;
3033                 } else {
3034                         if (!node->cy_string)
3035                                 return -EINVAL;
3036
3037                         /* skip leading lmm_ if present, to simplify parsing */
3038                         if (strncmp(string, "lmm_", 4) == 0)
3039                                 string += 4;
3040
3041                         if (node->cy_type == CYAML_TYPE_STRING) {
3042                                 if (!strcmp(string, "lcme_extent.e_end")) {
3043                                         if (!strcmp(node->cy_valuestring, "EOF") ||
3044                                             !strcmp(node->cy_valuestring, "eof"))
3045                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3046                                 } else if (!strcmp(string, "pool")) {
3047                                         lsa->lsa_pool_name = node->cy_valuestring;
3048                                 } else if (!strcmp(string, "pattern")) {
3049                                         if (!strcmp(node->cy_valuestring, "mdt"))
3050                                                 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3051                                         if (!strcmp(node->cy_valuestring,
3052                                                     "raid0,overstriped"))
3053                                                 lsa->lsa_pattern =
3054                                                         LLAPI_LAYOUT_OVERSTRIPING;
3055                                 } else if (!strcmp(string, "lcme_flags")) {
3056                                         rc = comp_str2flags(node->cy_valuestring,
3057                                                             &lsa->lsa_comp_flags,
3058                                                             &lsa->lsa_comp_neg_flags);
3059                                         if (rc)
3060                                                 return rc;
3061                                         /*
3062                                          * Only template flags have meaning in
3063                                          * the layout for a new file
3064                                          */
3065                                         lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3066                                 }
3067                         } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3068                                 if (!strcmp(string, "lcm_mirror_count")) {
3069                                         lsa->lsa_mirror_count = node->cy_valueint;
3070                                 } else if (!strcmp(string, "lcme_extent.e_start")) {
3071                                         if (node->cy_valueint == 0)
3072                                                 lsa->lsa_first_comp = true;
3073                                 } else if (!strcmp(string, "lcme_extent.e_end")) {
3074                                         if (node->cy_valueint == -1)
3075                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3076                                         else
3077                                                 lsa->lsa_comp_end = node->cy_valueint;
3078                                 } else if (!strcmp(string, "stripe_count")) {
3079                                         lsa->lsa_stripe_count = node->cy_valueint;
3080                                 } else if (!strcmp(string, "stripe_size")) {
3081                                         lsa->lsa_stripe_size = node->cy_valueint;
3082                                 } else if (!strcmp(string, "extension_size")) {
3083                                         lsa->lsa_extension_size = node->cy_valueint;
3084                                         lsa->lsa_extension_comp = true;
3085                                 } else if (!strcmp(string, "stripe_offset")) {
3086                                         lsa->lsa_stripe_off = node->cy_valueint;
3087                                 } else if (!strcmp(string, "l_ost_idx")) {
3088                                         osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3089                                         lsa->lsa_nr_tgts++;
3090                                 }
3091                         }
3092                 }
3093                 node = node->cy_next;
3094         }
3095
3096         if (prevp == &prev) {
3097                 rc = build_prev_component(layout, prevp, lsa, true);
3098                 if (rc)
3099                         return rc;
3100
3101                 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3102                         rc = build_component(layout, lsa, *layout != NULL);
3103         }
3104
3105         return rc;
3106 }
3107
3108 static int lfs_comp_create_from_yaml(char *template,
3109                                      struct llapi_layout **layout,
3110                                      struct lfs_setstripe_args *lsa,
3111                                      __u32 *osts)
3112 {
3113         struct cYAML *tree = NULL, *err_rc = NULL;
3114         int rc = 0;
3115
3116         tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3117         if (!tree) {
3118                 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3119                         progname, template);
3120                 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3121                                   "can't parse", &err_rc);
3122                 cYAML_print_tree2file(stderr, err_rc);
3123                 cYAML_free_tree(err_rc);