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