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