Whamcloud - gitweb
LU-13971 quota: report Pool Quotas for a user
[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         bool show_pools = false;
8453         struct option long_opts[] = {
8454         { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
8455         { .name = NULL } };
8456         char **poollist = NULL;
8457         char *buf = NULL;
8458         int poolcount, i;
8459
8460         qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8461         if (!qctl)
8462                 return -ENOMEM;
8463
8464         qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
8465         qctl->qc_type = ALLQUOTA;
8466         obd_uuid = (char *)qctl->obd_uuid.uuid;
8467
8468         while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
8469                 long_opts, NULL)) != -1) {
8470                 switch (c) {
8471                 case 'U':
8472                         show_default = true;
8473                 case 'u':
8474                         qtype = USRQUOTA;
8475                         goto quota_type;
8476                 case 'G':
8477                         show_default = true;
8478                 case 'g':
8479                         qtype = GRPQUOTA;
8480                         goto quota_type;
8481                 case 'P':
8482                         show_default = true;
8483                 case 'p':
8484                         qtype = PRJQUOTA;
8485 quota_type:
8486                         if (qctl->qc_type != ALLQUOTA) {
8487                                 fprintf(stderr,
8488                                         "%s quota: only one of -u, -g, or -p may be specified\n",
8489                                         progname);
8490                                 rc = CMD_HELP;
8491                                 goto out;
8492                         }
8493                         qctl->qc_type = qtype;
8494                         break;
8495                 case 't':
8496                         qctl->qc_cmd = LUSTRE_Q_GETINFO;
8497                         break;
8498                 case 'o':
8499                         valid = qctl->qc_valid = QC_UUID;
8500                         snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
8501                         break;
8502                 case 'i':
8503                         valid = qctl->qc_valid = QC_MDTIDX;
8504                         idx = qctl->qc_idx = atoi(optarg);
8505                         if (idx == 0 && *optarg != '0') {
8506                                 fprintf(stderr,
8507                                         "%s quota: invalid MDT index '%s'\n",
8508                                         progname, optarg);
8509                                 rc = CMD_HELP;
8510                                 goto out;
8511                         }
8512                         break;
8513                 case 'I':
8514                         valid = qctl->qc_valid = QC_OSTIDX;
8515                         idx = qctl->qc_idx = atoi(optarg);
8516                         if (idx == 0 && *optarg != '0') {
8517                                 fprintf(stderr,
8518                                         "%s quota: invalid OST index '%s'\n",
8519                                         progname, optarg);
8520                                 rc = CMD_HELP;
8521                                 goto out;
8522                         }
8523                         break;
8524                 case 'v':
8525                         verbose = 1;
8526                         break;
8527                 case 'q':
8528                         quiet = 1;
8529                         break;
8530                 case 'h':
8531                         human_readable = true;
8532                         break;
8533                 case LFS_POOL_OPT:
8534                         if ((!optarg) && (argv[optind] != NULL) &&
8535                                 (argv[optind][0] != '-') &&
8536                                 (argv[optind][0] != '/')) {
8537                                 optarg = argv[optind++];
8538                                 if (lfs_verify_poolarg(optarg)) {
8539                                         rc = -EINVAL;
8540                                         goto out;
8541                                 }
8542                                 strncpy(qctl->qc_poolname, optarg,
8543                                         LOV_MAXPOOLNAME);
8544                                 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
8545                                         qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
8546                                 else
8547                                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8548                                 break;
8549                         }
8550
8551                         /* optarg is NULL */
8552                         show_pools = true;
8553                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8554                         break;
8555                 default:
8556                         fprintf(stderr, "%s quota: unrecognized option '%s'\n",
8557                                 progname, argv[optind - 1]);
8558                         rc = CMD_HELP;
8559                         goto out;
8560                 }
8561         }
8562
8563         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
8564         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8565              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8566              qctl->qc_type == ALLQUOTA &&
8567              optind == argc - 1 && !show_default) {
8568                 qctl->qc_idx = idx;
8569
8570                 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
8571                         qctl->qc_type = qtype;
8572                         qctl->qc_valid = valid;
8573                         if (qtype == USRQUOTA) {
8574                                 qctl->qc_id = geteuid();
8575                                 rc = uid2name(&name, qctl->qc_id);
8576                         } else {
8577                                 qctl->qc_id = getegid();
8578                                 rc = gid2name(&name, qctl->qc_id);
8579                                 memset(&qctl->qc_dqblk, 0,
8580                                        sizeof(qctl->qc_dqblk));
8581                         }
8582                         if (rc)
8583                                 name = "<unknown>";
8584                         mnt = argv[optind];
8585                         rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
8586                                               human_readable, show_default);
8587                         if (rc1 && !rc)
8588                                 rc = rc1;
8589                 }
8590                 goto out;
8591         /* lfs quota -u username /path/to/lustre/mount */
8592         } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8593                    qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8594                 /* options should be followed by u/g-name and mntpoint */
8595                 if ((!show_default && optind + 2 != argc) ||
8596                     (show_default && optind + 1 != argc) ||
8597                     qctl->qc_type == ALLQUOTA) {
8598                         fprintf(stderr,
8599                                 "%s quota: name and mount point must be specified\n",
8600                                 progname);
8601                         rc = CMD_HELP;
8602                         goto out;
8603                 }
8604
8605                 if (!show_default) {
8606                         name = argv[optind++];
8607                         switch (qctl->qc_type) {
8608                         case USRQUOTA:
8609                                 rc = name2uid(&qctl->qc_id, name);
8610                                 break;
8611                         case GRPQUOTA:
8612                                 rc = name2gid(&qctl->qc_id, name);
8613                                 break;
8614                         case PRJQUOTA:
8615                                 rc = name2projid(&qctl->qc_id, name);
8616                                 break;
8617                         default:
8618                                 rc = -ENOTSUP;
8619                                 break;
8620                         }
8621                 } else {
8622                         qctl->qc_valid = QC_GENERAL;
8623                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
8624                                         LUSTRE_Q_GETDEFAULT_POOL :
8625                                         LUSTRE_Q_GETDEFAULT;
8626                         qctl->qc_id = 0;
8627                 }
8628
8629                 if (rc) {
8630                         if (str2quotaid(&qctl->qc_id, name)) {
8631                                 fprintf(stderr, "%s quota: invalid id '%s'\n",
8632                                         progname, name);
8633                                 rc = CMD_HELP;
8634                                 goto out;
8635                         }
8636                 }
8637         } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
8638                 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
8639                         progname);
8640                 rc = CMD_HELP;
8641                 goto out;
8642         }
8643
8644         mnt = argv[optind];
8645         if (show_pools) {
8646                 char *p;
8647
8648                 i = 0;
8649                 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
8650                 if (rc)
8651                         goto out;
8652
8653                 for (i = 0; i < poolcount; i++) {
8654                         p = memchr(poollist[i], '.', MAXNAMLEN);
8655                         if (!p) {
8656                                 fprintf(stderr, "bad string format %.*s\n",
8657                                         MAXNAMLEN, poollist[i]);
8658                                 rc = -EINVAL;
8659                                 goto out;
8660                         }
8661                         p++;
8662                         printf("Quotas for pool: %s\n", p);
8663                         strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
8664                         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8665                                              human_readable, show_default);
8666                         if (rc)
8667                                 break;
8668                 }
8669                 goto out;
8670         }
8671
8672         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
8673                              human_readable, show_default);
8674 out:
8675         free(buf);
8676         free(qctl);
8677         return rc;
8678 }
8679 #endif /* HAVE_SYS_QUOTA_H! */
8680
8681 static int flushctx_ioctl(char *mp)
8682 {
8683         int fd, rc;
8684
8685         fd = open(mp, O_RDONLY);
8686         if (fd == -1) {
8687                 fprintf(stderr, "flushctx: error open %s: %s\n",
8688                         mp, strerror(errno));
8689                 return -1;
8690         }
8691
8692         rc = ioctl(fd, LL_IOC_FLUSHCTX);
8693         if (rc == -1)
8694                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
8695                         mp, strerror(errno));
8696
8697         close(fd);
8698         return rc;
8699 }
8700
8701 static int lfs_flushctx(int argc, char **argv)
8702 {
8703         int     kdestroy = 0, reap = 0, c;
8704         char    mntdir[PATH_MAX] = {'\0'};
8705         int     index = 0;
8706         int     rc = 0;
8707
8708         while ((c = getopt(argc, argv, "kr")) != -1) {
8709                 switch (c) {
8710                 case 'k':
8711                         kdestroy = 1;
8712                         break;
8713                 case 'r':
8714                         reap = 1;
8715                         break;
8716                 default:
8717                         fprintf(stderr,
8718                                 "error: %s: option '-%c' unrecognized\n",
8719                                 argv[0], c);
8720                         return CMD_HELP;
8721                 }
8722         }
8723
8724         if (kdestroy) {
8725                 rc = system("kdestroy > /dev/null");
8726                 if (rc) {
8727                         rc = WEXITSTATUS(rc);
8728                         fprintf(stderr,
8729                                 "error destroying tickets: %d, continuing\n",
8730                                 rc);
8731                 }
8732         }
8733
8734         if (optind >= argc) {
8735                 /* flush for all mounted lustre fs. */
8736                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
8737                         /* Check if we have a mount point */
8738                         if (mntdir[0] == '\0')
8739                                 continue;
8740
8741                         if (flushctx_ioctl(mntdir))
8742                                 rc = -1;
8743
8744                         mntdir[0] = '\0'; /* avoid matching in next loop */
8745                 }
8746         } else {
8747                 /* flush fs as specified */
8748                 while (optind < argc) {
8749                         if (flushctx_ioctl(argv[optind++]))
8750                                 rc = -1;
8751                 }
8752         }
8753
8754         if (reap) {
8755                 rc = system("keyctl reap > /dev/null");
8756                 if (rc != 0) {
8757                         rc = WEXITSTATUS(rc);
8758                         fprintf(stderr, "error reaping keyring: %d\n", rc);
8759                 }
8760         }
8761
8762         return rc;
8763 }
8764
8765 static int lfs_changelog(int argc, char **argv)
8766 {
8767         void *changelog_priv;
8768         struct changelog_rec *rec;
8769         long long startrec = 0, endrec = 0;
8770         char *mdd;
8771         struct option long_opts[] = {
8772                 { .val = 'f', .name = "follow", .has_arg = no_argument },
8773                 { .name = NULL } };
8774         char short_opts[] = "f";
8775         int rc, follow = 0;
8776
8777         while ((rc = getopt_long(argc, argv, short_opts,
8778                 long_opts, NULL)) != -1) {
8779                 switch (rc) {
8780                 case 'f':
8781                         follow++;
8782                         break;
8783                 default:
8784                         fprintf(stderr,
8785                                 "%s changelog: unrecognized option '%s'\n",
8786                                 progname, argv[optind - 1]);
8787                         return CMD_HELP;
8788                 }
8789         }
8790         if (optind >= argc) {
8791                 fprintf(stderr, "%s changelog: mdtname must be specified\n",
8792                         progname);
8793                 return CMD_HELP;
8794         }
8795
8796         mdd = argv[optind++];
8797         if (argc > optind) {
8798                 errno = 0;
8799                 startrec = strtoll(argv[optind++], NULL, 10);
8800                 if (errno != 0 || startrec < 0) {
8801                         fprintf(stderr,
8802                                 "%s changelog: bad startrec\n",
8803                                 progname);
8804                         return CMD_HELP;
8805                 }
8806         }
8807
8808         if (argc > optind) {
8809                 errno = 0;
8810                 endrec = strtoll(argv[optind++], NULL, 10);
8811                 if (errno != 0 || endrec < 0) {
8812                         fprintf(stderr,
8813                                 "%s changelog: bad endrec\n",
8814                                 progname);
8815                         return CMD_HELP;
8816                 }
8817         }
8818
8819         rc = llapi_changelog_start(&changelog_priv,
8820                                    CHANGELOG_FLAG_BLOCK |
8821                                    CHANGELOG_FLAG_JOBID |
8822                                    CHANGELOG_FLAG_EXTRA_FLAGS |
8823                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
8824                                    mdd, startrec);
8825         if (rc < 0) {
8826                 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
8827                         progname, strerror(errno = -rc));
8828                 return rc;
8829         }
8830
8831         rc = llapi_changelog_set_xflags(changelog_priv,
8832                                         CHANGELOG_EXTRA_FLAG_UIDGID |
8833                                         CHANGELOG_EXTRA_FLAG_NID |
8834                                         CHANGELOG_EXTRA_FLAG_OMODE |
8835                                         CHANGELOG_EXTRA_FLAG_XATTR);
8836         if (rc < 0) {
8837                 fprintf(stderr,
8838                         "%s changelog: cannot set xflags for changelog: %s\n",
8839                         progname, strerror(errno = -rc));
8840                 return rc;
8841         }
8842
8843         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
8844                 time_t secs;
8845                 struct tm ts;
8846
8847                 if (endrec && rec->cr_index > endrec) {
8848                         llapi_changelog_free(&rec);
8849                         break;
8850                 }
8851                 if (rec->cr_index < startrec) {
8852                         llapi_changelog_free(&rec);
8853                         continue;
8854                 }
8855
8856                 secs = rec->cr_time >> 30;
8857                 gmtime_r(&secs, &ts);
8858                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
8859                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
8860                        changelog_type2str(rec->cr_type),
8861                        ts.tm_hour, ts.tm_min, ts.tm_sec,
8862                        (int)(rec->cr_time & ((1 << 30) - 1)),
8863                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
8864                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
8865
8866                 if (rec->cr_flags & CLF_JOBID) {
8867                         struct changelog_ext_jobid *jid =
8868                                 changelog_rec_jobid(rec);
8869
8870                         if (jid->cr_jobid[0] != '\0')
8871                                 printf(" j=%s", jid->cr_jobid);
8872                 }
8873
8874                 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
8875                         struct changelog_ext_extra_flags *ef =
8876                                 changelog_rec_extra_flags(rec);
8877
8878                         printf(" ef=0x%llx",
8879                                (unsigned long long)ef->cr_extra_flags);
8880
8881                         if (ef->cr_extra_flags & CLFE_UIDGID) {
8882                                 struct changelog_ext_uidgid *uidgid =
8883                                         changelog_rec_uidgid(rec);
8884
8885                                 printf(" u=%llu:%llu",
8886                                        (unsigned long long)uidgid->cr_uid,
8887                                        (unsigned long long)uidgid->cr_gid);
8888                         }
8889                         if (ef->cr_extra_flags & CLFE_NID) {
8890                                 struct changelog_ext_nid *nid =
8891                                         changelog_rec_nid(rec);
8892
8893                                 printf(" nid=%s",
8894                                        libcfs_nid2str(nid->cr_nid));
8895                         }
8896
8897                         if (ef->cr_extra_flags & CLFE_OPEN) {
8898                                 struct changelog_ext_openmode *omd =
8899                                         changelog_rec_openmode(rec);
8900                                 char mode[] = "---";
8901
8902                                 /* exec mode must be exclusive */
8903                                 if (omd->cr_openflags & MDS_FMODE_EXEC) {
8904                                         mode[2] = 'x';
8905                                 } else {
8906                                         if (omd->cr_openflags & MDS_FMODE_READ)
8907                                                 mode[0] = 'r';
8908                                         if (omd->cr_openflags &
8909                                             (MDS_FMODE_WRITE |
8910                                              MDS_OPEN_TRUNC |
8911                                              MDS_OPEN_APPEND))
8912                                                 mode[1] = 'w';
8913                                 }
8914
8915                                 if (strcmp(mode, "---") != 0)
8916                                         printf(" m=%s", mode);
8917                         }
8918
8919                         if (ef->cr_extra_flags & CLFE_XATTR) {
8920                                 struct changelog_ext_xattr *xattr =
8921                                         changelog_rec_xattr(rec);
8922
8923                                 if (xattr->cr_xattr[0] != '\0')
8924                                         printf(" x=%s", xattr->cr_xattr);
8925                         }
8926                 }
8927
8928                 if (!fid_is_zero(&rec->cr_pfid))
8929                         printf(" p="DFID, PFID(&rec->cr_pfid));
8930                 if (rec->cr_namelen)
8931                         printf(" %.*s", rec->cr_namelen,
8932                                changelog_rec_name(rec));
8933
8934                 if (rec->cr_flags & CLF_RENAME) {
8935                         struct changelog_ext_rename *rnm =
8936                                 changelog_rec_rename(rec);
8937
8938                         if (!fid_is_zero(&rnm->cr_sfid))
8939                                 printf(" s="DFID" sp="DFID" %.*s",
8940                                        PFID(&rnm->cr_sfid),
8941                                        PFID(&rnm->cr_spfid),
8942                                        (int)changelog_rec_snamelen(rec),
8943                                        changelog_rec_sname(rec));
8944                 }
8945                 printf("\n");
8946
8947                 llapi_changelog_free(&rec);
8948         }
8949
8950         llapi_changelog_fini(&changelog_priv);
8951
8952         if (rc < 0)
8953                 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
8954                         progname, strerror(errno = -rc));
8955
8956         return (rc == 1 ? 0 : rc);
8957 }
8958
8959 static int lfs_changelog_clear(int argc, char **argv)
8960 {
8961         long long endrec;
8962         int rc;
8963
8964         if (argc != 4)
8965                 return CMD_HELP;
8966
8967         errno = 0;
8968         endrec = strtoll(argv[3], NULL, 10);
8969         if (errno != 0 || endrec < 0) {
8970                 fprintf(stderr,
8971                         "%s: bad endrec '%s'\n",
8972                         argv[0], argv[3]);
8973                 return CMD_HELP;
8974         }
8975
8976         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
8977
8978         if (rc == -EINVAL)
8979                 fprintf(stderr, "%s: record out of range: %llu\n",
8980                         argv[0], endrec);
8981         else if (rc == -ENOENT)
8982                 fprintf(stderr, "%s: no changelog user: %s\n",
8983                         argv[0], argv[2]);
8984         else if (rc)
8985                 fprintf(stderr, "%s error: %s\n", argv[0],
8986                         strerror(-rc));
8987
8988         if (rc)
8989                 errno = -rc;
8990
8991         return rc;
8992 }
8993
8994 static void rstripc(char *str, int c)
8995 {
8996         char *end = str + strlen(str);
8997
8998         for (; str < end && end[-1] == c; --end)
8999                 end[-1] = '\0';
9000 }
9001
9002 static int lfs_fid2path(int argc, char **argv)
9003 {
9004         struct option long_opts[] = {
9005                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
9006                 { .val = 'c',   .name = "current",      .has_arg = no_argument },
9007                 { .val = 'c',   .name = "print-link",   .has_arg = no_argument },
9008                 { .val = 'f',   .name = "print-fid",    .has_arg = no_argument },
9009                 { .val = 'l',   .name = "link", .has_arg = required_argument },
9010                 { .name = NULL } };
9011         char short_opts[] = "cfl:pr:";
9012         bool print_link = false;
9013         bool print_fid = false;
9014         bool print_mnt_dir;
9015         char mnt_dir[PATH_MAX] = "";
9016         int mnt_fd = -1;
9017         char *path_or_fsname;
9018         long long recno = -1;
9019         int linkno = -1;
9020         char *endptr = NULL;
9021         int rc = 0;
9022         int c;
9023         int i;
9024
9025         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9026                 switch (c) {
9027                 case 'c':
9028                         print_link = true;
9029                         break;
9030                 case 'f':
9031                         print_fid = true;
9032                         break;
9033                 case 'l':
9034                         errno = 0;
9035                         linkno = strtol(optarg, &endptr, 10);
9036                         if (errno != 0 || *endptr != '\0' || linkno < 0) {
9037                                 fprintf(stderr,
9038                                         "%s fid2path: invalid linkno '%s'\n",
9039                                         progname, optarg);
9040                                 return CMD_HELP;
9041                         }
9042                         break;
9043                 case 'r':
9044                         /* recno is something to do with changelogs
9045                          * that was never implemented. We just pass it
9046                          * through for the MDT to ignore.
9047                          */
9048                         errno = 0;
9049                         recno = strtoll(optarg, &endptr, 10);
9050                         if (errno != 0 || *endptr != '\0' || recno < 0) {
9051                                 fprintf(stderr,
9052                                         "%s fid2path: invalid recno '%s'\n",
9053                                         progname, optarg);
9054                                 return CMD_HELP;
9055                         }
9056                         break;
9057                 default:
9058                         fprintf(stderr,
9059                                 "%s fid2path: unrecognized option '%s'\n",
9060                                 progname, argv[optind - 1]);
9061                         return CMD_HELP;
9062                 }
9063         }
9064
9065         if (argc - optind < 2) {
9066                 fprintf(stderr,
9067                         "Usage: %s fid2path FSNAME|ROOT FID...\n",
9068                         progname);
9069                 return CMD_HELP;
9070         }
9071
9072         path_or_fsname = argv[optind];
9073
9074         if (*path_or_fsname == '/') {
9075                 print_mnt_dir = true;
9076                 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9077         } else {
9078                 print_mnt_dir = false;
9079                 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9080         }
9081
9082         if (rc < 0) {
9083                 fprintf(stderr,
9084                         "%s fid2path: cannot resolve mount point for '%s': %s\n",
9085                         progname, path_or_fsname, strerror(-rc));
9086                 goto out;
9087         }
9088
9089         mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9090         if (mnt_fd < 0) {
9091                 fprintf(stderr,
9092                         "%s fid2path: cannot open mount point for '%s': %s\n",
9093                         progname, path_or_fsname, strerror(-rc));
9094                 goto out;
9095         }
9096
9097         /* Strip trailing slashes from mnt_dir. */
9098         rstripc(mnt_dir + 1, '/');
9099
9100         for (i = optind + 1; i < argc; i++) {
9101                 const char *fid_str = argv[i];
9102                 struct lu_fid fid;
9103                 int rc2;
9104
9105                 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9106                 if (rc2 < 0) {
9107                         fprintf(stderr,
9108                                 "%s fid2path: invalid FID '%s'\n",
9109                                 progname, fid_str);
9110                         if (rc == 0)
9111                                 rc = rc2;
9112
9113                         continue;
9114                 }
9115
9116                 int linktmp = (linkno >= 0) ? linkno : 0;
9117                 while (1) {
9118                         int oldtmp = linktmp;
9119                         long long rectmp = recno;
9120                         char path_buf[PATH_MAX];
9121
9122                         rc2 = llapi_fid2path_at(mnt_fd, &fid,
9123                                 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9124                         if (rc2 < 0) {
9125                                 fprintf(stderr,
9126                                         "%s fid2path: cannot find %s %s: %s\n",
9127                                         progname, path_or_fsname, fid_str,
9128                                         strerror(-rc2));
9129                                 if (rc == 0)
9130                                         rc = rc2;
9131                                 break;
9132                         }
9133
9134                         if (print_fid)
9135                                 printf("%s ", fid_str);
9136
9137                         if (print_link)
9138                                 printf("%d ", linktmp);
9139
9140                         /* You may think this looks wrong or weird (and it is!)
9141                          * but we are actually trying to preserve the old quirky
9142                          * behaviors (enforced by our old quirky tests!) that
9143                          * make lfs so much fun to work on:
9144                          *
9145                          *   lustre 0x200000007:0x1:0x0 => "/"
9146                          *   /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9147                          *
9148                          * Note that llapi_fid2path() returns "" for the root
9149                          * FID. */
9150
9151                         printf("%s%s%s\n",
9152                                print_mnt_dir ? mnt_dir : "",
9153                                (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9154                                path_buf);
9155
9156                         if (linkno >= 0)
9157                                 /* specified linkno */
9158                                 break;
9159
9160                         if (oldtmp == linktmp)
9161                                 /* no more links */
9162                                 break;
9163                 }
9164         }
9165 out:
9166         if (!(mnt_fd < 0))
9167                 close(mnt_fd);
9168
9169         return rc;
9170 }
9171
9172 static int lfs_path2fid(int argc, char **argv)
9173 {
9174         struct option long_opts[] = {
9175                 { .val = 'p', .name = "parents", .has_arg = no_argument },
9176                 { .name = NULL } };
9177         char            **path;
9178         const char        short_opts[] = "p";
9179         const char       *sep = "";
9180         struct lu_fid     fid;
9181         int               rc = 0;
9182         bool              show_parents = false;
9183
9184         while ((rc = getopt_long(argc, argv, short_opts,
9185                                  long_opts, NULL)) != -1) {
9186                 switch (rc) {
9187                 case 'p':
9188                         show_parents = true;
9189                         break;
9190                 default:
9191                         fprintf(stderr,
9192                                 "%s path2fid: unrecognized option '%s'\n",
9193                                 progname, argv[optind - 1]);
9194                         return CMD_HELP;
9195                 }
9196         }
9197
9198         if (optind > argc - 1) {
9199                 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9200                         progname);
9201                 return CMD_HELP;
9202         } else if (optind < argc - 1) {
9203                 sep = ": ";
9204         }
9205
9206         rc = 0;
9207         for (path = argv + optind; *path != NULL; path++) {
9208                 int err = 0;
9209
9210                 if (!show_parents) {
9211                         err = llapi_path2fid(*path, &fid);
9212                         if (!err)
9213                                 printf("%s%s"DFID"\n",
9214                                        *sep != '\0' ? *path : "", sep,
9215                                        PFID(&fid));
9216                 } else {
9217                         char            name[NAME_MAX + 1];
9218                         unsigned int    linkno = 0;
9219
9220                         while ((err = llapi_path2parent(*path, linkno, &fid,
9221                                                 name, sizeof(name))) == 0) {
9222                                 if (*sep != '\0' && linkno == 0)
9223                                         printf("%s%s", *path, sep);
9224
9225                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9226                                        PFID(&fid), name);
9227                                 linkno++;
9228                         }
9229
9230                         /* err == -ENODATA is end-of-loop */
9231                         if (linkno > 0 && err == -ENODATA) {
9232                                 printf("\n");
9233                                 err = 0;
9234                         }
9235                 }
9236
9237                 if (err) {
9238                         fprintf(stderr,
9239                                 "%s path2fid: cannot get %sfid for '%s': %s\n",
9240                                 progname, show_parents ? "parent " : "", *path,
9241                                 strerror(-err));
9242                         if (rc == 0) {
9243                                 rc = err;
9244                                 errno = -err;
9245                         }
9246                 }
9247         }
9248
9249         return rc;
9250 }
9251
9252 #define MAX_ERRNO       4095
9253 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9254
9255 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9256 {
9257         int rc, rc2, k;
9258
9259         rc = llapi_rmfid(device, fa);
9260         if (rc < 0) {
9261                 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9262                         progname, strerror(-rc));
9263                 return rc;
9264         }
9265
9266         for (k = 0; k < fa->fa_nr; k++) {
9267                 rc2 = (__s32)fa->fa_fids[k].f_ver;
9268                 if (!IS_ERR_VALUE(rc2))
9269                         continue;
9270
9271                 if (rc == 0)
9272                         rc = rc2;
9273
9274                 fa->fa_fids[k].f_ver = 0;
9275                 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9276                         progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9277         }
9278
9279         return rc;
9280 }
9281
9282 static int lfs_rmfid(int argc, char **argv)
9283 {
9284         char *fidstr, *device;
9285         int rc = 0, rc2, nr;
9286         struct fid_array *fa;
9287
9288         if (optind > argc - 1) {
9289                 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9290                 return CMD_HELP;
9291         }
9292
9293         device = argv[optind++];
9294
9295         nr = argc - optind;
9296         fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9297         if (!fa)
9298                 return -ENOMEM;
9299
9300         fa->fa_nr = 0;
9301         rc = 0;
9302         while (optind < argc) {
9303                 int found;
9304
9305                 fidstr = argv[optind++];
9306                 while (*fidstr == '[')
9307                         fidstr++;
9308                 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9309                 if (found != 3) {
9310                         fprintf(stderr, "unrecognized FID: %s\n",
9311                                 argv[optind - 1]);
9312                         exit(1);
9313                 }
9314                 fa->fa_nr++;
9315                 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9316                         /* start another batch */
9317                         rc2 = lfs_rmfid_and_show_errors(device, fa);
9318                         if (rc2 && !rc)
9319                                 rc = rc2;
9320                         fa->fa_nr = 0;
9321                 }
9322         }
9323         if (fa->fa_nr) {
9324                 rc2 = lfs_rmfid_and_show_errors(device, fa);
9325                 if (rc2 && !rc)
9326                         rc = rc2;
9327         }
9328
9329         return rc;
9330 }
9331
9332 static int lfs_data_version(int argc, char **argv)
9333 {
9334         char *path;
9335         __u64 data_version;
9336         int fd;
9337         int rc;
9338         int c;
9339         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9340
9341         if (argc < 2) {
9342                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9343                         progname);
9344                 return CMD_HELP;
9345         }
9346
9347         while ((c = getopt(argc, argv, "nrw")) != -1) {
9348                 switch (c) {
9349                 case 'n':
9350                         data_version_flags = 0;
9351                         break;
9352                 case 'r':
9353                         data_version_flags |= LL_DV_RD_FLUSH;
9354                         break;
9355                 case 'w':
9356                         data_version_flags |= LL_DV_WR_FLUSH;
9357                         break;
9358                 default:
9359                         fprintf(stderr,
9360                                 "%s data_version: unrecognized option '%s'\n",
9361                                 progname, argv[optind - 1]);
9362                         return CMD_HELP;
9363                 }
9364         }
9365         if (optind == argc) {
9366                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9367                         progname);
9368                 return CMD_HELP;
9369         }
9370
9371         path = argv[optind];
9372         fd = open(path, O_RDONLY);
9373         if (fd < 0) {
9374                 rc = -errno;
9375                 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9376                         progname, path, strerror(-rc));
9377                 return rc;
9378         }
9379
9380         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9381         if (rc < 0)
9382                 fprintf(stderr,
9383                         "%s data_version: cannot get version for '%s': %s\n",
9384                         progname, path, strerror(-rc));
9385         else
9386                 printf("%ju" "\n", (uintmax_t)data_version);
9387
9388         close(fd);
9389         return rc;
9390 }
9391
9392 static int lfs_hsm_state(int argc, char **argv)
9393 {
9394         int rc;
9395         int i = 1;
9396         char *path;
9397         struct hsm_user_state hus;
9398
9399         if (argc < 2)
9400                 return CMD_HELP;
9401
9402         do {
9403                 path = argv[i];
9404
9405                 rc = llapi_hsm_state_get(path, &hus);
9406                 if (rc) {
9407                         fprintf(stderr, "can't get hsm state for %s: %s\n",
9408                                 path, strerror(errno = -rc));
9409                         return rc;
9410                 }
9411
9412                 /* Display path name and status flags */
9413                 printf("%s: (0x%08x)", path, hus.hus_states);
9414
9415                 if (hus.hus_states & HS_RELEASED)
9416                         printf(" released");
9417                 if (hus.hus_states & HS_EXISTS)
9418                         printf(" exists");
9419                 if (hus.hus_states & HS_DIRTY)
9420                         printf(" dirty");
9421                 if (hus.hus_states & HS_ARCHIVED)
9422                         printf(" archived");
9423                 /* Display user-settable flags */
9424                 if (hus.hus_states & HS_NORELEASE)
9425                         printf(" never_release");
9426                 if (hus.hus_states & HS_NOARCHIVE)
9427                         printf(" never_archive");
9428                 if (hus.hus_states & HS_LOST)
9429                         printf(" lost_from_hsm");
9430
9431                 if (hus.hus_archive_id != 0)
9432                         printf(", archive_id:%d", hus.hus_archive_id);
9433                 printf("\n");
9434
9435         } while (++i < argc);
9436
9437         return 0;
9438 }
9439
9440 #define LFS_HSM_SET   0
9441 #define LFS_HSM_CLEAR 1
9442
9443 /**
9444  * Generic function to set or clear HSM flags.
9445  * Used by hsm_set and hsm_clear.
9446  *
9447  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
9448  */
9449 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
9450 {
9451         struct option long_opts[] = {
9452         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
9453         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
9454         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
9455         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
9456         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
9457         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
9458         { .val = 'i',   .name = "archive-id",   .has_arg = required_argument },
9459         { .name = NULL } };
9460         char short_opts[] = "lraAdei:";
9461         __u64 mask = 0;
9462         int c, rc;
9463         char *path;
9464         __u32 archive_id = 0;
9465         char *end = NULL;
9466
9467         if (argc < 3)
9468                 return CMD_HELP;
9469
9470         while ((c = getopt_long(argc, argv, short_opts,
9471                                 long_opts, NULL)) != -1) {
9472                 switch (c) {
9473                 case 'l':
9474                         mask |= HS_LOST;
9475                         break;
9476                 case 'a':
9477                         mask |= HS_NOARCHIVE;
9478                         break;
9479                 case 'A':
9480                         mask |= HS_ARCHIVED;
9481                         break;
9482                 case 'r':
9483                         mask |= HS_NORELEASE;
9484                         break;
9485                 case 'd':
9486                         mask |= HS_DIRTY;
9487                         break;
9488                 case 'e':
9489                         mask |= HS_EXISTS;
9490                         break;
9491                 case 'i':
9492                         errno = 0;
9493                         archive_id = strtol(optarg, &end, 10);
9494                         if (errno != 0 || *end != '\0' || archive_id < 0) {
9495                                 fprintf(stderr, "invalid archive_id: '%s'\n",
9496                                         end);
9497                                 return CMD_HELP;
9498                         }
9499                         break;
9500                 case '?':
9501                         return CMD_HELP;
9502                 default:
9503                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9504                                 argv[0], argv[optind - 1]);
9505                         return CMD_HELP;
9506                 }
9507         }
9508
9509         /* User should have specified a flag */
9510         if (mask == 0)
9511                 return CMD_HELP;
9512
9513         while (optind < argc) {
9514                 path = argv[optind];
9515
9516                 /* If mode == 0, this means we apply the mask. */
9517                 if (mode == LFS_HSM_SET)
9518                         rc = llapi_hsm_state_set(path, mask, 0, archive_id);
9519                 else
9520                         rc = llapi_hsm_state_set(path, 0, mask, 0);
9521
9522                 if (rc != 0) {
9523                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
9524                                 path, strerror(errno = -rc));
9525                         return rc;
9526                 }
9527                 optind++;
9528         }
9529
9530         return 0;
9531 }
9532
9533 static int lfs_hsm_action(int argc, char **argv)
9534 {
9535         int                              rc;
9536         int                              i = 1;
9537         char                            *path;
9538         struct hsm_current_action        hca;
9539         struct hsm_extent                he;
9540         enum hsm_user_action             hua;
9541         enum hsm_progress_states         hps;
9542
9543         if (argc < 2)
9544                 return CMD_HELP;
9545
9546         do {
9547                 path = argv[i];
9548
9549                 rc = llapi_hsm_current_action(path, &hca);
9550                 if (rc) {
9551                         fprintf(stderr, "can't get hsm action for %s: %s\n",
9552                                 path, strerror(errno = -rc));
9553                         return rc;
9554                 }
9555                 he = hca.hca_location;
9556                 hua = hca.hca_action;
9557                 hps = hca.hca_state;
9558
9559                 printf("%s: %s", path, hsm_user_action2name(hua));
9560
9561                 /* Skip file without action */
9562                 if (hca.hca_action == HUA_NONE) {
9563                         printf("\n");
9564                         continue;
9565                 }
9566
9567                 printf(" %s ", hsm_progress_state2name(hps));
9568
9569                 if ((hps == HPS_RUNNING) &&
9570                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
9571                         printf("(%llu bytes moved)\n",
9572                                (unsigned long long)he.length);
9573                 else if ((he.offset + he.length) == LUSTRE_EOF)
9574                         printf("(from %llu to EOF)\n",
9575                                (unsigned long long)he.offset);
9576                 else
9577                         printf("(from %llu to %llu)\n",
9578                                (unsigned long long)he.offset,
9579                                (unsigned long long)(he.offset + he.length));
9580
9581         } while (++i < argc);
9582
9583         return 0;
9584 }
9585
9586 static int lfs_hsm_set(int argc, char **argv)
9587 {
9588         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
9589 }
9590
9591 static int lfs_hsm_clear(int argc, char **argv)
9592 {
9593         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
9594 }
9595
9596 /**
9597  * Check file state and return its fid, to be used by lfs_hsm_request().
9598  *
9599  * \param[in]     file      Path to file to check
9600  * \param[in,out] fid       Pointer to allocated lu_fid struct.
9601  * \param[in,out] last_dev  Pointer to last device id used.
9602  *
9603  * \return 0 on success.
9604  */
9605 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
9606                                 dev_t *last_dev)
9607 {
9608         struct stat     st;
9609         int             rc;
9610
9611         rc = lstat(file, &st);
9612         if (rc) {
9613                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
9614                 return -errno;
9615         }
9616         /*
9617          * Checking for regular file as archiving as posix copytool
9618          * rejects archiving files other than regular files
9619          */
9620         if (!S_ISREG(st.st_mode)) {
9621                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
9622                 return CMD_HELP;
9623         }
9624         /* A request should be ... */
9625         if (*last_dev != st.st_dev && *last_dev != 0) {
9626                 fprintf(stderr,
9627                         "All files should be on the same filesystem: %s\n",
9628                         file);
9629                 return -EINVAL;
9630         }
9631         *last_dev = st.st_dev;
9632
9633         rc = llapi_path2fid(file, fid);
9634         if (rc) {
9635                 fprintf(stderr, "Cannot read FID of %s: %s\n",
9636                         file, strerror(-rc));
9637                 return rc;
9638         }
9639         return 0;
9640 }
9641
9642 /* Fill an HSM HUR item with a given file name.
9643  *
9644  * If mntpath is set, then the filename is actually a FID, and no
9645  * lookup on the filesystem will be performed.
9646  *
9647  * \param[in]  hur         the user request to fill
9648  * \param[in]  idx         index of the item inside the HUR to fill
9649  * \param[in]  mntpath     mountpoint of Lustre
9650  * \param[in]  fname       filename (if mtnpath is NULL)
9651  *                         or FID (if mntpath is set)
9652  * \param[in]  last_dev    pointer to last device id used
9653  *
9654  * \retval 0 on success
9655  * \retval CMD_HELP or a negative errno on error
9656  */
9657 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
9658                          const char *mntpath, const char *fname,
9659                          dev_t *last_dev)
9660 {
9661         struct hsm_user_item *hui = &hur->hur_user_item[idx];
9662         int rc;
9663
9664         hui->hui_extent.length = -1;
9665
9666         if (mntpath) {
9667                 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
9668                 if (rc)
9669                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
9670                                 fname);
9671         } else {
9672                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
9673         }
9674
9675         if (rc == 0)
9676                 hur->hur_request.hr_itemcount++;
9677
9678         return rc;
9679 }
9680
9681 static int lfs_hsm_request(int argc, char **argv, int action)
9682 {
9683         struct option long_opts[] = {
9684         { .val = 'a',   .name = "archive",      .has_arg = required_argument },
9685         { .val = 'D',   .name = "data",         .has_arg = required_argument },
9686         { .val = 'l',   .name = "filelist",     .has_arg = required_argument },
9687         { .val = 'm',   .name = "mntpath",      .has_arg = required_argument },
9688         { .name = NULL } };
9689         dev_t last_dev = 0;
9690         char short_opts[] = "l:D:a:m:";
9691         struct hsm_user_request *hur, *oldhur;
9692         int c, i;
9693         size_t len;
9694         int nbfile;
9695         char *line = NULL;
9696         char *filelist = NULL;
9697         char fullpath[PATH_MAX];
9698         char *opaque = NULL;
9699         int opaque_len = 0;
9700         int archive_id = 0;
9701         FILE *fp;
9702         int nbfile_alloc = 0;
9703         char *some_file = NULL;
9704         char *mntpath = NULL;
9705         int rc;
9706
9707         if (argc < 2)
9708                 return CMD_HELP;
9709
9710         while ((c = getopt_long(argc, argv, short_opts,
9711                                 long_opts, NULL)) != -1) {
9712                 switch (c) {
9713                 case 'l':
9714                         filelist = optarg;
9715                         break;
9716                 case 'D':
9717                         opaque = optarg;
9718                         break;
9719                 case 'a':
9720                         if (action != HUA_ARCHIVE &&
9721                             action != HUA_REMOVE) {
9722                                 fprintf(stderr,
9723                                         "error: -a is supported only when archiving or removing\n");
9724                                 return CMD_HELP;
9725                         }
9726                         archive_id = atoi(optarg);
9727                         break;
9728                 case 'm':
9729                         if (!some_file) {
9730                                 mntpath = optarg;
9731                                 some_file = strdup(optarg);
9732                         }
9733                         break;
9734                 case '?':
9735                         return CMD_HELP;
9736                 default:
9737                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
9738                                 argv[0], argv[optind - 1]);
9739                         return CMD_HELP;
9740                 }
9741         }
9742
9743         /* All remaining args are files, so we have at least nbfile */
9744         nbfile = argc - optind;
9745
9746         if ((nbfile == 0) && (!filelist))
9747                 return CMD_HELP;
9748
9749         if (opaque)
9750                 opaque_len = strlen(opaque);
9751
9752         /*
9753          * Alloc the request structure with enough place to store all files
9754          * from command line.
9755          */
9756         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
9757         if (!hur) {
9758                 fprintf(stderr, "Cannot create the request: %s\n",
9759                         strerror(errno));
9760                 return errno;
9761         }
9762         nbfile_alloc = nbfile;
9763
9764         hur->hur_request.hr_action = action;
9765         hur->hur_request.hr_archive_id = archive_id;
9766         hur->hur_request.hr_flags = 0;
9767
9768         /* All remaining args are files, add them */
9769         if (nbfile != 0 && some_file == NULL)
9770                 some_file = strdup(argv[optind]);
9771
9772         for (i = 0; i < nbfile; i++) {
9773                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
9774                                    &last_dev);
9775                 if (rc)
9776                         goto out_free;
9777         }
9778
9779         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
9780
9781         /* If a filelist was specified, read the filelist from it. */
9782         if (filelist) {
9783                 fp = fopen(filelist, "r");
9784                 if (!fp) {
9785                         fprintf(stderr, "Cannot read the file list %s: %s\n",
9786                                 filelist, strerror(errno));
9787                         rc = -errno;
9788                         goto out_free;
9789                 }
9790
9791                 while ((rc = getline(&line, &len, fp)) != -1) {
9792                         /*
9793                          * If allocated buffer was too small, get something
9794                          * larger
9795                          */
9796                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
9797                                 ssize_t size;
9798
9799                                 nbfile_alloc = nbfile_alloc * 2 + 1;
9800                                 oldhur = hur;
9801                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
9802                                                                    opaque_len);
9803                                 if (!hur) {
9804                                         fprintf(stderr,
9805                                                 "hsm: cannot allocate the request: %s\n",
9806                                                 strerror(errno));
9807                                         hur = oldhur;
9808                                         rc = -errno;
9809                                         fclose(fp);
9810                                         goto out_free;
9811                                 }
9812                                 size = hur_len(oldhur);
9813                                 if (size < 0) {
9814                                         fprintf(stderr,
9815                                                 "hsm: cannot allocate %u files + %u bytes data\n",
9816                                                 oldhur->hur_request.hr_itemcount,
9817                                                 oldhur->hur_request.hr_data_len);
9818                                         free(hur);
9819                                         hur = oldhur;
9820                                         rc = -E2BIG;
9821                                         fclose(fp);
9822                                         goto out_free;
9823                                 }
9824                                 memcpy(hur, oldhur, size);
9825                                 free(oldhur);
9826                         }
9827
9828                         /* Chop CR */
9829                         if (line[strlen(line) - 1] == '\n')
9830                                 line[strlen(line) - 1] = '\0';
9831
9832                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
9833                                            mntpath, line, &last_dev);
9834                         if (rc) {
9835                                 fclose(fp);
9836                                 goto out_free;
9837                         }
9838
9839                         if (!some_file) {
9840                                 some_file = line;
9841                                 line = NULL;
9842                         }
9843                 }
9844
9845                 rc = fclose(fp);
9846                 free(line);
9847         }
9848
9849         /* If a --data was used, add it to the request */
9850         hur->hur_request.hr_data_len = opaque_len;
9851         if (opaque)
9852                 memcpy(hur_data(hur), opaque, opaque_len);
9853
9854         /* Send the HSM request */
9855         if (realpath(some_file, fullpath) == NULL) {
9856                 fprintf(stderr, "Could not find path '%s': %s\n",
9857                         some_file, strerror(errno));
9858         }
9859         rc = llapi_hsm_request(fullpath, hur);
9860         if (rc) {
9861                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
9862                         some_file, strerror(-rc));
9863                 goto out_free;
9864         }
9865
9866 out_free:
9867         free(some_file);
9868         free(hur);
9869         return rc;
9870 }
9871
9872 static int lfs_hsm_archive(int argc, char **argv)
9873 {
9874         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
9875 }
9876
9877 static int lfs_hsm_restore(int argc, char **argv)
9878 {
9879         return lfs_hsm_request(argc, argv, HUA_RESTORE);
9880 }
9881
9882 static int lfs_hsm_release(int argc, char **argv)
9883 {
9884         return lfs_hsm_request(argc, argv, HUA_RELEASE);
9885 }
9886
9887 static int lfs_hsm_remove(int argc, char **argv)
9888 {
9889         return lfs_hsm_request(argc, argv, HUA_REMOVE);
9890 }
9891
9892 static int lfs_hsm_cancel(int argc, char **argv)
9893 {
9894         return lfs_hsm_request(argc, argv, HUA_CANCEL);
9895 }
9896
9897 static int lfs_swap_layouts(int argc, char **argv)
9898 {
9899         if (argc != 3)
9900                 return CMD_HELP;
9901
9902         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
9903                                   SWAP_LAYOUTS_KEEP_MTIME |
9904                                   SWAP_LAYOUTS_KEEP_ATIME);
9905 }
9906
9907 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
9908
9909 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
9910
9911 int lfs_get_mode(const char *string)
9912 {
9913         enum lock_mode_user mode;
9914
9915         for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
9916                 if (lock_mode_names[mode] == NULL)
9917                         continue;
9918                 if (strcmp(string, lock_mode_names[mode]) == 0)
9919                         return mode;
9920         }
9921
9922         return -EINVAL;
9923 }
9924
9925 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
9926 {
9927         enum lu_ladvise_type advice;
9928
9929         for (advice = 0;
9930              advice < ARRAY_SIZE(ladvise_names); advice++) {
9931                 if (ladvise_names[advice] == NULL)
9932                         continue;
9933                 if (strcmp(string, ladvise_names[advice]) == 0)
9934                         return advice;
9935         }
9936
9937         return LU_LADVISE_INVALID;
9938 }
9939
9940 static int lfs_ladvise(int argc, char **argv)
9941 {
9942         struct option long_opts[] = {
9943         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
9944         { .val = 'b',   .name = "background",   .has_arg = no_argument },
9945         { .val = 'e',   .name = "end",          .has_arg = required_argument },
9946         { .val = 'l',   .name = "length",       .has_arg = required_argument },
9947         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
9948         { .val = 's',   .name = "start",        .has_arg = required_argument },
9949         { .val = 'u',   .name = "unset",        .has_arg = no_argument },
9950         { .name = NULL } };
9951         char                     short_opts[] = "a:be:l:m:s:u";
9952         int                      c;
9953         int                      rc = 0;
9954         const char              *path;
9955         int                      fd;
9956         struct llapi_lu_ladvise  advice;
9957         enum lu_ladvise_type     advice_type = LU_LADVISE_INVALID;
9958         unsigned long long       start = 0;
9959         unsigned long long       end = LUSTRE_EOF;
9960         unsigned long long       length = 0;
9961         unsigned long long       size_units;
9962         unsigned long long       flags = 0;
9963         int                      mode = 0;
9964
9965         optind = 0;
9966         while ((c = getopt_long(argc, argv, short_opts,
9967                                 long_opts, NULL)) != -1) {
9968                 switch (c) {
9969                 case 'a':
9970                         advice_type = lfs_get_ladvice(optarg);
9971                         if (advice_type == LU_LADVISE_INVALID) {
9972                                 fprintf(stderr,
9973                                         "%s: invalid advice type '%s'\n",
9974                                         argv[0], optarg);
9975                                 fprintf(stderr, "Valid types:");
9976
9977                                 for (advice_type = 0;
9978                                      advice_type < ARRAY_SIZE(ladvise_names);
9979                                      advice_type++) {
9980                                         if (ladvise_names[advice_type] == NULL)
9981                                                 continue;
9982                                         fprintf(stderr, " %s",
9983                                                 ladvise_names[advice_type]);
9984                                 }
9985                                 fprintf(stderr, "\n");
9986
9987                                 return CMD_HELP;
9988                         }
9989                         break;
9990                 case 'b':
9991                         flags |= LF_ASYNC;
9992                         break;
9993                 case 'u':
9994                         flags |= LF_UNSET;
9995                         break;
9996                 case 'e':
9997                         size_units = 1;
9998                         rc = llapi_parse_size(optarg, &end,
9999                                               &size_units, 0);
10000                         if (rc) {
10001                                 fprintf(stderr, "%s: bad end offset '%s'\n",
10002                                         argv[0], optarg);
10003                                 return CMD_HELP;
10004                         }
10005                         break;
10006                 case 's':
10007                         size_units = 1;
10008                         rc = llapi_parse_size(optarg, &start,
10009                                               &size_units, 0);
10010                         if (rc) {
10011                                 fprintf(stderr,
10012                                         "%s: bad start offset '%s'\n",
10013                                         argv[0], optarg);
10014                                 return CMD_HELP;
10015                         }
10016                         break;
10017                 case 'l':
10018                         size_units = 1;
10019                         rc = llapi_parse_size(optarg, &length,
10020                                               &size_units, 0);
10021                         if (rc) {
10022                                 fprintf(stderr, "%s: bad length '%s'\n",
10023                                         argv[0], optarg);
10024                                 return CMD_HELP;
10025                         }
10026                         break;
10027                 case 'm':
10028                         mode = lfs_get_mode(optarg);
10029                         if (mode < 0) {
10030                                 fprintf(stderr,
10031                                         "%s: bad mode '%s', valid modes are READ or WRITE\n",
10032                                         argv[0], optarg);
10033                                 return CMD_HELP;
10034                         }
10035                         break;
10036                 case '?':
10037                         return CMD_HELP;
10038                 default:
10039                         fprintf(stderr, "%s: option '%s' unrecognized\n",
10040                                 argv[0], argv[optind - 1]);
10041                         return CMD_HELP;
10042                 }
10043         }
10044
10045         if (advice_type == LU_LADVISE_INVALID) {
10046                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10047                 fprintf(stderr, "Valid types:");
10048                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10049                      advice_type++) {
10050                         if (ladvise_names[advice_type] == NULL)
10051                                 continue;
10052                         fprintf(stderr, " %s", ladvise_names[advice_type]);
10053                 }
10054                 fprintf(stderr, "\n");
10055                 return CMD_HELP;
10056         }
10057
10058         if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10059                 fprintf(stderr,
10060                         "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10061                         argv[0]);
10062                 return CMD_HELP;
10063         }
10064
10065         if (argc <= optind) {
10066                 fprintf(stderr, "%s: please give one or more file names\n",
10067                         argv[0]);
10068                 return CMD_HELP;
10069         }
10070
10071         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10072                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10073                         argv[0]);
10074                 return CMD_HELP;
10075         }
10076
10077         if (end == LUSTRE_EOF && length != 0)
10078                 end = start + length;
10079
10080         if (end <= start) {
10081                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10082                         argv[0], start, end);
10083                 return CMD_HELP;
10084         }
10085
10086         if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10087                 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10088                         argv[0]);
10089                 return CMD_HELP;
10090         }
10091
10092         if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10093                 fprintf(stderr, "%s: mode is required with lockahead\n",
10094                         argv[0]);
10095                 return CMD_HELP;
10096         }
10097
10098         while (optind < argc) {
10099                 int rc2;
10100
10101                 path = argv[optind++];
10102
10103                 fd = open(path, O_RDONLY);
10104                 if (fd < 0) {
10105                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10106                                 argv[0], path, strerror(errno));
10107                         rc2 = -errno;
10108                         goto next;
10109                 }
10110
10111                 advice.lla_start = start;
10112                 advice.lla_end = end;
10113                 advice.lla_advice = advice_type;
10114                 advice.lla_value1 = 0;
10115                 advice.lla_value2 = 0;
10116                 advice.lla_value3 = 0;
10117                 advice.lla_value4 = 0;
10118                 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10119                         advice.lla_lockahead_mode = mode;
10120                         advice.lla_peradvice_flags = flags;
10121                 }
10122
10123                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10124                 close(fd);
10125                 if (rc2 < 0) {
10126                         fprintf(stderr,
10127                                 "%s: cannot give advice '%s' to file '%s': %s\n",
10128                                 argv[0], ladvise_names[advice_type],
10129                                 path, strerror(errno));
10130
10131                         goto next;
10132                 }
10133
10134 next:
10135                 if (rc == 0 && rc2 < 0)
10136                         rc = rc2;
10137         }
10138         return rc;
10139 }
10140
10141 static const char *const heat_names[] = LU_HEAT_NAMES;
10142
10143 static int lfs_heat_get(int argc, char **argv)
10144 {
10145         struct lu_heat *heat;
10146         int rc = 0, rc2;
10147         char *path;
10148         int fd;
10149         int i;
10150
10151         if (argc <= 1)
10152                 return CMD_HELP;
10153
10154         heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10155         if (!heat) {
10156                 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10157                 return -ENOMEM;
10158         }
10159
10160         optind = 1;
10161         while (optind < argc) {
10162                 path = argv[optind++];
10163
10164                 fd = open(path, O_RDONLY);
10165                 if (fd < 0) {
10166                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10167                                 argv[0], path, strerror(errno));
10168                         rc2 = -errno;
10169                         goto next;
10170                 }
10171
10172                 heat->lh_count = OBD_HEAT_COUNT;
10173                 rc2 = llapi_heat_get(fd, heat);
10174                 close(fd);
10175                 if (rc2 < 0) {
10176                         fprintf(stderr,
10177                                 "%s: cannot get heat of file '%s': %s\n",
10178                                 argv[0], path, strerror(errno));
10179                         goto next;
10180                 }
10181
10182                 printf("flags: %x\n", heat->lh_flags);
10183                 for (i = 0; i < heat->lh_count; i++)
10184                         printf("%s: %llu\n", heat_names[i],
10185                                (unsigned long long)heat->lh_heat[i]);
10186 next:
10187                 if (rc == 0 && rc2 < 0)
10188                         rc = rc2;
10189         }
10190
10191         free(heat);
10192         return rc;
10193 }
10194
10195 static int lfs_heat_set(int argc, char **argv)
10196 {
10197         struct option long_opts[] = {
10198         { .val = 'c',   .name = "clear",        .has_arg = no_argument },
10199         { .val = 'o',   .name = "off",          .has_arg = no_argument },
10200         { .val = 'O',   .name = "on",           .has_arg = no_argument },
10201         { .name = NULL } };
10202         enum lu_heat_flag flags = 0;
10203         int rc = 0, rc2;
10204         char *path;
10205         int fd;
10206         int c;
10207
10208         if (argc <= 1)
10209                 return CMD_HELP;
10210
10211         optind = 0;
10212         while ((c = getopt_long(argc, argv, "coO", long_opts, NULL)) != -1) {
10213                 switch (c) {
10214                 case 'c':
10215                         flags |= LU_HEAT_FLAG_CLEAR;
10216                         break;
10217                 case 'o':
10218                         flags |= LU_HEAT_FLAG_CLEAR;
10219                         flags |= LU_HEAT_FLAG_OFF;
10220                         break;
10221                 case 'O':
10222                         flags &= ~LU_HEAT_FLAG_OFF;
10223                         break;
10224                 case '?':
10225                         return CMD_HELP;
10226                 default:
10227                         fprintf(stderr, "%s: option '%s' unrecognized\n",
10228                                 argv[0], argv[optind - 1]);
10229                         return CMD_HELP;
10230                 }
10231         }
10232
10233         if (argc <= optind) {
10234                 fprintf(stderr, "%s: please give one or more file names\n",
10235                         argv[0]);
10236                 return CMD_HELP;
10237         }
10238
10239         while (optind < argc) {
10240                 path = argv[optind++];
10241
10242                 fd = open(path, O_RDONLY);
10243                 if (fd < 0) {
10244                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10245                                 argv[0], path, strerror(errno));
10246                         rc2 = -errno;
10247                         goto next;
10248                 }
10249
10250                 rc2 = llapi_heat_set(fd, flags);
10251                 close(fd);
10252                 if (rc2 < 0) {
10253                         fprintf(stderr,
10254                                 "%s: cannot setflags heat of file '%s': %s\n",
10255                                 argv[0], path, strerror(errno));
10256                         goto next;
10257                 }
10258 next:
10259                 if (rc == 0 && rc2 < 0)
10260                         rc = rc2;
10261         }
10262         return rc;
10263 }
10264
10265 /**
10266  * The input string contains a comma delimited list of component ids and
10267  * ranges, for example "1,2-4,7".
10268  */
10269 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10270 {
10271         bool end_of_loop = false;
10272         char *ptr = NULL;
10273         int nr = 0;
10274         int rc;
10275
10276         if (!arg)
10277                 return -EINVAL;
10278
10279         while (!end_of_loop) {
10280                 int start_index;
10281                 int end_index;
10282                 int i;
10283                 char *endptr = NULL;
10284
10285                 rc = -EINVAL;
10286                 ptr = strchrnul(arg, ',');
10287                 end_of_loop = *ptr == '\0';
10288                 *ptr = '\0';
10289
10290                 start_index = strtol(arg, &endptr, 0);
10291                 if (endptr == arg) /* no data at all */
10292                         break;
10293                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10294                         break;
10295                 if (start_index < 0)
10296                         break;
10297
10298                 end_index = start_index;
10299                 if (*endptr == '-') {
10300                         end_index = strtol(endptr + 1, &endptr, 0);
10301                         if (*endptr != '\0')
10302                                 break;
10303                         if (end_index < start_index)
10304                                 break;
10305                 }
10306
10307                 for (i = start_index; i <= end_index && size > 0; i++) {
10308                         int j;
10309
10310                         /* remove duplicate */
10311                         for (j = 0; j < nr; j++) {
10312                                 if (ids[j] == i)
10313                                         break;
10314                         }
10315                         if (j == nr) { /* no duplicate */
10316                                 ids[nr++] = i;
10317                                 --size;
10318                         }
10319                 }
10320
10321                 if (size == 0 && i < end_index)
10322                         break;
10323
10324                 *ptr = ',';
10325                 arg = ++ptr;
10326                 rc = 0;
10327         }
10328         if (!end_of_loop && ptr)
10329                 *ptr = ',';
10330
10331         return rc < 0 ? rc : nr;
10332 }
10333
10334 /**
10335  * struct verify_mirror_id - Mirror id to be verified.
10336  * @mirror_id:   A specified mirror id.
10337  * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10338  */
10339 struct verify_mirror_id {
10340         __u16 mirror_id;
10341         bool is_valid_id;
10342 };
10343
10344 /**
10345  * compare_mirror_ids() - Compare mirror ids.
10346  * @layout: Mirror component list.
10347  * @cbdata: Callback data in verify_mirror_id structure.
10348  *
10349  * This is a callback function called by llapi_layout_comp_iterate()
10350  * to compare the specified mirror id with the one in the current
10351  * component of @layout. If they are the same, then the specified
10352  * mirror id is valid.
10353  *
10354  * Return: a negative error code on failure or
10355  *         LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10356  *         LLAPI_LAYOUT_ITER_STOP: Stop iteration
10357  */
10358 static inline
10359 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10360 {
10361         struct verify_mirror_id *mirror_id_cbdata =
10362                                  (struct verify_mirror_id *)cbdata;
10363         uint32_t mirror_id;
10364         int rc = 0;
10365
10366         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10367         if (rc < 0) {
10368                 rc = -errno;
10369                 fprintf(stderr,
10370                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
10371                         progname, strerror(errno));
10372                 return rc;
10373         }
10374
10375         if (mirror_id_cbdata->mirror_id == mirror_id) {
10376                 mirror_id_cbdata->is_valid_id = true;
10377                 return LLAPI_LAYOUT_ITER_STOP;
10378         }
10379
10380         return LLAPI_LAYOUT_ITER_CONT;
10381 }
10382
10383 /**
10384  * verify_mirror_ids() - Verify specified mirror ids.
10385  * @fname:      Mirrored file name.
10386  * @mirror_ids: Specified mirror ids to be verified.
10387  * @ids_nr:     Number of specified mirror ids.
10388  *
10389  * This function verifies that specified @mirror_ids are valid
10390  * in the mirrored file @fname.
10391  *
10392  * Return: 0 on success or a negative error code on failure.
10393  */
10394 static inline
10395 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10396 {
10397         struct llapi_layout *layout = NULL;
10398         struct verify_mirror_id mirror_id_cbdata = { 0 };
10399         struct stat stbuf;
10400         uint32_t flr_state;
10401         int i;
10402         int fd;
10403         int rc = 0;
10404         int rc2 = 0;
10405
10406         if (ids_nr <= 0)
10407                 return -EINVAL;
10408
10409         if (stat(fname, &stbuf) < 0) {
10410                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10411                         progname, fname, strerror(errno));
10412                 rc = -errno;
10413                 goto error;
10414         }
10415
10416         if (!S_ISREG(stbuf.st_mode)) {
10417                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10418                         progname, fname);
10419                 rc = -EINVAL;
10420                 goto error;
10421         }
10422
10423         fd = open(fname, O_DIRECT | O_RDONLY);
10424         if (fd < 0) {
10425                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10426                         progname, fname, strerror(errno));
10427                 rc = -errno;
10428                 goto error;
10429         }
10430
10431         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
10432         if (rc < 0) {
10433                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
10434                         progname, fname, strerror(errno));
10435                 goto close_fd;
10436         }
10437
10438         layout = llapi_layout_get_by_fd(fd, 0);
10439         if (!layout) {
10440                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10441                         progname, fname, strerror(errno));
10442                 rc = -errno;
10443                 llapi_lease_release(fd);
10444                 goto close_fd;
10445         }
10446
10447         rc = llapi_layout_flags_get(layout, &flr_state);
10448         if (rc < 0) {
10449                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10450                         progname, fname, strerror(errno));
10451                 rc = -errno;
10452                 goto free_layout;
10453         }
10454
10455         flr_state &= LCM_FL_FLR_MASK;
10456         switch (flr_state) {
10457         case LCM_FL_NONE:
10458                 rc = -EINVAL;
10459                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
10460                         progname, fname, llapi_layout_flags_string(flr_state));
10461                 goto free_layout;
10462         default:
10463                 break;
10464         }
10465
10466         rc2 = 0;
10467         for (i = 0; i < ids_nr; i++) {
10468                 mirror_id_cbdata.mirror_id = mirror_ids[i];
10469                 mirror_id_cbdata.is_valid_id = false;
10470
10471                 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
10472                                                &mirror_id_cbdata);
10473                 if (rc < 0) {
10474                         rc = -errno;
10475                         fprintf(stderr,
10476                                 "%s: '%s' failed to verify mirror id: %u.\n",
10477                                 progname, fname, mirror_ids[i]);
10478                         goto free_layout;
10479                 }
10480
10481                 if (!mirror_id_cbdata.is_valid_id) {
10482                         rc2 = -EINVAL;
10483                         fprintf(stderr,
10484                                 "%s: '%s' invalid specified mirror id: %u.\n",
10485                                 progname, fname, mirror_ids[i]);
10486                 }
10487         }
10488         rc = rc2;
10489
10490 free_layout:
10491         llapi_layout_free(layout);
10492         llapi_lease_release(fd);
10493 close_fd:
10494         close(fd);
10495 error:
10496         return rc;
10497 }
10498
10499 static inline
10500 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
10501                            __u16 *mirror_ids, int ids_nr)
10502 {
10503         struct llapi_resync_comp comp_array[1024] = { { 0 } };
10504         struct llapi_layout *layout;
10505         struct stat stbuf;
10506         uint32_t flr_state;
10507         uint64_t start;
10508         uint64_t end;
10509         int comp_size = 0;
10510         int idx;
10511         int fd;
10512         int rc;
10513         int rc2;
10514
10515         if (stat(fname, &stbuf) < 0) {
10516                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10517                         progname, fname, strerror(errno));
10518                 rc = -errno;
10519                 goto error;
10520         }
10521         if (!S_ISREG(stbuf.st_mode)) {
10522                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10523                         progname, fname);
10524                 rc = -EINVAL;
10525                 goto error;
10526         }
10527
10528         fd = open(fname, O_DIRECT | O_RDWR);
10529         if (fd < 0) {
10530                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10531                         progname, fname, strerror(errno));
10532                 rc = -errno;
10533                 goto error;
10534         }
10535
10536         layout = llapi_layout_get_by_fd(fd, 0);
10537         if (!layout) {
10538                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
10539                         progname, fname, strerror(errno));
10540                 rc = -errno;
10541                 goto close_fd;
10542         }
10543
10544         rc = llapi_layout_flags_get(layout, &flr_state);
10545         if (rc) {
10546                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
10547                         progname, fname, strerror(errno));
10548                 rc = -errno;
10549                 goto free_layout;
10550         }
10551
10552         flr_state &= LCM_FL_FLR_MASK;
10553         if (flr_state == LCM_FL_NONE) {
10554                 rc = -EINVAL;
10555                 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
10556                         progname, fname);
10557                 goto free_layout;
10558         }
10559
10560         /* get stale component info */
10561         comp_size = llapi_mirror_find_stale(layout, comp_array,
10562                                             ARRAY_SIZE(comp_array),
10563                                             mirror_ids, ids_nr);
10564         if (comp_size <= 0) {
10565                 rc = comp_size;
10566                 goto free_layout;
10567         }
10568
10569         ioc->lil_mode = LL_LEASE_WRLCK;
10570         ioc->lil_flags = LL_LEASE_RESYNC;
10571         rc = llapi_lease_set(fd, ioc);
10572         if (rc < 0) {
10573                 if (rc == -EALREADY)
10574                         rc = 0;
10575                 else
10576                         fprintf(stderr,
10577                             "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
10578                                 progname, fname, strerror(-rc));
10579                 goto free_layout;
10580         }
10581
10582         /* get the read range [start, end) */
10583         start = comp_array[0].lrc_start;
10584         end = comp_array[0].lrc_end;
10585         for (idx = 1; idx < comp_size; idx++) {
10586                 if (comp_array[idx].lrc_start < start)
10587                         start = comp_array[idx].lrc_start;
10588                 if (end < comp_array[idx].lrc_end)
10589                         end = comp_array[idx].lrc_end;
10590         }
10591
10592         rc = llapi_lease_check(fd);
10593         if (rc != LL_LEASE_WRLCK) {
10594                 fprintf(stderr, "%s: '%s' lost lease lock.\n",
10595                         progname, fname);
10596                 goto free_layout;
10597         }
10598
10599         rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
10600                                       start, end);
10601         if (rc < 0)
10602                 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
10603                         progname, fname, strerror(-rc));
10604
10605         /* need to do the lease unlock even resync fails */
10606         ioc->lil_mode = LL_LEASE_UNLCK;
10607         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
10608         ioc->lil_count = 0;
10609         for (idx = 0; idx < comp_size; idx++) {
10610                 if (comp_array[idx].lrc_synced) {
10611                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
10612                         ioc->lil_count++;
10613                 }
10614         }
10615
10616         rc2 = llapi_lease_set(fd, ioc);
10617         /**
10618          * llapi_lease_set returns lease mode when it request to unlock
10619          * the lease lock.
10620          */
10621         if (rc2 <= 0) {
10622                 /* rc2 == 0 means lost lease lock */
10623                 if (rc2 == 0 && rc == 0)
10624                         rc = -EBUSY;
10625                 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
10626                         progname, fname,
10627                         rc2 == 0 ? "lost lease lock" : strerror(-rc2));
10628         }
10629
10630 free_layout:
10631         llapi_layout_free(layout);
10632 close_fd:
10633         close(fd);
10634 error:
10635         return rc;
10636 }
10637
10638 static inline int lfs_mirror_resync(int argc, char **argv)
10639 {
10640         struct ll_ioc_lease *ioc = NULL;
10641         __u16 mirror_ids[128] = { 0 };
10642         int ids_nr = 0;
10643         int c;
10644         int rc = 0;
10645
10646         struct option long_opts[] = {
10647         { .val = 'o',   .name = "only",         .has_arg = required_argument },
10648         { .name = NULL } };
10649
10650         while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
10651                 switch (c) {
10652                 case 'o':
10653                         rc = parse_mirror_ids(mirror_ids,
10654                                         sizeof(mirror_ids) / sizeof(__u16),
10655                                         optarg);
10656                         if (rc < 0) {
10657                                 fprintf(stderr,
10658                                         "%s: bad mirror ids '%s'.\n",
10659                                         argv[0], optarg);
10660                                 goto error;
10661                         }
10662                         ids_nr = rc;
10663                         break;
10664                 default:
10665                         fprintf(stderr, "%s: options '%s' unrecognized.\n",
10666                                 argv[0], argv[optind - 1]);
10667                         rc = -EINVAL;
10668                         goto error;
10669                 }
10670         }
10671
10672         if (argc == optind) {
10673                 fprintf(stderr, "%s: no file name given.\n", argv[0]);
10674                 rc = CMD_HELP;
10675                 goto error;
10676         }
10677
10678         if (ids_nr > 0 && argc > optind + 1) {
10679                 fprintf(stderr,
10680                     "%s: option '--only' cannot be used upon multiple files.\n",
10681                         argv[0]);
10682                 rc = CMD_HELP;
10683                 goto error;
10684         }
10685
10686         if (ids_nr > 0) {
10687                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
10688                 if (rc < 0)
10689                         goto error;
10690         }
10691
10692         /* set the lease on the file */
10693         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
10694         if (!ioc) {
10695                 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
10696                         argv[0], strerror(errno));
10697                 rc = -errno;
10698                 goto error;
10699         }
10700
10701         for (; optind < argc; optind++) {
10702                 rc = lfs_mirror_resync_file(argv[optind], ioc,
10703                                             mirror_ids, ids_nr);
10704                 /* ignore previous file's error, continue with next file */
10705
10706                 /* reset ioc */
10707                 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
10708         }
10709
10710         free(ioc);
10711 error:
10712         return rc;
10713 }
10714
10715 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
10716 {
10717         struct llapi_layout *layout;
10718         int rc;
10719
10720         layout = llapi_layout_get_by_fd(fd, 0);
10721         if (!layout) {
10722                 fprintf(stderr, "could not get layout.\n");
10723                 return  -EINVAL;
10724         }
10725
10726         rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
10727         if (rc < 0) {
10728                 fprintf(stderr, "failed to iterate layout\n");
10729                 llapi_layout_free(layout);
10730
10731                 return rc;
10732         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
10733                 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
10734                 llapi_layout_free(layout);
10735
10736                 return -EINVAL;
10737         }
10738         llapi_layout_free(layout);
10739
10740         return 0;
10741 }
10742
10743 /**
10744  * Check whether two files are the same file
10745  * \retval      0  same file
10746  * \retval      1  not the same file
10747  * \retval      <0 error code
10748  */
10749 static inline int check_same_file(int fd, const char *f2)
10750 {
10751         struct stat stbuf1;
10752         struct stat stbuf2;
10753
10754         if (fstat(fd, &stbuf1) < 0)
10755                 return -errno;
10756
10757         if (stat(f2, &stbuf2) < 0)
10758                 return 1;
10759
10760         if (stbuf1.st_rdev == stbuf2.st_rdev &&
10761             stbuf1.st_ino == stbuf2.st_ino)
10762                 return 0;
10763
10764         return 1;
10765 }
10766
10767 static inline int lfs_mirror_read(int argc, char **argv)
10768 {
10769         int rc = CMD_HELP;
10770         __u16 mirror_id = 0;
10771         const char *outfile = NULL;
10772         char *fname;
10773         int fd = 0;
10774         int outfd;
10775         int c;
10776         void *buf;
10777         const size_t buflen = 4 << 20;
10778         off_t pos;
10779         struct option long_opts[] = {
10780         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
10781         { .val = 'o',   .name = "outfile",      .has_arg = required_argument },
10782         { .name = NULL } };
10783
10784         while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
10785                 char *end;
10786
10787                 switch (c) {
10788                 case 'N': {
10789                         unsigned long int id;
10790
10791                         errno = 0;
10792                         id = strtoul(optarg, &end, 0);
10793                         if (errno != 0 || *end != '\0' || id == 0 ||
10794                             id > UINT16_MAX) {
10795                                 fprintf(stderr,
10796                                         "%s %s: invalid mirror ID '%s'\n",
10797                                         progname, argv[0], optarg);
10798                                 return rc;
10799                         }
10800
10801                         mirror_id = (__u16)id;
10802                         break;
10803                 }
10804                 case 'o':
10805                         outfile = optarg;
10806                         break;
10807                 default:
10808                         fprintf(stderr, "%s: option '%s' unrecognized.\n",
10809                                 progname, argv[optind - 1]);
10810                         return -EINVAL;
10811                 }
10812         }
10813
10814         if (argc == optind) {
10815                 fprintf(stderr, "%s %s: no mirrored file provided\n",
10816                         progname, argv[0]);
10817                 return rc;
10818         } else if (argc > optind + 1) {
10819                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10820                 return rc;
10821         }
10822
10823         if (mirror_id == 0) {
10824                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10825                         progname, argv[0]);
10826                 return rc;
10827         }
10828
10829         /* open mirror file */
10830         fname = argv[optind];
10831         fd = open(fname, O_DIRECT | O_RDONLY);
10832         if (fd < 0) {
10833                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10834                         progname, argv[0], fname, strerror(errno));
10835                 return rc;
10836         }
10837
10838         /* verify mirror id */
10839         rc = verify_mirror_id_by_fd(fd, mirror_id);
10840         if (rc) {
10841                 fprintf(stderr,
10842                         "%s %s: cannot find mirror with ID %u in '%s'\n",
10843                         progname, argv[0], mirror_id, fname);
10844                 goto close_fd;
10845         }
10846
10847         /* open output file - O_EXCL ensures output is not the same as input */
10848         if (outfile) {
10849                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
10850                 if (outfd < 0) {
10851                         fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
10852                                 progname, argv[0], outfile, strerror(errno));
10853                         rc = -errno;
10854                         goto close_fd;
10855                 }
10856         } else {
10857                 outfd = STDOUT_FILENO;
10858         }
10859
10860         /* allocate buffer */
10861         rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
10862         if (rc) {
10863                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
10864                                 progname, argv[0], rc);
10865                 goto close_outfd;
10866         }
10867
10868         pos = 0;
10869         while (1) {
10870                 ssize_t bytes_read;
10871                 ssize_t written = 0;
10872
10873                 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
10874                 if (bytes_read < 0) {
10875                         rc = bytes_read;
10876                         fprintf(stderr,
10877                                 "%s %s: fail to read data from mirror %u: %s\n",
10878                                 progname, argv[0], mirror_id, strerror(-rc));
10879                         goto free_buf;
10880                 }
10881
10882                 /* EOF reached */
10883                 if (bytes_read == 0)
10884                         break;
10885
10886                 while (written < bytes_read) {
10887                         ssize_t written2;
10888
10889                         written2 = write(outfd, buf + written,
10890                                          bytes_read - written);
10891                         if (written2 < 0) {
10892                                 fprintf(stderr,
10893                                         "%s %s: fail to write %s: %s\n",
10894                                         progname, argv[0], outfile ? : "STDOUT",
10895                                         strerror(errno));
10896                                 rc = -errno;
10897                                 goto free_buf;
10898                         }
10899                         written += written2;
10900                 }
10901
10902                 if (written != bytes_read) {
10903                         fprintf(stderr,
10904                 "%s %s: written %ld bytes does not match with %ld read.\n",
10905                                 progname, argv[0], written, bytes_read);
10906                         rc = -EIO;
10907                         goto free_buf;
10908                 }
10909
10910                 pos += bytes_read;
10911         }
10912
10913         fsync(outfd);
10914         rc = 0;
10915
10916 free_buf:
10917         free(buf);
10918 close_outfd:
10919         if (outfile)
10920                 close(outfd);
10921 close_fd:
10922         close(fd);
10923
10924         return rc;
10925 }
10926
10927 static inline int lfs_mirror_write(int argc, char **argv)
10928 {
10929         int rc = CMD_HELP;
10930         __u16 mirror_id = 0;
10931         const char *inputfile = NULL;
10932         char *fname;
10933         int fd = 0;
10934         int inputfd;
10935         int c;
10936         void *buf;
10937         const size_t buflen = 4 << 20;
10938         off_t pos;
10939         size_t page_size = sysconf(_SC_PAGESIZE);
10940         struct ll_ioc_lease_id ioc;
10941
10942         struct option long_opts[] = {
10943         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
10944         { .val = 'i',   .name = "inputfile",    .has_arg = required_argument },
10945         { .name = NULL } };
10946
10947         while ((c = getopt_long(argc, argv, "N:i:", long_opts, NULL)) >= 0) {
10948                 char *end;
10949
10950                 switch (c) {
10951                 case 'N': {
10952                         unsigned long int id;
10953
10954                         errno = 0;
10955                         id = strtoul(optarg, &end, 0);
10956                         if (errno != 0 || *end != '\0' || id == 0 ||
10957                             id > UINT16_MAX) {
10958                                 fprintf(stderr,
10959                                         "%s %s: invalid mirror ID '%s'\n",
10960                                         progname, argv[0], optarg);
10961                                 return rc;
10962                         }
10963
10964                         mirror_id = (__u16)id;
10965                         break;
10966                 }
10967                 case 'i':
10968                         inputfile = optarg;
10969                         break;
10970                 default:
10971                         fprintf(stderr, "%s: option '%s' unrecognized\n",
10972                                 progname, argv[optind - 1]);
10973                         return -EINVAL;
10974                 }
10975         }
10976
10977         if (argc == optind) {
10978                 fprintf(stderr, "%s %s: no mirrored file provided\n",
10979                         progname, argv[0]);
10980                 return rc;
10981         } else if (argc > optind + 1) {
10982                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
10983                 return rc;
10984         }
10985
10986         if (mirror_id == 0) {
10987                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
10988                         progname, argv[0]);
10989                 return rc;
10990         }
10991
10992         /* open mirror file */
10993         fname = argv[optind];
10994         fd = open(fname, O_DIRECT | O_WRONLY);
10995         if (fd < 0) {
10996                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
10997                         progname, argv[0], fname, strerror(errno));
10998                 return rc;
10999         }
11000
11001         /* verify mirror id */
11002         rc = verify_mirror_id_by_fd(fd, mirror_id);
11003         if (rc) {
11004                 fprintf(stderr,
11005                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11006                         progname, argv[0], mirror_id, fname);
11007                 goto close_fd;
11008         }
11009
11010         /* open input file */
11011         if (inputfile) {
11012                 rc = check_same_file(fd, inputfile);
11013                 if (rc == 0) {
11014                         fprintf(stderr,
11015                         "%s %s: input file cannot be the mirrored file\n",
11016                                 progname, argv[0]);
11017                         goto close_fd;
11018                 }
11019                 if (rc < 0)
11020                         goto close_fd;
11021
11022                 inputfd = open(inputfile, O_RDONLY, 0644);
11023                 if (inputfd < 0) {
11024                         fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11025                                 progname, argv[0], inputfile, strerror(errno));
11026                         rc = -errno;
11027                         goto close_fd;
11028                 }
11029         } else {
11030                 inputfd = STDIN_FILENO;
11031         }
11032
11033         /* allocate buffer */
11034         rc = posix_memalign(&buf, page_size, buflen);
11035         if (rc) {
11036                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11037                         progname, argv[0], rc);
11038                 goto close_inputfd;
11039         }
11040
11041         /* prepare target mirror components instantiation */
11042         ioc.lil_mode = LL_LEASE_WRLCK;
11043         ioc.lil_flags = LL_LEASE_RESYNC;
11044         ioc.lil_mirror_id = mirror_id;
11045         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11046         if (rc < 0) {
11047                 fprintf(stderr,
11048                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11049                         progname, argv[0], fname, strerror(errno));
11050                 goto free_buf;
11051         }
11052
11053         pos = 0;
11054         while (1) {
11055                 ssize_t bytes_read;
11056                 ssize_t written;
11057                 size_t to_write;
11058
11059                 rc = llapi_lease_check(fd);
11060                 if (rc != LL_LEASE_WRLCK) {
11061                         fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11062                                 progname, argv[0], fname);
11063                         goto free_buf;
11064                 }
11065
11066                 bytes_read = read(inputfd, buf, buflen);
11067                 if (bytes_read < 0) {
11068                         rc = bytes_read;
11069                         fprintf(stderr,
11070                                 "%s %s: fail to read data from '%s': %s\n",
11071                                 progname, argv[0], inputfile ? : "STDIN",
11072                                 strerror(errno));
11073                         rc = -errno;
11074                         goto free_buf;
11075                 }
11076
11077                 /* EOF reached */
11078                 if (bytes_read == 0)
11079                         break;
11080
11081                 /* round up to page align to make direct IO happy. */
11082                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11083
11084                 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11085                                              pos);
11086                 if (written < 0) {
11087                         rc = written;
11088                         fprintf(stderr,
11089                               "%s %s: fail to write to mirror %u: %s\n",
11090                                 progname, argv[0], mirror_id,
11091                                 strerror(-rc));
11092                         goto free_buf;
11093                 }
11094
11095                 pos += bytes_read;
11096         }
11097
11098         if (pos & (page_size - 1)) {
11099                 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11100                 if (rc < 0)
11101                         goto free_buf;
11102         }
11103
11104         ioc.lil_mode = LL_LEASE_UNLCK;
11105         ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11106         ioc.lil_count = 0;
11107         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11108         if (rc <= 0) {
11109                 if (rc == 0)
11110                         rc = -EBUSY;
11111                 fprintf(stderr,
11112                         "%s %s: release lease lock of '%s' failed: %s\n",
11113                         progname, argv[0], fname, strerror(errno));
11114                 goto free_buf;
11115         }
11116
11117         rc = 0;
11118
11119 free_buf:
11120         free(buf);
11121 close_inputfd:
11122         if (inputfile)
11123                 close(inputfd);
11124 close_fd:
11125         close(fd);
11126
11127         return rc;
11128 }
11129
11130 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11131 {
11132         struct llapi_layout *layout;
11133         struct collect_ids_data cid = { .cid_ids = ids,
11134                                         .cid_count = 0,
11135                                         .cid_exclude = exclude_id, };
11136         int rc;
11137
11138         layout = llapi_layout_get_by_fd(fd, 0);
11139         if (!layout) {
11140                 fprintf(stderr, "could not get layout\n");
11141                 return -EINVAL;
11142         }
11143
11144         rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11145         if (rc < 0) {
11146                 fprintf(stderr, "failed to iterate layout\n");
11147                 llapi_layout_free(layout);
11148
11149                 return rc;
11150         }
11151         llapi_layout_free(layout);
11152
11153         return cid.cid_count;
11154 }
11155
11156 static inline int lfs_mirror_copy(int argc, char **argv)
11157 {
11158         int rc = CMD_HELP;
11159         __u16 read_mirror_id = 0;
11160         __u16 ids[128] = { 0 };
11161         int count = 0;
11162         struct llapi_layout *layout = NULL;
11163         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11164         int comp_size = 0;
11165         char *fname;
11166         int fd = 0;
11167         int c;
11168         int i;
11169         ssize_t copied;
11170         struct ll_ioc_lease *ioc = NULL;
11171         struct ll_ioc_lease_id *resync_ioc;
11172
11173         struct option long_opts[] = {
11174         { .val = 'i',   .name = "read-mirror",  .has_arg = required_argument },
11175         { .val = 'o',   .name = "write-mirror", .has_arg = required_argument },
11176         { .name = NULL } };
11177
11178         while ((c = getopt_long(argc, argv, "i:o:", long_opts, NULL)) >= 0) {
11179                 char *end;
11180
11181                 switch (c) {
11182                 case 'i': {
11183                         unsigned long int id;
11184
11185                         errno = 0;
11186                         id = strtoul(optarg, &end, 0);
11187                         if (errno != 0 || *end != '\0' || id == 0 ||
11188                             id > UINT16_MAX) {
11189                                 fprintf(stderr,
11190                                         "%s %s: invalid read mirror ID '%s'\n",
11191                                         progname, argv[0], optarg);
11192                                 return rc;
11193                         }
11194
11195                         read_mirror_id = (__u16)id;
11196                         break;
11197                 }
11198                 case 'o':
11199                         if (!strcmp(optarg, "-1")) {
11200                                 /* specify all other mirrors */
11201                                 ids[0] = (__u16)-1;
11202                                 count = 1;
11203                         } else {
11204                                 count = parse_mirror_ids((__u16 *)ids,
11205                                                          ARRAY_SIZE(ids),
11206                                                          optarg);
11207                                 if (count < 0)
11208                                         return rc;
11209                         }
11210                         break;
11211                 default:
11212                         fprintf(stderr, "%s: option '%s' unrecognized\n",
11213                                 progname, argv[optind - 1]);
11214                         return -EINVAL;
11215                 }
11216         }
11217
11218         if (argc == optind) {
11219                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11220                         progname, argv[0]);
11221                 return rc;
11222         } else if (argc > optind + 1) {
11223                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11224                 return rc;
11225         }
11226
11227         if (read_mirror_id == 0) {
11228                 fprintf(stderr,
11229                         "%s %s: no valid read mirror ID %d is provided\n",
11230                         progname, argv[0], read_mirror_id);
11231                 return rc;
11232         }
11233
11234         if (count == 0) {
11235                 fprintf(stderr,
11236                         "%s %s: no write mirror ID is provided\n",
11237                         progname, argv[0]);
11238                 return rc;
11239         }
11240
11241         for (i = 0; i < count; i++) {
11242                 if (read_mirror_id == ids[i]) {
11243                         fprintf(stderr,
11244                         "%s %s: read and write mirror ID cannot be the same\n",
11245                                 progname, argv[0]);
11246                         return rc;
11247                 }
11248         }
11249
11250         /* open mirror file */
11251         fname = argv[optind];
11252
11253         fd = open(fname, O_DIRECT | O_RDWR);
11254         if (fd < 0) {
11255                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11256                         progname, argv[0], fname, strerror(errno));
11257                 return rc;
11258         }
11259
11260         /* write to all other mirrors */
11261         if (ids[0] == (__u16)-1) {
11262                 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11263                 if (count <= 0) {
11264                         rc = count;
11265                         fprintf(stderr,
11266                         "%s %s: failed to get other mirror ids in '%s': %d\n",
11267                                 progname, argv[0], fname, rc);
11268                         goto close_fd;
11269                 }
11270         }
11271
11272         /* verify mirror id */
11273         rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11274         if (rc) {
11275                 fprintf(stderr,
11276                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11277                         progname, argv[0], read_mirror_id, fname);
11278                 goto close_fd;
11279         }
11280
11281         for (i = 0; i < count; i++) {
11282                 rc = verify_mirror_id_by_fd(fd, ids[i]);
11283                 if (rc) {
11284                         fprintf(stderr,
11285                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11286                                 progname, argv[0], ids[i], fname);
11287                         goto close_fd;
11288                 }
11289         }
11290
11291         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11292         if (!ioc) {
11293                 fprintf(stderr,
11294                         "%s %s: cannot alloc comp id array for ioc: %s\n",
11295                         progname, argv[0], strerror(errno));
11296                 rc = -errno;
11297                 goto close_fd;
11298         }
11299
11300         /* get stale component info */
11301         layout = llapi_layout_get_by_fd(fd, 0);
11302         if (!layout) {
11303                 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11304                         progname, argv[0], fname, strerror(errno));
11305                 rc = -errno;
11306                 goto free_ioc;
11307         }
11308         comp_size = llapi_mirror_find_stale(layout, comp_array,
11309                                             ARRAY_SIZE(comp_array),
11310                                             ids, count);
11311         llapi_layout_free(layout);
11312         if (comp_size < 0) {
11313                 rc = comp_size;
11314                 goto free_ioc;
11315         }
11316
11317         /* prepare target mirror components instantiation */
11318         resync_ioc = (struct ll_ioc_lease_id *)ioc;
11319         resync_ioc->lil_mode = LL_LEASE_WRLCK;
11320         resync_ioc->lil_flags = LL_LEASE_RESYNC;
11321         if (count == 1)
11322                 resync_ioc->lil_mirror_id = ids[0];
11323         else
11324                 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11325         rc = llapi_lease_set(fd, ioc);
11326         if (rc < 0) {
11327                 fprintf(stderr,
11328                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11329                         progname, argv[0], fname, strerror(errno));
11330                 goto free_ioc;
11331         }
11332
11333         copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11334         if (copied < 0) {
11335                 rc = copied;
11336                 fprintf(stderr, "%s %s: copy error: %d\n",
11337                         progname, argv[0], rc);
11338                 goto free_ioc;
11339         }
11340
11341         fprintf(stdout, "mirror copied successfully: ");
11342         for (i = 0; i < copied; i++)
11343                 fprintf(stdout, "%d ", ids[i]);
11344         fprintf(stdout, "\n");
11345
11346         ioc->lil_mode = LL_LEASE_UNLCK;
11347         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11348         ioc->lil_count = 0;
11349         for (i = 0; i < comp_size; i++) {
11350                 int j;
11351
11352                 for (j = 0; j < copied; j++) {
11353                         if (comp_array[i].lrc_mirror_id != ids[j])
11354                                 continue;
11355
11356                         ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11357                         ioc->lil_count++;
11358                 }
11359         }
11360         rc = llapi_lease_set(fd, ioc);
11361         if (rc <= 0) {
11362                 if (rc == 0)
11363                         rc = -EBUSY;
11364                 fprintf(stderr,
11365                         "%s %s: release lease lock of '%s' failed: %s\n",
11366                         progname, argv[0], fname, strerror(errno));
11367                 goto free_ioc;
11368         }
11369
11370         rc = 0;
11371
11372 free_ioc:
11373         free(ioc);
11374 close_fd:
11375         close(fd);
11376
11377         return rc;
11378 }
11379
11380 /**
11381  * struct verify_chunk - Mirror chunk to be verified.
11382  * @chunk:        [start, end) of the chunk.
11383  * @mirror_count: Number of mirror ids in @mirror_id array.
11384  * @mirror_id:    Array of valid mirror ids that cover the chunk.
11385  */
11386 struct verify_chunk {
11387         struct lu_extent chunk;
11388         unsigned int mirror_count;
11389         __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11390 };
11391
11392 /**
11393  * print_chunks() - Print chunk information.
11394  * @fname:       Mirrored file name.
11395  * @chunks:      Array of chunks.
11396  * @chunk_count: Number of chunks in @chunks array.
11397  *
11398  * This function prints [start, end) of each chunk in @chunks
11399  * for mirrored file @fname, and also prints the valid mirror ids
11400  * that cover the chunk.
11401  *
11402  * Return: void.
11403  */
11404 static inline
11405 void print_chunks(const char *fname, struct verify_chunk *chunks,
11406                   int chunk_count)
11407 {
11408         int i;
11409         int j;
11410
11411         fprintf(stdout, "Chunks to be verified in %s:\n", fname);
11412         for (i = 0; i < chunk_count; i++) {
11413                 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
11414
11415                 if (chunks[i].mirror_count == 0)
11416                         fprintf(stdout, "\t[");
11417                 else {
11418                         fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
11419                         for (j = 1; j < chunks[i].mirror_count; j++)
11420                                 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
11421                 }
11422                 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
11423         }
11424         fprintf(stdout, "\n");
11425 }
11426
11427 /**
11428  * print_checksums() - Print CRC-32 checksum values.
11429  * @chunk: A chunk and its corresponding valid mirror ids.
11430  * @crc:   CRC-32 checksum values on the chunk for each valid mirror.
11431  *
11432  * This function prints CRC-32 checksum values on @chunk for
11433  * each valid mirror that covers it.
11434  *
11435  * Return: void.
11436  */
11437 static inline
11438 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
11439 {
11440         int i;
11441
11442         fprintf(stdout,
11443                 "CRC-32 checksum value for chunk "DEXT":\n",
11444                 PEXT(&chunk->chunk));
11445         for (i = 0; i < chunk->mirror_count; i++)
11446                 fprintf(stdout, "Mirror %u:\t%#lx\n",
11447                         chunk->mirror_id[i], crc[i]);
11448         fprintf(stdout, "\n");
11449 }
11450
11451 /**
11452  * filter_mirror_id() - Filter specified mirror ids.
11453  * @chunks:      Array of chunks.
11454  * @chunk_count: Number of chunks in @chunks array.
11455  * @mirror_ids:  Specified mirror ids to be verified.
11456  * @ids_nr:      Number of specified mirror ids.
11457  *
11458  * This function scans valid mirror ids that cover each chunk in @chunks
11459  * and filters specified mirror ids.
11460  *
11461  * Return: void.
11462  */
11463 static inline
11464 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
11465                       __u16 *mirror_ids, int ids_nr)
11466 {
11467         int i;
11468         int j;
11469         int k;
11470         __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11471         unsigned int valid_count = 0;
11472
11473         for (i = 0; i < chunk_count; i++) {
11474                 if (chunks[i].mirror_count == 0)
11475                         continue;
11476
11477                 valid_count = 0;
11478                 for (j = 0; j < ids_nr; j++) {
11479                         for (k = 0; k < chunks[i].mirror_count; k++) {
11480                                 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
11481                                         valid_id[valid_count] = mirror_ids[j];
11482                                         valid_count++;
11483                                         break;
11484                                 }
11485                         }
11486                 }
11487
11488                 memcpy(chunks[i].mirror_id, valid_id,
11489                        sizeof(__u16) * valid_count);
11490                 chunks[i].mirror_count = valid_count;
11491         }
11492 }
11493
11494 /**
11495  * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
11496  * @layout:      Mirror component list.
11497  * @chunks:      Array of chunks.
11498  * @chunks_size: Array size of @chunks.
11499  *
11500  * This function scans the components in @layout from offset 0 to LUSTRE_EOF
11501  * to find out chunk segments and store them in @chunks array.
11502  *
11503  * The @mirror_id array in each element of @chunks will store the valid
11504  * mirror ids that cover the chunk. If a mirror component covering the
11505  * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
11506  * will not be stored into the @mirror_id array, and the chunk for that
11507  * mirror will not be verified.
11508  *
11509  * The @mirror_count in each element of @chunks will store the number of
11510  * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
11511  * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
11512  * indicates the chunk is valid in only one mirror. In both cases, the
11513  * chunk will not be verified.
11514  *
11515  * Here is an example:
11516  *
11517  *  0      1M     2M     3M     4M           EOF
11518  *  +------+-------------+--------------------+
11519  *  |      |             |      S             |       mirror1
11520  *  +------+------+------+------+-------------+
11521  *  |             |   S  |   S  |             |       mirror2
11522  *  +-------------+------+------+-------------+
11523  *
11524  * prepared @chunks array will contain 5 elements:
11525  * (([0, 1M), [1, 2], 2),
11526  *  ([1M, 2M), [1, 2], 2),
11527  *  ([2M, 3M), [1], 1),
11528  *  ([3M, 4M], [], 0),
11529  *  ([4M, EOF), [2], 1))
11530  *
11531  * Return: the actual array size of @chunks on success
11532  *         or a negative error code on failure.
11533  */
11534 static inline
11535 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
11536                              struct verify_chunk *chunks,
11537                              size_t chunks_size)
11538 {
11539         uint64_t start;
11540         uint64_t end;
11541         uint32_t mirror_id;
11542         uint32_t flags;
11543         int idx = 0;
11544         int i = 0;
11545         int rc = 0;
11546
11547         memset(chunks, 0, sizeof(*chunks) * chunks_size);
11548
11549         while (1) {
11550                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
11551                 if (rc < 0) {
11552                         fprintf(stderr,
11553                                 "%s: move to the first layout component: %s.\n",
11554                                 progname, strerror(errno));
11555                         goto error;
11556                 }
11557
11558                 i = 0;
11559                 rc = 0;
11560                 chunks[idx].chunk.e_end = LUSTRE_EOF;
11561                 while (rc == 0) {
11562                         rc = llapi_layout_comp_extent_get(layout, &start, &end);
11563                         if (rc < 0) {
11564                                 fprintf(stderr,
11565                                         "%s: llapi_layout_comp_extent_get failed: %s.\n",
11566                                         progname, strerror(errno));
11567                                 goto error;
11568                         }
11569
11570                         if (start > chunks[idx].chunk.e_start ||
11571                             end <= chunks[idx].chunk.e_start)
11572                                 goto next;
11573
11574                         if (end < chunks[idx].chunk.e_end)
11575                                 chunks[idx].chunk.e_end = end;
11576
11577                         rc = llapi_layout_comp_flags_get(layout, &flags);
11578                         if (rc < 0) {
11579                                 fprintf(stderr,
11580                                         "%s: llapi_layout_comp_flags_get failed: %s.\n",
11581                                         progname, strerror(errno));
11582                                 goto error;
11583                         }
11584
11585                         if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
11586                                 goto next;
11587
11588                         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
11589                         if (rc < 0) {
11590                                 fprintf(stderr,
11591                                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
11592                                         progname, strerror(errno));
11593                                 goto error;
11594                         }
11595
11596                         chunks[idx].mirror_id[i] = mirror_id;
11597                         i++;
11598                         if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
11599                                 fprintf(stderr,
11600                                         "%s: mirror_id array is too small.\n",
11601                                         progname);
11602                                 rc = -EINVAL;
11603                                 goto error;
11604                         }
11605
11606 next:
11607                         rc = llapi_layout_comp_use(layout,
11608                                                    LLAPI_LAYOUT_COMP_USE_NEXT);
11609                         if (rc < 0) {
11610                                 fprintf(stderr,
11611                                         "%s: move to the next layout component: %s.\n",
11612                                         progname, strerror(errno));
11613                                 goto error;
11614                         }
11615                 } /* loop through all components */
11616
11617                 chunks[idx].mirror_count = i;
11618
11619                 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
11620                         break;
11621
11622                 idx++;
11623                 if (idx >= chunks_size) {
11624                         fprintf(stderr, "%s: chunks array is too small.\n",
11625                                 progname);
11626                         rc = -EINVAL;
11627                         goto error;
11628                 }
11629
11630                 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
11631         }
11632
11633 error:
11634         return rc < 0 ? rc : idx + 1;
11635 }
11636
11637 /**
11638  * lfs_mirror_verify_chunk() - Verify a chunk.
11639  * @fd:        File descriptor of the mirrored file.
11640  * @file_size: Size of the mirrored file.
11641  * @chunk:     A chunk and its corresponding valid mirror ids.
11642  * @verbose:   Verbose mode.
11643  *
11644  * This function verifies a @chunk contains exactly the same data
11645  * ammong the mirrors that cover it.
11646  *
11647  * If @verbose is specified, then the function will print where the
11648  * differences are if the data do not match. Otherwise, it will
11649  * just return an error in that case.
11650  *
11651  * Return: 0 on success or a negative error code on failure.
11652  */
11653 static inline
11654 int lfs_mirror_verify_chunk(int fd, size_t file_size,
11655                             struct verify_chunk *chunk, int verbose)
11656 {
11657         const size_t buflen = 4 * 1024 * 1024; /* 4M */
11658         void *buf;
11659         size_t page_size = sysconf(_SC_PAGESIZE);
11660         ssize_t bytes_read;
11661         ssize_t bytes_done;
11662         size_t count;
11663         off_t pos;
11664         unsigned long crc;
11665         unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11666         int i;
11667         int rc = 0;
11668
11669         if (file_size == 0)
11670                 return 0;
11671
11672         rc = posix_memalign(&buf, page_size, buflen);
11673         if (rc) /* error code is returned directly */
11674                 return -rc;
11675
11676         if (verbose > 1) {
11677                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11678                         PEXT(&chunk->chunk));
11679                 for (i = 0; i < chunk->mirror_count; i++)
11680                         fprintf(stdout, " %u", chunk->mirror_id[i]);
11681                 fprintf(stdout, "\n");
11682         }
11683
11684         bytes_done = 0;
11685         count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
11686         pos = chunk->chunk.e_start;
11687         while (bytes_done < count) {
11688                 /* compute initial CRC-32 checksum */
11689                 crc = crc32(0L, Z_NULL, 0);
11690                 memset(crc_array, 0, sizeof(crc_array));
11691
11692                 bytes_read = 0;
11693                 for (i = 0; i < chunk->mirror_count; i++) {
11694                         bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
11695                                                        buf, buflen, pos);
11696                         if (bytes_read < 0) {
11697                                 rc = bytes_read;
11698                                 fprintf(stderr,
11699                                         "%s: failed to read data from mirror %u: %s.\n",
11700                                         progname, chunk->mirror_id[i],
11701                                         strerror(-rc));
11702                                 goto error;
11703                         }
11704
11705                         /* compute new CRC-32 checksum */
11706                         crc_array[i] = crc32(crc, buf, bytes_read);
11707                 }
11708
11709                 if (verbose)
11710                         print_checksums(chunk, crc_array);
11711
11712                 /* compare CRC-32 checksum values */
11713                 for (i = 1; i < chunk->mirror_count; i++) {
11714                         if (crc_array[i] != crc_array[0]) {
11715                                 rc = -EINVAL;
11716
11717                                 fprintf(stderr,
11718                                         "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
11719                                         progname, PEXT(&chunk->chunk),
11720                                         chunk->mirror_id[0],
11721                                         chunk->mirror_id[i]);
11722                         }
11723                 }
11724
11725                 pos += bytes_read;
11726                 bytes_done += bytes_read;
11727         }
11728
11729         if (verbose > 1 && rc == 0) {
11730                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
11731                         PEXT(&chunk->chunk));
11732                 for (i = 0; i < chunk->mirror_count; i++)
11733                         fprintf(stdout, " %u", chunk->mirror_id[i]);
11734                 fprintf(stdout, " PASS\n\n");
11735         }
11736
11737 error:
11738         free(buf);
11739         return rc;
11740 }
11741
11742 /**
11743  * lfs_mirror_verify_file() - Verify a mirrored file.
11744  * @fname:      Mirrored file name.
11745  * @mirror_ids: Specified mirror ids to be verified.
11746  * @ids_nr:     Number of specified mirror ids.
11747  * @verbose:    Verbose mode.
11748  *
11749  * This function verifies that each SYNC mirror of a mirrored file
11750  * specified by @fname contains exactly the same data.
11751  *
11752  * If @mirror_ids is specified, then the function will verify the
11753  * mirrors specified by @mirror_ids contain exactly the same data.
11754  *
11755  * If @verbose is specified, then the function will print where the
11756  * differences are if the data do not match. Otherwise, it will
11757  * just return an error in that case.
11758  *
11759  * Return: 0 on success or a negative error code on failure.
11760  */
11761 static inline
11762 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
11763                            int verbose)
11764 {
11765         struct verify_chunk chunks_array[1024] = { };
11766         struct llapi_layout *layout = NULL;
11767         struct stat stbuf;
11768         uint32_t flr_state;
11769         int fd;
11770         int chunk_count = 0;
11771         int idx = 0;
11772         int rc = 0;
11773         int rc1 = 0;
11774         int rc2 = 0;
11775
11776         if (stat(fname, &stbuf) < 0) {
11777                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11778                         progname, fname, strerror(errno));
11779                 rc = -errno;
11780                 goto error;
11781         }
11782
11783         if (!S_ISREG(stbuf.st_mode)) {
11784                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11785                         progname, fname);
11786                 rc = -EINVAL;
11787                 goto error;
11788         }
11789
11790         if (stbuf.st_size == 0) {
11791                 if (verbose)
11792                         fprintf(stdout, "%s: '%s' file size is 0.\n",
11793                                 progname, fname);
11794                 rc = 0;
11795                 goto error;
11796         }
11797
11798         fd = open(fname, O_DIRECT | O_RDONLY);
11799         if (fd < 0) {
11800                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11801                         progname, fname, strerror(errno));
11802                 rc = -errno;
11803                 goto error;
11804         }
11805
11806         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11807         if (rc < 0) {
11808                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11809                         progname, fname, strerror(errno));
11810                 goto close_fd;
11811         }
11812
11813         layout = llapi_layout_get_by_fd(fd, 0);
11814         if (!layout) {
11815                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11816                         progname, fname, strerror(errno));
11817                 rc = -errno;
11818                 llapi_lease_release(fd);
11819                 goto close_fd;
11820         }
11821
11822         rc = llapi_layout_flags_get(layout, &flr_state);
11823         if (rc < 0) {
11824                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11825                         progname, fname, strerror(errno));
11826                 rc = -errno;
11827                 goto free_layout;
11828         }
11829
11830         flr_state &= LCM_FL_FLR_MASK;
11831         switch (flr_state) {
11832         case LCM_FL_NONE:
11833                 rc = -EINVAL;
11834                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11835                         progname, fname, llapi_layout_flags_string(flr_state));
11836                 goto free_layout;
11837         default:
11838                 break;
11839         }
11840
11841         /* find out mirror chunks to be verified */
11842         chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
11843                                                ARRAY_SIZE(chunks_array));
11844         if (chunk_count < 0) {
11845                 rc = chunk_count;
11846                 goto free_layout;
11847         }
11848
11849         if (ids_nr > 0)
11850                 /* filter specified mirror ids */
11851                 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
11852
11853         if (verbose > 2)
11854                 print_chunks(fname, chunks_array, chunk_count);
11855
11856         for (idx = 0; idx < chunk_count; idx++) {
11857                 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
11858                         if (verbose)
11859                                 fprintf(stdout,
11860                                         "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
11861                                         progname, fname,
11862                                         PEXT(&chunks_array[idx].chunk),
11863                                         (unsigned long long)stbuf.st_size);
11864                         break;
11865                 }
11866
11867                 if (chunks_array[idx].mirror_count == 0) {
11868                         fprintf(stderr,
11869                                 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
11870                                 progname, fname,
11871                                 PEXT(&chunks_array[idx].chunk));
11872                         if (verbose) {
11873                                 fprintf(stderr, "skipped\n");
11874                                 continue;
11875                         }
11876                         rc = -EINVAL;
11877                         fprintf(stderr, "failed\n");
11878                         goto free_layout;
11879                 }
11880
11881                 if (chunks_array[idx].mirror_count == 1) {
11882                         if (verbose)
11883                                 fprintf(stdout,
11884                                         "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
11885                                         progname, fname,
11886                                         PEXT(&chunks_array[idx].chunk),
11887                                         chunks_array[idx].mirror_id[0]);
11888                         continue;
11889                 }
11890
11891                 rc = llapi_lease_check(fd);
11892                 if (rc != LL_LEASE_RDLCK) {
11893                         fprintf(stderr, "%s: '%s' lost lease lock.\n",
11894                                 progname, fname);
11895                         goto free_layout;
11896                 }
11897
11898                 /* verify one chunk */
11899                 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
11900                                               &chunks_array[idx], verbose);
11901                 if (rc1 < 0) {
11902                         rc2 = rc1;
11903                         if (!verbose) {
11904                                 rc = rc1;
11905                                 goto free_layout;
11906                         }
11907                 }
11908         }
11909
11910         if (rc2 < 0)
11911                 rc = rc2;
11912
11913 free_layout:
11914         llapi_layout_free(layout);
11915         llapi_lease_release(fd);
11916 close_fd:
11917         close(fd);
11918 error:
11919         return rc;
11920 }
11921
11922 /**
11923  * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
11924  * @argc: The count of lfs mirror verify command line arguments.
11925  * @argv: Array of strings for lfs mirror verify command line arguments.
11926  *
11927  * This function parses lfs mirror verify command and verifies the
11928  * specified mirrored file(s).
11929  *
11930  * Return: 0 on success or a negative error code on failure.
11931  */
11932 static inline int lfs_mirror_verify(int argc, char **argv)
11933 {
11934         __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
11935         int ids_nr = 0;
11936         int c;
11937         int verbose = 0;
11938         int rc = 0;
11939         int rc1 = 0;
11940         char cmd[PATH_MAX];
11941
11942         struct option long_opts[] = {
11943         { .val = 'o',   .name = "only",         .has_arg = required_argument },
11944         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
11945         { .name = NULL } };
11946
11947         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11948         progname = cmd;
11949         while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
11950                 switch (c) {
11951                 case 'o':
11952                         rc = parse_mirror_ids(mirror_ids,
11953                                               ARRAY_SIZE(mirror_ids),
11954                                               optarg);
11955                         if (rc < 0) {
11956                                 fprintf(stderr,
11957                                         "%s: bad mirror ids '%s'.\n",
11958                                         progname, optarg);
11959                                 goto error;
11960                         }
11961                         ids_nr = rc;
11962                         if (ids_nr < 2) {
11963                                 fprintf(stderr,
11964                                         "%s: at least 2 mirror ids needed with '--only' option.\n",
11965                                         progname);
11966                                 rc = CMD_HELP;
11967                                 goto error;
11968                         }
11969                         break;
11970                 case 'v':
11971                         verbose++;
11972                         break;
11973                 default:
11974                         fprintf(stderr, "%s: option '%s' unrecognized.\n",
11975                                 progname, argv[optind - 1]);
11976                         rc = -EINVAL;
11977                         goto error;
11978                 }
11979         }
11980
11981         if (argc == optind) {
11982                 fprintf(stderr, "%s: no file name given.\n", progname);
11983                 rc = CMD_HELP;
11984                 goto error;
11985         }
11986
11987         if (ids_nr > 0 && argc > optind + 1) {
11988                 fprintf(stderr,
11989                         "%s: '--only' cannot be used upon multiple files.\n",
11990                         progname);
11991                 rc = CMD_HELP;
11992                 goto error;
11993         }
11994
11995         if (ids_nr > 0) {
11996                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11997                 if (rc < 0)
11998                         goto error;
11999         }
12000
12001         rc = 0;
12002         for (; optind < argc; optind++) {
12003                 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12004                                              verbose);
12005                 if (rc1 < 0)
12006                         rc = rc1;
12007         }
12008 error:
12009         return rc;
12010 }
12011
12012 /**
12013  * lfs_mirror() - Parse and execute lfs mirror commands.
12014  * @argc: The count of lfs mirror command line arguments.
12015  * @argv: Array of strings for lfs mirror command line arguments.
12016  *
12017  * This function parses lfs mirror commands and performs the
12018  * corresponding functions specified in mirror_cmdlist[].
12019  *
12020  * Return: 0 on success or an error code on failure.
12021  */
12022 static int lfs_mirror(int argc, char **argv)
12023 {
12024         char cmd[PATH_MAX];
12025         int rc = 0;
12026
12027         setlinebuf(stdout);
12028
12029         Parser_init("lfs-mirror > ", mirror_cmdlist);
12030
12031         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12032         progname = cmd;
12033         program_invocation_short_name = cmd;
12034         if (argc > 1)
12035                 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12036         else
12037                 rc = Parser_commands();
12038
12039         return rc < 0 ? -rc : rc;
12040 }
12041
12042 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12043 {
12044 #if __BYTE_ORDER == __BIG_ENDIAN
12045         __swab16s(&attrs->lsa_valid);
12046         __swab64s(&attrs->lsa_size);
12047         __swab64s(&attrs->lsa_blocks);
12048 #endif
12049 }
12050
12051 enum lfs_som_type {
12052         LFS_SOM_SIZE = 0x1,
12053         LFS_SOM_BLOCKS = 0x2,
12054         LFS_SOM_FLAGS = 0x4,
12055         LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12056                            LFS_SOM_FLAGS,
12057 };
12058
12059 static int lfs_getsom(int argc, char **argv)
12060 {
12061         const char *path;
12062         struct lustre_som_attrs *attrs;
12063         char buf[sizeof(*attrs) + 64];
12064         enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12065         int rc = 0, c;
12066
12067         while ((c = getopt(argc, argv, "sbf")) != -1) {
12068                 switch (c) {
12069                 case 's':
12070                         type = LFS_SOM_SIZE;
12071                         break;
12072                 case 'b':
12073                         type = LFS_SOM_BLOCKS;
12074                         break;
12075                 case 'f':
12076                         type = LFS_SOM_FLAGS;
12077                         break;
12078                 default:
12079                         fprintf(stderr, "%s: invalid option '%c'\n",
12080                                 progname, optopt);
12081                         return CMD_HELP;
12082                 }
12083         }
12084
12085         argc -= optind;
12086         argv += optind;
12087
12088         if (argc != 1) {
12089                 fprintf(stderr, "%s: %s\n",
12090                         progname, argc == 0 ? "miss file target" :
12091                         "input more than 2 files");
12092                 return CMD_HELP;
12093         }
12094
12095         path = argv[0];
12096         attrs = (void *)buf;
12097         rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12098         if (rc < 0) {
12099                 rc = -errno;
12100                 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12101                         argv[0], strerror(errno), errno);
12102                 return rc;
12103         }
12104
12105         lustre_som_swab(attrs);
12106
12107         switch (type) {
12108         case LFS_SOM_ATTR_ALL:
12109                 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12110                        path, (unsigned long long)attrs->lsa_size,
12111                        (unsigned long long)attrs->lsa_blocks,
12112                        attrs->lsa_valid);
12113                 break;
12114         case LFS_SOM_SIZE:
12115                 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12116                 break;
12117         case LFS_SOM_BLOCKS:
12118                 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12119                 break;
12120         case LFS_SOM_FLAGS:
12121                 printf("%x\n", attrs->lsa_valid);
12122                 break;
12123         default:
12124                 fprintf(stderr, "%s: unknown option\n", progname);
12125                 return CMD_HELP;
12126         }
12127
12128         return 0;
12129 }
12130
12131 /**
12132  * lfs_mirror_list_commands() - List lfs mirror commands.
12133  * @argc: The count of command line arguments.
12134  * @argv: Array of strings for command line arguments.
12135  *
12136  * This function lists lfs mirror commands defined in mirror_cmdlist[].
12137  *
12138  * Return: 0 on success.
12139  */
12140 static int lfs_mirror_list_commands(int argc, char **argv)
12141 {
12142         char buffer[81] = "";
12143
12144         Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
12145                              NULL, 0, 4);
12146
12147         return 0;
12148 }
12149
12150 static int lfs_pcc_attach(int argc, char **argv)
12151 {
12152         struct option long_opts[] = {
12153         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12154         { .name = NULL } };
12155         int c;
12156         int rc = 0;
12157         __u32 archive_id = 0;
12158         const char *path;
12159         char *end;
12160         char fullpath[PATH_MAX];
12161         enum lu_pcc_type type = LU_PCC_READWRITE;
12162
12163         optind = 0;
12164         while ((c = getopt_long(argc, argv, "i:",
12165                                 long_opts, NULL)) != -1) {
12166                 switch (c) {
12167                 case 'i':
12168                         errno = 0;
12169                         archive_id = strtoul(optarg, &end, 0);
12170                         if (errno != 0 || *end != '\0' ||
12171                             archive_id == 0 || archive_id > UINT32_MAX) {
12172                                 fprintf(stderr,
12173                                         "error: %s: bad archive ID '%s'\n",
12174                                         argv[0], optarg);
12175                                 return CMD_HELP;
12176                         }
12177                         break;
12178                 case '?':
12179                         return CMD_HELP;
12180                 default:
12181                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12182                                 argv[0], argv[optind - 1]);
12183                         return CMD_HELP;
12184                 }
12185         }
12186
12187         if (archive_id == 0) {
12188                 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12189                 return CMD_HELP;
12190         }
12191
12192         if (argc <= optind) {
12193                 fprintf(stderr, "%s: must specify one or more file names\n",
12194                         argv[0]);
12195                 return CMD_HELP;
12196         }
12197
12198         while (optind < argc) {
12199                 int rc2;
12200
12201                 path = argv[optind++];
12202                 if (!realpath(path, fullpath)) {
12203                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12204                                 argv[0], path, strerror(errno));
12205                         if (rc == 0)
12206                                 rc = -EINVAL;
12207                         continue;
12208                 }
12209
12210                 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12211                 if (rc2 < 0) {
12212                         fprintf(stderr,
12213                                 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12214                                 argv[0], path, archive_id, strerror(-rc2));
12215                         if (rc == 0)
12216                                 rc = rc2;
12217                 }
12218         }
12219         return rc;
12220 }
12221
12222 static int lfs_pcc_attach_fid(int argc, char **argv)
12223 {
12224         struct option long_opts[] = {
12225         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12226         { .val = 'm',   .name = "mnt",  .has_arg = required_argument },
12227         { .name = NULL } };
12228         char                     short_opts[] = "i:m:";
12229         int                      c;
12230         int                      rc = 0;
12231         __u32                    archive_id = 0;
12232         char                    *end;
12233         const char              *mntpath = NULL;
12234         const char              *fidstr;
12235         enum lu_pcc_type         type = LU_PCC_READWRITE;
12236
12237         optind = 0;
12238         while ((c = getopt_long(argc, argv, short_opts,
12239                                 long_opts, NULL)) != -1) {
12240                 switch (c) {
12241                 case 'i':
12242                         errno = 0;
12243                         archive_id = strtoul(optarg, &end, 0);
12244                         if (errno != 0 || *end != '\0' ||
12245                             archive_id > UINT32_MAX) {
12246                                 fprintf(stderr,
12247                                         "error: %s: bad archive ID '%s'\n",
12248                                         argv[0], optarg);
12249                                 return CMD_HELP;
12250                         }
12251                         break;
12252                 case 'm':
12253                         mntpath = optarg;
12254                         break;
12255                 case '?':
12256                         return CMD_HELP;
12257                 default:
12258                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12259                                 argv[0], argv[optind - 1]);
12260                         return CMD_HELP;
12261                 }
12262         }
12263
12264         if (archive_id == 0) {
12265                 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12266                 return CMD_HELP;
12267         }
12268
12269         if (!mntpath) {
12270                 fprintf(stderr, "%s: must specify Lustre mount point\n",
12271                         argv[0]);
12272                 return CMD_HELP;
12273         }
12274
12275         if (argc <= optind) {
12276                 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12277                 return CMD_HELP;
12278         }
12279
12280         while (optind < argc) {
12281                 int rc2;
12282
12283                 fidstr = argv[optind++];
12284
12285                 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12286                                                archive_id, type);
12287                 if (rc2 < 0) {
12288                         fprintf(stderr,
12289                                 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12290                                 argv[0], fidstr, mntpath, archive_id,
12291                                 strerror(rc2));
12292                 }
12293                 if (rc == 0 && rc2 < 0)
12294                         rc = rc2;
12295         }
12296         return rc;
12297 }
12298
12299 static int lfs_pcc_detach(int argc, char **argv)
12300 {
12301         struct option long_opts[] = {
12302         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12303         { .name = NULL } };
12304         char                     short_opts[] = "k";
12305         int                      c;
12306         int                      rc = 0;
12307         const char              *path;
12308         char                     fullpath[PATH_MAX];
12309         __u32                    detach_opt = PCC_DETACH_OPT_UNCACHE;
12310
12311         optind = 0;
12312         while ((c = getopt_long(argc, argv, short_opts,
12313                                 long_opts, NULL)) != -1) {
12314                 switch (c) {
12315                 case 'k':
12316                         detach_opt = PCC_DETACH_OPT_NONE;
12317                         break;
12318                 case '?':
12319                         return CMD_HELP;
12320                 default:
12321                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12322                                 argv[0], argv[optind - 1]);
12323                         return CMD_HELP;
12324                 }
12325         }
12326
12327         while (optind < argc) {
12328                 int rc2;
12329
12330                 path = argv[optind++];
12331                 if (!realpath(path, fullpath)) {
12332                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12333                                 argv[0], path, strerror(errno));
12334                         if (rc == 0)
12335                                 rc = -EINVAL;
12336                         continue;
12337                 }
12338
12339                 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12340                 if (rc2 < 0) {
12341                         rc2 = -errno;
12342                         fprintf(stderr,
12343                                 "%s: cannot detach '%s' from PCC: %s\n",
12344                                 argv[0], path, strerror(errno));
12345                         if (rc == 0)
12346                                 rc = rc2;
12347                 }
12348         }
12349         return rc;
12350 }
12351
12352 static int lfs_pcc_detach_fid(int argc, char **argv)
12353 {
12354         struct option long_opts[] = {
12355         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12356         { .name = NULL } };
12357         char             short_opts[] = "k";
12358         int              c;
12359         int              rc = 0;
12360         const char      *fid;
12361         const char      *mntpath;
12362         __u32            detach_opt = PCC_DETACH_OPT_UNCACHE;
12363
12364         optind = 0;
12365         while ((c = getopt_long(argc, argv, short_opts,
12366                                 long_opts, NULL)) != -1) {
12367                 switch (c) {
12368                 case 'k':
12369                         detach_opt = PCC_DETACH_OPT_NONE;
12370                         break;
12371                 case '?':
12372                         return CMD_HELP;
12373                 default:
12374                         fprintf(stderr, "%s: option '%s' unrecognized\n",
12375                                 argv[0], argv[optind - 1]);
12376                         return CMD_HELP;
12377                 }
12378         }
12379
12380         mntpath = argv[optind++];
12381
12382         while (optind < argc) {
12383                 int rc2;
12384
12385                 fid = argv[optind++];
12386
12387                 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12388                 if (rc2 < 0) {
12389                         fprintf(stderr,
12390                                 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12391                                 argv[0], fid, mntpath, strerror(-rc2));
12392                         if (rc == 0)
12393                                 rc = rc2;
12394                 }
12395         }
12396         return rc;
12397 }
12398
12399 static int lfs_pcc_state(int argc, char **argv)
12400 {
12401         int                      rc = 0;
12402         const char              *path;
12403         char                     fullpath[PATH_MAX];
12404         struct lu_pcc_state      state;
12405
12406         optind = 1;
12407
12408         if (argc <= 1) {
12409                 fprintf(stderr, "%s: must specify one or more file names\n",
12410                         argv[0]);
12411                 return CMD_HELP;
12412         }
12413
12414         while (optind < argc) {
12415                 int rc2;
12416
12417                 path = argv[optind++];
12418                 if (!realpath(path, fullpath)) {
12419                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12420                                 argv[0], path, strerror(errno));
12421                         if (rc == 0)
12422                                 rc = -EINVAL;
12423                         continue;
12424                 }
12425
12426                 rc2 = llapi_pcc_state_get(fullpath, &state);
12427                 if (rc2 < 0) {
12428                         if (rc == 0)
12429                                 rc = rc2;
12430                         fprintf(stderr,
12431                                 "%s: cannot get PCC state of '%s': %s\n",
12432                                 argv[0], path, strerror(-rc2));
12433                         continue;
12434                 }
12435
12436                 printf("file: %s", path);
12437                 printf(", type: %s", pcc_type2string(state.pccs_type));
12438                 if (state.pccs_type == LU_PCC_NONE &&
12439                     state.pccs_open_count == 0) {
12440                         printf("\n");
12441                         continue;
12442                 }
12443
12444                 printf(", PCC file: %s", state.pccs_path);
12445                 printf(", user number: %u", state.pccs_open_count);
12446                 printf(", flags: %x", state.pccs_flags);
12447                 printf("\n");
12448         }
12449         return rc;
12450 }
12451
12452 /**
12453  * lfs_pcc_list_commands() - List lfs pcc commands.
12454  * @argc: The count of command line arguments.
12455  * @argv: Array of strings for command line arguments.
12456  *
12457  * This function lists lfs pcc commands defined in pcc_cmdlist[].
12458  *
12459  * Return: 0 on success.
12460  */
12461 static int lfs_pcc_list_commands(int argc, char **argv)
12462 {
12463         char buffer[81] = "";
12464
12465         Parser_list_commands(pcc_cmdlist, buffer, sizeof(buffer),
12466                              NULL, 0, 4);
12467
12468         return 0;
12469 }
12470
12471 /**
12472  * lfs_pcc() - Parse and execute lfs pcc commands.
12473  * @argc: The count of lfs pcc command line arguments.
12474  * @argv: Array of strings for lfs pcc command line arguments.
12475  *
12476  * This function parses lfs pcc commands and performs the
12477  * corresponding functions specified in pcc_cmdlist[].
12478  *
12479  * Return: 0 on success or an error code on failure.
12480  */
12481 static int lfs_pcc(int argc, char **argv)
12482 {
12483         char cmd[PATH_MAX];
12484         int rc = 0;
12485
12486         setlinebuf(stdout);
12487
12488         Parser_init("lfs-pcc > ", pcc_cmdlist);
12489
12490         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12491         progname = cmd;
12492         program_invocation_short_name = cmd;
12493         if (argc > 1)
12494                 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
12495         else
12496                 rc = Parser_commands();
12497
12498         return rc < 0 ? -rc : rc;
12499 }
12500
12501 static int lfs_list_commands(int argc, char **argv)
12502 {
12503         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
12504
12505         Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
12506
12507         return 0;
12508 }
12509
12510 int main(int argc, char **argv)
12511 {
12512         int rc;
12513
12514         /* Ensure that liblustreapi constructor has run */
12515         if (!llapi_liblustreapi_initialized())
12516                 fprintf(stderr, "liblustreapi was not properly initialized\n");
12517
12518         setlinebuf(stdout);
12519         opterr = 0;
12520
12521         Parser_init("lfs > ", cmdlist);
12522
12523         progname = program_invocation_short_name; /* Used in error messages */
12524         if (argc > 1) {
12525                 llapi_set_command_name(argv[1]);
12526                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
12527                 llapi_clear_command_name();
12528         } else {
12529                 rc = Parser_commands();
12530         }
12531
12532         return rc < 0 ? -rc : rc;
12533 }
12534
12535 #ifdef _LUSTRE_IDL_H_
12536 /* Everything we need here should be included by lustreapi.h. */
12537 # error "lfs should not depend on lustre_idl.h"
12538 #endif /* _LUSTRE_IDL_H_ */