Whamcloud - gitweb
LU-11188 lfs: add "--perm" option to "lfs find"
[fs/lustre-release.git] / lustre / utils / lfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/lfs.c
32  *
33  * Author: Peter J. Braam <braam@clusterfs.com>
34  * Author: Phil Schwan <phil@clusterfs.com>
35  * Author: Robert Read <rread@clusterfs.com>
36  */
37
38 /* for O_DIRECTORY */
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <inttypes.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <mntent.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
61 #include <fcntl.h>
62 #include <dirent.h>
63 #include <time.h>
64 #include <ctype.h>
65 #include <zlib.h>
66 #include <libgen.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
69
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <lnetconfig/cyaml.h>
79 #include "lstddef.h"
80
81 /* all functions */
82 static int lfs_find(int argc, char **argv);
83 static int lfs_getstripe(int argc, char **argv);
84 static int lfs_getdirstripe(int argc, char **argv);
85 static int lfs_setdirstripe(int argc, char **argv);
86 static int lfs_rmentry(int argc, char **argv);
87 static int lfs_unlink_foreign(int argc, char **argv);
88 static int lfs_osts(int argc, char **argv);
89 static int lfs_mdts(int argc, char **argv);
90 static int lfs_df(int argc, char **argv);
91 static int lfs_getname(int argc, char **argv);
92 static int lfs_check(int argc, char **argv);
93 #ifdef HAVE_SYS_QUOTA_H
94 static int lfs_setquota(int argc, char **argv);
95 static int lfs_quota(int argc, char **argv);
96 static int lfs_project(int argc, char **argv);
97 #endif
98 static int lfs_flushctx(int argc, char **argv);
99 static int lfs_poollist(int argc, char **argv);
100 static int lfs_changelog(int argc, char **argv);
101 static int lfs_changelog_clear(int argc, char **argv);
102 static int lfs_fid2path(int argc, char **argv);
103 static int lfs_path2fid(int argc, char **argv);
104 static int lfs_rmfid(int argc, char **argv);
105 static int lfs_data_version(int argc, char **argv);
106 static int lfs_hsm_state(int argc, char **argv);
107 static int lfs_hsm_set(int argc, char **argv);
108 static int lfs_hsm_clear(int argc, char **argv);
109 static int lfs_hsm_action(int argc, char **argv);
110 static int lfs_hsm_archive(int argc, char **argv);
111 static int lfs_hsm_restore(int argc, char **argv);
112 static int lfs_hsm_release(int argc, char **argv);
113 static int lfs_hsm_remove(int argc, char **argv);
114 static int lfs_hsm_cancel(int argc, char **argv);
115 static int lfs_swap_layouts(int argc, char **argv);
116 static int lfs_mv(int argc, char **argv);
117 static int lfs_ladvise(int argc, char **argv);
118 static int lfs_getsom(int argc, char **argv);
119 static int lfs_heat_get(int argc, char **argv);
120 static int lfs_heat_set(int argc, char **argv);
121 static int lfs_mirror(int argc, char **argv);
122 static int lfs_mirror_list_commands(int argc, char **argv);
123 static int lfs_list_commands(int argc, char **argv);
124 static inline int lfs_mirror_resync(int argc, char **argv);
125 static inline int lfs_mirror_verify(int argc, char **argv);
126 static inline int lfs_mirror_read(int argc, char **argv);
127 static inline int lfs_mirror_write(int argc, char **argv);
128 static inline int lfs_mirror_copy(int argc, char **argv);
129 static int lfs_pcc_attach(int argc, char **argv);
130 static int lfs_pcc_attach_fid(int argc, char **argv);
131 static int lfs_pcc_detach(int argc, char **argv);
132 static int lfs_pcc_detach_fid(int argc, char **argv);
133 static int lfs_pcc_state(int argc, char **argv);
134 static int lfs_pcc(int argc, char **argv);
135 static int lfs_pcc_list_commands(int argc, char **argv);
136 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
137                               __u64 migration_flags,
138                               struct llapi_stripe_param *param,
139                               struct llapi_layout *layout);
140
141 struct pool_to_id_cbdata {
142         const char *pool;
143         __u32 id;
144 };
145 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
146 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
147
148 enum setstripe_origin {
149         SO_SETSTRIPE,
150         SO_MIGRATE,
151         SO_MIGRATE_MDT,
152         SO_MIRROR_CREATE,
153         SO_MIRROR_EXTEND,
154         SO_MIRROR_SPLIT,
155         SO_MIRROR_DELETE,
156 };
157
158 static int lfs_setstripe_internal(int argc, char **argv,
159                                   enum setstripe_origin opc);
160
161 static inline int lfs_setstripe(int argc, char **argv)
162 {
163         return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
164 }
165
166 static inline int lfs_setstripe_migrate(int argc, char **argv)
167 {
168         return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
169 }
170
171 static inline int lfs_mirror_create(int argc, char **argv)
172 {
173         return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
174 }
175
176 static inline int lfs_mirror_extend(int argc, char **argv)
177 {
178         return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
179 }
180
181 static inline int lfs_mirror_split(int argc, char **argv)
182 {
183         return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
184 }
185
186 static inline int lfs_mirror_delete(int argc, char **argv)
187 {
188         return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
189 }
190
191 /* Setstripe and migrate share mostly the same parameters */
192 #define SSM_CMD_COMMON(cmd) \
193         "usage: "cmd" [--component-end|-E <comp_end>]\n"                \
194         "                 [--stripe-count|-c <stripe_count>]\n"         \
195         "                 [--overstripe-count|-C <stripe_count>]\n"     \
196         "                 [--stripe-index|-i <start_ost_idx>]\n"        \
197         "                 [--stripe-size|-S <stripe_size>]\n"           \
198         "                 [--extension-size|--ext-size|-z]\n"           \
199         "                 [--layout|-L <pattern>]\n"                    \
200         "                 [--mirror-count|-N[mirror_count]]\n"          \
201         "                 [--ost|-o <ost_indices>]\n"                   \
202         "                 [--pool|-p <pool_name>]\n"                    \
203         "                 [--yaml|-y <yaml_template_file>]\n"           \
204         "                 [--copy=<lustre_src>]\n"
205
206 #define SSM_HELP_COMMON \
207         "\tstripe_count: Number of OSTs to stripe on (0=fs default, -1 all)\n" \
208         "\t              Using -C instead of -c allows overstriping, which\n"  \
209         "\t              will place more than one stripe per OST if\n"         \
210         "\t              stripe_count is greater than the number of OSTs.\n"   \
211         "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
212         "\tstripe_size:  Number of bytes on each OST (0=fs default)\n"         \
213         "\t              Optional K, M, or G suffix (for KB, MB, GB\n"         \
214         "\t              respectively).  Must be a multiple of 64KiB.\n"       \
215         "\textension_size:\n"                                                  \
216         "\t              Number of bytes the previous component is extended\n" \
217         "\t              each time. Optional K, M, or G suffix (for KB,\n"     \
218         "\t              MB, GB respectively)\n"                               \
219         "\tpool_name:    Name of OST pool to use (default none)\n"             \
220         "\tlayout:       stripe pattern type: raid0, mdt (default raid0)\n"    \
221         "\tost_indices:  List of OST indices, can be repeated multiple times\n"\
222         "\t              Indices be specified in a format of:\n"               \
223         "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"               \
224         "\t              Or:\n"                                                \
225         "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"         \
226         "\t              If --pool is set with --ost then the OSTs\n"          \
227         "\t              must be the members of the pool.\n"                   \
228         "\tcomp_end:     Extent end of component, start after previous end.\n" \
229         "\t              Optional K, M, or G suffix (for KiB, MiB, GiB), or\n" \
230         "\t              -1 or 'eof' for max file size). Must be a multiple\n" \
231         "\t              of stripe_size and a multiple of 64KiB.\n"            \
232         "\tyaml_template_file:\n"                                              \
233         "\t              YAML layout template file, can't be used with -c,\n"  \
234         "\t              -i, -S, -p, -o, or -E arguments.\n"                   \
235         "\tlustre_src:   Lustre file/dir whose layout info is used to set\n"   \
236         "\t              another lustre file or directory, can't used with\n"  \
237         "\t              -c, -i, -S, -p, -o, or -E arguments.\n"
238
239 #define MIRROR_CREATE_HELP                                                     \
240         "\tmirror_count: Number of mirrors to be created with the upcoming\n"  \
241         "\t              setstripe layout options\n"                           \
242         "\t              It defaults to 1 if not specified; if specified,\n"   \
243         "\t              it must follow the option without a space.\n"         \
244         "\t              The option can also be repeated multiple times to\n"  \
245         "\t              separate mirrors that have different layouts.\n"      \
246         "\tSETSTRIPE_OPTIONS: Mirror layout as with 'setstripe'\n"             \
247         "\t              It can be a plain layout or a composite layout.\n"    \
248         "\t              If not specified, the stripe options inherited\n"     \
249         "\t              from the previous component will be used.\n"          \
250         "\tflags:        set flags to the component of the current mirror.\n"  \
251         "\t              Only \"prefer\" flag is supported so far.\n"
252
253 #define MIRROR_EXTEND_HELP                                                     \
254         MIRROR_CREATE_HELP                                                     \
255         "\tvictim_file:  The layout of victim_file will be split and used\n"   \
256         "\t              as a mirror added to the mirrored file.\n"            \
257         "\tno-verify:    This option indicates not to verify the mirror(s)\n"  \
258         "\t              from victim file(s) in case the victim file(s)\n"     \
259         "\t              contains the same data as the original mirrored\n"    \
260         "\t              file.\n"
261
262 #define MIRROR_EXTEND_USAGE                                                    \
263         "                 {--mirror-count|-N[mirror_count]}\n"                 \
264         "                 [SETSTRIPE_OPTIONS|-f|--file <victim_file>]\n"       \
265         "                 [--no-verify]\n"
266
267 #define SETSTRIPE_USAGE                                                 \
268         SSM_CMD_COMMON("setstripe")                                     \
269         MIRROR_EXTEND_USAGE                                             \
270         "                 <directory|filename>\n"                       \
271         SSM_HELP_COMMON                                                 \
272         MIRROR_EXTEND_HELP
273
274 #define MIGRATE_USAGE                                                   \
275         SSM_CMD_COMMON("migrate  ")                                     \
276         "                 [--block|-b] [--non-block|-n]\n"              \
277         "                 [--non-direct|-D] [--verbose|-v]\n"           \
278         "                 <filename>\n"                                 \
279         SSM_HELP_COMMON                                                 \
280         "\n"                                                            \
281         "\tblock:        Block file access during data migration (default)\n" \
282         "\tnon-block:    Abort migrations if concurrent access is detected\n" \
283         "\tnon-direct:   Do not use direct I/O to copy file contents\n" \
284         "\tverbose:      Print each filename as it is migrated\n"       \
285
286 #define SETDIRSTRIPE_USAGE                                              \
287         "               [--mdt-count|-c stripe_count>\n"                \
288         "               [--mdt-hash|-H mdt_hash]\n"                     \
289         "               [--mdt-index|-i mdt_index[,mdt_index,...]\n"    \
290         "               [--default|-D] [--mode|-o mode]\n"              \
291         "               [--max-inherit|-X max_inherit]\n"               \
292         "               [--max-inherit-rr max_inherit_rr] <dir>\n"              \
293         "\tstripe_count: stripe count of the striped directory\n"       \
294         "\tmdt_index: MDT index of first stripe\n"                      \
295         "\tmdt_hash:  hash type of the striped directory. mdt types:\n" \
296         "       crush     CRUSH hash algorithm (default)\n" \
297         "       fnv_1a_64 FNV-1a hash algorithm\n"              \
298         "       all_char  sum of characters % MDT_COUNT (not recommended)\n" \
299         "\tdefault_stripe: set default dirstripe of the directory\n"    \
300         "\tmode: the file access permission of the directory (octal)\n" \
301         "To create dir with a foreign (free format) layout :\n" \
302         "setdirstripe|mkdir --foreign[=<foreign_type>] -x|-xattr <string> " \
303                 "[--mode|-o mode] [--flags <hex>] <dir>\n" \
304         "\tmode: the mode of the directory\n" \
305         "\tforeign_type: none or symlink\n"
306
307 /**
308  * command_t mirror_cmdlist - lfs mirror commands.
309  */
310 command_t mirror_cmdlist[] = {
311         { .pc_name = "create", .pc_func = lfs_mirror_create,
312           .pc_help = "Create a mirrored file.\n"
313                 "usage: lfs mirror create "
314                 "<--mirror-count|-N[mirror_count]> "
315                 "[SETSTRIPE_OPTIONS] ... <filename|directory> ...\n"
316           MIRROR_CREATE_HELP },
317         { .pc_name = "delete", .pc_func = lfs_mirror_delete,
318           .pc_help = "Delete a mirror from a file.\n"
319         "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
320         "\t               --component-id|--comp-id|-I <comp_id> |\n"
321         "\t               -p <pool>} <mirrored_file> ...\n"
322         },
323         { .pc_name = "extend", .pc_func = lfs_mirror_extend,
324           .pc_help = "Extend a mirrored file.\n"
325                 "usage: lfs mirror extend "
326                 "<--mirror-count|-N[mirror_count]> [--no-verify] "
327                 "[SETSTRIPE_OPTIONS|-f <victim_file>] ... <filename> ...\n"
328           MIRROR_EXTEND_HELP },
329         { .pc_name = "split", .pc_func = lfs_mirror_split,
330           .pc_help = "Split a mirrored file.\n"
331         "usage: lfs mirror split <--mirror-id <mirror_id> |\n"
332         "\t             --component-id|-I <comp_id>|-p <pool>> [--destroy|-d]\n"
333         "\t             [-f <new_file>] <mirrored_file> ...\n"
334         "\tmirror_id:   The numerical unique identifier for a mirror. It\n"
335         "\t             can be fetched by lfs getstripe command.\n"
336         "\tcomp_id:     Unique component ID within a mirror.\n"
337         "\tpool:        Components on specified pool.\n"
338         "\tnew_file:    This option indicates the layout of the split\n"
339         "\t             mirror will be stored into. If not specified,\n"
340         "\t             a new file named <mirrored_file>.mirror~<mirror_id>\n"
341         "\t             will be used.\n" },
342         { .pc_name = "read", .pc_func = lfs_mirror_read,
343           .pc_help = "Read the content of a specified mirror of a file.\n"
344                 "usage: lfs mirror read <--mirror-id|-N <mirror_id> "
345                 "[--outfile|-o <output_file>] <mirrored_file>\n" },
346         { .pc_name = "write", .pc_func = lfs_mirror_write,
347           .pc_help = "Write to a specified mirror of a file.\n"
348                 "usage: lfs mirror write <--mirror-id|-N <mirror_id> "
349                 "[--inputfile|-i <input_file>] <mirrored_file>\n" },
350         { .pc_name = "copy", .pc_func = lfs_mirror_copy,
351           .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
352                 "usage: lfs mirror copy <--read-mirror|-i <id0>> "
353                 "<--write-mirror|-o <id1,id2>> <mirrored_file>\n" },
354         { .pc_name = "resync", .pc_func = lfs_mirror_resync,
355           .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
356                 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
357                 "<mirrored file> [<mirrored file2>...]\n"},
358         { .pc_name = "verify", .pc_func = lfs_mirror_verify,
359           .pc_help = "Verify mirrored file(s).\n"
360                 "usage: lfs mirror verify "
361                 "[--only <mirror_id,mirror_id2[,...]>] "
362                 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
363         { .pc_name = "list-commands", .pc_func = lfs_mirror_list_commands,
364           .pc_help = "list commands supported by lfs mirror"},
365         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
366         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
367         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
368         { .pc_help = NULL }
369 };
370
371 /**
372  * command_t pcc_cmdlist - lfs pcc commands.
373  */
374 command_t pcc_cmdlist[] = {
375         { .pc_name = "attach", .pc_func = lfs_pcc_attach,
376           .pc_help = "Attach given files to the Persistent Client Cache.\n"
377                 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
378                 "\t-i: archive id for RW-PCC\n" },
379         { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
380           .pc_help = "Attach given files into PCC by FID(s).\n"
381                 "usage: lfs pcc attach_id <--id|-i NUM> <--mnt|-m mnt> "
382                 "<fid> ...\n"
383                 "\t-i: archive id for RW-PCC\n"
384                 "\t-m: Lustre mount point\n" },
385         { .pc_name = "state", .pc_func = lfs_pcc_state,
386           .pc_help = "Display the PCC state for given files.\n"
387                 "usage: lfs pcc state <file> ...\n" },
388         { .pc_name = "detach", .pc_func = lfs_pcc_detach,
389           .pc_help = "Detach given files from the Persistent Client Cache.\n"
390                 "usage: lfs pcc detach <file> ...\n" },
391         { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
392           .pc_help = "Detach given files from PCC by FID(s).\n"
393                 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
394         { .pc_name = "list-commands", .pc_func = lfs_pcc_list_commands,
395           .pc_help = "list commands supported by lfs pcc"},
396         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
397         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
398         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
399         { .pc_help = NULL }
400 };
401
402 /* all available commands */
403 command_t cmdlist[] = {
404         {"setstripe", lfs_setstripe, 0,
405          "To create a file with specified striping/composite layout, or\n"
406          "create/replace the default layout on an existing directory:\n"
407          SSM_CMD_COMMON("setstripe")
408          "                 [--mode <mode>]\n"
409          "                 <directory|filename>\n"
410          " or\n"
411          "To add component(s) to an existing composite file:\n"
412          SSM_CMD_COMMON("setstripe --component-add")
413          SSM_HELP_COMMON
414          "To totally delete the default striping from an existing directory:\n"
415          "usage: setstripe [--delete|-d] <directory>\n"
416          " or\n"
417          "To create a mirrored file or set s default mirror layout on a directory:\n"
418          "usage: setstripe {--mirror-count|-N}[mirror_count] [SETSTRIPE_OPTIONS] <directory|filename>\n"
419          " or\n"
420          "To delete the last component(s) from an existing composite file\n"
421          "(note that this will also delete any data in those components):\n"
422          "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
423          "                               [--component-flags|-F <comp_flags>]\n"
424          "                               <filename>\n"
425          "\tcomp_id:     Unique component ID to delete\n"
426          "\tcomp_flags:  'init' indicating all instantiated components\n"
427          "\t             '^init' indicating all uninstantiated components\n"
428          "\t-I and -F cannot be specified at the same time\n"
429          " or\n"
430          "To set or clear flags on a specific component\n"
431          "(note that this command can only be applied to mirrored files:\n"
432          "usage: setstripe --comp-set {-I comp_id|--comp-flags=comp_flags}\n"
433          "                            <filename>\n"
434          " or\n"
435          "To create a file with a foreign (free format) layout:\n"
436          "usage: setstripe --foreign[=<foreign_type>]\n"
437          "                 --xattr|-x <layout_string> [--flags <hex>]\n"
438          "                 [--mode <mode>] <filename>\n"},
439         {"getstripe", lfs_getstripe, 0,
440          "To list the layout pattern for a given file or files in a\n"
441          "directory or recursively for all files in a directory tree.\n"
442          "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
443          "                 [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
444          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
445          "                 [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
446          "                 [--layout|-L] [--generation|-g] [--yaml|-y]\n"
447          "                 [--component-id[=comp_id]|-I[comp_id]]\n"
448          "                 [--component-flags[=comp_flags]]\n"
449          "                 [--component-count]\n"
450          "                 [--extension-size|--ext-size|-z]\n"
451          "                 [--component-start[=[+-]comp_start]]\n"
452          "                 [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
453          "                 [[!] --mirror-index=[+-]<index> |\n"
454          "                  [!] --mirror-id=[+-]<id>] [--mirror-count|-N]\n"
455          "                 <directory|filename> ..."},
456         {"setdirstripe", lfs_setdirstripe, 0,
457          "Create striped directory on specified MDT, same as mkdir.\n"
458          "May be restricted to root or group users, depending on settings.\n"
459          "usage: setdirstripe [OPTION] <directory>\n"
460          SETDIRSTRIPE_USAGE},
461         {"getdirstripe", lfs_getdirstripe, 0,
462          "To list the layout pattern info for a given directory\n"
463          "or recursively for all directories in a directory tree.\n"
464          "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
465          "                    [--mdt-hash|-H] [--obd|-O <uuid>]\n"
466          "                    [--recursive|-r] [--yaml|-y]\n"
467          "                    [--verbose|-v] [--default|-D]\n"
468          "                    [--max-inherit|-X]\n"
469          "                    [--max-inherit-rr] <dir> ..."},
470         {"mkdir", lfs_setdirstripe, 0,
471          "Create striped directory on specified MDT, same as setdirstripe.\n"
472          "usage: mkdir [OPTION] <directory>\n"
473          SETDIRSTRIPE_USAGE},
474         {"rm_entry", lfs_rmentry, 0,
475          "To remove the name entry of the remote directory. Note: This\n"
476          "command will only delete the name entry, i.e. the remote directory\n"
477          "will become inaccessable after this command. This can only be done\n"
478          "by the administrator\n"
479          "usage: rm_entry <dir>\n"},
480         {"unlink_foreign", lfs_unlink_foreign, 0,
481          "To remove the foreign file/dir.\n"
482          "Note: This is for files/dirs prevented to be removed using\n"
483          "unlink/rmdir, but works also for regular ones\n"
484          "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
485         {"pool_list", lfs_poollist, 0,
486          "List pools or pool OSTs\n"
487          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
488         {"find", lfs_find, 0,
489          "find files matching given attributes recursively in directory tree.\n"
490          "usage: find <directory|filename> ...\n"
491          "     [[!] --atime|-A [+-]N[smhdwy]] [[!] --ctime|-C [+-]N[smhdwy]]\n"
492          "     [[!] --mtime|-M [+-]N[smhdwy]]\n"
493          "     [[!] --btime|--Btime|-B [+-]N[smhdwy]]\n"
494          "     [[!] --newer[XY] <reference>] [[!] --blocks|-b N]\n"
495          "     [--maxdepth|-D N] [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
496          "     [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
497          "     [[!] --perm [/-]mode] [[!] --pool <pool>] [--print|-P]\n"
498          "     [--print0|-0] [[!] --projid <projid>]\n"
499          "     [[!] --size|-s [+-]N[bkMGTPE]]\n"
500          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
501          "     [[!] --stripe-index|-i <index,...>]\n"
502          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
503          "     [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
504          "     [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
505          "     [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
506          "     [[!] --layout|-L released,raid0,mdt]\n"
507          "     [[!] --foreign[=<foreign_type>]]\n"
508          "     [[!] --component-count [+-]<comp_cnt>]\n"
509          "     [[!] --component-start [+-]N[kMGTPE]]\n"
510          "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
511          "     [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
512          "     [[!] --mirror-count|-N [+-]<n>]\n"
513          "     [[!] --mirror-state <[^]state>]\n"
514          "     [[!] --mdt-count|-T [+-]<stripes>]\n"
515          "     [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
516          "     [[!] --mdt-index|-m <uuid|index,...>]\n"
517          "\t !: used before an option indicates 'NOT' requested attribute\n"
518          "\t -: used before a value indicates less than requested value\n"
519          "\t +: used before a value indicates more than requested value\n"
520          "\t ^: used before a flag indicates to exclude it\n"},
521         {"check", lfs_check, 0,
522          "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
523          "or all the servers (MGTs, MDTs and OSTs).\n"
524          "usage: check <mgts|osts|mdts|all>"},
525         {"osts", lfs_osts, 0, "list OSTs connected to client "
526          "[for specified path only]\n" "usage: osts [path]"},
527         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
528          "[for specified path only]\n" "usage: mdts [path]"},
529         {"df", lfs_df, 0,
530          "report filesystem disk space usage or inodes usage "
531          "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
532          "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
533          "          [--pool|-p <fsname>[.<pool>]] [path]"},
534         {"getname", lfs_getname, 0,
535          "list instances and specified mount points [for specified path only]\n"
536          "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
537 #ifdef HAVE_SYS_QUOTA_H
538         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
539          "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
540          "                -b <block-softlimit> -B <block-hardlimit>\n"
541          "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
542          "       setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
543          "                [--block-softlimit <block-softlimit>]\n"
544          "                [--block-hardlimit <block-hardlimit>]\n"
545          "                [--inode-softlimit <inode-softlimit>]\n"
546          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
547          "       setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
548          "                [--block-grace 'notify'|<block-grace>]\n"
549          "                [--inode-grace 'notify'|<inode-grace>] <filesystem>\n"
550          "       setquota <-U|-G|-P>\n"
551          "                -b <block-softlimit> -B <block-hardlimit>\n"
552          "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
553          "       setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
554          "                [--block-softlimit <block-softlimit>]\n"
555          "                [--block-hardlimit <block-hardlimit>]\n"
556          "                [--inode-softlimit <inode-softlimit>]\n"
557          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
558          "       setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
559          "                <-d|--default>\n"
560          "       -b can be used instead of --block-softlimit/--block-grace\n"
561          "       -B can be used instead of --block-hardlimit\n"
562          "       -i can be used instead of --inode-softlimit/--inode-grace\n"
563          "       -I can be used instead of --inode-hardlimit\n"
564          "       -d can be used instead of --default\n\n"
565          "Note: The total quota space will be split into many qunits and\n"
566          "      balanced over all server targets, the minimal qunit size is\n"
567          "      1M bytes for block space and 1K inodes for inode space.\n\n"
568          "      The maximum quota grace time is 2^48 - 1 seconds.\n\n"
569          "      Quota space rebalancing process will stop when this mininum\n"
570          "      value is reached. As a result, quota exceeded can be returned\n"
571          "      while many targets still have 1MB or 1K inodes of spare\n"
572          "      quota space.\n\n"
573          "      When setting the grace time, 'notify' can be used as grace to\n"
574          "      be notified after the quota is over soft limit but prevents\n"
575          "      the soft limit from becoming the hard limit."},
576         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
577          "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
578                        "<ost_idx>]\n"
579          "             [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
580          "       quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
581         "        quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
582         {"project", lfs_project, 0,
583          "Change or list project attribute for specified file or directory.\n"
584          "usage: project [-d|-r] <file|directory...>\n"
585          "         list project ID and flags on file(s) or directories\n"
586          "       project [-p id] [-s] [-r] <file|directory...>\n"
587          "         set project ID and/or inherit flag for specified file(s) or directories\n"
588          "       project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
589          "         check project ID and flags on file(s) or directories, print outliers\n"
590          "       project -C [-r] [-k] <file|directory...>\n"
591          "         clear the project inherit flag and ID on the file or directory\n"
592         },
593 #endif
594         {"flushctx", lfs_flushctx, 0,
595          "Flush security context for current user.\n"
596          "usage: flushctx [-k] [-r] [mountpoint...]"},
597         {"changelog", lfs_changelog, 0,
598          "Show the metadata changes on an MDT."
599          "\nusage: changelog <mdtname> [startrec [endrec]]"},
600         {"changelog_clear", lfs_changelog_clear, 0,
601          "Indicate that old changelog records up to <endrec> are no longer of "
602          "interest to consumer <id>, allowing the system to free up space.\n"
603          "An <endrec> of 0 means all records.\n"
604          "usage: changelog_clear <mdtname> <id> <endrec>"},
605         {"fid2path", lfs_fid2path, 0,
606          "Resolve the full path(s) for given FID(s). For a specific hardlink "
607          "specify link number <linkno>.\n"
608          "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
609          "<fsname|root> <fid>..."},
610         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
611          "usage: path2fid [--parents] <path> ..."},
612         {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
613          "usage: rmfid <fsname|rootpath> <fid> ..."},
614         {"data_version", lfs_data_version, 0, "Display file data version for "
615          "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
616         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
617          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
618         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
619          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
620          "[--archived] [--lost] [--archive-id NUM] <file> ..."},
621         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
622          "files.\n"
623          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
624          "[--archived] [--lost] <file> ..."},
625         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
626          "given files.\n" "usage: hsm_action <file> ..."},
627         {"hsm_archive", lfs_hsm_archive, 0,
628          "Archive file to external storage.\n"
629          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
630          "<file> ..."},
631         {"hsm_restore", lfs_hsm_restore, 0,
632          "Restore file from external storage.\n"
633          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
634         {"hsm_release", lfs_hsm_release, 0,
635          "Release files from Lustre.\n"
636          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
637         {"hsm_remove", lfs_hsm_remove, 0,
638          "Remove file copy from external storage.\n"
639          "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
640          "[--archive NUM]\n"
641          "                  (FILE [FILE ...] | "
642          "--mntpath MOUNTPATH FID [FID ...])\n"
643          "\n"
644          "Note: To remove an archived copy of a file already deleted from a "
645          "Lustre FS, the\n"
646          "--mntpath option and a list of FIDs must be specified"
647         },
648         {"hsm_cancel", lfs_hsm_cancel, 0,
649          "Cancel requests related to specified files.\n"
650          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
651         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
652          "usage: swap_layouts <path1> <path2>"},
653         {"migrate", lfs_setstripe_migrate, 0,
654          "migrate a directory between MDTs.\n"
655          "usage: migrate [--mdt-count|-c] <stripe_count>\n"
656          "               [--mdt-hash|-H] <hash_type>\n"
657          "               [--mdt-index|-m] <start_mdt_index>\n"
658          "               [--verbose|-v]\n"
659          "               <directory>\n"
660          "\tmdt:        MDTs to stripe over, if only one MDT is specified\n"
661          "                      it's the MDT index of first stripe\n"
662          "\tmdt_count:  number of MDTs to stripe a directory over\n"
663          "\tmdt_hash:   hash type of the striped directory. mdt types:\n"
664          "              all_char  (type 1)sum of characters % MDT_COUNT\n"
665          "              fnv_1a_64 (type 2)FNV-1a hash algorithm (default)\n"
666          "              crush     (type 3)CRUSH hash algorithm\n"
667          "\n"
668          "migrate file objects from one OST "
669          "layout\nto another (may be not safe with concurent writes).\n"
670          "usage: migrate  "
671          "[--stripe-count|-c] <stripe_count>\n"
672          "[--overstripe-count|-C] <stripe_count>\n"
673          "              [--stripe-index|-i] <start_ost_index>\n"
674          "              [--stripe-size|-S] <stripe_size>\n"
675          "              [--pool|-p] <pool_name>\n"
676          "              [--ost|-o] <ost_indices>\n"
677          "              [--block|-b]\n"
678          "              [--non-block|-n]\n"
679          "              [--non-direct|-D]\n"
680          "              <file|directory>\n"
681          "\tstripe_count:     number of OSTs to stripe a file over\n"
682          "\t              Using -C instead of -c allows overstriping, which\n"
683          "\t              will place more than one stripe per OST if\n"
684          "\t              stripe_count is greater than the number of OSTs\n"
685          "\tstripe_ost_index: index of the first OST to stripe a file over\n"
686          "\tstripe_size:      number of bytes to store before moving to the next OST\n"
687          "\tpool_name:        name of the predefined pool of OSTs\n"
688          "\tost_indices:      OSTs to stripe over, in order\n"
689          "\tblock:        Block file access during data migration (default)\n"
690          "\tnon-block:    Abort migrations if concurrent access is detected\n"
691          "\tnon-direct:       do not use direct I/O to copy file contents.\n"},
692         {"mv", lfs_mv, 0,
693          "To move directories between MDTs. This command is deprecated, "
694          "use \"migrate\" instead.\n"
695          "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
696          "[--verbose|-v]\n"},
697         {"ladvise", lfs_ladvise, 0,
698          "Provide servers with advice about access patterns for a file.\n"
699          "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
700          "               [--background|-b] [--unset|-u]\n\n"
701          "               {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
702          "               {[--mode|-m [READ,WRITE]}\n"
703          "               <file> ...\n"},
704         {"mirror", lfs_mirror, mirror_cmdlist,
705          "lfs commands used to manage files with mirrored components:\n"
706          "lfs mirror create - create a mirrored file or directory\n"
707          "lfs mirror extend - add mirror(s) to an existing file\n"
708          "lfs mirror split  - split a mirror from an existing mirrored file\n"
709          "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
710          "lfs mirror read   - read a mirror content of a mirrored file\n"
711          "lfs mirror write  - write to a mirror of a mirrored file\n"
712          "lfs mirror copy   - copy a mirror to other mirror(s) of a file\n"
713          "lfs mirror verify - verify mirrored file(s)\n"},
714         {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
715          "usage: getsom [-s] [-b] [-f] <path>\n"
716          "\t-s: Only show the size value of the SOM data for a given file\n"
717          "\t-b: Only show the blocks value of the SOM data for a given file\n"
718          "\t-f: Only show the flags value of the SOM data for a given file\n"},
719         {"heat_get", lfs_heat_get, 0,
720          "To get heat of files.\n"
721          "usage: heat_get <file> ...\n"},
722         {"heat_set", lfs_heat_set, 0,
723          "To set heat flags of files.\n"
724          "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
725          "\t--clear|-c: Clear file heat for given files\n"
726          "\t--off|-o:   Turn off file heat for given files\n"
727          "\t--on|-O:    Turn on file heat for given files\n"},
728         {"pcc", lfs_pcc, pcc_cmdlist,
729          "lfs commands used to interact with PCC features:\n"
730          "lfs pcc attach - attach given files to Persistent Client Cache\n"
731          "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
732          "lfs pcc state  - display the PCC state for given files\n"
733          "lfs pcc detach - detach given files from Persistent Client Cache\n"
734          "lfs pcc detach_fid - detach given files from PCC by FID(s)\n"},
735         {"help", Parser_help, 0, "help"},
736         {"exit", Parser_quit, 0, "quit"},
737         {"quit", Parser_quit, 0, "quit"},
738         {"--version", Parser_version, 0,
739          "output build version of the utility and exit"},
740         {"--list-commands", lfs_list_commands, 0,
741          "list commands supported by the utility and exit"},
742         { 0, 0, 0, NULL }
743 };
744
745 static int check_hashtype(const char *hashtype)
746 {
747         int type_num = atoi(hashtype);
748         int i;
749
750         /* numeric hash type */
751         if (hashtype && strlen(hashtype) == 1 &&
752             (type_num > 0 && type_num < LMV_HASH_TYPE_MAX))
753                 return type_num;
754         /* string hash type */
755         for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
756                 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
757                         return i;
758
759         return 0;
760 }
761
762 static uint32_t check_foreign_type_name(const char *foreign_type_name)
763 {
764         uint32_t i;
765
766         for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
767                 if (!lu_foreign_types[i].lft_name)
768                         break;
769                 if (strcmp(foreign_type_name,
770                            lu_foreign_types[i].lft_name) == 0)
771                         return lu_foreign_types[i].lft_type;
772         }
773
774         return LU_FOREIGN_TYPE_UNKNOWN;
775 }
776
777 static const char *error_loc = "syserror";
778
779 enum {
780         MIGRATION_NONBLOCK      = 0x0001,
781         MIGRATION_MIRROR        = 0x0002,
782         MIGRATION_NONDIRECT     = 0x0004,
783         MIGRATION_VERBOSE       = 0x0008,
784 };
785
786 static int
787 migrate_open_files(const char *name, __u64 migration_flags,
788                    const struct llapi_stripe_param *param,
789                    struct llapi_layout *layout, int *fd_src, int *fd_tgt)
790 {
791         int                      fd = -1;
792         int                      fdv = -1;
793         int                      rflags;
794         int                      mdt_index;
795         int                      random_value;
796         char                     parent[PATH_MAX];
797         char                     volatile_file[PATH_MAX];
798         char                    *ptr;
799         int                      rc;
800         struct stat              st;
801         struct stat              stv;
802
803         if (!param && !layout) {
804                 error_loc = "layout information";
805                 return -EINVAL;
806         }
807
808         /* search for file directory pathname */
809         if (strlen(name) > sizeof(parent) - 1) {
810                 error_loc = "source file name";
811                 return -ERANGE;
812         }
813
814         strncpy(parent, name, sizeof(parent));
815         ptr = strrchr(parent, '/');
816         if (!ptr) {
817                 if (!getcwd(parent, sizeof(parent))) {
818                         error_loc = "getcwd";
819                         return -errno;
820                 }
821         } else {
822                 if (ptr == parent) /* leading '/' */
823                         ptr = parent + 1;
824                 *ptr = '\0';
825         }
826
827         /* open file, direct io */
828         /* even if the file is only read, WR mode is nedeed to allow
829          * layout swap on fd
830          */
831         rflags = O_RDWR;
832         if (!(migration_flags & MIGRATION_NONDIRECT))
833                 rflags |= O_DIRECT;
834         fd = open(name, rflags);
835         if (fd < 0) {
836                 rc = -errno;
837                 error_loc = "cannot open source file";
838                 return rc;
839         }
840
841         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
842         if (rc < 0) {
843                 error_loc = "cannot get MDT index";
844                 goto out;
845         }
846
847         do {
848                 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
849                 mode_t open_mode = S_IRUSR | S_IWUSR;
850
851                 random_value = random();
852                 rc = snprintf(volatile_file, sizeof(volatile_file),
853                               "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
854                               mdt_index, random_value);
855                 if (rc >= sizeof(volatile_file)) {
856                         rc = -ENAMETOOLONG;
857                         break;
858                 }
859
860                 /* create, open a volatile file, use caching (ie no directio) */
861                 if (layout) {
862                         /* Returns -1 and sets errno on error: */
863                         fdv = llapi_layout_file_open(volatile_file, open_flags,
864                                                      open_mode, layout);
865                         if (fdv < 0)
866                                 fdv = -errno;
867                 } else {
868                         /* Does the right thing on error: */
869                         fdv = llapi_file_open_param(volatile_file, open_flags,
870                                                     open_mode, param);
871                 }
872         } while (fdv < 0 && (rc = fdv) == -EEXIST);
873
874         if (rc < 0) {
875                 error_loc = "cannot create volatile file";
876                 goto out;
877         }
878
879         /*
880          * In case the MDT does not support creation of volatile files
881          * we should try to unlink it.
882          */
883         (void)unlink(volatile_file);
884
885         /*
886          * Not-owner (root?) special case.
887          * Need to set owner/group of volatile file like original.
888          * This will allow to pass related check during layout_swap.
889          */
890         rc = fstat(fd, &st);
891         if (rc != 0) {
892                 rc = -errno;
893                 error_loc = "cannot stat source file";
894                 goto out;
895         }
896
897         rc = fstat(fdv, &stv);
898         if (rc != 0) {
899                 rc = -errno;
900                 error_loc = "cannot stat volatile";
901                 goto out;
902         }
903
904         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
905                 rc = fchown(fdv, st.st_uid, st.st_gid);
906                 if (rc != 0) {
907                         rc = -errno;
908                         error_loc = "cannot change ownwership of volatile";
909                         goto out;
910                 }
911         }
912
913 out:
914         if (rc < 0) {
915                 if (fd > 0)
916                         close(fd);
917                 if (fdv > 0)
918                         close(fdv);
919         } else {
920                 *fd_src = fd;
921                 *fd_tgt = fdv;
922                 error_loc = NULL;
923         }
924         return rc;
925 }
926
927 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
928 {
929         struct llapi_layout *layout;
930         size_t buf_size = 4 * 1024 * 1024;
931         void *buf = NULL;
932         off_t pos = 0;
933         off_t data_end = 0;
934         size_t page_size = sysconf(_SC_PAGESIZE);
935         bool sparse;
936         int rc;
937
938         layout = llapi_layout_get_by_fd(fd_src, 0);
939         if (layout) {
940                 uint64_t stripe_size;
941
942                 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
943                 if (rc == 0)
944                         buf_size = stripe_size;
945
946                 llapi_layout_free(layout);
947         }
948
949         /* Use a page-aligned buffer for direct I/O */
950         rc = posix_memalign(&buf, page_size, buf_size);
951         if (rc != 0)
952                 return -rc;
953
954         sparse = llapi_file_is_sparse(fd_src);
955         if (sparse) {
956                 rc = ftruncate(fd_dst, pos);
957                 if (rc < 0) {
958                         rc = -errno;
959                         return rc;
960                 }
961         }
962
963         while (1) {
964                 off_t data_off;
965                 size_t to_read, to_write;
966                 ssize_t rsize;
967
968                 if (sparse && pos >= data_end) {
969                         size_t data_size;
970
971                         data_off = llapi_data_seek(fd_src, pos, &data_size);
972                         if (data_off < 0) {
973                                 /* Non-fatal, switch to full copy */
974                                 sparse = false;
975                                 continue;
976                         }
977                         /* hole at the end of file, truncate up to it */
978                         if (!data_size) {
979                                 rc = ftruncate(fd_dst, data_off);
980                                 if (rc < 0)
981                                         goto out;
982                         }
983                         pos = data_off & ~(page_size - 1);
984                         data_end = data_off + data_size;
985                         to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
986                         to_read = MIN(to_read, buf_size);
987                 } else {
988                         to_read = buf_size;
989                 }
990
991                 if (check_file) {
992                         rc = check_file(fd_src);
993                         if (rc < 0)
994                                 goto out;
995                 }
996
997                 rsize = pread(fd_src, buf, to_read, pos);
998                 if (rsize < 0) {
999                         rc = -errno;
1000                         goto out;
1001                 }
1002                 /* EOF */
1003                 if (rsize == 0)
1004                         break;
1005
1006                 to_write = rsize;
1007                 while (to_write > 0) {
1008                         ssize_t written;
1009
1010                         written = pwrite(fd_dst, buf, to_write, pos);
1011                         if (written < 0) {
1012                                 rc = -errno;
1013                                 goto out;
1014                         }
1015                         pos += written;
1016                         to_write -= written;
1017                 }
1018                 if (rc || rsize < to_read)
1019                         break;
1020         }
1021
1022         rc = fsync(fd_dst);
1023         if (rc < 0)
1024                 rc = -errno;
1025 out:
1026         /* Try to avoid page cache pollution after migration. */
1027         (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
1028         (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1029
1030         free(buf);
1031         return rc;
1032 }
1033
1034 static int migrate_copy_timestamps(int fd, int fdv)
1035 {
1036         struct stat st;
1037
1038         if (fstat(fd, &st) == 0) {
1039                 struct timeval tv[2] = {
1040                         {.tv_sec = st.st_atime},
1041                         {.tv_sec = st.st_mtime}
1042                 };
1043
1044                 return futimes(fdv, tv);
1045         }
1046
1047         return -errno;
1048 }
1049
1050 static int migrate_block(int fd, int fdv)
1051 {
1052         __u64   dv1;
1053         int     gid;
1054         int     rc;
1055         int     rc2;
1056
1057         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1058         if (rc < 0) {
1059                 error_loc = "cannot get dataversion";
1060                 return rc;
1061         }
1062
1063         do
1064                 gid = random();
1065         while (gid == 0);
1066
1067         /*
1068          * The grouplock blocks all concurrent accesses to the file.
1069          * It has to be taken after llapi_get_data_version as it would
1070          * block it too.
1071          */
1072         rc = llapi_group_lock(fd, gid);
1073         if (rc < 0) {
1074                 error_loc = "cannot get group lock";
1075                 return rc;
1076         }
1077
1078         rc = migrate_copy_data(fd, fdv, NULL);
1079         if (rc < 0) {
1080                 error_loc = "data copy failed";
1081                 goto out_unlock;
1082         }
1083
1084         /* Make sure we keep original atime/mtime values */
1085         rc = migrate_copy_timestamps(fd, fdv);
1086         if (rc < 0) {
1087                 error_loc = "timestamp copy failed";
1088                 goto out_unlock;
1089         }
1090
1091         /*
1092          * swap layouts
1093          * for a migration we need to check data version on file did
1094          * not change.
1095          *
1096          * Pass in gid=0 since we already own grouplock.
1097          */
1098         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1099                                            SWAP_LAYOUTS_CHECK_DV1);
1100         if (rc == -EAGAIN) {
1101                 error_loc = "file changed";
1102                 goto out_unlock;
1103         } else if (rc < 0) {
1104                 error_loc = "cannot swap layout";
1105                 goto out_unlock;
1106         }
1107
1108 out_unlock:
1109         rc2 = llapi_group_unlock(fd, gid);
1110         if (rc2 < 0 && rc == 0) {
1111                 error_loc = "unlock group lock";
1112                 rc = rc2;
1113         }
1114
1115         return rc;
1116 }
1117
1118 /**
1119  * Internal helper for migrate_copy_data(). Check lease and report error if
1120  * need be.
1121  *
1122  * \param[in]  fd           File descriptor on which to check the lease.
1123  *
1124  * \retval 0       Migration can keep on going.
1125  * \retval -errno  Error occurred, abort migration.
1126  */
1127 static int check_lease(int fd)
1128 {
1129         int rc;
1130
1131         rc = llapi_lease_check(fd);
1132         if (rc > 0)
1133                 return 0; /* llapi_check_lease returns > 0 on success. */
1134
1135         return -EBUSY;
1136 }
1137
1138 static int migrate_nonblock(int fd, int fdv)
1139 {
1140         __u64   dv1;
1141         __u64   dv2;
1142         int     rc;
1143
1144         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1145         if (rc < 0) {
1146                 error_loc = "cannot get data version";
1147                 return rc;
1148         }
1149
1150         rc = migrate_copy_data(fd, fdv, check_lease);
1151         if (rc < 0) {
1152                 error_loc = "data copy failed";
1153                 return rc;
1154         }
1155
1156         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1157         if (rc != 0) {
1158                 error_loc = "cannot get data version";
1159                 return rc;
1160         }
1161
1162         if (dv1 != dv2) {
1163                 rc = -EAGAIN;
1164                 error_loc = "source file changed";
1165                 return rc;
1166         }
1167
1168         /* Make sure we keep original atime/mtime values */
1169         rc = migrate_copy_timestamps(fd, fdv);
1170         if (rc < 0) {
1171                 error_loc = "timestamp copy failed";
1172                 return rc;
1173         }
1174
1175         return 0;
1176 }
1177
1178 static
1179 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1180 {
1181         struct pool_to_id_cbdata data = { .pool = pool };
1182         struct llapi_layout *layout = NULL;
1183         int rc;
1184
1185         layout = llapi_layout_get_by_path(fname, 0);
1186         if (!layout) {
1187                 fprintf(stderr,
1188                         "error %s: file '%s' couldn't get layout: rc=%d\n",
1189                         progname, fname, errno);
1190                 rc = -errno;
1191                 goto free_layout;
1192         }
1193         rc = llapi_layout_sanity(layout, fname, false, true);
1194         if (rc < 0) {
1195                 llapi_layout_sanity_perror(errno);
1196                 goto free_layout;
1197         }
1198         rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1199         if (rc < 0)
1200                 goto free_layout;
1201
1202         *comp_id = data.id;
1203         rc = 0;
1204
1205 free_layout:
1206         if (layout)
1207                 llapi_layout_free(layout);
1208         return rc;
1209 }
1210
1211 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1212                              __u32 flags, __u32 neg_flags)
1213 {
1214         __u32 ids[2];
1215         __u32 flags_array[2];
1216         size_t count = 0;
1217         int rc;
1218
1219         if (!comp_id) {
1220                 if (pool == NULL) {
1221                         fprintf(stderr,
1222                                 "error %s: neither component id nor pool is specified\n",
1223                                 progname);
1224                         return -EINVAL;
1225                 }
1226                 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1227                 if (rc)
1228                         return rc;
1229         }
1230
1231         if (flags) {
1232                 ids[count] = comp_id;
1233                 flags_array[count] = flags;
1234                 ++count;
1235         }
1236
1237         if (neg_flags) {
1238                 if (neg_flags & LCME_FL_STALE) {
1239                         fprintf(stderr,
1240                                 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1241                                 progname);
1242                         return -EINVAL;
1243                 }
1244
1245                 ids[count] = comp_id;
1246                 flags_array[count] = neg_flags | LCME_FL_NEG;
1247                 ++count;
1248         }
1249
1250         rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1251         if (rc) {
1252                 if (errno == EUCLEAN) {
1253                         rc = -errno;
1254                         fprintf(stderr,
1255                                 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1256                                 progname, comp_id, fname);
1257                 } else {
1258                         fprintf(stderr,
1259                                 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1260                                 progname, comp_id, fname, flags, neg_flags);
1261                 }
1262         }
1263
1264         return rc;
1265 }
1266
1267 static int lfs_component_del(char *fname, __u32 comp_id,
1268                              __u32 flags, __u32 neg_flags)
1269 {
1270         int     rc = 0;
1271
1272         if (flags && neg_flags)
1273                 return -EINVAL;
1274
1275         if (!flags && neg_flags)
1276                 flags = neg_flags | LCME_FL_NEG;
1277
1278         if ((flags && comp_id) || (!flags && !comp_id))
1279                 return -EINVAL;
1280
1281         if (flags) {
1282                 if (flags & ~LCME_KNOWN_FLAGS) {
1283                         fprintf(stderr,
1284                                 "%s setstripe: unknown flags %#x\n",
1285                                 progname, flags);
1286                         return -EINVAL;
1287                 }
1288         } else if (comp_id > LCME_ID_MAX) {
1289                 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1290                         progname, comp_id);
1291                 return -EINVAL;
1292         }
1293
1294         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1295         if (rc)
1296                 fprintf(stderr,
1297                         "%s setstripe: cannot delete component %#x from '%s': %s\n",
1298                         progname, comp_id, fname, strerror(errno));
1299         return rc;
1300 }
1301
1302 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1303 {
1304         int     rc;
1305
1306         if (!layout)
1307                 return -EINVAL;
1308
1309         rc = llapi_layout_file_comp_add(fname, layout);
1310         if (rc)
1311                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1312                         fname, strerror(errno));
1313         return rc;
1314 }
1315
1316 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1317                                 struct llapi_layout *layout)
1318 {
1319         struct stat     st;
1320         int     fd;
1321
1322         if (!layout)
1323                 return -EINVAL;
1324
1325         fd = lstat(fname, &st);
1326         if (fd == 0 && S_ISDIR(st.st_mode))
1327                 open_flags = O_DIRECTORY | O_RDONLY;
1328
1329         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1330         if (fd < 0)
1331                 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1332                         S_ISDIR(st.st_mode) ?
1333                                 "set default composite layout for" :
1334                                 "create composite file",
1335                         fname, strerror(errno));
1336         return fd;
1337 }
1338
1339 static int lfs_migrate(char *name, __u64 migration_flags,
1340                        struct llapi_stripe_param *param,
1341                        struct llapi_layout *layout)
1342 {
1343         struct llapi_layout *existing;
1344         uint64_t dom_new, dom_cur;
1345         int fd = -1;
1346         int fdv = -1;
1347         int rc;
1348
1349         rc = migrate_open_files(name, migration_flags, param, layout,
1350                                 &fd, &fdv);
1351         if (rc < 0)
1352                 goto out;
1353
1354         rc = llapi_layout_dom_size(layout, &dom_new);
1355         if (rc) {
1356                 error_loc = "cannot get new layout DoM size";
1357                 goto out;
1358         }
1359         /* special case for migration to DOM layout*/
1360         existing = llapi_layout_get_by_fd(fd, 0);
1361         if (!existing) {
1362                 error_loc = "cannot get existing layout";
1363                 goto out;
1364         }
1365
1366         rc = llapi_layout_dom_size(existing, &dom_cur);
1367         if (rc) {
1368                 error_loc = "cannot get current layout DoM size";
1369                 goto out;
1370         }
1371
1372         /*
1373          * if file has DoM layout already then migration is possible to
1374          * the new layout with the same DoM component via swap layout,
1375          * if new layout used bigger DOM size, then mirroring is used
1376          */
1377         if (dom_new > dom_cur) {
1378                 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags, param,
1379                                         layout);
1380                 if (rc)
1381                         error_loc = "cannot migrate to DOM layout";
1382                 goto out_closed;
1383         }
1384
1385         if (!(migration_flags & MIGRATION_NONBLOCK)) {
1386                 /*
1387                  * Blocking mode (forced if servers do not support file lease).
1388                  * It is also the default mode, since we cannot distinguish
1389                  * between a broken lease and a server that does not support
1390                  * atomic swap/close (LU-6785)
1391                  */
1392                 rc = migrate_block(fd, fdv);
1393                 goto out;
1394         }
1395
1396         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1397         if (rc < 0) {
1398                 error_loc = "cannot get lease";
1399                 goto out;
1400         }
1401
1402         rc = migrate_nonblock(fd, fdv);
1403         if (rc < 0) {
1404                 llapi_lease_release(fd);
1405                 goto out;
1406         }
1407
1408         /*
1409          * Atomically put lease, swap layouts and close.
1410          * for a migration we need to check data version on file did
1411          * not change.
1412          */
1413         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1414         if (rc < 0) {
1415                 error_loc = "cannot swap layout";
1416                 goto out;
1417         }
1418
1419 out:
1420         if (fd >= 0)
1421                 close(fd);
1422
1423         if (fdv >= 0)
1424                 close(fdv);
1425 out_closed:
1426         if (rc < 0)
1427                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1428                         progname, name, error_loc, strerror(-rc));
1429         else if (migration_flags & MIGRATION_VERBOSE)
1430                 printf("%s\n", name);
1431
1432         return rc;
1433 }
1434
1435 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1436 {
1437         char *name;
1438         char *dup_string = NULL;
1439         int rc = 0;
1440
1441         *flags = 0;
1442         *neg_flags = 0;
1443
1444         if (!string || !string[0])
1445                 return -EINVAL;
1446
1447         dup_string = strdup(string);
1448         if (!dup_string) {
1449                 llapi_printf(LLAPI_MSG_ERROR,
1450                              "%s: insufficient memory\n",
1451                              progname);
1452                 return -ENOMEM;
1453         }
1454
1455         for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1456                 bool found = false;
1457                 int i;
1458
1459                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1460                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1461                         const char *comp_name = comp_flags_table[i].cfn_name;
1462
1463                         if (strcmp(name, comp_name) == 0) {
1464                                 *flags |= comp_flag;
1465                                 found = true;
1466                         } else if (strncmp(name, "^", 1) == 0 &&
1467                                    strcmp(name + 1, comp_name) == 0) {
1468                                 *neg_flags |= comp_flag;
1469                                 found = true;
1470                         }
1471                 }
1472                 if (!found) {
1473                         llapi_printf(LLAPI_MSG_ERROR,
1474                                      "%s: component flag '%s' not supported\n",
1475                                      progname, name);
1476                         rc = -EINVAL;
1477                         goto out_free;
1478                 }
1479         }
1480
1481         if (!*flags && !*neg_flags)
1482                 rc = -EINVAL;
1483
1484         /* don't allow to set and exclude the same flag */
1485         if (*flags & *neg_flags)
1486                 rc = -EINVAL;
1487
1488 out_free:
1489         free(dup_string);
1490         return rc;
1491 }
1492
1493 static int mdthash_input(char *string, __u32 *inflags,
1494                          __u32 *exflags, __u32 *type)
1495 {
1496         char *name;
1497         struct mhf_list {
1498                 char *name;
1499                 __u32 flag;
1500         } mhflist[] = {
1501                 {"migrating", LMV_HASH_FLAG_MIGRATION},
1502                 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1503                 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1504         };
1505
1506         if (string == NULL)
1507                 return -EINVAL;
1508
1509         *inflags = 0;
1510         *exflags = 0;
1511         *type = 0;
1512         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1513                 bool found = false;
1514                 int i;
1515
1516                 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1517                         if (strcmp(name, mhflist[i].name) == 0 ||
1518                             name[0] == mhflist[i].name[0]) {
1519                                 *inflags |= mhflist[i].flag;
1520                                 found = true;
1521                         } else if (name[0] == '^' &&
1522                                    (strcmp(name + 1, mhflist[i].name) == 0 ||
1523                                     name[1] == mhflist[i].name[0])) {
1524                                 *exflags |= mhflist[i].flag;
1525                                 found = true;
1526                         }
1527                 }
1528                 if (!found) {
1529                         i = check_hashtype(name);
1530                         if (i > 0) {
1531                                 *type |= 1 << i;
1532                                 continue;
1533                         }
1534                         llapi_printf(LLAPI_MSG_ERROR,
1535                                      "%s: invalid mdt_hash value '%s'\n",
1536                                      progname, name);
1537                         return -EINVAL;
1538                 }
1539         }
1540
1541         /* don't allow to include and exclude the same flag */
1542         if (*inflags & *exflags) {
1543                 llapi_printf(LLAPI_MSG_ERROR,
1544                              "%s: include and exclude same flag '%s'\n",
1545                              progname, string);
1546                 return -EINVAL;
1547         }
1548
1549         return 0;
1550 }
1551
1552 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1553 {
1554         if (!string)
1555                 return -EINVAL;
1556
1557         *state = 0;
1558         *neg_state = 0;
1559
1560         if (strncmp(string, "^", 1) == 0) {
1561                 *neg_state = llapi_layout_string_flags(string + 1);
1562                 if (*neg_state != 0)
1563                         return 0;
1564         } else {
1565                 *state = llapi_layout_string_flags(string);
1566                 if (*state != 0)
1567                         return 0;
1568         }
1569
1570         llapi_printf(LLAPI_MSG_ERROR,
1571                      "%s: mirrored file state '%s' not supported\n",
1572                      progname, string);
1573         return -EINVAL;
1574 }
1575
1576 /**
1577  * struct mirror_args - Command-line arguments for mirror(s).
1578  * @m_count:  Number of mirrors to be created with this layout.
1579  * @m_flags:  Mirror level flags, only 'prefer' is supported.
1580  * @m_layout: Mirror layout.
1581  * @m_file:   A victim file. Its layout will be split and used as a mirror.
1582  * @m_next:   Point to the next node of the list.
1583  *
1584  * Command-line arguments for mirror(s) will be parsed and stored in
1585  * a linked list that consists of this structure.
1586  */
1587 struct mirror_args {
1588         __u32                   m_count;
1589         __u32                   m_flags;
1590         struct llapi_layout     *m_layout;
1591         const char              *m_file;
1592         struct mirror_args      *m_next;
1593         bool                    m_inherit;
1594 };
1595
1596 /**
1597  * enum mirror_flags - Flags for extending a mirrored file.
1598  * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1599  *             in case the victim file(s) contains the same data as the
1600  *             original mirrored file.
1601  * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1602  * @MF_COMP_ID: specified component id instead of mirror id
1603  *
1604  * Flags for extending a mirrored file.
1605  */
1606 enum mirror_flags {
1607         MF_NO_VERIFY    = 0x1,
1608         MF_DESTROY      = 0x2,
1609         MF_COMP_ID      = 0x4,
1610         MF_COMP_POOL    = 0x8,
1611 };
1612
1613 /**
1614  * mirror_create_sanity_check() - Check mirror list.
1615  * @list:  A linked list that stores the mirror arguments.
1616  *
1617  * This function does a sanity check on @list for creating
1618  * a mirrored file.
1619  *
1620  * Return: 0 on success or a negative error code on failure.
1621  */
1622 static int mirror_create_sanity_check(const char *fname,
1623                                       struct mirror_args *list,
1624                                       bool check_fname)
1625 {
1626         int rc = 0;
1627         bool has_m_file = false;
1628         bool has_m_layout = false;
1629
1630         if (!list)
1631                 return -EINVAL;
1632
1633         if (fname && check_fname) {
1634                 struct llapi_layout *layout;
1635
1636                 layout = llapi_layout_get_by_path(fname, 0);
1637                 if (!layout) {
1638                         fprintf(stderr,
1639                                 "error: %s: file '%s' couldn't get layout\n",
1640                                 progname, fname);
1641                         return -ENODATA;
1642                 }
1643
1644                 rc = llapi_layout_sanity(layout, fname, false, true);
1645
1646                 llapi_layout_free(layout);
1647
1648                 if (rc) {
1649                         llapi_layout_sanity_perror(rc);
1650                         return rc;
1651                 }
1652         }
1653
1654         while (list) {
1655                 if (list->m_file) {
1656                         has_m_file = true;
1657                         llapi_layout_free(list->m_layout);
1658
1659                         list->m_layout =
1660                                 llapi_layout_get_by_path(list->m_file, 0);
1661                         if (!list->m_layout) {
1662                                 fprintf(stderr,
1663                                         "error: %s: file '%s' has no layout\n",
1664                                         progname, list->m_file);
1665                                 return -ENODATA;
1666                         }
1667                 } else {
1668                         has_m_layout = true;
1669                         if (!list->m_layout) {
1670                                 fprintf(stderr, "error: %s: no mirror layout\n",
1671                                         progname);
1672                                 return -EINVAL;
1673                         }
1674                 }
1675
1676                 rc = llapi_layout_sanity(list->m_layout, fname, false, true);
1677                 if (rc) {
1678                         llapi_layout_sanity_perror(rc);
1679                         return rc;
1680                 }
1681
1682                 list = list->m_next;
1683         }
1684
1685         if (has_m_file && has_m_layout) {
1686                 fprintf(stderr,
1687                         "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1688                         progname);
1689                 return -EINVAL;
1690         }
1691
1692         return 0;
1693 }
1694
1695 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1696 {
1697         __u32 mirror_flags = *(__u32 *)cbdata;
1698         uint32_t flags;
1699         int rc;
1700
1701         rc = llapi_layout_comp_flags_get(layout, &flags);
1702         if (rc < 0)
1703                 return rc;
1704
1705         if (!flags) {
1706                 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1707                 if (rc)
1708                         return rc;
1709         }
1710
1711         return LLAPI_LAYOUT_ITER_CONT;
1712 }
1713
1714 /**
1715  * mirror_create() - Create a mirrored file.
1716  * @fname:        The file to be created.
1717  * @mirror_list:  A linked list that stores the mirror arguments.
1718  *
1719  * This function creates a mirrored file @fname with the mirror(s)
1720  * from @mirror_list.
1721  *
1722  * Return: 0 on success or a negative error code on failure.
1723  */
1724 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1725 {
1726         struct llapi_layout *layout = NULL;
1727         struct mirror_args *cur_mirror = NULL;
1728         uint16_t mirror_count = 0;
1729         int i = 0;
1730         int rc = 0;
1731
1732         rc = mirror_create_sanity_check(fname, mirror_list, false);
1733         if (rc)
1734                 return rc;
1735
1736         cur_mirror = mirror_list;
1737         while (cur_mirror) {
1738                 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1739                                                mirror_set_flags,
1740                                                &cur_mirror->m_flags);
1741                 if (rc) {
1742                         rc = -errno;
1743                         fprintf(stderr, "%s: failed to set mirror flags\n",
1744                                 progname);
1745                         goto error;
1746                 }
1747
1748                 for (i = 0; i < cur_mirror->m_count; i++) {
1749                         rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1750                         if (rc) {
1751                                 rc = -errno;
1752                                 fprintf(stderr,
1753                                         "error: %s: merge layout failed: %s\n",
1754                                         progname, strerror(errno));
1755                                 goto error;
1756                         }
1757                 }
1758                 mirror_count += cur_mirror->m_count;
1759                 cur_mirror = cur_mirror->m_next;
1760         }
1761
1762         if (!layout) {
1763                 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1764                 return -EINVAL;
1765         }
1766
1767         rc = llapi_layout_mirror_count_set(layout, mirror_count);
1768         if (rc) {
1769                 rc = -errno;
1770                 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1771                         progname, strerror(errno));
1772                 goto error;
1773         }
1774
1775         rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1776                                   layout);
1777         if (rc >= 0) {
1778                 close(rc);
1779                 rc = 0;
1780         }
1781
1782 error:
1783         llapi_layout_free(layout);
1784         return rc;
1785 }
1786
1787 /**
1788  * Compare files and check lease on @fd.
1789  *
1790  * \retval bytes number of bytes are the same
1791  */
1792 static ssize_t mirror_file_compare(int fd, int fdv)
1793 {
1794         const size_t buflen = 4 * 1024 * 1024; /* 4M */
1795         void *buf;
1796         ssize_t bytes_done = 0;
1797         ssize_t bytes_read = 0;
1798
1799         buf = malloc(buflen * 2);
1800         if (!buf)
1801                 return -ENOMEM;
1802
1803         while (1) {
1804                 if (!llapi_lease_check(fd)) {
1805                         bytes_done = -EBUSY;
1806                         break;
1807                 }
1808
1809                 bytes_read = read(fd, buf, buflen);
1810                 if (bytes_read <= 0)
1811                         break;
1812
1813                 if (bytes_read != read(fdv, buf + buflen, buflen))
1814                         break;
1815
1816                 /*
1817                  * XXX: should compute the checksum on each buffer and then
1818                  * compare checksum to avoid cache collision
1819                  */
1820                 if (memcmp(buf, buf + buflen, bytes_read))
1821                         break;
1822
1823                 bytes_done += bytes_read;
1824         }
1825
1826         free(buf);
1827
1828         return bytes_done;
1829 }
1830
1831 static int mirror_extend_file(const char *fname, const char *victim_file,
1832                               enum mirror_flags mirror_flags)
1833 {
1834         int fd = -1;
1835         int fdv = -1;
1836         struct stat stbuf;
1837         struct stat stbuf_v;
1838         struct ll_ioc_lease *data = NULL;
1839         int rc;
1840
1841         fd = open(fname, O_RDWR);
1842         if (fd < 0) {
1843                 error_loc = "open source file";
1844                 rc = -errno;
1845                 goto out;
1846         }
1847
1848         fdv = open(victim_file, O_RDWR);
1849         if (fdv < 0) {
1850                 error_loc = "open target file";
1851                 rc = -errno;
1852                 goto out;
1853         }
1854
1855         if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1856                 error_loc = "stat source or target file";
1857                 rc = -errno;
1858                 goto out;
1859         }
1860
1861         if (stbuf.st_dev != stbuf_v.st_dev) {
1862                 error_loc = "stat source and target file";
1863                 rc = -EXDEV;
1864                 goto out;
1865         }
1866
1867         /* mirrors should be of the same size */
1868         if (stbuf.st_size != stbuf_v.st_size) {
1869                 error_loc = "file sizes don't match";
1870                 rc = -EINVAL;
1871                 goto out;
1872         }
1873
1874         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1875         if (rc < 0) {
1876                 error_loc = "cannot get lease";
1877                 goto out;
1878         }
1879
1880         if (!(mirror_flags & MF_NO_VERIFY)) {
1881                 ssize_t ret;
1882                 /* mirrors should have the same contents */
1883                 ret = mirror_file_compare(fd, fdv);
1884                 if (ret != stbuf.st_size) {
1885                         error_loc = "file busy or contents don't match";
1886                         rc = ret < 0 ? ret : -EINVAL;
1887                         goto out;
1888                 }
1889         }
1890
1891         /* Get rid of caching pages from clients */
1892         rc = llapi_file_flush(fd);
1893         if (rc < 0) {
1894                 error_loc = "cannot get data version";
1895                 goto out;
1896         }
1897
1898         rc = llapi_file_flush(fdv);
1899         if (rc < 0) {
1900                 error_loc = "cannot get data version";
1901                 goto out;
1902         }
1903
1904         /* Make sure we keep original atime/mtime values */
1905         rc = migrate_copy_timestamps(fd, fdv);
1906         if (rc < 0) {
1907                 error_loc = "cannot copy timestamp";
1908                 goto out;
1909         }
1910
1911         /* Atomically put lease, merge layouts and close. */
1912         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1913         if (!data) {
1914                 error_loc = "memory allocation";
1915                 goto out;
1916         }
1917         data->lil_mode = LL_LEASE_UNLCK;
1918         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1919         data->lil_count = 1;
1920         data->lil_ids[0] = fdv;
1921         rc = llapi_lease_set(fd, data);
1922         if (rc < 0) {
1923                 error_loc = "cannot merge layout";
1924                 goto out;
1925         } else if (rc == 0) {
1926                 rc = -EBUSY;
1927                 error_loc = "lost lease lock";
1928                 goto out;
1929         }
1930         rc = 0;
1931
1932 out:
1933         if (data)
1934                 free(data);
1935         if (fd >= 0)
1936                 close(fd);
1937         if (fdv >= 0)
1938                 close(fdv);
1939         if (!rc)
1940                 (void) unlink(victim_file);
1941         if (rc < 0)
1942                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1943                         progname, fname, error_loc, strerror(-rc));
1944         return rc;
1945 }
1946
1947 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1948                                 bool inherit, uint32_t flags)
1949 {
1950         struct llapi_layout *f_layout = NULL;
1951         struct ll_ioc_lease *data = NULL;
1952         int fd = -1;
1953         int fdv = -1;
1954         int rc = 0;
1955
1956         if (inherit) {
1957                 f_layout = llapi_layout_get_by_path(name, 0);
1958                 if (!f_layout) {
1959                         fprintf(stderr, "%s: cannot get layout\n", progname);
1960                         goto out;
1961                 }
1962                 rc = llapi_layout_get_last_init_comp(f_layout);
1963                 if (rc) {
1964                         fprintf(stderr, "%s: cannot get the last init comp\n",
1965                                 progname);
1966                         goto out;
1967                 }
1968                 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1969                 if (rc) {
1970                         fprintf(stderr,
1971                                 "%s: cannot inherit from the last init comp\n",
1972                                 progname);
1973                         goto out;
1974                 }
1975         }
1976         llapi_layout_comp_flags_set(m_layout, flags);
1977         rc = migrate_open_files(name, MIGRATION_NONDIRECT, NULL, m_layout, &fd,
1978                                 &fdv);
1979         if (rc < 0)
1980                 goto out;
1981
1982         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1983         if (rc < 0) {
1984                 error_loc = "cannot get lease";
1985                 goto out;
1986         }
1987
1988         rc = migrate_nonblock(fd, fdv);
1989         if (rc < 0) {
1990                 llapi_lease_release(fd);
1991                 goto out;
1992         }
1993
1994         /* Atomically put lease, merge layouts and close. */
1995         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1996         if (!data) {
1997                 error_loc = "memory allocation";
1998                 goto out;
1999         }
2000         data->lil_mode = LL_LEASE_UNLCK;
2001         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2002         data->lil_count = 1;
2003         data->lil_ids[0] = fdv;
2004         rc = llapi_lease_set(fd, data);
2005         if (rc < 0) {
2006                 error_loc = "cannot merge layout";
2007                 goto out;
2008         } else if (rc == 0) {
2009                 rc = -EBUSY;
2010                 error_loc = "lost lease lock";
2011                 goto out;
2012         }
2013         rc = 0;
2014
2015 out:
2016         if (data)
2017                 free(data);
2018         if (fd >= 0)
2019                 close(fd);
2020         if (fdv >= 0)
2021                 close(fdv);
2022         if (rc < 0)
2023                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2024                         progname, name, error_loc, strerror(-rc));
2025         return rc;
2026 }
2027
2028 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2029                          enum mirror_flags mirror_flags)
2030 {
2031         int rc;
2032
2033         rc = mirror_create_sanity_check(fname, mirror_list, true);
2034         if (rc)
2035                 return rc;
2036
2037         while (mirror_list) {
2038                 if (mirror_list->m_file) {
2039                         rc = mirror_extend_file(fname, mirror_list->m_file,
2040                                                 mirror_flags);
2041                 } else {
2042                         __u32 mirror_count = mirror_list->m_count;
2043
2044                         while (mirror_count > 0) {
2045                                 rc = mirror_extend_layout(fname,
2046                                                         mirror_list->m_layout,
2047                                                         mirror_list->m_inherit,
2048                                                         mirror_list->m_flags);
2049                                 if (rc)
2050                                         break;
2051
2052                                 --mirror_count;
2053                         }
2054                 }
2055                 if (rc)
2056                         break;
2057
2058                 mirror_list = mirror_list->m_next;
2059         }
2060
2061         return rc;
2062 }
2063
2064 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2065 {
2066         uint32_t id;
2067         int rc;
2068
2069         rc = llapi_layout_mirror_id_get(layout, &id);
2070         if (rc < 0)
2071                 return rc;
2072
2073         if ((__u16)id == *(__u16 *)cbdata)
2074                 return LLAPI_LAYOUT_ITER_STOP;
2075
2076         return LLAPI_LAYOUT_ITER_CONT;
2077 }
2078
2079 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2080 {
2081         uint32_t id;
2082         int rc;
2083
2084         rc = llapi_layout_comp_id_get(layout, &id);
2085         if (rc < 0)
2086                 return rc;
2087
2088         if (id == *(__u32 *)cbdata)
2089                 return LLAPI_LAYOUT_ITER_STOP;
2090
2091         return LLAPI_LAYOUT_ITER_CONT;
2092 }
2093
2094 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2095 {
2096         char buf[LOV_MAXPOOLNAME + 1];
2097         struct pool_to_id_cbdata *d = (void *)cbdata;
2098         uint32_t id;
2099         int rc;
2100
2101         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2102         if (rc < 0)
2103                 return rc;
2104         if (strcmp(d->pool, buf))
2105                 return LLAPI_LAYOUT_ITER_CONT;
2106
2107         rc = llapi_layout_mirror_id_get(layout, &id);
2108         if (rc < 0)
2109                 return rc;
2110         d->id = id;
2111
2112         return LLAPI_LAYOUT_ITER_STOP;
2113 }
2114
2115 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2116 {
2117         char buf[LOV_MAXPOOLNAME + 1];
2118         struct pool_to_id_cbdata *d = (void *)cbdata;
2119         uint32_t id;
2120         int rc;
2121
2122         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2123         if (rc < 0)
2124                 return rc;
2125         if (strcmp(d->pool, buf))
2126                 return LLAPI_LAYOUT_ITER_CONT;
2127
2128         rc = llapi_layout_comp_id_get(layout, &id);
2129         if (rc < 0)
2130                 return rc;
2131         d->id = id;
2132
2133         return LLAPI_LAYOUT_ITER_STOP;
2134 }
2135
2136 struct collect_ids_data {
2137         __u16   *cid_ids;
2138         int     cid_count;
2139         __u16   cid_exclude;
2140 };
2141
2142 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2143 {
2144         struct collect_ids_data *cid = cbdata;
2145         uint32_t id;
2146         int rc;
2147
2148         rc = llapi_layout_mirror_id_get(layout, &id);
2149         if (rc < 0)
2150                 return rc;
2151
2152         if ((__u16)id != cid->cid_exclude) {
2153                 int i;
2154
2155                 for (i = 0; i < cid->cid_count; i++) {
2156                         /* already collected the mirror id */
2157                         if (id == cid->cid_ids[i])
2158                                 return LLAPI_LAYOUT_ITER_CONT;
2159                 }
2160                 cid->cid_ids[cid->cid_count] = id;
2161                 cid->cid_count++;
2162         }
2163
2164         return LLAPI_LAYOUT_ITER_CONT;
2165 }
2166
2167 /**
2168  * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2169  * @mirror_id: Mirror id to be checked.
2170  * @layout:    Mirror component list.
2171  *
2172  * This function checks if a mirror with specified @mirror_id is the last
2173  * non-stale mirror of a layout @layout.
2174  *
2175  * Return: true or false.
2176  */
2177 static inline
2178 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2179 {
2180         __u16 mirror_ids[128] = { 0 };
2181         struct collect_ids_data cid = { .cid_ids = mirror_ids,
2182                                         .cid_count = 0,
2183                                         .cid_exclude = mirror_id, };
2184         int i;
2185
2186         llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2187
2188         for (i = 0; i < cid.cid_count; i++) {
2189                 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2190                 int comp_size = 0;
2191
2192                 comp_size = llapi_mirror_find_stale(layout, comp_array,
2193                                                     ARRAY_SIZE(comp_array),
2194                                                     &mirror_ids[i], 1);
2195                 if (comp_size == 0)
2196                         return false;
2197         }
2198
2199         return true;
2200 }
2201
2202 static int mirror_split(const char *fname, __u32 id, const char *pool,
2203                         enum mirror_flags mflags, const char *victim_file)
2204 {
2205         struct llapi_layout *layout;
2206         char parent[PATH_MAX];
2207         char victim[PATH_MAX];
2208         int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2209         char *ptr;
2210         struct ll_ioc_lease *data;
2211         uint16_t mirror_count;
2212         __u32 mirror_id;
2213         int mdt_index;
2214         int fd, fdv;
2215         bool purge = true; /* delete mirror by setting fdv=fd */
2216         int rc;
2217
2218         if (victim_file && (strcmp(fname, victim_file) == 0)) {
2219                 fprintf(stderr,
2220                         "error %s: the source file '%s' and -f file are the same\n",
2221                         progname, fname);
2222                 return -EINVAL;
2223         }
2224
2225         /* check fname contains mirror with mirror_id/comp_id */
2226         layout = llapi_layout_get_by_path(fname, 0);
2227         if (!layout) {
2228                 fprintf(stderr,
2229                         "error %s: file '%s' couldn't get layout\n",
2230                         progname, fname);
2231                 return -EINVAL;
2232         }
2233
2234         rc = llapi_layout_sanity(layout, fname, false, true);
2235         if (rc) {
2236                 llapi_layout_sanity_perror(rc);
2237                 goto free_layout;
2238         }
2239
2240         rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2241         if (rc) {
2242                 fprintf(stderr,
2243                         "error %s: file '%s' couldn't get mirror count\n",
2244                         progname, fname);
2245                 goto free_layout;
2246         }
2247         if (mirror_count < 2) {
2248                 fprintf(stderr,
2249                         "error %s: file '%s' has %d component, cannot split\n",
2250                         progname, fname, mirror_count);
2251                 goto free_layout;
2252         }
2253
2254         if (mflags & MF_COMP_POOL) {
2255                 struct pool_to_id_cbdata data = { .pool = pool };
2256
2257                 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2258                                                &data);
2259                 mirror_id = data.id;
2260         } else if (mflags & MF_COMP_ID) {
2261                 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2262                 mirror_id = mirror_id_of(id);
2263         } else {
2264                 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2265                 mirror_id = id;
2266         }
2267         if (rc < 0) {
2268                 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2269                         progname, fname);
2270                 goto free_layout;
2271         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2272                 if (mflags & MF_COMP_POOL) {
2273                         fprintf(stderr,
2274                                 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2275                                 progname, fname, pool);
2276                         goto free_layout;
2277                 } else if (mflags & MF_COMP_ID) {
2278                         fprintf(stderr,
2279                                 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2280                                 progname, fname, id);
2281                         goto free_layout;
2282                 } else {
2283                         fprintf(stderr,
2284                                 "error %s: file '%s' does not contain mirror with id %u\n",
2285                                 progname, fname, id);
2286                         goto free_layout;
2287                 }
2288         }
2289
2290         fd = open(fname, O_RDWR);
2291         if (fd < 0) {
2292                 fprintf(stderr,
2293                         "error %s: open file '%s' failed: %s\n",
2294                         progname, fname, strerror(errno));
2295                 goto free_layout;
2296         }
2297
2298         /* get victim file directory pathname */
2299         if (strlen(fname) > sizeof(parent) - 1) {
2300                 fprintf(stderr, "error %s: file name of '%s' too long\n",
2301                         progname, fname);
2302                 rc = -ERANGE;
2303                 goto close_fd;
2304         }
2305         strncpy(parent, fname, sizeof(parent));
2306         ptr = strrchr(parent, '/');
2307         if (!ptr) {
2308                 if (!getcwd(parent, sizeof(parent))) {
2309                         fprintf(stderr, "error %s: getcwd failed: %s\n",
2310                                 progname, strerror(errno));
2311                         rc = -errno;
2312                         goto close_fd;
2313                 }
2314         } else {
2315                 if (ptr == parent)
2316                         ptr = parent + 1;
2317                 *ptr = '\0';
2318         }
2319
2320         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2321         if (rc < 0) {
2322                 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2323                         progname, fname);
2324                 goto close_fd;
2325         }
2326
2327 again:
2328         if (!victim_file) {
2329                 /* use a temp file to store the splitted layout */
2330                 if (mflags & MF_DESTROY) {
2331                         if (last_non_stale_mirror(mirror_id, layout)) {
2332                                 rc = -EUCLEAN;
2333                                 fprintf(stderr,
2334                                         "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2335                                         progname, fname);
2336                                 goto close_fd;
2337                         }
2338
2339                         if (purge) {
2340                                 /* don't use volatile file for mirror destroy */
2341                                 fdv = fd;
2342                         } else {
2343                                 /**
2344                                  * try the old way to delete mirror using
2345                                  * volatile file.
2346                                  */
2347                                 fdv = llapi_create_volatile_idx(parent,
2348                                                 mdt_index, O_LOV_DELAY_CREATE);
2349                         }
2350                 } else {
2351                         snprintf(victim, sizeof(victim), "%s.mirror~%u",
2352                                  fname, mirror_id);
2353                         fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2354                 }
2355         } else {
2356                 /* user specified victim file */
2357                 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2358         }
2359
2360         if (fdv < 0) {
2361                 fprintf(stderr,
2362                         "error %s: create victim file failed: %s\n",
2363                         progname, strerror(errno));
2364                 goto close_fd;
2365         }
2366
2367         /* get lease lock of fname */
2368         rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2369         if (rc < 0) {
2370                 fprintf(stderr,
2371                         "error %s: cannot get lease of file '%s': %d\n",
2372                         progname, fname, rc);
2373                 goto close_victim;
2374         }
2375
2376         /* Atomatically put lease, split layouts and close. */
2377         data = malloc(offsetof(typeof(*data), lil_ids[2]));
2378         if (!data) {
2379                 rc = -ENOMEM;
2380                 goto close_victim;
2381         }
2382
2383         data->lil_mode = LL_LEASE_UNLCK;
2384         data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2385         data->lil_count = 2;
2386         data->lil_ids[0] = fdv;
2387         data->lil_ids[1] = mirror_id;
2388         rc = llapi_lease_set(fd, data);
2389         if (rc <= 0) {
2390                 if (rc == -EINVAL && purge) {
2391                         /* could be old MDS which prohibit fd==fdv */
2392                         purge = false;
2393                         goto again;
2394
2395                 }
2396                 if (rc == 0) /* lost lease lock */
2397                         rc = -EBUSY;
2398                 fprintf(stderr,
2399                         "error %s: cannot split '%s': %s\n",
2400                         progname, fname, strerror(-rc));
2401         } else {
2402                 rc = 0;
2403         }
2404         free(data);
2405
2406 close_victim:
2407         if (!purge)
2408                 close(fdv);
2409 close_fd:
2410         close(fd);
2411 free_layout:
2412         llapi_layout_free(layout);
2413         return rc;
2414 }
2415
2416 static inline
2417 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2418                            __u16 *mirror_ids, int ids_nr);
2419
2420 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2421                               __u64 migration_flags,
2422                               struct llapi_stripe_param *param,
2423                               struct llapi_layout *layout)
2424 {
2425         struct ll_ioc_lease *data = NULL;
2426         int rc;
2427
2428         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2429         if (rc < 0) {
2430                 error_loc = "cannot get lease";
2431                 goto out_close;
2432         }
2433
2434         /* Atomically put lease, merge layouts, resync and close. */
2435         data = calloc(1, offsetof(typeof(*data), lil_ids[1024]));
2436         if (!data) {
2437                 error_loc = "memory allocation";
2438                 goto out_close;
2439         }
2440         data->lil_mode = LL_LEASE_UNLCK;
2441         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2442         data->lil_count = 1;
2443         data->lil_ids[0] = fdv;
2444         rc = llapi_lease_set(fd, data);
2445         if (rc < 0) {
2446                 error_loc = "cannot merge layout";
2447                 goto out_close;
2448         } else if (rc == 0) {
2449                 rc = -EBUSY;
2450                 error_loc = "lost lease lock";
2451                 goto out_close;
2452         }
2453         close(fd);
2454         close(fdv);
2455
2456         rc = lfs_mirror_resync_file(name, data, NULL, 0);
2457         if (rc) {
2458                 error_loc = "cannot resync file";
2459                 goto out;
2460         }
2461
2462         /* delete first mirror now */
2463         rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2464         if (rc < 0)
2465                 error_loc = "cannot delete old layout";
2466         goto out;
2467
2468 out_close:
2469         close(fd);
2470         close(fdv);
2471 out:
2472         if (rc < 0)
2473                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2474                         progname, name, error_loc, strerror(-rc));
2475         else if (migration_flags & MIGRATION_VERBOSE)
2476                 printf("%s\n", name);
2477         if (data)
2478                 free(data);
2479         return rc;
2480 }
2481
2482 /**
2483  * Parse a string containing an target index list into an array of integers.
2484  *
2485  * The input string contains a comma delimited list of individual
2486  * indices and ranges, for example "1,2-4,7". Add the indices into the
2487  * \a tgts array and remove duplicates.
2488  *
2489  * \param[out] tgts             array to store indices in
2490  * \param[in] size              size of \a tgts array
2491  * \param[in] offset            starting index in \a tgts
2492  * \param[in] arg               string containing OST index list
2493  * \param[in/out] overstriping  index list may contain duplicates
2494  *
2495  * \retval positive    number of indices in \a tgts
2496  * \retval -EINVAL     unable to parse \a arg
2497  */
2498 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2499                          unsigned long long *pattern)
2500 {
2501         int rc;
2502         int nr = offset;
2503         int slots = size - offset;
2504         char *ptr = NULL;
2505         bool overstriped = false;
2506         bool end_of_loop;
2507
2508         if (!arg)
2509                 return -EINVAL;
2510
2511         end_of_loop = false;
2512         while (!end_of_loop) {
2513                 int start_index = 0;
2514                 int end_index = 0;
2515                 int i;
2516                 char *endptr = NULL;
2517
2518                 rc = -EINVAL;
2519
2520                 ptr = strchrnul(arg, ',');
2521
2522                 end_of_loop = *ptr == '\0';
2523                 *ptr = '\0';
2524
2525                 errno = 0;
2526                 start_index = strtol(arg, &endptr, 0);
2527                 if (endptr == arg) /* no data at all */
2528                         break;
2529                 if (errno != 0 || start_index < -1 ||
2530                     (*endptr != '-' && *endptr != '\0'))
2531                         break;
2532
2533                 end_index = start_index;
2534                 if (*endptr == '-') {
2535                         errno = 0;
2536                         end_index = strtol(endptr + 1, &endptr, 0);
2537                         if (errno != 0 || *endptr != '\0' || end_index < -1)
2538                                 break;
2539                         if (end_index < start_index)
2540                                 break;
2541                 }
2542
2543                 for (i = start_index; i <= end_index && slots > 0; i++) {
2544                         int j;
2545
2546                         /* remove duplicate */
2547                         for (j = 0; j < offset; j++) {
2548                                 if (tgts[j] == i && pattern &&
2549                                     *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2550                                         overstriped = true;
2551                                 else if (tgts[j] == i)
2552                                         return -EINVAL;
2553                         }
2554
2555                         j = offset;
2556
2557                         if (j == offset) { /* check complete */
2558                                 tgts[nr++] = i;
2559                                 --slots;
2560                         }
2561                 }
2562
2563                 if (slots == 0 && i < end_index)
2564                         break;
2565
2566                 *ptr = ',';
2567                 arg = ++ptr;
2568                 offset = nr;
2569                 rc = 0;
2570         }
2571         if (!end_of_loop && ptr)
2572                 *ptr = ',';
2573
2574         if (!overstriped && pattern)
2575                 *pattern = LLAPI_LAYOUT_DEFAULT;
2576
2577         return rc < 0 ? rc : nr;
2578 }
2579
2580 struct lfs_setstripe_args {
2581         unsigned long long       lsa_comp_end;
2582         unsigned long long       lsa_stripe_size;
2583         unsigned long long       lsa_extension_size;
2584         long long                lsa_stripe_count;
2585         long long                lsa_stripe_off;
2586         __u32                    lsa_comp_flags;
2587         __u32                    lsa_comp_neg_flags;
2588         unsigned long long       lsa_pattern;
2589         unsigned int             lsa_mirror_count;
2590         int                      lsa_nr_tgts;
2591         bool                     lsa_first_comp;
2592         bool                     lsa_extension_comp;
2593         __u32                   *lsa_tgts;
2594         char                    *lsa_pool_name;
2595 };
2596
2597 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2598 {
2599         unsigned int mirror_count = lsa->lsa_mirror_count;
2600         bool first_comp = lsa->lsa_first_comp;
2601
2602         memset(lsa, 0, sizeof(*lsa));
2603
2604         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2605         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2606         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2607         lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2608         lsa->lsa_pool_name = NULL;
2609
2610         lsa->lsa_mirror_count = mirror_count;
2611         lsa->lsa_first_comp = first_comp;
2612 }
2613
2614 /**
2615  * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2616  * @lsa: Stripe options to be initialized and inherited.
2617  *
2618  * This function initializes stripe options in @lsa and inherit
2619  * stripe_size, stripe_count and OST pool_name options.
2620  *
2621  * Return: void.
2622  */
2623 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2624 {
2625         unsigned long long stripe_size;
2626         long long stripe_count;
2627         char *pool_name = NULL;
2628
2629         stripe_size = lsa->lsa_stripe_size;
2630         stripe_count = lsa->lsa_stripe_count;
2631         pool_name = lsa->lsa_pool_name;
2632
2633         setstripe_args_init(lsa);
2634
2635         lsa->lsa_stripe_size = stripe_size;
2636         lsa->lsa_stripe_count = stripe_count;
2637         lsa->lsa_pool_name = pool_name;
2638 }
2639
2640 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2641 {
2642         return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2643                 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2644                 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2645                 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2646                 lsa->lsa_comp_end != 0);
2647 }
2648
2649 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2650 {
2651         if (lsa->lsa_nr_tgts) {
2652                 if (lsa->lsa_nr_tgts < 0 ||
2653                     lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2654                         fprintf(stderr, "Invalid nr_tgts(%d)\n",
2655                                 lsa->lsa_nr_tgts);
2656                         errno = EINVAL;
2657                         return -1;
2658                 }
2659
2660                 if (lsa->lsa_stripe_count > 0 &&
2661                     lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2662                     lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2663                     lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2664                         fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2665                                 lsa->lsa_stripe_count,
2666                                 lsa->lsa_nr_tgts);
2667                         errno = EINVAL;
2668                         return -1;
2669                 }
2670         }
2671
2672         return 0;
2673
2674 }
2675
2676 /**
2677  * comp_args_to_layout() - Create or extend a composite layout.
2678  * @composite:       Pointer to the composite layout.
2679  * @lsa:             Stripe options for the new component.
2680  *
2681  * This function creates or extends a composite layout by adding a new
2682  * component with stripe options from @lsa.
2683  *
2684  * When modified, adjust llapi_stripe_param_verify() if needed as well.
2685  *
2686  * Return: 0 on success or an error code on failure.
2687  */
2688 static int comp_args_to_layout(struct llapi_layout **composite,
2689                                struct lfs_setstripe_args *lsa,
2690                                bool set_extent)
2691 {
2692         struct llapi_layout *layout = *composite;
2693         uint64_t prev_end = 0;
2694         uint64_t size;
2695         int i = 0, rc;
2696
2697 new_comp:
2698         if (!layout) {
2699                 layout = llapi_layout_alloc();
2700                 if (!layout) {
2701                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2702                                 strerror(errno));
2703                         errno = ENOMEM;
2704                         return -1;
2705                 }
2706                 *composite = layout;
2707                 lsa->lsa_first_comp = true;
2708         } else {
2709                 uint64_t start;
2710
2711                 /*
2712                  * Get current component extent, current component
2713                  * must be the tail component.
2714                  */
2715                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2716                 if (rc) {
2717                         fprintf(stderr, "Get comp extent failed. %s\n",
2718                                 strerror(errno));
2719                         return rc;
2720                 }
2721
2722                 if (lsa->lsa_first_comp) {
2723                         prev_end = 0;
2724                         rc = llapi_layout_add_first_comp(layout);
2725                 } else {
2726                         rc = llapi_layout_comp_add(layout);
2727                 }
2728                 if (rc) {
2729                         fprintf(stderr, "Add component failed. %s\n",
2730                                 strerror(errno));
2731                         return rc;
2732                 }
2733         }
2734
2735         rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2736         if (rc) {
2737                 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2738                         lsa->lsa_comp_flags, strerror(errno));
2739                 return rc;
2740         }
2741
2742         if (set_extent) {
2743                 uint64_t comp_end = lsa->lsa_comp_end;
2744
2745                 /*
2746                  * The extendable component is 0-length, so it can be removed
2747                  * if there is insufficient space to extend it.
2748                  */
2749                 if (lsa->lsa_extension_comp)
2750                         comp_end = prev_end;
2751
2752                 rc = llapi_layout_comp_extent_set(layout, prev_end,
2753                                                   comp_end);
2754                 if (rc) {
2755                         fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2756                                 prev_end, comp_end, strerror(errno));
2757                         return rc;
2758                 }
2759         }
2760         /* reset lsa_first_comp */
2761         lsa->lsa_first_comp = false;
2762
2763         /* Data-on-MDT component setting */
2764         if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2765                 /*
2766                  * In case of Data-on-MDT patterns the only extra option
2767                  * applicable is stripe size option.
2768                  */
2769                 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2770                         fprintf(stderr,
2771                                 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2772                                 lsa->lsa_stripe_count);
2773                         errno = EINVAL;
2774                         return -1;
2775                 }
2776                 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2777                         fprintf(stderr,
2778                                 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2779                                 lsa->lsa_stripe_size);
2780                         errno = EINVAL;
2781                         return -1;
2782                 }
2783                 if (lsa->lsa_nr_tgts != 0) {
2784                         fprintf(stderr,
2785                                 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2786                                 lsa->lsa_nr_tgts);
2787                         errno = EINVAL;
2788                         return -1;
2789                 }
2790                 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2791                         fprintf(stderr,
2792                                 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2793                                 lsa->lsa_stripe_off);
2794                         errno = EINVAL;
2795                         return -1;
2796                 }
2797                 if (lsa->lsa_pool_name != 0) {
2798                         fprintf(stderr,
2799                                 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2800                                 lsa->lsa_pool_name);
2801                         errno = EINVAL;
2802                         return -1;
2803                 }
2804
2805                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2806                 if (rc) {
2807                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2808                                 lsa->lsa_pattern,
2809                                 strerror(errno));
2810                         return rc;
2811                 }
2812                 /* Data-on-MDT component has always single stripe up to end */
2813                 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2814         } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2815                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2816                 if (rc) {
2817                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2818                                 lsa->lsa_pattern,
2819                                 strerror(errno));
2820                         return rc;
2821                 }
2822         }
2823
2824         size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2825                 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2826
2827         if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2828                 rc = llapi_layout_extension_size_set(layout, size);
2829         else
2830                 rc = llapi_layout_stripe_size_set(layout, size);
2831
2832         if (rc) {
2833                 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2834                         size, strerror(errno));
2835                 return rc;
2836         }
2837
2838         rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2839         if (rc) {
2840                 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2841                         lsa->lsa_stripe_count, strerror(errno));
2842                 return rc;
2843         }
2844
2845         if (lsa->lsa_pool_name) {
2846                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2847                 if (rc) {
2848                         fprintf(stderr, "Set pool name: %s failed. %s\n",
2849                                 lsa->lsa_pool_name, strerror(errno));
2850                         return rc;
2851                 }
2852         } else {
2853                 rc = llapi_layout_pool_name_set(layout, "");
2854                 if (rc) {
2855                         fprintf(stderr, "Clear pool name failed: %s\n",
2856                                 strerror(errno));
2857                         return rc;
2858                 }
2859         }
2860
2861         rc = lsa_args_stripe_count_check(lsa);
2862         if (rc)
2863                 return rc;
2864
2865         if (lsa->lsa_nr_tgts > 0) {
2866                 bool found = false;
2867
2868                 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2869                         rc = llapi_layout_ost_index_set(layout, i,
2870                                                         lsa->lsa_tgts[i]);
2871                         if (rc)
2872                                 break;
2873
2874                         /* Make sure stripe offset is in OST list. */
2875                         if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2876                                 found = true;
2877                 }
2878                 if (!found) {
2879                         fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2880                                 lsa->lsa_stripe_off);
2881                         errno = EINVAL;
2882                         return -1;
2883                 }
2884         } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2885                    lsa->lsa_stripe_off != -1) {
2886                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2887         }
2888         if (rc) {
2889                 fprintf(stderr, "Set ost index %d failed. %s\n",
2890                         i, strerror(errno));
2891                 return rc;
2892         }
2893
2894         /* Create the second, virtual component of extension space */
2895         if (lsa->lsa_extension_comp) {
2896                 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
2897                 lsa->lsa_extension_comp = false;
2898                 goto new_comp;
2899         }
2900
2901         return rc;
2902 }
2903
2904 static int build_component(struct llapi_layout **layout,
2905                            struct lfs_setstripe_args *lsa, bool set_extent)
2906 {
2907         int rc;
2908
2909         rc = comp_args_to_layout(layout, lsa, set_extent);
2910         if (rc)
2911                 return rc;
2912
2913         if (lsa->lsa_mirror_count > 0) {
2914                 rc = llapi_layout_mirror_count_set(*layout,
2915                                                    lsa->lsa_mirror_count);
2916                 if (rc)
2917                         return rc;
2918
2919                 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2920                 if (rc)
2921                         return rc;
2922                 lsa->lsa_mirror_count = 0;
2923         }
2924
2925         return rc;
2926 }
2927
2928 static int build_prev_component(struct llapi_layout **layout,
2929                                 struct lfs_setstripe_args *prev,
2930                                 struct lfs_setstripe_args *lsa,
2931                                 bool set_extent)
2932 {
2933         int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
2934         int rc;
2935
2936         if (prev->lsa_stripe_size) {
2937                 if (extension) {
2938                         prev->lsa_comp_end = lsa->lsa_comp_end;
2939                         prev->lsa_extension_size = lsa->lsa_extension_size;
2940                         prev->lsa_extension_comp = true;
2941                 }
2942
2943                 rc = build_component(layout, prev, true);
2944                 if (rc)
2945                         return rc;
2946         }
2947
2948         /*
2949          * Copy lsa to previous lsa;
2950          * if this is an extension component, make the previous invalid;
2951          */
2952         if (extension)
2953                 prev->lsa_stripe_size = 0;
2954         else
2955                 *prev = *lsa;
2956
2957         return 0;
2958 }
2959
2960 static int build_layout_from_yaml_node(struct cYAML *node,
2961                                        struct llapi_layout **layout,
2962                                        struct lfs_setstripe_args *lsa,
2963                                        struct lfs_setstripe_args *prevp)
2964 {
2965         struct lfs_setstripe_args prev = { 0 };
2966         __u32 *osts = lsa->lsa_tgts;
2967         char *string;
2968         int rc = 0;
2969
2970         if (!prevp)
2971                 prevp = &prev;
2972
2973         while (node) {
2974                 string = node->cy_string;
2975
2976                 if (node->cy_type == CYAML_TYPE_OBJECT) {
2977                         /* go deep to sub blocks */
2978                         if (string && !strncmp(string, "component", 9) &&
2979                             strncmp(string, "component0", 10) &&
2980                             strncmp(string, "components", 10)) {
2981                                 rc = build_prev_component(layout, prevp, lsa,
2982                                                           true);
2983                                 if (rc)
2984                                         return rc;
2985
2986                                 /* initialize lsa. */
2987                                 setstripe_args_init(lsa);
2988                                 lsa->lsa_first_comp = false;
2989                                 lsa->lsa_tgts = osts;
2990                         }
2991
2992                         rc = build_layout_from_yaml_node(node->cy_child, layout,
2993                                                          lsa, prevp);
2994                         if (rc)
2995                                 return rc;
2996                 } else {
2997                         if (!node->cy_string)
2998                                 return -EINVAL;
2999
3000                         /* skip leading lmm_ if present, to simplify parsing */
3001                         if (strncmp(string, "lmm_", 4) == 0)
3002                                 string += 4;
3003
3004                         if (node->cy_type == CYAML_TYPE_STRING) {
3005                                 if (!strcmp(string, "lcme_extent.e_end")) {
3006                                         if (!strcmp(node->cy_valuestring, "EOF") ||
3007                                             !strcmp(node->cy_valuestring, "eof"))
3008                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3009                                 } else if (!strcmp(string, "pool")) {
3010                                         lsa->lsa_pool_name = node->cy_valuestring;
3011                                 } else if (!strcmp(string, "pattern")) {
3012                                         if (!strcmp(node->cy_valuestring, "mdt"))
3013                                                 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3014                                         if (!strcmp(node->cy_valuestring,
3015                                                     "raid0,overstriped"))
3016                                                 lsa->lsa_pattern =
3017                                                         LLAPI_LAYOUT_OVERSTRIPING;
3018                                 } else if (!strcmp(string, "lcme_flags")) {
3019                                         rc = comp_str2flags(node->cy_valuestring,
3020                                                             &lsa->lsa_comp_flags,
3021                                                             &lsa->lsa_comp_neg_flags);
3022                                         if (rc)
3023                                                 return rc;
3024                                         /*
3025                                          * Only template flags have meaning in
3026                                          * the layout for a new file
3027                                          */
3028                                         lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3029                                 }
3030                         } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3031                                 if (!strcmp(string, "lcm_mirror_count")) {
3032                                         lsa->lsa_mirror_count = node->cy_valueint;
3033                                 } else if (!strcmp(string, "lcme_extent.e_start")) {
3034                                         if (node->cy_valueint == 0)
3035                                                 lsa->lsa_first_comp = true;
3036                                 } else if (!strcmp(string, "lcme_extent.e_end")) {
3037                                         if (node->cy_valueint == -1)
3038                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3039                                         else
3040                                                 lsa->lsa_comp_end = node->cy_valueint;
3041                                 } else if (!strcmp(string, "stripe_count")) {
3042                                         lsa->lsa_stripe_count = node->cy_valueint;
3043                                 } else if (!strcmp(string, "stripe_size")) {
3044                                         lsa->lsa_stripe_size = node->cy_valueint;
3045                                 } else if (!strcmp(string, "extension_size")) {
3046                                         lsa->lsa_extension_size = node->cy_valueint;
3047                                         lsa->lsa_extension_comp = true;
3048                                 } else if (!strcmp(string, "stripe_offset")) {
3049                                         lsa->lsa_stripe_off = node->cy_valueint;
3050                                 } else if (!strcmp(string, "l_ost_idx")) {
3051                                         osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3052                                         lsa->lsa_nr_tgts++;
3053                                 }
3054                         }
3055                 }
3056                 node = node->cy_next;
3057         }
3058
3059         if (prevp == &prev) {
3060                 rc = build_prev_component(layout, prevp, lsa, true);
3061                 if (rc)
3062                         return rc;
3063
3064                 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3065                         rc = build_component(layout, lsa, *layout != NULL);
3066         }
3067
3068         return rc;
3069 }
3070
3071 static int lfs_comp_create_from_yaml(char *template,
3072                                      struct llapi_layout **layout,
3073                                      struct lfs_setstripe_args *lsa,
3074                                      __u32 *osts)
3075 {
3076         struct cYAML *tree = NULL, *err_rc = NULL;
3077         int rc = 0;
3078
3079         tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3080         if (!tree) {
3081                 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3082                         progname, template);
3083                 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3084                                   "can't parse", &err_rc);
3085                 cYAML_print_tree2file(stderr, err_rc);
3086                 cYAML_free_tree(err_rc);
3087                 rc = -EINVAL;
3088                 goto err;
3089         }
3090
3091         /* initialize lsa for plain file */
3092         setstripe_args_init(lsa);
3093         lsa->lsa_tgts = osts;
3094
3095         rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3096         if (rc) {
3097                 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3098                         progname, template);
3099                 goto err;
3100         }
3101         /* clean clean lsa */
3102         setstripe_args_init(lsa);
3103
3104 err:
3105         if (tree)
3106                 cYAML_free_tree(tree);
3107         return rc;
3108 }
3109
3110 /**
3111  * Get the extension size from the next (SEL) component and extend the
3112  * current component on it. The start of the next component is to be
3113  * adjusted as well.
3114  *
3115  * \param[in] layout    the current layout
3116  * \param[in] start     the start of the current component
3117  * \param[in,out] end   the end of the current component
3118  * \param[in] offset    the offset to adjust the end position to instead of
3119  *                      extension size
3120  *
3121  * \retval 0            - extended successfully
3122  * \retval < 0          - error
3123  */
3124 static int layout_extend_comp(struct llapi_layout *layout,
3125                               uint64_t start, uint64_t *end,
3126                               uint64_t offset)
3127 {
3128         uint64_t size, next_start, next_end;
3129         int rc;
3130
3131         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3132         if (rc < 0) {
3133                 fprintf(stderr,
3134                         "%s setstripe: cannot move component cursor: %s\n",
3135                         progname, strerror(errno));
3136                 return rc;
3137         }
3138
3139         /*
3140          * Even if the @size will not be used below, this will fail if
3141          * this is not a SEL component - a good confirmation we are
3142          * working on right components.
3143          */
3144         rc = llapi_layout_extension_size_get(layout, &size);
3145         if (rc < 0) {
3146                 fprintf(stderr,
3147                         "%s setstripe: cannot get component ext size: %s\n",
3148                         progname, strerror(errno));
3149                 return rc;
3150         }
3151
3152         rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3153         if (rc) {
3154                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3155                         progname, strerror(errno));
3156                 return rc;
3157         }
3158
3159         next_start += offset ?: size;
3160         rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3161         if (rc) {
3162                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3163                         progname, strerror(errno));
3164                 return rc;
3165         }
3166
3167         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3168         if (rc < 0) {
3169                 fprintf(stderr,
3170                         "%s setstripe: cannot move component cursor: %s\n",
3171                         progname, strerror(errno));
3172                 return rc;
3173         }
3174
3175         *end += offset ?: size;
3176         rc = llapi_layout_comp_extent_set(layout, start, *end);
3177         if (rc) {
3178                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3179                         progname, strerror(errno));
3180                 return rc;
3181         }
3182
3183         return 0;
3184 }
3185
3186 /**
3187  * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3188  * end of the last component in the existing file, and adjust the
3189  * first extent start of the components to be added accordingly.
3190  *
3191  * In the create mode, we need to check if the first component is an extendable
3192  * SEL component and extend its length to the extension size (first component
3193  * of the PFL file is initialised at the create time, cannot be 0-lenght.
3194  */
3195 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3196                                       bool comp_add)
3197 {
3198         struct llapi_layout *head;
3199         uint64_t start = 0, prev_end = 0;
3200         uint64_t end;
3201         int rc, ret = 0;
3202
3203         if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3204                 return 0;
3205
3206         errno = 0;
3207         while (comp_add) {
3208                 head = llapi_layout_get_by_path(fname, 0);
3209                 if (!head) {
3210                         fprintf(stderr,
3211                                 "%s setstripe: cannot read layout from '%s': %s\n",
3212                                 progname, fname, strerror(errno));
3213                         return -EINVAL;
3214                 } else if (errno == ENODATA) {
3215                         /*
3216                          * file without LOVEA, this component-add will be turned
3217                          * into a component-create.
3218                          */
3219                         llapi_layout_free(head);
3220                         ret = -ENODATA;
3221
3222                         /*
3223                          * the new layout will be added to an empty one, it
3224                          * still needs to be adjusted below
3225                          */
3226                         comp_add = 0;
3227                         break;
3228                 } else if (!llapi_layout_is_composite(head)) {
3229                         fprintf(stderr,
3230                                 "%s setstripe: '%s' not a composite file\n",
3231                                 progname, fname);
3232                         llapi_layout_free(head);
3233                         return -EINVAL;
3234                 }
3235
3236                 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3237                 if (rc) {
3238                         fprintf(stderr,
3239                                 "%s setstripe: cannot get prev extent: %s\n",
3240                                 progname, strerror(errno));
3241                         llapi_layout_free(head);
3242                         return rc;
3243                 }
3244
3245                 llapi_layout_free(head);
3246                 break;
3247         }
3248
3249         /* Make sure we use the first component of the layout to be added. */
3250         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3251         if (rc < 0) {
3252                 fprintf(stderr,
3253                         "%s setstripe: cannot move component cursor: %s\n",
3254                         progname, strerror(errno));
3255                 return rc;
3256         }
3257
3258         rc = llapi_layout_comp_extent_get(layout, &start, &end);
3259         if (rc) {
3260                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3261                         progname, strerror(errno));
3262                 return rc;
3263         }
3264
3265         if (start == 0 && end == 0) {
3266                 rc = layout_extend_comp(layout, start, &end,
3267                                         comp_add ? prev_end : 0);
3268                 if (rc)
3269                         return rc;
3270         }
3271
3272         if (start > prev_end || end < prev_end) {
3273                 fprintf(stderr,
3274                         "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3275                         progname, start, end, prev_end);
3276                 return -EINVAL;
3277         }
3278
3279         rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3280         if (rc) {
3281                 fprintf(stderr,
3282                         "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3283                         progname, prev_end, end, strerror(errno));
3284                 return rc;
3285         }
3286
3287         return ret;
3288 }
3289
3290 static int mirror_adjust_first_extents(struct mirror_args *list)
3291 {
3292         int rc = 0;
3293
3294         if (!list)
3295                 return 0;
3296
3297         while (list) {
3298                 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3299                 if (rc)
3300                         break;
3301                 list = list->m_next;
3302         }
3303
3304         return rc;
3305 }
3306
3307 static inline bool arg_is_eof(char *arg)
3308 {
3309         return !strncmp(arg, "-1", strlen("-1")) ||
3310                !strncmp(arg, "EOF", strlen("EOF")) ||
3311                !strncmp(arg, "eof", strlen("eof"));
3312 }
3313
3314 /**
3315  * lfs_mirror_alloc() - Allocate a mirror argument structure.
3316  *
3317  * Return: Valid mirror_args pointer on success and
3318  *         NULL if memory allocation fails.
3319  */
3320 static struct mirror_args *lfs_mirror_alloc(void)
3321 {
3322         struct mirror_args *mirror = NULL;
3323
3324         while (1) {
3325                 mirror = calloc(1, sizeof(*mirror));
3326                 if (mirror) {
3327                         mirror->m_inherit = false;
3328                         break;
3329                 }
3330
3331                 sleep(1);
3332         }
3333
3334         return mirror;
3335 }
3336
3337 /**
3338  * lfs_mirror_free() - Free memory allocated for a mirror argument
3339  *                     structure.
3340  * @mirror: Previously allocated mirror argument structure by
3341  *          lfs_mirror_alloc().
3342  *
3343  * Free memory allocated for @mirror.
3344  *
3345  * Return: void.
3346  */
3347 static void lfs_mirror_free(struct mirror_args *mirror)
3348 {
3349         if (mirror->m_layout)
3350                 llapi_layout_free(mirror->m_layout);
3351         free(mirror);
3352 }
3353
3354 /**
3355  * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3356  * @mirror_list: Previously allocated mirror list.
3357  *
3358  * Free memory allocated for @mirror_list.
3359  *
3360  * Return: void.
3361  */
3362 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3363 {
3364         struct mirror_args *next_mirror = NULL;
3365
3366         while (mirror_list) {
3367                 next_mirror = mirror_list->m_next;
3368                 lfs_mirror_free(mirror_list);
3369                 mirror_list = next_mirror;
3370         }
3371 }
3372
3373 enum {
3374         LFS_POOL_OPT = 3,
3375         LFS_COMP_COUNT_OPT,
3376         LFS_COMP_START_OPT,
3377         LFS_COMP_FLAGS_OPT,
3378         LFS_COMP_DEL_OPT,
3379         LFS_COMP_SET_OPT,
3380         LFS_COMP_ADD_OPT,
3381         LFS_COMP_NO_VERIFY_OPT,
3382         LFS_PROJID_OPT,
3383         LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3384         LFS_MIRROR_ID_OPT,
3385         LFS_MIRROR_STATE_OPT,
3386         LFS_LAYOUT_COPY,
3387         LFS_MIRROR_INDEX_OPT,
3388         LFS_LAYOUT_FOREIGN_OPT,
3389         LFS_MODE_OPT,
3390         LFS_NEWERXY_OPT,
3391         LFS_INHERIT_RR_OPT,
3392         LFS_FIND_PERM,
3393 };
3394
3395 /* functions */
3396 static int lfs_setstripe_internal(int argc, char **argv,
3397                                   enum setstripe_origin opc)
3398 {
3399         struct lfs_setstripe_args        lsa = { 0 };
3400         struct llapi_stripe_param       *param = NULL;
3401         struct find_param                migrate_mdt_param = {
3402                 .fp_max_depth = -1,
3403                 .fp_mdt_index = -1,
3404         };
3405         char                            *fname;
3406         int                              result = 0;
3407         int                              result2 = 0;
3408         char                            *end;
3409         int                              c;
3410         int                              delete = 0;
3411         unsigned long long               size_units = 1;
3412         bool                             migrate_mode = false;
3413         bool                             migrate_mdt_mode = false;
3414         bool                             setstripe_mode = false;
3415         bool                             migration_block = false;
3416         __u64                            migration_flags = 0;
3417         __u32                            tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3418         int                              comp_del = 0, comp_set = 0;
3419         int                              comp_add = 0;
3420         __u32                            comp_id = 0;
3421         struct llapi_layout             *layout = NULL;
3422         struct llapi_layout             **lpp = &layout;
3423         bool                             mirror_mode = false;
3424         bool                             has_m_file = false;
3425         __u32                            mirror_count = 0;
3426         enum mirror_flags                mirror_flags = 0;
3427         struct mirror_args              *mirror_list = NULL;
3428         struct mirror_args              *new_mirror = NULL;
3429         struct mirror_args              *last_mirror = NULL;
3430         __u16                            mirror_id = 0;
3431         char                             cmd[PATH_MAX];
3432         bool from_yaml = false;
3433         bool from_copy = false;
3434         char *template = NULL;
3435         bool foreign_mode = false;
3436         char *xattr = NULL;
3437         uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3438         char *mode_opt = NULL;
3439         mode_t previous_umask = 0;
3440         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3441
3442         struct option long_opts[] = {
3443 /* find { .val = '0',   .name = "null",         .has_arg = no_argument }, */
3444 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
3445         /* --block is only valid in migrate mode */
3446         { .val = 'b',   .name = "block",        .has_arg = no_argument },
3447 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
3448         { .val = LFS_COMP_ADD_OPT,
3449                         .name = "comp-add",     .has_arg = no_argument },
3450         { .val = LFS_COMP_ADD_OPT,
3451                         .name = "component-add", .has_arg = no_argument },
3452         { .val = LFS_COMP_DEL_OPT,
3453                         .name = "comp-del",     .has_arg = no_argument },
3454         { .val = LFS_COMP_DEL_OPT,
3455                         .name = "component-del", .has_arg = no_argument },
3456         { .val = LFS_COMP_FLAGS_OPT,
3457                         .name = "comp-flags",   .has_arg = required_argument },
3458         { .val = LFS_COMP_FLAGS_OPT,
3459                         .name = "component-flags",
3460                                                 .has_arg = required_argument },
3461         { .val = LFS_COMP_SET_OPT,
3462                         .name = "comp-set",     .has_arg = no_argument },
3463         { .val = LFS_COMP_SET_OPT,
3464                         .name = "component-set",
3465                                                 .has_arg = no_argument},
3466         { .val = LFS_COMP_NO_VERIFY_OPT,
3467                         .name = "no-verify",    .has_arg = no_argument},
3468         { .val = LFS_LAYOUT_FLAGS_OPT,
3469                         .name = "flags",        .has_arg = required_argument},
3470         { .val = LFS_LAYOUT_FOREIGN_OPT,
3471                         .name = "foreign",      .has_arg = optional_argument},
3472         { .val = LFS_MIRROR_ID_OPT,
3473                         .name = "mirror-id",    .has_arg = required_argument},
3474         { .val = LFS_MODE_OPT,
3475                         .name = "mode",         .has_arg = required_argument},
3476         { .val = LFS_LAYOUT_COPY,
3477                         .name = "copy",         .has_arg = required_argument},
3478         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument},
3479         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument},
3480         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument},
3481         { .val = 'C',   .name = "overstripe-count",
3482                                                 .has_arg = required_argument},
3483         { .val = 'd',   .name = "delete",       .has_arg = no_argument},
3484         { .val = 'd',   .name = "destroy",      .has_arg = no_argument},
3485         /* --non-direct is only valid in migrate mode */
3486         { .val = 'D',   .name = "non-direct",   .has_arg = no_argument },
3487         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument},
3488         { .val = 'E',   .name = "component-end",
3489                                                 .has_arg = required_argument},
3490         { .val = 'f',   .name = "file",         .has_arg = required_argument },
3491 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
3492 /* find { .val = 'g',   .name = "gid",          .has_arg = no_argument }, */
3493 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
3494 /* find { .val = 'h',   .name = "help",         .has_arg = no_argument }, */
3495         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument},
3496         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument},
3497         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
3498         { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
3499         { .val = 'I',   .name = "component-id", .has_arg = required_argument},
3500 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
3501         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
3502         { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
3503         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
3504         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument},
3505         /* --non-block is only valid in migrate mode */
3506         { .val = 'n',   .name = "non-block",    .has_arg = no_argument },
3507         { .val = 'N',   .name = "mirror-count", .has_arg = optional_argument},
3508         { .val = 'o',   .name = "ost",          .has_arg = required_argument },
3509 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3510         { .val = 'o',   .name = "ost-list",     .has_arg = required_argument },
3511         { .val = 'o',   .name = "ost_list",     .has_arg = required_argument },
3512 #endif
3513         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
3514 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
3515 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
3516 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3517 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
3518         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
3519         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
3520 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
3521 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3522 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
3523 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
3524         /* --verbose is only valid in migrate mode */
3525         { .val = 'v',   .name = "verbose",      .has_arg = no_argument},
3526         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
3527 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3528         { .val = 'y',   .name = "yaml",         .has_arg = required_argument },
3529         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument},
3530         { .val = 'z',   .name = "extension-size", .has_arg = required_argument},
3531         { .name = NULL } };
3532
3533         setstripe_args_init(&lsa);
3534
3535         migrate_mode = (opc == SO_MIGRATE);
3536         mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3537         setstripe_mode = (opc == SO_SETSTRIPE);
3538         if (opc == SO_MIRROR_DELETE) {
3539                 delete = 1;
3540                 mirror_flags = MF_DESTROY;
3541         }
3542
3543         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3544         progname = cmd;
3545         while ((c = getopt_long(argc, argv,
3546                                 "bc:C:dDE:f:H:i:I:m:N::no:p:L:s:S:vx:y:z:",
3547                                 long_opts, NULL)) >= 0) {
3548                 size_units = 1;
3549                 switch (c) {
3550                 case 0:
3551                         /* Long options. */
3552                         break;
3553                 case LFS_COMP_ADD_OPT:
3554                         comp_add = 1;
3555                         break;
3556                 case LFS_COMP_DEL_OPT:
3557                         comp_del = 1;
3558                         break;
3559                 case LFS_COMP_FLAGS_OPT:
3560                         result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3561                                                 &lsa.lsa_comp_neg_flags);
3562                         if (result != 0)
3563                                 goto usage_error;
3564                         if (mirror_mode && lsa.lsa_comp_neg_flags) {
3565                                 fprintf(stderr,
3566                                         "%s: inverted flags are not supported\n",
3567                                         progname);
3568                                 goto usage_error;
3569                         }
3570                         break;
3571                 case LFS_COMP_SET_OPT:
3572                         comp_set = 1;
3573                         break;
3574                 case LFS_COMP_NO_VERIFY_OPT:
3575                         mirror_flags |= MF_NO_VERIFY;
3576                         break;
3577                 case LFS_MIRROR_ID_OPT: {
3578                         unsigned long int id;
3579
3580                         errno = 0;
3581                         id = strtoul(optarg, &end, 0);
3582                         if (errno != 0 || *end != '\0' || id == 0 ||
3583                             id > UINT16_MAX) {
3584                                 fprintf(stderr,
3585                                         "%s %s: invalid mirror ID '%s'\n",
3586                                         progname, argv[0], optarg);
3587                                 goto usage_error;
3588                         }
3589
3590                         mirror_id = (__u16)id;
3591                         break;
3592                 }
3593                 case LFS_LAYOUT_FLAGS_OPT: {
3594                         uint32_t neg_flags;
3595
3596                         /* check for numeric flags (foreign and mirror cases) */
3597                         if (setstripe_mode && !mirror_mode && !last_mirror) {
3598                                 errno = 0;
3599                                 flags = strtoul(optarg, &end, 16);
3600                                 if (errno != 0 || *end != '\0' ||
3601                                     flags >= UINT32_MAX) {
3602                                         fprintf(stderr,
3603                                                 "%s %s: invalid hex flags '%s'\n",
3604                                                 progname, argv[0], optarg);
3605                                         return CMD_HELP;
3606                                 }
3607                                 if (!foreign_mode) {
3608                                         fprintf(stderr,
3609                                                 "%s %s: hex flags must be specified with --foreign option\n",
3610                                                 progname, argv[0]);
3611                                         return CMD_HELP;
3612                                 }
3613                                 break;
3614                         }
3615
3616                         if (!mirror_mode || !last_mirror) {
3617                                 fprintf(stderr,
3618                                         "error: %s: --flags must be specified with --mirror-count|-N option\n",
3619                                         progname);
3620                                 goto usage_error;
3621                         }
3622
3623                         result = comp_str2flags(optarg, &last_mirror->m_flags,
3624                                                 &neg_flags);
3625                         if (result != 0)
3626                                 goto usage_error;
3627
3628                         if (neg_flags) {
3629                                 fprintf(stderr,
3630                                         "%s: inverted flags are not supported\n",
3631                                         progname);
3632                                 result = -EINVAL;
3633                                 goto usage_error;
3634                         }
3635                         if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3636                                 fprintf(stderr,
3637                                         "%s: unsupported mirror flags: %s\n",
3638                                         progname, optarg);
3639                                 result = -EINVAL;
3640                                 goto error;
3641                         }
3642                         break;
3643                 }
3644                 case LFS_LAYOUT_FOREIGN_OPT:
3645                         if (optarg) {
3646                                 /* check pure numeric */
3647                                 type = strtoul(optarg, &end, 0);
3648                                 if (*end) {
3649                                         /* check name */
3650                                         type = check_foreign_type_name(optarg);
3651                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3652                                                 fprintf(stderr,
3653                                                         "%s %s: unrecognized foreign type '%s'\n",
3654                                                         progname, argv[0],
3655                                                         optarg);
3656                                                 return CMD_HELP;
3657                                         }
3658                                 } else if (type >= UINT32_MAX) {
3659                                         fprintf(stderr,
3660                                                 "%s %s: invalid foreign type '%s'\n",
3661                                                 progname, argv[0], optarg);
3662                                         return CMD_HELP;
3663                                 }
3664                         }
3665                         foreign_mode = true;
3666                         break;
3667                 case LFS_MODE_OPT:
3668                         mode_opt = optarg;
3669                         if (mode_opt) {
3670                                 mode = strtoul(mode_opt, &end, 8);
3671                                 if (*end != '\0') {
3672                                         fprintf(stderr,
3673                                                 "%s %s: bad mode '%s'\n",
3674                                                 progname, argv[0], mode_opt);
3675                                         return CMD_HELP;
3676                                 }
3677                                 previous_umask = umask(0);
3678                         }
3679                         break;
3680                 case LFS_LAYOUT_COPY:
3681                         from_copy = true;
3682                         template = optarg;
3683                         break;
3684                 case 'b':
3685                         if (!migrate_mode) {
3686                                 fprintf(stderr,
3687                                         "%s %s: -b|--block valid only for migrate command\n",
3688                                         progname, argv[0]);
3689                                 goto usage_error;
3690                         }
3691                         migration_block = true;
3692                         break;
3693                 case 'C':
3694                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3695                                 fprintf(stderr,
3696                                         "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3697                                         progname, argv[0]);
3698                                 goto usage_error;
3699                         }
3700                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3701                         /* fall through */
3702                 case 'c':
3703                         errno = 0;
3704                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3705                         if (errno != 0 || *end != '\0'||
3706                             lsa.lsa_stripe_count < -1 ||
3707                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3708                                 fprintf(stderr,
3709                                         "%s %s: invalid stripe count '%s'\n",
3710                                         progname, argv[0], optarg);
3711                                 goto usage_error;
3712                         }
3713
3714                         if (lsa.lsa_stripe_count == -1)
3715                                 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3716                         break;
3717                 case 'd':
3718                         /* delete the default striping pattern */
3719                         delete = 1;
3720                         if (opc == SO_MIRROR_SPLIT) {
3721                                 if (has_m_file) {
3722                                         fprintf(stderr,
3723                                               "%s %s: -d cannot used with -f\n",
3724                                                 progname, argv[0]);
3725                                         goto usage_error;
3726                                 }
3727                                 mirror_flags |= MF_DESTROY;
3728                         }
3729                         break;
3730                 case 'D':
3731                         if (!migrate_mode) {
3732                                 fprintf(stderr,
3733                                         "%s %s: -D|--non-direct is valid only for migrate command\n",
3734                                         progname, argv[0]);
3735                                 goto usage_error;
3736                         }
3737                         migration_flags |= MIGRATION_NONDIRECT;
3738                         break;
3739                 case 'E':
3740                         if (lsa.lsa_comp_end != 0) {
3741                                 result = comp_args_to_layout(lpp, &lsa, true);
3742                                 if (result) {
3743                                         fprintf(stderr, "%s: invalid layout\n",
3744                                                 progname);
3745                                         goto usage_error;
3746                                 }
3747
3748                                 setstripe_args_init_inherit(&lsa);
3749                         }
3750
3751                         if (arg_is_eof(optarg)) {
3752                                 lsa.lsa_comp_end = LUSTRE_EOF;
3753                         } else {
3754                                 result = llapi_parse_size(optarg,
3755                                                           &lsa.lsa_comp_end,
3756                                                           &size_units, 0);
3757                                 /* assume units of KB if too small */
3758                                 if (lsa.lsa_comp_end < 4096)
3759                                         lsa.lsa_comp_end *= 1024;
3760                                 if (result ||
3761                                     lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3762                                         fprintf(stderr,
3763                                                 "%s %s: invalid component end '%s'\n",
3764                                                 progname, argv[0], optarg);
3765                                         goto usage_error;
3766                                 }
3767                         }
3768                         break;
3769                 case 'H':
3770                         if (!migrate_mode) {
3771                                 fprintf(stderr,
3772                                         "--mdt-hash is valid only for migrate command\n");
3773                                 return CMD_HELP;
3774                         }
3775
3776                         lsa.lsa_pattern = check_hashtype(optarg);
3777                         if (lsa.lsa_pattern == 0) {
3778                                 fprintf(stderr,
3779                                         "%s %s: bad stripe hash type '%s'\n",
3780                                         progname, argv[0], optarg);
3781                                 return CMD_HELP;
3782                         }
3783                         break;
3784                 case 'i':
3785                         errno = 0;
3786                         lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3787                         if (errno != 0 || *end != '\0' ||
3788                             lsa.lsa_stripe_off < -1 ||
3789                             lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3790                                 fprintf(stderr,
3791                                         "%s %s: invalid stripe offset '%s'\n",
3792                                         progname, argv[0], optarg);
3793                                 goto usage_error;
3794                         }
3795                         if (lsa.lsa_stripe_off == -1)
3796                                 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3797                         break;
3798                 case 'I':
3799                         comp_id = strtoul(optarg, &end, 0);
3800                         if (*end != '\0' || comp_id == 0 ||
3801                             comp_id > LCME_ID_MAX) {
3802                                 fprintf(stderr,
3803                                         "%s %s: invalid component ID '%s'\n",
3804                                         progname, argv[0], optarg);
3805                                 goto usage_error;
3806                         }
3807                         break;
3808                 case 'f':
3809                         if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3810                                 fprintf(stderr,
3811                                         "error: %s: invalid option: %s\n",
3812                                         progname, argv[optopt + 1]);
3813                                 goto usage_error;
3814                         }
3815                         if (opc == SO_MIRROR_EXTEND) {
3816                                 if (!last_mirror) {
3817                                         fprintf(stderr,
3818                                 "error: %s: '-N' must exist in front of '%s'\n",
3819                                                 progname, argv[optopt + 1]);
3820                                         goto usage_error;
3821                                 }
3822                                 last_mirror->m_file = optarg;
3823                                 last_mirror->m_count = 1;
3824                         } else {
3825                                 /* mirror split */
3826                                 if (!mirror_list)
3827                                         mirror_list = lfs_mirror_alloc();
3828                                 mirror_list->m_file = optarg;
3829                         }
3830                         has_m_file = true;
3831                         break;
3832                 case 'L':
3833                         if (strcmp(argv[optind - 1], "mdt") == 0) {
3834                                 /* Can be only the first component */
3835                                 if (layout) {
3836                                         result = -EINVAL;
3837                                         fprintf(stderr,
3838                                                 "error: 'mdt' layout can be only the first one\n");
3839                                         goto error;
3840                                 }
3841                                 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3842                                         result = -EFBIG;
3843                                         fprintf(stderr,
3844                                                 "error: 'mdt' layout size is too big\n");
3845                                         goto error;
3846                                 }
3847                                 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
3848                         } else if (strcmp(argv[optind - 1], "raid0") != 0) {
3849                                 result = -EINVAL;
3850                                 fprintf(stderr,
3851                                         "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
3852                                         argv[optind]);
3853                                 goto error;
3854                         }
3855                         break;
3856                 case 'm':
3857                         if (!migrate_mode) {
3858                                 fprintf(stderr,
3859                                         "%s %s: -m|--mdt-index is valid only for migrate command\n",
3860                                         progname, argv[0]);
3861                                 goto usage_error;
3862                         }
3863                         migrate_mdt_mode = true;
3864                         lsa.lsa_nr_tgts = parse_targets(tgts,
3865                                                 sizeof(tgts) / sizeof(__u32),
3866                                                 lsa.lsa_nr_tgts, optarg, NULL);
3867                         if (lsa.lsa_nr_tgts < 0) {
3868                                 fprintf(stderr,
3869                                         "%s %s: invalid MDT target(s) '%s'\n",
3870                                         progname, argv[0], optarg);
3871                                 return CMD_HELP;
3872                         }
3873
3874                         lsa.lsa_tgts = tgts;
3875                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3876                                 lsa.lsa_stripe_off = tgts[0];
3877                         break;
3878                 case 'n':
3879                         if (!migrate_mode) {
3880                                 fprintf(stderr,
3881                                         "%s %s: -n|--non-block valid only for migrate command\n",
3882                                         progname, argv[0]);
3883                                 goto usage_error;
3884                         }
3885                         migration_flags |= MIGRATION_NONBLOCK;
3886                         break;
3887                 case 'N':
3888                         if (opc == SO_SETSTRIPE) {
3889                                 opc = SO_MIRROR_CREATE;
3890                                 mirror_mode = true;
3891                         }
3892                         mirror_count = 1;
3893                         if (optarg) {
3894                                 errno = 0;
3895                                 mirror_count = strtoul(optarg, &end, 0);
3896                                 if (errno != 0 || *end != '\0' ||
3897                                     mirror_count == 0 ||
3898                                     mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
3899                                         fprintf(stderr,
3900                                                 "error: %s: bad mirror count: %s\n",
3901                                                 progname, optarg);
3902                                         result = -EINVAL;
3903                                         goto error;
3904                                 }
3905                         }
3906
3907                         new_mirror = lfs_mirror_alloc();
3908                         new_mirror->m_count = mirror_count;
3909
3910                         if (!mirror_list)
3911                                 mirror_list = new_mirror;
3912
3913                         if (last_mirror) {
3914                                 /* wrap up last mirror */
3915                                 if (!setstripe_args_specified(&lsa))
3916                                         last_mirror->m_inherit = true;
3917                                 if (lsa.lsa_comp_end == 0)
3918                                         lsa.lsa_comp_end = LUSTRE_EOF;
3919
3920                                 result = comp_args_to_layout(lpp, &lsa, true);
3921                                 if (result) {
3922                                         lfs_mirror_free(new_mirror);
3923                                         goto error;
3924                                 }
3925
3926                                 setstripe_args_init_inherit(&lsa);
3927
3928                                 last_mirror->m_next = new_mirror;
3929                         }
3930
3931                         last_mirror = new_mirror;
3932                         lpp = &last_mirror->m_layout;
3933                         break;
3934                 case 'o':
3935 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3936                         if (strcmp(argv[optind - 1], "--ost-list") == 0)
3937                                 fprintf(stderr,
3938                                         "warning: '--ost-list' is deprecated, use '--ost' instead\n");
3939 #endif
3940                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3941                                 fprintf(stderr,
3942                                         "%s %s: -o|--ost incompatible with DoM layout\n",
3943                                         progname, argv[0]);
3944                                 goto usage_error;
3945                         }
3946                         /*
3947                          * -o allows overstriping, and must note it because
3948                          * parse_targets is shared with MDT striping, which
3949                          * does not allow duplicates
3950                          */
3951                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3952                         lsa.lsa_nr_tgts = parse_targets(tgts,
3953                                                 sizeof(tgts) / sizeof(__u32),
3954                                                 lsa.lsa_nr_tgts, optarg,
3955                                                 &lsa.lsa_pattern);
3956                         if (lsa.lsa_nr_tgts < 0) {
3957                                 fprintf(stderr,
3958                                         "%s %s: invalid OST target(s) '%s'\n",
3959                                         progname, argv[0], optarg);
3960                                 goto usage_error;
3961                         }
3962
3963                         lsa.lsa_tgts = tgts;
3964                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3965                                 lsa.lsa_stripe_off = tgts[0];
3966                         break;
3967                 case 'p':
3968                         if (!optarg)
3969                                 goto usage_error;
3970                         lsa.lsa_pool_name = optarg;
3971
3972                         if (strlen(lsa.lsa_pool_name) == 0 ||
3973                             strncmp(lsa.lsa_pool_name, "none",
3974                                     LOV_MAXPOOLNAME) == 0)
3975                                 lsa.lsa_pool_name = NULL;
3976                         break;
3977                 case 'S':
3978                         result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3979                                                   &size_units, 0);
3980                         /* assume units of KB if too small to be valid */
3981                         if (lsa.lsa_stripe_size < 4096)
3982                                 lsa.lsa_stripe_size *= 1024;
3983                         if (result ||
3984                             lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
3985                                 fprintf(stderr,
3986                                         "%s %s: invalid stripe size '%s'\n",
3987                                         progname, argv[0], optarg);
3988                                 goto usage_error;
3989                         }
3990                         break;
3991                 case 'v':
3992                         if (!migrate_mode) {
3993                                 fprintf(stderr,
3994                                         "%s %s: -v|--verbose valid only for migrate command\n",
3995                                         progname, argv[0]);
3996                                 goto usage_error;
3997                         }
3998                         migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3999                         migration_flags = MIGRATION_VERBOSE;
4000                         break;
4001                 case 'x':
4002                         xattr = optarg;
4003                         break;
4004                 case 'y':
4005                         from_yaml = true;
4006                         template = optarg;
4007                         break;
4008                 case 'z':
4009                         result = llapi_parse_size(optarg,
4010                                                   &lsa.lsa_extension_size,
4011                                                   &size_units, 0);
4012                         if (result) {
4013                                 fprintf(stderr,
4014                                         "%s %s: invalid extension size '%s'\n",
4015                                         progname, argv[0], optarg);
4016                                 goto usage_error;
4017                         }
4018
4019                         lsa.lsa_extension_comp = true;
4020                         break;
4021                 default:
4022                         fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4023                                 progname, argv[0], argv[optind - 1]);
4024                         goto usage_error;
4025                 }
4026         }
4027
4028         fname = argv[optind];
4029
4030         if (optind == argc) {
4031                 fprintf(stderr, "%s %s: FILE must be specified\n",
4032                         progname, argv[0]);
4033                 goto usage_error;
4034         }
4035
4036         /* lfs migrate $filename should keep the file's layout by default */
4037         if (migrate_mode && !setstripe_args_specified(&lsa) && !layout &&
4038             !from_yaml)
4039                 from_copy = true;
4040
4041         if (xattr && !foreign_mode) {
4042                 /*
4043                  * only print a warning as this is harmless and will be ignored
4044                  */
4045                 fprintf(stderr,
4046                         "%s %s: xattr has been specified for non-foreign layout\n",
4047                         progname, argv[0]);
4048         } else if (foreign_mode && !xattr) {
4049                 fprintf(stderr,
4050                         "%s %s: xattr must be provided in foreign mode\n",
4051                         progname, argv[0]);
4052                 goto usage_error;
4053         }
4054
4055         if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4056             comp_set || comp_id || delete || from_copy ||
4057             setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4058             lsa.lsa_tgts)) {
4059                 fprintf(stderr,
4060                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4061                         progname, argv[0]);
4062                 return CMD_HELP;
4063         }
4064
4065         if (mirror_mode && mirror_count == 0) {
4066                 fprintf(stderr,
4067                         "error: %s: --mirror-count|-N option is required\n",
4068                         progname);
4069                 result = -EINVAL;
4070                 goto error;
4071         }
4072
4073         if (mirror_mode) {
4074                 if (!setstripe_args_specified(&lsa))
4075                         last_mirror->m_inherit = true;
4076                 if (lsa.lsa_comp_end == 0)
4077                         lsa.lsa_comp_end = LUSTRE_EOF;
4078         }
4079
4080         if (lsa.lsa_comp_end != 0) {
4081                 result = comp_args_to_layout(lpp, &lsa, true);
4082                 if (result) {
4083                         fprintf(stderr, "error: %s: invalid layout\n",
4084                                 progname);
4085                         result = -EINVAL;
4086                         goto error;
4087                 }
4088         }
4089
4090         if (mirror_flags & MF_NO_VERIFY) {
4091                 if (opc != SO_MIRROR_EXTEND) {
4092                         fprintf(stderr,
4093                                 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4094                                 progname);
4095                         result = -EINVAL;
4096                         goto error;
4097                 } else if (!has_m_file) {
4098                         fprintf(stderr,
4099                                 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4100                                 progname);
4101                         result = -EINVAL;
4102                         goto error;
4103                 }
4104         }
4105
4106         if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4107                 fprintf(stderr,
4108                         "%s %s: --component-set doesn't have component-id set\n",
4109                         progname, argv[0]);
4110                 goto usage_error;
4111         }
4112
4113         if ((delete + comp_set + comp_del + comp_add) > 1) {
4114                 fprintf(stderr,
4115                         "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4116                         progname, argv[0]);
4117                 goto usage_error;
4118         }
4119
4120         if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4121                        lsa.lsa_comp_flags != 0 || layout != NULL)) {
4122                 fprintf(stderr,
4123                         "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4124                         progname, argv[0]);
4125                 goto usage_error;
4126         }
4127
4128         if ((comp_set || comp_del) &&
4129             (setstripe_args_specified(&lsa) || layout != NULL)) {
4130                 fprintf(stderr,
4131                         "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4132                         progname, argv[0]);
4133                 goto usage_error;
4134         }
4135
4136         if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4137                 fprintf(stderr,
4138                         "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4139                         progname, argv[0]);
4140                 goto usage_error;
4141         }
4142
4143         if (comp_add || comp_del) {
4144                 struct stat st;
4145
4146                 result = lstat(fname, &st);
4147                 if (result == 0 && S_ISDIR(st.st_mode)) {
4148                         fprintf(stderr,
4149                                 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4150                                 progname);
4151                         goto usage_error;
4152                 }
4153
4154                 if (mirror_mode) {
4155                         fprintf(stderr,
4156                                 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4157                                 progname);
4158                         goto usage_error;
4159                 }
4160         }
4161
4162         if (comp_add) {
4163                 if (!layout) {
4164                         fprintf(stderr,
4165                                 "%s %s: option -E must be specified with --component-add\n",
4166                                 progname, argv[0]);
4167                         goto usage_error;
4168                 }
4169         }
4170
4171         if (from_yaml && from_copy) {
4172                 fprintf(stderr,
4173                         "%s: can't specify --yaml and --copy together\n",
4174                         progname);
4175                 goto error;
4176         }
4177
4178         if ((from_yaml || from_copy) &&
4179             (setstripe_args_specified(&lsa) || layout != NULL)) {
4180                 fprintf(stderr,
4181                         "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4182                         argv[0]);
4183                 goto error;
4184         }
4185
4186         if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
4187                 fprintf(stderr,
4188                         "%s %s: options --non-block and --block are mutually exclusive\n",
4189                         progname, argv[0]);
4190                 goto usage_error;
4191         }
4192
4193         if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4194             opc != SO_MIRROR_DELETE && comp_id != 0) {
4195                 fprintf(stderr,
4196                         "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4197                         progname);
4198                 goto usage_error;
4199         }
4200
4201         if (migrate_mdt_mode) {
4202                 struct lmv_user_md *lmu;
4203
4204                 /* initialize migrate mdt parameters */
4205                 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4206                                                  LMV_USER_MAGIC_SPECIFIC));
4207                 if (!lmu) {
4208                         fprintf(stderr,
4209                                 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4210                                 progname, argv[0], strerror(ENOMEM));
4211                         result = -ENOMEM;
4212                         goto error;
4213                 }
4214                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4215                         lmu->lum_stripe_count = lsa.lsa_stripe_count;
4216                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4217                         fprintf(stderr,
4218                                 "%s %s: migrate should specify MDT index\n",
4219                                 progname, argv[0]);
4220                         free(lmu);
4221                         goto usage_error;
4222                 }
4223                 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4224                 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4225                         lmu->lum_hash_type = lsa.lsa_pattern;
4226                 else
4227                         lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4228                 if (lsa.lsa_pool_name) {
4229                         strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4230                                 sizeof(lmu->lum_pool_name) - 1);
4231                         lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4232                 }
4233                 if (lsa.lsa_nr_tgts > 1) {
4234                         int i;
4235
4236                         if (lsa.lsa_stripe_count > 0 &&
4237                             lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4238                             lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4239                                 fprintf(stderr,
4240                                         "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4241                                         progname, lsa.lsa_stripe_count,
4242                                         lsa.lsa_nr_tgts);
4243                                 free(lmu);
4244                                 goto usage_error;
4245                         }
4246
4247                         lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4248                         lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4249                         for (i = 0; i < lsa.lsa_nr_tgts; i++)
4250                                 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4251                 } else {
4252                         lmu->lum_magic = LMV_USER_MAGIC;
4253                 }
4254
4255                 migrate_mdt_param.fp_lmv_md = lmu;
4256                 migrate_mdt_param.fp_migrate = 1;
4257         } else if (!layout) {
4258                 if (lsa_args_stripe_count_check(&lsa))
4259                         goto usage_error;
4260
4261                 /* initialize stripe parameters */
4262                 param = calloc(1, offsetof(typeof(*param),
4263                                lsp_osts[lsa.lsa_nr_tgts]));
4264                 if (!param) {
4265                         fprintf(stderr,
4266                                 "%s %s: cannot allocate memory for parameters: %s\n",
4267                                 progname, argv[0], strerror(ENOMEM));
4268                         result = -ENOMEM;
4269                         goto error;
4270                 }
4271
4272                 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4273                         param->lsp_stripe_size = lsa.lsa_stripe_size;
4274                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4275                         if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4276                                 param->lsp_stripe_count = -1;
4277                         else
4278                                 param->lsp_stripe_count = lsa.lsa_stripe_count;
4279                 }
4280                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4281                         param->lsp_stripe_offset = -1;
4282                 else
4283                         param->lsp_stripe_offset = lsa.lsa_stripe_off;
4284                 param->lsp_stripe_pattern =
4285                                 llapi_pattern_to_lov(lsa.lsa_pattern);
4286                 if (param->lsp_stripe_pattern == EINVAL) {
4287                         fprintf(stderr, "error: %s: invalid stripe pattern\n",
4288                                 argv[0]);
4289                         free(param);
4290                         goto usage_error;
4291                 }
4292                 param->lsp_pool = lsa.lsa_pool_name;
4293                 param->lsp_is_specific = false;
4294
4295                 if (lsa.lsa_nr_tgts > 0) {
4296                         param->lsp_is_specific = true;
4297                         param->lsp_stripe_count = lsa.lsa_nr_tgts;
4298                         memcpy(param->lsp_osts, tgts,
4299                                sizeof(*tgts) * lsa.lsa_nr_tgts);
4300                 }
4301         }
4302
4303         if (from_yaml) {
4304                 /* generate a layout from a YAML template */
4305                 result = lfs_comp_create_from_yaml(template, &layout,
4306                                                    &lsa, tgts);
4307                 if (result) {
4308                         fprintf(stderr,
4309                                 "error: %s: can't create composite layout from template file %s\n",
4310                                 argv[0], template);
4311                         goto error;
4312                 }
4313         }
4314
4315         if (layout != NULL || mirror_list != NULL) {
4316                 if (mirror_list)
4317                         result = mirror_adjust_first_extents(mirror_list);
4318                 else
4319                         result = layout_adjust_first_extent(fname, layout,
4320                                                             comp_add);
4321                 if (result == -ENODATA)
4322                         comp_add = 0;
4323                 else if (result != 0) {
4324                         fprintf(stderr, "error: %s: invalid layout\n",
4325                                 progname);
4326                         goto error;
4327                 }
4328         }
4329
4330         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4331                 if (from_copy) {
4332                         layout = llapi_layout_get_by_path(template ?: fname, 0);
4333                         if (!layout) {
4334                                 fprintf(stderr,
4335                                         "%s: can't create composite layout from file %s: %s\n",
4336                                         progname, template ?: fname,
4337                                         strerror(errno));
4338                                 result = -errno;
4339                                 goto error;
4340                         }
4341                 }
4342
4343                 if (migrate_mdt_mode) {
4344                         result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4345                 } else if (migrate_mode) {
4346                         result = lfs_migrate(fname, migration_flags, param,
4347                                              layout);
4348                 } else if (comp_set != 0) {
4349                         result = lfs_component_set(fname, comp_id,
4350                                                    lsa.lsa_pool_name,
4351                                                    lsa.lsa_comp_flags,
4352                                                    lsa.lsa_comp_neg_flags);
4353                 } else if (comp_del != 0) {
4354                         result = lfs_component_del(fname, comp_id,
4355                                                    lsa.lsa_comp_flags,
4356                                                    lsa.lsa_comp_neg_flags);
4357                 } else if (comp_add != 0) {
4358                         result = lfs_component_add(fname, layout);
4359                 } else if (opc == SO_MIRROR_CREATE) {
4360                         result = mirror_create(fname, mirror_list);
4361                 } else if (opc == SO_MIRROR_EXTEND) {
4362                         result = mirror_extend(fname, mirror_list,
4363                                                mirror_flags);
4364                 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4365                         if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4366                                 fprintf(stderr,
4367                                         "%s: no mirror specified to delete from '%s'\n",
4368                                         progname, fname);
4369                                 goto usage_error;
4370                         }
4371                         if (lsa.lsa_pool_name)
4372                                 mirror_flags |= MF_COMP_POOL;
4373                         else if (mirror_id != 0)
4374                                 comp_id = mirror_id;
4375                         else
4376                                 mirror_flags |= MF_COMP_ID;
4377                         if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4378                                 fprintf(stderr,
4379                                         "%s: the file specified by -f cannot be same as the source file '%s'\n",
4380                                         progname, fname);
4381                                 goto usage_error;
4382                         }
4383                         result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4384                                               mirror_flags,
4385                                               has_m_file ? mirror_list->m_file :
4386                                               NULL);
4387                 } else if (layout) {
4388                         result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4389                                                       mode, layout);
4390                         if (result >= 0) {
4391                                 close(result);
4392                                 result = 0;
4393                         }
4394                 } else if (foreign_mode) {
4395                         result = llapi_file_create_foreign(fname, mode, type,
4396                                                            flags, xattr);
4397                         if (result >= 0) {
4398                                 close(result);
4399                                 result = 0;
4400                         }
4401                 } else {
4402                         result = llapi_file_open_param(fname,
4403                                                        O_CREAT | O_WRONLY,
4404                                                        mode, param);
4405                         if (result >= 0) {
4406                                 close(result);
4407                                 result = 0;
4408                         }
4409                 }
4410                 if (result) {
4411                         /* Save the first error encountered. */
4412                         if (result2 == 0)
4413                                 result2 = result;
4414                         continue;
4415                 }
4416         }
4417
4418         if (mode_opt)
4419                 umask(previous_umask);
4420
4421         free(param);
4422         free(migrate_mdt_param.fp_lmv_md);
4423         llapi_layout_free(layout);
4424         lfs_mirror_list_free(mirror_list);
4425         return result2;
4426 usage_error:
4427         result = CMD_HELP;
4428 error:
4429         llapi_layout_free(layout);
4430         lfs_mirror_list_free(mirror_list);
4431         return result;
4432 }
4433
4434 static int lfs_poollist(int argc, char **argv)
4435 {
4436         if (argc != 2)
4437                 return CMD_HELP;
4438
4439         return llapi_poollist(argv[1]);
4440 }
4441
4442 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4443 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4444                        char *str)
4445 {
4446         long long t = 0;
4447         int sign = 0;
4448         char *endptr = "AD";
4449         char *timebuf;
4450
4451         if (str[0] == '+')
4452                 sign = 1;
4453         else if (str[0] == '-')
4454                 sign = -1;
4455
4456         if (sign)
4457                 str++;
4458
4459         for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4460                 long long val = strtoll(timebuf, &endptr, 0);
4461                 int unit = 1;
4462
4463                 switch (*endptr) {
4464                 case  'y':
4465                         unit *= 52; /* 52 weeks + 1 day below */
4466                 case  'w':      /* fallthrough */
4467                         unit *= 7;
4468                         if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4469                                 param->fp_time_margin *= (1 + unit / 52);
4470                         unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4471                 case '\0': /* days are default unit if none used */
4472                 case  'd':      /* fallthrough */
4473                         unit *= 24;
4474                 case  'h':      /* fallthrough */
4475                         unit *= 60;
4476                 case  'm':      /* fallthrough */
4477                         unit *= 60;
4478                 case  's':      /* fallthrough */
4479                         break;
4480                         /* don't need to multiply by 1 for seconds */
4481                 default:
4482                         fprintf(stderr,
4483                                 "%s find: bad time string '%s': %s\n",
4484                                 progname, timebuf, strerror(EINVAL));
4485                         return LONG_MAX;
4486                 }
4487
4488                 if (param->fp_time_margin == 0 ||
4489                     (*endptr && unit < param->fp_time_margin))
4490                         param->fp_time_margin = unit;
4491
4492                 t += val * unit;
4493         }
4494         if (*time < t) {
4495                 if (sign != 0)
4496                         str--;
4497                 fprintf(stderr, "%s find: bad time '%s': too large\n",
4498                         progname, str);
4499                 return LONG_MAX;
4500         }
4501
4502         *set = *time - t;
4503
4504         return sign;
4505 }
4506
4507 static int str2quotaid(__u32 *id, const char *arg)
4508 {
4509         unsigned long int projid_tmp = 0;
4510         char *endptr = NULL;
4511
4512         projid_tmp = strtoul(arg, &endptr, 10);
4513         if (*endptr != '\0')
4514                 return -EINVAL;
4515         if (projid_tmp >= UINT32_MAX)
4516                 return -ERANGE;
4517
4518         *id = projid_tmp;
4519         return 0;
4520 }
4521
4522 static int name2uid(unsigned int *id, const char *name)
4523 {
4524         struct passwd *passwd;
4525
4526         passwd = getpwnam(name);
4527         if (!passwd)
4528                 return -ENOENT;
4529         *id = passwd->pw_uid;
4530
4531         return 0;
4532 }
4533
4534 static int name2gid(unsigned int *id, const char *name)
4535 {
4536         struct group *group;
4537
4538         group = getgrnam(name);
4539         if (!group)
4540                 return -ENOENT;
4541         *id = group->gr_gid;
4542
4543         return 0;
4544 }
4545
4546 static inline int name2projid(unsigned int *id, const char *name)
4547 {
4548         return -ENOTSUP;
4549 }
4550
4551 static int uid2name(char **name, unsigned int id)
4552 {
4553         struct passwd *passwd;
4554
4555         passwd = getpwuid(id);
4556         if (!passwd)
4557                 return -ENOENT;
4558         *name = passwd->pw_name;
4559
4560         return 0;
4561 }
4562
4563 static inline int gid2name(char **name, unsigned int id)
4564 {
4565         struct group *group;
4566
4567         group = getgrgid(id);
4568         if (!group)
4569                 return -ENOENT;
4570         *name = group->gr_name;
4571
4572         return 0;
4573 }
4574
4575 static int name2layout(__u32 *layout, char *name)
4576 {
4577         char *ptr, *layout_name;
4578
4579         *layout = 0;
4580         for (ptr = name; ; ptr = NULL) {
4581                 layout_name = strtok(ptr, ",");
4582                 if (!layout_name)
4583                         break;
4584                 if (strcmp(layout_name, "released") == 0)
4585                         *layout |= LOV_PATTERN_F_RELEASED;
4586                 else if (strcmp(layout_name, "raid0") == 0)
4587                         *layout |= LOV_PATTERN_RAID0;
4588                 else if (strcmp(layout_name, "mdt") == 0)
4589                         *layout |= LOV_PATTERN_MDT;
4590                 else if (strcmp(layout_name, "overstriping") == 0)
4591                         *layout |= LOV_PATTERN_OVERSTRIPING;
4592                 else
4593                         return -1;
4594         }
4595         return 0;
4596 }
4597
4598 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4599 {
4600         int loop;
4601         int user, group, other;
4602         int who, all;
4603         char c, op;
4604         mode_t perm;
4605         mode_t usermask;
4606         mode_t previous_flags;
4607
4608         user = group = other = 0;
4609         all = 0;
4610         loop = 1;
4611         perm = 0;
4612         previous_flags = 0;
4613         *end = input;
4614         usermask = 0;
4615
4616         while (loop) {
4617                 switch (*input) {
4618                 case 'u':
4619                         user = 1;
4620                         break;
4621                 case 'g':
4622                         group = 1;
4623                         break;
4624                 case 'o':
4625                         other = 1;
4626                         break;
4627                 case 'a':
4628                         user = group = other = 1;
4629                         all = 1;
4630                         break;
4631                 default:
4632                         loop = 0;
4633                 }
4634
4635                 if (loop)
4636                         input++;
4637         }
4638
4639         who = user || group || other;
4640         if (!who) {
4641                 /* get the umask */
4642                 usermask = umask(0022);
4643                 umask(usermask);
4644                 usermask &= 07777;
4645         }
4646
4647         if (*input == '-' || *input == '+' || *input == '=')
4648                 op = *input++;
4649         else
4650                 /* operation is required */
4651                 return -1;
4652
4653         /* get the flags in *outmode */
4654         switch (*input) {
4655         case 'u':
4656                 previous_flags = (*outmode & 0700);
4657                 perm |= user  ? previous_flags : 0;
4658                 perm |= group ? (previous_flags >> 3) : 0;
4659                 perm |= other ? (previous_flags >> 6) : 0;
4660                 input++;
4661                 goto write_perm;
4662         case 'g':
4663                 previous_flags = (*outmode & 0070);
4664                 perm |= user  ? (previous_flags << 3) : 0;
4665                 perm |= group ? previous_flags : 0;
4666                 perm |= other ? (previous_flags >> 3) : 0;
4667                 input++;
4668                 goto write_perm;
4669         case 'o':
4670                 previous_flags = (*outmode & 0007);
4671                 perm |= user  ? (previous_flags << 6) : 0;
4672                 perm |= group ? (previous_flags << 3) : 0;
4673                 perm |= other ? previous_flags : 0;
4674                 input++;
4675                 goto write_perm;
4676         default:
4677                 break;
4678         }
4679
4680         /* this part is optional,
4681          * if empty perm = 0 and *outmode is not modified
4682          */
4683         loop = 1;
4684         while (loop) {
4685                 c = *input;
4686                 switch (c) {
4687                 case 'r':
4688                         perm |= user  ? 0400 : 0;
4689                         perm |= group ? 0040 : 0;
4690                         perm |= other ? 0004 : 0;
4691                         /* set read permission for uog except for umask's
4692                          * permissions
4693                          */
4694                         perm |= who   ? 0 : (0444 & ~usermask);
4695                         break;
4696                 case 'w':
4697                         perm |= user  ? 0200 : 0;
4698                         perm |= group ? 0020 : 0;
4699                         perm |= other ? 0002 : 0;
4700                         /* set write permission for uog except for umask'
4701                          * permissions
4702                          */
4703                         perm |= who   ? 0 : (0222 & ~usermask);
4704                         break;
4705                 case 'x':
4706                         perm |= user  ? 0100 : 0;
4707                         perm |= group ? 0010 : 0;
4708                         perm |= other ? 0001 : 0;
4709                         /* set execute permission for uog except for umask'
4710                          * permissions
4711                          */
4712                         perm |= who   ? 0 : (0111 & ~usermask);
4713                         break;
4714                 case 'X':
4715                         /*
4716                          * Adds execute permission to 'u', 'g' and/or 'g' if
4717                          * specified and either 'u', 'g' or 'o' already has
4718                          * execute permissions.
4719                          */
4720                         if ((*outmode & 0111) != 0) {
4721                                 perm |= user  ? 0100 : 0;
4722                                 perm |= group ? 0010 : 0;
4723                                 perm |= other ? 0001 : 0;
4724                                 perm |= !who  ? 0111 : 0;
4725                         }
4726                         break;
4727                 case 's':
4728                         /* s is ignored if o is given, but it's not an error */
4729                         if (other && !group && !user)
4730                                 break;
4731                         perm |= user  ? S_ISUID : 0;
4732                         perm |= group ? S_ISGID : 0;
4733                         break;
4734                 case 't':
4735                         /* 't' should be used when 'a' is given
4736                          * or who is empty
4737                          */
4738                         perm |= (!who || all) ? S_ISVTX : 0;
4739                         /* using ugo with t is not an error */
4740                         break;
4741                 default:
4742                         loop = 0;
4743                         break;
4744                 }
4745                 if (loop)
4746                         input++;
4747         }
4748
4749 write_perm:
4750         /* uog flags should be only one character long */
4751         if (previous_flags && (*input != '\0' && *input != ','))
4752                 return -1;
4753
4754         switch (op) {
4755         case '-':
4756                 /* remove the flags from outmode */
4757                 *outmode &= ~perm;
4758                 break;
4759         case '+':
4760                 /* add the flags to outmode */
4761                 *outmode |= perm;
4762                 break;
4763         case '=':
4764                 /* set the flags of outmode to perm */
4765                 if (perm != 0)
4766                         *outmode = perm;
4767                 break;
4768         }
4769
4770         *end = input;
4771         return 0;
4772 }
4773
4774 static int str2mode_t(const char *input, mode_t *outmode)
4775 {
4776         int ret;
4777         const char *iter;
4778
4779         ret = 0;
4780
4781         if (*input >= '0' && *input <= '7') {
4782                 /* parse octal representation */
4783                 char *end;
4784
4785                 iter = input;
4786
4787                 /* look for invalid digits in octal representation */
4788                 while (isdigit(*iter))
4789                         if (*iter++ > '7')
4790                                 return -1;
4791
4792                 errno = 0;
4793                 *outmode = strtoul(input, &end, 8);
4794
4795                 if (errno != 0 || *outmode > 07777) {
4796                         *outmode = 0;
4797                         ret = -1;
4798                 }
4799
4800         } else if (*input == '8' || *input == '9') {
4801                 /* error: invalid octal number */
4802                 ret = -1;
4803         } else {
4804                 /* parse coma seperated list of symbolic representation */
4805                 int rc;
4806                 const char *end;
4807
4808                 *outmode = 0;
4809                 rc = 0;
4810                 end = NULL;
4811
4812                 do {
4813                         rc = parse_symbolic(input, outmode, &end);
4814                         if (rc)
4815                                 return -1;
4816
4817                         input = end+1;
4818                 } while (*end == ',');
4819
4820                 if (*end != '\0')
4821                         ret = -1;
4822         }
4823         return ret;
4824 }
4825
4826 static int lfs_find(int argc, char **argv)
4827 {
4828         int c, rc;
4829         int ret = 0;
4830         time_t t;
4831         struct find_param param = {
4832                 .fp_max_depth = -1,
4833                 .fp_quiet = 1,
4834                 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
4835         };
4836         struct option long_opts[] = {
4837         { .val = 'A',   .name = "atime",        .has_arg = required_argument },
4838         { .val = 'b',   .name = "blocks",       .has_arg = required_argument },
4839         { .val = 'B',   .name = "btime",        .has_arg = required_argument },
4840         { .val = 'B',   .name = "Btime",        .has_arg = required_argument },
4841         { .val = LFS_COMP_COUNT_OPT,
4842                         .name = "comp-count",   .has_arg = required_argument },
4843         { .val = LFS_COMP_COUNT_OPT,
4844                         .name = "component-count",
4845                                                 .has_arg = required_argument },
4846         { .val = LFS_COMP_FLAGS_OPT,
4847                         .name = "comp-flags",   .has_arg = required_argument },
4848         { .val = LFS_COMP_FLAGS_OPT,
4849                         .name = "component-flags",
4850                                                 .has_arg = required_argument },
4851         { .val = LFS_COMP_START_OPT,
4852                         .name = "comp-start",   .has_arg = required_argument },
4853         { .val = LFS_COMP_START_OPT,
4854                         .name = "component-start",
4855                                                 .has_arg = required_argument },
4856         { .val = LFS_MIRROR_STATE_OPT,
4857                         .name = "mirror-state", .has_arg = required_argument },
4858         { .val = LFS_NEWERXY_OPT,
4859                         .name = "newer",        .has_arg = required_argument},
4860         { .val = LFS_NEWERXY_OPT,
4861                         .name = "neweraa",      .has_arg = required_argument},
4862         { .val = LFS_NEWERXY_OPT,
4863                         .name = "neweram",      .has_arg = required_argument},
4864         { .val = LFS_NEWERXY_OPT,
4865                         .name = "newerac",      .has_arg = required_argument},
4866         { .val = LFS_NEWERXY_OPT,
4867                         .name = "newerab",      .has_arg = required_argument},
4868         { .val = LFS_NEWERXY_OPT,
4869                         .name = "newerma",      .has_arg = required_argument},
4870         { .val = LFS_NEWERXY_OPT,
4871                         .name = "newermm",      .has_arg = required_argument},
4872         { .val = LFS_NEWERXY_OPT,
4873                         .name = "newermc",      .has_arg = required_argument},
4874         { .val = LFS_NEWERXY_OPT,
4875                         .name = "newermb",      .has_arg = required_argument},
4876         { .val = LFS_NEWERXY_OPT,
4877                         .name = "newerca",      .has_arg = required_argument},
4878         { .val = LFS_NEWERXY_OPT,
4879                         .name = "newercm",      .has_arg = required_argument},
4880         { .val = LFS_NEWERXY_OPT,
4881                         .name = "newercc",      .has_arg = required_argument},
4882         { .val = LFS_NEWERXY_OPT,
4883                         .name = "newercb",      .has_arg = required_argument},
4884         { .val = LFS_NEWERXY_OPT,
4885                         .name = "newerba",      .has_arg = required_argument},
4886         { .val = LFS_NEWERXY_OPT,
4887                         .name = "newerbm",      .has_arg = required_argument},
4888         { .val = LFS_NEWERXY_OPT,
4889                         .name = "newerbc",      .has_arg = required_argument},
4890         { .val = LFS_NEWERXY_OPT,
4891                         .name = "newerbb",      .has_arg = required_argument},
4892         { .val = LFS_NEWERXY_OPT,
4893                         .name = "newerBa",      .has_arg = required_argument},
4894         { .val = LFS_NEWERXY_OPT,
4895                         .name = "newerBm",      .has_arg = required_argument},
4896         { .val = LFS_NEWERXY_OPT,
4897                         .name = "newerBc",      .has_arg = required_argument},
4898         { .val = LFS_NEWERXY_OPT,
4899                         .name = "newerBB",      .has_arg = required_argument},
4900         { .val = LFS_NEWERXY_OPT,
4901                         .name = "newerat",      .has_arg = required_argument},
4902         { .val = LFS_NEWERXY_OPT,
4903                         .name = "newermt",      .has_arg = required_argument},
4904         { .val = LFS_NEWERXY_OPT,
4905                         .name = "newerct",      .has_arg = required_argument},
4906         { .val = LFS_NEWERXY_OPT,
4907                         .name = "newerbt",      .has_arg = required_argument},
4908         { .val = LFS_NEWERXY_OPT,
4909                         .name = "newerBt",      .has_arg = required_argument},
4910         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
4911         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
4912         { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
4913 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
4914         { .val = 'D',   .name = "maxdepth",     .has_arg = required_argument },
4915         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument },
4916         { .val = 'E',   .name = "component-end",
4917                                                 .has_arg = required_argument },
4918 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
4919         { .val = LFS_LAYOUT_FOREIGN_OPT,
4920                         .name = "foreign",      .has_arg = optional_argument},
4921         { .val = 'g',   .name = "gid",          .has_arg = required_argument },
4922         { .val = 'G',   .name = "group",        .has_arg = required_argument },
4923         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
4924         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
4925         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
4926 /* getstripe { .val = 'I', .name = "comp-id",   .has_arg = required_argument }*/
4927         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
4928         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
4929         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
4930         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
4931         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument },
4932         { .val = 'M',   .name = "mtime",        .has_arg = required_argument },
4933         { .val = 'n',   .name = "name",         .has_arg = required_argument },
4934         { .val = 'N',   .name = "mirror-count", .has_arg = required_argument },
4935 /* find { .val = 'o'    .name = "or", .has_arg = no_argument }, like find(1) */
4936         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
4937         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
4938         /* no short option for pool yet, can be 'p' after 2.18 */
4939         { .val = LFS_POOL_OPT,
4940                         .name = "pool",         .has_arg = required_argument },
4941         { .val = '0',   .name = "print0",       .has_arg = no_argument },
4942         { .val = 'P',   .name = "print",        .has_arg = no_argument },
4943         { .val = LFS_PROJID_OPT,
4944                         .name = "projid",       .has_arg = required_argument },
4945 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
4946 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
4947 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
4948         { .val = 's',   .name = "size",         .has_arg = required_argument },
4949         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
4950         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
4951         { .val = 't',   .name = "type",         .has_arg = required_argument },
4952         { .val = LFS_FIND_PERM,
4953                         .name = "perm",         .has_arg = required_argument },
4954         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
4955         { .val = 'u',   .name = "uid",          .has_arg = required_argument },
4956         { .val = 'U',   .name = "user",         .has_arg = required_argument },
4957         { .val = 'z',   .name = "extension-size",
4958                                                 .has_arg = required_argument },
4959         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument },
4960 /* getstripe { .val = 'v', .name = "verbose",   .has_arg = no_argument }, */
4961 /* getstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
4962         { .name = NULL } };
4963         int optidx = 0;
4964         int pathstart = -1;
4965         int pathend = -1;
4966         int pathbad = -1;
4967         int neg_opt = 0;
4968         time_t *xtime;
4969         int *xsign;
4970         int isoption;
4971         char *endptr;
4972
4973         time(&t);
4974
4975         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
4976         while ((c = getopt_long_only(argc, argv,
4977                 "-0A:b:B:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:vz:",
4978                 long_opts, &optidx)) >= 0) {
4979                 xtime = NULL;
4980                 xsign = NULL;
4981                 if (neg_opt)
4982                         --neg_opt;
4983                 /* '!' is part of option */
4984                 /*
4985                  * when getopt_long_only() finds a string which is not
4986                  * an option nor a known option argument it returns 1
4987                  * in that case if we already have found pathstart and pathend
4988                  * (i.e. we have the list of pathnames),
4989                  * the only supported value is "!"
4990                  */
4991                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4992                 if (!isoption && pathend != -1) {
4993                         fprintf(stderr,
4994                                 "err: %s: filename|dirname must either precede options or follow options\n",
4995                                 argv[0]);
4996                         ret = CMD_HELP;
4997                         goto err;
4998                 }
4999                 if (!isoption && pathstart == -1)
5000                         pathstart = optind - 1;
5001                 if (isoption && pathstart != -1 && pathend == -1)
5002                         pathend = optind - 2;
5003                 switch (c) {
5004                 case 0:
5005                         /* Long options. */
5006                         break;
5007                 case 1:
5008                         /*
5009                          * unknown; opt is "!" or path component,
5010                          * checking done above.
5011                          */
5012                         if (strcmp(optarg, "!") == 0)
5013                                 neg_opt = 2;
5014                         break;
5015                 case 'A':
5016                         xtime = &param.fp_atime;
5017                         xsign = &param.fp_asign;
5018                         param.fp_exclude_atime = !!neg_opt;
5019                         /* no break, this falls through to 'B' for btime */
5020                 case 'B':
5021                         if (c == 'B') {
5022                                 xtime = &param.fp_btime;
5023                                 xsign = &param.fp_bsign;
5024                                 param.fp_exclude_btime = !!neg_opt;
5025                         }
5026                         /* no break, this falls through to 'C' for ctime */
5027                 case 'C':
5028                         if (c == 'C') {
5029                                 xtime = &param.fp_ctime;
5030                                 xsign = &param.fp_csign;
5031                                 param.fp_exclude_ctime = !!neg_opt;
5032                         }
5033                         /* no break, this falls through to 'M' for mtime */
5034                 case 'M':
5035                         if (c == 'M') {
5036                                 xtime = &param.fp_mtime;
5037                                 xsign = &param.fp_msign;
5038                                 param.fp_exclude_mtime = !!neg_opt;
5039                         }
5040                         rc = set_time(&param, &t, xtime, optarg);
5041                         if (rc == LONG_MAX) {
5042                                 ret = -1;
5043                                 goto err;
5044                         }
5045                         if (rc)
5046                                 *xsign = rc;
5047                         break;
5048                 case 'b':
5049                         if (optarg[0] == '+') {
5050                                 param.fp_blocks_sign = -1;
5051                                 optarg++;
5052                         } else if (optarg[0] == '-') {
5053                                 param.fp_blocks_sign =  1;
5054                                 optarg++;
5055                         }
5056
5057                         param.fp_blocks_units = 1024;
5058                         ret = llapi_parse_size(optarg, &param.fp_blocks,
5059                                                &param.fp_blocks_units, 0);
5060                         if (ret) {
5061                                 fprintf(stderr, "error: bad blocks '%s'\n",
5062                                         optarg);
5063                                 goto err;
5064                         }
5065                         param.fp_check_blocks = 1;
5066                         param.fp_exclude_blocks = !!neg_opt;
5067                         break;
5068                 case LFS_COMP_COUNT_OPT:
5069                         if (optarg[0] == '+') {
5070                                 param.fp_comp_count_sign = -1;
5071                                 optarg++;
5072                         } else if (optarg[0] == '-') {
5073                                 param.fp_comp_count_sign =  1;
5074                                 optarg++;
5075                         }
5076
5077                         errno = 0;
5078                         param.fp_comp_count = strtoul(optarg, &endptr, 0);
5079                         if (errno != 0 || *endptr != '\0' ||
5080                             param.fp_comp_count > UINT32_MAX) {
5081                                 fprintf(stderr,
5082                                         "error: bad component count '%s'\n",
5083                                         optarg);
5084                                 goto err;
5085                         }
5086                         param.fp_check_comp_count = 1;
5087                         param.fp_exclude_comp_count = !!neg_opt;
5088                         break;
5089                 case LFS_COMP_FLAGS_OPT:
5090                         rc = comp_str2flags(optarg, &param.fp_comp_flags,
5091                                             &param.fp_comp_neg_flags);
5092                         if (rc) {
5093                                 fprintf(stderr,
5094                                         "error: bad component flags '%s'\n",
5095                                         optarg);
5096                                 goto err;
5097                         }
5098                         param.fp_check_comp_flags = 1;
5099                         if (neg_opt) {
5100                                 __u32 flags = param.fp_comp_neg_flags;
5101
5102                                 param.fp_comp_neg_flags = param.fp_comp_flags;
5103                                 param.fp_comp_flags = flags;
5104                         }
5105                         break;
5106                 case LFS_COMP_START_OPT:
5107                         if (optarg[0] == '+') {
5108                                 param.fp_comp_start_sign = -1;
5109                                 optarg++;
5110                         } else if (optarg[0] == '-') {
5111                                 param.fp_comp_start_sign =  1;
5112                                 optarg++;
5113                         }
5114
5115                         rc = llapi_parse_size(optarg, &param.fp_comp_start,
5116                                               &param.fp_comp_start_units, 0);
5117                         if (rc) {
5118                                 fprintf(stderr,
5119                                         "error: bad component start '%s'\n",
5120                                         optarg);
5121                                 goto err;
5122                         }
5123                         param.fp_check_comp_start = 1;
5124                         param.fp_exclude_comp_start = !!neg_opt;
5125                         break;
5126                 case LFS_MIRROR_STATE_OPT:
5127                         rc = mirror_str2state(optarg, &param.fp_mirror_state,
5128                                               &param.fp_mirror_neg_state);
5129                         if (rc) {
5130                                 fprintf(stderr,
5131                                         "error: bad mirrored file state '%s'\n",
5132                                         optarg);
5133                                 goto err;
5134                         }
5135                         param.fp_check_mirror_state = 1;
5136                         if (neg_opt) {
5137                                 __u16 state = param.fp_mirror_neg_state;
5138
5139                                 param.fp_mirror_neg_state =
5140                                         param.fp_mirror_state;
5141                                 param.fp_mirror_state = state;
5142                         }
5143                         break;
5144                 case 'c':
5145                         if (optarg[0] == '+') {
5146                                 param.fp_stripe_count_sign = -1;
5147                                 optarg++;
5148                         } else if (optarg[0] == '-') {
5149                                 param.fp_stripe_count_sign =  1;
5150                                 optarg++;
5151                         }
5152
5153                         errno = 0;
5154                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5155                         if (errno != 0 || *endptr != '\0' ||
5156                             param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5157                                 fprintf(stderr,
5158                                         "error: bad stripe_count '%s'\n",
5159                                         optarg);
5160                                 ret = -1;
5161                                 goto err;
5162                         }
5163                         param.fp_check_stripe_count = 1;
5164                         param.fp_exclude_stripe_count = !!neg_opt;
5165                         break;
5166                 case 'D':
5167                         errno = 0;
5168                         param.fp_max_depth = strtol(optarg, 0, 0);
5169                         if (errno != 0 || param.fp_max_depth < 0) {
5170                                 fprintf(stderr,
5171                                         "error: bad maxdepth '%s'\n",
5172                                         optarg);
5173                                 ret = -1;
5174                                 goto err;
5175                         }
5176                         break;
5177                 case 'E':
5178                         if (optarg[0] == '+') {
5179                                 param.fp_comp_end_sign = -1;
5180                                 optarg++;
5181                         } else if (optarg[0] == '-') {
5182                                 param.fp_comp_end_sign =  1;
5183                                 optarg++;
5184                         }
5185
5186                         if (arg_is_eof(optarg)) {
5187                                 param.fp_comp_end = LUSTRE_EOF;
5188                                 param.fp_comp_end_units = 1;
5189                                 rc = 0;
5190                         } else {
5191                                 rc = llapi_parse_size(optarg,
5192                                                 &param.fp_comp_end,
5193                                                 &param.fp_comp_end_units, 0);
5194                                 /* assume units of KB if too small */
5195                                 if (param.fp_comp_end < 4096)
5196                                         param.fp_comp_end *= 1024;
5197                         }
5198                         if (rc) {
5199                                 fprintf(stderr,
5200                                         "error: bad component end '%s'\n",
5201                                         optarg);
5202                                 goto err;
5203                         }
5204                         param.fp_check_comp_end = 1;
5205                         param.fp_exclude_comp_end = !!neg_opt;
5206                         break;
5207                 case LFS_LAYOUT_FOREIGN_OPT: {
5208                         /* all types by default */
5209                         uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5210
5211                         if (optarg) {
5212                                 /* check pure numeric */
5213                                 type = strtoul(optarg, &endptr, 0);
5214                                 if (*endptr) {
5215                                         /* check name */
5216                                         type = check_foreign_type_name(optarg);
5217                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5218                                                 fprintf(stderr,
5219                                                         "%s %s: unknown foreign type '%s'\n",
5220                                                         progname, argv[0],
5221                                                         optarg);
5222                                                 return CMD_HELP;
5223                                         }
5224                                 } else if (type >= UINT32_MAX) {
5225                                         fprintf(stderr,
5226                                                 "%s %s: invalid foreign type '%s'\n",
5227                                                 progname, argv[0], optarg);
5228                                         return CMD_HELP;
5229                                 }
5230                         }
5231                         param.fp_foreign_type = type;
5232                         param.fp_check_foreign = 1;
5233                         param.fp_exclude_foreign = !!neg_opt;
5234                         break;
5235                 }
5236                 case LFS_NEWERXY_OPT: {
5237                         char x = 'm';
5238                         char y = 'm';
5239                         int xidx;
5240                         int negidx;
5241                         time_t *newery;
5242                         time_t ref = time(NULL);
5243
5244                         /* no need to check bad options, they won't get here */
5245                         if (strlen(long_opts[optidx].name) == 7) {
5246                                 x = long_opts[optidx].name[5];
5247                                 y = long_opts[optidx].name[6];
5248                         }
5249
5250                         if (y == 't') {
5251                                 static const char *const fmts[] = {
5252                                         "%Y-%m-%d %H:%M:%S",
5253                                         "%Y-%m-%d %H:%M",
5254                                         "%Y-%m-%d",
5255                                         "%H:%M:%S", /* sometime today */
5256                                         "%H:%M",
5257                                         "@%s",
5258                                         "%s",
5259                                         NULL };
5260                                 struct tm tm;
5261                                 bool found = false;
5262                                 int i;
5263
5264                                 for (i = 0; fmts[i] != NULL; i++) {
5265                                         char *ptr;
5266
5267                                         /* Init for times relative to today */
5268                                         if (strncmp(fmts[i], "%H", 2) == 0)
5269                                                 localtime_r(&ref, &tm);
5270                                         else
5271                                                 memset(&tm, 0, sizeof(tm));
5272                                         ptr = strptime(optarg, fmts[i], &tm);
5273                                         /* Skip spaces */
5274                                         while (ptr && isspace(*ptr))
5275                                                 ptr++;
5276                                         if (ptr == optarg + strlen(optarg)) {
5277                                                 found = true;
5278                                                 break;
5279                                         }
5280                                 }
5281
5282                                 if (!found) {
5283                                         fprintf(stderr,
5284                                                 "%s: invalid time '%s'\n",
5285                                                 progname, optarg);
5286                                         fprintf(stderr,
5287                                                 "supported formats are:\n  ");
5288                                         for (i = 0; fmts[i] != NULL; i++)
5289                                                 fprintf(stderr, "'%s', ",
5290                                                         fmts[i]);
5291                                         fprintf(stderr, "\n");
5292                                         ret = -EINVAL;
5293                                         goto err;
5294                                 }
5295
5296                                 ref = mktime(&tm);
5297                         } else if (y == 'b' || y == 'B') {
5298                                 lstatx_t stx;
5299
5300                                 rc = llapi_get_lum_file(optarg, NULL, &stx,
5301                                                         NULL, 0);
5302                                 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5303                                         if (!(stx.stx_mask & STATX_BTIME))
5304                                                 ret = -EOPNOTSUPP;
5305                                         else
5306                                                 ret = -errno;
5307                                         fprintf(stderr,
5308                                                 "%s: get btime failed '%s': %s\n",
5309                                                 progname, optarg,
5310                                                 strerror(-ret));
5311                                         goto err;
5312                                 }
5313
5314                                 ref = stx.stx_btime.tv_sec;
5315                         } else {
5316                                 struct stat statbuf;
5317
5318                                 if (stat(optarg, &statbuf) < 0) {
5319                                         fprintf(stderr,
5320                                                 "%s: cannot stat file '%s': %s\n",
5321                                                 progname, optarg,
5322                                                 strerror(errno));
5323                                         ret = -errno;
5324                                         goto err;
5325                                 }
5326
5327                                 switch (y) {
5328                                 case 'a':
5329                                         ref = statbuf.st_atime;
5330                                         break;
5331                                 case 'm':
5332                                         ref = statbuf.st_mtime;
5333                                         break;
5334                                 case 'c':
5335                                         ref = statbuf.st_ctime;
5336                                         break;
5337                                 default:
5338                                         fprintf(stderr,
5339                                                 "%s: invalid Y argument: '%c'\n",
5340                                                 progname, x);
5341                                         ret = -EINVAL;
5342                                         goto err;
5343                                 }
5344                         }
5345
5346                         switch (x) {
5347                         case 'a':
5348                                 xidx = NEWERXY_ATIME;
5349                                 break;
5350                         case 'm':
5351                                 xidx = NEWERXY_MTIME;
5352                                 break;
5353                         case 'c':
5354                                 xidx = NEWERXY_CTIME;
5355                                 break;
5356                         case 'b':
5357                         case 'B':
5358                                 xidx = NEWERXY_BTIME;
5359                                 break;
5360                         default:
5361                                 fprintf(stderr,
5362                                         "%s: invalid X argument: '%c'\n",
5363                                         progname, x);
5364                                 ret = -EINVAL;
5365                                 goto err;
5366                         }
5367
5368                         negidx = !!neg_opt;
5369                         newery = &param.fp_newery[xidx][negidx];
5370
5371                         if (*newery == 0) {
5372                                 *newery = ref;
5373                         } else {
5374                                 if (negidx)
5375                                         *newery = *newery > ref ? ref : *newery;
5376                                 else
5377                                         *newery = *newery > ref ? *newery : ref;
5378                         }
5379                         param.fp_newerxy = 1;
5380                         break;
5381                 }
5382                 case 'g':
5383                 case 'G':
5384                         rc = name2gid(&param.fp_gid, optarg);
5385                         if (rc) {
5386                                 if (str2quotaid(&param.fp_gid, optarg)) {
5387                                         fprintf(stderr,
5388                                                 "Group/GID: %s cannot be found.\n",
5389                                                 optarg);
5390                                         ret = -1;
5391                                         goto err;
5392                                 }
5393                         }
5394                         param.fp_exclude_gid = !!neg_opt;
5395                         param.fp_check_gid = 1;
5396                         break;
5397                 case 'H':
5398                         rc = mdthash_input(optarg, &param.fp_hash_inflags,
5399                                            &param.fp_hash_exflags,
5400                                            &param.fp_hash_type);
5401                         if (rc) {
5402                                 ret = -1;
5403                                 goto err;
5404                         }
5405                         if (param.fp_hash_inflags || param.fp_hash_exflags)
5406                                 param.fp_check_hash_flag = 1;
5407                         param.fp_exclude_hash_type = !!neg_opt;
5408                         break;
5409                 case 'l':
5410                         param.fp_lazy = 1;
5411                         break;
5412                 case 'L':
5413                         ret = name2layout(&param.fp_layout, optarg);
5414                         if (ret)
5415                                 goto err;
5416                         param.fp_exclude_layout = !!neg_opt;
5417                         param.fp_check_layout = 1;
5418                         break;
5419                 case 'u':
5420                 case 'U':
5421                         rc = name2uid(&param.fp_uid, optarg);
5422                         if (rc) {
5423                                 if (str2quotaid(&param.fp_uid, optarg)) {
5424                                         fprintf(stderr,
5425                                                 "User/UID: %s cannot be found.\n",
5426                                                 optarg);
5427                                         ret = -1;
5428                                         goto err;
5429                                 }
5430                         }
5431                         param.fp_exclude_uid = !!neg_opt;
5432                         param.fp_check_uid = 1;
5433                         break;
5434                 case 'n':
5435                         param.fp_pattern = (char *)optarg;
5436                         param.fp_exclude_pattern = !!neg_opt;
5437                         break;
5438                 case 'N':
5439                         if (optarg[0] == '+') {
5440                                 param.fp_mirror_count_sign = -1;
5441                                 optarg++;
5442                         } else if (optarg[0] == '-') {
5443                                 param.fp_mirror_count_sign =  1;
5444                                 optarg++;
5445                         }
5446
5447                         errno = 0;
5448                         param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5449                         if (errno != 0 || *endptr != '\0' ||
5450                             param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5451                                 fprintf(stderr,
5452                                         "error: bad mirror count '%s'\n",
5453                                         optarg);
5454                                 goto err;
5455                         }
5456                         param.fp_check_mirror_count = 1;
5457                         param.fp_exclude_mirror_count = !!neg_opt;
5458                         break;
5459                 case 'm':
5460                 case 'i':
5461                 case 'O': {
5462                         char *buf, *token, *next, *p;
5463                         int len = 1;
5464                         void *tmp;
5465
5466                         buf = strdup(optarg);
5467                         if (!buf) {
5468                                 ret = -ENOMEM;
5469                                 goto err;
5470                         }
5471
5472                         param.fp_exclude_obd = !!neg_opt;
5473
5474                         token = buf;
5475                         while (token && *token) {
5476                                 token = strchr(token, ',');
5477                                 if (token) {
5478                                         len++;
5479                                         token++;
5480                                 }
5481                         }
5482                         if (c == 'm') {
5483                                 param.fp_exclude_mdt = !!neg_opt;
5484                                 param.fp_num_alloc_mdts += len;
5485                                 tmp = realloc(param.fp_mdt_uuid,
5486                                               param.fp_num_alloc_mdts *
5487                                               sizeof(*param.fp_mdt_uuid));
5488                                 if (!tmp) {
5489                                         ret = -ENOMEM;
5490                                         goto err_free;
5491                                 }
5492
5493                                 param.fp_mdt_uuid = tmp;
5494                         } else {
5495                                 param.fp_exclude_obd = !!neg_opt;
5496                                 param.fp_num_alloc_obds += len;
5497                                 tmp = realloc(param.fp_obd_uuid,
5498                                               param.fp_num_alloc_obds *
5499                                               sizeof(*param.fp_obd_uuid));
5500                                 if (!tmp) {
5501                                         ret = -ENOMEM;
5502                                         goto err_free;
5503                                 }
5504
5505                                 param.fp_obd_uuid = tmp;
5506                         }
5507                         for (token = buf; token && *token; token = next) {
5508                                 struct obd_uuid *puuid;
5509
5510                                 if (c == 'm') {
5511                                         puuid =
5512                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
5513                                 } else {
5514                                         puuid =
5515                                         &param.fp_obd_uuid[param.fp_num_obds++];
5516                                 }
5517                                 p = strchr(token, ',');
5518                                 next = 0;
5519                                 if (p) {
5520                                         *p = 0;
5521                                         next = p+1;
5522                                 }
5523
5524                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5525                                         ret = -E2BIG;
5526                                         goto err_free;
5527                                 }
5528
5529                                 strncpy(puuid->uuid, token,
5530                                         sizeof(puuid->uuid));
5531                         }
5532 err_free:
5533                         if (buf)
5534                                 free(buf);
5535                         break;
5536                 }
5537 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5538                 case 'p':
5539 #endif
5540                 case LFS_POOL_OPT:
5541                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
5542                                 fprintf(stderr,
5543                                         "Pool name %s is too long (max %d)\n",
5544                                         optarg, LOV_MAXPOOLNAME);
5545                                 ret = -1;
5546                                 goto err;
5547                         }
5548                         /*
5549                          * We do check for empty pool because empty pool
5550                          * is used to find V1 LOV attributes
5551                          */
5552                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5553                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5554                         param.fp_exclude_pool = !!neg_opt;
5555                         param.fp_check_pool = 1;
5556                         break;
5557 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
5558                 case 'p': /* want this for --pool, to match getstripe/find */
5559                         fprintf(stderr,
5560                                 "warning: -p deprecated, use --print0 or -0\n");
5561 #endif
5562                 case '0':
5563                         param.fp_zero_end = 1;
5564                         break;
5565                 case 'P': /* we always print, this option is a no-op */
5566                         break;
5567                 case LFS_PROJID_OPT:
5568                         rc = name2projid(&param.fp_projid, optarg);
5569                         if (rc) {
5570                                 if (str2quotaid(&param.fp_projid, optarg)) {
5571                                         fprintf(stderr,
5572                                                 "Invalid project ID: %s\n",
5573                                                 optarg);
5574                                         ret = -1;
5575                                         goto err;
5576                                 }
5577                         }
5578                         param.fp_exclude_projid = !!neg_opt;
5579                         param.fp_check_projid = 1;
5580                         break;
5581                 case 's':
5582                         if (optarg[0] == '+') {
5583                                 param.fp_size_sign = -1;
5584                                 optarg++;
5585                         } else if (optarg[0] == '-') {
5586                                 param.fp_size_sign =  1;
5587                                 optarg++;
5588                         }
5589
5590                         ret = llapi_parse_size(optarg, &param.fp_size,
5591                                                &param.fp_size_units, 0);
5592                         if (ret) {
5593                                 fprintf(stderr, "error: bad file size '%s'\n",
5594                                         optarg);
5595                                 goto err;
5596                         }
5597                         param.fp_check_size = 1;
5598                         param.fp_exclude_size = !!neg_opt;
5599                         break;
5600                 case 'S':
5601                         if (optarg[0] == '+') {
5602                                 param.fp_stripe_size_sign = -1;
5603                                 optarg++;
5604                         } else if (optarg[0] == '-') {
5605                                 param.fp_stripe_size_sign =  1;
5606                                 optarg++;
5607                         }
5608
5609                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
5610                                                &param.fp_stripe_size_units, 0);
5611                         /* assume units of KB if too small to be valid */
5612                         if (param.fp_stripe_size < 4096)
5613                                 param.fp_stripe_size *= 1024;
5614                         if (ret) {
5615                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
5616                                         optarg);
5617                                 goto err;
5618                         }
5619                         param.fp_check_stripe_size = 1;
5620                         param.fp_exclude_stripe_size = !!neg_opt;
5621                         break;
5622                 case 't':
5623                         param.fp_exclude_type = !!neg_opt;
5624                         switch (optarg[0]) {
5625                         case 'b':
5626                                 param.fp_type = S_IFBLK;
5627                                 break;
5628                         case 'c':
5629                                 param.fp_type = S_IFCHR;
5630                                 break;
5631                         case 'd':
5632                                 param.fp_type = S_IFDIR;
5633                                 break;
5634                         case 'f':
5635                                 param.fp_type = S_IFREG;
5636                                 break;
5637                         case 'l':
5638                                 param.fp_type = S_IFLNK;
5639                                 break;
5640                         case 'p':
5641                                 param.fp_type = S_IFIFO;
5642                                 break;
5643                         case 's':
5644                                 param.fp_type = S_IFSOCK;
5645                                 break;
5646                         default:
5647                                 fprintf(stderr, "error: %s: bad type '%s'\n",
5648                                         argv[0], optarg);
5649                                 ret = CMD_HELP;
5650                                 goto err;
5651                         };
5652                         break;
5653                 case LFS_FIND_PERM:
5654                         param.fp_exclude_perm = !!neg_opt;
5655                         param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5656                         if (*optarg == '/') {
5657                                 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5658                                 optarg++;
5659                         } else if (*optarg == '-') {
5660                                 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5661                                 optarg++;
5662                         }
5663
5664                         if (str2mode_t(optarg, &param.fp_perm)) {
5665                                 fprintf(stderr, "error: invalid mode '%s'\n",
5666                                         optarg);
5667                                 ret = -1;
5668                                 goto err;
5669                         }
5670                         break;
5671                 case 'T':
5672                         if (optarg[0] == '+') {
5673                                 param.fp_mdt_count_sign = -1;
5674                                 optarg++;
5675                         } else if (optarg[0] == '-') {
5676                                 param.fp_mdt_count_sign =  1;
5677                                 optarg++;
5678                         }
5679
5680                         errno = 0;
5681                         param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5682                         if (errno != 0 || *endptr != '\0' ||
5683                             param.fp_mdt_count >= UINT32_MAX) {
5684                                 fprintf(stderr, "error: bad mdt_count '%s'\n",
5685                                         optarg);
5686                                 ret = -1;
5687                                 goto err;
5688                         }
5689                         param.fp_check_mdt_count = 1;
5690                         param.fp_exclude_mdt_count = !!neg_opt;
5691                         break;
5692                 case 'z':
5693                         if (optarg[0] == '+') {
5694                                 param.fp_ext_size_sign = -1;
5695                                 optarg++;
5696                         } else if (optarg[0] == '-') {
5697                                 param.fp_ext_size_sign =  1;
5698                                 optarg++;
5699                         }
5700
5701                         ret = llapi_parse_size(optarg, &param.fp_ext_size,
5702                                                &param.fp_ext_size_units, 0);
5703                         if (ret) {
5704                                 fprintf(stderr, "error: bad ext-size '%s'\n",
5705                                         optarg);
5706                                 goto err;
5707                         }
5708                         param.fp_ext_size /= SEL_UNIT_SIZE;
5709                         param.fp_ext_size_units /= SEL_UNIT_SIZE;
5710                         param.fp_check_ext_size = 1;
5711                         param.fp_exclude_ext_size = !!neg_opt;
5712                         break;
5713                 default:
5714                         ret = CMD_HELP;
5715                         goto err;
5716                 };
5717         }
5718
5719         if (pathstart == -1) {
5720                 fprintf(stderr, "error: %s: no filename|pathname\n",
5721                         argv[0]);
5722                 ret = CMD_HELP;
5723                 goto err;
5724         } else if (pathend == -1) {
5725                 /* no options */
5726                 pathend = argc;
5727         }
5728
5729         do {
5730                 rc = llapi_find(argv[pathstart], &param);
5731                 if (rc && !ret) {
5732                         ret = rc;
5733                         pathbad = pathstart;
5734                 }
5735         } while (++pathstart < pathend);
5736
5737         if (ret)
5738                 fprintf(stderr, "%s: failed for '%s': %s\n",
5739                         progname, argv[pathbad], strerror(-rc));
5740
5741 err:
5742         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5743                 free(param.fp_obd_uuid);
5744
5745         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5746                 free(param.fp_mdt_uuid);
5747
5748         return ret;
5749 }
5750
5751 static int lfs_getstripe_internal(int argc, char **argv,
5752                                   struct find_param *param)
5753 {
5754         struct option long_opts[] = {
5755 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
5756 /* find { .val = 'b',   .name = "blocks",       .has_arg = required_argument }*/
5757 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
5758 /* find { .val = 'B',   .name = "Btime",        .has_arg = required_argument }*/
5759         { .val = LFS_COMP_COUNT_OPT,
5760                         .name = "comp-count",   .has_arg = no_argument },
5761         { .val = LFS_COMP_COUNT_OPT,
5762                 .name = "component-count",      .has_arg = no_argument },
5763         { .val = LFS_COMP_FLAGS_OPT,
5764                         .name = "comp-flags",   .has_arg = optional_argument },
5765         { .val = LFS_COMP_FLAGS_OPT,
5766                 .name = "component-flags",      .has_arg = optional_argument },
5767         { .val = LFS_COMP_START_OPT,
5768                         .name = "comp-start",   .has_arg = optional_argument },
5769         { .val = LFS_COMP_START_OPT,
5770                 .name = "component-start",      .has_arg = optional_argument },
5771         { .val = LFS_MIRROR_INDEX_OPT,
5772                 .name = "mirror-index",         .has_arg = required_argument },
5773         { .val = LFS_MIRROR_ID_OPT,
5774                 .name = "mirror-id",            .has_arg = required_argument },
5775         { .val = 'c',   .name = "stripe-count", .has_arg = no_argument },
5776         { .val = 'c',   .name = "stripe_count", .has_arg = no_argument },
5777 /* find { .val = 'C',   .name = "ctime",        .has_arg = required_argument }*/
5778         { .val = 'd',   .name = "directory",    .has_arg = no_argument },
5779         { .val = 'D',   .name = "default",      .has_arg = no_argument },
5780         { .val = 'E',   .name = "comp-end",     .has_arg = optional_argument },
5781         { .val = 'E',   .name = "component-end", .has_arg = optional_argument },
5782         { .val = 'F',   .name = "fid",          .has_arg = no_argument },
5783         { .val = 'g',   .name = "generation",   .has_arg = no_argument },
5784 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
5785 /* dirstripe { .val = 'H', .name = "mdt-hash",  .has_arg = required_argument }*/
5786         { .val = 'i',   .name = "stripe-index", .has_arg = no_argument },
5787         { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
5788         { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
5789         { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
5790 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
5791         { .val = 'L',   .name = "layout",       .has_arg = no_argument },
5792         { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
5793         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
5794         { .val = 'm',   .name = "mdt_index",    .has_arg = no_argument },
5795 /* find { .val = 'M',   .name = "mtime",        .has_arg = required_argument }*/
5796 /* find { .val = 'n',   .name = "name",         .has_arg = required_argument }*/
5797         { .val = 'N',   .name = "mirror-count", .has_arg = no_argument },
5798         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
5799         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
5800         { .val = 'p',   .name = "pool",         .has_arg = no_argument },
5801 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
5802         { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
5803         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
5804         { .val = 'R',   .name = "raw",          .has_arg = no_argument },
5805         { .val = 'S',   .name = "stripe-size",  .has_arg = no_argument },
5806         { .val = 'S',   .name = "stripe_size",  .has_arg = no_argument },
5807 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
5808 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
5809 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
5810 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
5811         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
5812 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
5813         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
5814         { .val = 'z',   .name = "extension-size", .has_arg = no_argument },
5815         { .val = 'z',   .name = "ext-size",     .has_arg = no_argument },
5816         { .name = NULL } };
5817         int c, rc;
5818         int neg_opt = 0;
5819         int pathstart = -1, pathend = -1;
5820         int isoption;
5821         char *end, *tmp;
5822
5823         while ((c = getopt_long(argc, argv,
5824                         "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
5825                         long_opts, NULL)) != -1) {
5826                 if (neg_opt)
5827                         --neg_opt;
5828
5829                 /* '!' is part of option */
5830                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5831                 if (!isoption && pathend != -1) {
5832                         fprintf(stderr,
5833                                 "error: %s: filename|dirname must either precede options or follow options\n",
5834                                 argv[0]);
5835                         return CMD_HELP;
5836                 }
5837                 if (!isoption && pathstart == -1)
5838                         pathstart = optind - 1;
5839                 if (isoption && pathstart != -1 && pathend == -1)
5840                         pathend = optind - 2;
5841
5842                 switch (c) {
5843                 case 1:
5844                         /* unknown: opt is "!" */
5845                         if (strcmp(optarg, "!") == 0)
5846                                 neg_opt = 2;
5847                         break;
5848                 case 'c':
5849                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
5850                                 param->fp_verbose |= VERBOSE_COUNT;
5851                                 param->fp_max_depth = 0;
5852                         }
5853                         break;
5854                 case LFS_COMP_COUNT_OPT:
5855                         param->fp_verbose |= VERBOSE_COMP_COUNT;
5856                         param->fp_max_depth = 0;
5857                         break;
5858                 case LFS_COMP_FLAGS_OPT:
5859                         if (optarg) {
5860                                 rc = comp_str2flags(optarg,
5861                                                     &param->fp_comp_flags,
5862                                                     &param->fp_comp_neg_flags);
5863                                 if (rc != 0) {
5864                                         fprintf(stderr,
5865                                                 "error: %s bad component flags '%s'.\n",
5866                                                 argv[0], optarg);
5867                                         return CMD_HELP;
5868                                 }
5869                                 param->fp_check_comp_flags = 1;
5870                         } else {
5871                                 param->fp_verbose |= VERBOSE_COMP_FLAGS;
5872                                 param->fp_max_depth = 0;
5873                         }
5874                         break;
5875                 case LFS_COMP_START_OPT:
5876                         if (optarg) {
5877                                 tmp = optarg;
5878                                 if (tmp[0] == '+') {
5879                                         param->fp_comp_start_sign = -1;
5880                                         tmp++;
5881                                 } else if (tmp[0] == '-') {
5882                                         param->fp_comp_start_sign = 1;
5883                                         tmp++;
5884                                 }
5885                                 rc = llapi_parse_size(tmp,
5886                                                 &param->fp_comp_start,
5887                                                 &param->fp_comp_start_units, 0);
5888                                 if (rc != 0) {
5889                                         fprintf(stderr,
5890                                                 "error: %s bad component start '%s'.\n",
5891                                                 argv[0], tmp);
5892                                         return CMD_HELP;
5893                                 }
5894                                 param->fp_check_comp_start = 1;
5895                         } else {
5896                                 param->fp_verbose |= VERBOSE_COMP_START;
5897                                 param->fp_max_depth = 0;
5898                         }
5899                         break;
5900                 case LFS_MIRROR_INDEX_OPT: {
5901                         unsigned long int mirror_index;
5902
5903                         if (optarg[0] == '+') {
5904                                 param->fp_mirror_index_sign = -1;
5905                                 optarg++;
5906                         } else if (optarg[0] == '-') {
5907                                 param->fp_mirror_index_sign = 1;
5908                                 optarg++;
5909                         }
5910
5911                         errno = 0;
5912                         mirror_index = strtoul(optarg, &end, 0);
5913                         if (errno != 0 || *end != '\0' ||
5914                             mirror_index > UINT16_MAX || (mirror_index == 0 &&
5915                             param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
5916                                 fprintf(stderr,
5917                                         "%s %s: invalid mirror index '%s'\n",
5918                                         progname, argv[0], optarg);
5919                                 return CMD_HELP;
5920                         }
5921
5922                         param->fp_mirror_index = (__u16)mirror_index;
5923
5924                         if (param->fp_mirror_id != 0) {
5925                                 fprintf(stderr,
5926                                         "%s %s: can't specify both mirror index and mirror ID\n",
5927                                         progname, argv[0]);
5928                                 return CMD_HELP;
5929                         }
5930                         param->fp_check_mirror_index = 1;
5931                         param->fp_exclude_mirror_index = !!neg_opt;
5932                         break;
5933                 }
5934                 case LFS_MIRROR_ID_OPT: {
5935                         unsigned long int mirror_id;
5936
5937                         if (optarg[0] == '+') {
5938                                 param->fp_mirror_id_sign = -1;
5939                                 optarg++;
5940                         } else if (optarg[0] == '-') {
5941                                 param->fp_mirror_id_sign = 1;
5942                                 optarg++;
5943                         }
5944
5945                         errno = 0;
5946                         mirror_id = strtoul(optarg, &end, 0);
5947                         if (errno != 0 || *end != '\0' ||
5948                             mirror_id > UINT16_MAX || (mirror_id == 0 &&
5949                             param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
5950                                 fprintf(stderr,
5951                                         "%s %s: invalid mirror ID '%s'\n",
5952                                         progname, argv[0], optarg);
5953                                 return CMD_HELP;
5954                         }
5955
5956                         param->fp_mirror_id = (__u16)mirror_id;
5957
5958                         if (param->fp_mirror_index != 0) {
5959                                 fprintf(stderr,
5960                                         "%s %s: can't specify both mirror index and mirror ID\n",
5961                                         progname, argv[0]);
5962                                 return CMD_HELP;
5963                         }
5964                         param->fp_check_mirror_id = 1;
5965                         param->fp_exclude_mirror_id = !!neg_opt;
5966                         break;
5967                 }
5968                 case 'd':
5969                         param->fp_max_depth = 0;
5970                         break;
5971                 case 'D':
5972                         param->fp_get_default_lmv = 1;
5973                         break;
5974                 case 'E':
5975                         if (optarg) {
5976                                 tmp = optarg;
5977                                 if (tmp[0] == '+') {
5978                                         param->fp_comp_end_sign = -1;
5979                                         tmp++;
5980                                 } else if (tmp[0] == '-') {
5981                                         param->fp_comp_end_sign = 1;
5982                                         tmp++;
5983                                 }
5984
5985                                 if (arg_is_eof(tmp)) {
5986                                         param->fp_comp_end = LUSTRE_EOF;
5987                                         param->fp_comp_end_units = 1;
5988                                         rc = 0;
5989                                 } else {
5990                                         rc = llapi_parse_size(tmp,
5991                                                 &param->fp_comp_end,
5992                                                 &param->fp_comp_end_units, 0);
5993                                         /* assume units of KB if too small */
5994                                         if (param->fp_comp_end < 4096)
5995                                                 param->fp_comp_end *= 1024;
5996                                 }
5997                                 if (rc != 0) {
5998                                         fprintf(stderr,
5999                                                 "error: %s bad component end '%s'.\n",
6000                                                 argv[0], tmp);
6001                                         return CMD_HELP;
6002                                 }
6003                                 param->fp_check_comp_end = 1;
6004                         } else {
6005                                 param->fp_verbose |= VERBOSE_COMP_END;
6006                                 param->fp_max_depth = 0;
6007                         }
6008                         break;
6009                 case 'F':
6010                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6011                                 param->fp_verbose |= VERBOSE_DFID;
6012                                 param->fp_max_depth = 0;
6013                         }
6014                         break;
6015                 case 'g':
6016                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6017                                 param->fp_verbose |= VERBOSE_GENERATION;
6018                                 param->fp_max_depth = 0;
6019                         }
6020                         break;
6021                 case 'i':
6022                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6023                                 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6024                                 param->fp_max_depth = 0;
6025                         }
6026                         break;
6027                 case 'I':
6028                         if (optarg) {
6029                                 param->fp_comp_id = strtoul(optarg, &end, 0);
6030                                 if (*end != '\0' || param->fp_comp_id == 0 ||
6031                                     param->fp_comp_id > LCME_ID_MAX) {
6032                                         fprintf(stderr,
6033                                                 "error: %s bad component id '%s'\n",
6034                                                 argv[0], optarg);
6035                                         return CMD_HELP;
6036                                 }
6037                                 param->fp_check_comp_id = 1;
6038                         } else {
6039                                 param->fp_max_depth = 0;
6040                                 param->fp_verbose |= VERBOSE_COMP_ID;
6041                         }
6042                         break;
6043                 case 'L':
6044                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6045                                 param->fp_verbose |= VERBOSE_PATTERN;
6046                                 param->fp_max_depth = 0;
6047                         }
6048                         break;
6049 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6050                 case 'M':
6051                         fprintf(stderr,
6052                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6053 #endif
6054                 case 'm':
6055                         if (!(param->fp_verbose & VERBOSE_DETAIL))
6056                                 param->fp_max_depth = 0;
6057                         param->fp_verbose |= VERBOSE_MDTINDEX;
6058                         break;
6059                 case 'N':
6060                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6061                                 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6062                                 param->fp_max_depth = 0;
6063                         }
6064                         break;
6065                 case 'O':
6066                         if (param->fp_obd_uuid) {
6067                                 fprintf(stderr,
6068                                         "error: %s: only one obduuid allowed",
6069                                         argv[0]);
6070                                 return CMD_HELP;
6071                         }
6072                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
6073                         break;
6074                 case 'p':
6075                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6076                                 param->fp_verbose |= VERBOSE_POOL;
6077                                 param->fp_max_depth = 0;
6078                         }
6079                         break;
6080                 case 'q':
6081                         param->fp_quiet++;
6082                         break;
6083                 case 'r':
6084                         param->fp_recursive = 1;
6085                         break;
6086                 case 'R':
6087                         param->fp_raw = 1;
6088                         break;
6089                 case 'S':
6090                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6091                                 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6092                                 param->fp_max_depth = 0;
6093                         }
6094                         break;
6095                 case 'v':
6096                         param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6097                         break;
6098                 case 'y':
6099                         param->fp_yaml = 1;
6100                         break;
6101                 case 'z':
6102                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6103                                 param->fp_verbose |= VERBOSE_EXT_SIZE;
6104                                 param->fp_max_depth = 0;
6105                         }
6106                         break;
6107                 default:
6108                         return CMD_HELP;
6109                 }
6110         }
6111
6112         if (pathstart == -1) {
6113                 fprintf(stderr, "error: %s: no filename|pathname\n",
6114                                 argv[0]);
6115                 return CMD_HELP;
6116         } else if (pathend == -1) {
6117                 /* no options */
6118                 pathend = argc;
6119         }
6120
6121         if (pathend > argc)
6122                 return CMD_HELP;
6123
6124         if (param->fp_recursive)
6125                 param->fp_max_depth = -1;
6126         else if (param->fp_verbose & VERBOSE_DETAIL)
6127                 param->fp_max_depth = 1;
6128
6129         if (!param->fp_verbose)
6130                 param->fp_verbose = VERBOSE_DEFAULT;
6131         if (param->fp_quiet)
6132                 param->fp_verbose = VERBOSE_OBJID;
6133
6134         do {
6135                 rc = llapi_getstripe(argv[pathstart], param);
6136         } while (++pathstart < pathend && !rc);
6137
6138         if (rc)
6139                 fprintf(stderr, "error: %s failed for %s.\n",
6140                         argv[0], argv[optind - 1]);
6141         return rc;
6142 }
6143
6144 static int lfs_tgts(int argc, char **argv)
6145 {
6146         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6147         struct find_param param;
6148         int index = 0, rc = 0;
6149
6150         if (argc > 2)
6151                 return CMD_HELP;
6152
6153         if (argc == 2 && !realpath(argv[1], path)) {
6154                 rc = -errno;
6155                 fprintf(stderr, "error: invalid path '%s': %s\n",
6156                         argv[1], strerror(-rc));
6157                 return rc;
6158         }
6159
6160         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6161                 /* Check if we have a mount point */
6162                 if (mntdir[0] == '\0')
6163                         continue;
6164
6165                 memset(&param, 0, sizeof(param));
6166                 if (!strcmp(argv[0], "mdts"))
6167                         param.fp_get_lmv = 1;
6168
6169                 rc = llapi_ostlist(mntdir, &param);
6170                 if (rc) {
6171                         fprintf(stderr, "error: %s: failed on %s\n",
6172                                 argv[0], mntdir);
6173                 }
6174                 if (path[0] != '\0')
6175                         break;
6176                 memset(mntdir, 0, PATH_MAX);
6177         }
6178
6179         return rc;
6180 }
6181
6182 static int lfs_getstripe(int argc, char **argv)
6183 {
6184         struct find_param param = { 0 };
6185
6186         param.fp_max_depth = 1;
6187         return lfs_getstripe_internal(argc, argv, &param);
6188 }
6189
6190 /* functions */
6191 static int lfs_getdirstripe(int argc, char **argv)
6192 {
6193         struct find_param param = { 0 };
6194         struct option long_opts[] = {
6195         { .val = 'c',   .name = "mdt-count",     .has_arg = no_argument },
6196         { .val = 'D',   .name = "default",       .has_arg = no_argument },
6197         { .val = 'H',   .name = "mdt-hash",      .has_arg = no_argument },
6198         { .val = 'i',   .name = "mdt-index",     .has_arg = no_argument },
6199         { .val = 'm',   .name = "mdt-index",     .has_arg = no_argument },
6200         { .val = 'O',   .name = "obd",           .has_arg = required_argument },
6201         { .val = 'r',   .name = "recursive",     .has_arg = no_argument },
6202         { .val = 'T',   .name = "mdt-count",     .has_arg = no_argument },
6203         { .val = 'v',   .name = "verbose",       .has_arg = no_argument },
6204         { .val = 'X',   .name = "max-inherit",   .has_arg = no_argument },
6205         { .val = 'y',   .name = "yaml",          .has_arg = no_argument },
6206         { .val = LFS_INHERIT_RR_OPT,
6207                         .name = "max-inherit-rr", .has_arg = no_argument },
6208         { .name = NULL } };
6209         int c, rc;
6210
6211         param.fp_get_lmv = 1;
6212
6213         while ((c = getopt_long(argc, argv,
6214                                 "cDHimO:rtTvXy", long_opts, NULL)) != -1) {
6215                 switch (c) {
6216                 case 'c':
6217                 case 'T':
6218                         param.fp_verbose |= VERBOSE_COUNT;
6219                         break;
6220                 case 'D':
6221                         param.fp_get_default_lmv = 1;
6222                         break;
6223                 case 'i':
6224                 case 'm':
6225                         param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6226                         break;
6227 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6228                 case 't':
6229                         fprintf(stderr,
6230                                 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6231 #endif
6232                 case 'H':
6233                         param.fp_verbose |= VERBOSE_HASH_TYPE;
6234                         break;
6235                 case 'O':
6236                         if (param.fp_obd_uuid) {
6237                                 fprintf(stderr,
6238                                         "error: %s: only one obduuid allowed",
6239                                         argv[0]);
6240                                 return CMD_HELP;
6241                         }
6242                         param.fp_obd_uuid = (struct obd_uuid *)optarg;
6243                         break;
6244                 case 'r':
6245                         param.fp_recursive = 1;
6246                         break;
6247                 case 'v':
6248                         param.fp_verbose |= VERBOSE_DETAIL;
6249                         break;
6250                 case 'X':
6251                         param.fp_verbose |= VERBOSE_INHERIT;
6252                         break;
6253                 case LFS_INHERIT_RR_OPT:
6254                         param.fp_verbose |= VERBOSE_INHERIT_RR;
6255                         break;
6256                 case 'y':
6257                         param.fp_yaml = 1;
6258                         break;
6259                 default:
6260                         fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6261                                 progname, argv[0], argv[optind - 1]);
6262                         return CMD_HELP;
6263                 }
6264         }
6265
6266         if (optind >= argc)
6267                 return CMD_HELP;
6268
6269         if (param.fp_recursive)
6270                 param.fp_max_depth = -1;
6271
6272         if (!param.fp_verbose)
6273                 param.fp_verbose = VERBOSE_DEFAULT;
6274
6275         do {
6276                 rc = llapi_getstripe(argv[optind], &param);
6277         } while (++optind < argc && !rc);
6278
6279         if (rc)
6280                 fprintf(stderr, "error: %s failed for %s.\n",
6281                         argv[0], argv[optind - 1]);
6282         return rc;
6283 }
6284
6285 enum mntdf_flags {
6286         MNTDF_INODES    = 0x0001,
6287         MNTDF_COOKED    = 0x0002,
6288         MNTDF_LAZY      = 0x0004,
6289         MNTDF_VERBOSE   = 0x0008,
6290         MNTDF_SHOW      = 0x0010,
6291         MNTDF_DECIMAL   = 0x0020,
6292 };
6293
6294 #define COOK(value, base)                                       \
6295 ({                                                              \
6296         int radix = 0;                                          \
6297         while (value > base) {                                  \
6298                 value /= base;                                  \
6299                 radix++;                                        \
6300         }                                                       \
6301         radix;                                                  \
6302 })
6303 #define UUF     "%-20s"
6304 #define CSF     "%11s"
6305 #define CDF     "%11llu"
6306 #define HDF     "%8.1f%c"
6307 #define RSF     "%4s"
6308 #define RDF     "%3d%%"
6309
6310 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6311 {
6312         double avail, used, ratio = 0;
6313
6314         if (inodes) {
6315                 avail = st->os_ffree;
6316                 used = st->os_files - st->os_ffree;
6317         } else {
6318                 avail = st->os_bavail;
6319                 used = st->os_blocks - st->os_bfree;
6320         }
6321         if (avail + used > 0)
6322                 ratio = used / (used + avail) * 100;
6323
6324         /* Round up to match df(1) usage percentage */
6325         return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6326 }
6327
6328 /*
6329  * This is to identify various problem states for "lfs df" if .osn_err = true,
6330  * so only show flags reflecting those states by default. Informational states
6331  * are only shown with "-v" and use lower-case names to distinguish them.
6332  * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6333  */
6334 static struct obd_statfs_state_names {
6335         enum obd_statfs_state   osn_state;
6336         const char              osn_name;
6337         bool                    osn_err;
6338 } oss_names[] = {
6339         { .osn_state = OS_STATFS_DEGRADED,   .osn_name = 'D', .osn_err = true },
6340         { .osn_state = OS_STATFS_READONLY,   .osn_name = 'R', .osn_err = true },
6341         { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6342         { .osn_state = OS_STATFS_UNUSED1,    .osn_name = '?', .osn_err = true },
6343         { .osn_state = OS_STATFS_UNUSED2,    .osn_name = '?', .osn_err = true },
6344         { .osn_state = OS_STATFS_ENOSPC,     .osn_name = 'S', .osn_err = true },
6345         { .osn_state = OS_STATFS_ENOINO,     .osn_name = 'I', .osn_err = true },
6346         { .osn_state = OS_STATFS_SUM,        .osn_name = 'a', /* aggregate */ },
6347         { .osn_state = OS_STATFS_NONROT,     .osn_name = 'f', /* flash */     },
6348 };
6349
6350 static int showdf(char *mntdir, struct obd_statfs *stat,
6351                   char *uuid, enum mntdf_flags flags,
6352                   char *type, int index, int rc)
6353 {
6354         long long avail, used, total;
6355         int ratio = 0;
6356         char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6357         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6358         char tbuf[3 * sizeof(__u64)];
6359         char ubuf[3 * sizeof(__u64)];
6360         char abuf[3 * sizeof(__u64)];
6361         char rbuf[3 * sizeof(__u64)];
6362
6363         if (!uuid || !stat)
6364                 return -EINVAL;
6365
6366         switch (rc) {
6367         case 0:
6368                 if (flags & MNTDF_INODES) {
6369                         avail = stat->os_ffree;
6370                         used = stat->os_files - stat->os_ffree;
6371                         total = stat->os_files;
6372                 } else {
6373                         int shift = flags & MNTDF_COOKED ? 0 : 10;
6374
6375                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
6376                         used  = ((stat->os_blocks - stat->os_bfree) *
6377                                  stat->os_bsize) >> shift;
6378                         total = (stat->os_blocks * stat->os_bsize) >> shift;
6379                 }
6380
6381                 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6382
6383                 if (flags & MNTDF_COOKED) {
6384                         int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6385                         double cook_val;
6386                         int i;
6387
6388                         cook_val = (double)total;
6389                         i = COOK(cook_val, base);
6390                         if (i > 0)
6391                                 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6392                                          suffix[i - 1]);
6393                         else
6394                                 snprintf(tbuf, sizeof(tbuf), CDF, total);
6395
6396                         cook_val = (double)used;
6397                         i = COOK(cook_val, base);
6398                         if (i > 0)
6399                                 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6400                                          suffix[i - 1]);
6401                         else
6402                                 snprintf(ubuf, sizeof(ubuf), CDF, used);
6403
6404                         cook_val = (double)avail;
6405                         i = COOK(cook_val, base);
6406                         if (i > 0)
6407                                 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6408                                          suffix[i - 1]);
6409                         else
6410                                 snprintf(abuf, sizeof(abuf), CDF, avail);
6411                 } else {
6412                         snprintf(tbuf, sizeof(tbuf), CDF, total);
6413                         snprintf(ubuf, sizeof(tbuf), CDF, used);
6414                         snprintf(abuf, sizeof(tbuf), CDF, avail);
6415                 }
6416
6417                 sprintf(rbuf, RDF, ratio);
6418                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6419                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6420                 if (type)
6421                         printf("[%s:%d]", type, index);
6422
6423                 if (stat->os_state) {
6424                         uint32_t i;
6425
6426                         printf(" ");
6427                         for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6428                                 if (oss_names[i].osn_state & stat->os_state &&
6429                                     (oss_names[i].osn_err ||
6430                                      flags & MNTDF_VERBOSE))
6431                                         printf("%c", oss_names[i].osn_name);
6432                         }
6433                 }
6434
6435                 printf("\n");
6436                 break;
6437         case -ENODATA:
6438                 printf(UUF": inactive device\n", uuid);
6439                 break;
6440         default:
6441                 printf(UUF": %s\n", uuid, strerror(-rc));
6442                 break;
6443         }
6444
6445         return 0;
6446 }
6447
6448 struct ll_stat_type {
6449         int   st_op;
6450         char *st_name;
6451 };
6452
6453 #define LL_STATFS_MAX   LOV_MAX_STRIPE_COUNT
6454
6455 struct ll_statfs_data {
6456         int                     sd_index;
6457         struct obd_statfs       sd_st;
6458 };
6459
6460 struct ll_statfs_buf {
6461         int                     sb_count;
6462         struct ll_statfs_data   sb_buf[LL_STATFS_MAX];
6463 };
6464
6465 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6466                  int ops, struct ll_statfs_buf *lsb)
6467 {
6468         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6469         struct obd_uuid uuid_buf;
6470         char *poolname = NULL;
6471         struct ll_stat_type types[] = {
6472                 { .st_op = LL_STATFS_LMV,       .st_name = "MDT" },
6473                 { .st_op = LL_STATFS_LOV,       .st_name = "OST" },
6474                 { .st_name = NULL } };
6475         struct ll_stat_type *tp;
6476         __u64 ost_files = 0;
6477         __u64 ost_ffree = 0;
6478         __u32 index;
6479         __u32 type;
6480         int fd;
6481         int rc = 0;
6482         int rc2;
6483
6484         if (pool) {
6485                 poolname = strchr(pool, '.');
6486                 if (poolname) {
6487                         if (strncmp(fsname, pool, strlen(fsname))) {
6488                                 fprintf(stderr, "filesystem name incorrect\n");
6489                                 return -ENODEV;
6490                         }
6491                         poolname++;
6492                 } else
6493                         poolname = pool;
6494         }
6495
6496         fd = open(mntdir, O_RDONLY);
6497         if (fd < 0) {
6498                 rc = -errno;
6499                 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6500                         strerror(errno));
6501                 return rc;
6502         }
6503
6504         if (flags & MNTDF_SHOW) {
6505                 if (flags & MNTDF_INODES)
6506                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6507                                "UUID", "Inodes", "IUsed", "IFree",
6508                                "IUse%", "Mounted on");
6509                 else
6510                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6511                                "UUID",
6512                                flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6513                                "Used", "Available", "Use%", "Mounted on");
6514         }
6515
6516         for (tp = types; tp->st_name != NULL; tp++) {
6517                 bool have_ost = false;
6518
6519                 if (!(tp->st_op & ops))
6520                         continue;
6521
6522                 for (index = 0; ; index++) {
6523                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
6524                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6525                         type = flags & MNTDF_LAZY ?
6526                                 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6527                         rc2 = llapi_obd_fstatfs(fd, type, index,
6528                                                &stat_buf, &uuid_buf);
6529                         if (rc2 == -ENODEV)
6530                                 break;
6531                         if (rc2 == -EAGAIN)
6532                                 continue;
6533                         if (rc2 == -ENODATA) { /* Inactive device, OK. */
6534                                 if (!(flags & MNTDF_VERBOSE))
6535                                         continue;
6536                         } else if (rc2 < 0 && rc == 0) {
6537                                 rc = rc2;
6538                         }
6539
6540                         /*
6541                          * If we have OSTs then don't report MDT block counts.
6542                          * For MDT-only filesystems the expectation is that all
6543                          * layouts have a DoM component.  For filesystems with
6544                          * OSTs, files are not necessarily going to store data
6545                          * on MDTs, and MDT space is limited to a fraction of
6546                          * OST space, so don't include it in the summary.
6547                          */
6548                         if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6549                                 have_ost = true;
6550                                 sum.os_blocks = 0;
6551                                 sum.os_bfree = 0;
6552                                 sum.os_bavail = 0;
6553                         }
6554
6555                         if (poolname && tp->st_op == LL_STATFS_LOV &&
6556                             llapi_search_ost(fsname, poolname,
6557                                              obd_uuid2str(&uuid_buf)) != 1)
6558                                 continue;
6559
6560                         /*
6561                          * the llapi_obd_fstatfs() call may have returned with
6562                          * an error, but if it filled in uuid_buf we will at
6563                          * lease use that to print out a message for that OBD.
6564                          * If we didn't get anything in the uuid_buf, then fill
6565                          * it in so that we can print an error message.
6566                          */
6567                         if (uuid_buf.uuid[0] == '\0')
6568                                 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6569                                          "%s%04x", tp->st_name, index);
6570                         if (!rc && lsb) {
6571                                 lsb->sb_buf[lsb->sb_count].sd_index = index;
6572                                 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6573                                 lsb->sb_count++;
6574                         }
6575                         if (flags & MNTDF_SHOW)
6576                                 showdf(mntdir, &stat_buf,
6577                                        obd_uuid2str(&uuid_buf), flags,
6578                                        tp->st_name, index, rc2);
6579
6580                         if (rc2)
6581                                 continue;
6582
6583                         if (tp->st_op == LL_STATFS_LMV) {
6584                                 sum.os_ffree += stat_buf.os_ffree;
6585                                 sum.os_files += stat_buf.os_files;
6586                         } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6587                                 ost_files += stat_buf.os_files;
6588                                 ost_ffree += stat_buf.os_ffree;
6589                         }
6590                         sum.os_blocks += stat_buf.os_blocks *
6591                                          stat_buf.os_bsize;
6592                         sum.os_bfree  += stat_buf.os_bfree *
6593                                          stat_buf.os_bsize;
6594                         sum.os_bavail += stat_buf.os_bavail *
6595                                          stat_buf.os_bsize;
6596                 }
6597         }
6598
6599         close(fd);
6600
6601         /*
6602          * If we have _some_ OSTs, but don't have as many free objects on the
6603          * OST as inodes on the MDTs, reduce the reported number of inodes
6604          * to compensate, so that the "inodes in use" number is correct.
6605          * This should be kept in sync with ll_statfs_internal().
6606          */
6607         if (ost_files && ost_ffree < sum.os_ffree) {
6608                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6609                 sum.os_ffree = ost_ffree;
6610         }
6611         if (flags & MNTDF_SHOW) {
6612                 printf("\n");
6613                 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6614                 printf("\n");
6615         }
6616
6617         return rc;
6618 }
6619
6620 enum {
6621         LAYOUT_INHERIT_UNSET    = -2,
6622 };
6623
6624 /* functions */
6625 static int lfs_setdirstripe(int argc, char **argv)
6626 {
6627         char *dname;
6628         struct lfs_setstripe_args lsa = { 0 };
6629         struct llapi_stripe_param *param = NULL;
6630         __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6631         char *end;
6632         int c;
6633         char *mode_opt = NULL;
6634         bool default_stripe = false;
6635         bool delete = false;
6636         bool foreign_mode = false;
6637         mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6638         mode_t previous_mode = 0;
6639         char *xattr = NULL;
6640         __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6641         int max_inherit = LAYOUT_INHERIT_UNSET;
6642         int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6643         struct option long_opts[] = {
6644         { .val = 'c',   .name = "count",        .has_arg = required_argument },
6645         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument },
6646         { .val = 'd',   .name = "delete",       .has_arg = no_argument },
6647         { .val = 'D',   .name = "default",      .has_arg = no_argument },
6648         { .val = 'D',   .name = "default_stripe", .has_arg = no_argument },
6649         { .val = LFS_LAYOUT_FLAGS_OPT,
6650                         .name = "flags",        .has_arg = required_argument },
6651         { .val = LFS_LAYOUT_FOREIGN_OPT,
6652                         .name = "foreign",      .has_arg = optional_argument},
6653         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
6654 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6655         { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
6656         { .val = 'i',   .name = "mdt",          .has_arg = required_argument },
6657 #else
6658 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
6659         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
6660         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
6661 #endif
6662 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6663         { .val = 'i',   .name = "index",        .has_arg = required_argument },
6664 #endif
6665         { .val = 'o',   .name = "mode",         .has_arg = required_argument },
6666 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6667         { .val = 't',   .name = "hash-type",    .has_arg = required_argument },
6668 #endif
6669         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
6670         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
6671         { .val = 'X',   .name = "max-inherit",  .has_arg = required_argument },
6672         { .val = LFS_INHERIT_RR_OPT,
6673                         .name = "max-inherit-rr", .has_arg = required_argument},
6674 /* setstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
6675         { .name = NULL } };
6676         int result = 0;
6677
6678         setstripe_args_init(&lsa);
6679
6680         while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:x:X:",
6681                                 long_opts, NULL)) >= 0) {
6682                 switch (c) {
6683                 case 0:
6684                         /* Long options. */
6685                         break;
6686                 case 'c':
6687                 case 'T':
6688                         errno = 0;
6689                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6690                         if (errno != 0 || *end != '\0' ||
6691                             lsa.lsa_stripe_count < -1 ||
6692                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6693                                 fprintf(stderr,
6694                                         "%s %s: invalid stripe count '%s'\n",
6695                                         progname, argv[0], optarg);
6696                                 return CMD_HELP;
6697                         }
6698                         break;
6699                 case 'd':
6700                         delete = true;
6701                         default_stripe = true;
6702                         break;
6703                 case 'D':
6704                         default_stripe = true;
6705                         break;
6706                 case LFS_LAYOUT_FOREIGN_OPT:
6707                         if (optarg) {
6708                                 /* check pure numeric */
6709                                 type = strtoul(optarg, &end, 0);
6710                                 if (*end) {
6711                                         /* check name */
6712                                         type = check_foreign_type_name(optarg);
6713                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6714                                                 fprintf(stderr,
6715                                                         "%s %s: unknown foreign type '%s'\n",
6716                                                         progname, argv[0],
6717                                                         optarg);
6718                                                 return CMD_HELP;
6719                                         }
6720                                 } else if (type >= UINT32_MAX) {
6721                                         fprintf(stderr,
6722                                                 "%s %s: invalid foreign type '%s'\n",
6723                                                 progname, argv[0], optarg);
6724                                         return CMD_HELP;
6725                                 }
6726                         }
6727                         foreign_mode = true;
6728                         break;
6729                 case LFS_LAYOUT_FLAGS_OPT:
6730                         errno = 0;
6731                         flags = strtoul(optarg, &end, 16);
6732                         if (errno != 0 || *end != '\0' ||
6733                             flags >= UINT32_MAX) {
6734                                 fprintf(stderr,
6735                                         "%s %s: invalid hex flags '%s'\n",
6736                                         progname, argv[0], optarg);
6737                                 return CMD_HELP;
6738                         }
6739                         if (!foreign_mode) {
6740                                 fprintf(stderr,
6741                                         "%s %s: hex flags must be specified with --foreign option\n",
6742                                         progname, argv[0]);
6743                                 return CMD_HELP;
6744                         }
6745                         break;
6746 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6747                 case 't':
6748                         fprintf(stderr,
6749                                 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6750 #endif
6751                 case 'H':
6752                         lsa.lsa_pattern = check_hashtype(optarg);
6753                         if (lsa.lsa_pattern == 0) {
6754                                 fprintf(stderr,
6755                                         "%s %s: bad stripe hash type '%s'\n",
6756                                         progname, argv[0], optarg);
6757                                 return CMD_HELP;
6758                         }
6759                         break;
6760                 case 'i':
6761 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6762                 case 'm':
6763 #endif
6764 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6765                         if (strcmp(argv[optind - 1], "--index") == 0)
6766                                 fprintf(stderr,
6767                                         "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6768                                         progname, argv[0]);
6769 #endif
6770                         lsa.lsa_nr_tgts = parse_targets(mdts,
6771                                                 sizeof(mdts) / sizeof(__u32),
6772                                                 lsa.lsa_nr_tgts, optarg, NULL);
6773                         if (lsa.lsa_nr_tgts < 0) {
6774                                 fprintf(stderr,
6775                                         "%s %s: invalid MDT target(s) '%s'\n",
6776                                         progname, argv[0], optarg);
6777                                 return CMD_HELP;
6778                         }
6779
6780                         lsa.lsa_tgts = mdts;
6781                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6782                                 lsa.lsa_stripe_off = mdts[0];
6783                         break;
6784 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 15, 53, 0)
6785                 case 'm':
6786                         fprintf(stderr,
6787                                 "warning: '-m' is deprecated, use '--mode' or '-o' instead\n");
6788 #endif
6789                 case 'o':
6790                         mode_opt = optarg;
6791                         break;
6792                 case 'x':
6793                         xattr = optarg;
6794                         break;
6795                 case 'X':
6796                         if (!default_stripe) {
6797                                 fprintf(stderr,
6798                                         "%s %s: '--max-inherit' must be specified with '-D'\n",
6799                                         progname, argv[0]);
6800                                 return CMD_HELP;
6801                         }
6802                         errno = 0;
6803                         max_inherit = strtol(optarg, &end, 10);
6804                         if (errno != 0 || *end != '\0' || max_inherit < -2) {
6805                                 fprintf(stderr,
6806                                         "%s %s: invalid max-inherit '%s'\n",
6807                                         progname, argv[0], optarg);
6808                                 return CMD_HELP;
6809                         }
6810                         if (max_inherit == 0) {
6811                                 max_inherit = LMV_INHERIT_NONE;
6812                         } else if (max_inherit == -1) {
6813                                 max_inherit = LMV_INHERIT_UNLIMITED;
6814                         } else if (max_inherit > LMV_INHERIT_MAX) {
6815                                 fprintf(stderr,
6816                                         "%s %s: max-inherit %d exceeds maximum %u\n",
6817                                         progname, argv[0], max_inherit,
6818                                         LMV_INHERIT_MAX);
6819                                 return CMD_HELP;
6820                         }
6821                         break;
6822                 case LFS_INHERIT_RR_OPT:
6823                         if (!default_stripe) {
6824                                 fprintf(stderr,
6825                                         "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
6826                                         progname, argv[0]);
6827                                 return CMD_HELP;
6828                         }
6829                         errno = 0;
6830                         max_inherit_rr = strtol(optarg, &end, 10);
6831                         if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
6832                                 fprintf(stderr,
6833                                         "%s %s: invalid max-inherit-rr '%s'\n",
6834                                         progname, argv[0], optarg);
6835                                 return CMD_HELP;
6836                         }
6837                         if (max_inherit_rr == 0) {
6838                                 max_inherit_rr = LMV_INHERIT_RR_NONE;
6839                         } else if (max_inherit_rr == -1) {
6840                                 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
6841                         } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
6842                                 fprintf(stderr,
6843                                         "%s %s: max-inherit-rr %d exceeds maximum %u\n",
6844                                         progname, argv[0], max_inherit_rr,
6845                                         LMV_INHERIT_RR_MAX);
6846                                 return CMD_HELP;
6847                         }
6848                         break;
6849                 default:
6850                         fprintf(stderr, "%s %s: unrecognized option '%s'\n",
6851                                 progname, argv[0], argv[optind - 1]);
6852                         return CMD_HELP;
6853                 }
6854         }
6855
6856         if (optind == argc) {
6857                 fprintf(stderr, "%s %s: DIR must be specified\n",
6858                         progname, argv[0]);
6859                 return CMD_HELP;
6860         }
6861
6862         if (xattr && !foreign_mode) {
6863                 /*
6864                  * only print a warning as this is armless and will be
6865                  * ignored
6866                  */
6867                 fprintf(stderr,
6868                         "%s %s: xattr has been specified for non-foreign layout\n",
6869                         progname, argv[0]);
6870         } else if (foreign_mode && !xattr) {
6871                 fprintf(stderr,
6872                         "%s %s: xattr must be provided in foreign mode\n",
6873                         progname, argv[0]);
6874                 return CMD_HELP;
6875         }
6876
6877         if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
6878             lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
6879                 fprintf(stderr,
6880                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
6881                         progname, argv[0]);
6882                 return CMD_HELP;
6883         }
6884
6885         if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
6886             lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
6887                 /* if no parameters set, create directory on least-used MDTs */
6888                 lsa.lsa_stripe_off = -1;
6889                 lsa.lsa_stripe_count = 1;
6890         }
6891
6892         if (delete &&
6893             (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
6894              lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
6895                 fprintf(stderr,
6896                         "%s %s: cannot specify -d with -c or -i options\n",
6897                         progname, argv[0]);
6898                 return CMD_HELP;
6899         }
6900
6901         if (mode_opt) {
6902                 mode = strtoul(mode_opt, &end, 8);
6903                 if (*end != '\0') {
6904                         fprintf(stderr,
6905                                 "%s %s: bad MODE '%s'\n",
6906                                 progname, argv[0], mode_opt);
6907                         return CMD_HELP;
6908                 }
6909                 previous_mode = umask(0);
6910         }
6911
6912         if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
6913             lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
6914             lsa.lsa_stripe_off != -1) {
6915                 fprintf(stderr,
6916                         "%s %s: max-inherit-rr is meaningless if stripe offset != -1\n",
6917                         progname, argv[0]);
6918                 return CMD_HELP;
6919         }
6920
6921         /* foreign LMV/dir case */
6922         if (foreign_mode) {
6923                 if (argc > optind + 1) {
6924                         fprintf(stderr,
6925                                 "%s %s: cannot specify multiple foreign dirs\n",
6926                                 progname, argv[0]);
6927                         return CMD_HELP;
6928                 }
6929
6930                 dname = argv[optind];
6931                 result = llapi_dir_create_foreign(dname, mode, type, flags,
6932                                                   xattr);
6933                 if (result != 0)
6934                         fprintf(stderr,
6935                                 "%s mkdir: can't create foreign dir '%s': %s\n",
6936                                 progname, dname, strerror(-result));
6937                 return result;
6938         }
6939
6940         /*
6941          * initialize stripe parameters, in case param is converted to specific,
6942          * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
6943          */
6944         param = calloc(1, offsetof(typeof(*param),
6945                        lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
6946                                 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
6947         if (!param) {
6948                 fprintf(stderr,
6949                         "%s %s: cannot allocate memory for parameters: %s\n",
6950                         progname, argv[0], strerror(ENOMEM));
6951                 return CMD_HELP;
6952         }
6953
6954         /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
6955         if (default_stripe && lsa.lsa_stripe_off == -1 &&
6956             (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
6957              lsa.lsa_stripe_count == 0))
6958                 lsa.lsa_stripe_count = 1;
6959         if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
6960                 param->lsp_stripe_count = lsa.lsa_stripe_count;
6961         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
6962                 param->lsp_stripe_offset = -1;
6963         else
6964                 param->lsp_stripe_offset = lsa.lsa_stripe_off;
6965         if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
6966                 param->lsp_stripe_pattern = lsa.lsa_pattern;
6967         else
6968                 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
6969         param->lsp_pool = lsa.lsa_pool_name;
6970         param->lsp_is_specific = false;
6971         if (default_stripe) {
6972                 if (max_inherit == LAYOUT_INHERIT_UNSET)
6973                         max_inherit = LMV_INHERIT_DEFAULT;
6974                 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
6975                         max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
6976                 param->lsp_max_inherit = max_inherit;
6977                 param->lsp_max_inherit_rr = max_inherit_rr;
6978         }
6979         if (strcmp(argv[0], "mkdir") == 0)
6980                 param->lsp_is_create = true;
6981         if (lsa.lsa_nr_tgts > 1) {
6982                 if (lsa.lsa_stripe_count > 0 &&
6983                     lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
6984                     lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
6985                         fprintf(stderr,
6986                                 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
6987                                 argv[0], lsa.lsa_stripe_count,
6988                                 lsa.lsa_nr_tgts);
6989                         free(param);
6990                         return CMD_HELP;
6991                 }
6992
6993                 param->lsp_is_specific = true;
6994                 param->lsp_stripe_count = lsa.lsa_nr_tgts;
6995                 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
6996         }
6997
6998         dname = argv[optind];
6999         do {
7000                 if (default_stripe) {
7001                         result = llapi_dir_set_default_lmv(dname, param);
7002                         if (result)
7003                                 fprintf(stderr,
7004                                         "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7005                                         progname, dname, strerror(-result));
7006                         continue;
7007                 }
7008
7009                 result = llapi_dir_create(dname, mode, param);
7010                 if (result)
7011                         fprintf(stderr,
7012                                 "%s setdirstripe: cannot create dir '%s': %s\n",
7013                                 progname, dname, strerror(-result));
7014         } while (!result && (dname = argv[++optind]));
7015
7016         if (mode_opt)
7017                 umask(previous_mode);
7018
7019         free(param);
7020         return result;
7021 }
7022
7023 /* functions */
7024 static int lfs_rmentry(int argc, char **argv)
7025 {
7026         char *dname;
7027         int   index;
7028         int   result = 0;
7029
7030         if (argc <= 1) {
7031                 fprintf(stderr, "error: %s: missing dirname\n",
7032                         argv[0]);
7033                 return CMD_HELP;
7034         }
7035
7036         index = 1;
7037         dname = argv[index];
7038         while (dname) {
7039                 result = llapi_direntry_remove(dname);
7040                 if (result) {
7041                         fprintf(stderr,
7042                                 "error: %s: remove dir entry '%s' failed\n",
7043                                 argv[0], dname);
7044                         break;
7045                 }
7046                 dname = argv[++index];
7047         }
7048         return result;
7049 }
7050
7051 static int lfs_unlink_foreign(int argc, char **argv)
7052 {
7053         char *name;
7054         int   index;
7055         int   result = 0;
7056
7057         if (argc <= 1) {
7058                 fprintf(stderr, "error: %s: missing pathname\n",
7059                         argv[0]);
7060                 return CMD_HELP;
7061         }
7062
7063         index = 1;
7064         name = argv[index];
7065         while (name != NULL) {
7066                 result = llapi_unlink_foreign(name);
7067                 if (result) {
7068                         fprintf(stderr,
7069                                 "error: %s: unlink foreign entry '%s' failed\n",
7070                                 argv[0], name);
7071                         break;
7072                 }
7073                 name = argv[++index];
7074         }
7075         return result;
7076 }
7077
7078 static int lfs_mv(int argc, char **argv)
7079 {
7080         struct lmv_user_md lmu = { LMV_USER_MAGIC };
7081         struct find_param param = {
7082                 .fp_max_depth = -1,
7083                 .fp_mdt_index = -1,
7084         };
7085         char *end;
7086         int c;
7087         int rc = 0;
7088         struct option long_opts[] = {
7089         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
7090         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
7091         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7092         { .name = NULL } };
7093
7094         while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7095                 switch (c) {
7096 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7097                 case 'M':
7098                         fprintf(stderr,
7099                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7100 #endif
7101                 case 'm':
7102                         errno = 0;
7103                         lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7104                         if (errno != 0 || *end != '\0' ||
7105                             lmu.lum_stripe_offset >= UINT32_MAX) {
7106                                 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7107                                         progname, optarg);
7108                                 return CMD_HELP;
7109                         }
7110                         break;
7111                 case 'v':
7112                         param.fp_verbose = VERBOSE_DETAIL;
7113                         break;
7114                 default:
7115                         fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7116                                 progname, argv[optind - 1]);
7117                         return CMD_HELP;
7118                 }
7119         }
7120
7121         if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7122                 fprintf(stderr, "%s mv: MDT index must be specified\n",
7123                         progname);
7124                 return CMD_HELP;
7125         }
7126
7127         if (optind >= argc) {
7128                 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7129                 return CMD_HELP;
7130         }
7131
7132         lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7133
7134         /* initialize migrate mdt parameters */
7135         param.fp_lmv_md = &lmu;
7136         param.fp_migrate = 1;
7137         rc = llapi_migrate_mdt(argv[optind], &param);
7138         if (rc != 0)
7139                 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7140                         progname, argv[optind], lmu.lum_stripe_offset,
7141                         strerror(-rc));
7142         return rc;
7143 }
7144
7145 static int lfs_osts(int argc, char **argv)
7146 {
7147         return lfs_tgts(argc, argv);
7148 }
7149
7150 static int lfs_mdts(int argc, char **argv)
7151 {
7152         return lfs_tgts(argc, argv);
7153 }
7154
7155 static int lfs_df(int argc, char **argv)
7156 {
7157         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7158         enum mntdf_flags flags = MNTDF_SHOW;
7159         int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7160         int c, rc = 0, index = 0;
7161         char fsname[PATH_MAX] = "", *pool_name = NULL;
7162         struct option long_opts[] = {
7163         { .val = 'h',   .name = "human-readable", .has_arg = no_argument },
7164         { .val = 'H',   .name = "si",           .has_arg = no_argument },
7165         { .val = 'i',   .name = "inodes",       .has_arg = no_argument },
7166         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
7167         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
7168         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7169         { .name = NULL} };
7170
7171         while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7172                 switch (c) {
7173                 case 'h':
7174                         flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7175                         break;
7176                 case 'H':
7177                         flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7178                         break;
7179                 case 'i':
7180                         flags |= MNTDF_INODES;
7181                         break;
7182                 case 'l':
7183                         flags |= MNTDF_LAZY;
7184                         break;
7185                 case 'p':
7186                         pool_name = optarg;
7187                         break;
7188                 case 'v':
7189                         flags |= MNTDF_VERBOSE;
7190                         break;
7191                 default:
7192                         return CMD_HELP;
7193                 }
7194         }
7195         if (optind < argc && !realpath(argv[optind], path)) {
7196                 rc = -errno;
7197                 fprintf(stderr, "error: invalid path '%s': %s\n",
7198                         argv[optind], strerror(-rc));
7199                 return rc;
7200         }
7201
7202         while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7203                 /* Check if we have a mount point */
7204                 if (mntdir[0] == '\0')
7205                         continue;
7206
7207                 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7208                 if (rc || path[0] != '\0')
7209                         break;
7210                 fsname[0] = '\0'; /* avoid matching in next loop */
7211                 mntdir[0] = '\0'; /* avoid matching in next loop */
7212         }
7213
7214         return rc;
7215 }
7216
7217 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7218                           bool opt_instance, bool opt_fsname, bool opt_mntdir)
7219 {
7220         int rc = 0;
7221
7222         if (opt_fsname == opt_instance) { /* both true or both false */
7223                 rc = llapi_getname(mntdir, buf, buflen);
7224         } else if (opt_fsname) {
7225                 /*
7226                  * llapi_search_mounts() fills @buf with fsname, but that is not
7227                  * called if explicit paths are specified on the command-line
7228                  */
7229                 if (buf[0] == '\0')
7230                         rc = llapi_get_fsname(mntdir, buf, buflen);
7231         } else /* if (opt_instance) */ {
7232                 rc = llapi_get_instance(mntdir, buf, buflen);
7233         }
7234
7235         if (rc < 0) {
7236                 fprintf(stderr, "cannot get instance for '%s': %s\n",
7237                         mntdir, strerror(-rc));
7238                 return rc;
7239         }
7240
7241         if (opt_mntdir)
7242                 printf("%s %s\n", buf, mntdir);
7243         else
7244                 printf("%s\n", buf);
7245
7246         return 0;
7247 }
7248
7249 static int lfs_getname(int argc, char **argv)
7250 {
7251         struct option long_opts[] = {
7252         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7253         { .val = 'i',   .name = "instance",     .has_arg = no_argument },
7254         { .val = 'n',   .name = "fsname",       .has_arg = no_argument },
7255         { .name = NULL} };
7256         bool opt_instance = false, opt_fsname = false;
7257         char fsname[PATH_MAX] = "";
7258         int rc = 0, rc2, c;
7259
7260         while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7261                 switch (c) {
7262                 case 'i':
7263                         opt_instance = true;
7264                         break;
7265                 case 'n':
7266                         opt_fsname = true;
7267                         break;
7268                 case 'h':
7269                 default:
7270                         return CMD_HELP;
7271                 }
7272         }
7273
7274         if (optind == argc) { /* no paths specified, get all paths. */
7275                 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7276                 int index = 0;
7277
7278                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7279                         rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7280                                              opt_instance, opt_fsname, true);
7281                         if (!rc)
7282                                 rc = rc2;
7283                         path[0] = fsname[0] = mntdir[0] = '\0';
7284                 }
7285         } else { /* paths specified, only attempt to search these. */
7286                 bool opt_mntdir;
7287
7288                 /* if only one path is given, print only requested info */
7289                 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7290
7291                 for (; optind < argc; optind++) {
7292                         rc2 = print_instance(argv[optind], fsname,
7293                                              sizeof(fsname), opt_instance,
7294                                              opt_fsname, opt_mntdir);
7295                         if (!rc)
7296                                 rc = rc2;
7297                         fsname[0] = '\0';
7298                 }
7299         }
7300
7301         return rc;
7302 }
7303
7304 static int lfs_check(int argc, char **argv)
7305 {
7306         char mntdir[PATH_MAX] = {'\0'};
7307         int num_types = 1;
7308         char *obd_types[3];
7309         char obd_type1[4];
7310         char obd_type2[4];
7311         char obd_type3[4];
7312         int rc;
7313
7314         if (argc != 2) {
7315                 fprintf(stderr, "%s check: server type must be specified\n",
7316                         progname);
7317                 return CMD_HELP;
7318         }
7319
7320         obd_types[0] = obd_type1;
7321         obd_types[1] = obd_type2;
7322         obd_types[2] = obd_type3;
7323
7324         if (strcmp(argv[1], "osts") == 0) {
7325                 strcpy(obd_types[0], "osc");
7326         } else if (strcmp(argv[1], "mdts") == 0 ||
7327                    strcmp(argv[1], "mds") == 0) {
7328                 strcpy(obd_types[0], "mdc");
7329         } else if (strcmp(argv[1], "mgts") == 0) {
7330                 strcpy(obd_types[0], "mgc");
7331         } else if (strcmp(argv[1], "all") == 0 ||
7332                    strcmp(argv[1], "servers") == 0) {
7333                 num_types = 3;
7334                 strcpy(obd_types[0], "osc");
7335                 strcpy(obd_types[1], "mdc");
7336                 strcpy(obd_types[2], "mgc");
7337         } else {
7338                 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7339                         progname, argv[1]);
7340                 return CMD_HELP;
7341         }
7342
7343         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
7344         if (rc < 0 || mntdir[0] == '\0') {
7345                 fprintf(stderr,
7346                         "%s check: cannot find mounted Lustre filesystem: %s\n",
7347                         progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7348                 return rc;
7349         }
7350
7351         rc = llapi_target_check(num_types, obd_types, mntdir);
7352         if (rc)
7353                 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7354                         progname, argv[1], strerror(-rc));
7355
7356         return rc;
7357 }
7358
7359 #ifdef HAVE_SYS_QUOTA_H
7360 #define ADD_OVERFLOW(a, b) \
7361                      ((((a) + (b)) < (a)) ? \
7362                       ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7363
7364 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7365  * returns the value or ULONG_MAX on integer overflow or incorrect format
7366  * Notes:
7367  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7368  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7369  *        3. empty integer value is interpreted as 0
7370  */
7371 static unsigned long str2sec(const char *timestr)
7372 {
7373         const char spec[] = "smhdw";
7374         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7375         unsigned long val = 0;
7376         char *tail;
7377
7378         if (strpbrk(timestr, spec) == NULL) {
7379                 /*
7380                  * no specifiers inside the time string,
7381                  * should treat it as an integer value
7382                  */
7383                 val = strtoul(timestr, &tail, 10);
7384                 return *tail ? ULONG_MAX : val;
7385         }
7386
7387         /* format string is XXwXXdXXhXXmXXs */
7388         while (*timestr) {
7389                 unsigned long v;
7390                 int ind;
7391                 char *ptr;
7392
7393                 v = strtoul(timestr, &tail, 10);
7394                 if (v == ULONG_MAX || *tail == '\0')
7395                         /*
7396                          * value too large (ULONG_MAX or more)
7397                          * or missing specifier
7398                          */
7399                         goto error;
7400
7401                 ptr = strchr(spec, *tail);
7402                 if (!ptr)
7403                         /* unknown specifier */
7404                         goto error;
7405
7406                 ind = ptr - spec;
7407
7408                 /* check if product will overflow the type */
7409                 if (!(v < ULONG_MAX / mult[ind]))
7410                         goto error;
7411
7412                 ADD_OVERFLOW(val, mult[ind] * v);
7413                 if (val == ULONG_MAX)
7414                         goto error;
7415
7416                 timestr = tail + 1;
7417         }
7418
7419         return val;
7420
7421 error:
7422         return ULONG_MAX;
7423 }
7424
7425 #define ARG2ULL(nr, str, def_units)                                     \
7426 do {                                                                    \
7427         unsigned long long limit, units = def_units;                    \
7428         int rc;                                                         \
7429                                                                         \
7430         rc = llapi_parse_size(str, &limit, &units, 1);                  \
7431         if (rc < 0) {                                                   \
7432                 fprintf(stderr, "%s: invalid limit '%s'\n",             \
7433                         progname, str);                                 \
7434                 return CMD_HELP;                                        \
7435         }                                                               \
7436         nr = limit;                                                     \
7437 } while (0)
7438
7439 static inline int has_times_option(int argc, char **argv)
7440 {
7441         int i;
7442
7443         for (i = 1; i < argc; i++)
7444                 if (!strcmp(argv[i], "-t"))
7445                         return 1;
7446
7447         return 0;
7448 }
7449
7450 static inline int lfs_verify_poolarg(char *pool)
7451 {
7452         if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7453                 fprintf(stderr,
7454                         "Pool name '%.*s' is longer than %d\n",
7455                         LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7456                 return 1;
7457         }
7458         return 0;
7459 }
7460
7461 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7462 {
7463         int c, rc;
7464         char *mnt, *obd_type = (char *)qctl->obd_type;
7465         struct obd_dqblk *dqb = &qctl->qc_dqblk;
7466         struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7467         struct option long_opts[] = {
7468         { .val = 'b',   .name = "block-grace",  .has_arg = required_argument },
7469         { .val = 'g',   .name = "group",        .has_arg = no_argument },
7470         { .val = 'i',   .name = "inode-grace",  .has_arg = required_argument },
7471         { .val = 'p',   .name = "projid",       .has_arg = no_argument },
7472         { .val = 't',   .name = "times",        .has_arg = no_argument },
7473         { .val = 'u',   .name = "user",         .has_arg = no_argument },
7474         { .val = LFS_POOL_OPT,
7475                         .name = "pool",         .has_arg = required_argument },
7476         { .name = NULL } };
7477         int qtype;
7478
7479         qctl->qc_cmd  = LUSTRE_Q_SETINFO;
7480         qctl->qc_type = ALLQUOTA;
7481
7482         while ((c = getopt_long(argc, argv, "b:gi:ptu",
7483                                 long_opts, NULL)) != -1) {
7484                 switch (c) {
7485                 case 'u':
7486                         qtype = USRQUOTA;
7487                         goto quota_type;
7488                 case 'g':
7489                         qtype = GRPQUOTA;
7490                         goto quota_type;
7491                 case 'p':
7492                         qtype = PRJQUOTA;
7493 quota_type:
7494                         if (qctl->qc_type != ALLQUOTA) {
7495                                 fprintf(stderr,
7496                                         "error: -u/g/p can't be used more than once\n");
7497                                 return CMD_HELP;
7498                         }
7499                         qctl->qc_type = qtype;
7500                         break;
7501                 case 'b':
7502                         if (strncmp(optarg, NOTIFY_GRACE,
7503                                     strlen(NOTIFY_GRACE)) == 0) {
7504                                 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7505                         } else {
7506                                 dqi->dqi_bgrace = str2sec(optarg);
7507                                 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7508                                         fprintf(stderr,
7509                                                 "error: bad block-grace: %s\n",
7510                                                 optarg);
7511                                         return CMD_HELP;
7512                                 }
7513                         }
7514                         dqb->dqb_valid |= QIF_BTIME;
7515                         break;
7516                 case 'i':
7517                         if (strncmp(optarg, NOTIFY_GRACE,
7518                                     strlen(NOTIFY_GRACE)) == 0) {
7519                                 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7520                         } else {
7521                                 dqi->dqi_igrace = str2sec(optarg);
7522                                 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7523                                         fprintf(stderr,
7524                                                 "error: bad inode-grace: %s\n",
7525                                                 optarg);
7526                                         return CMD_HELP;
7527                                 }
7528                         }
7529                         dqb->dqb_valid |= QIF_ITIME;
7530                         break;
7531                 case 't': /* Yes, of course! */
7532                         break;
7533                 case LFS_POOL_OPT:
7534                         if (lfs_verify_poolarg(optarg))
7535                                 return -1;
7536                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7537                         qctl->qc_cmd  = LUSTRE_Q_SETINFOPOOL;
7538                         break;
7539                 /* getopt prints error message for us when opterr != 0 */
7540                 default:
7541                         return CMD_HELP;
7542                 }
7543         }
7544
7545         if (qctl->qc_type == ALLQUOTA) {
7546                 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
7547                 return CMD_HELP;
7548         }
7549
7550         if (optind != argc - 1) {
7551                 fprintf(stderr, "error: unexpected parameters encountered\n");
7552                 return CMD_HELP;
7553         }
7554
7555         mnt = argv[optind];
7556         rc = llapi_quotactl(mnt, qctl);
7557         if (rc) {
7558                 if (*obd_type)
7559                         fprintf(stderr, "%s %s ", obd_type,
7560                                 obd_uuid2str(&qctl->obd_uuid));
7561                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7562                 return rc;
7563         }
7564
7565         return 0;
7566 }
7567
7568 #define BSLIMIT (1 << 0)
7569 #define BHLIMIT (1 << 1)
7570 #define ISLIMIT (1 << 2)
7571 #define IHLIMIT (1 << 3)
7572
7573 int lfs_setquota(int argc, char **argv)
7574 {
7575         int c, rc = 0;
7576         struct if_quotactl *qctl;
7577         char *mnt, *obd_type;
7578         struct obd_dqblk *dqb;
7579         struct option long_opts[] = {
7580         { .val = 'b',   .name = "block-softlimit",
7581                                                 .has_arg = required_argument },
7582         { .val = 'B',   .name = "block-hardlimit",
7583                                                 .has_arg = required_argument },
7584         { .val = 'd',   .name = "default",      .has_arg = no_argument },
7585         { .val = 'g',   .name = "group",        .has_arg = required_argument },
7586         { .val = 'G',   .name = "default-grp",  .has_arg = no_argument },
7587         { .val = 'i',   .name = "inode-softlimit",
7588                                                 .has_arg = required_argument },
7589         { .val = 'I',   .name = "inode-hardlimit",
7590                                                 .has_arg = required_argument },
7591         { .val = 'p',   .name = "projid",       .has_arg = required_argument },
7592         { .val = 'P',   .name = "default-prj",  .has_arg = no_argument },
7593         { .val = 'u',   .name = "user",         .has_arg = required_argument },
7594         { .val = 'U',   .name = "default-usr",  .has_arg = no_argument },
7595         { .val = LFS_POOL_OPT,
7596                         .name = "pool",         .has_arg = required_argument },
7597         { .name = NULL } };
7598         unsigned int limit_mask = 0;
7599         bool use_default = false;
7600         int qtype, qctl_len;
7601
7602         qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
7603         qctl = malloc(qctl_len);
7604         if (!qctl)
7605                 return -ENOMEM;
7606
7607         memset(qctl, 0, qctl_len);
7608         obd_type = (char *)qctl->obd_type;
7609         dqb = &qctl->qc_dqblk;
7610
7611         if (has_times_option(argc, argv)) {
7612                 rc = lfs_setquota_times(argc, argv, qctl);
7613                 goto out;
7614         }
7615
7616         qctl->qc_cmd  = LUSTRE_Q_SETQUOTA;
7617         qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
7618                                    * so it can be used as a marker that qc_type
7619                                    * isn't reinitialized from command line
7620                                    */
7621
7622         while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
7623                 long_opts, NULL)) != -1) {
7624                 switch (c) {
7625                 case 'U':
7626                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7627                         qtype = USRQUOTA;
7628                         qctl->qc_id = 0;
7629                         goto quota_type_def;
7630                 case 'u':
7631                         qtype = USRQUOTA;
7632                         rc = name2uid(&qctl->qc_id, optarg);
7633                         goto quota_type;
7634                 case 'G':
7635                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7636                         qtype = GRPQUOTA;
7637                         qctl->qc_id = 0;
7638                         goto quota_type_def;
7639                 case 'g':
7640                         qtype = GRPQUOTA;
7641                         rc = name2gid(&qctl->qc_id, optarg);
7642                         goto quota_type;
7643                 case 'P':
7644                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7645                         qtype = PRJQUOTA;
7646                         qctl->qc_id = 0;
7647                         goto quota_type_def;
7648                 case 'p':
7649                         qtype = PRJQUOTA;
7650                         rc = name2projid(&qctl->qc_id, optarg);
7651 quota_type:
7652                         if (rc) {
7653                                 if (str2quotaid(&qctl->qc_id, optarg)) {
7654                                         fprintf(stderr,
7655                                                 "%s setquota: invalid id '%s'\n",
7656                                                 progname, optarg);
7657                                         rc = -1;
7658                                         goto out;
7659                                 }
7660                         }
7661
7662                         if (qctl->qc_id == 0) {
7663                                 fprintf(stderr,
7664                                         "%s setquota: can't set quota for root usr/group/project.\n",
7665                                         progname);
7666                                 rc = -1;
7667                                 goto out;
7668                         }
7669
7670 quota_type_def:
7671                         if (qctl->qc_type != ALLQUOTA) {
7672                                 fprintf(stderr,
7673                                         "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
7674                                         progname);
7675                                 rc = CMD_HELP;
7676                                 goto out;
7677                         }
7678                         qctl->qc_type = qtype;
7679                         break;
7680                 case 'd':
7681                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
7682                         use_default = true;
7683                         break;
7684                 case 'b':
7685                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
7686                         dqb->dqb_bsoftlimit >>= 10;
7687                         limit_mask |= BSLIMIT;
7688                         if (dqb->dqb_bsoftlimit &&
7689                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
7690                                 fprintf(stderr,
7691                                         "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7692                                         progname,
7693                                         (unsigned long long)dqb->dqb_bsoftlimit,
7694                                         progname);
7695                         break;
7696                 case 'B':
7697                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
7698                         dqb->dqb_bhardlimit >>= 10;
7699                         limit_mask |= BHLIMIT;
7700                         if (dqb->dqb_bhardlimit &&
7701                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
7702                                 fprintf(stderr,
7703                                         "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
7704                                         "See '%s help setquota' or Lustre manual for details\n",
7705                                         progname,
7706                                         (unsigned long long)dqb->dqb_bhardlimit,
7707                                         progname);
7708                         break;
7709                 case 'i':
7710                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
7711                         limit_mask |= ISLIMIT;
7712                         if (dqb->dqb_isoftlimit &&
7713                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
7714                                 fprintf(stderr,
7715                                         "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7716                                         progname,
7717                                         (unsigned long long)dqb->dqb_isoftlimit,
7718                                         progname);
7719                         break;
7720                 case 'I':
7721                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
7722                         limit_mask |= IHLIMIT;
7723                         if (dqb->dqb_ihardlimit &&
7724                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
7725                                 fprintf(stderr,
7726                                         "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
7727                                         progname,
7728                                         (unsigned long long)dqb->dqb_ihardlimit,
7729                                         progname);
7730                         break;
7731                 case LFS_POOL_OPT:
7732                         if (lfs_verify_poolarg(optarg)) {
7733                                 rc = -1;
7734                                 goto out;
7735                         }
7736                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7737                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
7738                                                 LUSTRE_Q_SETDEFAULT_POOL :
7739                                                 LUSTRE_Q_SETQUOTAPOOL;
7740                         break;
7741                 default:
7742                         fprintf(stderr,
7743                                 "%s setquota: unrecognized option '%s'\n",
7744                                 progname, argv[optind - 1]);
7745                         rc = CMD_HELP;
7746                         goto out;
7747                 }
7748         }
7749
7750         if (qctl->qc_type == ALLQUOTA) {
7751                 fprintf(stderr,
7752                         "%s setquota: either -u or -g must be specified\n",
7753                         progname);
7754                 rc = CMD_HELP;
7755                 goto out;
7756         }
7757
7758         if (!use_default && limit_mask == 0) {
7759                 fprintf(stderr,
7760                         "%s setquota: at least one limit must be specified\n",
7761                         progname);
7762                 rc = CMD_HELP;
7763                 goto out;
7764         }
7765
7766         if (use_default && limit_mask != 0) {
7767                 fprintf(stderr,
7768                         "%s setquota: limits should not be specified when using default quota\n",
7769                         progname);
7770                 rc = CMD_HELP;
7771                 goto out;
7772         }
7773
7774         if (use_default && qctl->qc_id == 0) {
7775                 fprintf(stderr,
7776                         "%s setquota: can not set default quota for root user/group/project\n",
7777                         progname);
7778                 rc = CMD_HELP;
7779                 goto out;
7780         }
7781
7782         if (optind != argc - 1) {
7783                 fprintf(stderr,
7784                         "%s setquota: filesystem not specified or unexpected argument '%s'\n",
7785                         progname, argv[optind]);
7786                 rc = CMD_HELP;
7787                 goto out;
7788         }
7789
7790         mnt = argv[optind];
7791
7792         if (use_default) {
7793                 dqb->dqb_bhardlimit = 0;
7794                 dqb->dqb_bsoftlimit = 0;
7795                 dqb->dqb_ihardlimit = 0;
7796                 dqb->dqb_isoftlimit = 0;
7797                 dqb->dqb_itime = 0;
7798                 dqb->dqb_btime = 0;
7799                 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
7800                 /* do not set inode limits for Pool Quotas */
7801                 if (qctl->qc_cmd  == LUSTRE_Q_SETDEFAULT_POOL)
7802                         dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
7803         } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
7804                    (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
7805                 /* sigh, we can't just set blimits/ilimits */
7806                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
7807                                                .qc_type = qctl->qc_type,
7808                                                .qc_id   = qctl->qc_id};
7809
7810                 rc = llapi_quotactl(mnt, &tmp_qctl);
7811                 if (rc < 0)
7812                         goto out;
7813
7814                 if (!(limit_mask & BHLIMIT))
7815                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
7816                 if (!(limit_mask & BSLIMIT))
7817                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
7818                 if (!(limit_mask & IHLIMIT))
7819                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
7820                 if (!(limit_mask & ISLIMIT))
7821                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
7822
7823                 /* Keep grace times if we have got no softlimit arguments */
7824                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
7825                         dqb->dqb_valid |= QIF_BTIME;
7826                         dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
7827                 }
7828
7829                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
7830                         dqb->dqb_valid |= QIF_ITIME;
7831                         dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
7832                 }
7833         }
7834
7835         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
7836         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
7837
7838         rc = llapi_quotactl(mnt, qctl);
7839         if (rc) {
7840                 if (*obd_type)
7841                         fprintf(stderr,
7842                                 "%s setquota: cannot quotactl '%s' '%s': %s",
7843                                 progname, obd_type,
7844                                 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
7845         }
7846 out:
7847         free(qctl);
7848         return rc;
7849 }
7850
7851 /* Converts seconds value into format string
7852  * result is returned in buf
7853  * Notes:
7854  *        1. result is in descenting order: 1w2d3h4m5s
7855  *        2. zero fields are not filled (except for p. 3): 5d1s
7856  *        3. zero seconds value is presented as "0s"
7857  */
7858 static char *__sec2str(time_t seconds, char *buf)
7859 {
7860         const char spec[] = "smhdw";
7861         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7862         unsigned long c;
7863         char *tail = buf;
7864         int i;
7865
7866         for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
7867                 c = seconds / mult[i];
7868
7869                 if (c > 0 || (i == 0 && buf == tail))
7870                         tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
7871                                           spec[i]);
7872
7873                 seconds %= mult[i];
7874         }
7875
7876         return tail;
7877 }
7878
7879 static void sec2str(time_t seconds, char *buf, int rc)
7880 {
7881         char *tail = buf;
7882
7883         if (rc)
7884                 *tail++ = '[';
7885
7886         tail = __sec2str(seconds, tail);
7887
7888         if (rc && tail - buf < 39) {
7889                 *tail++ = ']';
7890                 *tail++ = 0;
7891         }
7892 }
7893
7894 static void diff2str(time_t seconds, char *buf, time_t now)
7895 {
7896         buf[0] = 0;
7897         if (!seconds)
7898                 return;
7899         if (seconds <= now) {
7900                 strcpy(buf, "none");
7901                 return;
7902         }
7903         __sec2str(seconds - now, buf);
7904 }
7905
7906 static void print_quota_title(char *name, struct if_quotactl *qctl,
7907                               bool human_readable, bool show_default)
7908 {
7909         if (show_default) {
7910                 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
7911                 printf("%15s %8s%8s%8s %8s%8s%8s\n",
7912                        "Filesystem", "bquota", "blimit", "bgrace",
7913                        "iquota", "ilimit", "igrace");
7914         } else {
7915                 printf("Disk quotas for %s %s (%cid %u):\n",
7916                        qtype_name(qctl->qc_type), name,
7917                        *qtype_name(qctl->qc_type), qctl->qc_id);
7918                 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
7919                        "Filesystem", human_readable ? "used" : "kbytes",
7920                        "quota", "limit", "grace",
7921                        "files", "quota", "limit", "grace");
7922         }
7923 }
7924
7925 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
7926 {
7927         if (!h) {
7928                 snprintf(buf, buflen, "%ju", (uintmax_t)num);
7929         } else {
7930                 if (num >> 40)
7931                         snprintf(buf, buflen, "%5.4gP",
7932                                  (double)num / ((__u64)1 << 40));
7933                 else if (num >> 30)
7934                         snprintf(buf, buflen, "%5.4gT",
7935                                  (double)num / (1 << 30));
7936                 else if (num >> 20)
7937                         snprintf(buf, buflen, "%5.4gG",
7938                                  (double)num / (1 << 20));
7939                 else if (num >> 10)
7940                         snprintf(buf, buflen, "%5.4gM",
7941                                  (double)num / (1 << 10));
7942                 else
7943                         snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
7944         }
7945 }
7946
7947 #define STRBUF_LEN      32
7948 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
7949                         int rc, bool h, bool show_default)
7950 {
7951         time_t now;
7952
7953         time(&now);
7954
7955         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
7956             qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
7957             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
7958             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
7959                 int bover = 0, iover = 0;
7960                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
7961                 char numbuf[3][STRBUF_LEN];
7962                 char timebuf[40];
7963                 char strbuf[STRBUF_LEN];
7964
7965                 if (dqb->dqb_bhardlimit &&
7966                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
7967                         bover = 1;
7968                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
7969                         if (dqb->dqb_btime > now)
7970                                 bover = 2;
7971                         else
7972                                 bover = 3;
7973                 }
7974
7975                 if (dqb->dqb_ihardlimit &&
7976                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
7977                         iover = 1;
7978                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
7979                         if (dqb->dqb_itime > now)
7980                                 iover = 2;
7981                         else
7982                                 iover = 3;
7983                 }
7984
7985                 if (strlen(mnt) > 15)
7986                         printf("%s\n%15s", mnt, "");
7987                 else
7988                         printf("%15s", mnt);
7989
7990                 if (bover)
7991                         diff2str(dqb->dqb_btime, timebuf, now);
7992                 else if (show_default)
7993                         snprintf(timebuf, sizeof(timebuf), "%llu",
7994                                  (unsigned long long)dqb->dqb_btime);
7995
7996                 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
7997                            strbuf, sizeof(strbuf), h);
7998                 if (rc == -EREMOTEIO)
7999                         sprintf(numbuf[0], "%s*", strbuf);
8000                 else
8001                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8002                                 "%s" : "[%s]", strbuf);
8003
8004                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8005                 if (type == QC_GENERAL)
8006                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8007                                 "%s" : "[%s]", strbuf);
8008                 else
8009                         sprintf(numbuf[1], "%s", "-");
8010
8011                 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8012                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8013                         "%s" : "[%s]", strbuf);
8014
8015                 if (show_default)
8016                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8017                 else
8018                         printf(" %7s%c %6s %7s %7s",
8019                                numbuf[0], bover ? '*' : ' ', numbuf[1],
8020                                numbuf[2], bover > 1 ? timebuf : "-");
8021
8022                 if (iover)
8023                         diff2str(dqb->dqb_itime, timebuf, now);
8024                 else if (show_default)
8025                         snprintf(timebuf, sizeof(timebuf), "%llu",
8026                                  (unsigned long long)dqb->dqb_itime);
8027
8028                 snprintf(numbuf[0], sizeof(numbuf),
8029                          (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8030                          (uintmax_t)dqb->dqb_curinodes);
8031
8032                 if (type == QC_GENERAL)
8033                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8034                                 "%ju" : "[%ju]",
8035                                 (uintmax_t)dqb->dqb_isoftlimit);
8036                 else
8037                         sprintf(numbuf[1], "%s", "-");
8038
8039                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8040                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8041
8042                 if (show_default)
8043                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8044                 else if (type != QC_OSTIDX)
8045                         printf(" %7s%c %6s %7s %7s",
8046                                numbuf[0], iover ? '*' : ' ', numbuf[1],
8047                                numbuf[2], iover > 1 ? timebuf : "-");
8048                 else
8049                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8050                 printf("\n");
8051         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8052                    qctl->qc_cmd == Q_GETOINFO) {
8053                 char bgtimebuf[40];
8054                 char igtimebuf[40];
8055
8056                 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8057                         strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8058                 else
8059                         sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8060                 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8061                         strncpy(igtimebuf, NOTIFY_GRACE, 40);
8062                 else
8063                         sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8064
8065                 printf("Block grace time: %s; Inode grace time: %s\n",
8066                        bgtimebuf, igtimebuf);
8067         }
8068 }
8069
8070 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8071 {
8072         char *dash, *endp;
8073
8074         /* format is "lustre-OST0001" */
8075         dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8076         if (!dash) {
8077                 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8078                 return -EINVAL;
8079         }
8080         dash += 4;
8081
8082         *idx = strtoul(dash, &endp, 16);
8083         if (*idx > 0xffff) {
8084                 fprintf(stderr, "wrong index %s\n", tgtname);
8085                 return -ERANGE;
8086         }
8087
8088         return 0;
8089 }
8090
8091 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8092                            bool h, __u64 *total)
8093 {
8094         int rc = 0, rc1 = 0, count = 0, i = 0;
8095         char **list = NULL, *buffer = NULL;
8096         __u32 valid = qctl->qc_valid;
8097
8098         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8099                 return 0;
8100
8101         /* Is it correct for the case OST0000, OST0002, OST0003 -
8102          * we will ask OST0001 that is absent and won't ask OST0003? */
8103         rc = llapi_get_obd_count(mnt, &count, is_mdt);
8104         if (rc) {
8105                 fprintf(stderr, "can not get %s count: %s\n",
8106                         is_mdt ? "mdt" : "ost", strerror(-rc));
8107                 return rc;
8108         }
8109
8110         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8111                 char fname[PATH_MAX];
8112                 char fsname[LUSTRE_MAXFSNAME + 1];
8113                 int bufsize = sizeof(struct obd_uuid) * count;
8114
8115                 rc = llapi_search_fsname(mnt, fsname);
8116                 if (rc) {
8117                         fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8118                                 mnt);
8119                         goto out;
8120                 }
8121                 buffer = malloc(bufsize + sizeof(*list) * count);
8122                 if (!buffer)
8123                         return -ENOMEM;
8124                 list = (char **)(buffer + bufsize);
8125                 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8126                 count = llapi_get_poolmembers(fname, list, count,
8127                                               buffer, bufsize);
8128                 if (count <= 0)
8129                         goto out;
8130         }
8131
8132         for (i = 0; i < count; i++) {
8133                 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8134                         unsigned int index;
8135
8136                         if (tgt_name2index(list[i], &index))
8137                                 continue;
8138                         qctl->qc_idx = index;
8139                 } else {
8140                         qctl->qc_idx = i;
8141                 }
8142
8143                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8144                 rc = llapi_quotactl(mnt, qctl);
8145                 if (rc) {
8146                         /* It is remote client case. */
8147                         if (rc == -EOPNOTSUPP) {
8148                                 rc = 0;
8149                                 goto out;
8150                         }
8151
8152                         if (!rc1)
8153                                 rc1 = rc;
8154                         fprintf(stderr, "quotactl %s%d failed.\n",
8155                                 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8156                         continue;
8157                 }
8158
8159                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8160                             qctl->qc_valid, 0, h, false);
8161                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8162                                    qctl->qc_dqblk.dqb_bhardlimit;
8163         }
8164 out:
8165         if (buffer)
8166                 free(buffer);
8167         qctl->qc_valid = valid;
8168         return rc ? : rc1;
8169 }
8170
8171 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8172                            int verbose, int quiet, bool human_readable,
8173                            bool show_default)
8174 {
8175         int rc1 = 0, rc2 = 0, rc3 = 0;
8176         char *obd_type = (char *)qctl->obd_type;
8177         char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8178         __u64 total_ialloc = 0, total_balloc = 0;
8179         bool use_default_for_blk = false;
8180         bool use_default_for_file = false;
8181         int inacc;
8182
8183         rc1 = llapi_quotactl(mnt, qctl);
8184         if (rc1 < 0) {
8185                 switch (rc1) {
8186                 case -ESRCH:
8187                         fprintf(stderr, "%s quotas are not enabled.\n",
8188                                 qtype_name(qctl->qc_type));
8189                         goto out;
8190                 case -EPERM:
8191                         fprintf(stderr, "Permission denied.\n");
8192                 case -ENODEV:
8193                 case -ENOENT:
8194                         /* We already got error message. */
8195                         goto out;
8196                 default:
8197                         fprintf(stderr, "Unexpected quotactl error: %s\n",
8198                                 strerror(-rc1));
8199                 }
8200         }
8201
8202         if (!show_default && qctl->qc_id == 0) {
8203                 qctl->qc_dqblk.dqb_bhardlimit = 0;
8204                 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8205                 qctl->qc_dqblk.dqb_ihardlimit = 0;
8206                 qctl->qc_dqblk.dqb_isoftlimit = 0;
8207                 qctl->qc_dqblk.dqb_btime = 0;
8208                 qctl->qc_dqblk.dqb_itime = 0;
8209                 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8210         }
8211
8212         if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8213             LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8214                 use_default_for_blk = true;
8215                 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8216         }
8217
8218         if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8219             LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8220                 use_default_for_file = true;
8221                 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8222         }
8223
8224         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8225              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8226              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8227              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8228                 print_quota_title(name, qctl, human_readable, show_default);
8229
8230         if (rc1 && *obd_type)
8231                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8232
8233         if (qctl->qc_valid != QC_GENERAL)
8234                 mnt = "";
8235
8236         inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8237                  qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8238                 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8239                  (QIF_LIMITS|QIF_USAGE));
8240
8241         print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8242
8243         if (!show_default && verbose &&
8244             qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8245             qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8246                 char strbuf[STRBUF_LEN];
8247
8248                 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8249                                       &total_ialloc);
8250                 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8251                                       &total_balloc);
8252                 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8253                            human_readable);
8254                 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8255                        (uintmax_t)total_ialloc, strbuf);
8256         }
8257
8258         if (use_default_for_blk)
8259                 printf("%cid %u is using default block quota setting\n",
8260                        *qtype_name(qctl->qc_type), qctl->qc_id);
8261
8262         if (use_default_for_file)
8263                 printf("%cid %u is using default file quota setting\n",
8264                        *qtype_name(qctl->qc_type), qctl->qc_id);
8265
8266         if (rc1 || rc2 || rc3 || inacc)
8267                 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8268 out:
8269         if (rc1)
8270                 return rc1;
8271         if (rc2)
8272                 return rc2;
8273         if (rc3)
8274                 return rc3;
8275         if (inacc)
8276                 return -EIO;
8277
8278         return 0;
8279 }
8280
8281 static int lfs_project(int argc, char **argv)
8282 {
8283         int ret = 0, err = 0, c, i;
8284         struct project_handle_control phc = { 0 };
8285         enum lfs_project_ops_t op;
8286
8287         phc.newline = true;
8288         phc.assign_projid = false;
8289         /* default action */
8290         op = LFS_PROJECT_LIST;
8291
8292         while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8293                 switch (c) {
8294                 case 'c':
8295                         if (op != LFS_PROJECT_LIST) {
8296                                 fprintf(stderr,
8297                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8298                                         progname);
8299                                 return CMD_HELP;
8300                         }
8301
8302                         op = LFS_PROJECT_CHECK;
8303                         break;
8304                 case 'C':
8305                         if (op != LFS_PROJECT_LIST) {
8306                                 fprintf(stderr,
8307                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8308                                         progname);
8309                                 return CMD_HELP;
8310                         }
8311
8312                         op = LFS_PROJECT_CLEAR;
8313                         break;
8314                 case 's':
8315                         if (op != LFS_PROJECT_LIST) {
8316                                 fprintf(stderr,
8317                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8318                                         progname);
8319                                 return CMD_HELP;
8320                         }
8321
8322                         phc.set_inherit = true;
8323                         op = LFS_PROJECT_SET;
8324                         break;
8325                 case 'd':
8326                         phc.dironly = true;
8327                         break;
8328                 case 'k':
8329                         phc.keep_projid = true;
8330                         break;
8331                 case 'r':
8332                         phc.recursive = true;
8333                         break;
8334                 case 'p':
8335                         if (str2quotaid(&phc.projid, optarg)) {
8336                                 fprintf(stderr,
8337                                         "Invalid project ID: %s\n",
8338                                         optarg);
8339                                 return CMD_HELP;
8340                         }
8341
8342                         phc.assign_projid = true;
8343
8344                         break;
8345                 case '0':
8346                         phc.newline = false;
8347                         break;
8348                 default:
8349                         fprintf(stderr, "%s: invalid option '%c'\n",
8350                                 progname, optopt);
8351                         return CMD_HELP;
8352                 }
8353         }
8354
8355         if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8356                 op = LFS_PROJECT_SET;
8357                 phc.set_projid = true;
8358         } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8359                 phc.set_projid = true;
8360         }
8361
8362         switch (op) {
8363         case LFS_PROJECT_CHECK:
8364                 if (phc.keep_projid) {
8365                         fprintf(stderr,
8366                                 "%s: '-k' is useless together with '-c'\n",
8367                                 progname);
8368                         return CMD_HELP;
8369                 }
8370                 break;
8371         case LFS_PROJECT_CLEAR:
8372                 if (!phc.newline) {
8373                         fprintf(stderr,
8374                                 "%s: '-0' is useless together with '-C'\n",
8375                                 progname);
8376                         return CMD_HELP;
8377                 }
8378                 if (phc.assign_projid) {
8379                         fprintf(stderr,
8380                                 "%s: '-p' is useless together with '-C'\n",
8381                                 progname);
8382                         return CMD_HELP;
8383                 }
8384                 break;
8385         case LFS_PROJECT_SET:
8386                 if (!phc.newline) {
8387                         fprintf(stderr,
8388                                 "%s: '-0' is useless together with '-s'\n",
8389                                 progname);
8390                         return CMD_HELP;
8391                 }
8392                 if (phc.keep_projid) {
8393                         fprintf(stderr,
8394                                 "%s: '-k' is useless together with '-s'\n",
8395                                 progname);
8396                         return CMD_HELP;
8397                 }
8398                 break;
8399         default:
8400                 if (!phc.newline) {
8401                         fprintf(stderr,
8402                                 "%s: '-0' is useless for list operations\n",
8403                                 progname);
8404                         return CMD_HELP;
8405                 }
8406                 break;
8407         }
8408
8409         argv += optind;
8410         argc -= optind;
8411         if (argc == 0) {
8412                 fprintf(stderr, "%s: missing file or directory target(s)\n",
8413                         progname);
8414                 return CMD_HELP;
8415         }
8416
8417         for (i = 0; i < argc; i++) {
8418                 switch (op) {
8419                 case LFS_PROJECT_CHECK:
8420                         err = lfs_project_check(argv[i], &phc);
8421                         break;
8422                 case LFS_PROJECT_LIST:
8423                         err = lfs_project_list(argv[i], &phc);
8424                         break;
8425                 case LFS_PROJECT_CLEAR:
8426                         err = lfs_project_clear(argv[i], &phc);
8427                         break;
8428                 case LFS_PROJECT_SET:
8429                         err = lfs_project_set(argv[i], &phc);
8430                         break;
8431                 default:
8432                         break;
8433                 }
8434                 if (err && !ret)
8435                         ret = err;
8436         }
8437
8438         return ret;
8439 }
8440
8441 static int lfs_quota(int argc, char **argv)
8442 {
8443         int c;
8444         char *mnt, *name = NULL;
8445         struct if_quotactl *qctl;
8446         char *obd_uuid;
8447         int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
8448         __u32 valid = QC_GENERAL, idx = 0;
8449         bool human_readable = false;
8450         bool show_default = false;
8451         int qtype;
8452         struct option long_opts[] = {
8453         { .val = LFS_POOL_OPT, .name = "pool", .has_arg = required_argument },
8454         { .name = NULL } };
8455
8456         qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8457         if (!qctl)
8458                 return -ENOMEM;
8459
8460         qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8461         qctl->qc_type = ALLQUOTA;
8462         obd_uuid = (char *)qctl->obd_uuid.uuid;
8463
8464         while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8465                 long_opts, NULL)) != -1) {
8466                 switch (c) {
8467                 case 'U':
8468                         show_default = true;
8469                 case 'u':
8470                         qtype = USRQUOTA;
8471                         goto quota_type;
8472                 case 'G':
8473                         show_default = true;
8474                 case 'g':
8475                         qtype = GRPQUOTA;
8476                         goto quota_type;
8477                 case 'P':
8478                         show_default = true;
8479                 case 'p':
8480                         qtype = PRJQUOTA;
8481 quota_type:
8482                         if (qctl->qc_type != ALLQUOTA) {
8483                                 fprintf(stderr,
8484                                         "%s quota: only one of -u, -g, or -p may be specified\n",
8485                                         progname);
8486                                 rc = CMD_HELP;
8487                                 goto out;
8488                         }
8489                         qctl->qc_type = qtype;
8490                         break;
8491                 case 't':
8492                         qctl->qc_cmd = LUSTRE_Q_GETINFO;
8493                         break;
8494                 case 'o':
8495                         valid = qctl->qc_valid = QC_UUID;
8496                         snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8497                         break;
8498                 case 'i':
8499                         valid = qctl->qc_valid = QC_MDTIDX;
8500                         idx = qctl->qc_idx = atoi(optarg);
8501                         if (idx == 0 && *optarg != '0') {
8502                                 fprintf(stderr,
8503                                         "%s quota: invalid MDT index '%s'\n",
8504                                         progname, optarg);
8505                                 rc = CMD_HELP;
8506                                 goto out;
8507                         }
8508                         break;
8509                 case 'I':
8510                         valid = qctl->qc_valid = QC_OSTIDX;
8511                         idx = qctl->qc_idx = atoi(optarg);
8512                         if (idx == 0 && *optarg != '0') {
8513                                 fprintf(stderr,
8514                                         "%s quota: invalid OST index '%s'\n",
8515                                         progname, optarg);
8516                                 rc = CMD_HELP;
8517                                 goto out;
8518                         }
8519                         break;
8520                 case 'v':
8521                         verbose = 1;
8522                         break;
8523                 case 'q':
8524                         quiet = 1;
8525                         break;
8526                 case 'h':
8527                         human_readable = true;
8528                         break;
8529                 case LFS_POOL_OPT:
8530                         if (lfs_verify_poolarg(optarg)) {
8531                                 rc = -1;
8532                                 goto out;
8533                         }
8534                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8535                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETINFO ?
8536                                                 LUSTRE_Q_GETINFOPOOL :
8537                                                 LUSTRE_Q_GETQUOTAPOOL;
8538                         break;
8539                 default:
8540                         fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8541                                 progname, argv[optind - 1]);
8542                         rc = CMD_HELP;
8543                         goto out;
8544                 }
8545         }
8546
8547         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8548         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8549              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8550              qctl->qc_type == ALLQUOTA &&
8551              optind == argc - 1 && !show_default) {
8552                 qctl->qc_idx = idx;
8553
8554                 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8555                         qctl->qc_type = qtype;
8556                         qctl->qc_valid = valid;
8557                         if (qtype == USRQUOTA) {
8558                                 qctl->qc_id = geteuid();
8559                                 rc = uid2name(&name, qctl->qc_id);
8560                         } else {
8561                                 qctl->qc_id = getegid();
8562                                 rc = gid2name(&name, qctl->qc_id);
8563                                 memset(&qctl->qc_dqblk, 0,
8564                                        sizeof(qctl->qc_dqblk));
8565                         }
8566                         if (rc)
8567                                 name = "<unknown>";
8568                         mnt = argv[optind];
8569                         rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8570                                               human_readable, show_default);
8571                         if (rc1 && !rc)
8572                                 rc = rc1;
8573                 }
8574                 goto out;
8575         /* lfs quota -u username /path/to/lustre/mount */
8576         } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8577                    qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8578                 /* options should be followed by u/g-name and mntpoint */
8579                 if ((!show_default && optind + 2 != argc) ||
8580                     (show_default && optind + 1 != argc) ||
8581                     qctl->qc_type == ALLQUOTA) {
8582                         fprintf(stderr,
8583                                 "%s quota: name and mount point must be specified\n",
8584                                 progname);
8585                         rc = CMD_HELP;
8586                         goto out;
8587                 }
8588
8589                 if (!show_default) {
8590                         name = argv[optind++];
8591                         switch (qctl->qc_type) {
8592                         case USRQUOTA:
8593                                 rc = name2uid(&qctl->qc_id, name);
8594                                 break;
8595                         case GRPQUOTA:
8596                                 rc = name2gid(&qctl->qc_id, name);
8597                                 break;
8598                         case PRJQUOTA:
8599                                 rc = name2projid(&qctl->qc_id, name);
8600                                 break;
8601                         default:
8602                                 rc = -ENOTSUP;
8603                                 break;
8604                         }
8605                 } else {
8606                         qctl->qc_valid = QC_GENERAL;
8607                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8608                                         LUSTRE_Q_GETDEFAULT_POOL :
8609                                         LUSTRE_Q_GETDEFAULT;
8610                         qctl->qc_id = 0;
8611                 }
8612
8613                 if (rc) {
8614                         if (str2quotaid(&qctl->qc_id, name)) {
8615                                 fprintf(stderr, "%s quota: invalid id '%s'\n",
8616                                         progname, name);
8617                                 rc = CMD_HELP;
8618                                 goto out;
8619                         }
8620                 }
8621         } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8622                 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8623                         progname);
8624                 rc = CMD_HELP;
8625                 goto out;
8626         }
8627
8628         mnt = argv[optind];
8629         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8630                              human_readable, show_default);
8631 out:
8632         free(qctl);
8633         return rc;
8634 }
8635 #endif /* HAVE_SYS_QUOTA_H! */
8636
8637 static int flushctx_ioctl(char *mp)
8638 {
8639         int fd, rc;
8640
8641         fd = open(mp, O_RDONLY);
8642         if (fd == -1) {
8643                 fprintf(stderr, "flushctx: error open %s: %s\n",
8644                         mp, strerror(errno));
8645                 return -1;
8646         }
8647
8648         rc = ioctl(fd, LL_IOC_FLUSHCTX);
8649         if (rc == -1)
8650                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8651                         mp, strerror(errno));
8652
8653         close(fd);
8654         return rc;
8655 }
8656
8657 static int lfs_flushctx(int argc, char **argv)
8658 {
8659         int     kdestroy = 0, reap = 0, c;
8660         char    mntdir[PATH_MAX] = {'\0'};
8661         int     index = 0;
8662         int     rc = 0;
8663
8664         while ((c = getopt(argc, argv, "kr")) != -1) {
8665                 switch (c) {
8666                 case 'k':
8667                         kdestroy = 1;
8668                         break;
8669                 case 'r':
8670                         reap = 1;
8671                         break;
8672                 default:
8673                         fprintf(stderr,
8674                                 "error: %s: option '-%c' unrecognized\n",
8675                                 argv[0], c);
8676                         return CMD_HELP;
8677                 }
8678         }
8679
8680         if (kdestroy) {
8681                 rc = system("kdestroy > /dev/null");
8682                 if (rc) {
8683                         rc = WEXITSTATUS(rc);
8684                         fprintf(stderr,
8685                                 "error destroying tickets: %d, continuing\n",
8686                                 rc);
8687                 }
8688         }
8689
8690         if (optind >= argc) {
8691                 /* flush for all mounted lustre fs. */
8692                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8693                         /* Check if we have a mount point */
8694                         if (mntdir[0] == '\0')
8695                                 continue;
8696
8697                         if (flushctx_ioctl(mntdir))
8698                                 rc = -1;
8699
8700                         mntdir[0] = '\0'; /* avoid matching in next loop */
8701                 }
8702         } else {
8703                 /* flush fs as specified */
8704                 while (optind < argc) {
8705                         if (flushctx_ioctl(argv[optind++]))
8706                                 rc = -1;
8707                 }
8708         }
8709
8710         if (reap) {
8711                 rc = system("keyctl reap > /dev/null");
8712                 if (rc != 0) {
8713                         rc = WEXITSTATUS(rc);
8714                         fprintf(stderr, "error reaping keyring: %d\n", rc);
8715                 }
8716         }
8717
8718         return rc;
8719 }
8720
8721 static int lfs_changelog(int argc, char **argv)
8722 {
8723         void *changelog_priv;
8724         struct changelog_rec *rec;
8725         long long startrec = 0, endrec = 0;
8726         char *mdd;
8727         struct option long_opts[] = {
8728                 { .val = 'f', .name = "follow", .has_arg = no_argument },
8729                 { .name = NULL } };
8730         char short_opts[] = "f";
8731         int rc, follow = 0;
8732
8733         while ((rc = getopt_long(argc, argv, short_opts,
8734                 long_opts, NULL)) != -1) {
8735                 switch (rc) {
8736                 case 'f':
8737                         follow++;
8738                         break;
8739                 default:
8740                         fprintf(stderr,
8741                                 "%s changelog: unrecognized option '%s'\n",
8742                                 progname, argv[optind - 1]);
8743                         return CMD_HELP;
8744                 }
8745         }
8746         if (optind >= argc) {
8747                 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8748                         progname);
8749                 return CMD_HELP;
8750         }
8751
8752         mdd = argv[optind++];
8753         if (argc > optind) {
8754                 errno = 0;
8755                 startrec = strtoll(argv[optind++], NULL, 10);
8756                 if (errno != 0 || startrec < 0) {
8757                         fprintf(stderr,
8758                                 "%s changelog: bad startrec\n",
8759                                 progname);
8760                         return CMD_HELP;
8761                 }
8762         }
8763
8764         if (argc > optind) {
8765                 errno = 0;
8766                 endrec = strtoll(argv[optind++], NULL, 10);
8767                 if (errno != 0 || endrec < 0) {
8768                         fprintf(stderr,
8769                                 "%s changelog: bad endrec\n",
8770                                 progname);
8771                         return CMD_HELP;
8772                 }
8773         }
8774
8775         rc = llapi_changelog_start(&changelog_priv,
8776                                    CHANGELOG_FLAG_BLOCK |
8777                                    CHANGELOG_FLAG_JOBID |
8778                                    CHANGELOG_FLAG_EXTRA_FLAGS |
8779                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8780                                    mdd, startrec);
8781         if (rc < 0) {
8782                 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8783                         progname, strerror(errno = -rc));
8784                 return rc;
8785         }
8786
8787         rc = llapi_changelog_set_xflags(changelog_priv,
8788                                         CHANGELOG_EXTRA_FLAG_UIDGID |
8789                                         CHANGELOG_EXTRA_FLAG_NID |
8790                                         CHANGELOG_EXTRA_FLAG_OMODE |
8791                                         CHANGELOG_EXTRA_FLAG_XATTR);
8792         if (rc < 0) {
8793                 fprintf(stderr,
8794                         "%s changelog: cannot set xflags for changelog: %s\n",
8795                         progname, strerror(errno = -rc));
8796                 return rc;
8797         }
8798
8799         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8800                 time_t secs;
8801                 struct tm ts;
8802
8803                 if (endrec && rec->cr_index > endrec) {
8804                         llapi_changelog_free(&rec);
8805                         break;
8806                 }
8807                 if (rec->cr_index < startrec) {
8808                         llapi_changelog_free(&rec);
8809                         continue;
8810                 }
8811
8812                 secs = rec->cr_time >> 30;
8813                 gmtime_r(&secs, &ts);
8814                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8815                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8816                        changelog_type2str(rec->cr_type),
8817                        ts.tm_hour, ts.tm_min, ts.tm_sec,
8818                        (int)(rec->cr_time & ((1 << 30) - 1)),
8819                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8820                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8821
8822                 if (rec->cr_flags & CLF_JOBID) {
8823                         struct changelog_ext_jobid *jid =
8824                                 changelog_rec_jobid(rec);
8825
8826                         if (jid->cr_jobid[0] != '\0')
8827                                 printf(" j=%s", jid->cr_jobid);
8828                 }
8829
8830                 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8831                         struct changelog_ext_extra_flags *ef =
8832                                 changelog_rec_extra_flags(rec);
8833
8834                         printf(" ef=0x%llx",
8835                                (unsigned long long)ef->cr_extra_flags);
8836
8837                         if (ef->cr_extra_flags & CLFE_UIDGID) {
8838                                 struct changelog_ext_uidgid *uidgid =
8839                                         changelog_rec_uidgid(rec);
8840
8841                                 printf(" u=%llu:%llu",
8842                                        (unsigned long long)uidgid->cr_uid,
8843                                        (unsigned long long)uidgid->cr_gid);
8844                         }
8845                         if (ef->cr_extra_flags & CLFE_NID) {
8846                                 struct changelog_ext_nid *nid =
8847                                         changelog_rec_nid(rec);
8848
8849                                 printf(" nid=%s",
8850                                        libcfs_nid2str(nid->cr_nid));
8851                         }
8852
8853                         if (ef->cr_extra_flags & CLFE_OPEN) {
8854                                 struct changelog_ext_openmode *omd =
8855                                         changelog_rec_openmode(rec);
8856                                 char mode[] = "---";
8857
8858                                 /* exec mode must be exclusive */
8859                                 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8860                                         mode[2] = 'x';
8861                                 } else {
8862                                         if (omd->cr_openflags & MDS_FMODE_READ)
8863                                                 mode[0] = 'r';
8864                                         if (omd->cr_openflags &
8865                                             (MDS_FMODE_WRITE |
8866                                              MDS_OPEN_TRUNC |
8867                                              MDS_OPEN_APPEND))
8868                                                 mode[1] = 'w';
8869                                 }
8870
8871                                 if (strcmp(mode, "---") != 0)
8872                                         printf(" m=%s", mode);
8873                         }
8874
8875                         if (ef->cr_extra_flags & CLFE_XATTR) {
8876                                 struct changelog_ext_xattr *xattr =
8877                                         changelog_rec_xattr(rec);
8878
8879                                 if (xattr->cr_xattr[0] != '\0')
8880                                         printf(" x=%s", xattr->cr_xattr);
8881                         }
8882                 }
8883
8884                 if (!fid_is_zero(&rec->cr_pfid))
8885                         printf(" p="DFID, PFID(&rec->cr_pfid));
8886                 if (rec->cr_namelen)
8887                         printf(" %.*s", rec->cr_namelen,
8888                                changelog_rec_name(rec));
8889
8890                 if (rec->cr_flags & CLF_RENAME) {
8891                         struct changelog_ext_rename *rnm =
8892                                 changelog_rec_rename(rec);
8893
8894                         if (!fid_is_zero(&rnm->cr_sfid))
8895                                 printf(" s="DFID" sp="DFID" %.*s",
8896                                        PFID(&rnm->cr_sfid),
8897                                        PFID(&rnm->cr_spfid),
8898                                        (int)changelog_rec_snamelen(rec),
8899                                        changelog_rec_sname(rec));
8900                 }
8901                 printf("\n");
8902
8903                 llapi_changelog_free(&rec);
8904         }
8905
8906         llapi_changelog_fini(&changelog_priv);
8907
8908         if (rc < 0)
8909                 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8910                         progname, strerror(errno = -rc));
8911
8912         return (rc == 1 ? 0 : rc);
8913 }
8914
8915 static int lfs_changelog_clear(int argc, char **argv)
8916 {
8917         long long endrec;
8918         int rc;
8919
8920         if (argc != 4)
8921                 return CMD_HELP;
8922
8923         errno = 0;
8924         endrec = strtoll(argv[3], NULL, 10);
8925         if (errno != 0 || endrec < 0) {
8926                 fprintf(stderr,
8927                         "%s: bad endrec '%s'\n",
8928                         argv[0], argv[3]);
8929                 return CMD_HELP;
8930         }
8931
8932         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8933
8934         if (rc == -EINVAL)
8935                 fprintf(stderr, "%s: record out of range: %llu\n",
8936                         argv[0], endrec);
8937         else if (rc == -ENOENT)
8938                 fprintf(stderr, "%s: no changelog user: %s\n",
8939                         argv[0], argv[2]);
8940         else if (rc)
8941                 fprintf(stderr, "%s error: %s\n", argv[0],
8942                         strerror(-rc));
8943
8944         if (rc)
8945                 errno = -rc;
8946
8947         return rc;
8948 }
8949
8950 static void rstripc(char *str, int c)
8951 {
8952         char *end = str + strlen(str);
8953
8954         for (; str < end && end[-1] == c; --end)
8955                 end[-1] = '\0';
8956 }
8957
8958 static int lfs_fid2path(int argc, char **argv)
8959 {
8960         struct option long_opts[] = {
8961                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
8962                 { .val = 'c',   .name = "current",      .has_arg = no_argument },
8963                 { .val = 'c',   .name = "print-link",   .has_arg = no_argument },
8964                 { .val = 'f',   .name = "print-fid",    .has_arg = no_argument },
8965                 { .val = 'l',   .name = "link", .has_arg = required_argument },
8966                 { .name = NULL } };
8967         char short_opts[] = "cfl:pr:";
8968         bool print_link = false;
8969         bool print_fid = false;
8970         bool print_mnt_dir;
8971         char mnt_dir[PATH_MAX] = "";
8972         int mnt_fd = -1;
8973         char *path_or_fsname;
8974         long long recno = -1;
8975         int linkno = -1;
8976         char *endptr = NULL;
8977         int rc = 0;
8978         int c;
8979         int i;
8980
8981         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
8982                 switch (c) {
8983                 case 'c':
8984                         print_link = true;
8985                         break;
8986                 case 'f':
8987                         print_fid = true;
8988                         break;
8989                 case 'l':
8990                         errno = 0;
8991                         linkno = strtol(optarg, &endptr, 10);
8992                         if (errno != 0 || *endptr != '\0' || linkno < 0) {
8993                                 fprintf(stderr,
8994                                         "%s fid2path: invalid linkno '%s'\n",
8995                                         progname, optarg);
8996                                 return CMD_HELP;
8997                         }
8998                         break;
8999                 case 'r':
9000                         /* recno is something to do with changelogs
9001                          * that was never implemented. We just pass it
9002                          * through for the MDT to ignore.
9003                          */
9004                         errno = 0;
9005                         recno = strtoll(optarg, &endptr, 10);
9006                         if (errno != 0 || *endptr != '\0' || recno < 0) {
9007                                 fprintf(stderr,
9008                                         "%s fid2path: invalid recno '%s'\n",
9009                                         progname, optarg);
9010                                 return CMD_HELP;
9011                         }
9012                         break;
9013                 default:
9014                         fprintf(stderr,
9015                                 "%s fid2path: unrecognized option '%s'\n",
9016                                 progname, argv[optind - 1]);
9017                         return CMD_HELP;
9018                 }
9019         }
9020
9021         if (argc - optind < 2) {
9022                 fprintf(stderr,
9023                         "Usage: %s fid2path FSNAME|ROOT FID...\n",
9024                         progname);
9025                 return CMD_HELP;
9026         }
9027
9028         path_or_fsname = argv[optind];
9029
9030         if (*path_or_fsname == '/') {
9031                 print_mnt_dir = true;
9032                 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9033         } else {
9034                 print_mnt_dir = false;
9035                 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9036         }
9037
9038         if (rc < 0) {
9039                 fprintf(stderr,
9040                         "%s fid2path: cannot resolve mount point for '%s': %s\n",
9041                         progname, path_or_fsname, strerror(-rc));
9042                 goto out;
9043         }
9044
9045         mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9046         if (mnt_fd < 0) {
9047                 fprintf(stderr,
9048                         "%s fid2path: cannot open mount point for '%s': %s\n",
9049                         progname, path_or_fsname, strerror(-rc));
9050                 goto out;
9051         }
9052
9053         /* Strip trailing slashes from mnt_dir. */
9054         rstripc(mnt_dir + 1, '/');
9055
9056         for (i = optind + 1; i < argc; i++) {
9057                 const char *fid_str = argv[i];
9058                 struct lu_fid fid;
9059                 int rc2;
9060
9061                 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9062                 if (rc2 < 0) {
9063                         fprintf(stderr,
9064                                 "%s fid2path: invalid FID '%s'\n",
9065                                 progname, fid_str);
9066                         if (rc == 0)
9067                                 rc = rc2;
9068
9069                         continue;
9070                 }
9071
9072                 int linktmp = (linkno >= 0) ? linkno : 0;
9073                 while (1) {
9074                         int oldtmp = linktmp;
9075                         long long rectmp = recno;
9076                         char path_buf[PATH_MAX];
9077
9078                         rc2 = llapi_fid2path_at(mnt_fd, &fid,
9079                                 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9080                         if (rc2 < 0) {
9081                                 fprintf(stderr,
9082                                         "%s fid2path: cannot find %s %s: %s\n",
9083                                         progname, path_or_fsname, fid_str,
9084                                         strerror(-rc2));
9085                                 if (rc == 0)
9086                                         rc = rc2;
9087                                 break;
9088                         }
9089
9090                         if (print_fid)
9091                                 printf("%s ", fid_str);
9092
9093                         if (print_link)
9094                                 printf("%d ", linktmp);
9095
9096                         /* You may think this looks wrong or weird (and it is!)
9097                          * but we are actually trying to preserve the old quirky
9098                          * behaviors (enforced by our old quirky tests!) that
9099                          * make lfs so much fun to work on:
9100                          *
9101                          *   lustre 0x200000007:0x1:0x0 => "/"
9102                          *   /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9103                          *
9104                          * Note that llapi_fid2path() returns "" for the root
9105                          * FID. */
9106
9107                         printf("%s%s%s\n",
9108                                print_mnt_dir ? mnt_dir : "",
9109                                (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9110                                path_buf);
9111
9112                         if (linkno >= 0)
9113                                 /* specified linkno */
9114                                 break;
9115
9116                         if (oldtmp == linktmp)
9117                                 /* no more links */
9118                                 break;
9119                 }
9120         }
9121 out:
9122         if (!(mnt_fd < 0))
9123                 close(mnt_fd);
9124
9125         return rc;
9126 }
9127
9128 static int lfs_path2fid(int argc, char **argv)
9129 {
9130         struct option long_opts[] = {
9131                 { .val = 'p', .name = "parents", .has_arg = no_argument },
9132                 { .name = NULL } };
9133         char            **path;
9134         const char        short_opts[] = "p";
9135         const char       *sep = "";
9136         struct lu_fid     fid;
9137         int               rc = 0;
9138         bool              show_parents = false;
9139
9140         while ((rc = getopt_long(argc, argv, short_opts,
9141                                  long_opts, NULL)) != -1) {
9142                 switch (rc) {
9143                 case 'p':
9144                         show_parents = true;
9145                         break;
9146                 default:
9147                         fprintf(stderr,
9148                                 "%s path2fid: unrecognized option '%s'\n",
9149                                 progname, argv[optind - 1]);
9150                         return CMD_HELP;
9151                 }
9152         }
9153
9154         if (optind > argc - 1) {
9155                 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9156                         progname);
9157                 return CMD_HELP;
9158         } else if (optind < argc - 1) {
9159                 sep = ": ";
9160         }
9161
9162         rc = 0;
9163         for (path = argv + optind; *path != NULL; path++) {
9164                 int err = 0;
9165
9166                 if (!show_parents) {
9167                         err = llapi_path2fid(*path, &fid);
9168                         if (!err)
9169                                 printf("%s%s"DFID"\n",
9170                                        *sep != '\0' ? *path : "", sep,
9171                                        PFID(&fid));
9172                 } else {
9173                         char            name[NAME_MAX + 1];
9174                         unsigned int    linkno = 0;
9175
9176                         while ((err = llapi_path2parent(*path, linkno, &fid,
9177                                                 name, sizeof(name))) == 0) {
9178                                 if (*sep != '\0' && linkno == 0)
9179                                         printf("%s%s", *path, sep);
9180
9181                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9182                                        PFID(&fid), name);
9183                                 linkno++;
9184                         }
9185
9186                         /* err == -ENODATA is end-of-loop */
9187                         if (linkno > 0 && err == -ENODATA) {
9188                                 printf("\n");
9189                                 err = 0;
9190                         }
9191                 }
9192
9193                 if (err) {
9194                         fprintf(stderr,
9195                                 "%s path2fid: cannot get %sfid for '%s': %s\n",
9196                                 progname, show_parents ? "parent " : "", *path,
9197                                 strerror(-err));
9198                         if (rc == 0) {
9199                                 rc = err;
9200                                 errno = -err;
9201                         }
9202                 }
9203         }
9204
9205         return rc;
9206 }
9207
9208 #define MAX_ERRNO       4095
9209 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9210
9211 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9212 {
9213         int rc, rc2, k;
9214
9215         rc = llapi_rmfid(device, fa);
9216         if (rc < 0) {
9217                 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9218                         progname, strerror(-rc));
9219                 return rc;
9220         }
9221
9222         for (k = 0; k < fa->fa_nr; k++) {
9223                 rc2 = (__s32)fa->fa_fids[k].f_ver;
9224                 if (!IS_ERR_VALUE(rc2))
9225                         continue;
9226
9227                 if (rc == 0)
9228                         rc = rc2;
9229
9230                 fa->fa_fids[k].f_ver = 0;
9231                 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9232                         progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9233         }
9234
9235         return rc;
9236 }
9237
9238 static int lfs_rmfid(int argc, char **argv)
9239 {
9240         char *fidstr, *device;
9241         int rc = 0, rc2, nr;
9242         struct fid_array *fa;
9243
9244         if (optind > argc - 1) {
9245                 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9246                 return CMD_HELP;
9247         }
9248
9249         device = argv[optind++];
9250
9251         nr = argc - optind;
9252         fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9253         if (!fa)
9254                 return -ENOMEM;
9255
9256         fa->fa_nr = 0;
9257         rc = 0;
9258         while (optind < argc) {
9259                 int found;
9260
9261                 fidstr = argv[optind++];
9262                 while (*fidstr == '[')
9263                         fidstr++;
9264                 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9265                 if (found != 3) {
9266                         fprintf(stderr, "unrecognized FID: %s\n",
9267                                 argv[optind - 1]);
9268                         exit(1);
9269                 }
9270                 fa->fa_nr++;
9271                 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9272                         /* start another batch */
9273                         rc2 = lfs_rmfid_and_show_errors(device, fa);
9274                         if (rc2 && !rc)
9275                                 rc = rc2;
9276                         fa->fa_nr = 0;
9277                 }
9278         }
9279         if (fa->fa_nr) {
9280                 rc2 = lfs_rmfid_and_show_errors(device, fa);
9281                 if (rc2 && !rc)
9282                         rc = rc2;
9283         }
9284
9285         return rc;
9286 }
9287
9288 static int lfs_data_version(int argc, char **argv)
9289 {
9290         char *path;
9291         __u64 data_version;
9292         int fd;
9293         int rc;
9294         int c;
9295         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9296
9297         if (argc < 2) {
9298                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9299                         progname);
9300                 return CMD_HELP;
9301         }
9302
9303         while ((c = getopt(argc, argv, "nrw")) != -1) {
9304                 switch (c) {
9305                 case 'n':
9306                         data_version_flags = 0;
9307                         break;
9308                 case 'r':
9309                         data_version_flags |= LL_DV_RD_FLUSH;
9310                         break;
9311                 case 'w':
9312                         data_version_flags |= LL_DV_WR_FLUSH;
9313                         break;
9314                 default:
9315                         fprintf(stderr,
9316                                 "%s data_version: unrecognized option '%s'\n",
9317                                 progname, argv[optind - 1]);
9318                         return CMD_HELP;
9319                 }
9320         }
9321         if (optind == argc) {
9322                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9323                         progname);
9324                 return CMD_HELP;
9325         }
9326
9327         path = argv[optind];
9328         fd = open(path, O_RDONLY);
9329         if (fd < 0) {
9330                 rc = -errno;
9331                 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9332                         progname, path, strerror(-rc));
9333                 return rc;
9334         }
9335
9336         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9337         if (rc < 0)
9338                 fprintf(stderr,
9339                         "%s data_version: cannot get version for '%s': %s\n",
9340                         progname, path, strerror(-rc));
9341         else
9342                 printf("%ju" "\n", (uintmax_t)data_version);
9343
9344         close(fd);
9345         return rc;
9346 }
9347
9348 static int lfs_hsm_state(int argc, char **argv)
9349 {
9350         int rc;
9351         int i = 1;
9352         char *path;
9353         struct hsm_user_state hus;
9354
9355         if (argc < 2)
9356                 return CMD_HELP;
9357
9358         do {
9359                 path = argv[i];
9360
9361                 rc = llapi_hsm_state_get(path, &hus);
9362                 if (rc) {
9363                         fprintf(stderr, "can't get hsm state for %s: %s\n",
9364                                 path, strerror(errno = -rc));
9365                         return rc;
9366                 }
9367
9368                 /* Display path name and status flags */
9369                 printf("%s: (0x%08x)", path, hus.hus_states);
9370
9371                 if (hus.hus_states & HS_RELEASED)
9372                         printf(" released");
9373                 if (hus.hus_states & HS_EXISTS)
9374                         printf(" exists");
9375                 if (hus.hus_states & HS_DIRTY)
9376                         printf(" dirty");
9377                 if (hus.hus_states & HS_ARCHIVED)
9378                         printf(" archived");
9379                 /* Display user-settable flags */
9380                 if (hus.hus_states & HS_NORELEASE)
9381                         printf(" never_release");
9382                 if (hus.hus_states & HS_NOARCHIVE)
9383                         printf(" never_archive");
9384                 if (hus.hus_states & HS_LOST)
9385                         printf(" lost_from_hsm");
9386
9387                 if (hus.hus_archive_id != 0)
9388                         printf(", archive_id:%d", hus.hus_archive_id);
9389                 printf("\n");
9390
9391         } while (++i < argc);
9392
9393         return 0;
9394 }
9395
9396 #define LFS_HSM_SET   0
9397 #define LFS_HSM_CLEAR 1
9398
9399 /**
9400  * Generic function to set or clear HSM flags.
9401  * Used by hsm_set and hsm_clear.
9402  *
9403  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9404  */
9405 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9406 {
9407         struct option long_opts[] = {
9408         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
9409         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
9410         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
9411         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
9412         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
9413         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
9414         { .val = 'i',   .name = "archive-id",   .has_arg = required_argument },
9415         { .name = NULL } };
9416         char short_opts[] = "lraAdei:";
9417         __u64 mask = 0;
9418         int c, rc;
9419         char *path;
9420         __u32 archive_id = 0;
9421         char *end = NULL;
9422
9423         if (argc < 3)
9424                 return CMD_HELP;
9425
9426         while ((c = getopt_long(argc, argv, short_opts,
9427                                 long_opts, NULL)) != -1) {
9428                 switch (c) {
9429                 case 'l':
9430                         mask |= HS_LOST;
9431                         break;
9432                 case 'a':
9433                         mask |= HS_NOARCHIVE;
9434                         break;
9435                 case 'A':
9436                         mask |= HS_ARCHIVED;
9437                         break;
9438                 case 'r':
9439                         mask |= HS_NORELEASE;
9440                         break;
9441                 case 'd':
9442                         mask |= HS_DIRTY;
9443                         break;
9444                 case 'e':
9445                         mask |= HS_EXISTS;
9446                         break;
9447                 case 'i':
9448                         errno = 0;
9449                         archive_id = strtol(optarg, &end, 10);
9450                         if (errno != 0 || *end != '\0' || archive_id < 0) {
9451                                 fprintf(stderr, "invalid archive_id: '%s'\n",
9452                                         end);
9453                                 return CMD_HELP;
9454                         }
9455                         break;
9456                 case '?':
9457                         return CMD_HELP;
9458                 default:
9459                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9460                                 argv[0], argv[optind - 1]);
9461                         return CMD_HELP;
9462                 }
9463         }
9464
9465         /* User should have specified a flag */
9466         if (mask == 0)
9467                 return CMD_HELP;
9468
9469         while (optind < argc) {
9470                 path = argv[optind];
9471
9472                 /* If mode == 0, this means we apply the mask. */
9473                 if (mode == LFS_HSM_SET)
9474                         rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9475                 else
9476                         rc = llapi_hsm_state_set(path, 0, mask, 0);
9477
9478                 if (rc != 0) {
9479                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9480                                 path, strerror(errno = -rc));
9481                         return rc;
9482                 }
9483                 optind++;
9484         }
9485
9486         return 0;
9487 }
9488
9489 static int lfs_hsm_action(int argc, char **argv)
9490 {
9491         int                              rc;
9492         int                              i = 1;
9493         char                            *path;
9494         struct hsm_current_action        hca;
9495         struct hsm_extent                he;
9496         enum hsm_user_action             hua;
9497         enum hsm_progress_states         hps;
9498
9499         if (argc < 2)
9500                 return CMD_HELP;
9501
9502         do {
9503                 path = argv[i];
9504
9505                 rc = llapi_hsm_current_action(path, &hca);
9506                 if (rc) {
9507                         fprintf(stderr, "can't get hsm action for %s: %s\n",
9508                                 path, strerror(errno = -rc));
9509                         return rc;
9510                 }
9511                 he = hca.hca_location;
9512                 hua = hca.hca_action;
9513                 hps = hca.hca_state;
9514
9515                 printf("%s: %s", path, hsm_user_action2name(hua));
9516
9517                 /* Skip file without action */
9518                 if (hca.hca_action == HUA_NONE) {
9519                         printf("\n");
9520                         continue;
9521                 }
9522
9523                 printf(" %s ", hsm_progress_state2name(hps));
9524
9525                 if ((hps == HPS_RUNNING) &&
9526                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9527                         printf("(%llu bytes moved)\n",
9528                                (unsigned long long)he.length);
9529                 else if ((he.offset + he.length) == LUSTRE_EOF)
9530                         printf("(from %llu to EOF)\n",
9531                                (unsigned long long)he.offset);
9532                 else
9533                         printf("(from %llu to %llu)\n",
9534                                (unsigned long long)he.offset,
9535                                (unsigned long long)(he.offset + he.length));
9536
9537         } while (++i < argc);
9538
9539         return 0;
9540 }
9541
9542 static int lfs_hsm_set(int argc, char **argv)
9543 {
9544         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9545 }
9546
9547 static int lfs_hsm_clear(int argc, char **argv)
9548 {
9549         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9550 }
9551
9552 /**
9553  * Check file state and return its fid, to be used by lfs_hsm_request().
9554  *
9555  * \param[in]     file      Path to file to check
9556  * \param[in,out] fid       Pointer to allocated lu_fid struct.
9557  * \param[in,out] last_dev  Pointer to last device id used.
9558  *
9559  * \return 0 on success.
9560  */
9561 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9562                                 dev_t *last_dev)
9563 {
9564         struct stat     st;
9565         int             rc;
9566
9567         rc = lstat(file, &st);
9568         if (rc) {
9569                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9570                 return -errno;
9571         }
9572         /*
9573          * Checking for regular file as archiving as posix copytool
9574          * rejects archiving files other than regular files
9575          */
9576         if (!S_ISREG(st.st_mode)) {
9577                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9578                 return CMD_HELP;
9579         }
9580         /* A request should be ... */
9581         if (*last_dev != st.st_dev && *last_dev != 0) {
9582                 fprintf(stderr,
9583                         "All files should be on the same filesystem: %s\n",
9584                         file);
9585                 return -EINVAL;
9586         }
9587         *last_dev = st.st_dev;
9588
9589         rc = llapi_path2fid(file, fid);
9590         if (rc) {
9591                 fprintf(stderr, "Cannot read FID of %s: %s\n",
9592                         file, strerror(-rc));
9593                 return rc;
9594         }
9595         return 0;
9596 }
9597
9598 /* Fill an HSM HUR item with a given file name.
9599  *
9600  * If mntpath is set, then the filename is actually a FID, and no
9601  * lookup on the filesystem will be performed.
9602  *
9603  * \param[in]  hur         the user request to fill
9604  * \param[in]  idx         index of the item inside the HUR to fill
9605  * \param[in]  mntpath     mountpoint of Lustre
9606  * \param[in]  fname       filename (if mtnpath is NULL)
9607  *                         or FID (if mntpath is set)
9608  * \param[in]  last_dev    pointer to last device id used
9609  *
9610  * \retval 0 on success
9611  * \retval CMD_HELP or a negative errno on error
9612  */
9613 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9614                          const char *mntpath, const char *fname,
9615                          dev_t *last_dev)
9616 {
9617         struct hsm_user_item *hui = &hur->hur_user_item[idx];
9618         int rc;
9619
9620         hui->hui_extent.length = -1;
9621
9622         if (mntpath) {
9623                 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9624                 if (rc)
9625                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9626                                 fname);
9627         } else {
9628                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9629         }
9630
9631         if (rc == 0)
9632                 hur->hur_request.hr_itemcount++;
9633
9634         return rc;
9635 }
9636
9637 static int lfs_hsm_request(int argc, char **argv, int action)
9638 {
9639         struct option long_opts[] = {
9640         { .val = 'a',   .name = "archive",      .has_arg = required_argument },
9641         { .val = 'D',   .name = "data",         .has_arg = required_argument },
9642         { .val = 'l',   .name = "filelist",     .has_arg = required_argument },
9643         { .val = 'm',   .name = "mntpath",      .has_arg = required_argument },
9644         { .name = NULL } };
9645         dev_t last_dev = 0;
9646         char short_opts[] = "l:D:a:m:";
9647         struct hsm_user_request *hur, *oldhur;
9648         int c, i;
9649         size_t len;
9650         int nbfile;
9651         char *line = NULL;
9652         char *filelist = NULL;
9653         char fullpath[PATH_MAX];
9654         char *opaque = NULL;
9655         int opaque_len = 0;
9656         int archive_id = 0;
9657         FILE *fp;
9658         int nbfile_alloc = 0;
9659         char *some_file = NULL;
9660         char *mntpath = NULL;
9661         int rc;
9662
9663         if (argc < 2)
9664                 return CMD_HELP;
9665
9666         while ((c = getopt_long(argc, argv, short_opts,
9667                                 long_opts, NULL)) != -1) {
9668                 switch (c) {
9669                 case 'l':
9670                         filelist = optarg;
9671                         break;
9672                 case 'D':
9673                         opaque = optarg;
9674                         break;
9675                 case 'a':
9676                         if (action != HUA_ARCHIVE &&
9677                             action != HUA_REMOVE) {
9678                                 fprintf(stderr,
9679                                         "error: -a is supported only when archiving or removing\n");
9680                                 return CMD_HELP;
9681                         }
9682                         archive_id = atoi(optarg);
9683                         break;
9684                 case 'm':
9685                         if (!some_file) {
9686                                 mntpath = optarg;
9687                                 some_file = strdup(optarg);
9688                         }
9689                         break;
9690                 case '?':
9691                         return CMD_HELP;
9692                 default:
9693                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9694                                 argv[0], argv[optind - 1]);
9695                         return CMD_HELP;
9696                 }
9697         }
9698
9699         /* All remaining args are files, so we have at least nbfile */
9700         nbfile = argc - optind;
9701
9702         if ((nbfile == 0) && (!filelist))
9703                 return CMD_HELP;
9704
9705         if (opaque)
9706                 opaque_len = strlen(opaque);
9707
9708         /*
9709          * Alloc the request structure with enough place to store all files
9710          * from command line.
9711          */
9712         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9713         if (!hur) {
9714                 fprintf(stderr, "Cannot create the request: %s\n",
9715                         strerror(errno));
9716                 return errno;
9717         }
9718         nbfile_alloc = nbfile;
9719
9720         hur->hur_request.hr_action = action;
9721         hur->hur_request.hr_archive_id = archive_id;
9722         hur->hur_request.hr_flags = 0;
9723
9724         /* All remaining args are files, add them */
9725         if (nbfile != 0 && some_file == NULL)
9726                 some_file = strdup(argv[optind]);
9727
9728         for (i = 0; i < nbfile; i++) {
9729                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9730                                    &last_dev);
9731                 if (rc)
9732                         goto out_free;
9733         }
9734
9735         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9736
9737         /* If a filelist was specified, read the filelist from it. */
9738         if (filelist) {
9739                 fp = fopen(filelist, "r");
9740                 if (!fp) {
9741                         fprintf(stderr, "Cannot read the file list %s: %s\n",
9742                                 filelist, strerror(errno));
9743                         rc = -errno;
9744                         goto out_free;
9745                 }
9746
9747                 while ((rc = getline(&line, &len, fp)) != -1) {
9748                         /*
9749                          * If allocated buffer was too small, get something
9750                          * larger
9751                          */
9752                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9753                                 ssize_t size;
9754
9755                                 nbfile_alloc = nbfile_alloc * 2 + 1;
9756                                 oldhur = hur;
9757                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9758                                                                    opaque_len);
9759                                 if (!hur) {
9760                                         fprintf(stderr,
9761                                                 "hsm: cannot allocate the request: %s\n",
9762                                                 strerror(errno));
9763                                         hur = oldhur;
9764                                         rc = -errno;
9765                                         fclose(fp);
9766                                         goto out_free;
9767                                 }
9768                                 size = hur_len(oldhur);
9769                                 if (size < 0) {
9770                                         fprintf(stderr,
9771                                                 "hsm: cannot allocate %u files + %u bytes data\n",
9772                                                 oldhur->hur_request.hr_itemcount,
9773                                                 oldhur->hur_request.hr_data_len);
9774                                         free(hur);
9775                                         hur = oldhur;
9776                                         rc = -E2BIG;
9777                                         fclose(fp);
9778                                         goto out_free;
9779                                 }
9780                                 memcpy(hur, oldhur, size);
9781                                 free(oldhur);
9782                         }
9783
9784                         /* Chop CR */
9785                         if (line[strlen(line) - 1] == '\n')
9786                                 line[strlen(line) - 1] = '\0';
9787
9788                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9789                                            mntpath, line, &last_dev);
9790                         if (rc) {
9791                                 fclose(fp);
9792                                 goto out_free;
9793                         }
9794
9795                         if (!some_file) {
9796                                 some_file = line;
9797                                 line = NULL;
9798                         }
9799                 }
9800
9801                 rc = fclose(fp);
9802                 free(line);
9803         }
9804
9805         /* If a --data was used, add it to the request */
9806         hur->hur_request.hr_data_len = opaque_len;
9807         if (opaque)
9808                 memcpy(hur_data(hur), opaque, opaque_len);
9809
9810         /* Send the HSM request */
9811         if (realpath(some_file, fullpath) == NULL) {
9812                 fprintf(stderr, "Could not find path '%s': %s\n",
9813                         some_file, strerror(errno));
9814         }
9815         rc = llapi_hsm_request(fullpath, hur);
9816         if (rc) {
9817                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9818                         some_file, strerror(-rc));
9819                 goto out_free;
9820         }
9821
9822 out_free:
9823         free(some_file);
9824         free(hur);
9825         return rc;
9826 }
9827
9828 static int lfs_hsm_archive(int argc, char **argv)
9829 {
9830         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9831 }
9832
9833 static int lfs_hsm_restore(int argc, char **argv)
9834 {
9835         return lfs_hsm_request(argc, argv, HUA_RESTORE);
9836 }
9837
9838 static int lfs_hsm_release(int argc, char **argv)
9839 {
9840         return lfs_hsm_request(argc, argv, HUA_RELEASE);
9841 }
9842
9843 static int lfs_hsm_remove(int argc, char **argv)
9844 {
9845         return lfs_hsm_request(argc, argv, HUA_REMOVE);
9846 }
9847
9848 static int lfs_hsm_cancel(int argc, char **argv)
9849 {
9850         return lfs_hsm_request(argc, argv, HUA_CANCEL);
9851 }
9852
9853 static int lfs_swap_layouts(int argc, char **argv)
9854 {
9855         if (argc != 3)
9856                 return CMD_HELP;
9857
9858         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9859                                   SWAP_LAYOUTS_KEEP_MTIME |
9860                                   SWAP_LAYOUTS_KEEP_ATIME);
9861 }
9862
9863 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9864
9865 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9866
9867 int lfs_get_mode(const char *string)
9868 {
9869         enum lock_mode_user mode;
9870
9871         for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9872                 if (lock_mode_names[mode] == NULL)
9873                         continue;
9874                 if (strcmp(string, lock_mode_names[mode]) == 0)
9875                         return mode;
9876         }
9877
9878         return -EINVAL;
9879 }
9880
9881 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9882 {
9883         enum lu_ladvise_type advice;
9884
9885         for (advice = 0;
9886              advice < ARRAY_SIZE(ladvise_names); advice++) {
9887                 if (ladvise_names[advice] == NULL)
9888                         continue;
9889                 if (strcmp(string, ladvise_names[advice]) == 0)
9890                         return advice;
9891         }
9892
9893         return LU_LADVISE_INVALID;
9894 }
9895
9896 static int lfs_ladvise(int argc, char **argv)
9897 {
9898         struct option long_opts[] = {
9899         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
9900         { .val = 'b',   .name = "background",   .has_arg = no_argument },
9901         { .val = 'e',   .name = "end",          .has_arg = required_argument },
9902         { .val = 'l',   .name = "length",       .has_arg = required_argument },
9903         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
9904         { .val = 's',   .name = "start",        .has_arg = required_argument },
9905         { .val = 'u',   .name = "unset",        .has_arg = no_argument },
9906         { .name = NULL } };
9907         char                     short_opts[] = "a:be:l:m:s:u";
9908         int                      c;
9909         int                      rc = 0;
9910         const char              *path;
9911         int                      fd;
9912         struct llapi_lu_ladvise  advice;
9913         enum lu_ladvise_type     advice_type = LU_LADVISE_INVALID;
9914         unsigned long long       start = 0;
9915         unsigned long long       end = LUSTRE_EOF;
9916         unsigned long long       length = 0;
9917         unsigned long long       size_units;
9918         unsigned long long       flags = 0;
9919         int                      mode = 0;
9920
9921         optind = 0;
9922         while ((c = getopt_long(argc, argv, short_opts,
9923                                 long_opts, NULL)) != -1) {
9924                 switch (c) {
9925                 case 'a':
9926                         advice_type = lfs_get_ladvice(optarg);
9927                         if (advice_type == LU_LADVISE_INVALID) {
9928                                 fprintf(stderr,
9929                                         "%s: invalid advice type '%s'\n",
9930                                         argv[0], optarg);
9931                                 fprintf(stderr, "Valid types:");
9932
9933                                 for (advice_type = 0;
9934                                      advice_type < ARRAY_SIZE(ladvise_names);
9935                                      advice_type++) {
9936                                         if (ladvise_names[advice_type] == NULL)
9937                                                 continue;
9938                                         fprintf(stderr, " %s",
9939                                                 ladvise_names[advice_type]);
9940                                 }
9941                                 fprintf(stderr, "\n");
9942
9943                                 return CMD_HELP;
9944                         }
9945                         break;
9946                 case 'b':
9947                         flags |= LF_ASYNC;
9948                         break;
9949                 case 'u':
9950                         flags |= LF_UNSET;
9951                         break;
9952                 case 'e':
9953                         size_units = 1;
9954                         rc = llapi_parse_size(optarg, &end,
9955                                               &size_units, 0);
9956                         if (rc) {
9957                                 fprintf(stderr, "%s: bad end offset '%s'\n",
9958                                         argv[0], optarg);
9959                                 return CMD_HELP;
9960                         }
9961                         break;
9962                 case 's':
9963                         size_units = 1;
9964                         rc = llapi_parse_size(optarg, &start,
9965                                               &size_units, 0);
9966                         if (rc) {
9967                                 fprintf(stderr,
9968                                         "%s: bad start offset '%s'\n",
9969                                         argv[0], optarg);
9970                                 return CMD_HELP;
9971                         }
9972                         break;
9973                 case 'l':
9974                         size_units = 1;
9975                         rc = llapi_parse_size(optarg, &length,
9976                                               &size_units, 0);
9977                         if (rc) {
9978                                 fprintf(stderr, "%s: bad length '%s'\n",
9979                                         argv[0], optarg);
9980                                 return CMD_HELP;
9981                         }
9982                         break;
9983                 case 'm':
9984                         mode = lfs_get_mode(optarg);
9985                         if (mode < 0) {
9986                                 fprintf(stderr,
9987                                         "%s: bad mode '%s', valid modes are READ or WRITE\n",
9988                                         argv[0], optarg);
9989                                 return CMD_HELP;
9990                         }
9991                         break;
9992                 case '?':
9993                         return CMD_HELP;
9994                 default:
9995                         fprintf(stderr, "%s: option '%s' unrecognized\n",
9996                                 argv[0], argv[optind - 1]);
9997                         return CMD_HELP;
9998                 }
9999         }
10000
10001         if (advice_type == LU_LADVISE_INVALID) {
10002                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10003                 fprintf(stderr, "Valid types:");
10004                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10005                      advice_type++) {
10006                         if (ladvise_names[advice_type] == NULL)
10007                                 continue;
10008                         fprintf(stderr, " %s", ladvise_names[advice_type]);
10009                 }
10010                 fprintf(stderr, "\n");
10011                 return CMD_HELP;
10012         }
10013
10014         if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10015                 fprintf(stderr,
10016                         "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10017                         argv[0]);
10018                 return CMD_HELP;
10019         }
10020
10021         if (argc <= optind) {
10022                 fprintf(stderr, "%s: please give one or more file names\n",
10023                         argv[0]);
10024                 return CMD_HELP;
10025         }
10026
10027         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10028                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10029                         argv[0]);
10030                 return CMD_HELP;
10031         }
10032
10033         if (end == LUSTRE_EOF && length != 0)
10034                 end = start + length;
10035
10036         if (end <= start) {
10037                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10038                         argv[0], start, end);
10039                 return CMD_HELP;
10040         }
10041
10042         if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10043                 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10044                         argv[0]);
10045                 return CMD_HELP;
10046         }
10047
10048         if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10049                 fprintf(stderr, "%s: mode is required with lockahead\n",
10050                         argv[0]);
10051                 return CMD_HELP;
10052         }
10053
10054         while (optind < argc) {
10055                 int rc2;
10056
10057                 path = argv[optind++];
10058
10059                 fd = open(path, O_RDONLY);
10060                 if (fd < 0) {
10061                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10062                                 argv[0], path, strerror(errno));
10063                         rc2 = -errno;
10064                         goto next;
10065                 }
10066
10067                 advice.lla_start = start;
10068                 advice.lla_end = end;
10069                 advice.lla_advice = advice_type;
10070                 advice.lla_value1 = 0;
10071                 advice.lla_value2 = 0;
10072                 advice.lla_value3 = 0;
10073                 advice.lla_value4 = 0;
10074                 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10075                         advice.lla_lockahead_mode = mode;
10076                         advice.lla_peradvice_flags = flags;
10077                 }
10078
10079                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10080                 close(fd);
10081                 if (rc2 < 0) {
10082                         fprintf(stderr,
10083                                 "%s: cannot give advice '%s' to file '%s': %s\n",
10084                                 argv[0], ladvise_names[advice_type],
10085                                 path, strerror(errno));
10086
10087                         goto next;
10088                 }
10089
10090 next:
10091                 if (rc == 0 && rc2 < 0)
10092                         rc = rc2;
10093         }
10094         return rc;
10095 }
10096
10097 static const char *const heat_names[] = LU_HEAT_NAMES;
10098
10099 static int lfs_heat_get(int argc, char **argv)
10100 {
10101         struct lu_heat *heat;
10102         int rc = 0, rc2;
10103         char *path;
10104         int fd;
10105         int i;
10106
10107         if (argc <= 1)
10108                 return CMD_HELP;
10109
10110         heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10111         if (!heat) {
10112                 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10113                 return -ENOMEM;
10114         }
10115
10116         optind = 1;
10117         while (optind < argc) {
10118                 path = argv[optind++];
10119
10120                 fd = open(path, O_RDONLY);
10121                 if (fd < 0) {
10122                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10123                                 argv[0], path, strerror(errno));
10124                         rc2 = -errno;
10125                         goto next;
10126                 }
10127
10128                 heat->lh_count = OBD_HEAT_COUNT;
10129                 rc2 = llapi_heat_get(fd, heat);
10130                 close(fd);
10131                 if (rc2 < 0) {
10132                         fprintf(stderr,
10133                                 "%s: cannot get heat of file '%s': %s\n",
10134                                 argv[0], path, strerror(errno));
10135                         goto next;
10136                 }
10137
10138                 printf("flags: %x\n", heat->lh_flags);
10139                 for (i = 0; i < heat->lh_count; i++)
10140                         printf("%s: %llu\n", heat_names[i],
10141                                (unsigned long long)heat->lh_heat[i]);
10142 next:
10143                 if (rc == 0 && rc2 < 0)
10144                         rc = rc2;
10145         }
10146
10147         free(heat);
10148         return rc;
10149 }
10150
10151 static int lfs_heat_set(int argc, char **argv)
10152 {
10153         struct option long_opts[] = {
10154         { .val = 'c',   .name = "clear",        .has_arg = no_argument },
10155         { .val = 'o',   .name = "off",          .has_arg = no_argument },
10156         { .val = 'O',   .name = "on",           .has_arg = no_argument },
10157         { .name = NULL } };
10158         enum lu_heat_flag flags = 0;
10159         int rc = 0, rc2;
10160         char *path;
10161         int fd;
10162         int c;
10163
10164         if (argc <= 1)
10165                 return CMD_HELP;
10166
10167         optind = 0;
10168         while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
10169                 switch (c) {
10170                 case 'c':
10171                         flags |= LU_HEAT_FLAG_CLEAR;
10172                         break;
10173                 case 'o':
10174                         flags |= LU_HEAT_FLAG_CLEAR;
10175                         flags |= LU_HEAT_FLAG_OFF;
10176                         break;
10177                 case 'O':
10178                         flags &= ~LU_HEAT_FLAG_OFF;
10179                         break;
10180                 case '?':
10181                         return CMD_HELP;
10182                 default:
10183                         fprintf(stderr, "%s: option '%s' unrecognized\n",
10184                                 argv[0], argv[optind - 1]);
10185                         return CMD_HELP;
10186                 }
10187         }
10188
10189         if (argc <= optind) {
10190                 fprintf(stderr, "%s: please give one or more file names\n",
10191                         argv[0]);
10192                 return CMD_HELP;
10193         }
10194
10195         while (optind < argc) {
10196                 path = argv[optind++];
10197
10198                 fd = open(path, O_RDONLY);
10199                 if (fd < 0) {
10200                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10201                                 argv[0], path, strerror(errno));
10202                         rc2 = -errno;
10203                         goto next;
10204                 }
10205
10206                 rc2 = llapi_heat_set(fd, flags);
10207                 close(fd);
10208                 if (rc2 < 0) {
10209                         fprintf(stderr,
10210                                 "%s: cannot setflags heat of file '%s': %s\n",
10211                                 argv[0], path, strerror(errno));
10212                         goto next;
10213                 }
10214 next:
10215                 if (rc == 0 && rc2 < 0)
10216                         rc = rc2;
10217         }
10218         return rc;
10219 }
10220
10221 /**
10222  * The input string contains a comma delimited list of component ids and
10223  * ranges, for example "1,2-4,7".
10224  */
10225 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10226 {
10227         bool end_of_loop = false;
10228         char *ptr = NULL;
10229         int nr = 0;
10230         int rc;
10231
10232         if (!arg)
10233                 return -EINVAL;
10234
10235         while (!end_of_loop) {
10236                 int start_index;
10237                 int end_index;
10238                 int i;
10239                 char *endptr = NULL;
10240
10241                 rc = -EINVAL;
10242                 ptr = strchrnul(arg, ',');
10243                 end_of_loop = *ptr == '\0';
10244                 *ptr = '\0';
10245
10246                 start_index = strtol(arg, &endptr, 0);
10247                 if (endptr == arg) /* no data at all */
10248                         break;
10249                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10250                         break;
10251                 if (start_index < 0)
10252                         break;
10253
10254                 end_index = start_index;
10255                 if (*endptr == '-') {
10256                         end_index = strtol(endptr + 1, &endptr, 0);
10257                         if (*endptr != '\0')
10258                                 break;
10259                         if (end_index < start_index)
10260                                 break;
10261                 }
10262
10263                 for (i = start_index; i <= end_index && size > 0; i++) {
10264                         int j;
10265
10266                         /* remove duplicate */
10267                         for (j = 0; j < nr; j++) {
10268                                 if (ids[j] == i)
10269                                         break;
10270                         }
10271                         if (j == nr) { /* no duplicate */
10272                                 ids[nr++] = i;
10273                                 --size;
10274                         }
10275                 }
10276
10277                 if (size == 0 && i < end_index)
10278                         break;
10279
10280                 *ptr = ',';
10281                 arg = ++ptr;
10282                 rc = 0;
10283         }
10284         if (!end_of_loop && ptr)
10285                 *ptr = ',';
10286
10287         return rc < 0 ? rc : nr;
10288 }
10289
10290 /**
10291  * struct verify_mirror_id - Mirror id to be verified.
10292  * @mirror_id:   A specified mirror id.
10293  * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10294  */
10295 struct verify_mirror_id {
10296         __u16 mirror_id;
10297         bool is_valid_id;
10298 };
10299
10300 /**
10301  * compare_mirror_ids() - Compare mirror ids.
10302  * @layout: Mirror component list.
10303  * @cbdata: Callback data in verify_mirror_id structure.
10304  *
10305  * This is a callback function called by llapi_layout_comp_iterate()
10306  * to compare the specified mirror id with the one in the current
10307  * component of @layout. If they are the same, then the specified
10308  * mirror id is valid.
10309  *
10310  * Return: a negative error code on failure or
10311  *         LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10312  *         LLAPI_LAYOUT_ITER_STOP: Stop iteration
10313  */
10314 static inline
10315 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10316 {
10317         struct verify_mirror_id *mirror_id_cbdata =
10318                                  (struct verify_mirror_id *)cbdata;
10319         uint32_t mirror_id;
10320         int rc = 0;
10321
10322         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10323         if (rc < 0) {
10324                 rc = -errno;
10325                 fprintf(stderr,
10326                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
10327                         progname, strerror(errno));
10328                 return rc;
10329         }
10330
10331         if (mirror_id_cbdata->mirror_id == mirror_id) {
10332                 mirror_id_cbdata->is_valid_id = true;
10333                 return LLAPI_LAYOUT_ITER_STOP;
10334         }
10335
10336         return LLAPI_LAYOUT_ITER_CONT;
10337 }
10338
10339 /**
10340  * verify_mirror_ids() - Verify specified mirror ids.
10341  * @fname:      Mirrored file name.
10342  * @mirror_ids: Specified mirror ids to be verified.
10343  * @ids_nr:     Number of specified mirror ids.
10344  *
10345  * This function verifies that specified @mirror_ids are valid
10346  * in the mirrored file @fname.
10347  *
10348  * Return: 0 on success or a negative error code on failure.
10349  */
10350 static inline
10351 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10352 {
10353         struct llapi_layout *layout = NULL;
10354         struct verify_mirror_id mirror_id_cbdata = { 0 };
10355         struct stat stbuf;
10356         uint32_t flr_state;
10357         int i;
10358         int fd;
10359         int rc = 0;
10360         int rc2 = 0;
10361
10362         if (ids_nr <= 0)
10363                 return -EINVAL;
10364
10365         if (stat(fname, &stbuf) < 0) {
10366                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10367                         progname, fname, strerror(errno));
10368                 rc = -errno;
10369                 goto error;
10370         }
10371
10372         if (!S_ISREG(stbuf.st_mode)) {
10373                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10374                         progname, fname);
10375                 rc = -EINVAL;
10376                 goto error;
10377         }
10378
10379         fd = open(fname, O_DIRECT | O_RDONLY);
10380         if (fd < 0) {
10381                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10382                         progname, fname, strerror(errno));
10383                 rc = -errno;
10384                 goto error;
10385         }
10386
10387         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10388         if (rc < 0) {
10389                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10390                         progname, fname, strerror(errno));
10391                 goto close_fd;
10392         }
10393
10394         layout = llapi_layout_get_by_fd(fd, 0);
10395         if (!layout) {
10396                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10397                         progname, fname, strerror(errno));
10398                 rc = -errno;
10399                 llapi_lease_release(fd);
10400                 goto close_fd;
10401         }
10402
10403         rc = llapi_layout_flags_get(layout, &flr_state);
10404         if (rc < 0) {
10405                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10406                         progname, fname, strerror(errno));
10407                 rc = -errno;
10408                 goto free_layout;
10409         }
10410
10411         flr_state &= LCM_FL_FLR_MASK;
10412         switch (flr_state) {
10413         case LCM_FL_NONE:
10414                 rc = -EINVAL;
10415                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10416                         progname, fname, llapi_layout_flags_string(flr_state));
10417                 goto free_layout;
10418         default:
10419                 break;
10420         }
10421
10422         rc2 = 0;
10423         for (i = 0; i < ids_nr; i++) {
10424                 mirror_id_cbdata.mirror_id = mirror_ids[i];
10425                 mirror_id_cbdata.is_valid_id = false;
10426
10427                 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10428                                                &mirror_id_cbdata);
10429                 if (rc < 0) {
10430                         rc = -errno;
10431                         fprintf(stderr,
10432                                 "%s: '%s' failed to verify mirror id: %u.\n",
10433                                 progname, fname, mirror_ids[i]);
10434                         goto free_layout;
10435                 }
10436
10437                 if (!mirror_id_cbdata.is_valid_id) {
10438                         rc2 = -EINVAL;
10439                         fprintf(stderr,
10440                                 "%s: '%s' invalid specified mirror id: %u.\n",
10441                                 progname, fname, mirror_ids[i]);
10442                 }
10443         }
10444         rc = rc2;
10445
10446 free_layout:
10447         llapi_layout_free(layout);
10448         llapi_lease_release(fd);
10449 close_fd:
10450         close(fd);
10451 error:
10452         return rc;
10453 }
10454
10455 static inline
10456 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10457                            __u16 *mirror_ids, int ids_nr)
10458 {
10459         struct llapi_resync_comp comp_array[1024] = { { 0 } };
10460         struct llapi_layout *layout;
10461         struct stat stbuf;
10462         uint32_t flr_state;
10463         uint64_t start;
10464         uint64_t end;
10465         int comp_size = 0;
10466         int idx;
10467         int fd;
10468         int rc;
10469         int rc2;
10470
10471         if (stat(fname, &stbuf) < 0) {
10472                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10473                         progname, fname, strerror(errno));
10474                 rc = -errno;
10475                 goto error;
10476         }
10477         if (!S_ISREG(stbuf.st_mode)) {
10478                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10479                         progname, fname);
10480                 rc = -EINVAL;
10481                 goto error;
10482         }
10483
10484         fd = open(fname, O_DIRECT | O_RDWR);
10485         if (fd < 0) {
10486                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10487                         progname, fname, strerror(errno));
10488                 rc = -errno;
10489                 goto error;
10490         }
10491
10492         layout = llapi_layout_get_by_fd(fd, 0);
10493         if (!layout) {
10494                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10495                         progname, fname, strerror(errno));
10496                 rc = -errno;
10497                 goto close_fd;
10498         }
10499
10500         rc = llapi_layout_flags_get(layout, &flr_state);
10501         if (rc) {
10502                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10503                         progname, fname, strerror(errno));
10504                 rc = -errno;
10505                 goto free_layout;
10506         }
10507
10508         flr_state &= LCM_FL_FLR_MASK;
10509         if (flr_state == LCM_FL_NONE) {
10510                 rc = -EINVAL;
10511                 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10512                         progname, fname);
10513                 goto free_layout;
10514         }
10515
10516         /* get stale component info */
10517         comp_size = llapi_mirror_find_stale(layout, comp_array,
10518                                             ARRAY_SIZE(comp_array),
10519                                             mirror_ids, ids_nr);
10520         if (comp_size <= 0) {
10521                 rc = comp_size;
10522                 goto free_layout;
10523         }
10524
10525         ioc->lil_mode = LL_LEASE_WRLCK;
10526         ioc->lil_flags = LL_LEASE_RESYNC;
10527         rc = llapi_lease_set(fd, ioc);
10528         if (rc < 0) {
10529                 if (rc == -EALREADY)
10530                         rc = 0;
10531                 else
10532                         fprintf(stderr,
10533                             "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10534                                 progname, fname, strerror(-rc));
10535                 goto free_layout;
10536         }
10537
10538         /* get the read range [start, end) */
10539         start = comp_array[0].lrc_start;
10540         end = comp_array[0].lrc_end;
10541         for (idx = 1; idx < comp_size; idx++) {
10542                 if (comp_array[idx].lrc_start < start)
10543                         start = comp_array[idx].lrc_start;
10544                 if (end < comp_array[idx].lrc_end)
10545                         end = comp_array[idx].lrc_end;
10546         }
10547
10548         rc = llapi_lease_check(fd);
10549         if (rc != LL_LEASE_WRLCK) {
10550                 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10551                         progname, fname);
10552                 goto free_layout;
10553         }
10554
10555         rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10556                                       start, end);
10557         if (rc < 0)
10558                 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10559                         progname, fname, strerror(-rc));
10560
10561         /* need to do the lease unlock even resync fails */
10562         ioc->lil_mode = LL_LEASE_UNLCK;
10563         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10564         ioc->lil_count = 0;
10565         for (idx = 0; idx < comp_size; idx++) {
10566                 if (comp_array[idx].lrc_synced) {
10567                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10568                         ioc->lil_count++;
10569                 }
10570         }
10571
10572         rc2 = llapi_lease_set(fd, ioc);
10573         /**
10574          * llapi_lease_set returns lease mode when it request to unlock
10575          * the lease lock.
10576          */
10577         if (rc2 <= 0) {
10578                 /* rc2 == 0 means lost lease lock */
10579                 if (rc2 == 0 && rc == 0)
10580                         rc = -EBUSY;
10581                 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10582                         progname, fname,
10583                         rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10584         }
10585
10586 free_layout:
10587         llapi_layout_free(layout);
10588 close_fd:
10589         close(fd);
10590 error:
10591         return rc;
10592 }
10593
10594 static inline int lfs_mirror_resync(int argc, char **argv)
10595 {
10596         struct ll_ioc_lease *ioc = NULL;
10597         __u16 mirror_ids[128] = { 0 };
10598         int ids_nr = 0;
10599         int c;
10600         int rc = 0;
10601
10602         struct option long_opts[] = {
10603         { .val = 'o',   .name = "only",         .has_arg = required_argument },
10604         { .name = NULL } };
10605
10606         while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10607                 switch (c) {
10608                 case 'o':
10609                         rc = parse_mirror_ids(mirror_ids,
10610                                         sizeof(mirror_ids) / sizeof(__u16),
10611                                         optarg);
10612                         if (rc < 0) {
10613                                 fprintf(stderr,
10614                                         "%s: bad mirror ids '%s'.\n",
10615                                         argv[0], optarg);
10616                                 goto error;
10617                         }
10618                         ids_nr = rc;
10619                         break;
10620                 default:
10621                         fprintf(stderr, "%s: options '%s' unrecognized.\n",
10622                                 argv[0], argv[optind - 1]);
10623                         rc = -EINVAL;
10624                         goto error;
10625                 }
10626         }
10627
10628         if (argc == optind) {
10629                 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10630                 rc = CMD_HELP;
10631                 goto error;
10632         }
10633
10634         if (ids_nr > 0 && argc > optind + 1) {
10635                 fprintf(stderr,
10636                     "%s: option '--only' cannot be used upon multiple files.\n",
10637                         argv[0]);
10638                 rc = CMD_HELP;
10639                 goto error;
10640         }
10641
10642         if (ids_nr > 0) {
10643                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10644                 if (rc < 0)
10645                         goto error;
10646         }
10647
10648         /* set the lease on the file */
10649         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10650         if (!ioc) {
10651                 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10652                         argv[0], strerror(errno));
10653                 rc = -errno;
10654                 goto error;
10655         }
10656
10657         for (; optind < argc; optind++) {
10658                 rc = lfs_mirror_resync_file(argv[optind], ioc,
10659                                             mirror_ids, ids_nr);
10660                 /* ignore previous file's error, continue with next file */
10661
10662                 /* reset ioc */
10663                 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10664         }
10665
10666         free(ioc);
10667 error:
10668         return rc;
10669 }
10670
10671 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10672 {
10673         struct llapi_layout *layout;
10674         int rc;
10675
10676         layout = llapi_layout_get_by_fd(fd, 0);
10677         if (!layout) {
10678                 fprintf(stderr, "could not get layout.\n");
10679                 return  -EINVAL;
10680         }
10681
10682         rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10683         if (rc < 0) {
10684                 fprintf(stderr, "failed to iterate layout\n");
10685                 llapi_layout_free(layout);
10686
10687                 return rc;
10688         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10689                 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10690                 llapi_layout_free(layout);
10691
10692                 return -EINVAL;
10693         }
10694         llapi_layout_free(layout);
10695
10696         return 0;
10697 }
10698
10699 /**
10700  * Check whether two files are the same file
10701  * \retval      0  same file
10702  * \retval      1  not the same file
10703  * \retval      <0 error code
10704  */
10705 static inline int check_same_file(int fd, const char *f2)
10706 {
10707         struct stat stbuf1;
10708         struct stat stbuf2;
10709
10710         if (fstat(fd, &stbuf1) < 0)
10711                 return -errno;
10712
10713         if (stat(f2, &stbuf2) < 0)
10714                 return 1;
10715
10716         if (stbuf1.st_rdev == stbuf2.st_rdev &&
10717             stbuf1.st_ino == stbuf2.st_ino)
10718                 return 0;
10719
10720         return 1;
10721 }
10722
10723 static inline int lfs_mirror_read(int argc, char **argv)
10724 {
10725         int rc = CMD_HELP;
10726         __u16 mirror_id = 0;
10727         const char *outfile = NULL;
10728         char *fname;
10729         int fd = 0;
10730         int outfd;
10731         int c;
10732         void *buf;
10733         const size_t buflen = 4 << 20;
10734         off_t pos;
10735         struct option long_opts[] = {
10736         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
10737         { .val = 'o',   .name = "outfile",      .has_arg = required_argument },
10738         { .name = NULL } };
10739
10740         while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10741                 char *end;
10742
10743                 switch (c) {
10744                 case 'N': {
10745                         unsigned long int id;
10746
10747                         errno = 0;
10748                         id = strtoul(optarg, &end, 0);
10749                         if (errno != 0 || *end != '\0' || id == 0 ||
10750                             id > UINT16_MAX) {
10751                                 fprintf(stderr,
10752                                         "%s %s: invalid mirror ID '%s'\n",
10753                                         progname, argv[0], optarg);
10754                                 return rc;
10755                         }
10756
10757                         mirror_id = (__u16)id;
10758                         break;
10759                 }
10760                 case 'o':
10761                         outfile = optarg;
10762                         break;
10763                 default:
10764                         fprintf(stderr, "%s: option '%s' unrecognized.\n",
10765                                 progname, argv[optind - 1]);
10766                         return -EINVAL;
10767                 }
10768         }
10769
10770         if (argc == optind) {
10771                 fprintf(stderr, "%s %s: no mirrored file provided\n",
10772                         progname, argv[0]);
10773                 return rc;
10774         } else if (argc > optind + 1) {
10775                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10776                 return rc;
10777         }
10778
10779         if (mirror_id == 0) {
10780                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10781                         progname, argv[0]);
10782                 return rc;
10783         }
10784
10785         /* open mirror file */
10786         fname = argv[optind];
10787         fd = open(fname, O_DIRECT | O_RDONLY);
10788         if (fd < 0) {
10789                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10790                         progname, argv[0], fname, strerror(errno));
10791                 return rc;
10792         }
10793
10794         /* verify mirror id */
10795         rc = verify_mirror_id_by_fd(fd, mirror_id);
10796         if (rc) {
10797                 fprintf(stderr,
10798                         "%s %s: cannot find mirror with ID %u in '%s'\n",
10799                         progname, argv[0], mirror_id, fname);
10800                 goto close_fd;
10801         }
10802
10803         /* open output file - O_EXCL ensures output is not the same as input */
10804         if (outfile) {
10805                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10806                 if (outfd < 0) {
10807                         fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10808                                 progname, argv[0], outfile, strerror(errno));
10809                         rc = -errno;
10810                         goto close_fd;
10811                 }
10812         } else {
10813                 outfd = STDOUT_FILENO;
10814         }
10815
10816         /* allocate buffer */
10817         rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10818         if (rc) {
10819                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10820                                 progname, argv[0], rc);
10821                 goto close_outfd;
10822         }
10823
10824         pos = 0;
10825         while (1) {
10826                 ssize_t bytes_read;
10827                 ssize_t written = 0;
10828
10829                 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10830                 if (bytes_read < 0) {
10831                         rc = bytes_read;
10832                         fprintf(stderr,
10833                                 "%s %s: fail to read data from mirror %u: %s\n",
10834                                 progname, argv[0], mirror_id, strerror(-rc));
10835                         goto free_buf;
10836                 }
10837
10838                 /* EOF reached */
10839                 if (bytes_read == 0)
10840                         break;
10841
10842                 while (written < bytes_read) {
10843                         ssize_t written2;
10844
10845                         written2 = write(outfd, buf + written,
10846                                          bytes_read - written);
10847                         if (written2 < 0) {
10848                                 fprintf(stderr,
10849                                         "%s %s: fail to write %s: %s\n",
10850                                         progname, argv[0], outfile ? : "STDOUT",
10851                                         strerror(errno));
10852                                 rc = -errno;
10853                                 goto free_buf;
10854                         }
10855                         written += written2;
10856                 }
10857
10858                 if (written != bytes_read) {
10859                         fprintf(stderr,
10860                 "%s %s: written %ld bytes does not match with %ld read.\n",
10861                                 progname, argv[0], written, bytes_read);
10862                         rc = -EIO;
10863                         goto free_buf;
10864                 }
10865
10866                 pos += bytes_read;
10867         }
10868
10869         fsync(outfd);
10870         rc = 0;
10871
10872 free_buf:
10873         free(buf);
10874 close_outfd:
10875         if (outfile)
10876                 close(outfd);
10877 close_fd:
10878         close(fd);
10879
10880         return rc;
10881 }
10882
10883 static inline int lfs_mirror_write(int argc, char **argv)
10884 {
10885         int rc = CMD_HELP;
10886         __u16 mirror_id = 0;
10887         const char *inputfile = NULL;
10888         char *fname;
10889         int fd = 0;
10890         int inputfd;
10891         int c;
10892         void *buf;
10893         const size_t buflen = 4 << 20;
10894         off_t pos;
10895         size_t page_size = sysconf(_SC_PAGESIZE);
10896         struct ll_ioc_lease_id ioc;
10897
10898         struct option long_opts[] = {
10899         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
10900         { .val = 'i',   .name = "inputfile",    .has_arg = required_argument },
10901         { .name = NULL } };
10902
10903         while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10904                 char *end;
10905
10906                 switch (c) {
10907                 case 'N': {
10908                         unsigned long int id;
10909
10910                         errno = 0;
10911                         id = strtoul(optarg, &end, 0);
10912                         if (errno != 0 || *end != '\0' || id == 0 ||
10913                             id > UINT16_MAX) {
10914                                 fprintf(stderr,
10915                                         "%s %s: invalid mirror ID '%s'\n",
10916                                         progname, argv[0], optarg);
10917                                 return rc;
10918                         }
10919
10920                         mirror_id = (__u16)id;
10921                         break;
10922                 }
10923                 case 'i':
10924                         inputfile = optarg;
10925                         break;
10926                 default:
10927                         fprintf(stderr, "%s: option '%s' unrecognized\n",
10928                                 progname, argv[optind - 1]);
10929                         return -EINVAL;
10930                 }
10931         }
10932
10933         if (argc == optind) {
10934                 fprintf(stderr, "%s %s: no mirrored file provided\n",
10935                         progname, argv[0]);
10936                 return rc;
10937         } else if (argc > optind + 1) {
10938                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10939                 return rc;
10940         }
10941
10942         if (mirror_id == 0) {
10943                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10944                         progname, argv[0]);
10945                 return rc;
10946         }
10947
10948         /* open mirror file */
10949         fname = argv[optind];
10950         fd = open(fname, O_DIRECT | O_WRONLY);
10951         if (fd < 0) {
10952                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10953                         progname, argv[0], fname, strerror(errno));
10954                 return rc;
10955         }
10956
10957         /* verify mirror id */
10958         rc = verify_mirror_id_by_fd(fd, mirror_id);
10959         if (rc) {
10960                 fprintf(stderr,
10961                         "%s %s: cannot find mirror with ID %u in '%s'\n",
10962                         progname, argv[0], mirror_id, fname);
10963                 goto close_fd;
10964         }
10965
10966         /* open input file */
10967         if (inputfile) {
10968                 rc = check_same_file(fd, inputfile);
10969                 if (rc == 0) {
10970                         fprintf(stderr,
10971                         "%s %s: input file cannot be the mirrored file\n",
10972                                 progname, argv[0]);
10973                         goto close_fd;
10974                 }
10975                 if (rc < 0)
10976                         goto close_fd;
10977
10978                 inputfd = open(inputfile, O_RDONLY, 0644);
10979                 if (inputfd < 0) {
10980                         fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
10981                                 progname, argv[0], inputfile, strerror(errno));
10982                         rc = -errno;
10983                         goto close_fd;
10984                 }
10985         } else {
10986                 inputfd = STDIN_FILENO;
10987         }
10988
10989         /* allocate buffer */
10990         rc = posix_memalign(&buf, page_size, buflen);
10991         if (rc) {
10992                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10993                         progname, argv[0], rc);
10994                 goto close_inputfd;
10995         }
10996
10997         /* prepare target mirror components instantiation */
10998         ioc.lil_mode = LL_LEASE_WRLCK;
10999         ioc.lil_flags = LL_LEASE_RESYNC;
11000         ioc.lil_mirror_id = mirror_id;
11001         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11002         if (rc < 0) {
11003                 fprintf(stderr,
11004                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11005                         progname, argv[0], fname, strerror(errno));
11006                 goto free_buf;
11007         }
11008
11009         pos = 0;
11010         while (1) {
11011                 ssize_t bytes_read;
11012                 ssize_t written;
11013                 size_t to_write;
11014
11015                 rc = llapi_lease_check(fd);
11016                 if (rc != LL_LEASE_WRLCK) {
11017                         fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11018                                 progname, argv[0], fname);
11019                         goto free_buf;
11020                 }
11021
11022                 bytes_read = read(inputfd, buf, buflen);
11023                 if (bytes_read < 0) {
11024                         rc = bytes_read;
11025                         fprintf(stderr,
11026                                 "%s %s: fail to read data from '%s': %s\n",
11027                                 progname, argv[0], inputfile ? : "STDIN",
11028                                 strerror(errno));
11029                         rc = -errno;
11030                         goto free_buf;
11031                 }
11032
11033                 /* EOF reached */
11034                 if (bytes_read == 0)
11035                         break;
11036
11037                 /* round up to page align to make direct IO happy. */
11038                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11039
11040                 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11041                                              pos);
11042                 if (written < 0) {
11043                         rc = written;
11044                         fprintf(stderr,
11045                               "%s %s: fail to write to mirror %u: %s\n",
11046                                 progname, argv[0], mirror_id,
11047                                 strerror(-rc));
11048                         goto free_buf;
11049                 }
11050
11051                 pos += bytes_read;
11052         }
11053
11054         if (pos & (page_size - 1)) {
11055                 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11056                 if (rc < 0)
11057                         goto free_buf;
11058         }
11059
11060         ioc.lil_mode = LL_LEASE_UNLCK;
11061         ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11062         ioc.lil_count = 0;
11063         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11064         if (rc <= 0) {
11065                 if (rc == 0)
11066                         rc = -EBUSY;
11067                 fprintf(stderr,
11068                         "%s %s: release lease lock of '%s' failed: %s\n",
11069                         progname, argv[0], fname, strerror(errno));
11070                 goto free_buf;
11071         }
11072
11073         rc = 0;
11074
11075 free_buf:
11076         free(buf);
11077 close_inputfd:
11078         if (inputfile)
11079                 close(inputfd);
11080 close_fd:
11081         close(fd);
11082
11083         return rc;
11084 }
11085
11086 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11087 {
11088         struct llapi_layout *layout;
11089         struct collect_ids_data cid = { .cid_ids = ids,
11090                                         .cid_count = 0,
11091                                         .cid_exclude = exclude_id, };
11092         int rc;
11093
11094         layout = llapi_layout_get_by_fd(fd, 0);
11095         if (!layout) {
11096                 fprintf(stderr, "could not get layout\n");
11097                 return -EINVAL;
11098         }
11099
11100         rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11101         if (rc < 0) {
11102                 fprintf(stderr, "failed to iterate layout\n");
11103                 llapi_layout_free(layout);
11104
11105                 return rc;
11106         }
11107         llapi_layout_free(layout);
11108
11109         return cid.cid_count;
11110 }
11111
11112 static inline int lfs_mirror_copy(int argc, char **argv)
11113 {
11114         int rc = CMD_HELP;
11115         __u16 read_mirror_id = 0;
11116         __u16 ids[128] = { 0 };
11117         int count = 0;
11118         struct llapi_layout *layout = NULL;
11119         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11120         int comp_size = 0;
11121         char *fname;
11122         int fd = 0;
11123         int c;
11124         int i;
11125         ssize_t copied;
11126         struct ll_ioc_lease *ioc = NULL;
11127         struct ll_ioc_lease_id *resync_ioc;
11128
11129         struct option long_opts[] = {
11130         { .val = 'i',   .name = "read-mirror",  .has_arg = required_argument },
11131         { .val = 'o',   .name = "write-mirror", .has_arg = required_argument },
11132         { .name = NULL } };
11133
11134         while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
11135                 char *end;
11136
11137                 switch (c) {
11138                 case 'i': {
11139                         unsigned long int id;
11140
11141                         errno = 0;
11142                         id = strtoul(optarg, &end, 0);
11143                         if (errno != 0 || *end != '\0' || id == 0 ||
11144                             id > UINT16_MAX) {
11145                                 fprintf(stderr,
11146                                         "%s %s: invalid read mirror ID '%s'\n",
11147                                         progname, argv[0], optarg);
11148                                 return rc;
11149                         }
11150
11151                         read_mirror_id = (__u16)id;
11152                         break;
11153                 }
11154                 case 'o':
11155                         if (!strcmp(optarg, "-1")) {
11156                                 /* specify all other mirrors */
11157                                 ids[0] = (__u16)-1;
11158                                 count = 1;
11159                         } else {
11160                                 count = parse_mirror_ids((__u16 *)ids,
11161                                                          ARRAY_SIZE(ids),
11162                                                          optarg);
11163                                 if (count < 0)
11164                                         return rc;
11165                         }
11166                         break;
11167                 default:
11168                         fprintf(stderr, "%s: option '%s' unrecognized\n",
11169                                 progname, argv[optind - 1]);
11170                         return -EINVAL;
11171                 }
11172         }
11173
11174         if (argc == optind) {
11175                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11176                         progname, argv[0]);
11177                 return rc;
11178         } else if (argc > optind + 1) {
11179                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11180                 return rc;
11181         }
11182
11183         if (read_mirror_id == 0) {
11184                 fprintf(stderr,
11185                         "%s %s: no valid read mirror ID %d is provided\n",
11186                         progname, argv[0], read_mirror_id);
11187                 return rc;
11188         }
11189
11190         if (count == 0) {
11191                 fprintf(stderr,
11192                         "%s %s: no write mirror ID is provided\n",
11193                         progname, argv[0]);
11194                 return rc;
11195         }
11196
11197         for (i = 0; i < count; i++) {
11198                 if (read_mirror_id == ids[i]) {
11199                         fprintf(stderr,
11200                         "%s %s: read and write mirror ID cannot be the same\n",
11201                                 progname, argv[0]);
11202                         return rc;
11203                 }
11204         }
11205
11206         /* open mirror file */
11207         fname = argv[optind];
11208
11209         fd = open(fname, O_DIRECT | O_RDWR);
11210         if (fd < 0) {
11211                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11212                         progname, argv[0], fname, strerror(errno));
11213                 return rc;
11214         }
11215
11216         /* write to all other mirrors */
11217         if (ids[0] == (__u16)-1) {
11218                 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11219                 if (count <= 0) {
11220                         rc = count;
11221                         fprintf(stderr,
11222                         "%s %s: failed to get other mirror ids in '%s': %d\n",
11223                                 progname, argv[0], fname, rc);
11224                         goto close_fd;
11225                 }
11226         }
11227
11228         /* verify mirror id */
11229         rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11230         if (rc) {
11231                 fprintf(stderr,
11232                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11233                         progname, argv[0], read_mirror_id, fname);
11234                 goto close_fd;
11235         }
11236
11237         for (i = 0; i < count; i++) {
11238                 rc = verify_mirror_id_by_fd(fd, ids[i]);
11239                 if (rc) {
11240                         fprintf(stderr,
11241                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11242                                 progname, argv[0], ids[i], fname);
11243                         goto close_fd;
11244                 }
11245         }
11246
11247         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11248         if (!ioc) {
11249                 fprintf(stderr,
11250                         "%s %s: cannot alloc comp id array for ioc: %s\n",
11251                         progname, argv[0], strerror(errno));
11252                 rc = -errno;
11253                 goto close_fd;
11254         }
11255
11256         /* get stale component info */
11257         layout = llapi_layout_get_by_fd(fd, 0);
11258         if (!layout) {
11259                 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11260                         progname, argv[0], fname, strerror(errno));
11261                 rc = -errno;
11262                 goto free_ioc;
11263         }
11264         comp_size = llapi_mirror_find_stale(layout, comp_array,
11265                                             ARRAY_SIZE(comp_array),
11266                                             ids, count);
11267         llapi_layout_free(layout);
11268         if (comp_size < 0) {
11269                 rc = comp_size;
11270                 goto free_ioc;
11271         }
11272
11273         /* prepare target mirror components instantiation */
11274         resync_ioc = (struct ll_ioc_lease_id *)ioc;
11275         resync_ioc->lil_mode = LL_LEASE_WRLCK;
11276         resync_ioc->lil_flags = LL_LEASE_RESYNC;
11277         if (count == 1)
11278                 resync_ioc->lil_mirror_id = ids[0];
11279         else
11280                 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11281         rc = llapi_lease_set(fd, ioc);
11282         if (rc < 0) {
11283                 fprintf(stderr,
11284                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11285                         progname, argv[0], fname, strerror(errno));
11286                 goto free_ioc;
11287         }
11288
11289         copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11290         if (copied < 0) {
11291                 rc = copied;
11292                 fprintf(stderr, "%s %s: copy error: %d\n",
11293                         progname, argv[0], rc);
11294                 goto free_ioc;
11295         }
11296
11297         fprintf(stdout, "mirror copied successfully: ");
11298         for (i = 0; i < copied; i++)
11299                 fprintf(stdout, "%d ", ids[i]);
11300         fprintf(stdout, "\n");
11301
11302         ioc->lil_mode = LL_LEASE_UNLCK;
11303         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11304         ioc->lil_count = 0;
11305         for (i = 0; i < comp_size; i++) {
11306                 int j;
11307
11308                 for (j = 0; j < copied; j++) {
11309                         if (comp_array[i].lrc_mirror_id != ids[j])
11310                                 continue;
11311
11312                         ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11313                         ioc->lil_count++;
11314                 }
11315         }
11316         rc = llapi_lease_set(fd, ioc);
11317         if (rc <= 0) {
11318                 if (rc == 0)
11319                         rc = -EBUSY;
11320                 fprintf(stderr,
11321                         "%s %s: release lease lock of '%s' failed: %s\n",
11322                         progname, argv[0], fname, strerror(errno));
11323                 goto free_ioc;
11324         }
11325
11326         rc = 0;
11327
11328 free_ioc:
11329         free(ioc);
11330 close_fd:
11331         close(fd);
11332
11333         return rc;
11334 }
11335
11336 /**
11337  * struct verify_chunk - Mirror chunk to be verified.
11338  * @chunk:        [start, end) of the chunk.
11339  * @mirror_count: Number of mirror ids in @mirror_id array.
11340  * @mirror_id:    Array of valid mirror ids that cover the chunk.
11341  */
11342 struct verify_chunk {
11343         struct lu_extent chunk;
11344         unsigned int mirror_count;
11345         __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11346 };
11347
11348 /**
11349  * print_chunks() - Print chunk information.
11350  * @fname:       Mirrored file name.
11351  * @chunks:      Array of chunks.
11352  * @chunk_count: Number of chunks in @chunks array.
11353  *
11354  * This function prints [start, end) of each chunk in @chunks
11355  * for mirrored file @fname, and also prints the valid mirror ids
11356  * that cover the chunk.
11357  *
11358  * Return: void.
11359  */
11360 static inline
11361 void print_chunks(const char *fname, struct verify_chunk *chunks,
11362                   int chunk_count)
11363 {
11364         int i;
11365         int j;
11366
11367         fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11368         for (i = 0; i < chunk_count; i++) {
11369                 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11370
11371                 if (chunks[i].mirror_count == 0)
11372                         fprintf(stdout, "\t[");
11373                 else {
11374                         fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11375                         for (j = 1; j < chunks[i].mirror_count; j++)
11376                                 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11377                 }
11378                 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11379         }
11380         fprintf(stdout, "\n");
11381 }
11382
11383 /**
11384  * print_checksums() - Print CRC-32 checksum values.
11385  * @chunk: A chunk and its corresponding valid mirror ids.
11386  * @crc:   CRC-32 checksum values on the chunk for each valid mirror.
11387  *
11388  * This function prints CRC-32 checksum values on @chunk for
11389  * each valid mirror that covers it.
11390  *
11391  * Return: void.
11392  */
11393 static inline
11394 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11395 {
11396         int i;
11397
11398         fprintf(stdout,
11399                 "CRC-32 checksum value for chunk "DEXT":\n",
11400                 PEXT(&chunk->chunk));
11401         for (i = 0; i < chunk->mirror_count; i++)
11402                 fprintf(stdout, "Mirror %u:\t%#lx\n",
11403                         chunk->mirror_id[i], crc[i]);
11404         fprintf(stdout, "\n");
11405 }
11406
11407 /**
11408  * filter_mirror_id() - Filter specified mirror ids.
11409  * @chunks:      Array of chunks.
11410  * @chunk_count: Number of chunks in @chunks array.
11411  * @mirror_ids:  Specified mirror ids to be verified.
11412  * @ids_nr:      Number of specified mirror ids.
11413  *
11414  * This function scans valid mirror ids that cover each chunk in @chunks
11415  * and filters specified mirror ids.
11416  *
11417  * Return: void.
11418  */
11419 static inline
11420 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11421                       __u16 *mirror_ids, int ids_nr)
11422 {
11423         int i;
11424         int j;
11425         int k;
11426         __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11427         unsigned int valid_count = 0;
11428
11429         for (i = 0; i < chunk_count; i++) {
11430                 if (chunks[i].mirror_count == 0)
11431                         continue;
11432
11433                 valid_count = 0;
11434                 for (j = 0; j < ids_nr; j++) {
11435                         for (k = 0; k < chunks[i].mirror_count; k++) {
11436                                 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11437                                         valid_id[valid_count] = mirror_ids[j];
11438                                         valid_count++;
11439                                         break;
11440                                 }
11441                         }
11442                 }
11443
11444                 memcpy(chunks[i].mirror_id, valid_id,
11445                        sizeof(__u16) * valid_count);
11446                 chunks[i].mirror_count = valid_count;
11447         }
11448 }
11449
11450 /**
11451  * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11452  * @layout:      Mirror component list.
11453  * @chunks:      Array of chunks.
11454  * @chunks_size: Array size of @chunks.
11455  *
11456  * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11457  * to find out chunk segments and store them in @chunks array.
11458  *
11459  * The @mirror_id array in each element of @chunks will store the valid
11460  * mirror ids that cover the chunk. If a mirror component covering the
11461  * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11462  * will not be stored into the @mirror_id array, and the chunk for that
11463  * mirror will not be verified.
11464  *
11465  * The @mirror_count in each element of @chunks will store the number of
11466  * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11467  * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11468  * indicates the chunk is valid in only one mirror. In both cases, the
11469  * chunk will not be verified.
11470  *
11471  * Here is an example:
11472  *
11473  *  0      1M     2M     3M     4M           EOF
11474  *  +------+-------------+--------------------+
11475  *  |      |             |      S             |       mirror1
11476  *  +------+------+------+------+-------------+
11477  *  |             |   S  |   S  |             |       mirror2
11478  *  +-------------+------+------+-------------+
11479  *
11480  * prepared @chunks array will contain 5 elements:
11481  * (([0, 1M), [1, 2], 2),
11482  *  ([1M, 2M), [1, 2], 2),
11483  *  ([2M, 3M), [1], 1),
11484  *  ([3M, 4M], [], 0),
11485  *  ([4M, EOF), [2], 1))
11486  *
11487  * Return: the actual array size of @chunks on success
11488  *         or a negative error code on failure.
11489  */
11490 static inline
11491 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11492                              struct verify_chunk *chunks,
11493                              size_t chunks_size)
11494 {
11495         uint64_t start;
11496         uint64_t end;
11497         uint32_t mirror_id;
11498         uint32_t flags;
11499         int idx = 0;
11500         int i = 0;
11501         int rc = 0;
11502
11503         memset(chunks, 0, sizeof(*chunks) * chunks_size);
11504
11505         while (1) {
11506                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11507                 if (rc < 0) {
11508                         fprintf(stderr,
11509                                 "%s: move to the first layout component: %s.\n",
11510                                 progname, strerror(errno));
11511                         goto error;
11512                 }
11513
11514                 i = 0;
11515                 rc = 0;
11516                 chunks[idx].chunk.e_end = LUSTRE_EOF;
11517                 while (rc == 0) {
11518                         rc = llapi_layout_comp_extent_get(layout, &start, &end);
11519                         if (rc < 0) {
11520                                 fprintf(stderr,
11521                                         "%s: llapi_layout_comp_extent_get failed: %s.\n",
11522                                         progname, strerror(errno));
11523                                 goto error;
11524                         }
11525
11526                         if (start > chunks[idx].chunk.e_start ||
11527                             end <= chunks[idx].chunk.e_start)
11528                                 goto next;
11529
11530                         if (end < chunks[idx].chunk.e_end)
11531                                 chunks[idx].chunk.e_end = end;
11532
11533                         rc = llapi_layout_comp_flags_get(layout, &flags);
11534                         if (rc < 0) {
11535                                 fprintf(stderr,
11536                                         "%s: llapi_layout_comp_flags_get failed: %s.\n",
11537                                         progname, strerror(errno));
11538                                 goto error;
11539                         }
11540
11541                         if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11542                                 goto next;
11543
11544                         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11545                         if (rc < 0) {
11546                                 fprintf(stderr,
11547                                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
11548                                         progname, strerror(errno));
11549                                 goto error;
11550                         }
11551
11552                         chunks[idx].mirror_id[i] = mirror_id;
11553                         i++;
11554                         if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11555                                 fprintf(stderr,
11556                                         "%s: mirror_id array is too small.\n",
11557                                         progname);
11558                                 rc = -EINVAL;
11559                                 goto error;
11560                         }
11561
11562 next:
11563                         rc = llapi_layout_comp_use(layout,
11564                                                    LLAPI_LAYOUT_COMP_USE_NEXT);
11565                         if (rc < 0) {
11566                                 fprintf(stderr,
11567                                         "%s: move to the next layout component: %s.\n",
11568                                         progname, strerror(errno));
11569                                 goto error;
11570                         }
11571                 } /* loop through all components */
11572
11573                 chunks[idx].mirror_count = i;
11574
11575                 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11576                         break;
11577
11578                 idx++;
11579                 if (idx >= chunks_size) {
11580                         fprintf(stderr, "%s: chunks array is too small.\n",
11581                                 progname);
11582                         rc = -EINVAL;
11583                         goto error;
11584                 }
11585
11586                 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11587         }
11588
11589 error:
11590         return rc < 0 ? rc : idx + 1;
11591 }
11592
11593 /**
11594  * lfs_mirror_verify_chunk() - Verify a chunk.
11595  * @fd:        File descriptor of the mirrored file.
11596  * @file_size: Size of the mirrored file.
11597  * @chunk:     A chunk and its corresponding valid mirror ids.
11598  * @verbose:   Verbose mode.
11599  *
11600  * This function verifies a @chunk contains exactly the same data
11601  * ammong the mirrors that cover it.
11602  *
11603  * If @verbose is specified, then the function will print where the
11604  * differences are if the data do not match. Otherwise, it will
11605  * just return an error in that case.
11606  *
11607  * Return: 0 on success or a negative error code on failure.
11608  */
11609 static inline
11610 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11611                             struct verify_chunk *chunk, int verbose)
11612 {
11613         const size_t buflen = 4 * 1024 * 1024; /* 4M */
11614         void *buf;
11615         size_t page_size = sysconf(_SC_PAGESIZE);
11616         ssize_t bytes_read;
11617         ssize_t bytes_done;
11618         size_t count;
11619         off_t pos;
11620         unsigned long crc;
11621         unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11622         int i;
11623         int rc = 0;
11624
11625         if (file_size == 0)
11626                 return 0;
11627
11628         rc = posix_memalign(&buf, page_size, buflen);
11629         if (rc) /* error code is returned directly */
11630                 return -rc;
11631
11632         if (verbose > 1) {
11633                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11634                         PEXT(&chunk->chunk));
11635                 for (i = 0; i < chunk->mirror_count; i++)
11636                         fprintf(stdout, " %u", chunk->mirror_id[i]);
11637                 fprintf(stdout, "\n");
11638         }
11639
11640         bytes_done = 0;
11641         count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11642         pos = chunk->chunk.e_start;
11643         while (bytes_done < count) {
11644                 /* compute initial CRC-32 checksum */
11645                 crc = crc32(0L, Z_NULL, 0);
11646                 memset(crc_array, 0, sizeof(crc_array));
11647
11648                 bytes_read = 0;
11649                 for (i = 0; i < chunk->mirror_count; i++) {
11650                         bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11651                                                        buf, buflen, pos);
11652                         if (bytes_read < 0) {
11653                                 rc = bytes_read;
11654                                 fprintf(stderr,
11655                                         "%s: failed to read data from mirror %u: %s.\n",
11656                                         progname, chunk->mirror_id[i],
11657                                         strerror(-rc));
11658                                 goto error;
11659                         }
11660
11661                         /* compute new CRC-32 checksum */
11662                         crc_array[i] = crc32(crc, buf, bytes_read);
11663                 }
11664
11665                 if (verbose)
11666                         print_checksums(chunk, crc_array);
11667
11668                 /* compare CRC-32 checksum values */
11669                 for (i = 1; i < chunk->mirror_count; i++) {
11670                         if (crc_array[i] != crc_array[0]) {
11671                                 rc = -EINVAL;
11672
11673                                 fprintf(stderr,
11674                                         "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11675                                         progname, PEXT(&chunk->chunk),
11676                                         chunk->mirror_id[0],
11677                                         chunk->mirror_id[i]);
11678                         }
11679                 }
11680
11681                 pos += bytes_read;
11682                 bytes_done += bytes_read;
11683         }
11684
11685         if (verbose > 1 && rc == 0) {
11686                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11687                         PEXT(&chunk->chunk));
11688                 for (i = 0; i < chunk->mirror_count; i++)
11689                         fprintf(stdout, " %u", chunk->mirror_id[i]);
11690                 fprintf(stdout, " PASS\n\n");
11691         }
11692
11693 error:
11694         free(buf);
11695         return rc;
11696 }
11697
11698 /**
11699  * lfs_mirror_verify_file() - Verify a mirrored file.
11700  * @fname:      Mirrored file name.
11701  * @mirror_ids: Specified mirror ids to be verified.
11702  * @ids_nr:     Number of specified mirror ids.
11703  * @verbose:    Verbose mode.
11704  *
11705  * This function verifies that each SYNC mirror of a mirrored file
11706  * specified by @fname contains exactly the same data.
11707  *
11708  * If @mirror_ids is specified, then the function will verify the
11709  * mirrors specified by @mirror_ids contain exactly the same data.
11710  *
11711  * If @verbose is specified, then the function will print where the
11712  * differences are if the data do not match. Otherwise, it will
11713  * just return an error in that case.
11714  *
11715  * Return: 0 on success or a negative error code on failure.
11716  */
11717 static inline
11718 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11719                            int verbose)
11720 {
11721         struct verify_chunk chunks_array[1024] = { };
11722         struct llapi_layout *layout = NULL;
11723         struct stat stbuf;
11724         uint32_t flr_state;
11725         int fd;
11726         int chunk_count = 0;
11727         int idx = 0;
11728         int rc = 0;
11729         int rc1 = 0;
11730         int rc2 = 0;
11731
11732         if (stat(fname, &stbuf) < 0) {
11733                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11734                         progname, fname, strerror(errno));
11735                 rc = -errno;
11736                 goto error;
11737         }
11738
11739         if (!S_ISREG(stbuf.st_mode)) {
11740                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11741                         progname, fname);
11742                 rc = -EINVAL;
11743                 goto error;
11744         }
11745
11746         if (stbuf.st_size == 0) {
11747                 if (verbose)
11748                         fprintf(stdout, "%s: '%s' file size is 0.\n",
11749                                 progname, fname);
11750                 rc = 0;
11751                 goto error;
11752         }
11753
11754         fd = open(fname, O_DIRECT | O_RDONLY);
11755         if (fd < 0) {
11756                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11757                         progname, fname, strerror(errno));
11758                 rc = -errno;
11759                 goto error;
11760         }
11761
11762         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11763         if (rc < 0) {
11764                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11765                         progname, fname, strerror(errno));
11766                 goto close_fd;
11767         }
11768
11769         layout = llapi_layout_get_by_fd(fd, 0);
11770         if (!layout) {
11771                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11772                         progname, fname, strerror(errno));
11773                 rc = -errno;
11774                 llapi_lease_release(fd);
11775                 goto close_fd;
11776         }
11777
11778         rc = llapi_layout_flags_get(layout, &flr_state);
11779         if (rc < 0) {
11780                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11781                         progname, fname, strerror(errno));
11782                 rc = -errno;
11783                 goto free_layout;
11784         }
11785
11786         flr_state &= LCM_FL_FLR_MASK;
11787         switch (flr_state) {
11788         case LCM_FL_NONE:
11789                 rc = -EINVAL;
11790                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11791                         progname, fname, llapi_layout_flags_string(flr_state));
11792                 goto free_layout;
11793         default:
11794                 break;
11795         }
11796
11797         /* find out mirror chunks to be verified */
11798         chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11799                                                ARRAY_SIZE(chunks_array));
11800         if (chunk_count < 0) {
11801                 rc = chunk_count;
11802                 goto free_layout;
11803         }
11804
11805         if (ids_nr > 0)
11806                 /* filter specified mirror ids */
11807                 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11808
11809         if (verbose > 2)
11810                 print_chunks(fname, chunks_array, chunk_count);
11811
11812         for (idx = 0; idx < chunk_count; idx++) {
11813                 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11814                         if (verbose)
11815                                 fprintf(stdout,
11816                                         "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11817                                         progname, fname,
11818                                         PEXT(&chunks_array[idx].chunk),
11819                                         (unsigned long long)stbuf.st_size);
11820                         break;
11821                 }
11822
11823                 if (chunks_array[idx].mirror_count == 0) {
11824                         fprintf(stderr,
11825                                 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11826                                 progname, fname,
11827                                 PEXT(&chunks_array[idx].chunk));
11828                         if (verbose) {
11829                                 fprintf(stderr, "skipped\n");
11830                                 continue;
11831                         }
11832                         rc = -EINVAL;
11833                         fprintf(stderr, "failed\n");
11834                         goto free_layout;
11835                 }
11836
11837                 if (chunks_array[idx].mirror_count == 1) {
11838                         if (verbose)
11839                                 fprintf(stdout,
11840                                         "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11841                                         progname, fname,
11842                                         PEXT(&chunks_array[idx].chunk),
11843                                         chunks_array[idx].mirror_id[0]);
11844                         continue;
11845                 }
11846
11847                 rc = llapi_lease_check(fd);
11848                 if (rc != LL_LEASE_RDLCK) {
11849                         fprintf(stderr, "%s: '%s' lost lease lock.\n",
11850                                 progname, fname);
11851                         goto free_layout;
11852                 }
11853
11854                 /* verify one chunk */
11855                 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11856                                               &chunks_array[idx], verbose);
11857                 if (rc1 < 0) {
11858                         rc2 = rc1;
11859                         if (!verbose) {
11860                                 rc = rc1;
11861                                 goto free_layout;
11862                         }
11863                 }
11864         }
11865
11866         if (rc2 < 0)
11867                 rc = rc2;
11868
11869 free_layout:
11870         llapi_layout_free(layout);
11871         llapi_lease_release(fd);
11872 close_fd:
11873         close(fd);
11874 error:
11875         return rc;
11876 }
11877
11878 /**
11879  * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11880  * @argc: The count of lfs mirror verify command line arguments.
11881  * @argv: Array of strings for lfs mirror verify command line arguments.
11882  *
11883  * This function parses lfs mirror verify command and verifies the
11884  * specified mirrored file(s).
11885  *
11886  * Return: 0 on success or a negative error code on failure.
11887  */
11888 static inline int lfs_mirror_verify(int argc, char **argv)
11889 {
11890         __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11891         int ids_nr = 0;
11892         int c;
11893         int verbose = 0;
11894         int rc = 0;
11895         int rc1 = 0;
11896         char cmd[PATH_MAX];
11897
11898         struct option long_opts[] = {
11899         { .val = 'o',   .name = "only",         .has_arg = required_argument },
11900         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
11901         { .name = NULL } };
11902
11903         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11904         progname = cmd;
11905         while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11906                 switch (c) {
11907                 case 'o':
11908                         rc = parse_mirror_ids(mirror_ids,
11909                                               ARRAY_SIZE(mirror_ids),
11910                                               optarg);
11911                         if (rc < 0) {
11912                                 fprintf(stderr,
11913                                         "%s: bad mirror ids '%s'.\n",
11914                                         progname, optarg);
11915                                 goto error;
11916                         }
11917                         ids_nr = rc;
11918                         if (ids_nr < 2) {
11919                                 fprintf(stderr,
11920                                         "%s: at least 2 mirror ids needed with '--only' option.\n",
11921                                         progname);
11922                                 rc = CMD_HELP;
11923                                 goto error;
11924                         }
11925                         break;
11926                 case 'v':
11927                         verbose++;
11928                         break;
11929                 default:
11930                         fprintf(stderr, "%s: option '%s' unrecognized.\n",
11931                                 progname, argv[optind - 1]);
11932                         rc = -EINVAL;
11933                         goto error;
11934                 }
11935         }
11936
11937         if (argc == optind) {
11938                 fprintf(stderr, "%s: no file name given.\n", progname);
11939                 rc = CMD_HELP;
11940                 goto error;
11941         }
11942
11943         if (ids_nr > 0 && argc > optind + 1) {
11944                 fprintf(stderr,
11945                         "%s: '--only' cannot be used upon multiple files.\n",
11946                         progname);
11947                 rc = CMD_HELP;
11948                 goto error;
11949         }
11950
11951         if (ids_nr > 0) {
11952                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11953                 if (rc < 0)
11954                         goto error;
11955         }
11956
11957         rc = 0;
11958         for (; optind < argc; optind++) {
11959                 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
11960                                              verbose);
11961                 if (rc1 < 0)
11962                         rc = rc1;
11963         }
11964 error:
11965         return rc;
11966 }
11967
11968 /**
11969  * lfs_mirror() - Parse and execute lfs mirror commands.
11970  * @argc: The count of lfs mirror command line arguments.
11971  * @argv: Array of strings for lfs mirror command line arguments.
11972  *
11973  * This function parses lfs mirror commands and performs the
11974  * corresponding functions specified in mirror_cmdlist[].
11975  *
11976  * Return: 0 on success or an error code on failure.
11977  */
11978 static int lfs_mirror(int argc, char **argv)
11979 {
11980         char cmd[PATH_MAX];
11981         int rc = 0;
11982
11983         setlinebuf(stdout);
11984
11985         Parser_init("lfs-mirror > ", mirror_cmdlist);
11986
11987         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11988         progname = cmd;
11989         program_invocation_short_name = cmd;
11990         if (argc > 1)
11991                 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
11992         else
11993                 rc = Parser_commands();
11994
11995         return rc < 0 ? -rc : rc;
11996 }
11997
11998 static void lustre_som_swab(struct lustre_som_attrs *attrs)
11999 {
12000 #if __BYTE_ORDER == __BIG_ENDIAN
12001         __swab16s(&attrs->lsa_valid);
12002         __swab64s(&attrs->lsa_size);
12003         __swab64s(&attrs->lsa_blocks);
12004 #endif
12005 }
12006
12007 enum lfs_som_type {
12008         LFS_SOM_SIZE = 0x1,
12009         LFS_SOM_BLOCKS = 0x2,
12010         LFS_SOM_FLAGS = 0x4,
12011         LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12012                            LFS_SOM_FLAGS,
12013 };
12014
12015 static int lfs_getsom(int argc, char **argv)
12016 {
12017         const char *path;
12018         struct lustre_som_attrs *attrs;
12019         char buf[sizeof(*attrs) + 64];
12020         enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12021         int rc = 0, c;
12022
12023         while ((c = getopt(argc, argv, "sbf")) != -1) {
12024                 switch (c) {
12025                 case 's':
12026                         type = LFS_SOM_SIZE;
12027                         break;
12028                 case 'b':
12029                         type = LFS_SOM_BLOCKS;
12030                         break;
12031                 case 'f':
12032                         type = LFS_SOM_FLAGS;
12033                         break;
12034                 default:
12035                         fprintf(stderr, "%s: invalid option '%c'\n",
12036                                 progname, optopt);
12037                         return CMD_HELP;
12038                 }
12039         }
12040
12041         argc -= optind;
12042         argv += optind;
12043
12044         if (argc != 1) {
12045                 fprintf(stderr, "%s: %s\n",
12046                         progname, argc == 0 ? "miss file target" :
12047                         "input more than 2 files");
12048                 return CMD_HELP;
12049         }
12050
12051         path = argv[0];
12052         attrs = (void *)buf;
12053         rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12054         if (rc < 0) {
12055                 rc = -errno;
12056                 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12057                         argv[0], strerror(errno), errno);
12058                 return rc;
12059         }
12060
12061         lustre_som_swab(attrs);
12062
12063         switch (type) {
12064         case LFS_SOM_ATTR_ALL:
12065                 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12066                        path, (unsigned long long)attrs->lsa_size,
12067                        (unsigned long long)attrs->lsa_blocks,
12068                        attrs->lsa_valid);
12069                 break;
12070         case LFS_SOM_SIZE:
12071                 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12072                 break;
12073         case LFS_SOM_BLOCKS:
12074                 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12075                 break;
12076         case LFS_SOM_FLAGS:
12077                 printf("%x\n", attrs->lsa_valid);
12078                 break;
12079         default:
12080                 fprintf(stderr, "%s: unknown option\n", progname);
12081                 return CMD_HELP;
12082         }
12083
12084         return 0;
12085 }
12086
12087 /**
12088  * lfs_mirror_list_commands() - List lfs mirror commands.
12089  * @argc: The count of command line arguments.
12090  * @argv: Array of strings for command line arguments.
12091  *
12092  * This function lists lfs mirror commands defined in mirror_cmdlist[].
12093  *
12094  * Return: 0 on success.
12095  */
12096 static int lfs_mirror_list_commands(int argc, char **argv)
12097 {
12098         char buffer[81] = "";
12099
12100         Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12101                              NULL, 0, 4);
12102
12103         return 0;
12104 }
12105
12106 static int lfs_pcc_attach(int argc, char **argv)
12107 {
12108         struct option long_opts[] = {
12109         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12110         { .name = NULL } };
12111         int c;
12112         int rc = 0;
12113         __u32 archive_id = 0;
12114         const char *path;
12115         char *end;
12116         char fullpath[PATH_MAX];
12117         enum lu_pcc_type type = LU_PCC_READWRITE;
12118
12119         optind = 0;
12120         while ((c = getopt_long(argc, argv, "i:",
12121                                 long_opts, NULL)) != -1) {
12122                 switch (c) {
12123                 case 'i':
12124                         errno = 0;
12125                         archive_id = strtoul(optarg, &end, 0);
12126                         if (errno != 0 || *end != '\0' ||
12127                             archive_id == 0 || archive_id > UINT32_MAX) {
12128                                 fprintf(stderr,
12129                                         "error: %s: bad archive ID '%s'\n",
12130                                         argv[0], optarg);
12131                                 return CMD_HELP;
12132                         }
12133                         break;
12134                 case '?':
12135                         return CMD_HELP;
12136                 default:
12137                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12138                                 argv[0], argv[optind - 1]);
12139                         return CMD_HELP;
12140                 }
12141         }
12142
12143         if (archive_id == 0) {
12144                 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12145                 return CMD_HELP;
12146         }
12147
12148         if (argc <= optind) {
12149                 fprintf(stderr, "%s: must specify one or more file names\n",
12150                         argv[0]);
12151                 return CMD_HELP;
12152         }
12153
12154         while (optind < argc) {
12155                 int rc2;
12156
12157                 path = argv[optind++];
12158                 if (!realpath(path, fullpath)) {
12159                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12160                                 argv[0], path, strerror(errno));
12161                         if (rc == 0)
12162                                 rc = -EINVAL;
12163                         continue;
12164                 }
12165
12166                 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12167                 if (rc2 < 0) {
12168                         fprintf(stderr,
12169                                 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12170                                 argv[0], path, archive_id, strerror(-rc2));
12171                         if (rc == 0)
12172                                 rc = rc2;
12173                 }
12174         }
12175         return rc;
12176 }
12177
12178 static int lfs_pcc_attach_fid(int argc, char **argv)
12179 {
12180         struct option long_opts[] = {
12181         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12182         { .val = 'm',   .name = "mnt",  .has_arg = required_argument },
12183         { .name = NULL } };
12184         char                     short_opts[] = "i:m:";
12185         int                      c;
12186         int                      rc = 0;
12187         __u32                    archive_id = 0;
12188         char                    *end;
12189         const char              *mntpath = NULL;
12190         const char              *fidstr;
12191         enum lu_pcc_type         type = LU_PCC_READWRITE;
12192
12193         optind = 0;
12194         while ((c = getopt_long(argc, argv, short_opts,
12195                                 long_opts, NULL)) != -1) {
12196                 switch (c) {
12197                 case 'i':
12198                         errno = 0;
12199                         archive_id = strtoul(optarg, &end, 0);
12200                         if (errno != 0 || *end != '\0' ||
12201                             archive_id > UINT32_MAX) {
12202                                 fprintf(stderr,
12203                                         "error: %s: bad archive ID '%s'\n",
12204                                         argv[0], optarg);
12205                                 return CMD_HELP;
12206                         }
12207                         break;
12208                 case 'm':
12209                         mntpath = optarg;
12210                         break;
12211                 case '?':
12212                         return CMD_HELP;
12213                 default:
12214                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12215                                 argv[0], argv[optind - 1]);
12216                         return CMD_HELP;
12217                 }
12218         }
12219
12220         if (archive_id == 0) {
12221                 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12222                 return CMD_HELP;
12223         }
12224
12225         if (!mntpath) {
12226                 fprintf(stderr, "%s: must specify Lustre mount point\n",
12227                         argv[0]);
12228                 return CMD_HELP;
12229         }
12230
12231         if (argc <= optind) {
12232                 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12233                 return CMD_HELP;
12234         }
12235
12236         while (optind < argc) {
12237                 int rc2;
12238
12239                 fidstr = argv[optind++];
12240
12241                 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12242                                                archive_id, type);
12243                 if (rc2 < 0) {
12244                         fprintf(stderr,
12245                                 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12246                                 argv[0], fidstr, mntpath, archive_id,
12247                                 strerror(rc2));
12248                 }
12249                 if (rc == 0 && rc2 < 0)
12250                         rc = rc2;
12251         }
12252         return rc;
12253 }
12254
12255 static int lfs_pcc_detach(int argc, char **argv)
12256 {
12257         struct option long_opts[] = {
12258         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12259         { .name = NULL } };
12260         char                     short_opts[] = "k";
12261         int                      c;
12262         int                      rc = 0;
12263         const char              *path;
12264         char                     fullpath[PATH_MAX];
12265         __u32                    detach_opt = PCC_DETACH_OPT_UNCACHE;
12266
12267         optind = 0;
12268         while ((c = getopt_long(argc, argv, short_opts,
12269                                 long_opts, NULL)) != -1) {
12270                 switch (c) {
12271                 case 'k':
12272                         detach_opt = PCC_DETACH_OPT_NONE;
12273                         break;
12274                 case '?':
12275                         return CMD_HELP;
12276                 default:
12277                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12278                                 argv[0], argv[optind - 1]);
12279                         return CMD_HELP;
12280                 }
12281         }
12282
12283         while (optind < argc) {
12284                 int rc2;
12285
12286                 path = argv[optind++];
12287                 if (!realpath(path, fullpath)) {
12288                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12289                                 argv[0], path, strerror(errno));
12290                         if (rc == 0)
12291                                 rc = -EINVAL;
12292                         continue;
12293                 }
12294
12295                 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12296                 if (rc2 < 0) {
12297                         rc2 = -errno;
12298                         fprintf(stderr,
12299                                 "%s: cannot detach '%s' from PCC: %s\n",
12300                                 argv[0], path, strerror(errno));
12301                         if (rc == 0)
12302                                 rc = rc2;
12303                 }
12304         }
12305         return rc;
12306 }
12307
12308 static int lfs_pcc_detach_fid(int argc, char **argv)
12309 {
12310         struct option long_opts[] = {
12311         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12312         { .name = NULL } };
12313         char             short_opts[] = "k";
12314         int              c;
12315         int              rc = 0;
12316         const char      *fid;
12317         const char      *mntpath;
12318         __u32            detach_opt = PCC_DETACH_OPT_UNCACHE;
12319
12320         optind = 0;
12321         while ((c = getopt_long(argc, argv, short_opts,
12322                                 long_opts, NULL)) != -1) {
12323                 switch (c) {
12324                 case 'k':
12325                         detach_opt = PCC_DETACH_OPT_NONE;
12326                         break;
12327                 case '?':
12328                         return CMD_HELP;
12329                 default:
12330                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12331                                 argv[0], argv[optind - 1]);
12332                         return CMD_HELP;
12333                 }
12334         }
12335
12336         mntpath = argv[optind++];
12337
12338         while (optind < argc) {
12339                 int rc2;
12340
12341                 fid = argv[optind++];
12342
12343                 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12344                 if (rc2 < 0) {
12345                         fprintf(stderr,
12346                                 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12347                                 argv[0], fid, mntpath, strerror(-rc2));
12348                         if (rc == 0)
12349                                 rc = rc2;
12350                 }
12351         }
12352         return rc;
12353 }
12354
12355 static int lfs_pcc_state(int argc, char **argv)
12356 {
12357         int                      rc = 0;
12358         const char              *path;
12359         char                     fullpath[PATH_MAX];
12360         struct lu_pcc_state      state;
12361
12362         optind = 1;
12363
12364         if (argc <= 1) {
12365                 fprintf(stderr, "%s: must specify one or more file names\n",
12366                         argv[0]);
12367                 return CMD_HELP;
12368         }
12369
12370         while (optind < argc) {
12371                 int rc2;
12372
12373                 path = argv[optind++];
12374                 if (!realpath(path, fullpath)) {
12375                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12376                                 argv[0], path, strerror(errno));
12377                         if (rc == 0)
12378                                 rc = -EINVAL;
12379                         continue;
12380                 }
12381
12382                 rc2 = llapi_pcc_state_get(fullpath, &state);
12383                 if (rc2 < 0) {
12384                         if (rc == 0)
12385                                 rc = rc2;
12386                         fprintf(stderr,
12387                                 "%s: cannot get PCC state of '%s': %s\n",
12388                                 argv[0], path, strerror(-rc2));
12389                         continue;
12390                 }
12391
12392                 printf("file: %s", path);
12393                 printf(", type: %s", pcc_type2string(state.pccs_type));
12394                 if (state.pccs_type == LU_PCC_NONE &&
12395                     state.pccs_open_count == 0) {
12396                         printf("\n");
12397                         continue;
12398                 }
12399
12400                 printf(", PCC file: %s", state.pccs_path);
12401                 printf(", user number: %u", state.pccs_open_count);
12402                 printf(", flags: %x", state.pccs_flags);
12403                 printf("\n");
12404         }
12405         return rc;
12406 }
12407
12408 /**
12409  * lfs_pcc_list_commands() - List lfs pcc commands.
12410  * @argc: The count of command line arguments.
12411  * @argv: Array of strings for command line arguments.
12412  *
12413  * This function lists lfs pcc commands defined in pcc_cmdlist[].
12414  *
12415  * Return: 0 on success.
12416  */
12417 static int lfs_pcc_list_commands(int argc, char **argv)
12418 {
12419         char buffer[81] = "";
12420
12421         Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12422                              NULL, 0, 4);
12423
12424         return 0;
12425 }
12426
12427 /**
12428  * lfs_pcc() - Parse and execute lfs pcc commands.
12429  * @argc: The count of lfs pcc command line arguments.
12430  * @argv: Array of strings for lfs pcc command line arguments.
12431  *
12432  * This function parses lfs pcc commands and performs the
12433  * corresponding functions specified in pcc_cmdlist[].
12434  *
12435  * Return: 0 on success or an error code on failure.
12436  */
12437 static int lfs_pcc(int argc, char **argv)
12438 {
12439         char cmd[PATH_MAX];
12440         int rc = 0;
12441
12442         setlinebuf(stdout);
12443
12444         Parser_init("lfs-pcc > ", pcc_cmdlist);
12445
12446         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12447         progname = cmd;
12448         program_invocation_short_name = cmd;
12449         if (argc > 1)
12450                 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12451         else
12452                 rc = Parser_commands();
12453
12454         return rc < 0 ? -rc : rc;
12455 }
12456
12457 static int lfs_list_commands(int argc, char **argv)
12458 {
12459         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12460
12461         Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12462
12463         return 0;
12464 }
12465
12466 int main(int argc, char **argv)
12467 {
12468         int rc;
12469
12470         /* Ensure that liblustreapi constructor has run */
12471         if (!llapi_liblustreapi_initialized())
12472                 fprintf(stderr, "liblustreapi was not properly initialized\n");
12473
12474         setlinebuf(stdout);
12475         opterr = 0;
12476
12477         Parser_init("lfs > ", cmdlist);
12478
12479         progname = program_invocation_short_name; /* Used in error messages */
12480         if (argc > 1) {
12481                 llapi_set_command_name(argv[1]);
12482                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12483                 llapi_clear_command_name();
12484         } else {
12485                 rc = Parser_commands();
12486         }
12487
12488         return rc < 0 ? -rc : rc;
12489 }
12490
12491 #ifdef _LUSTRE_IDL_H_
12492 /* Everything we need here should be included by lustreapi.h. */
12493 # error "lfs should not depend on lustre_idl.h"
12494 #endif /* _LUSTRE_IDL_H_ */