Whamcloud - gitweb
LU-10772 utils: incorrect check in build_layout_from_yaml_node
[fs/lustre-release.git] / lustre / utils / lfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/utils/lfs.c
33  *
34  * Author: Peter J. Braam <braam@clusterfs.com>
35  * Author: Phil Schwan <phil@clusterfs.com>
36  * Author: Robert Read <rread@clusterfs.com>
37  */
38
39 /* for O_DIRECTORY */
40 #ifndef _GNU_SOURCE
41 #define _GNU_SOURCE
42 #endif
43
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <inttypes.h>
47 #include <getopt.h>
48 #include <string.h>
49 #include <mntent.h>
50 #include <unistd.h>
51 #include <errno.h>
52 #include <err.h>
53 #include <pwd.h>
54 #include <grp.h>
55 #include <sys/ioctl.h>
56 #include <sys/quota.h>
57 #include <sys/time.h>
58 #include <sys/types.h>
59 #include <sys/stat.h>
60 #include <sys/param.h>
61 #include <sys/xattr.h>
62 #include <fcntl.h>
63 #include <dirent.h>
64 #include <time.h>
65 #include <ctype.h>
66 #include <zlib.h>
67 #include <libgen.h>
68 #include <asm/byteorder.h>
69 #include "lfs_project.h"
70
71 #include <libcfs/util/string.h>
72 #include <libcfs/util/ioctl.h>
73 #include <libcfs/util/parser.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <cyaml.h>
79
80 #ifndef ARRAY_SIZE
81 # define ARRAY_SIZE(a) ((sizeof(a)) / (sizeof((a)[0])))
82 #endif /* !ARRAY_SIZE */
83
84 /* all functions */
85 static int lfs_find(int argc, char **argv);
86 static int lfs_getstripe(int argc, char **argv);
87 static int lfs_getdirstripe(int argc, char **argv);
88 static int lfs_setdirstripe(int argc, char **argv);
89 static int lfs_rmentry(int argc, char **argv);
90 static int lfs_osts(int argc, char **argv);
91 static int lfs_mdts(int argc, char **argv);
92 static int lfs_df(int argc, char **argv);
93 static int lfs_getname(int argc, char **argv);
94 static int lfs_check(int argc, char **argv);
95 #ifdef HAVE_SYS_QUOTA_H
96 static int lfs_setquota(int argc, char **argv);
97 static int lfs_quota(int argc, char **argv);
98 static int lfs_project(int argc, char **argv);
99 #endif
100 static int lfs_flushctx(int argc, char **argv);
101 static int lfs_cp(int argc, char **argv);
102 static int lfs_ls(int argc, char **argv);
103 static int lfs_poollist(int argc, char **argv);
104 static int lfs_changelog(int argc, char **argv);
105 static int lfs_changelog_clear(int argc, char **argv);
106 static int lfs_fid2path(int argc, char **argv);
107 static int lfs_path2fid(int argc, char **argv);
108 static int lfs_data_version(int argc, char **argv);
109 static int lfs_hsm_state(int argc, char **argv);
110 static int lfs_hsm_set(int argc, char **argv);
111 static int lfs_hsm_clear(int argc, char **argv);
112 static int lfs_hsm_action(int argc, char **argv);
113 static int lfs_hsm_archive(int argc, char **argv);
114 static int lfs_hsm_restore(int argc, char **argv);
115 static int lfs_hsm_release(int argc, char **argv);
116 static int lfs_hsm_remove(int argc, char **argv);
117 static int lfs_hsm_cancel(int argc, char **argv);
118 static int lfs_swap_layouts(int argc, char **argv);
119 static int lfs_mv(int argc, char **argv);
120 static int lfs_ladvise(int argc, char **argv);
121 static int lfs_getsom(int argc, char **argv);
122 static int lfs_mirror(int argc, char **argv);
123 static int lfs_mirror_list_commands(int argc, char **argv);
124 static int lfs_list_commands(int argc, char **argv);
125 static inline int lfs_mirror_resync(int argc, char **argv);
126 static inline int lfs_mirror_verify(int argc, char **argv);
127 static inline int lfs_mirror_dump(int argc, char **argv);
128
129 enum setstripe_origin {
130         SO_SETSTRIPE,
131         SO_MIGRATE,
132         SO_MIGRATE_MDT,
133         SO_MIRROR_CREATE,
134         SO_MIRROR_EXTEND,
135         SO_MIRROR_SPLIT,
136 };
137 static int lfs_setstripe_internal(int argc, char **argv,
138                                   enum setstripe_origin opc);
139
140 static inline int lfs_setstripe(int argc, char **argv)
141 {
142         return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
143 }
144 static inline int lfs_setstripe_migrate(int argc, char **argv)
145 {
146         return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
147 }
148 static inline int lfs_mirror_create(int argc, char **argv)
149 {
150         return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
151 }
152 static inline int lfs_mirror_extend(int argc, char **argv)
153 {
154         return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
155 }
156 static inline int lfs_mirror_split(int argc, char **argv)
157 {
158         return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
159 }
160
161 /* Setstripe and migrate share mostly the same parameters */
162 #define SSM_CMD_COMMON(cmd) \
163         "usage: "cmd" [--component-end|-E <comp_end>]\n"                \
164         "                 [--stripe-count|-c <stripe_count>]\n"         \
165         "                 [--stripe-index|-i <start_ost_idx>]\n"        \
166         "                 [--stripe-size|-S <stripe_size>]\n"           \
167         "                 [--layout|-L <pattern>]\n"                    \
168         "                 [--pool|-p <pool_name>]\n"                    \
169         "                 [--ost|-o <ost_indices>]\n"                   \
170         "                 [--yaml|-y <yaml_template_file>]\n"           \
171         "                 [--copy=<lustre_src>]\n"
172
173 #define SSM_HELP_COMMON \
174         "\tstripe_count: Number of OSTs to stripe over (0=fs default, -1 all)\n" \
175         "\tstart_ost_idx: OST index of first stripe (-1=default round robin)\n"\
176         "\tstripe_size:  Number of bytes on each OST (0=fs default)\n" \
177         "\t              Can be specified with K, M or G (for KB, MB, GB\n" \
178         "\t              respectively)\n"                               \
179         "\tpool_name:    Name of OST pool to use (default none)\n"      \
180         "\tlayout:       stripe pattern type: raid0, mdt (default raid0)\n"\
181         "\tost_indices:  List of OST indices, can be repeated multiple times\n"\
182         "\t              Indices be specified in a format of:\n"        \
183         "\t                -o <ost_1>,<ost_i>-<ost_j>,<ost_n>\n"        \
184         "\t              Or:\n"                                         \
185         "\t                -o <ost_1> -o <ost_i>-<ost_j> -o <ost_n>\n"  \
186         "\t              If --pool is set with --ost, then the OSTs\n"  \
187         "\t              must be the members of the pool.\n"            \
188         "\tcomp_end:     Extent end of component, start after previous end.\n"\
189         "\t              Can be specified with K, M or G (for KB, MB, GB\n" \
190         "\t              respectively, -1 for EOF). Must be a multiple of\n"\
191         "\t              stripe_size.\n"                                      \
192         "\tyaml_template_file:\n"                                             \
193         "\t              YAML layout template file, can't be used with -c,\n" \
194         "\t              -i, -S, -p, -o, or -E arguments.\n"                  \
195         "\tlustre_src:   Lustre file/dir whose layout info is used to set\n"  \
196         "\t              another lustre file or directory, can't used with\n" \
197         "\t              -c, -i, -S, -p, -o, or -E arguments.\n"
198
199 #define MIRROR_CREATE_HELP                                                     \
200         "\tmirror_count: Number of mirrors to be created with the upcoming\n"  \
201         "\t              setstripe layout options\n"                           \
202         "\t              It defaults to 1 if not specified; if specified,\n"   \
203         "\t              it must follow the option without a space.\n"         \
204         "\t              The option can also be repeated multiple times to\n"  \
205         "\t              separate mirrors that have different layouts.\n"      \
206         "\tsetstripe options: Mirror layout\n"                                 \
207         "\t              It can be a plain layout or a composite layout.\n"    \
208         "\t              If not specified, the stripe options inherited\n"     \
209         "\t              from the previous component will be used.\n"          \
210         "\tflags:        set flags to the component of the current mirror.\n"  \
211         "\t              Only \"prefer\" flag is supported so far.\n"
212
213 #define MIRROR_EXTEND_HELP                                                     \
214         MIRROR_CREATE_HELP                                                     \
215         "\tvictim_file:  The layout of victim_file will be split and used\n"   \
216         "\t              as a mirror added to the mirrored file.\n"            \
217         "\tno-verify:    This option indicates not to verify the mirror(s)\n"  \
218         "\t              from victim file(s) in case the victim file(s)\n"     \
219         "\t              contains the same data as the original mirrored\n"    \
220         "\t              file.\n"
221
222 #define MIRROR_EXTEND_USAGE                                                    \
223         "                 <--mirror-count|-N[mirror_count]>\n"                 \
224         "                 [setstripe options|-f <victim_file>]\n"              \
225         "                 [--no-verify]\n"
226
227 #define SETSTRIPE_USAGE                                                 \
228         SSM_CMD_COMMON("setstripe")                                     \
229         MIRROR_EXTEND_USAGE                                             \
230         "                 <directory|filename>\n"                       \
231         SSM_HELP_COMMON                                                 \
232         MIRROR_EXTEND_HELP
233
234 #define MIGRATE_USAGE                                                   \
235         SSM_CMD_COMMON("migrate  ")                                     \
236         "                 [--block|-b]\n"                               \
237         "                 [--non-block|-n]\n"                           \
238         "                 [--non-direct|-D]\n"                          \
239         "                 <filename>\n"                                 \
240         SSM_HELP_COMMON                                                 \
241         "\n"                                                            \
242         "\tblock:        Block file access during data migration (default)\n" \
243         "\tnon-block:    Abort migrations if concurrent access is detected\n" \
244         "\tnon-direct:   Do not use direct I/O to copy file contents\n" \
245
246 #define SETDIRSTRIPE_USAGE                                              \
247         "               [--mdt-count|-c stripe_count>\n"                \
248         "               [--mdt-index|-i mdt_index[,mdt_index,...]\n"    \
249         "               [--mdt-hash|-H mdt_hash]\n"                     \
250         "               [--default|-D] [--mode|-m mode] <dir>\n"        \
251         "\tstripe_count: stripe count of the striped directory\n"       \
252         "\tmdt_index: MDT index of first stripe\n"                      \
253         "\tmdt_hash:  hash type of the striped directory. mdt types:\n" \
254         "       fnv_1a_64 FNV-1a hash algorithm (default)\n"            \
255         "       all_char  sum of characters % MDT_COUNT (not recommended)\n" \
256         "\tdefault_stripe: set default dirstripe of the directory\n"    \
257         "\tmode: the mode of the directory\n"
258
259 /**
260  * command_t mirror_cmdlist - lfs mirror commands.
261  */
262 command_t mirror_cmdlist[] = {
263         { .pc_name = "create", .pc_func = lfs_mirror_create,
264           .pc_help = "Create a mirrored file.\n"
265                 "usage: lfs mirror create "
266                 "<--mirror-count|-N[mirror_count]> "
267                 "[setstripe options] ... <filename|directory>\n"
268           MIRROR_CREATE_HELP },
269         { .pc_name = "extend", .pc_func = lfs_mirror_extend,
270           .pc_help = "Extend a mirrored file.\n"
271                 "usage: lfs mirror extend "
272                 "<--mirror-count|-N[mirror_count]> [--no-verify] "
273                 "[setstripe options|-f <victim_file>] ... <filename>\n"
274           MIRROR_EXTEND_HELP },
275         { .pc_name = "split", .pc_func = lfs_mirror_split,
276           .pc_help = "Split a mirrored file.\n"
277         "usage: lfs mirror split <--mirror-id <mirror_id> | \n"
278         "\t             <--component-id|-I <comp_id>> [--destroy|-d] \n"
279         "\t             [-f <new_file>] <mirrored file>\n"
280         "\tmirror_id:   The numerical unique identifier for a mirror. It\n"
281         "\t             can be fetched by lfs getstripe command.\n"
282         "\tcomp_id:     Unique component ID within a mirror.\n"
283         "\tnew_file:    This option indicates the layout of the split\n"
284         "\t             mirror will be stored into. If not specified,\n"
285         "\t             a new file named <mirrored_file>.mirror~<mirror_id>\n"
286         "\t             will be used.\n" },
287         { .pc_name = "dump", .pc_func = lfs_mirror_dump,
288           .pc_help = "Dump the content of a specified mirror of a file.\n"
289                   "usage: lfs mirror dump <--mirror-id|-N <mirror_id> "
290                   "[--outfile|-o <output_file>] <mirrored_file>\n" },
291         { .pc_name = "resync", .pc_func = lfs_mirror_resync,
292           .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
293                 "usage: lfs mirror resync [--only <mirror_id[,...]>] "
294                 "<mirrored file> [<mirrored file2>...]\n"},
295         { .pc_name = "verify", .pc_func = lfs_mirror_verify,
296           .pc_help = "Verify mirrored file(s).\n"
297                 "usage: lfs mirror verify "
298                 "[--only <mirror_id,mirror_id2[,...]>] "
299                 "[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n"},
300         { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
301           .pc_help = "list commands supported by lfs mirror"},
302         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
303         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
304         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
305         { .pc_help = NULL }
306 };
307
308 /* all available commands */
309 command_t cmdlist[] = {
310         {"setstripe", lfs_setstripe, 0,
311          "To create a file with specified striping/composite layout, or\n"
312          "create/replace the default layout on an existing directory:\n"
313          SSM_CMD_COMMON("setstripe")
314          "                 <directory|filename>\n"
315          " or\n"
316          "To add component(s) to an existing composite file:\n"
317          SSM_CMD_COMMON("setstripe --component-add")
318          SSM_HELP_COMMON
319          "To totally delete the default striping from an existing directory:\n"
320          "usage: setstripe -d <directory>\n"
321          " or\n"
322          "To create a mirrored file or set s default mirror layout on a directory:\n"
323          "usage: setstripe -N[mirror_count] [STRIPE_OPTIONS] <directory|filename>\n"
324          " or\n"
325          "To delete the last component(s) from an existing composite file\n"
326          "(note that this will also delete any data in those components):\n"
327          "usage: setstripe --component-del [--component-id|-I <comp_id>]\n"
328          "                               [--component-flags|-F <comp_flags>]\n"
329          "                               <filename>\n"
330          "\tcomp_id:     Unique component ID to delete\n"
331          "\tcomp_flags:  'init' indicating all instantiated components\n"
332          "\t             '^init' indicating all uninstantiated components\n"
333          "\t-I and -F cannot be specified at the same time\n"},
334         {"getstripe", lfs_getstripe, 0,
335          "To list the layout pattern for a given file or files in a\n"
336          "directory or recursively for all files in a directory tree.\n"
337          "usage: getstripe [--ost|-O <uuid>] [--quiet|-q] [--verbose|-v]\n"
338          "                 [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
339          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
340          "                 [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
341          "                 [--layout|-L] [--generation|-g] [--yaml|-y]\n"
342          "                 [--component-id[=comp_id]|-I[comp_id]]\n"
343          "                 [--component-flags[=comp_flags]]\n"
344          "                 [--component-count]\n"
345          "                 [--component-start[=[+-]comp_start]]\n"
346          "                 [--component-end[=[+-]comp_end]|-E[[+-]comp_end]]\n"
347          "                 [[!] --mirror-index=[+-]<index> |\n"
348          "                  [!] --mirror-id=[+-]<id>]\n"
349          "                 <directory|filename> ..."},
350         {"setdirstripe", lfs_setdirstripe, 0,
351          "To create a striped directory on a specified MDT. This can only\n"
352          "be done on MDT0 with the right of administrator.\n"
353          "usage: setdirstripe [OPTION] <directory>\n"
354          SETDIRSTRIPE_USAGE},
355         {"getdirstripe", lfs_getdirstripe, 0,
356          "To list the layout pattern info for a given directory\n"
357          "or recursively for all directories in a directory tree.\n"
358          "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
359          "                    [--mdt-hash|-H] [--obd|-O <uuid>]\n"
360          "                    [--recursive|-r] [--yaml|-y]\n"
361          "                    [--default|-D] <dir> ..."},
362         {"mkdir", lfs_setdirstripe, 0,
363          "To create a striped directory on a specified MDT. This can only\n"
364          "be done on MDT0 with the right of administrator.\n"
365          "usage: mkdir [OPTION] <directory>\n"
366          SETDIRSTRIPE_USAGE},
367         {"rm_entry", lfs_rmentry, 0,
368          "To remove the name entry of the remote directory. Note: This\n"
369          "command will only delete the name entry, i.e. the remote directory\n"
370          "will become inaccessable after this command. This can only be done\n"
371          "by the administrator\n"
372          "usage: rm_entry <dir>\n"},
373         {"pool_list", lfs_poollist, 0,
374          "List pools or pool OSTs\n"
375          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
376         {"find", lfs_find, 0,
377          "find files matching given attributes recursively in directory tree.\n"
378          "usage: find <directory|filename> ...\n"
379          "     [[!] --atime|-A [+-]N] [[!] --ctime|-C [+-]N]\n"
380          "     [[!] --mtime|-M [+-]N] [--maxdepth|-D N] [[!] --blocks|-b N]\n"
381          "     [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
382          "     [[!] --name|-n <pattern>] [[!] --ost|-O <uuid|index,...>]\n"
383          "     [--print|-P] [--print0|-0] [[!] --size|-s [+-]N[bkMGTPE]]\n"
384          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
385          "     [[!] --stripe-index|-i <index,...>]\n"
386          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
387          "     [[!] --gid|-g|--group|-G <gid>|<gname>]\n"
388          "     [[!] --uid|-u|--user|-U <uid>|<uname>] [[!] --pool <pool>]\n"
389          "     [[!] --projid <projid>]\n"
390          "     [[!] --layout|-L released,raid0,mdt]\n"
391          "     [[!] --component-count [+-]<comp_cnt>]\n"
392          "     [[!] --component-start [+-]N[kMGTPE]]\n"
393          "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
394          "     [[!] --component-flags <comp_flags>]\n"
395          "     [[!] --mirror-count|-N [+-]<n>]\n"
396          "     [[!] --mirror-state <[^]state>]\n"
397          "     [[!] --mdt-count|-T [+-]<stripes>]\n"
398          "     [[!] --mdt-hash|-H <hashtype>\n"
399          "\t !: used before an option indicates 'NOT' requested attribute\n"
400          "\t -: used before a value indicates less than requested value\n"
401          "\t +: used before a value indicates more than requested value\n"
402          "\thashtype:   hash type of the striped directory.\n"
403          "\t            fnv_1a_64 FNV-1a hash algorithm\n"
404          "\t            all_char  sum of characters % MDT_COUNT\n"},
405         {"check", lfs_check, 0,
406          "Display the status of MDS or OSTs (as specified in the command)\n"
407          "or all the servers (MDS and OSTs).\n"
408          "usage: check <osts|mds|servers>"},
409         {"osts", lfs_osts, 0, "list OSTs connected to client "
410          "[for specified path only]\n" "usage: osts [path]"},
411         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
412          "[for specified path only]\n" "usage: mdts [path]"},
413         {"df", lfs_df, 0,
414          "report filesystem disk space usage or inodes usage"
415          "of each MDS and all OSDs or a batch belonging to a specific pool .\n"
416          "Usage: df [-i] [-h] [--lazy|-l] [--pool|-p <fsname>[.<pool>] [path]"},
417         {"getname", lfs_getname, 0, "list instances and specified mount points "
418          "[for specified path only]\n"
419          "Usage: getname [-h]|[path ...] "},
420 #ifdef HAVE_SYS_QUOTA_H
421         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
422          "usage: setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
423          "                -b <block-softlimit> -B <block-hardlimit>\n"
424          "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
425          "       setquota <-u|--user|-g|--group|-p|--projid> <uname>|<uid>|<gname>|<gid>|<projid>\n"
426          "                [--block-softlimit <block-softlimit>]\n"
427          "                [--block-hardlimit <block-hardlimit>]\n"
428          "                [--inode-softlimit <inode-softlimit>]\n"
429          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
430          "       setquota [-t] <-u|--user|-g|--group|-p|--projid>\n"
431          "                [--block-grace <block-grace>]\n"
432          "                [--inode-grace <inode-grace>] <filesystem>\n"
433          "       setquota <-U|-G|-P>\n"
434          "                -b <block-softlimit> -B <block-hardlimit>\n"
435          "                -i <inode-softlimit> -I <inode-hardlimit> <filesystem>\n"
436          "       setquota <-U|--default-usr|-G|--default-grp|-P|--default-prj>\n"
437          "                [--block-softlimit <block-softlimit>]\n"
438          "                [--block-hardlimit <block-hardlimit>]\n"
439          "                [--inode-softlimit <inode-softlimit>]\n"
440          "                [--inode-hardlimit <inode-hardlimit>] <filesystem>\n"
441          "       setquota <-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>\n"
442          "                <-d|--default>\n"
443          "       -b can be used instead of --block-softlimit/--block-grace\n"
444          "       -B can be used instead of --block-hardlimit\n"
445          "       -i can be used instead of --inode-softlimit/--inode-grace\n"
446          "       -I can be used instead of --inode-hardlimit\n"
447          "       -d can be used instead of --default\n\n"
448          "Note: The total quota space will be split into many qunits and\n"
449          "      balanced over all server targets, the minimal qunit size is\n"
450          "      1M bytes for block space and 1K inodes for inode space.\n\n"
451          "      The maximum quota grace time is 2^48 - 1 seconds.\n\n"
452          "      Quota space rebalancing process will stop when this mininum\n"
453          "      value is reached. As a result, quota exceeded can be returned\n"
454          "      while many targets still have 1MB or 1K inodes of spare\n"
455          "      quota space."},
456         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
457          "usage: quota [-q] [-v] [-h] [-o <obd_uuid>|-i <mdt_idx>|-I "
458                        "<ost_idx>]\n"
459          "             [<-u|-g|-p> <uname>|<uid>|<gname>|<gid>|<projid>] <filesystem>\n"
460          "       quota [-o <obd_uuid>|-i <mdt_idx>|-I <ost_idx>] -t <-u|-g|-p> <filesystem>\n"
461         "        quota [-q] [-v] [h] <-U|-G|-P> <filesystem>"},
462         {"project", lfs_project, 0,
463          "Change or list project attribute for specified file or directory.\n"
464          "usage: project [-d|-r] <file|directory...>\n"
465          "         list project ID and flags on file(s) or directories\n"
466          "       project [-p id] [-s] [-r] <file|directory...>\n"
467          "         set project ID and/or inherit flag for specified file(s) or directories\n"
468          "       project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
469          "         check project ID and flags on file(s) or directories, print outliers\n"
470          "       project -C [-r] [-k] <file|directory...>\n"
471          "         clear the project inherit flag and ID on the file or directory\n"
472         },
473 #endif
474         {"flushctx", lfs_flushctx, 0, "Flush security context for current user.\n"
475          "usage: flushctx [-k] [mountpoint...]"},
476         {"cp", lfs_cp, 0,
477          "Remote user copy files and directories.\n"
478          "usage: cp [OPTION]... [-T] SOURCE DEST\n\tcp [OPTION]... SOURCE... DIRECTORY\n\tcp [OPTION]... -t DIRECTORY SOURCE..."},
479         {"ls", lfs_ls, 0,
480          "Remote user list directory contents.\n"
481          "usage: ls [OPTION]... [FILE]..."},
482         {"changelog", lfs_changelog, 0,
483          "Show the metadata changes on an MDT."
484          "\nusage: changelog <mdtname> [startrec [endrec]]"},
485         {"changelog_clear", lfs_changelog_clear, 0,
486          "Indicate that old changelog records up to <endrec> are no longer of "
487          "interest to consumer <id>, allowing the system to free up space.\n"
488          "An <endrec> of 0 means all records.\n"
489          "usage: changelog_clear <mdtname> <id> <endrec>"},
490         {"fid2path", lfs_fid2path, 0,
491          "Resolve the full path(s) for given FID(s). For a specific hardlink "
492          "specify link number <linkno>.\n"
493         /* "For a historical link name, specify changelog record <recno>.\n" */
494          "usage: fid2path [--link <linkno>] <fsname|rootpath> <fid> ..."
495                 /* [ --rec <recno> ] */ },
496         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
497          "usage: path2fid [--parents] <path> ..."},
498         {"data_version", lfs_data_version, 0, "Display file data version for "
499          "a given path.\n" "usage: data_version -[n|r|w] <path>"},
500         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
501          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
502         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
503          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
504          "[--archived] [--lost] [--archive-id NUM] <file> ..."},
505         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
506          "files.\n"
507          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
508          "[--archived] [--lost] <file> ..."},
509         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
510          "given files.\n" "usage: hsm_action <file> ..."},
511         {"hsm_archive", lfs_hsm_archive, 0,
512          "Archive file to external storage.\n"
513          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
514          "<file> ..."},
515         {"hsm_restore", lfs_hsm_restore, 0,
516          "Restore file from external storage.\n"
517          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
518         {"hsm_release", lfs_hsm_release, 0,
519          "Release files from Lustre.\n"
520          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
521         {"hsm_remove", lfs_hsm_remove, 0,
522          "Remove file copy from external storage.\n"
523          "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
524          "[--archive NUM]\n"
525          "                  (FILE [FILE ...] | "
526          "--mntpath MOUNTPATH FID [FID ...])\n"
527          "\n"
528          "Note: To remove an archived copy of a file already deleted from a "
529          "Lustre FS, the\n"
530          "--mntpath option and a list of FIDs must be specified"
531         },
532         {"hsm_cancel", lfs_hsm_cancel, 0,
533          "Cancel requests related to specified files.\n"
534          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
535         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
536          "usage: swap_layouts <path1> <path2>"},
537         {"migrate", lfs_setstripe_migrate, 0,
538          "migrate a directory between MDTs.\n"
539          "usage: migrate [--mdt|-m] <start_mdt_index>\n"
540          "               [--mdt-count|-c] <stripe_count>\n"
541          "               [--mdt-hash|-H] <hash_type>\n"
542          "               [--verbose|-v]\n"
543          "               <directory>\n"
544          "\tmdt:        MDTs to stripe over, if only one MDT is specified\n"
545          "                      it's the MDT index of first stripe\n"
546          "\tmdt_count:  number of MDTs to stripe a directory over\n"
547          "\tmdt_hash:   hash type of the striped directory. mdt types:\n"
548          "                      fnv_1a_64 FNV-1a hash algorithm (default)\n"
549          "                      all_char  sum of characters % MDT_COUNT\n"
550          "\n"
551          "migrate file objects from one OST "
552          "layout\nto another (may be not safe with concurent writes).\n"
553          "usage: migrate  "
554          "[--stripe-count|-c] <stripe_count>\n"
555          "              [--stripe-index|-i] <start_ost_index>\n"
556          "              [--stripe-size|-S] <stripe_size>\n"
557          "              [--pool|-p] <pool_name>\n"
558          "              [--ost|-o] <ost_indices>\n"
559          "              [--block|-b]\n"
560          "              [--non-block|-n]\n"
561          "              [--non-direct|-D]\n"
562          "              <file|directory>\n"
563          "\tstripe_count:     number of OSTs to stripe a file over\n"
564          "\tstripe_ost_index: index of the first OST to stripe a file over\n"
565          "\tstripe_size:      number of bytes to store before moving to the next OST\n"
566          "\tpool_name:        name of the predefined pool of OSTs\n"
567          "\tost_indices:      OSTs to stripe over, in order\n"
568          "\tblock:            wait for the operation to return before continuing\n"
569          "\tnon-block:        do not wait for the operation to return\n"
570          "\tnon-direct:       do not use direct I/O to copy file contents.\n"},
571         {"mv", lfs_mv, 0,
572          "To move directories between MDTs. This command is deprecated, "
573          "use \"migrate\" instead.\n"
574          "usage: mv <directory|filename> [--mdt-index|-m] <mdt_index> "
575          "[--verbose|-v]\n"},
576         {"ladvise", lfs_ladvise, 0,
577          "Provide servers with advice about access patterns for a file.\n"
578          "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
579          "               [--background|-b] [--unset|-u]\n\n"
580          "               {[--end|-e END[kMGT]] | [--length|-l LENGTH[kMGT]]}\n"
581          "               {[--mode|-m [READ,WRITE]}\n"
582          "               <file> ...\n"},
583         {"mirror", lfs_mirror, mirror_cmdlist,
584          "lfs commands used to manage files with mirrored components:\n"
585          "lfs mirror create - create a mirrored file or directory\n"
586          "lfs mirror extend - add mirror(s) to an existing file\n"
587          "lfs mirror split  - split a mirror from an existing mirrored file\n"
588          "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
589          "lfs mirror dump   - dump a mirror content of a mirrored file\n"
590          "lfs mirror verify - verify mirrored file(s)\n"},
591         {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
592          "usage: getsom [-s] [-b] [-f] <path>\n"
593          "\t-s: Only show the size value of the SOM data for a given file\n"
594          "\t-b: Only show the blocks value of the SOM data for a given file\n"
595          "\t-f: Only show the flags value of the SOM data for a given file\n"},
596         {"help", Parser_help, 0, "help"},
597         {"exit", Parser_quit, 0, "quit"},
598         {"quit", Parser_quit, 0, "quit"},
599         {"--version", Parser_version, 0,
600          "output build version of the utility and exit"},
601         {"--list-commands", lfs_list_commands, 0,
602          "list commands supported by the utility and exit"},
603         { 0, 0, 0, NULL }
604 };
605
606
607 static int check_hashtype(const char *hashtype)
608 {
609         int i;
610
611         for (i = LMV_HASH_TYPE_ALL_CHARS; i < LMV_HASH_TYPE_MAX; i++)
612                 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
613                         return i;
614
615         return 0;
616 }
617
618
619 static const char *error_loc = "syserror";
620
621 enum {
622         MIGRATION_NONBLOCK      = 1 << 0,
623         MIGRATION_MIRROR        = 1 << 1,
624         MIGRATION_NONDIRECT     = 1 << 2,
625 };
626
627 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
628                                 struct llapi_layout *layout);
629
630 static int
631 migrate_open_files(const char *name, __u64 migration_flags,
632                    const struct llapi_stripe_param *param,
633                    struct llapi_layout *layout, int *fd_src, int *fd_tgt)
634 {
635         int                      fd = -1;
636         int                      fdv = -1;
637         int                      rflags;
638         int                      mdt_index;
639         int                      random_value;
640         char                     parent[PATH_MAX];
641         char                     volatile_file[PATH_MAX];
642         char                    *ptr;
643         int                      rc;
644         struct stat              st;
645         struct stat              stv;
646
647         if (param == NULL && layout == NULL) {
648                 error_loc = "layout information";
649                 return -EINVAL;
650         }
651
652         /* search for file directory pathname */
653         if (strlen(name) > sizeof(parent) - 1) {
654                 error_loc = "source file name";
655                 return -ERANGE;
656         }
657
658         strncpy(parent, name, sizeof(parent));
659         ptr = strrchr(parent, '/');
660         if (ptr == NULL) {
661                 if (getcwd(parent, sizeof(parent)) == NULL) {
662                         error_loc = "getcwd";
663                         return -errno;
664                 }
665         } else {
666                 if (ptr == parent) /* leading '/' */
667                         ptr = parent + 1;
668                 *ptr = '\0';
669         }
670
671         /* open file, direct io */
672         /* even if the file is only read, WR mode is nedeed to allow
673          * layout swap on fd */
674         rflags = O_RDWR;
675         if (!(migration_flags & MIGRATION_NONDIRECT))
676                 rflags |= O_DIRECT;
677         fd = open(name, rflags);
678         if (fd < 0) {
679                 rc = -errno;
680                 error_loc = "cannot open source file";
681                 return rc;
682         }
683
684         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
685         if (rc < 0) {
686                 error_loc = "cannot get MDT index";
687                 goto out;
688         }
689
690         do {
691                 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW;
692                 mode_t open_mode = S_IRUSR | S_IWUSR;
693
694                 random_value = random();
695                 rc = snprintf(volatile_file, sizeof(volatile_file),
696                               "%s/%s:%.4X:%.4X", parent, LUSTRE_VOLATILE_HDR,
697                               mdt_index, random_value);
698                 if (rc >= sizeof(volatile_file)) {
699                         rc = -ENAMETOOLONG;
700                         break;
701                 }
702
703                 /* create, open a volatile file, use caching (ie no directio) */
704                 if (param != NULL)
705                         fdv = llapi_file_open_param(volatile_file, open_flags,
706                                                     open_mode, param);
707                 else
708                         fdv = lfs_component_create(volatile_file, open_flags,
709                                                    open_mode, layout);
710         } while (fdv < 0 && (rc = fdv) == -EEXIST);
711
712         if (rc < 0) {
713                 error_loc = "cannot create volatile file";
714                 goto out;
715         }
716
717         /* In case the MDT does not support creation of volatile files
718          * we should try to unlink it. */
719         (void)unlink(volatile_file);
720
721         /* Not-owner (root?) special case.
722          * Need to set owner/group of volatile file like original.
723          * This will allow to pass related check during layout_swap.
724          */
725         rc = fstat(fd, &st);
726         if (rc != 0) {
727                 rc = -errno;
728                 error_loc = "cannot stat source file";
729                 goto out;
730         }
731
732         rc = fstat(fdv, &stv);
733         if (rc != 0) {
734                 rc = -errno;
735                 error_loc = "cannot stat volatile";
736                 goto out;
737         }
738
739         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
740                 rc = fchown(fdv, st.st_uid, st.st_gid);
741                 if (rc != 0) {
742                         rc = -errno;
743                         error_loc = "cannot change ownwership of volatile";
744                         goto out;
745                 }
746         }
747
748 out:
749         if (rc < 0) {
750                 if (fd > 0)
751                         close(fd);
752                 if (fdv > 0)
753                         close(fdv);
754         } else {
755                 *fd_src = fd;
756                 *fd_tgt = fdv;
757                 error_loc = NULL;
758         }
759         return rc;
760 }
761
762 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int))
763 {
764         struct llapi_layout *layout;
765         size_t   buf_size = 4 * 1024 * 1024;
766         void    *buf = NULL;
767         ssize_t  rsize = -1;
768         ssize_t  wsize = 0;
769         size_t   rpos = 0;
770         size_t   wpos = 0;
771         off_t    bufoff = 0;
772         int      rc;
773
774         layout = llapi_layout_get_by_fd(fd_src, 0);
775         if (layout != NULL) {
776                 uint64_t stripe_size;
777
778                 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
779                 if (rc == 0)
780                         buf_size = stripe_size;
781
782                 llapi_layout_free(layout);
783         }
784
785         /* Use a page-aligned buffer for direct I/O */
786         rc = posix_memalign(&buf, getpagesize(), buf_size);
787         if (rc != 0)
788                 return -rc;
789
790         while (1) {
791                 /* read new data only if we have written all
792                  * previously read data */
793                 if (wpos == rpos) {
794                         if (check_file) {
795                                 rc = check_file(fd_src);
796                                 if (rc < 0)
797                                         break;
798                         }
799
800                         rsize = read(fd_src, buf, buf_size);
801                         if (rsize < 0) {
802                                 rc = -errno;
803                                 break;
804                         }
805                         rpos += rsize;
806                         bufoff = 0;
807                 }
808                 /* eof ? */
809                 if (rsize == 0)
810                         break;
811
812                 wsize = write(fd_dst, buf + bufoff, rpos - wpos);
813                 if (wsize < 0) {
814                         rc = -errno;
815                         break;
816                 }
817                 wpos += wsize;
818                 bufoff += wsize;
819         }
820
821         if (rc == 0) {
822                 rc = fsync(fd_dst);
823                 if (rc < 0)
824                         rc = -errno;
825         }
826
827         free(buf);
828         return rc;
829 }
830
831 static int migrate_copy_timestamps(int fd, int fdv)
832 {
833         struct stat st;
834
835         if (fstat(fd, &st) == 0) {
836                 struct timeval tv[2] = {
837                         {.tv_sec = st.st_atime},
838                         {.tv_sec = st.st_mtime}
839                 };
840
841                 return futimes(fdv, tv);
842         }
843
844         return -errno;
845 }
846
847 static int migrate_block(int fd, int fdv)
848 {
849         __u64   dv1;
850         int     gid;
851         int     rc;
852         int     rc2;
853
854         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
855         if (rc < 0) {
856                 error_loc = "cannot get dataversion";
857                 return rc;
858         }
859
860         do
861                 gid = random();
862         while (gid == 0);
863
864         /* The grouplock blocks all concurrent accesses to the file.
865          * It has to be taken after llapi_get_data_version as it would
866          * block it too. */
867         rc = llapi_group_lock(fd, gid);
868         if (rc < 0) {
869                 error_loc = "cannot get group lock";
870                 return rc;
871         }
872
873         rc = migrate_copy_data(fd, fdv, NULL);
874         if (rc < 0) {
875                 error_loc = "data copy failed";
876                 goto out_unlock;
877         }
878
879         /* Make sure we keep original atime/mtime values */
880         rc = migrate_copy_timestamps(fd, fdv);
881         if (rc < 0) {
882                 error_loc = "timestamp copy failed";
883                 goto out_unlock;
884         }
885
886         /* swap layouts
887          * for a migration we need to check data version on file did
888          * not change.
889          *
890          * Pass in gid=0 since we already own grouplock. */
891         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
892                                            SWAP_LAYOUTS_CHECK_DV1);
893         if (rc == -EAGAIN) {
894                 error_loc = "file changed";
895                 goto out_unlock;
896         } else if (rc < 0) {
897                 error_loc = "cannot swap layout";
898                 goto out_unlock;
899         }
900
901 out_unlock:
902         rc2 = llapi_group_unlock(fd, gid);
903         if (rc2 < 0 && rc == 0) {
904                 error_loc = "unlock group lock";
905                 rc = rc2;
906         }
907
908         return rc;
909 }
910
911 /**
912  * Internal helper for migrate_copy_data(). Check lease and report error if
913  * need be.
914  *
915  * \param[in]  fd           File descriptor on which to check the lease.
916  *
917  * \retval 0       Migration can keep on going.
918  * \retval -errno  Error occurred, abort migration.
919  */
920 static int check_lease(int fd)
921 {
922         int rc;
923
924         rc = llapi_lease_check(fd);
925         if (rc > 0)
926                 return 0; /* llapi_check_lease returns > 0 on success. */
927
928         return -EBUSY;
929 }
930
931 static int migrate_nonblock(int fd, int fdv)
932 {
933         __u64   dv1;
934         __u64   dv2;
935         int     rc;
936
937         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
938         if (rc < 0) {
939                 error_loc = "cannot get data version";
940                 return rc;
941         }
942
943         rc = migrate_copy_data(fd, fdv, check_lease);
944         if (rc < 0) {
945                 error_loc = "data copy failed";
946                 return rc;
947         }
948
949         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
950         if (rc != 0) {
951                 error_loc = "cannot get data version";
952                 return rc;
953         }
954
955         if (dv1 != dv2) {
956                 rc = -EAGAIN;
957                 error_loc = "source file changed";
958                 return rc;
959         }
960
961         /* Make sure we keep original atime/mtime values */
962         rc = migrate_copy_timestamps(fd, fdv);
963         if (rc < 0) {
964                 error_loc = "timestamp copy failed";
965                 return rc;
966         }
967
968         return 0;
969 }
970
971 static int lfs_component_set(char *fname, int comp_id,
972                              __u32 flags, __u32 neg_flags)
973 {
974         __u32 ids[2];
975         __u32 flags_array[2];
976         size_t count = 0;
977         int rc;
978
979         if (flags) {
980                 ids[count] = comp_id;
981                 flags_array[count] = flags;
982                 ++count;
983         }
984
985         if (neg_flags) {
986                 ids[count] = comp_id;
987                 flags_array[count] = neg_flags | LCME_FL_NEG;
988                 ++count;
989         }
990
991         rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
992         if (rc)
993                 fprintf(stderr,
994                         "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
995                         progname, comp_id, fname, flags, neg_flags);
996
997         return rc;
998 }
999
1000 static int lfs_component_del(char *fname, __u32 comp_id,
1001                              __u32 flags, __u32 neg_flags)
1002 {
1003         int     rc = 0;
1004
1005         if (flags && neg_flags)
1006                 return -EINVAL;
1007
1008         if (!flags && neg_flags)
1009                 flags = neg_flags | LCME_FL_NEG;
1010
1011         if ((flags && comp_id) || (!flags && !comp_id))
1012                 return -EINVAL;
1013
1014         /* LCME_FL_INIT is the only supported flag in PFL */
1015         if (flags) {
1016                 if (flags & ~LCME_KNOWN_FLAGS) {
1017                         fprintf(stderr,
1018                                 "%s setstripe: unknown flags %#x\n",
1019                                 progname, flags);
1020                         return -EINVAL;
1021                 }
1022         } else if (comp_id > LCME_ID_MAX) {
1023                 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1024                         progname, comp_id);
1025                 return -EINVAL;
1026         }
1027
1028         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1029         if (rc)
1030                 fprintf(stderr,
1031                         "%s setstripe: cannot delete component %#x from '%s': %s\n",
1032                         progname, comp_id, fname, strerror(errno));
1033         return rc;
1034 }
1035
1036 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1037 {
1038         int     rc;
1039
1040         if (layout == NULL)
1041                 return -EINVAL;
1042
1043         rc = llapi_layout_file_comp_add(fname, layout);
1044         if (rc)
1045                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1046                         fname, strerror(errno));
1047         return rc;
1048 }
1049
1050 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1051                                 struct llapi_layout *layout)
1052 {
1053         struct stat     st;
1054         int     fd;
1055
1056         if (layout == NULL)
1057                 return -EINVAL;
1058
1059         fd = lstat(fname, &st);
1060         if (fd == 0 && S_ISDIR(st.st_mode))
1061                 open_flags = O_DIRECTORY | O_RDONLY;
1062
1063         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1064         if (fd < 0)
1065                 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1066                         S_ISDIR(st.st_mode) ?
1067                                 "set default composite layout for" :
1068                                 "create composite file",
1069                         fname, strerror(errno));
1070         return fd;
1071 }
1072
1073 static int lfs_migrate(char *name, __u64 migration_flags,
1074                        struct llapi_stripe_param *param,
1075                        struct llapi_layout *layout)
1076 {
1077         int fd = -1;
1078         int fdv = -1;
1079         int rc;
1080
1081         rc = migrate_open_files(name, migration_flags, param, layout,
1082                                 &fd, &fdv);
1083         if (rc < 0)
1084                 goto out;
1085
1086         if (!(migration_flags & MIGRATION_NONBLOCK)) {
1087                 /* Blocking mode (forced if servers do not support file lease).
1088                  * It is also the default mode, since we cannot distinguish
1089                  * between a broken lease and a server that does not support
1090                  * atomic swap/close (LU-6785) */
1091                 rc = migrate_block(fd, fdv);
1092                 goto out;
1093         }
1094
1095         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1096         if (rc < 0) {
1097                 error_loc = "cannot get lease";
1098                 goto out;
1099         }
1100
1101         rc = migrate_nonblock(fd, fdv);
1102         if (rc < 0) {
1103                 llapi_lease_release(fd);
1104                 goto out;
1105         }
1106
1107         /* Atomically put lease, swap layouts and close.
1108          * for a migration we need to check data version on file did
1109          * not change. */
1110         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1111         if (rc < 0) {
1112                 error_loc = "cannot swap layout";
1113                 goto out;
1114         }
1115
1116 out:
1117         if (fd >= 0)
1118                 close(fd);
1119
1120         if (fdv >= 0)
1121                 close(fdv);
1122
1123         if (rc < 0)
1124                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1125                         progname, name, error_loc, strerror(-rc));
1126         return rc;
1127 }
1128
1129 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1130 {
1131         char *name;
1132
1133         if (string == NULL)
1134                 return -EINVAL;
1135
1136         *flags = 0;
1137         *neg_flags = 0;
1138         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1139                 bool found = false;
1140                 int i;
1141
1142                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1143                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1144                         const char *comp_name = comp_flags_table[i].cfn_name;
1145
1146                         if (strcmp(name, comp_name) == 0) {
1147                                 *flags |= comp_flag;
1148                                 found = true;
1149                         } else if (strncmp(name, "^", 1) == 0 &&
1150                                    strcmp(name + 1, comp_name) == 0) {
1151                                 *neg_flags |= comp_flag;
1152                                 found = true;
1153                         }
1154                 }
1155                 if (!found) {
1156                         llapi_printf(LLAPI_MSG_ERROR,
1157                                      "%s: component flag '%s' not supported\n",
1158                                      progname, name);
1159                         return -EINVAL;
1160                 }
1161         }
1162
1163         if (!*flags && !*neg_flags)
1164                 return -EINVAL;
1165
1166         /* don't allow to set and exclude the same flag */
1167         if (*flags & *neg_flags)
1168                 return -EINVAL;
1169
1170         return 0;
1171 }
1172
1173 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1174 {
1175         if (string == NULL)
1176                 return -EINVAL;
1177
1178         *state = 0;
1179         *neg_state = 0;
1180
1181         if (strncmp(string, "^", 1) == 0) {
1182                 *neg_state = llapi_layout_string_flags(string + 1);
1183                 if (*neg_state != 0)
1184                         return 0;
1185         } else {
1186                 *state = llapi_layout_string_flags(string);
1187                 if (*state != 0)
1188                         return 0;
1189         }
1190
1191         llapi_printf(LLAPI_MSG_ERROR,
1192                      "%s: mirrored file state '%s' not supported\n",
1193                      progname, string);
1194         return -EINVAL;
1195 }
1196
1197 /**
1198  * struct mirror_args - Command-line arguments for mirror(s).
1199  * @m_count:  Number of mirrors to be created with this layout.
1200  * @m_flags:  Mirror level flags, only 'prefer' is supported.
1201  * @m_layout: Mirror layout.
1202  * @m_file:   A victim file. Its layout will be split and used as a mirror.
1203  * @m_next:   Point to the next node of the list.
1204  *
1205  * Command-line arguments for mirror(s) will be parsed and stored in
1206  * a linked list that consists of this structure.
1207  */
1208 struct mirror_args {
1209         __u32                   m_count;
1210         __u32                   m_flags;
1211         struct llapi_layout     *m_layout;
1212         const char              *m_file;
1213         struct mirror_args      *m_next;
1214 };
1215
1216 static int mirror_sanity_check_flags(struct llapi_layout *layout, void *unused)
1217 {
1218         uint32_t flags;
1219         int rc;
1220
1221         rc = llapi_layout_comp_flags_get(layout, &flags);
1222         if (rc)
1223                 return -errno;
1224
1225         if (flags & LCME_FL_NEG) {
1226                 fprintf(stderr, "error: %s: negative flags are not supported\n",
1227                         progname);
1228                 return -EINVAL;
1229         }
1230
1231         if (flags & LCME_FL_STALE) {
1232                 fprintf(stderr, "error: %s: setting '%s' is not supported\n",
1233                         progname, comp_flags_table[LCME_FL_STALE].cfn_name);
1234                 return -EINVAL;
1235         }
1236
1237         return LLAPI_LAYOUT_ITER_CONT;
1238 }
1239
1240 static inline int mirror_sanity_check_one(struct llapi_layout *layout)
1241 {
1242         uint64_t start, end;
1243         uint64_t pattern;
1244         int rc;
1245
1246         /* LU-10112: do not support dom+flr in phase 1 */
1247         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
1248         if (rc)
1249                 return -errno;
1250
1251         rc = llapi_layout_pattern_get(layout, &pattern);
1252         if (rc)
1253                 return -errno;
1254
1255         if (pattern == LOV_PATTERN_MDT || pattern == LLAPI_LAYOUT_MDT) {
1256                 fprintf(stderr, "error: %s: doesn't support dom+flr for now\n",
1257                         progname);
1258                 return -ENOTSUP;
1259         }
1260
1261         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_LAST);
1262         if (rc)
1263                 return -errno;
1264
1265         rc = llapi_layout_comp_extent_get(layout, &start, &end);
1266         if (rc)
1267                 return -errno;
1268
1269         if (end != LUSTRE_EOF) {
1270                 fprintf(stderr, "error: %s: mirror layout doesn't reach eof\n",
1271                         progname);
1272                 return -EINVAL;
1273         }
1274
1275         rc = llapi_layout_comp_iterate(layout, mirror_sanity_check_flags, NULL);
1276         return rc;
1277 }
1278
1279 /**
1280  * enum mirror_flags - Flags for extending a mirrored file.
1281  * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1282  *             in case the victim file(s) contains the same data as the
1283  *             original mirrored file.
1284  * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1285  * @MF_COMP_ID: specified component id instead of mirror id
1286  *
1287  * Flags for extending a mirrored file.
1288  */
1289 enum mirror_flags {
1290         MF_NO_VERIFY    = 0x1,
1291         MF_DESTROY      = 0x2,
1292         MF_COMP_ID      = 0x4,
1293 };
1294
1295 /**
1296  * mirror_create_sanity_check() - Check mirror list.
1297  * @list:  A linked list that stores the mirror arguments.
1298  *
1299  * This function does a sanity check on @list for creating
1300  * a mirrored file.
1301  *
1302  * Return: 0 on success or a negative error code on failure.
1303  */
1304 static int mirror_create_sanity_check(const char *fname,
1305                                       struct mirror_args *list)
1306 {
1307         int rc = 0;
1308         bool has_m_file = false;
1309         bool has_m_layout = false;
1310
1311         if (list == NULL)
1312                 return -EINVAL;
1313
1314         if (fname) {
1315                 struct llapi_layout *layout;
1316
1317                 layout = llapi_layout_get_by_path(fname, 0);
1318                 if (!layout) {
1319                         fprintf(stderr,
1320                                 "error: %s: file '%s' couldn't get layout\n",
1321                                 progname, fname);
1322                         return -ENODATA;
1323                 }
1324
1325                 rc = mirror_sanity_check_one(layout);
1326                 llapi_layout_free(layout);
1327
1328                 if (rc)
1329                         return rc;
1330         }
1331
1332         while (list != NULL) {
1333                 if (list->m_file != NULL) {
1334                         has_m_file = true;
1335                         llapi_layout_free(list->m_layout);
1336
1337                         list->m_layout =
1338                                 llapi_layout_get_by_path(list->m_file, 0);
1339                         if (list->m_layout == NULL) {
1340                                 fprintf(stderr,
1341                                         "error: %s: file '%s' has no layout\n",
1342                                         progname, list->m_file);
1343                                 return -ENODATA;
1344                         }
1345                 } else {
1346                         has_m_layout = true;
1347                         if (list->m_layout == NULL) {
1348                                 fprintf(stderr, "error: %s: no mirror layout\n",
1349                                         progname);
1350                                 return -EINVAL;
1351                         }
1352                 }
1353
1354                 rc = mirror_sanity_check_one(list->m_layout);
1355                 if (rc)
1356                         return rc;
1357
1358                 list = list->m_next;
1359         }
1360
1361         if (has_m_file && has_m_layout) {
1362                 fprintf(stderr,
1363                         "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1364                         progname);
1365                 return -EINVAL;
1366         }
1367
1368         return 0;
1369 }
1370
1371 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1372 {
1373         __u32 mirror_flags = *(__u32 *)cbdata;
1374         uint32_t flags;
1375         int rc;
1376
1377         rc = llapi_layout_comp_flags_get(layout, &flags);
1378         if (rc < 0)
1379                 return rc;
1380
1381         if (!flags) {
1382                 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1383                 if (rc)
1384                         return rc;
1385         }
1386
1387         return LLAPI_LAYOUT_ITER_CONT;
1388 }
1389
1390 /**
1391  * mirror_create() - Create a mirrored file.
1392  * @fname:        The file to be created.
1393  * @mirror_list:  A linked list that stores the mirror arguments.
1394  *
1395  * This function creates a mirrored file @fname with the mirror(s)
1396  * from @mirror_list.
1397  *
1398  * Return: 0 on success or a negative error code on failure.
1399  */
1400 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1401 {
1402         struct llapi_layout *layout = NULL;
1403         struct mirror_args *cur_mirror = NULL;
1404         uint16_t mirror_count = 0;
1405         int i = 0;
1406         int rc = 0;
1407
1408         rc = mirror_create_sanity_check(NULL, mirror_list);
1409         if (rc)
1410                 return rc;
1411
1412         cur_mirror = mirror_list;
1413         while (cur_mirror != NULL) {
1414                 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1415                                                mirror_set_flags,
1416                                                &cur_mirror->m_flags);
1417                 if (rc) {
1418                         rc = -errno;
1419                         fprintf(stderr, "%s: failed to set mirror flags\n",
1420                                 progname);
1421                         goto error;
1422                 }
1423
1424                 for (i = 0; i < cur_mirror->m_count; i++) {
1425                         rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1426                         if (rc) {
1427                                 rc = -errno;
1428                                 fprintf(stderr, "error: %s: "
1429                                         "merge layout failed: %s\n",
1430                                         progname, strerror(errno));
1431                                 goto error;
1432                         }
1433                 }
1434                 mirror_count += cur_mirror->m_count;
1435                 cur_mirror = cur_mirror->m_next;
1436         }
1437
1438         if (layout == NULL) {
1439                 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1440                 return -EINVAL;
1441         }
1442
1443         rc = llapi_layout_mirror_count_set(layout, mirror_count);
1444         if (rc) {
1445                 rc = -errno;
1446                 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1447                         progname, strerror(errno));
1448                 goto error;
1449         }
1450
1451         rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1452                                   layout);
1453         if (rc >= 0) {
1454                 close(rc);
1455                 rc = 0;
1456         }
1457
1458 error:
1459         llapi_layout_free(layout);
1460         return rc;
1461 }
1462
1463 /**
1464  * Compare files and check lease on @fd.
1465  *
1466  * \retval bytes number of bytes are the same
1467  */
1468 static ssize_t mirror_file_compare(int fd, int fdv)
1469 {
1470         const size_t buflen = 4 * 1024 * 1024; /* 4M */
1471         void *buf;
1472         ssize_t bytes_done = 0;
1473         ssize_t bytes_read = 0;
1474
1475         buf = malloc(buflen * 2);
1476         if (!buf)
1477                 return -ENOMEM;
1478
1479         while (1) {
1480                 if (!llapi_lease_check(fd)) {
1481                         bytes_done = -EBUSY;
1482                         break;
1483                 }
1484
1485                 bytes_read = read(fd, buf, buflen);
1486                 if (bytes_read <= 0)
1487                         break;
1488
1489                 if (bytes_read != read(fdv, buf + buflen, buflen))
1490                         break;
1491
1492                 /* XXX: should compute the checksum on each buffer and then
1493                  * compare checksum to avoid cache collision */
1494                 if (memcmp(buf, buf + buflen, bytes_read))
1495                         break;
1496
1497                 bytes_done += bytes_read;
1498         }
1499
1500         free(buf);
1501
1502         return bytes_done;
1503 }
1504
1505 static int mirror_extend_file(const char *fname, const char *victim_file,
1506                               enum mirror_flags mirror_flags)
1507 {
1508         int fd = -1;
1509         int fdv = -1;
1510         struct stat stbuf;
1511         struct stat stbuf_v;
1512         struct ll_ioc_lease *data = NULL;
1513         int rc;
1514
1515         fd = open(fname, O_RDWR);
1516         if (fd < 0) {
1517                 error_loc = "open source file";
1518                 rc = -errno;
1519                 goto out;
1520         }
1521
1522         fdv = open(victim_file, O_RDWR);
1523         if (fdv < 0) {
1524                 error_loc = "open target file";
1525                 rc = -errno;
1526                 goto out;
1527         }
1528
1529         if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1530                 error_loc = "stat source or target file";
1531                 rc = -errno;
1532                 goto out;
1533         }
1534
1535         if (stbuf.st_dev != stbuf_v.st_dev) {
1536                 error_loc = "stat source and target file";
1537                 rc = -EXDEV;
1538                 goto out;
1539         }
1540
1541         /* mirrors should be of the same size */
1542         if (stbuf.st_size != stbuf_v.st_size) {
1543                 error_loc = "file sizes don't match";
1544                 rc = -EINVAL;
1545                 goto out;
1546         }
1547
1548         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1549         if (rc < 0) {
1550                 error_loc = "cannot get lease";
1551                 goto out;
1552         }
1553
1554         if (!(mirror_flags & MF_NO_VERIFY)) {
1555                 ssize_t ret;
1556                 /* mirrors should have the same contents */
1557                 ret = mirror_file_compare(fd, fdv);
1558                 if (ret != stbuf.st_size) {
1559                         error_loc = "file busy or contents don't match";
1560                         rc = ret < 0 ? ret : -EINVAL;
1561                         goto out;
1562                 }
1563         }
1564
1565         /* Get rid of caching pages from clients */
1566         rc = llapi_file_flush(fd);
1567         if (rc < 0) {
1568                 error_loc = "cannot get data version";
1569                 goto out;
1570         }
1571
1572         rc = llapi_file_flush(fdv);
1573         if (rc < 0) {
1574                 error_loc = "cannot get data version";
1575                 goto out;
1576
1577         }
1578
1579         /* Make sure we keep original atime/mtime values */
1580         rc = migrate_copy_timestamps(fd, fdv);
1581         if (rc < 0) {
1582                 error_loc = "cannot copy timestamp";
1583                 goto out;
1584         }
1585
1586         /* Atomically put lease, merge layouts and close. */
1587         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1588         if (!data) {
1589                 error_loc = "memory allocation";
1590                 goto out;
1591         }
1592         data->lil_mode = LL_LEASE_UNLCK;
1593         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1594         data->lil_count = 1;
1595         data->lil_ids[0] = fdv;
1596         rc = llapi_lease_set(fd, data);
1597         if (rc < 0) {
1598                 error_loc = "cannot merge layout";
1599                 goto out;
1600         } else if (rc == 0) {
1601                 rc = -EBUSY;
1602                 error_loc = "lost lease lock";
1603                 goto out;
1604         }
1605         rc = 0;
1606
1607 out:
1608         if (data)
1609                 free(data);
1610         if (fd >= 0)
1611                 close(fd);
1612         if (fdv >= 0)
1613                 close(fdv);
1614         if (!rc)
1615                 (void) unlink(victim_file);
1616         if (rc < 0)
1617                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1618                         progname, fname, error_loc, strerror(-rc));
1619         return rc;
1620 }
1621
1622 static int mirror_extend_layout(char *name, struct llapi_layout *layout)
1623 {
1624         struct ll_ioc_lease *data = NULL;
1625         int fd = -1;
1626         int fdv = -1;
1627         int rc;
1628
1629         rc = migrate_open_files(name, 0, NULL, layout, &fd, &fdv);
1630         if (rc < 0)
1631                 goto out;
1632
1633         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1634         if (rc < 0) {
1635                 error_loc = "cannot get lease";
1636                 goto out;
1637         }
1638
1639         rc = migrate_nonblock(fd, fdv);
1640         if (rc < 0) {
1641                 llapi_lease_release(fd);
1642                 goto out;
1643         }
1644
1645         /* Atomically put lease, merge layouts and close. */
1646         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1647         if (!data) {
1648                 error_loc = "memory allocation";
1649                 goto out;
1650         }
1651         data->lil_mode = LL_LEASE_UNLCK;
1652         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1653         data->lil_count = 1;
1654         data->lil_ids[0] = fdv;
1655         rc = llapi_lease_set(fd, data);
1656         if (rc < 0) {
1657                 error_loc = "cannot merge layout";
1658                 goto out;
1659         } else if (rc == 0) {
1660                 rc = -EBUSY;
1661                 error_loc = "lost lease lock";
1662                 goto out;
1663         }
1664         rc = 0;
1665
1666 out:
1667         if (data)
1668                 free(data);
1669         if (fd >= 0)
1670                 close(fd);
1671         if (fdv >= 0)
1672                 close(fdv);
1673         if (rc < 0)
1674                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1675                         progname, name, error_loc, strerror(-rc));
1676         return rc;
1677 }
1678
1679 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
1680                          enum mirror_flags mirror_flags)
1681 {
1682         int rc;
1683
1684         rc = mirror_create_sanity_check(fname, mirror_list);
1685         if (rc)
1686                 return rc;
1687
1688         while (mirror_list) {
1689                 if (mirror_list->m_file != NULL) {
1690                         rc = mirror_extend_file(fname, mirror_list->m_file,
1691                                                 mirror_flags);
1692                 } else {
1693                         __u32 mirror_count = mirror_list->m_count;
1694
1695                         while (mirror_count > 0) {
1696                                 rc = mirror_extend_layout(fname,
1697                                                         mirror_list->m_layout);
1698                                 if (rc)
1699                                         break;
1700
1701                                 --mirror_count;
1702                         }
1703                 }
1704                 if (rc)
1705                         break;
1706
1707                 mirror_list = mirror_list->m_next;
1708         }
1709
1710         return rc;
1711 }
1712
1713 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
1714 {
1715         uint32_t id;
1716         int rc;
1717
1718         rc = llapi_layout_mirror_id_get(layout, &id);
1719         if (rc < 0)
1720                 return rc;
1721
1722         if ((__u16)id == *(__u16 *)cbdata)
1723                 return LLAPI_LAYOUT_ITER_STOP;
1724
1725         return LLAPI_LAYOUT_ITER_CONT;
1726 }
1727
1728 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
1729 {
1730         uint32_t id;
1731         int rc;
1732
1733         rc = llapi_layout_comp_id_get(layout, &id);
1734         if (rc < 0)
1735                 return rc;
1736
1737         if (id == *(__u32 *)cbdata)
1738                 return LLAPI_LAYOUT_ITER_STOP;
1739
1740         return LLAPI_LAYOUT_ITER_CONT;
1741 }
1742 static int mirror_split(const char *fname, __u32 id,
1743                         enum mirror_flags mflags, const char *victim_file)
1744 {
1745         struct llapi_layout *layout;
1746         char parent[PATH_MAX];
1747         char victim[PATH_MAX];
1748         int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
1749         char *ptr;
1750         struct ll_ioc_lease *data;
1751         uint16_t mirror_count;
1752         int mdt_index;
1753         int fd, fdv;
1754         int rc;
1755
1756         /* check fname contains mirror with mirror_id/comp_id */
1757         layout = llapi_layout_get_by_path(fname, 0);
1758         if (!layout) {
1759                 fprintf(stderr,
1760                         "error %s: file '%s' couldn't get layout\n",
1761                         progname, fname);
1762                 return -EINVAL;
1763         }
1764
1765         rc = mirror_sanity_check_one(layout);
1766         if (rc)
1767                 goto free_layout;
1768
1769         rc = llapi_layout_mirror_count_get(layout, &mirror_count);
1770         if (rc) {
1771                 fprintf(stderr,
1772                         "error %s: file '%s' couldn't get mirror count\n",
1773                         progname, fname);
1774                 goto free_layout;
1775         }
1776         if (mirror_count < 2) {
1777                 fprintf(stderr,
1778                         "error %s: file '%s' has %d component, cannot split\n",
1779                         progname, fname, mirror_count);
1780                 goto free_layout;
1781         }
1782
1783         if (mflags & MF_COMP_ID) {
1784                 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
1785                 id = mirror_id_of(id);
1786         } else {
1787                 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
1788         }
1789         if (rc < 0) {
1790                 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
1791                         progname, fname);
1792                 goto free_layout;
1793         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
1794                 fprintf(stderr,
1795                      "error %s: file '%s' does not contain mirror with id %u\n",
1796                         progname, fname, id);
1797                 goto free_layout;
1798         }
1799
1800         fd = open(fname, O_RDWR);
1801         if (fd < 0) {
1802                 fprintf(stderr,
1803                         "error %s: open file '%s' failed: %s\n",
1804                         progname, fname, strerror(errno));
1805                 goto free_layout;
1806         }
1807
1808         /* get victim file directory pathname */
1809         if (strlen(fname) > sizeof(parent) - 1) {
1810                 fprintf(stderr, "error %s: file name of '%s' too long\n",
1811                         progname, fname);
1812                 rc = -ERANGE;
1813                 goto close_fd;
1814         }
1815         strncpy(parent, fname, sizeof(parent));
1816         ptr = strrchr(parent, '/');
1817         if (ptr == NULL) {
1818                 if (getcwd(parent, sizeof(parent)) == NULL) {
1819                         fprintf(stderr, "error %s: getcwd failed: %s\n",
1820                                 progname, strerror(errno));
1821                         rc = -errno;
1822                         goto close_fd;
1823                 }
1824         } else {
1825                 if (ptr == parent)
1826                         ptr = parent + 1;
1827                 *ptr = '\0';
1828         }
1829
1830         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
1831         if (rc < 0) {
1832                 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
1833                         progname, fname);
1834                 goto close_fd;
1835         }
1836
1837         if (victim_file == NULL) {
1838                 /* use a temp file to store the splitted layout */
1839                 if (mflags & MF_DESTROY) {
1840                         fdv = llapi_create_volatile_idx(parent, mdt_index,
1841                                                         O_LOV_DELAY_CREATE);
1842                 } else {
1843                         snprintf(victim, sizeof(victim), "%s.mirror~%u",
1844                                  fname, id);
1845                         fdv = open(victim, flags, S_IRUSR | S_IWUSR);
1846                 }
1847         } else {
1848                 /* user specified victim file */
1849                 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
1850         }
1851
1852         if (fdv < 0) {
1853                 fprintf(stderr,
1854                         "error %s: create victim file failed: %s\n",
1855                         progname, strerror(errno));
1856                 goto close_fd;
1857         }
1858
1859         /* get lease lock of fname */
1860         rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
1861         if (rc < 0) {
1862                 fprintf(stderr,
1863                         "error %s: cannot get lease of file '%s': %d\n",
1864                         progname, fname, rc);
1865                 goto close_victim;
1866         }
1867
1868         /* Atomatically put lease, split layouts and close. */
1869         data = malloc(offsetof(typeof(*data), lil_ids[2]));
1870         if (!data) {
1871                 rc = -ENOMEM;
1872                 goto close_victim;
1873         }
1874
1875         data->lil_mode = LL_LEASE_UNLCK;
1876         data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
1877         data->lil_count = 2;
1878         data->lil_ids[0] = fdv;
1879         data->lil_ids[1] = id;
1880         rc = llapi_lease_set(fd, data);
1881         if (rc <= 0) {
1882                 if (rc == 0) /* lost lease lock */
1883                         rc = -EBUSY;
1884                 fprintf(stderr,
1885                         "error %s: cannot split '%s': %s\n",
1886                         progname, fname, strerror(-rc));
1887         } else {
1888                 rc = 0;
1889         }
1890         free(data);
1891
1892 close_victim:
1893         close(fdv);
1894 close_fd:
1895         close(fd);
1896 free_layout:
1897         llapi_layout_free(layout);
1898         return rc;
1899 }
1900
1901 /**
1902  * Parse a string containing an target index list into an array of integers.
1903  *
1904  * The input string contains a comma delimited list of individual
1905  * indices and ranges, for example "1,2-4,7". Add the indices into the
1906  * \a tgts array and remove duplicates.
1907  *
1908  * \param[out] tgts    array to store indices in
1909  * \param[in] size     size of \a tgts array
1910  * \param[in] offset   starting index in \a tgts
1911  * \param[in] arg      string containing OST index list
1912  *
1913  * \retval positive    number of indices in \a tgts
1914  * \retval -EINVAL     unable to parse \a arg
1915  */
1916 static int parse_targets(__u32 *tgts, int size, int offset, char *arg)
1917 {
1918         int rc;
1919         int nr = offset;
1920         int slots = size - offset;
1921         char *ptr = NULL;
1922         bool end_of_loop;
1923
1924         if (arg == NULL)
1925                 return -EINVAL;
1926
1927         end_of_loop = false;
1928         while (!end_of_loop) {
1929                 int start_index;
1930                 int end_index;
1931                 int i;
1932                 char *endptr = NULL;
1933
1934                 rc = -EINVAL;
1935
1936                 ptr = strchrnul(arg, ',');
1937
1938                 end_of_loop = *ptr == '\0';
1939                 *ptr = '\0';
1940
1941                 start_index = strtol(arg, &endptr, 0);
1942                 if (endptr == arg) /* no data at all */
1943                         break;
1944                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
1945                         break;
1946
1947                 end_index = start_index;
1948                 if (*endptr == '-') {
1949                         end_index = strtol(endptr + 1, &endptr, 0);
1950                         if (*endptr != '\0')
1951                                 break;
1952                         if (end_index < start_index)
1953                                 break;
1954                 }
1955
1956                 for (i = start_index; i <= end_index && slots > 0; i++) {
1957                         int j;
1958
1959                         /* remove duplicate */
1960                         for (j = 0; j < offset; j++) {
1961                                 if (tgts[j] == i)
1962                                         break;
1963                         }
1964                         if (j == offset) { /* no duplicate */
1965                                 tgts[nr++] = i;
1966                                 --slots;
1967                         }
1968                 }
1969                 if (slots == 0 && i < end_index)
1970                         break;
1971
1972                 *ptr = ',';
1973                 arg = ++ptr;
1974                 offset = nr;
1975                 rc = 0;
1976         }
1977         if (!end_of_loop && ptr != NULL)
1978                 *ptr = ',';
1979
1980         return rc < 0 ? rc : nr;
1981 }
1982
1983 struct lfs_setstripe_args {
1984         unsigned long long       lsa_comp_end;
1985         unsigned long long       lsa_stripe_size;
1986         long long                lsa_stripe_count;
1987         long long                lsa_stripe_off;
1988         __u32                    lsa_comp_flags;
1989         __u32                    lsa_comp_neg_flags;
1990         unsigned long long       lsa_pattern;
1991         unsigned int             lsa_mirror_count;
1992         int                      lsa_nr_tgts;
1993         bool                     lsa_first_comp;
1994         __u32                   *lsa_tgts;
1995         char                    *lsa_pool_name;
1996 };
1997
1998 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
1999 {
2000         unsigned int mirror_count = lsa->lsa_mirror_count;
2001         bool first_comp = lsa->lsa_first_comp;
2002
2003         memset(lsa, 0, sizeof(*lsa));
2004
2005         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2006         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2007         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2008         lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2009         lsa->lsa_pool_name = NULL;
2010
2011         lsa->lsa_mirror_count = mirror_count;
2012         lsa->lsa_first_comp = first_comp;
2013 }
2014
2015 /**
2016  * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2017  * @lsa: Stripe options to be initialized and inherited.
2018  *
2019  * This function initializes stripe options in @lsa and inherit
2020  * stripe_size, stripe_count and OST pool_name options.
2021  *
2022  * Return: void.
2023  */
2024 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2025 {
2026         unsigned long long stripe_size;
2027         long long stripe_count;
2028         char *pool_name = NULL;
2029
2030         stripe_size = lsa->lsa_stripe_size;
2031         stripe_count = lsa->lsa_stripe_count;
2032         pool_name = lsa->lsa_pool_name;
2033
2034         setstripe_args_init(lsa);
2035
2036         lsa->lsa_stripe_size = stripe_size;
2037         lsa->lsa_stripe_count = stripe_count;
2038         lsa->lsa_pool_name = pool_name;
2039 }
2040
2041 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2042 {
2043         return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2044                 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2045                 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2046                 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2047                 lsa->lsa_pool_name != NULL ||
2048                 lsa->lsa_comp_end != 0);
2049 }
2050
2051 /**
2052  * comp_args_to_layout() - Create or extend a composite layout.
2053  * @composite:       Pointer to the composite layout.
2054  * @lsa:             Stripe options for the new component.
2055  *
2056  * This function creates or extends a composite layout by adding a new
2057  * component with stripe options from @lsa.
2058  *
2059  * Return: 0 on success or an error code on failure.
2060  */
2061 static int comp_args_to_layout(struct llapi_layout **composite,
2062                                struct lfs_setstripe_args *lsa,
2063                                bool set_extent)
2064 {
2065         struct llapi_layout *layout = *composite;
2066         uint64_t prev_end = 0;
2067         int i = 0, rc;
2068
2069         if (layout == NULL) {
2070                 layout = llapi_layout_alloc();
2071                 if (layout == NULL) {
2072                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2073                                 strerror(errno));
2074                         return -ENOMEM;
2075                 }
2076                 *composite = layout;
2077         } else {
2078                 uint64_t start;
2079
2080                 /* Get current component extent, current component
2081                  * must be the tail component. */
2082                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2083                 if (rc) {
2084                         fprintf(stderr, "Get comp extent failed. %s\n",
2085                                 strerror(errno));
2086                         return rc;
2087                 }
2088
2089                 if (lsa->lsa_first_comp)
2090                         prev_end = 0;
2091
2092                 if (lsa->lsa_first_comp)
2093                         rc = llapi_layout_add_first_comp(layout);
2094                 else
2095                         rc = llapi_layout_comp_add(layout);
2096                 if (rc) {
2097                         fprintf(stderr, "Add component failed. %s\n",
2098                                 strerror(errno));
2099                         return rc;
2100                 }
2101         }
2102         /* reset lsa_first_comp */
2103         lsa->lsa_first_comp = false;
2104
2105         if (set_extent) {
2106                 rc = llapi_layout_comp_extent_set(layout, prev_end,
2107                                                   lsa->lsa_comp_end);
2108                 if (rc) {
2109                         fprintf(stderr, "Set extent [%lu, %llu) failed. %s\n",
2110                                 prev_end, lsa->lsa_comp_end, strerror(errno));
2111                         return rc;
2112                 }
2113         }
2114
2115         /* Data-on-MDT component setting */
2116         if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2117                 /* In case of Data-on-MDT patterns the only extra option
2118                  * applicable is stripe size option. */
2119                 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2120                         fprintf(stderr, "Option 'stripe-count' can't be "
2121                                 "specified with Data-on-MDT component: %lld\n",
2122                                 lsa->lsa_stripe_count);
2123                         return -EINVAL;
2124                 }
2125                 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT) {
2126                         fprintf(stderr, "Option 'stripe-size' can't be "
2127                                 "specified with Data-on-MDT component: %llu\n",
2128                                 lsa->lsa_stripe_size);
2129                         return -EINVAL;
2130                 }
2131                 if (lsa->lsa_nr_tgts != 0) {
2132                         fprintf(stderr, "Option 'ost-list' can't be specified "
2133                                 "with Data-on-MDT component: '%i'\n",
2134                                 lsa->lsa_nr_tgts);
2135                         return -EINVAL;
2136                 }
2137                 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2138                         fprintf(stderr, "Option 'stripe-offset' can't be "
2139                                 "specified with Data-on-MDT component: %lld\n",
2140                                 lsa->lsa_stripe_off);
2141                         return -EINVAL;
2142                 }
2143                 if (lsa->lsa_pool_name != 0) {
2144                         fprintf(stderr, "Option 'pool' can't be specified "
2145                                 "with Data-on-MDT component: '%s'\n",
2146                                 lsa->lsa_pool_name);
2147                         return -EINVAL;
2148                 }
2149
2150                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2151                 if (rc) {
2152                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2153                                 lsa->lsa_pattern, strerror(errno));
2154                         return rc;
2155                 }
2156                 /* Data-on-MDT component has always single stripe up to end */
2157                 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2158         }
2159
2160         rc = llapi_layout_stripe_size_set(layout, lsa->lsa_stripe_size);
2161         if (rc) {
2162                 fprintf(stderr, "Set stripe size %llu failed: %s\n",
2163                         lsa->lsa_stripe_size, strerror(errno));
2164                 return rc;
2165         }
2166
2167         rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2168         if (rc) {
2169                 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2170                         lsa->lsa_stripe_count, strerror(errno));
2171                 return rc;
2172         }
2173
2174         rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2175         if (rc) {
2176                 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2177                         lsa->lsa_comp_flags, strerror(errno));
2178                 return rc;
2179         }
2180
2181         if (lsa->lsa_pool_name != NULL) {
2182                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2183                 if (rc) {
2184                         fprintf(stderr, "Set pool name: %s failed. %s\n",
2185                                 lsa->lsa_pool_name, strerror(errno));
2186                         return rc;
2187                 }
2188         } else {
2189                 rc = llapi_layout_pool_name_set(layout, "");
2190                 if (rc) {
2191                         fprintf(stderr, "Clear pool name failed: %s\n",
2192                                 strerror(errno));
2193                         return rc;
2194                 }
2195         }
2196
2197         if (lsa->lsa_nr_tgts > 0) {
2198                 if (lsa->lsa_stripe_count > 0 &&
2199                     lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2200                     lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2201                     lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2202                         fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2203                                 lsa->lsa_stripe_count, lsa->lsa_nr_tgts);
2204                         return -EINVAL;
2205                 }
2206                 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2207                         rc = llapi_layout_ost_index_set(layout, i,
2208                                                         lsa->lsa_tgts[i]);
2209                         if (rc)
2210                                 break;
2211                 }
2212         } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
2213                    lsa->lsa_stripe_off != -1) {
2214                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
2215         }
2216         if (rc) {
2217                 fprintf(stderr, "Set ost index %d failed. %s\n",
2218                         i, strerror(errno));
2219                 return rc;
2220         }
2221
2222         return 0;
2223 }
2224
2225 static int build_component(struct llapi_layout **layout,
2226                            struct lfs_setstripe_args *lsa, bool set_extent)
2227 {
2228         int rc;
2229
2230         rc = comp_args_to_layout(layout, lsa, set_extent);
2231         if (rc)
2232                 return rc;
2233
2234         if (lsa->lsa_mirror_count > 0) {
2235                 rc = llapi_layout_mirror_count_set(*layout,
2236                                                    lsa->lsa_mirror_count);
2237                 if (rc)
2238                         return rc;
2239
2240                 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
2241                 if (rc)
2242                         return rc;
2243                 lsa->lsa_mirror_count = 0;
2244         }
2245
2246         return rc;
2247 }
2248
2249 static int build_layout_from_yaml_node(struct cYAML *node,
2250                                        struct llapi_layout **layout,
2251                                        struct lfs_setstripe_args *lsa,
2252                                        __u32 *osts)
2253 {
2254         char *string;
2255         int rc = 0;
2256
2257         while (node) {
2258                 if (node->cy_type == CYAML_TYPE_OBJECT) {
2259                         /* go deep to sub blocks */
2260                         rc = build_layout_from_yaml_node(node->cy_child, layout,
2261                                                          lsa, osts);
2262                         if (rc)
2263                                 return rc;
2264                 } else {
2265                         if (node->cy_string == NULL)
2266                                 return -EINVAL;
2267
2268                         string = node->cy_string;
2269                         /* skip leading lmm_ if present, to simplify parsing */
2270                         if (strncmp(string, "lmm_", 4) == 0)
2271                                 string += 4;
2272
2273                         if (node->cy_type == CYAML_TYPE_STRING) {
2274                                 if (!strcmp(string, "lcme_extent.e_end")) {
2275                                         if (!strcmp(node->cy_valuestring, "EOF") ||
2276                                             !strcmp(node->cy_valuestring, "eof"))
2277                                                 lsa->lsa_comp_end = LUSTRE_EOF;
2278                                 } else if (!strcmp(string, "pool")) {
2279                                         lsa->lsa_pool_name = node->cy_valuestring;
2280                                 } else if (!strcmp(string, "pattern")) {
2281                                         if (!strcmp(node->cy_valuestring, "mdt"))
2282                                                 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
2283                                 }
2284                         } else if (node->cy_type == CYAML_TYPE_NUMBER) {
2285                                 if (!strcmp(string, "lcm_mirror_count")) {
2286                                         lsa->lsa_mirror_count = node->cy_valueint;
2287                                 } else if (!strcmp(string, "lcme_extent.e_start")) {
2288                                         if (node->cy_valueint != 0 || *layout != NULL) {
2289                                                 rc = build_component(layout, lsa, true);
2290                                                 if (rc)
2291                                                         return rc;
2292                                         }
2293
2294                                         if (node->cy_valueint == 0)
2295                                                 lsa->lsa_first_comp = true;
2296
2297                                         /* initialize lsa */
2298                                         setstripe_args_init(lsa);
2299                                         lsa->lsa_tgts = osts;
2300                                 } else if (!strcmp(string, "lcme_extent.e_end")) {
2301                                         if (node->cy_valueint == -1)
2302                                                 lsa->lsa_comp_end = LUSTRE_EOF;
2303                                         else
2304                                                 lsa->lsa_comp_end = node->cy_valueint;
2305                                 } else if (!strcmp(string, "stripe_count")) {
2306                                         lsa->lsa_stripe_count = node->cy_valueint;
2307                                 } else if (!strcmp(string, "stripe_size")) {
2308                                         lsa->lsa_stripe_size = node->cy_valueint;
2309                                 } else if (!strcmp(string, "stripe_offset")) {
2310                                         lsa->lsa_stripe_off = node->cy_valueint;
2311                                 } else if (!strcmp(string, "l_ost_idx")) {
2312                                         osts[lsa->lsa_nr_tgts] = node->cy_valueint;
2313                                         lsa->lsa_nr_tgts++;
2314                                 }
2315                         }
2316                 }
2317                 node = node->cy_next;
2318         }
2319
2320         return rc;
2321 }
2322
2323 static int lfs_comp_create_from_yaml(char *template,
2324                                      struct llapi_layout **layout,
2325                                      struct lfs_setstripe_args *lsa,
2326                                      __u32 *osts)
2327 {
2328         struct cYAML *tree = NULL, *err_rc = NULL;
2329         int rc = 0;
2330
2331         tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
2332         if (!tree) {
2333                 fprintf(stderr, "%s: cannot parse YAML file %s\n",
2334                         progname, template);
2335                 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
2336                                   "can't parse", &err_rc);
2337                 cYAML_print_tree2file(stderr, err_rc);
2338                 cYAML_free_tree(err_rc);
2339                 rc = -EINVAL;
2340                 goto err;
2341         }
2342
2343         /* initialize lsa for plain file */
2344         setstripe_args_init(lsa);
2345         lsa->lsa_tgts = osts;
2346
2347         rc = build_layout_from_yaml_node(tree, layout, lsa, osts);
2348         if (rc) {
2349                 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
2350                         progname, template);
2351                 goto err;
2352         } else {
2353                 rc = build_component(layout, lsa, *layout != NULL);
2354         }
2355         /* clean clean lsa */
2356         setstripe_args_init(lsa);
2357
2358 err:
2359         if (tree)
2360                 cYAML_free_tree(tree);
2361         return rc;
2362 }
2363
2364 /* In 'lfs setstripe --component-add' mode, we need to fetch the extent
2365  * end of the last component in the existing file, and adjust the
2366  * first extent start of the components to be added accordingly. */
2367 static int adjust_first_extent(char *fname, struct llapi_layout *layout)
2368 {
2369         struct llapi_layout *head;
2370         uint64_t start, end, stripe_size, prev_end = 0;
2371         int rc;
2372
2373         if (layout == NULL) {
2374                 fprintf(stderr,
2375                         "%s setstripe: layout must be specified\n",
2376                         progname);
2377                 return -EINVAL;
2378         }
2379
2380         errno = 0;
2381         head = llapi_layout_get_by_path(fname, 0);
2382         if (head == NULL) {
2383                 fprintf(stderr,
2384                         "%s setstripe: cannot read layout from '%s': %s\n",
2385                         progname, fname, strerror(errno));
2386                 return -EINVAL;
2387         } else if (errno == ENODATA) {
2388                 /* file without LOVEA, this component-add will be turned
2389                  * into a component-create. */
2390                 llapi_layout_free(head);
2391                 return -ENODATA;
2392         } else if (!llapi_layout_is_composite(head)) {
2393                 fprintf(stderr, "%s setstripe: '%s' not a composite file\n",
2394                         progname, fname);
2395                 llapi_layout_free(head);
2396                 return -EINVAL;
2397         }
2398
2399         rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
2400         if (rc) {
2401                 fprintf(stderr, "%s setstripe: cannot get prev extent: %s\n",
2402                         progname, strerror(errno));
2403                 llapi_layout_free(head);
2404                 return rc;
2405         }
2406
2407         llapi_layout_free(head);
2408
2409         /* Make sure we use the first component of the layout to be added. */
2410         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
2411         if (rc < 0) {
2412                 fprintf(stderr,
2413                         "%s setstripe: cannot move component cursor: %s\n",
2414                         progname, strerror(errno));
2415                 return rc;
2416         }
2417
2418         rc = llapi_layout_comp_extent_get(layout, &start, &end);
2419         if (rc) {
2420                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
2421                         progname, strerror(errno));
2422                 return rc;
2423         }
2424
2425         if (start > prev_end || end <= prev_end) {
2426                 fprintf(stderr,
2427                         "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
2428                         progname, start, end, prev_end);
2429                 return -EINVAL;
2430         }
2431
2432         rc = llapi_layout_stripe_size_get(layout, &stripe_size);
2433         if (rc) {
2434                 fprintf(stderr, "%s setstripe: cannot get stripe size: %s\n",
2435                         progname, strerror(errno));
2436                 return rc;
2437         }
2438
2439         if (stripe_size != LLAPI_LAYOUT_DEFAULT &&
2440             (prev_end & (stripe_size - 1))) {
2441                 fprintf(stderr,
2442                         "%s setstripe: stripe size %lu not aligned with %lu\n",
2443                         progname, stripe_size, prev_end);
2444                 return -EINVAL;
2445         }
2446
2447         rc = llapi_layout_comp_extent_set(layout, prev_end, end);
2448         if (rc) {
2449                 fprintf(stderr,
2450                         "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
2451                         progname, prev_end, end, strerror(errno));
2452                 return rc;
2453         }
2454
2455         return 0;
2456 }
2457
2458 static inline bool arg_is_eof(char *arg)
2459 {
2460         return !strncmp(arg, "-1", strlen("-1")) ||
2461                !strncmp(arg, "EOF", strlen("EOF")) ||
2462                !strncmp(arg, "eof", strlen("eof"));
2463 }
2464
2465 /**
2466  * lfs_mirror_alloc() - Allocate a mirror argument structure.
2467  *
2468  * Return: Valid mirror_args pointer on success and
2469  *         NULL if memory allocation fails.
2470  */
2471 static struct mirror_args *lfs_mirror_alloc(void)
2472 {
2473         struct mirror_args *mirror = NULL;
2474
2475         while (1) {
2476                 mirror = calloc(1, sizeof(*mirror));
2477                 if (mirror != NULL)
2478                         break;
2479
2480                 sleep(1);
2481         }
2482
2483         return mirror;
2484 }
2485
2486 /**
2487  * lfs_mirror_free() - Free memory allocated for a mirror argument
2488  *                     structure.
2489  * @mirror: Previously allocated mirror argument structure by
2490  *          lfs_mirror_alloc().
2491  *
2492  * Free memory allocated for @mirror.
2493  *
2494  * Return: void.
2495  */
2496 static void lfs_mirror_free(struct mirror_args *mirror)
2497 {
2498         if (mirror->m_layout != NULL)
2499                 llapi_layout_free(mirror->m_layout);
2500         free(mirror);
2501 }
2502
2503 /**
2504  * lfs_mirror_list_free() - Free memory allocated for a mirror list.
2505  * @mirror_list: Previously allocated mirror list.
2506  *
2507  * Free memory allocated for @mirror_list.
2508  *
2509  * Return: void.
2510  */
2511 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
2512 {
2513         struct mirror_args *next_mirror = NULL;
2514
2515         while (mirror_list != NULL) {
2516                 next_mirror = mirror_list->m_next;
2517                 lfs_mirror_free(mirror_list);
2518                 mirror_list = next_mirror;
2519         }
2520 }
2521
2522 enum {
2523         LFS_POOL_OPT = 3,
2524         LFS_COMP_COUNT_OPT,
2525         LFS_COMP_START_OPT,
2526         LFS_COMP_FLAGS_OPT,
2527         LFS_COMP_DEL_OPT,
2528         LFS_COMP_SET_OPT,
2529         LFS_COMP_ADD_OPT,
2530         LFS_COMP_NO_VERIFY_OPT,
2531         LFS_PROJID_OPT,
2532         LFS_MIRROR_FLAGS_OPT,
2533         LFS_MIRROR_ID_OPT,
2534         LFS_MIRROR_STATE_OPT,
2535         LFS_LAYOUT_COPY,
2536         LFS_MIRROR_INDEX_OPT,
2537 };
2538
2539 /* functions */
2540 static int lfs_setstripe_internal(int argc, char **argv,
2541                                   enum setstripe_origin opc)
2542 {
2543         struct lfs_setstripe_args        lsa = { 0 };
2544         struct llapi_stripe_param       *param = NULL;
2545         struct find_param                migrate_mdt_param = {
2546                 .fp_max_depth = -1,
2547                 .fp_mdt_index = -1,
2548         };
2549         char                            *fname;
2550         int                              result = 0;
2551         int                              result2 = 0;
2552         char                            *end;
2553         int                              c;
2554         int                              delete = 0;
2555         unsigned long long               size_units = 1;
2556         bool                             migrate_mode = false;
2557         bool                             migrate_mdt_mode = false;
2558         bool                             migration_block = false;
2559         __u64                            migration_flags = 0;
2560         __u32                            tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
2561         int                              comp_del = 0, comp_set = 0;
2562         int                              comp_add = 0;
2563         __u32                            comp_id = 0;
2564         struct llapi_layout             *layout = NULL;
2565         struct llapi_layout             **lpp = &layout;
2566         bool                             mirror_mode = false;
2567         bool                             has_m_file = false;
2568         __u32                            mirror_count = 0;
2569         enum mirror_flags                mirror_flags = 0;
2570         struct mirror_args              *mirror_list = NULL;
2571         struct mirror_args              *new_mirror = NULL;
2572         struct mirror_args              *last_mirror = NULL;
2573         __u16                            mirror_id = 0;
2574         char                             cmd[PATH_MAX];
2575         bool from_yaml = false;
2576         bool from_copy = false;
2577         char *template = NULL;
2578
2579         struct option long_opts[] = {
2580 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
2581         /* --block is only valid in migrate mode */
2582         { .val = 'b',   .name = "block",        .has_arg = no_argument },
2583         { .val = LFS_COMP_ADD_OPT,
2584                         .name = "comp-add",     .has_arg = no_argument },
2585         { .val = LFS_COMP_ADD_OPT,
2586                         .name = "component-add", .has_arg = no_argument },
2587         { .val = LFS_COMP_DEL_OPT,
2588                         .name = "comp-del",     .has_arg = no_argument },
2589         { .val = LFS_COMP_DEL_OPT,
2590                         .name = "component-del", .has_arg = no_argument },
2591         { .val = LFS_COMP_FLAGS_OPT,
2592                         .name = "comp-flags",   .has_arg = required_argument },
2593         { .val = LFS_COMP_FLAGS_OPT,
2594                         .name = "component-flags",
2595                                                 .has_arg = required_argument },
2596         { .val = LFS_COMP_SET_OPT,
2597                         .name = "comp-set",     .has_arg = no_argument },
2598         { .val = LFS_COMP_SET_OPT,
2599                         .name = "component-set",
2600                                                 .has_arg = no_argument},
2601         { .val = LFS_COMP_NO_VERIFY_OPT,
2602                         .name = "no-verify",    .has_arg = no_argument},
2603         { .val = LFS_MIRROR_FLAGS_OPT,
2604                         .name = "flags",        .has_arg = required_argument},
2605         { .val = LFS_MIRROR_ID_OPT,
2606                         .name = "mirror-id",    .has_arg = required_argument},
2607         { .val = LFS_LAYOUT_COPY,
2608                         .name = "copy",         .has_arg = required_argument},
2609         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument},
2610         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument},
2611         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument},
2612 /* find { .val = 'C',   .name = "ctime",        .has_arg = required_argument }*/
2613         { .val = 'd',   .name = "delete",       .has_arg = no_argument},
2614         { .val = 'd',   .name = "destroy",      .has_arg = no_argument},
2615         /* --non-direct is only valid in migrate mode */
2616         { .val = 'D',   .name = "non-direct",   .has_arg = no_argument },
2617         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument},
2618         { .val = 'E',   .name = "component-end",
2619                                                 .has_arg = required_argument},
2620         { .val = 'f',   .name = "file",         .has_arg = required_argument },
2621 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
2622 /* find { .val = 'g',   .name = "gid",          .has_arg = no_argument }, */
2623 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
2624         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument},
2625         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument},
2626         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
2627         { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
2628         { .val = 'I',   .name = "component-id", .has_arg = required_argument},
2629         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
2630         { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
2631         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
2632         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument},
2633         /* --non-block is only valid in migrate mode */
2634         { .val = 'n',   .name = "non-block",    .has_arg = no_argument },
2635         { .val = 'N',   .name = "mirror-count", .has_arg = optional_argument},
2636         { .val = 'o',   .name = "ost",          .has_arg = required_argument },
2637 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2638         { .val = 'o',   .name = "ost-list",     .has_arg = required_argument },
2639         { .val = 'o',   .name = "ost_list",     .has_arg = required_argument },
2640 #endif
2641         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
2642 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
2643 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
2644 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
2645 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
2646         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
2647         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
2648 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
2649 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
2650 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
2651 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
2652         /* --verbose is only valid in migrate mode */
2653         { .val = 'v',   .name = "verbose",      .has_arg = no_argument},
2654         { .val = 'y',   .name = "yaml",         .has_arg = required_argument },
2655         { .name = NULL } };
2656
2657         setstripe_args_init(&lsa);
2658
2659         migrate_mode = (opc == SO_MIGRATE);
2660         mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
2661
2662         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
2663         progname = cmd;
2664         while ((c = getopt_long(argc, argv,
2665                                 "bc:dDE:f:H:i:I:m:N::no:p:L:s:S:vy:", long_opts,
2666                                 NULL)) >= 0) {
2667                 switch (c) {
2668                 case 0:
2669                         /* Long options. */
2670                         break;
2671                 case LFS_COMP_ADD_OPT:
2672                         comp_add = 1;
2673                         break;
2674                 case LFS_COMP_DEL_OPT:
2675                         comp_del = 1;
2676                         break;
2677                 case LFS_COMP_FLAGS_OPT:
2678                         result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
2679                                                 &lsa.lsa_comp_neg_flags);
2680                         if (result != 0)
2681                                 goto usage_error;
2682                         if (mirror_mode && lsa.lsa_comp_neg_flags) {
2683                                 fprintf(stderr, "%s: inverted flags are not supported\n",
2684                                         progname);
2685                                 goto usage_error;
2686                         }
2687                         if (lsa.lsa_comp_neg_flags & LCME_FL_STALE) {
2688                                 fprintf(stderr,
2689                                         "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
2690                                         progname);
2691                                 result = -EINVAL;
2692                                 goto error;
2693                         }
2694
2695                         break;
2696                 case LFS_COMP_SET_OPT:
2697                         comp_set = 1;
2698                         break;
2699                 case LFS_COMP_NO_VERIFY_OPT:
2700                         mirror_flags |= MF_NO_VERIFY;
2701                         break;
2702                 case LFS_MIRROR_ID_OPT:
2703                         mirror_id = strtoul(optarg, &end, 0);
2704                         if (*end != '\0' || mirror_id == 0) {
2705                                 fprintf(stderr,
2706                                         "%s %s: invalid mirror ID '%s'\n",
2707                                         progname, argv[0], optarg);
2708                                 goto usage_error;
2709                         }
2710                         break;
2711                 case LFS_MIRROR_FLAGS_OPT: {
2712                         __u32 flags;
2713
2714                         if (!mirror_mode || !last_mirror) {
2715                                 fprintf(stderr, "error: %s: --flags must be specified with --mirror-count|-N option\n",
2716                                         progname);
2717                                 goto usage_error;
2718                         }
2719
2720                         result = comp_str2flags(optarg, &last_mirror->m_flags,
2721                                                 &flags);
2722                         if (result != 0)
2723                                 goto usage_error;
2724
2725                         if (flags) {
2726                                 fprintf(stderr, "%s: inverted flags are not supported\n",
2727                                         progname);
2728                                 result = -EINVAL;
2729                                 goto usage_error;
2730                         }
2731                         if (last_mirror->m_flags & ~LCME_USER_FLAGS) {
2732                                 fprintf(stderr,
2733                                         "%s: unsupported mirror flags: %s\n",
2734                                         progname, optarg);
2735                                 result = -EINVAL;
2736                                 goto error;
2737                         }
2738                         break;
2739                 }
2740                 case LFS_LAYOUT_COPY:
2741                         from_copy = true;
2742                         template = optarg;
2743                         break;
2744                 case 'b':
2745                         if (!migrate_mode) {
2746                                 fprintf(stderr,
2747                                         "%s %s: -b|--block valid only for migrate command\n",
2748                                         progname, argv[0]);
2749                                 goto usage_error;
2750                         }
2751                         migration_block = true;
2752                         break;
2753                 case 'c':
2754                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
2755                         if (*end != '\0') {
2756                                 fprintf(stderr,
2757                                         "%s %s: invalid stripe count '%s'\n",
2758                                         progname, argv[0], optarg);
2759                                 goto usage_error;
2760                         }
2761
2762                         if (lsa.lsa_stripe_count == -1)
2763                                 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
2764                         break;
2765                 case 'd':
2766                         /* delete the default striping pattern */
2767                         delete = 1;
2768                         if (opc == SO_MIRROR_SPLIT) {
2769                                 if (has_m_file) {
2770                                         fprintf(stderr,
2771                                               "%s %s: -d cannot used with -f\n",
2772                                                 progname, argv[0]);
2773                                         goto usage_error;
2774                                 }
2775                                 mirror_flags |= MF_DESTROY;
2776                         }
2777                         break;
2778                 case 'D':
2779                         if (!migrate_mode) {
2780                                 fprintf(stderr,
2781                                         "%s %s: -D|--non-direct is valid "
2782                                         "only for migrate command\n",
2783                                         progname, argv[0]);
2784                                 goto usage_error;
2785                         }
2786                         migration_flags |= MIGRATION_NONDIRECT;
2787                         break;
2788                 case 'E':
2789                         if (lsa.lsa_comp_end != 0) {
2790                                 result = comp_args_to_layout(lpp, &lsa, true);
2791                                 if (result) {
2792                                         fprintf(stderr,
2793                                                 "%s %s: invalid layout\n",
2794                                                 progname, argv[0]);
2795                                         goto usage_error;
2796                                 }
2797
2798                                 setstripe_args_init_inherit(&lsa);
2799                         }
2800
2801                         if (arg_is_eof(optarg)) {
2802                                 lsa.lsa_comp_end = LUSTRE_EOF;
2803                         } else {
2804                                 result = llapi_parse_size(optarg,
2805                                                         &lsa.lsa_comp_end,
2806                                                         &size_units, 0);
2807                                 if (result) {
2808                                         fprintf(stderr,
2809                                                 "%s %s: invalid component end '%s'\n",
2810                                                 progname, argv[0], optarg);
2811                                         goto usage_error;
2812                                 }
2813                         }
2814                         break;
2815                 case 'H':
2816                         if (!migrate_mode) {
2817                                 fprintf(stderr, "--mdt-hash is valid only for migrate command\n");
2818                                 return CMD_HELP;
2819                         }
2820
2821                         lsa.lsa_pattern = check_hashtype(optarg);
2822                         if (lsa.lsa_pattern == 0) {
2823                                 fprintf(stderr,
2824                                         "%s %s: bad stripe hash type '%s'\n",
2825                                         progname, argv[0], optarg);
2826                                 return CMD_HELP;
2827                         }
2828                         break;
2829                 case 'i':
2830                         lsa.lsa_stripe_off = strtol(optarg, &end, 0);
2831                         if (*end != '\0') {
2832                                 fprintf(stderr,
2833                                         "%s %s: invalid stripe offset '%s'\n",
2834                                         progname, argv[0], optarg);
2835                                 goto usage_error;
2836                         }
2837                         if (lsa.lsa_stripe_off == -1)
2838                                 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2839                         break;
2840                 case 'I':
2841                         comp_id = strtoul(optarg, &end, 0);
2842                         if (*end != '\0' || comp_id == 0 ||
2843                             comp_id > LCME_ID_MAX) {
2844                                 fprintf(stderr,
2845                                         "%s %s: invalid component ID '%s'\n",
2846                                         progname, argv[0], optarg);
2847                                 goto usage_error;
2848                         }
2849                         break;
2850                 case 'f':
2851                         if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
2852                                 fprintf(stderr,
2853                                         "error: %s: invalid option: %s\n",
2854                                         progname, argv[optopt + 1]);
2855                                 goto usage_error;
2856                         }
2857                         if (opc == SO_MIRROR_EXTEND) {
2858                                 if (last_mirror == NULL) {
2859                                         fprintf(stderr,
2860                                 "error: %s: '-N' must exist in front of '%s'\n",
2861                                                 progname, argv[optopt + 1]);
2862                                         goto usage_error;
2863                                 }
2864                                 last_mirror->m_file = optarg;
2865                                 last_mirror->m_count = 1;
2866                         } else {
2867                                 /* mirror split */
2868                                 if (mirror_list == NULL)
2869                                         mirror_list = lfs_mirror_alloc();
2870                                 mirror_list->m_file = optarg;
2871                         }
2872                         has_m_file = true;
2873                         break;
2874                 case 'L':
2875                         if (strcmp(argv[optind - 1], "mdt") == 0) {
2876                                 /* Can be only the first component */
2877                                 if (layout != NULL) {
2878                                         result = -EINVAL;
2879                                         fprintf(stderr, "error: 'mdt' layout "
2880                                                 "can be only the first one\n");
2881                                         goto error;
2882                                 }
2883                                 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
2884                                         result = -EFBIG;
2885                                         fprintf(stderr, "error: 'mdt' layout "
2886                                                 "size is too big\n");
2887                                         goto error;
2888                                 }
2889                                 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
2890                         } else if (strcmp(argv[optind - 1], "raid0") != 0) {
2891                                 result = -EINVAL;
2892                                 fprintf(stderr, "error: layout '%s' is "
2893                                         "unknown, supported layouts are: "
2894                                         "'mdt', 'raid0'\n", argv[optind]);
2895                                 goto error;
2896                         }
2897                         break;
2898                 case 'm':
2899                         if (!migrate_mode) {
2900                                 fprintf(stderr,
2901                                         "%s %s: -m|--mdt is valid only for migrate command\n",
2902                                         progname, argv[0]);
2903                                 goto usage_error;
2904                         }
2905                         migrate_mdt_mode = true;
2906                         lsa.lsa_nr_tgts = parse_targets(tgts,
2907                                                 sizeof(tgts) / sizeof(__u32),
2908                                                 lsa.lsa_nr_tgts, optarg);
2909                         if (lsa.lsa_nr_tgts < 0) {
2910                                 fprintf(stderr,
2911                                         "%s %s: invalid MDT target(s) '%s'\n",
2912                                         progname, argv[0], optarg);
2913                                 return CMD_HELP;
2914                         }
2915
2916                         lsa.lsa_tgts = tgts;
2917                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2918                                 lsa.lsa_stripe_off = tgts[0];
2919                         break;
2920                 case 'n':
2921                         if (!migrate_mode) {
2922                                 fprintf(stderr,
2923                                         "%s %s: -n|--non-block valid only for migrate command\n",
2924                                         progname, argv[0]);
2925                                 goto usage_error;
2926                         }
2927                         migration_flags |= MIGRATION_NONBLOCK;
2928                         break;
2929                 case 'N':
2930                         if (opc == SO_SETSTRIPE) {
2931                                 opc = SO_MIRROR_CREATE;
2932                                 mirror_mode = true;
2933                         }
2934                         mirror_count = 1;
2935                         if (optarg != NULL) {
2936                                 mirror_count = strtoul(optarg, &end, 0);
2937                                 if (*end != '\0' || mirror_count == 0) {
2938                                         fprintf(stderr,
2939                                                 "error: %s: bad mirror count: %s\n",
2940                                                 progname, optarg);
2941                                         result = -EINVAL;
2942                                         goto error;
2943                                 }
2944                         }
2945
2946                         new_mirror = lfs_mirror_alloc();
2947                         new_mirror->m_count = mirror_count;
2948
2949                         if (mirror_list == NULL)
2950                                 mirror_list = new_mirror;
2951
2952                         if (last_mirror != NULL) {
2953                                 /* wrap up last mirror */
2954                                 if (lsa.lsa_comp_end == 0)
2955                                         lsa.lsa_comp_end = LUSTRE_EOF;
2956
2957                                 result = comp_args_to_layout(lpp, &lsa, true);
2958                                 if (result) {
2959                                         lfs_mirror_free(new_mirror);
2960                                         goto error;
2961                                 }
2962
2963                                 setstripe_args_init_inherit(&lsa);
2964
2965                                 last_mirror->m_next = new_mirror;
2966                         }
2967
2968                         last_mirror = new_mirror;
2969                         lpp = &last_mirror->m_layout;
2970                         break;
2971                 case 'o':
2972 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
2973                         if (strcmp(argv[optind - 1], "--ost-list") == 0)
2974                                 fprintf(stderr, "warning: '--ost-list' is "
2975                                         "deprecated, use '--ost' instead\n");
2976 #endif
2977                         lsa.lsa_nr_tgts = parse_targets(tgts,
2978                                                 sizeof(tgts) / sizeof(__u32),
2979                                                 lsa.lsa_nr_tgts, optarg);
2980                         if (lsa.lsa_nr_tgts < 0) {
2981                                 fprintf(stderr,
2982                                         "%s %s: invalid OST target(s) '%s'\n",
2983                                         progname, argv[0], optarg);
2984                                 goto usage_error;
2985                         }
2986
2987                         lsa.lsa_tgts = tgts;
2988                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
2989                                 lsa.lsa_stripe_off = tgts[0];
2990                         break;
2991                 case 'p':
2992                         if (optarg == NULL)
2993                                 goto usage_error;
2994                         lsa.lsa_pool_name = optarg;
2995
2996                         if (strlen(lsa.lsa_pool_name) == 0 ||
2997                             strncmp(lsa.lsa_pool_name, "none",
2998                                     LOV_MAXPOOLNAME) == 0)
2999                                 lsa.lsa_pool_name = NULL;
3000                         break;
3001                 case 'S':
3002                         result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
3003                                                   &size_units, 0);
3004                         if (result) {
3005                                 fprintf(stderr,
3006                                         "%s %s: invalid stripe size '%s'\n",
3007                                         progname, argv[0], optarg);
3008                                 goto usage_error;
3009                         }
3010                         break;
3011                 case 'v':
3012                         if (!migrate_mode) {
3013                                 fprintf(stderr,
3014                                         "%s %s: -v|--verbose valid only for migrate command\n",
3015                                         progname, argv[0]);
3016                                 goto usage_error;
3017                         }
3018                         migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
3019                         break;
3020                 case 'y':
3021                         from_yaml = true;
3022                         template = optarg;
3023                         break;
3024                 default:
3025                         fprintf(stderr, "%s %s: unrecognized option '%s'\n",
3026                                 progname, argv[0], argv[optind - 1]);
3027                         goto usage_error;
3028                 }
3029         }
3030
3031         fname = argv[optind];
3032
3033         if (optind == argc) {
3034                 fprintf(stderr, "%s %s: FILE must be specified\n",
3035                         progname, argv[0]);
3036                 goto usage_error;
3037         }
3038
3039         if (mirror_mode && mirror_count == 0) {
3040                 fprintf(stderr,
3041                         "error: %s: --mirror-count|-N option is required\n",
3042                         progname);
3043                 result = -EINVAL;
3044                 goto error;
3045         }
3046
3047         if (mirror_mode) {
3048                 if (lsa.lsa_comp_end == 0)
3049                         lsa.lsa_comp_end = LUSTRE_EOF;
3050         }
3051
3052         if (lsa.lsa_comp_end != 0) {
3053                 result = comp_args_to_layout(lpp, &lsa, true);
3054                 if (result)
3055                         goto error;
3056         }
3057
3058         if (mirror_flags & MF_NO_VERIFY) {
3059                 if (opc != SO_MIRROR_EXTEND) {
3060                         fprintf(stderr,
3061                                 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
3062                                 progname);
3063                         result = -EINVAL;
3064                         goto error;
3065                 } else if (!has_m_file) {
3066                         fprintf(stderr,
3067                                 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
3068                                 progname);
3069                         result = -EINVAL;
3070                         goto error;
3071                 }
3072         }
3073
3074         /* Only LCME_FL_INIT flags is used in PFL, and it shouldn't be
3075          * altered by user space tool, so we don't need to support the
3076          * --component-set for this moment. */
3077         if (comp_set && !comp_id) {
3078                 fprintf(stderr, "%s %s: --component-set doesn't have component-id set\n",
3079                         progname, argv[0]);
3080                 goto usage_error;
3081         }
3082
3083         if ((delete + comp_set + comp_del + comp_add) > 1) {
3084                 fprintf(stderr,
3085                         "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
3086                         progname, argv[0]);
3087                 goto usage_error;
3088         }
3089
3090         if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
3091                        lsa.lsa_comp_flags != 0 || layout != NULL)) {
3092                 fprintf(stderr,
3093                         "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
3094                         progname, argv[0]);
3095                 goto usage_error;
3096         }
3097
3098         if ((comp_set || comp_del) &&
3099             (setstripe_args_specified(&lsa) || layout != NULL)) {
3100                 fprintf(stderr,
3101                         "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
3102                         progname, argv[0]);
3103                 goto usage_error;
3104         }
3105
3106         if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
3107                 fprintf(stderr,
3108                         "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
3109                         progname, argv[0]);
3110                 goto usage_error;
3111         }
3112
3113         if (comp_add || comp_del) {
3114                 struct stat st;
3115
3116                 result = lstat(fname, &st);
3117                 if (result == 0 && S_ISDIR(st.st_mode)) {
3118                         fprintf(stderr,
3119                                 "%s setstripe: cannot use --component-add or --component-del for directory\n",
3120                                 progname);
3121                         goto usage_error;
3122                 }
3123
3124                 if (mirror_mode) {
3125                         fprintf(stderr, "error: %s: can't use --component-add "
3126                                 "or --component-del for mirror operation\n",
3127                                 progname);
3128                         goto usage_error;
3129                 }
3130         }
3131
3132         if (comp_add) {
3133                 if (layout == NULL) {
3134                         fprintf(stderr,
3135                                 "%s %s: option -E must be specified with --component-add\n",
3136                                 progname, argv[0]);
3137                         goto usage_error;
3138                 }
3139
3140                 result = adjust_first_extent(fname, layout);
3141                 if (result == -ENODATA)
3142                         comp_add = 0;
3143                 else if (result != 0)
3144                         goto error;
3145         }
3146
3147         if (from_yaml && from_copy) {
3148                 fprintf(stderr,
3149                         "%s: can't specify --yaml and --copy together\n",
3150                         progname);
3151                 goto error;
3152         }
3153
3154         if ((from_yaml || from_copy) &&
3155             (setstripe_args_specified(&lsa) || layout != NULL)) {
3156                 fprintf(stderr, "error: %s: can't specify --yaml with "
3157                         "-c, -S, -i, -o, -p or -E options.\n",
3158                         argv[0]);
3159                 goto error;
3160         }
3161
3162         if ((migration_flags & MIGRATION_NONBLOCK) && migration_block) {
3163                 fprintf(stderr,
3164                         "%s %s: options --non-block and --block are mutually exclusive\n",
3165                         progname, argv[0]);
3166                 goto usage_error;
3167         }
3168
3169         if (!comp_del && !comp_set && (opc != SO_MIRROR_SPLIT) &&
3170             comp_id != 0) {
3171                 fprintf(stderr,
3172                 "%s %s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
3173                         progname, argv[0]);
3174                 goto usage_error;
3175         }
3176
3177         if (migrate_mdt_mode) {
3178                 struct lmv_user_md *lmu;
3179
3180                 /* initialize migrate mdt parameters */
3181                 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
3182                                                  LMV_USER_MAGIC_SPECIFIC));
3183                 if (!lmu) {
3184                         fprintf(stderr,
3185                                 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
3186                                 progname, argv[0], strerror(ENOMEM));
3187                         result = -ENOMEM;
3188                         goto error;
3189                 }
3190                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
3191                         lmu->lum_stripe_count = lsa.lsa_stripe_count;
3192                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
3193                         fprintf(stderr,
3194                                 "%s %s: migrate should specify MDT index\n",
3195                                 progname, argv[0]);
3196                         free(lmu);
3197                         goto usage_error;
3198                 }
3199                 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
3200                 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
3201                         lmu->lum_hash_type = lsa.lsa_pattern;
3202                 else
3203                         lmu->lum_hash_type = LMV_HASH_TYPE_FNV_1A_64;
3204                 if (lsa.lsa_pool_name)
3205                         strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
3206                                 sizeof(lmu->lum_pool_name));
3207                 if (lsa.lsa_nr_tgts > 1) {
3208                         int i;
3209
3210                         if (lsa.lsa_stripe_count > 0 &&
3211                             lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3212                             lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
3213                                 fprintf(stderr,
3214                                         "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
3215                                         progname, lsa.lsa_stripe_count,
3216                                                 lsa.lsa_nr_tgts);
3217                                 free(lmu);
3218                                 goto usage_error;
3219                         }
3220
3221                         lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
3222                         lmu->lum_stripe_count = lsa.lsa_nr_tgts;
3223                         for (i = 0; i < lsa.lsa_nr_tgts; i++)
3224                                 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
3225                 } else {
3226                         lmu->lum_magic = LMV_USER_MAGIC;
3227                 }
3228
3229                 migrate_mdt_param.fp_lmv_md = lmu;
3230                 migrate_mdt_param.fp_migrate = 1;
3231         } else if (layout == NULL) {
3232                 /* initialize stripe parameters */
3233                 param = calloc(1, offsetof(typeof(*param),
3234                                lsp_osts[lsa.lsa_nr_tgts]));
3235                 if (param == NULL) {
3236                         fprintf(stderr,
3237                                 "%s %s: cannot allocate memory for parameters: %s\n",
3238                                 progname, argv[0], strerror(ENOMEM));
3239                         result = -ENOMEM;
3240                         goto error;
3241                 }
3242
3243                 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
3244                         param->lsp_stripe_size = lsa.lsa_stripe_size;
3245                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
3246                         if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
3247                                 param->lsp_stripe_count = -1;
3248                         else
3249                                 param->lsp_stripe_count = lsa.lsa_stripe_count;
3250                 }
3251                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
3252                         param->lsp_stripe_offset = -1;
3253                 else
3254                         param->lsp_stripe_offset = lsa.lsa_stripe_off;
3255                 param->lsp_pool = lsa.lsa_pool_name;
3256                 param->lsp_is_specific = false;
3257                 if (lsa.lsa_nr_tgts > 0) {
3258                         if (lsa.lsa_stripe_count > 0 &&
3259                             lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
3260                             lsa.lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
3261                             lsa.lsa_nr_tgts != lsa.lsa_stripe_count) {
3262                                 fprintf(stderr, "error: %s: stripe count %lld "
3263                                         "doesn't match the number of OSTs: %d\n"
3264                                         , argv[0], lsa.lsa_stripe_count,
3265                                         lsa.lsa_nr_tgts);
3266                                 free(param);
3267                                 goto usage_error;
3268                         }
3269
3270                         param->lsp_is_specific = true;
3271                         param->lsp_stripe_count = lsa.lsa_nr_tgts;
3272                         memcpy(param->lsp_osts, tgts,
3273                                sizeof(*tgts) * lsa.lsa_nr_tgts);
3274                 }
3275         }
3276
3277         if (from_yaml) {
3278                 /* generate a layout from a YAML template */
3279                 result = lfs_comp_create_from_yaml(template, &layout,
3280                                                    &lsa, tgts);
3281                 if (result) {
3282                         fprintf(stderr, "error: %s: can't create composite "
3283                                 "layout from template file %s\n",
3284                                 argv[0], template);
3285                         goto error;
3286                 }
3287         } else if (from_copy) {
3288                 layout = llapi_layout_get_by_path(template, 0);
3289                 if (layout == NULL) {
3290                         fprintf(stderr,
3291                             "%s: can't create composite layout from file %s.\n",
3292                                 progname, template);
3293                         goto error;
3294                 }
3295         }
3296
3297         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
3298                 if (migrate_mdt_mode) {
3299                         result = llapi_migrate_mdt(fname, &migrate_mdt_param);
3300                 } else if (migrate_mode) {
3301                         result = lfs_migrate(fname, migration_flags, param,
3302                                              layout);
3303                 } else if (comp_set != 0) {
3304                         result = lfs_component_set(fname, comp_id,
3305                                                    lsa.lsa_comp_flags,
3306                                                    lsa.lsa_comp_neg_flags);
3307                 } else if (comp_del != 0) {
3308                         result = lfs_component_del(fname, comp_id,
3309                                                    lsa.lsa_comp_flags,
3310                                                    lsa.lsa_comp_neg_flags);
3311                 } else if (comp_add != 0) {
3312                         result = lfs_component_add(fname, layout);
3313                 } else if (opc == SO_MIRROR_CREATE) {
3314                         result = mirror_create(fname, mirror_list);
3315                 } else if (opc == SO_MIRROR_EXTEND) {
3316                         result = mirror_extend(fname, mirror_list,
3317                                                mirror_flags);
3318                 } else if (opc == SO_MIRROR_SPLIT) {
3319                         if (mirror_id == 0 && comp_id == 0) {
3320                                 fprintf(stderr,
3321                         "%s %s: no mirror id or component id is specified\n",
3322                                         progname, argv[0]);
3323                                 goto usage_error;
3324                         }
3325                         if (mirror_id != 0)
3326                                 comp_id = mirror_id;
3327                         else
3328                                 mirror_flags |= MF_COMP_ID;
3329                         result = mirror_split(fname, comp_id, mirror_flags,
3330                                               has_m_file ? mirror_list->m_file :
3331                                               NULL);
3332                 } else if (layout != NULL) {
3333                         result = lfs_component_create(fname, O_CREAT | O_WRONLY,
3334                                                       0666, layout);
3335                         if (result >= 0) {
3336                                 close(result);
3337                                 result = 0;
3338                         }
3339                 } else {
3340                         result = llapi_file_open_param(fname,
3341                                                        O_CREAT | O_WRONLY,
3342                                                        0666, param);
3343                         if (result >= 0) {
3344                                 close(result);
3345                                 result = 0;
3346                         }
3347                 }
3348                 if (result) {
3349                         /* Save the first error encountered. */
3350                         if (result2 == 0)
3351                                 result2 = result;
3352                         continue;
3353                 }
3354         }
3355
3356         free(param);
3357         free(migrate_mdt_param.fp_lmv_md);
3358         llapi_layout_free(layout);
3359         lfs_mirror_list_free(mirror_list);
3360         return result2;
3361 usage_error:
3362         result = CMD_HELP;
3363 error:
3364         llapi_layout_free(layout);
3365         lfs_mirror_list_free(mirror_list);
3366         return result;
3367 }
3368
3369 static int lfs_poollist(int argc, char **argv)
3370 {
3371         if (argc != 2)
3372                 return CMD_HELP;
3373
3374         return llapi_poollist(argv[1]);
3375 }
3376
3377 static int set_time(time_t *time, time_t *set, char *str)
3378 {
3379         time_t t;
3380         int res = 0;
3381         char *endptr;
3382
3383         if (str[0] == '+')
3384                 res = 1;
3385         else if (str[0] == '-')
3386                 res = -1;
3387
3388         if (res)
3389                 str++;
3390
3391         t = strtol(str, &endptr, 0);
3392         if (*endptr != '\0') {
3393                 fprintf(stderr,
3394                         "%s find: bad time '%s': %s\n",
3395                         progname, str, strerror(EINVAL));
3396                 return INT_MAX;
3397         }
3398         if (*time < t * 24 * 60 * 60) {
3399                 if (res != 0)
3400                         str--;
3401                 fprintf(stderr,
3402                         "%s find: bad time '%s': too large\n",
3403                         progname, str);
3404                 return INT_MAX;
3405         }
3406
3407         *set = *time - t * 24 * 60 * 60;
3408         return res;
3409 }
3410
3411 static int name2uid(unsigned int *id, const char *name)
3412 {
3413         struct passwd *passwd;
3414
3415         passwd = getpwnam(name);
3416         if (passwd == NULL)
3417                 return -ENOENT;
3418         *id = passwd->pw_uid;
3419
3420         return 0;
3421 }
3422
3423 static int name2gid(unsigned int *id, const char *name)
3424 {
3425         struct group *group;
3426
3427         group = getgrnam(name);
3428         if (group == NULL)
3429                 return -ENOENT;
3430         *id = group->gr_gid;
3431
3432         return 0;
3433 }
3434
3435 static inline int name2projid(unsigned int *id, const char *name)
3436 {
3437         return -ENOTSUP;
3438 }
3439
3440 static int uid2name(char **name, unsigned int id)
3441 {
3442         struct passwd *passwd;
3443
3444         passwd = getpwuid(id);
3445         if (passwd == NULL)
3446                 return -ENOENT;
3447         *name = passwd->pw_name;
3448
3449         return 0;
3450 }
3451
3452 static inline int gid2name(char **name, unsigned int id)
3453 {
3454         struct group *group;
3455
3456         group = getgrgid(id);
3457         if (group == NULL)
3458                 return -ENOENT;
3459         *name = group->gr_name;
3460
3461         return 0;
3462 }
3463
3464 static int name2layout(__u32 *layout, char *name)
3465 {
3466         char *ptr, *layout_name;
3467
3468         *layout = 0;
3469         for (ptr = name; ; ptr = NULL) {
3470                 layout_name = strtok(ptr, ",");
3471                 if (layout_name == NULL)
3472                         break;
3473                 if (strcmp(layout_name, "released") == 0)
3474                         *layout |= LOV_PATTERN_F_RELEASED;
3475                 else if (strcmp(layout_name, "raid0") == 0)
3476                         *layout |= LOV_PATTERN_RAID0;
3477                 else if (strcmp(layout_name, "mdt") == 0)
3478                         *layout |= LOV_PATTERN_MDT;
3479                 else
3480                         return -1;
3481         }
3482         return 0;
3483 }
3484
3485 static int lfs_find(int argc, char **argv)
3486 {
3487         int c, rc;
3488         int ret = 0;
3489         time_t t;
3490         struct find_param param = {
3491                 .fp_max_depth = -1,
3492                 .fp_quiet = 1,
3493         };
3494         struct option long_opts[] = {
3495         { .val = 'A',   .name = "atime",        .has_arg = required_argument },
3496         { .val = 'b',   .name = "blocks",       .has_arg = required_argument },
3497         { .val = LFS_COMP_COUNT_OPT,
3498                         .name = "comp-count",   .has_arg = required_argument },
3499         { .val = LFS_COMP_COUNT_OPT,
3500                         .name = "component-count",
3501                                                 .has_arg = required_argument },
3502         { .val = LFS_COMP_FLAGS_OPT,
3503                         .name = "comp-flags",   .has_arg = required_argument },
3504         { .val = LFS_COMP_FLAGS_OPT,
3505                         .name = "component-flags",
3506                                                 .has_arg = required_argument },
3507         { .val = LFS_COMP_START_OPT,
3508                         .name = "comp-start",   .has_arg = required_argument },
3509         { .val = LFS_COMP_START_OPT,
3510                         .name = "component-start",
3511                                                 .has_arg = required_argument },
3512         { .val = LFS_MIRROR_STATE_OPT,
3513                         .name = "mirror-state", .has_arg = required_argument },
3514         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
3515         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
3516         { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
3517 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
3518         { .val = 'D',   .name = "maxdepth",     .has_arg = required_argument },
3519         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument },
3520         { .val = 'E',   .name = "component-end",
3521                                                 .has_arg = required_argument },
3522 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
3523         { .val = 'g',   .name = "gid",          .has_arg = required_argument },
3524         { .val = 'G',   .name = "group",        .has_arg = required_argument },
3525         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
3526         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
3527         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
3528 /* getstripe { .val = 'I', .name = "comp-id",   .has_arg = required_argument }*/
3529         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
3530         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
3531         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
3532         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument },
3533         { .val = 'M',   .name = "mtime",        .has_arg = required_argument },
3534         { .val = 'n',   .name = "name",         .has_arg = required_argument },
3535         { .val = 'N',   .name = "mirror-count", .has_arg = required_argument },
3536 /* find { .val = 'o'    .name = "or", .has_arg = no_argument }, like find(1) */
3537         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
3538         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
3539         /* no short option for pool yet, can be 'p' after 2.18 */
3540         { .val = LFS_POOL_OPT,
3541                         .name = "pool",         .has_arg = required_argument },
3542         { .val = '0',   .name = "print0",       .has_arg = no_argument },
3543         { .val = 'P',   .name = "print",        .has_arg = no_argument },
3544         { .val = LFS_PROJID_OPT,
3545                         .name = "projid",       .has_arg = required_argument },
3546 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
3547 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
3548 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
3549         { .val = 's',   .name = "size",         .has_arg = required_argument },
3550         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
3551         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
3552         { .val = 't',   .name = "type",         .has_arg = required_argument },
3553         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
3554         { .val = 'u',   .name = "uid",          .has_arg = required_argument },
3555         { .val = 'U',   .name = "user",         .has_arg = required_argument },
3556 /* getstripe { .val = 'v', .name = "verbose",   .has_arg = no_argument }, */
3557 /* getstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
3558         { .name = NULL } };
3559         int pathstart = -1;
3560         int pathend = -1;
3561         int neg_opt = 0;
3562         time_t *xtime;
3563         int *xsign;
3564         int isoption;
3565         char *endptr;
3566
3567         time(&t);
3568
3569         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
3570         while ((c = getopt_long_only(argc, argv,
3571                         "-0A:b:c:C:D:E:g:G:H:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:v",
3572                         long_opts, NULL)) >= 0) {
3573                 xtime = NULL;
3574                 xsign = NULL;
3575                 if (neg_opt)
3576                         --neg_opt;
3577                 /* '!' is part of option */
3578                 /* when getopt_long_only() finds a string which is not
3579                  * an option nor a known option argument it returns 1
3580                  * in that case if we already have found pathstart and pathend
3581                  * (i.e. we have the list of pathnames),
3582                  * the only supported value is "!"
3583                  */
3584                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
3585                 if (!isoption && pathend != -1) {
3586                         fprintf(stderr, "err: %s: filename|dirname must either "
3587                                         "precede options or follow options\n",
3588                                         argv[0]);
3589                         ret = CMD_HELP;
3590                         goto err;
3591                 }
3592                 if (!isoption && pathstart == -1)
3593                         pathstart = optind - 1;
3594                 if (isoption && pathstart != -1 && pathend == -1)
3595                         pathend = optind - 2;
3596                 switch (c) {
3597                 case 0:
3598                         /* Long options. */
3599                         break;
3600                 case 1:
3601                         /* unknown; opt is "!" or path component,
3602                          * checking done above.
3603                          */
3604                         if (strcmp(optarg, "!") == 0)
3605                                 neg_opt = 2;
3606                         break;
3607                 case 'A':
3608                         xtime = &param.fp_atime;
3609                         xsign = &param.fp_asign;
3610                         param.fp_exclude_atime = !!neg_opt;
3611                         /* no break, this falls through to 'C' for ctime */
3612                 case 'C':
3613                         if (c == 'C') {
3614                                 xtime = &param.fp_ctime;
3615                                 xsign = &param.fp_csign;
3616                                 param.fp_exclude_ctime = !!neg_opt;
3617                         }
3618                         /* no break, this falls through to 'M' for mtime */
3619                 case 'M':
3620                         if (c == 'M') {
3621                                 xtime = &param.fp_mtime;
3622                                 xsign = &param.fp_msign;
3623                                 param.fp_exclude_mtime = !!neg_opt;
3624                         }
3625                         rc = set_time(&t, xtime, optarg);
3626                         if (rc == INT_MAX) {
3627                                 ret = -1;
3628                                 goto err;
3629                         }
3630                         if (rc)
3631                                 *xsign = rc;
3632                         break;
3633                 case 'b':
3634                         if (optarg[0] == '+') {
3635                                 param.fp_blocks_sign = -1;
3636                                 optarg++;
3637                         } else if (optarg[0] == '-') {
3638                                 param.fp_blocks_sign =  1;
3639                                 optarg++;
3640                         }
3641
3642                         param.fp_blocks_units = 1024;
3643                         ret = llapi_parse_size(optarg, &param.fp_blocks,
3644                                                &param.fp_blocks_units, 0);
3645                         if (ret) {
3646                                 fprintf(stderr, "error: bad blocks '%s'\n",
3647                                         optarg);
3648                                 goto err;
3649                         }
3650                         param.fp_check_blocks = 1;
3651                         param.fp_exclude_blocks = !!neg_opt;
3652                         break;
3653                 case LFS_COMP_COUNT_OPT:
3654                         if (optarg[0] == '+') {
3655                                 param.fp_comp_count_sign = -1;
3656                                 optarg++;
3657                         } else if (optarg[0] == '-') {
3658                                 param.fp_comp_count_sign =  1;
3659                                 optarg++;
3660                         }
3661
3662                         param.fp_comp_count = strtoul(optarg, &endptr, 0);
3663                         if (*endptr != '\0') {
3664                                 fprintf(stderr, "error: bad component count "
3665                                         "'%s'\n", optarg);
3666                                 goto err;
3667                         }
3668                         param.fp_check_comp_count = 1;
3669                         param.fp_exclude_comp_count = !!neg_opt;
3670                         break;
3671                 case LFS_COMP_FLAGS_OPT:
3672                         rc = comp_str2flags(optarg, &param.fp_comp_flags,
3673                                             &param.fp_comp_neg_flags);
3674                         if (rc) {
3675                                 fprintf(stderr, "error: bad component flags "
3676                                         "'%s'\n", optarg);
3677                                 goto err;
3678                         }
3679                         param.fp_check_comp_flags = 1;
3680                         if (neg_opt) {
3681                                 __u32 flags = param.fp_comp_neg_flags;
3682                                 param.fp_comp_neg_flags = param.fp_comp_flags;
3683                                 param.fp_comp_flags = flags;
3684                         }
3685                         break;
3686                 case LFS_COMP_START_OPT:
3687                         if (optarg[0] == '+') {
3688                                 param.fp_comp_start_sign = -1;
3689                                 optarg++;
3690                         } else if (optarg[0] == '-') {
3691                                 param.fp_comp_start_sign =  1;
3692                                 optarg++;
3693                         }
3694
3695                         rc = llapi_parse_size(optarg, &param.fp_comp_start,
3696                                               &param.fp_comp_start_units, 0);
3697                         if (rc) {
3698                                 fprintf(stderr, "error: bad component start "
3699                                         "'%s'\n", optarg);
3700                                 goto err;
3701                         }
3702                         param.fp_check_comp_start = 1;
3703                         param.fp_exclude_comp_start = !!neg_opt;
3704                         break;
3705                 case LFS_MIRROR_STATE_OPT:
3706                         rc = mirror_str2state(optarg, &param.fp_mirror_state,
3707                                               &param.fp_mirror_neg_state);
3708                         if (rc) {
3709                                 fprintf(stderr,
3710                                         "error: bad mirrored file state '%s'\n",
3711                                         optarg);
3712                                 goto err;
3713                         }
3714                         param.fp_check_mirror_state = 1;
3715                         if (neg_opt) {
3716                                 __u16 state = param.fp_mirror_neg_state;
3717                                 param.fp_mirror_neg_state =
3718                                         param.fp_mirror_state;
3719                                 param.fp_mirror_state = state;
3720                         }
3721                         break;
3722                 case 'c':
3723                         if (optarg[0] == '+') {
3724                                 param.fp_stripe_count_sign = -1;
3725                                 optarg++;
3726                         } else if (optarg[0] == '-') {
3727                                 param.fp_stripe_count_sign =  1;
3728                                 optarg++;
3729                         }
3730
3731                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
3732                         if (*endptr != '\0') {
3733                                 fprintf(stderr,"error: bad stripe_count '%s'\n",
3734                                         optarg);
3735                                 ret = -1;
3736                                 goto err;
3737                         }
3738                         param.fp_check_stripe_count = 1;
3739                         param.fp_exclude_stripe_count = !!neg_opt;
3740                         break;
3741                 case 'D':
3742                         param.fp_max_depth = strtol(optarg, 0, 0);
3743                         break;
3744                 case 'E':
3745                         if (optarg[0] == '+') {
3746                                 param.fp_comp_end_sign = -1;
3747                                 optarg++;
3748                         } else if (optarg[0] == '-') {
3749                                 param.fp_comp_end_sign =  1;
3750                                 optarg++;
3751                         }
3752
3753                         if (arg_is_eof(optarg)) {
3754                                 param.fp_comp_end = LUSTRE_EOF;
3755                                 param.fp_comp_end_units = 1;
3756                                 rc = 0;
3757                         } else {
3758                                 rc = llapi_parse_size(optarg,
3759                                                 &param.fp_comp_end,
3760                                                 &param.fp_comp_end_units, 0);
3761                         }
3762                         if (rc) {
3763                                 fprintf(stderr, "error: bad component end "
3764                                         "'%s'\n", optarg);
3765                                 goto err;
3766                         }
3767                         param.fp_check_comp_end = 1;
3768                         param.fp_exclude_comp_end = !!neg_opt;
3769                         break;
3770                 case 'g':
3771                 case 'G':
3772                         rc = name2gid(&param.fp_gid, optarg);
3773                         if (rc) {
3774                                 param.fp_gid = strtoul(optarg, &endptr, 10);
3775                                 if (*endptr != '\0') {
3776                                         fprintf(stderr, "Group/GID: %s cannot "
3777                                                 "be found.\n", optarg);
3778                                         ret = -1;
3779                                         goto err;
3780                                 }
3781                         }
3782                         param.fp_exclude_gid = !!neg_opt;
3783                         param.fp_check_gid = 1;
3784                         break;
3785                 case 'H':
3786                         param.fp_hash_type = check_hashtype(optarg);
3787                         if (param.fp_hash_type == 0) {
3788                                 fprintf(stderr, "error: bad hash_type '%s'\n",
3789                                         optarg);
3790                                 ret = -1;
3791                                 goto err;
3792                         }
3793                         param.fp_check_hash_type = 1;
3794                         param.fp_exclude_hash_type = !!neg_opt;
3795                         break;
3796                 case 'L':
3797                         ret = name2layout(&param.fp_layout, optarg);
3798                         if (ret)
3799                                 goto err;
3800                         param.fp_exclude_layout = !!neg_opt;
3801                         param.fp_check_layout = 1;
3802                         break;
3803                 case 'u':
3804                 case 'U':
3805                         rc = name2uid(&param.fp_uid, optarg);
3806                         if (rc) {
3807                                 param.fp_uid = strtoul(optarg, &endptr, 10);
3808                                 if (*endptr != '\0') {
3809                                         fprintf(stderr, "User/UID: %s cannot "
3810                                                 "be found.\n", optarg);
3811                                         ret = -1;
3812                                         goto err;
3813                                 }
3814                         }
3815                         param.fp_exclude_uid = !!neg_opt;
3816                         param.fp_check_uid = 1;
3817                         break;
3818                 case 'n':
3819                         param.fp_pattern = (char *)optarg;
3820                         param.fp_exclude_pattern = !!neg_opt;
3821                         break;
3822                 case 'N':
3823                         if (optarg[0] == '+') {
3824                                 param.fp_mirror_count_sign = -1;
3825                                 optarg++;
3826                         } else if (optarg[0] == '-') {
3827                                 param.fp_mirror_count_sign =  1;
3828                                 optarg++;
3829                         }
3830
3831                         param.fp_mirror_count = strtoul(optarg, &endptr, 0);
3832                         if (*endptr != '\0') {
3833                                 fprintf(stderr,
3834                                         "error: bad mirror count '%s'\n",
3835                                         optarg);
3836                                 goto err;
3837                         }
3838                         param.fp_check_mirror_count = 1;
3839                         param.fp_exclude_mirror_count = !!neg_opt;
3840                         break;
3841                 case 'm':
3842                 case 'i':
3843                 case 'O': {
3844                         char *buf, *token, *next, *p;
3845                         int len = 1;
3846                         void *tmp;
3847
3848                         buf = strdup(optarg);
3849                         if (buf == NULL) {
3850                                 ret = -ENOMEM;
3851                                 goto err;
3852                         }
3853
3854                         param.fp_exclude_obd = !!neg_opt;
3855
3856                         token = buf;
3857                         while (token && *token) {
3858                                 token = strchr(token, ',');
3859                                 if (token) {
3860                                         len++;
3861                                         token++;
3862                                 }
3863                         }
3864                         if (c == 'm') {
3865                                 param.fp_exclude_mdt = !!neg_opt;
3866                                 param.fp_num_alloc_mdts += len;
3867                                 tmp = realloc(param.fp_mdt_uuid,
3868                                               param.fp_num_alloc_mdts *
3869                                               sizeof(*param.fp_mdt_uuid));
3870                                 if (tmp == NULL) {
3871                                         ret = -ENOMEM;
3872                                         goto err_free;
3873                                 }
3874
3875                                 param.fp_mdt_uuid = tmp;
3876                         } else {
3877                                 param.fp_exclude_obd = !!neg_opt;
3878                                 param.fp_num_alloc_obds += len;
3879                                 tmp = realloc(param.fp_obd_uuid,
3880                                               param.fp_num_alloc_obds *
3881                                               sizeof(*param.fp_obd_uuid));
3882                                 if (tmp == NULL) {
3883                                         ret = -ENOMEM;
3884                                         goto err_free;
3885                                 }
3886
3887                                 param.fp_obd_uuid = tmp;
3888                         }
3889                         for (token = buf; token && *token; token = next) {
3890                                 struct obd_uuid *puuid;
3891                                 if (c == 'm') {
3892                                         puuid =
3893                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
3894                                 } else {
3895                                         puuid =
3896                                         &param.fp_obd_uuid[param.fp_num_obds++];
3897                                 }
3898                                 p = strchr(token, ',');
3899                                 next = 0;
3900                                 if (p) {
3901                                         *p = 0;
3902                                         next = p+1;
3903                                 }
3904
3905                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
3906                                         ret = -E2BIG;
3907                                         goto err_free;
3908                                 }
3909
3910                                 strncpy(puuid->uuid, token,
3911                                         sizeof(puuid->uuid));
3912                         }
3913 err_free:
3914                         if (buf)
3915                                 free(buf);
3916                         break;
3917                 }
3918 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
3919                 case 'p':
3920 #endif
3921                 case LFS_POOL_OPT:
3922                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
3923                                 fprintf(stderr,
3924                                         "Pool name %s is too long (max %d)\n",
3925                                         optarg, LOV_MAXPOOLNAME);
3926                                 ret = -1;
3927                                 goto err;
3928                         }
3929                         /*
3930                          * We do check for empty pool because empty pool
3931                          * is used to find V1 LOV attributes
3932                          */
3933                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
3934                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
3935                         param.fp_exclude_pool = !!neg_opt;
3936                         param.fp_check_pool = 1;
3937                         break;
3938 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 14, 53, 0)
3939                 case 'p': /* want this for --pool, to match getstripe/find */
3940                         fprintf(stderr,
3941                                 "warning: -p deprecated, use --print0 or -0\n");
3942 #endif
3943                 case '0':
3944                         param.fp_zero_end = 1;
3945                         break;
3946                 case 'P': /* we always print, this option is a no-op */
3947                         break;
3948                 case LFS_PROJID_OPT:
3949                         rc = name2projid(&param.fp_projid, optarg);
3950                         if (rc) {
3951                                 param.fp_projid = strtoul(optarg, &endptr, 10);
3952                                 if (*endptr != '\0') {
3953                                         fprintf(stderr,
3954                                                 "Invalid project ID: %s",
3955                                                 optarg);
3956                                         ret = -1;
3957                                         goto err;
3958                                 }
3959                         }
3960                         param.fp_exclude_projid = !!neg_opt;
3961                         param.fp_check_projid = 1;
3962                         break;
3963                 case 's':
3964                         if (optarg[0] == '+') {
3965                                 param.fp_size_sign = -1;
3966                                 optarg++;
3967                         } else if (optarg[0] == '-') {
3968                                 param.fp_size_sign =  1;
3969                                 optarg++;
3970                         }
3971
3972                         ret = llapi_parse_size(optarg, &param.fp_size,
3973                                                &param.fp_size_units, 0);
3974                         if (ret) {
3975                                 fprintf(stderr, "error: bad file size '%s'\n",
3976                                         optarg);
3977                                 goto err;
3978                         }
3979                         param.fp_check_size = 1;
3980                         param.fp_exclude_size = !!neg_opt;
3981                         break;
3982                 case 'S':
3983                         if (optarg[0] == '+') {
3984                                 param.fp_stripe_size_sign = -1;
3985                                 optarg++;
3986                         } else if (optarg[0] == '-') {
3987                                 param.fp_stripe_size_sign =  1;
3988                                 optarg++;
3989                         }
3990
3991                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
3992                                                &param.fp_stripe_size_units, 0);
3993                         if (ret) {
3994                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
3995                                         optarg);
3996                                 goto err;
3997                         }
3998                         param.fp_check_stripe_size = 1;
3999                         param.fp_exclude_stripe_size = !!neg_opt;
4000                         break;
4001                 case 't':
4002                         param.fp_exclude_type = !!neg_opt;
4003                         switch (optarg[0]) {
4004                         case 'b':
4005                                 param.fp_type = S_IFBLK;
4006                                 break;
4007                         case 'c':
4008                                 param.fp_type = S_IFCHR;
4009                                 break;
4010                         case 'd':
4011                                 param.fp_type = S_IFDIR;
4012                                 break;
4013                         case 'f':
4014                                 param.fp_type = S_IFREG;
4015                                 break;
4016                         case 'l':
4017                                 param.fp_type = S_IFLNK;
4018                                 break;
4019                         case 'p':
4020                                 param.fp_type = S_IFIFO;
4021                                 break;
4022                         case 's':
4023                                 param.fp_type = S_IFSOCK;
4024                                 break;
4025                         default:
4026                                 fprintf(stderr, "error: %s: bad type '%s'\n",
4027                                         argv[0], optarg);
4028                                 ret = CMD_HELP;
4029                                 goto err;
4030                         };
4031                         break;
4032                 case 'T':
4033                         if (optarg[0] == '+') {
4034                                 param.fp_mdt_count_sign = -1;
4035                                 optarg++;
4036                         } else if (optarg[0] == '-') {
4037                                 param.fp_mdt_count_sign =  1;
4038                                 optarg++;
4039                         }
4040
4041                         param.fp_mdt_count = strtoul(optarg, &endptr, 0);
4042                         if (*endptr != '\0') {
4043                                 fprintf(stderr, "error: bad mdt_count '%s'\n",
4044                                         optarg);
4045                                 ret = -1;
4046                                 goto err;
4047                         }
4048                         param.fp_check_mdt_count = 1;
4049                         param.fp_exclude_mdt_count = !!neg_opt;
4050                         break;
4051                 default:
4052                         ret = CMD_HELP;
4053                         goto err;
4054                 };
4055         }
4056
4057         if (pathstart == -1) {
4058                 fprintf(stderr, "error: %s: no filename|pathname\n",
4059                         argv[0]);
4060                 ret = CMD_HELP;
4061                 goto err;
4062         } else if (pathend == -1) {
4063                 /* no options */
4064                 pathend = argc;
4065         }
4066
4067         do {
4068                 rc = llapi_find(argv[pathstart], &param);
4069                 if (rc != 0 && ret == 0)
4070                         ret = rc;
4071         } while (++pathstart < pathend);
4072
4073         if (ret)
4074                 fprintf(stderr, "error: %s failed for %s.\n",
4075                         argv[0], argv[optind - 1]);
4076 err:
4077         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
4078                 free(param.fp_obd_uuid);
4079
4080         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
4081                 free(param.fp_mdt_uuid);
4082
4083         return ret;
4084 }
4085
4086 static int lfs_getstripe_internal(int argc, char **argv,
4087                                   struct find_param *param)
4088 {
4089         struct option long_opts[] = {
4090 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
4091 /* find { .val = 'b',   .name = "blocks",       .has_arg = required_argument }*/
4092         { .val = LFS_COMP_COUNT_OPT,
4093                         .name = "comp-count",   .has_arg = no_argument },
4094         { .val = LFS_COMP_COUNT_OPT,
4095                 .name = "component-count",      .has_arg = no_argument },
4096         { .val = LFS_COMP_FLAGS_OPT,
4097                         .name = "comp-flags",   .has_arg = optional_argument },
4098         { .val = LFS_COMP_FLAGS_OPT,
4099                 .name = "component-flags",      .has_arg = optional_argument },
4100         { .val = LFS_COMP_START_OPT,
4101                         .name = "comp-start",   .has_arg = optional_argument },
4102         { .val = LFS_COMP_START_OPT,
4103                 .name = "component-start",      .has_arg = optional_argument },
4104         { .val = LFS_MIRROR_INDEX_OPT,
4105                 .name = "mirror-index",         .has_arg = required_argument },
4106         { .val = LFS_MIRROR_ID_OPT,
4107                 .name = "mirror-id",            .has_arg = required_argument },
4108         { .val = 'c',   .name = "stripe-count", .has_arg = no_argument },
4109         { .val = 'c',   .name = "stripe_count", .has_arg = no_argument },
4110 /* find { .val = 'C',   .name = "ctime",        .has_arg = required_argument }*/
4111         { .val = 'd',   .name = "directory",    .has_arg = no_argument },
4112         { .val = 'D',   .name = "default",      .has_arg = no_argument },
4113         { .val = 'E',   .name = "comp-end",     .has_arg = optional_argument },
4114         { .val = 'E',   .name = "component-end", .has_arg = optional_argument },
4115         { .val = 'F',   .name = "fid",          .has_arg = no_argument },
4116         { .val = 'g',   .name = "generation",   .has_arg = no_argument },
4117 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
4118 /* dirstripe { .val = 'H', .name = "mdt-hash",  .has_arg = required_argument }*/
4119         { .val = 'i',   .name = "stripe-index", .has_arg = no_argument },
4120         { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
4121         { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
4122         { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
4123         { .val = 'L',   .name = "layout",       .has_arg = no_argument },
4124         { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
4125         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
4126         { .val = 'm',   .name = "mdt_index",    .has_arg = no_argument },
4127 /* find { .val = 'M',   .name = "mtime",        .has_arg = required_argument }*/
4128 /* find { .val = 'n',   .name = "name",         .has_arg = required_argument }*/
4129         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
4130         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
4131         { .val = 'p',   .name = "pool",         .has_arg = no_argument },
4132 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
4133         { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
4134         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
4135         { .val = 'R',   .name = "raw",          .has_arg = no_argument },
4136         { .val = 'S',   .name = "stripe-size",  .has_arg = no_argument },
4137         { .val = 'S',   .name = "stripe_size",  .has_arg = no_argument },
4138 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
4139 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
4140 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
4141 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
4142         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
4143         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
4144         { .name = NULL } };
4145         int c, rc;
4146         int neg_opt = 0;
4147         int pathstart = -1, pathend = -1;
4148         int isoption;
4149         char *end, *tmp;
4150
4151         while ((c = getopt_long(argc, argv,
4152                         "-cdDE::FghiI::LmMoO:pqrRsSvy",
4153                         long_opts, NULL)) != -1) {
4154                 if (neg_opt)
4155                         --neg_opt;
4156
4157                 /* '!' is part of option */
4158                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
4159                 if (!isoption && pathend != -1) {
4160                         fprintf(stderr,
4161                                 "error: %s: filename|dirname must either precede options or follow options\n",
4162                                 argv[0]);
4163                         return CMD_HELP;
4164                 }
4165                 if (!isoption && pathstart == -1)
4166                         pathstart = optind - 1;
4167                 if (isoption && pathstart != -1 && pathend == -1)
4168                         pathend = optind - 2;
4169
4170                 switch (c) {
4171                 case 1:
4172                         /* unknown: opt is "!" */
4173                         if (strcmp(optarg, "!") == 0)
4174                                 neg_opt = 2;
4175                         break;
4176                 case 'c':
4177                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4178                                 param->fp_verbose |= VERBOSE_COUNT;
4179                                 param->fp_max_depth = 0;
4180                         }
4181                         break;
4182                 case LFS_COMP_COUNT_OPT:
4183                         param->fp_verbose |= VERBOSE_COMP_COUNT;
4184                         param->fp_max_depth = 0;
4185                         break;
4186                 case LFS_COMP_FLAGS_OPT:
4187                         if (optarg != NULL) {
4188                                 rc = comp_str2flags(optarg,
4189                                                     &param->fp_comp_flags,
4190                                                     &param->fp_comp_neg_flags);
4191                                 if (rc != 0) {
4192                                         fprintf(stderr, "error: %s bad "
4193                                                 "component flags '%s'.\n",
4194                                                 argv[0], optarg);
4195                                         return CMD_HELP;
4196                                 }
4197                                 param->fp_check_comp_flags = 1;
4198                         } else {
4199                                 param->fp_verbose |= VERBOSE_COMP_FLAGS;
4200                                 param->fp_max_depth = 0;
4201                         }
4202                         break;
4203                 case LFS_COMP_START_OPT:
4204                         if (optarg != NULL) {
4205                                 tmp = optarg;
4206                                 if (tmp[0] == '+') {
4207                                         param->fp_comp_start_sign = -1;
4208                                         tmp++;
4209                                 } else if (tmp[0] == '-') {
4210                                         param->fp_comp_start_sign = 1;
4211                                         tmp++;
4212                                 }
4213                                 rc = llapi_parse_size(tmp,
4214                                                 &param->fp_comp_start,
4215                                                 &param->fp_comp_start_units, 0);
4216                                 if (rc != 0) {
4217                                         fprintf(stderr, "error: %s bad "
4218                                                 "component start '%s'.\n",
4219                                                 argv[0], tmp);
4220                                         return CMD_HELP;
4221                                 } else {
4222                                         param->fp_check_comp_start = 1;
4223                                 }
4224                         } else {
4225                                 param->fp_verbose |= VERBOSE_COMP_START;
4226                                 param->fp_max_depth = 0;
4227                         }
4228                         break;
4229                 case LFS_MIRROR_INDEX_OPT:
4230                         if (optarg[0] == '+') {
4231                                 param->fp_mirror_index_sign = -1;
4232                                 optarg++;
4233                         } else if (optarg[0] == '-') {
4234                                 param->fp_mirror_index_sign = 1;
4235                                 optarg++;
4236                         }
4237
4238                         param->fp_mirror_index = strtoul(optarg, &end, 0);
4239                         if (*end != '\0' || (param->fp_mirror_index == 0 &&
4240                             param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
4241                                 fprintf(stderr,
4242                                         "%s %s: invalid mirror index '%s'\n",
4243                                         progname, argv[0], optarg);
4244                                 return CMD_HELP;
4245                         }
4246                         if (param->fp_mirror_id != 0) {
4247                                 fprintf(stderr,
4248                                         "%s %s: can't specify both mirror index and mirror ID\n",
4249                                         progname, argv[0]);
4250                                 return CMD_HELP;
4251                         }
4252                         param->fp_check_mirror_index = 1;
4253                         param->fp_exclude_mirror_index = !!neg_opt;
4254                         break;
4255                 case LFS_MIRROR_ID_OPT:
4256                         if (optarg[0] == '+') {
4257                                 param->fp_mirror_id_sign = -1;
4258                                 optarg++;
4259                         } else if (optarg[0] == '-') {
4260                                 param->fp_mirror_id_sign = 1;
4261                                 optarg++;
4262                         }
4263
4264                         param->fp_mirror_id = strtoul(optarg, &end, 0);
4265                         if (*end != '\0' || (param->fp_mirror_id == 0 &&
4266                             param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
4267                                 fprintf(stderr,
4268                                         "%s %s: invalid mirror ID '%s'\n",
4269                                         progname, argv[0], optarg);
4270                                 return CMD_HELP;
4271                         }
4272                         if (param->fp_mirror_index != 0) {
4273                                 fprintf(stderr,
4274                                         "%s %s: can't specify both mirror index and mirror ID\n",
4275                                         progname, argv[0]);
4276                                 return CMD_HELP;
4277                         }
4278                         param->fp_check_mirror_id = 1;
4279                         param->fp_exclude_mirror_id = !!neg_opt;
4280                         break;
4281                 case 'd':
4282                         param->fp_max_depth = 0;
4283                         break;
4284                 case 'D':
4285                         param->fp_get_default_lmv = 1;
4286                         break;
4287                 case 'E':
4288                         if (optarg != NULL) {
4289                                 tmp = optarg;
4290                                 if (tmp[0] == '+') {
4291                                         param->fp_comp_end_sign = -1;
4292                                         tmp++;
4293                                 } else if (tmp[0] == '-') {
4294                                         param->fp_comp_end_sign = 1;
4295                                         tmp++;
4296                                 }
4297
4298                                 if (arg_is_eof(tmp)) {
4299                                         param->fp_comp_end = LUSTRE_EOF;
4300                                         param->fp_comp_end_units = 1;
4301                                         rc = 0;
4302                                 } else {
4303                                         rc = llapi_parse_size(tmp,
4304                                                 &param->fp_comp_end,
4305                                                 &param->fp_comp_end_units, 0);
4306                                 }
4307                                 if (rc != 0) {
4308                                         fprintf(stderr, "error: %s bad "
4309                                                 "component end '%s'.\n",
4310                                                 argv[0], tmp);
4311                                         return CMD_HELP;
4312                                 }
4313                                 param->fp_check_comp_end = 1;
4314                         } else {
4315                                 param->fp_verbose |= VERBOSE_COMP_END;
4316                                 param->fp_max_depth = 0;
4317                         }
4318                         break;
4319                 case 'F':
4320                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4321                                 param->fp_verbose |= VERBOSE_DFID;
4322                                 param->fp_max_depth = 0;
4323                         }
4324                         break;
4325                 case 'g':
4326                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4327                                 param->fp_verbose |= VERBOSE_GENERATION;
4328                                 param->fp_max_depth = 0;
4329                         }
4330                         break;
4331                 case 'i':
4332                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4333                                 param->fp_verbose |= VERBOSE_OFFSET;
4334                                 param->fp_max_depth = 0;
4335                         }
4336                         break;
4337                 case 'I':
4338                         if (optarg != NULL) {
4339                                 param->fp_comp_id = strtoul(optarg, &end, 0);
4340                                 if (*end != '\0' || param->fp_comp_id == 0 ||
4341                                     param->fp_comp_id > LCME_ID_MAX) {
4342                                         fprintf(stderr, "error: %s bad "
4343                                                 "component id '%s'\n",
4344                                                 argv[0], optarg);
4345                                         return CMD_HELP;
4346                                 } else {
4347                                         param->fp_check_comp_id = 1;
4348                                 }
4349                         } else {
4350                                 param->fp_max_depth = 0;
4351                                 param->fp_verbose |= VERBOSE_COMP_ID;
4352                         }
4353                         break;
4354                 case 'L':
4355                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4356                                 param->fp_verbose |= VERBOSE_LAYOUT;
4357                                 param->fp_max_depth = 0;
4358                         }
4359                         break;
4360 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4361                 case 'M':
4362                         fprintf(stderr, "warning: '-M' deprecated"
4363                                 ", use '--mdt-index' or '-m' instead\n");
4364 #endif
4365                 case 'm':
4366                         if (!(param->fp_verbose & VERBOSE_DETAIL))
4367                                 param->fp_max_depth = 0;
4368                         param->fp_verbose |= VERBOSE_MDTINDEX;
4369                         break;
4370                 case 'O':
4371                         if (param->fp_obd_uuid) {
4372                                 fprintf(stderr,
4373                                         "error: %s: only one obduuid allowed",
4374                                         argv[0]);
4375                                 return CMD_HELP;
4376                         }
4377                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
4378                         break;
4379                 case 'p':
4380                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4381                                 param->fp_verbose |= VERBOSE_POOL;
4382                                 param->fp_max_depth = 0;
4383                         }
4384                         break;
4385                 case 'q':
4386                         param->fp_quiet++;
4387                         break;
4388                 case 'r':
4389                         param->fp_recursive = 1;
4390                         break;
4391                 case 'R':
4392                         param->fp_raw = 1;
4393                         break;
4394                 case 'S':
4395                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
4396                                 param->fp_verbose |= VERBOSE_SIZE;
4397                                 param->fp_max_depth = 0;
4398                         }
4399                         break;
4400                 case 'v':
4401                         param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
4402                         break;
4403                 case 'y':
4404                         param->fp_yaml = 1;
4405                         break;
4406                 default:
4407                         return CMD_HELP;
4408                 }
4409         }
4410
4411         if (pathstart == -1) {
4412                 fprintf(stderr, "error: %s: no filename|pathname\n",
4413                                 argv[0]);
4414                 return CMD_HELP;
4415         } else if (pathend == -1) {
4416                 /* no options */
4417                 pathend = argc;
4418         }
4419
4420         if (pathend > argc)
4421                 return CMD_HELP;
4422
4423         if (param->fp_recursive)
4424                 param->fp_max_depth = -1;
4425         else if (param->fp_verbose & VERBOSE_DETAIL)
4426                 param->fp_max_depth = 1;
4427
4428         if (!param->fp_verbose)
4429                 param->fp_verbose = VERBOSE_DEFAULT;
4430         if (param->fp_quiet)
4431                 param->fp_verbose = VERBOSE_OBJID;
4432
4433         do {
4434                 rc = llapi_getstripe(argv[pathstart], param);
4435         } while (++pathstart < pathend && !rc);
4436
4437         if (rc)
4438                 fprintf(stderr, "error: %s failed for %s.\n",
4439                         argv[0], argv[optind - 1]);
4440         return rc;
4441 }
4442
4443 static int lfs_tgts(int argc, char **argv)
4444 {
4445         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
4446         struct find_param param;
4447         int index = 0, rc=0;
4448
4449         if (argc > 2)
4450                 return CMD_HELP;
4451
4452         if (argc == 2 && !realpath(argv[1], path)) {
4453                 rc = -errno;
4454                 fprintf(stderr, "error: invalid path '%s': %s\n",
4455                         argv[1], strerror(-rc));
4456                 return rc;
4457         }
4458
4459         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
4460                 /* Check if we have a mount point */
4461                 if (mntdir[0] == '\0')
4462                         continue;
4463
4464                 memset(&param, 0, sizeof(param));
4465                 if (!strcmp(argv[0], "mdts"))
4466                         param.fp_get_lmv = 1;
4467
4468                 rc = llapi_ostlist(mntdir, &param);
4469                 if (rc) {
4470                         fprintf(stderr, "error: %s: failed on %s\n",
4471                                 argv[0], mntdir);
4472                 }
4473                 if (path[0] != '\0')
4474                         break;
4475                 memset(mntdir, 0, PATH_MAX);
4476         }
4477
4478         return rc;
4479 }
4480
4481 static int lfs_getstripe(int argc, char **argv)
4482 {
4483         struct find_param param = { 0 };
4484
4485         param.fp_max_depth = 1;
4486         return lfs_getstripe_internal(argc, argv, &param);
4487 }
4488
4489 /* functions */
4490 static int lfs_getdirstripe(int argc, char **argv)
4491 {
4492         struct find_param param = { 0 };
4493         struct option long_opts[] = {
4494         { .val = 'c',   .name = "mdt-count",    .has_arg = no_argument },
4495         { .val = 'D',   .name = "default",      .has_arg = no_argument },
4496         { .val = 'H',   .name = "mdt-hash",     .has_arg = no_argument },
4497         { .val = 'i',   .name = "mdt-index",    .has_arg = no_argument },
4498         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
4499         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
4500         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
4501         { .val = 'T',   .name = "mdt-count",    .has_arg = no_argument },
4502         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
4503         { .name = NULL } };
4504         int c, rc;
4505
4506         param.fp_get_lmv = 1;
4507
4508         while ((c = getopt_long(argc, argv,
4509                                 "cDHimO:rtTy", long_opts, NULL)) != -1)
4510         {
4511                 switch (c) {
4512                 case 'c':
4513                 case 'T':
4514                         param.fp_verbose |= VERBOSE_COUNT;
4515                         break;
4516                 case 'D':
4517                         param.fp_get_default_lmv = 1;
4518                         break;
4519                 case 'i':
4520                 case 'm':
4521                         param.fp_verbose |= VERBOSE_OFFSET;
4522                         break;
4523 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4524                 case 't':
4525                         fprintf(stderr, "warning: '-t' deprecated, "
4526                                 "use '--mdt-hash' or '-H' instead\n");
4527 #endif
4528                 case 'H':
4529                         param.fp_verbose |= VERBOSE_HASH_TYPE;
4530                         break;
4531                 case 'O':
4532                         if (param.fp_obd_uuid) {
4533                                 fprintf(stderr,
4534                                         "error: %s: only one obduuid allowed",
4535                                         argv[0]);
4536                                 return CMD_HELP;
4537                         }
4538                         param.fp_obd_uuid = (struct obd_uuid *)optarg;
4539                         break;
4540                 case 'r':
4541                         param.fp_recursive = 1;
4542                         break;
4543                 case 'y':
4544                         param.fp_yaml = 1;
4545                         break;
4546                 default:
4547                         return CMD_HELP;
4548                 }
4549         }
4550
4551         if (optind >= argc)
4552                 return CMD_HELP;
4553
4554         if (param.fp_recursive)
4555                 param.fp_max_depth = -1;
4556
4557         if (!param.fp_verbose)
4558                 param.fp_verbose = VERBOSE_DEFAULT;
4559
4560         do {
4561                 rc = llapi_getstripe(argv[optind], &param);
4562         } while (++optind < argc && !rc);
4563
4564         if (rc)
4565                 fprintf(stderr, "error: %s failed for %s.\n",
4566                         argv[0], argv[optind - 1]);
4567         return rc;
4568 }
4569
4570 enum mntdf_flags {
4571         MNTDF_INODES    = 0x0001,
4572         MNTDF_COOKED    = 0x0002,
4573         MNTDF_LAZY      = 0x0004,
4574         MNTDF_VERBOSE   = 0x0008,
4575         MNTDF_SHOW      = 0x0010,
4576 };
4577
4578 #define COOK(value)                                             \
4579 ({                                                              \
4580         int radix = 0;                                          \
4581         while (value > 1024) {                                  \
4582                 value /= 1024;                                  \
4583                 radix++;                                        \
4584         }                                                       \
4585         radix;                                                  \
4586 })
4587 #define UUF     "%-20s"
4588 #define CSF     "%11s"
4589 #define CDF     "%11llu"
4590 #define HDF     "%8.1f%c"
4591 #define RSF     "%4s"
4592 #define RDF     "%3d%%"
4593
4594 static inline int obd_statfs_ratio(const struct obd_statfs *st)
4595 {
4596         double avail, used, ratio = 0;
4597
4598         avail = st->os_bavail;
4599         used  = st->os_blocks - st->os_bfree;
4600         if (avail + used > 0)
4601                 ratio = used / (used + avail) * 100 + 0.5;
4602
4603         return (int)ratio;
4604 }
4605
4606 static int showdf(char *mntdir, struct obd_statfs *stat,
4607                   char *uuid, enum mntdf_flags flags,
4608                   char *type, int index, int rc)
4609 {
4610         long long avail, used, total;
4611         int ratio = 0;
4612         char *suffix = "KMGTPEZY";
4613         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
4614         char tbuf[3 * sizeof(__u64)];
4615         char ubuf[3 * sizeof(__u64)];
4616         char abuf[3 * sizeof(__u64)];
4617         char rbuf[3 * sizeof(__u64)];
4618
4619         if (!uuid || !stat)
4620                 return -EINVAL;
4621
4622         switch (rc) {
4623         case 0:
4624                 if (flags & MNTDF_INODES) {
4625                         avail = stat->os_ffree;
4626                         used = stat->os_files - stat->os_ffree;
4627                         total = stat->os_files;
4628                 } else {
4629                         int shift = flags & MNTDF_COOKED ? 0 : 10;
4630
4631                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
4632                         used  = ((stat->os_blocks - stat->os_bfree) *
4633                                  stat->os_bsize) >> shift;
4634                         total = (stat->os_blocks * stat->os_bsize) >> shift;
4635                 }
4636
4637                 ratio = obd_statfs_ratio(stat);
4638
4639                 if (flags & MNTDF_COOKED) {
4640                         int i;
4641                         double cook_val;
4642
4643                         cook_val = (double)total;
4644                         i = COOK(cook_val);
4645                         if (i > 0)
4646                                 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
4647                                          suffix[i - 1]);
4648                         else
4649                                 snprintf(tbuf, sizeof(tbuf), CDF, total);
4650
4651                         cook_val = (double)used;
4652                         i = COOK(cook_val);
4653                         if (i > 0)
4654                                 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
4655                                          suffix[i - 1]);
4656                         else
4657                                 snprintf(ubuf, sizeof(ubuf), CDF, used);
4658
4659                         cook_val = (double)avail;
4660                         i = COOK(cook_val);
4661                         if (i > 0)
4662                                 snprintf(abuf, sizeof(abuf), HDF, cook_val,
4663                                          suffix[i - 1]);
4664                         else
4665                                 snprintf(abuf, sizeof(abuf), CDF, avail);
4666                 } else {
4667                         snprintf(tbuf, sizeof(tbuf), CDF, total);
4668                         snprintf(ubuf, sizeof(tbuf), CDF, used);
4669                         snprintf(abuf, sizeof(tbuf), CDF, avail);
4670                 }
4671
4672                 sprintf(rbuf, RDF, ratio);
4673                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
4674                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
4675                 if (type)
4676                         printf("[%s:%d]", type, index);
4677
4678                 if (stat->os_state) {
4679                         /*
4680                          * Each character represents the matching
4681                          * OS_STATE_* bit.
4682                          */
4683                         const char state_names[] = "DRSI";
4684                         __u32      state;
4685                         __u32      i;
4686
4687                         printf(" ");
4688                         for (i = 0, state = stat->os_state;
4689                              state && i < sizeof(state_names); i++) {
4690                                 if (!(state & (1 << i)))
4691                                         continue;
4692                                 printf("%c", state_names[i]);
4693                                 state ^= 1 << i;
4694                         }
4695                 }
4696
4697                 printf("\n");
4698                 break;
4699         case -ENODATA:
4700                 printf(UUF": inactive device\n", uuid);
4701                 break;
4702         default:
4703                 printf(UUF": %s\n", uuid, strerror(-rc));
4704                 break;
4705         }
4706
4707         return 0;
4708 }
4709
4710 struct ll_stat_type {
4711         int   st_op;
4712         char *st_name;
4713 };
4714
4715 #define LL_STATFS_MAX   LOV_MAX_STRIPE_COUNT
4716
4717 struct ll_statfs_data {
4718         int                     sd_index;
4719         struct obd_statfs       sd_st;
4720 };
4721
4722 struct ll_statfs_buf {
4723         int                     sb_count;
4724         struct ll_statfs_data   sb_buf[LL_STATFS_MAX];
4725 };
4726
4727 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
4728                  int ops, struct ll_statfs_buf *lsb)
4729 {
4730         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
4731         struct obd_uuid uuid_buf;
4732         char *poolname = NULL;
4733         struct ll_stat_type types[] = {
4734                 { .st_op = LL_STATFS_LMV,       .st_name = "MDT" },
4735                 { .st_op = LL_STATFS_LOV,       .st_name = "OST" },
4736                 { .st_name = NULL } };
4737         struct ll_stat_type *tp;
4738         __u64 ost_ffree = 0;
4739         __u32 index;
4740         __u32 type;
4741         int fd;
4742         int rc = 0;
4743         int rc2;
4744
4745         if (pool) {
4746                 poolname = strchr(pool, '.');
4747                 if (poolname != NULL) {
4748                         if (strncmp(fsname, pool, strlen(fsname))) {
4749                                 fprintf(stderr, "filesystem name incorrect\n");
4750                                 return -ENODEV;
4751                         }
4752                         poolname++;
4753                 } else
4754                         poolname = pool;
4755         }
4756
4757         fd = open(mntdir, O_RDONLY);
4758         if (fd < 0) {
4759                 rc = -errno;
4760                 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
4761                         strerror(errno));
4762                 return rc;
4763         }
4764
4765         if (flags & MNTDF_SHOW) {
4766                 if (flags & MNTDF_INODES)
4767                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4768                                "UUID", "Inodes", "IUsed", "IFree",
4769                                "IUse%", "Mounted on");
4770                 else
4771                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
4772                                "UUID",
4773                                flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
4774                                "Used", "Available", "Use%", "Mounted on");
4775         }
4776
4777         for (tp = types; tp->st_name != NULL; tp++) {
4778                 if (!(tp->st_op & ops))
4779                         continue;
4780
4781                 for (index = 0; ; index++) {
4782                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
4783                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
4784                         type = flags & MNTDF_LAZY ?
4785                                 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
4786                         rc2 = llapi_obd_fstatfs(fd, type, index,
4787                                                &stat_buf, &uuid_buf);
4788                         if (rc2 == -ENODEV)
4789                                 break;
4790                         if (rc2 == -EAGAIN)
4791                                 continue;
4792                         if (rc2 == -ENODATA) { /* Inactive device, OK. */
4793                                 if (!(flags & MNTDF_VERBOSE))
4794                                         continue;
4795                         } else if (rc2 < 0 && rc == 0) {
4796                                 rc = rc2;
4797                         }
4798
4799                         if (poolname && tp->st_op == LL_STATFS_LOV &&
4800                             llapi_search_ost(fsname, poolname,
4801                                              obd_uuid2str(&uuid_buf)) != 1)
4802                                 continue;
4803
4804                         /* the llapi_obd_statfs() call may have returned with
4805                          * an error, but if it filled in uuid_buf we will at
4806                          * lease use that to print out a message for that OBD.
4807                          * If we didn't get anything in the uuid_buf, then fill
4808                          * it in so that we can print an error message. */
4809                         if (uuid_buf.uuid[0] == '\0')
4810                                 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
4811                                          "%s%04x", tp->st_name, index);
4812                         if (!rc && lsb) {
4813                                 lsb->sb_buf[lsb->sb_count].sd_index = index;
4814                                 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
4815                                 lsb->sb_count++;
4816                         }
4817                         if (flags & MNTDF_SHOW)
4818                                 showdf(mntdir, &stat_buf,
4819                                        obd_uuid2str(&uuid_buf), flags,
4820                                        tp->st_name, index, rc2);
4821
4822                         if (rc2 == 0) {
4823                                 if (tp->st_op == LL_STATFS_LMV) {
4824                                         sum.os_ffree += stat_buf.os_ffree;
4825                                         sum.os_files += stat_buf.os_files;
4826                                 } else /* if (tp->st_op == LL_STATFS_LOV) */ {
4827                                         sum.os_blocks += stat_buf.os_blocks *
4828                                                 stat_buf.os_bsize;
4829                                         sum.os_bfree  += stat_buf.os_bfree *
4830                                                 stat_buf.os_bsize;
4831                                         sum.os_bavail += stat_buf.os_bavail *
4832                                                 stat_buf.os_bsize;
4833                                         ost_ffree += stat_buf.os_ffree;
4834                                 }
4835                         }
4836                 }
4837         }
4838
4839         close(fd);
4840
4841         /* If we don't have as many objects free on the OST as inodes
4842          * on the MDS, we reduce the total number of inodes to
4843          * compensate, so that the "inodes in use" number is correct.
4844          * Matches ll_statfs_internal() so the results are consistent. */
4845         if (ost_ffree < sum.os_ffree) {
4846                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
4847                 sum.os_ffree = ost_ffree;
4848         }
4849         if (flags & MNTDF_SHOW) {
4850                 printf("\n");
4851                 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
4852                 printf("\n");
4853         }
4854
4855         return rc;
4856 }
4857
4858 static int ll_statfs_data_comp(const void *sd1, const void *sd2)
4859 {
4860         const struct obd_statfs *st1 = &((const struct ll_statfs_data *)sd1)->
4861                                                 sd_st;
4862         const struct obd_statfs *st2 = &((const struct ll_statfs_data *)sd2)->
4863                                                 sd_st;
4864         int r1 = obd_statfs_ratio(st1);
4865         int r2 = obd_statfs_ratio(st2);
4866         int64_t result = r1 - r2;
4867
4868         /* if both space usage are above 90, compare free inodes */
4869         if (r1 > 90 && r2 > 90)
4870                 result = st2->os_ffree - st1->os_ffree;
4871
4872         if (result < 0)
4873                 return -1;
4874         else if (result == 0)
4875                 return 0;
4876         else
4877                 return 1;
4878 }
4879
4880 /* functions */
4881 static int lfs_setdirstripe(int argc, char **argv)
4882 {
4883         char                    *dname;
4884         int                     result;
4885         struct lfs_setstripe_args        lsa = { 0 };
4886         struct llapi_stripe_param       *param = NULL;
4887         __u32                   mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
4888         char                    *end;
4889         int                     c;
4890         char                    *mode_opt = NULL;
4891         bool                    default_stripe = false;
4892         mode_t                  mode = S_IRWXU | S_IRWXG | S_IRWXO;
4893         mode_t                  previous_mode = 0;
4894         bool                    delete = false;
4895         struct ll_statfs_buf    *lsb = NULL;
4896         char                    mntdir[PATH_MAX] = "";
4897         bool                    auto_distributed = false;
4898
4899         struct option long_opts[] = {
4900         { .val = 'c',   .name = "count",        .has_arg = required_argument },
4901         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument },
4902         { .val = 'd',   .name = "delete",       .has_arg = no_argument },
4903         { .val = 'D',   .name = "default",      .has_arg = no_argument },
4904         { .val = 'D',   .name = "default_stripe", .has_arg = no_argument },
4905         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
4906         { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
4907 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4908         { .val = 'i',   .name = "index",        .has_arg = required_argument },
4909 #endif
4910         { .val = 'o',   .name = "mode",         .has_arg = required_argument },
4911 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4912         { .val = 't',   .name = "hash-type",    .has_arg = required_argument },
4913 #endif
4914         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
4915 /* setstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
4916         { .name = NULL } };
4917
4918         setstripe_args_init(&lsa);
4919
4920         while ((c = getopt_long(argc, argv, "c:dDi:H:m:o:t:T:", long_opts,
4921                                 NULL)) >= 0) {
4922                 switch (c) {
4923                 case 0:
4924                         /* Long options. */
4925                         break;
4926                 case 'c':
4927                 case 'T':
4928                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
4929                         if (*end != '\0') {
4930                                 fprintf(stderr,
4931                                         "%s %s: invalid stripe count '%s'\n",
4932                                         progname, argv[0], optarg);
4933                                 return CMD_HELP;
4934                         }
4935                         break;
4936                 case 'd':
4937                         delete = true;
4938                         default_stripe = true;
4939                         break;
4940                 case 'D':
4941                         default_stripe = true;
4942                         break;
4943 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4944                 case 't':
4945                         fprintf(stderr, "warning: '--hash-type' and '-t' "
4946                               "deprecated, use '--mdt-hash' or '-H' instead\n");
4947 #endif
4948                 case 'H':
4949                         lsa.lsa_pattern = check_hashtype(optarg);
4950                         if (lsa.lsa_pattern == 0) {
4951                                 fprintf(stderr,
4952                                         "%s %s: bad stripe hash type '%s'\n",
4953                                         progname, argv[0], optarg);
4954                                 return CMD_HELP;
4955                         }
4956                         break;
4957                 case 'i':
4958 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4959                         if (strcmp(argv[optind - 1], "--index") == 0)
4960                                 fprintf(stderr,
4961                                         "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
4962                                         progname, argv[0]);
4963 #endif
4964                         lsa.lsa_nr_tgts = parse_targets(mdts,
4965                                                 sizeof(mdts) / sizeof(__u32),
4966                                                 lsa.lsa_nr_tgts, optarg);
4967                         if (lsa.lsa_nr_tgts < 0) {
4968                                 fprintf(stderr,
4969                                         "%s %s: invalid MDT target(s) '%s'\n",
4970                                         progname, argv[0], optarg);
4971                                 return CMD_HELP;
4972                         }
4973
4974                         lsa.lsa_tgts = mdts;
4975                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4976                                 lsa.lsa_stripe_off = mdts[0];
4977                         break;
4978 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 16, 53, 0)
4979                 case 'm':
4980                         fprintf(stderr, "warning: '-m' is deprecated, "
4981                                 "use '--mode' or '-o' instead\n");
4982 #endif
4983                 case 'o':
4984                         mode_opt = optarg;
4985                         break;
4986                 default:
4987                         fprintf(stderr, "%s %s: unrecognized option '%s'\n",
4988                                 progname, argv[0], argv[optind - 1]);
4989                         return CMD_HELP;
4990                 }
4991         }
4992
4993         if (optind == argc) {
4994                 fprintf(stderr, "%s %s: DIR must be specified\n",
4995                         progname, argv[0]);
4996                 return CMD_HELP;
4997         }
4998
4999         if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
5000             lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT) {
5001                 fprintf(stderr,
5002                         "%s %s: stripe offset and count must be specified\n",
5003                         progname, argv[0]);
5004                 return CMD_HELP;
5005         }
5006
5007         if (delete &&
5008             (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
5009              lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
5010                 fprintf(stderr,
5011                         "%s %s: cannot specify -d with -c or -i options\n",
5012                         progname, argv[0]);
5013                 return CMD_HELP;
5014         }
5015
5016         if (mode_opt != NULL) {
5017                 mode = strtoul(mode_opt, &end, 8);
5018                 if (*end != '\0') {
5019                         fprintf(stderr,
5020                                 "%s %s: bad MODE '%s'\n",
5021                                 progname, argv[0], mode_opt);
5022                         return CMD_HELP;
5023                 }
5024                 previous_mode = umask(0);
5025         }
5026
5027         /*
5028          * initialize stripe parameters, in case param is converted to specific,
5029          * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
5030          */
5031         param = calloc(1, offsetof(typeof(*param),
5032                        lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
5033                                 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
5034         if (param == NULL) {
5035                 fprintf(stderr,
5036                         "%s %s: cannot allocate memory for parameters: %s\n",
5037                         progname, argv[0], strerror(ENOMEM));
5038                 return CMD_HELP;
5039         }
5040
5041         if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
5042                 param->lsp_stripe_count = lsa.lsa_stripe_count;
5043         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
5044                 param->lsp_stripe_offset = -1;
5045         else
5046                 param->lsp_stripe_offset = lsa.lsa_stripe_off;
5047         if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
5048                 param->lsp_stripe_pattern = lsa.lsa_pattern;
5049         else
5050                 param->lsp_stripe_pattern = LMV_HASH_TYPE_FNV_1A_64;
5051         param->lsp_pool = lsa.lsa_pool_name;
5052         param->lsp_is_specific = false;
5053         if (lsa.lsa_nr_tgts > 1) {
5054                 if (lsa.lsa_stripe_count > 0 &&
5055                     lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
5056                     lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
5057                         fprintf(stderr, "error: %s: stripe count %lld doesn't "
5058                                 "match the number of MDTs: %d\n",
5059                                 argv[0], lsa.lsa_stripe_count, lsa.lsa_nr_tgts);
5060                         free(param);
5061                         return CMD_HELP;
5062                 }
5063
5064                 param->lsp_is_specific = true;
5065                 param->lsp_stripe_count = lsa.lsa_nr_tgts;
5066                 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
5067         }
5068
5069         dname = argv[optind];
5070         do {
5071                 if (default_stripe) {
5072                         result = llapi_dir_set_default_lmv(dname, param);
5073                 } else {
5074                         /* if current \a dname isn't under the same \a mntdir
5075                          * as the last one, and the last one was
5076                          * auto-distributed, restore \a param.
5077                          */
5078                         if (mntdir[0] != '\0' &&
5079                             strncmp(dname, mntdir, strlen(mntdir)) &&
5080                             auto_distributed) {
5081                                 param->lsp_is_specific = false;
5082                                 param->lsp_stripe_offset = -1;
5083                                 auto_distributed = false;
5084                         }
5085
5086                         if (!param->lsp_is_specific &&
5087                             param->lsp_stripe_offset == -1) {
5088                                 char path[PATH_MAX] = "";
5089
5090                                 if (!lsb) {
5091                                         lsb = malloc(sizeof(*lsb));
5092                                         if (!lsb) {
5093                                                 result = -ENOMEM;
5094                                                 break;
5095                                         }
5096                                 }
5097                                 lsb->sb_count = 0;
5098
5099                                 /* use mntdir for dirname() temporarily */
5100                                 strncpy(mntdir, dname, sizeof(mntdir));
5101                                 if (!realpath(dirname(mntdir), path)) {
5102                                         result = -errno;
5103                                         fprintf(stderr,
5104                                                 "error: invalid path '%s': %s\n",
5105                                                 argv[optind], strerror(errno));
5106                                         break;
5107                                 }
5108                                 mntdir[0] = '\0';
5109
5110                                 result = llapi_search_mounts(path, 0, mntdir,
5111                                                              NULL);
5112                                 if (result < 0 || mntdir[0] == '\0') {
5113                                         fprintf(stderr,
5114                                                 "No suitable Lustre mount found\n");
5115                                         break;
5116                                 }
5117
5118                                 result = mntdf(mntdir, NULL, NULL, 0,
5119                                                LL_STATFS_LMV, lsb);
5120                                 if (result < 0)
5121                                         break;
5122
5123                                 if (param->lsp_stripe_count > lsb->sb_count) {
5124                                         fprintf(stderr,
5125                                                 "error: stripe count %d is too big\n",
5126                                                 param->lsp_stripe_count);
5127                                         result = -ERANGE;
5128                                         break;
5129                                 }
5130
5131                                 qsort(lsb->sb_buf, lsb->sb_count,
5132                                       sizeof(struct ll_statfs_data),
5133                                       ll_statfs_data_comp);
5134
5135                                 auto_distributed = true;
5136                         }
5137
5138                         if (auto_distributed) {
5139                                 int r;
5140                                 int nr = MAX(param->lsp_stripe_count,
5141                                              lsb->sb_count / 2);
5142
5143                                 /* don't use server whose usage is above 90% */
5144                                 while (nr != param->lsp_stripe_count &&
5145                                        obd_statfs_ratio(&lsb->sb_buf[nr].sd_st)
5146                                        > 90)
5147                                         nr = MAX(param->lsp_stripe_count,
5148                                                  nr / 2);
5149
5150                                 /* get \a r between [0, nr) */
5151                                 r = rand() % nr;
5152
5153                                 param->lsp_stripe_offset =
5154                                         lsb->sb_buf[r].sd_index;
5155                                 if (param->lsp_stripe_count > 1) {
5156                                         int i = 0;
5157
5158                                         param->lsp_is_specific = true;
5159                                         for (; i < param->lsp_stripe_count; i++)
5160                                                 param->lsp_tgts[(i + r) % nr] =
5161                                                         lsb->sb_buf[i].sd_index;
5162                                 }
5163                         }
5164
5165                         result = llapi_dir_create(dname, mode, param);
5166                 }
5167
5168                 if (result) {
5169                         fprintf(stderr,
5170                                 "%s setdirstripe: cannot create stripe dir '%s': %s\n",
5171                                 progname, dname, strerror(-result));
5172                         break;
5173                 }
5174                 dname = argv[++optind];
5175         } while (dname != NULL);
5176
5177         if (mode_opt != NULL)
5178                 umask(previous_mode);
5179
5180         free(lsb);
5181         free(param);
5182         return result;
5183 }
5184
5185 /* functions */
5186 static int lfs_rmentry(int argc, char **argv)
5187 {
5188         char *dname;
5189         int   index;
5190         int   result = 0;
5191
5192         if (argc <= 1) {
5193                 fprintf(stderr, "error: %s: missing dirname\n",
5194                         argv[0]);
5195                 return CMD_HELP;
5196         }
5197
5198         index = 1;
5199         dname = argv[index];
5200         while (dname != NULL) {
5201                 result = llapi_direntry_remove(dname);
5202                 if (result) {
5203                         fprintf(stderr, "error: %s: remove dir entry '%s' "
5204                                 "failed\n", argv[0], dname);
5205                         break;
5206                 }
5207                 dname = argv[++index];
5208         }
5209         return result;
5210 }
5211
5212 static int lfs_mv(int argc, char **argv)
5213 {
5214         struct lmv_user_md lmu = { LMV_USER_MAGIC };
5215         struct find_param param = {
5216                 .fp_max_depth = -1,
5217                 .fp_mdt_index = -1,
5218         };
5219         char *end;
5220         int c;
5221         int rc = 0;
5222         struct option long_opts[] = {
5223         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
5224         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
5225         { .name = NULL } };
5226
5227         while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
5228                 switch (c) {
5229 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
5230                 case 'M':
5231                         fprintf(stderr, "warning: '-M' deprecated"
5232                                 ", use '--mdt-index' or '-m' instead\n");
5233 #endif
5234                 case 'm':
5235                         lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
5236                         if (*end != '\0') {
5237                                 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
5238                                         progname, optarg);
5239                                 return CMD_HELP;
5240                         }
5241                         break;
5242                 case 'v':
5243                         param.fp_verbose = VERBOSE_DETAIL;
5244                         break;
5245                 default:
5246                         fprintf(stderr, "%s mv: unrecognized option '%s'\n",
5247                                 progname, argv[optind - 1]);
5248                         return CMD_HELP;
5249                 }
5250         }
5251
5252         if (lmu.lum_stripe_offset == -1) {
5253                 fprintf(stderr, "%s mv: MDT index must be specified\n",
5254                         progname);
5255                 return CMD_HELP;
5256         }
5257
5258         if (optind >= argc) {
5259                 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
5260                 return CMD_HELP;
5261         }
5262
5263
5264         /* initialize migrate mdt parameters */
5265         param.fp_lmv_md = &lmu;
5266         param.fp_migrate = 1;
5267         rc = llapi_migrate_mdt(argv[optind], &param);
5268         if (rc != 0)
5269                 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
5270                         progname, argv[optind], param.fp_mdt_index,
5271                         strerror(-rc));
5272         return rc;
5273 }
5274
5275 static int lfs_osts(int argc, char **argv)
5276 {
5277         return lfs_tgts(argc, argv);
5278 }
5279
5280 static int lfs_mdts(int argc, char **argv)
5281 {
5282         return lfs_tgts(argc, argv);
5283 }
5284
5285 static int lfs_df(int argc, char **argv)
5286 {
5287         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
5288         enum mntdf_flags flags = MNTDF_SHOW;
5289         int ops = LL_STATFS_LMV | LL_STATFS_LOV;
5290         int c, rc = 0, index = 0;
5291         char fsname[PATH_MAX] = "", *pool_name = NULL;
5292         struct option long_opts[] = {
5293         { .val = 'h',   .name = "human-readable",
5294                                                 .has_arg = no_argument },
5295         { .val = 'i',   .name = "inodes",       .has_arg = no_argument },
5296         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
5297         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
5298         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
5299         { .name = NULL} };
5300
5301         while ((c = getopt_long(argc, argv, "hilp:v", long_opts, NULL)) != -1) {
5302                 switch (c) {
5303                 case 'h':
5304                         flags |= MNTDF_COOKED;
5305                         break;
5306                 case 'i':
5307                         flags |= MNTDF_INODES;
5308                         break;
5309                 case 'l':
5310                         flags |= MNTDF_LAZY;
5311                         break;
5312                 case 'p':
5313                         pool_name = optarg;
5314                         break;
5315                 case 'v':
5316                         flags |= MNTDF_VERBOSE;
5317                         break;
5318                 default:
5319                         return CMD_HELP;
5320                 }
5321         }
5322         if (optind < argc && !realpath(argv[optind], path)) {
5323                 rc = -errno;
5324                 fprintf(stderr, "error: invalid path '%s': %s\n",
5325                         argv[optind], strerror(-rc));
5326                 return rc;
5327         }
5328
5329         while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5330                 /* Check if we have a mount point */
5331                 if (mntdir[0] == '\0')
5332                         continue;
5333
5334                 rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
5335                 if (rc || path[0] != '\0')
5336                         break;
5337                 fsname[0] = '\0'; /* avoid matching in next loop */
5338                 mntdir[0] = '\0'; /* avoid matching in next loop */
5339         }
5340
5341         return rc;
5342 }
5343
5344 static int lfs_getname(int argc, char **argv)
5345 {
5346         char mntdir[PATH_MAX] = "", path[PATH_MAX] = "", fsname[PATH_MAX] = "";
5347         int rc = 0, index = 0, c;
5348         char buf[sizeof(struct obd_uuid)];
5349
5350         while ((c = getopt(argc, argv, "h")) != -1)
5351                 return CMD_HELP;
5352
5353         if (optind == argc) { /* no paths specified, get all paths. */
5354                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
5355                         rc = llapi_getname(mntdir, buf, sizeof(buf));
5356                         if (rc < 0) {
5357                                 fprintf(stderr,
5358                                         "cannot get name for `%s': %s\n",
5359                                         mntdir, strerror(-rc));
5360                                 break;
5361                         }
5362
5363                         printf("%s %s\n", buf, mntdir);
5364
5365                         path[0] = fsname[0] = mntdir[0] = 0;
5366                 }
5367         } else { /* paths specified, only attempt to search these. */
5368                 for (; optind < argc; optind++) {
5369                         rc = llapi_getname(argv[optind], buf, sizeof(buf));
5370                         if (rc < 0) {
5371                                 fprintf(stderr,
5372                                         "cannot get name for `%s': %s\n",
5373                                         argv[optind], strerror(-rc));
5374                                 break;
5375                         }
5376
5377                         printf("%s %s\n", buf, argv[optind]);
5378                 }
5379         }
5380         return rc;
5381 }
5382
5383 static int lfs_check(int argc, char **argv)
5384 {
5385         int rc;
5386         char mntdir[PATH_MAX] = {'\0'};
5387         int num_types = 1;
5388         char *obd_types[2];
5389         char obd_type1[4];
5390         char obd_type2[4];
5391
5392         if (argc != 2) {
5393                 fprintf(stderr, "%s check: server type must be specified\n",
5394                         progname);
5395                 return CMD_HELP;
5396         }
5397
5398         obd_types[0] = obd_type1;
5399         obd_types[1] = obd_type2;
5400
5401         if (strcmp(argv[1], "osts") == 0) {
5402                 strcpy(obd_types[0], "osc");
5403         } else if (strcmp(argv[1], "mds") == 0) {
5404                 strcpy(obd_types[0], "mdc");
5405         } else if (strcmp(argv[1], "servers") == 0) {
5406                 num_types = 2;
5407                 strcpy(obd_types[0], "osc");
5408                 strcpy(obd_types[1], "mdc");
5409         } else {
5410                 fprintf(stderr, "%s check: unrecognized option '%s'\n",
5411                         progname, argv[1]);
5412                 return CMD_HELP;
5413         }
5414
5415         rc = llapi_search_mounts(NULL, 0, mntdir, NULL);
5416         if (rc < 0 || mntdir[0] == '\0') {
5417                 fprintf(stderr,
5418                         "%s check: cannot find mounted Lustre filesystem: %s\n",
5419                         progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
5420                 return rc;
5421         }
5422
5423         rc = llapi_target_check(num_types, obd_types, mntdir);
5424         if (rc)
5425                 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
5426                         progname, argv[1], strerror(-rc));
5427
5428         return rc;
5429
5430 }
5431
5432 #ifdef HAVE_SYS_QUOTA_H
5433 #define ARG2INT(nr, str, msg)                                           \
5434 do {                                                                    \
5435         char *endp;                                                     \
5436         nr = strtol(str, &endp, 0);                                     \
5437         if (*endp != '\0') {                                            \
5438                 fprintf(stderr, "%s: bad %s '%s'\n",                    \
5439                         progname, msg, str);                            \
5440                 return CMD_HELP;                                        \
5441         }                                                               \
5442 } while (0)
5443
5444 #define ADD_OVERFLOW(a,b) ((a + b) < a) ? (a = ULONG_MAX) : (a = a + b)
5445
5446 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
5447  * returns the value or ULONG_MAX on integer overflow or incorrect format
5448  * Notes:
5449  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
5450  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
5451  *        3. empty integer value is interpreted as 0
5452  */
5453 static unsigned long str2sec(const char* timestr)
5454 {
5455         const char spec[] = "smhdw";
5456         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5457         unsigned long val = 0;
5458         char *tail;
5459
5460         if (strpbrk(timestr, spec) == NULL) {
5461                 /* no specifiers inside the time string,
5462                    should treat it as an integer value */
5463                 val = strtoul(timestr, &tail, 10);
5464                 return *tail ? ULONG_MAX : val;
5465         }
5466
5467         /* format string is XXwXXdXXhXXmXXs */
5468         while (*timestr) {
5469                 unsigned long v;
5470                 int ind;
5471                 char* ptr;
5472
5473                 v = strtoul(timestr, &tail, 10);
5474                 if (v == ULONG_MAX || *tail == '\0')
5475                         /* value too large (ULONG_MAX or more)
5476                            or missing specifier */
5477                         goto error;
5478
5479                 ptr = strchr(spec, *tail);
5480                 if (ptr == NULL)
5481                         /* unknown specifier */
5482                         goto error;
5483
5484                 ind = ptr - spec;
5485
5486                 /* check if product will overflow the type */
5487                 if (!(v < ULONG_MAX / mult[ind]))
5488                         goto error;
5489
5490                 ADD_OVERFLOW(val, mult[ind] * v);
5491                 if (val == ULONG_MAX)
5492                         goto error;
5493
5494                 timestr = tail + 1;
5495         }
5496
5497         return val;
5498
5499 error:
5500         return ULONG_MAX;
5501 }
5502
5503 #define ARG2ULL(nr, str, def_units)                                     \
5504 do {                                                                    \
5505         unsigned long long limit, units = def_units;                    \
5506         int rc;                                                         \
5507                                                                         \
5508         rc = llapi_parse_size(str, &limit, &units, 1);                  \
5509         if (rc < 0) {                                                   \
5510                 fprintf(stderr, "%s: invalid limit '%s'\n",             \
5511                         progname, str);                                 \
5512                 return CMD_HELP;                                        \
5513         }                                                               \
5514         nr = limit;                                                     \
5515 } while (0)
5516
5517 static inline int has_times_option(int argc, char **argv)
5518 {
5519         int i;
5520
5521         for (i = 1; i < argc; i++)
5522                 if (!strcmp(argv[i], "-t"))
5523                         return 1;
5524
5525         return 0;
5526 }
5527
5528 int lfs_setquota_times(int argc, char **argv)
5529 {
5530         int c, rc;
5531         struct if_quotactl qctl;
5532         char *mnt, *obd_type = (char *)qctl.obd_type;
5533         struct obd_dqblk *dqb = &qctl.qc_dqblk;
5534         struct obd_dqinfo *dqi = &qctl.qc_dqinfo;
5535         struct option long_opts[] = {
5536         { .val = 'b',   .name = "block-grace",  .has_arg = required_argument },
5537         { .val = 'g',   .name = "group",        .has_arg = no_argument },
5538         { .val = 'i',   .name = "inode-grace",  .has_arg = required_argument },
5539         { .val = 'p',   .name = "projid",       .has_arg = no_argument },
5540         { .val = 't',   .name = "times",        .has_arg = no_argument },
5541         { .val = 'u',   .name = "user",         .has_arg = no_argument },
5542         { .name = NULL } };
5543         int qtype;
5544
5545         memset(&qctl, 0, sizeof(qctl));
5546         qctl.qc_cmd  = LUSTRE_Q_SETINFO;
5547         qctl.qc_type = ALLQUOTA;
5548
5549         while ((c = getopt_long(argc, argv, "b:gi:ptu",
5550                                 long_opts, NULL)) != -1) {
5551                 switch (c) {
5552                 case 'u':
5553                         qtype = USRQUOTA;
5554                         goto quota_type;
5555                 case 'g':
5556                         qtype = GRPQUOTA;
5557                         goto quota_type;
5558                 case 'p':
5559                         qtype = PRJQUOTA;
5560 quota_type:
5561                         if (qctl.qc_type != ALLQUOTA) {
5562                                 fprintf(stderr, "error: -u/g/p can't be used "
5563                                                 "more than once\n");
5564                                 return CMD_HELP;
5565                         }
5566                         qctl.qc_type = qtype;
5567                         break;
5568                 case 'b':
5569                         if ((dqi->dqi_bgrace = str2sec(optarg)) == ULONG_MAX) {
5570                                 fprintf(stderr, "error: bad block-grace: %s\n",
5571                                         optarg);
5572                                 return CMD_HELP;
5573                         }
5574                         dqb->dqb_valid |= QIF_BTIME;
5575                         break;
5576                 case 'i':
5577                         if ((dqi->dqi_igrace = str2sec(optarg)) == ULONG_MAX) {
5578                                 fprintf(stderr, "error: bad inode-grace: %s\n",
5579                                         optarg);
5580                                 return CMD_HELP;
5581                         }
5582                         dqb->dqb_valid |= QIF_ITIME;
5583                         break;
5584                 case 't': /* Yes, of course! */
5585                         break;
5586                 default: /* getopt prints error message for us when opterr != 0 */
5587                         return CMD_HELP;
5588                 }
5589         }
5590
5591         if (qctl.qc_type == ALLQUOTA) {
5592                 fprintf(stderr, "error: neither -u, -g nor -p specified\n");
5593                 return CMD_HELP;
5594         }
5595
5596         if (optind != argc - 1) {
5597                 fprintf(stderr, "error: unexpected parameters encountered\n");
5598                 return CMD_HELP;
5599         }
5600
5601         if ((dqb->dqb_valid | QIF_BTIME && dqi->dqi_bgrace >= UINT_MAX) ||
5602             (dqb->dqb_valid | QIF_ITIME && dqi->dqi_igrace >= UINT_MAX)) {
5603                 fprintf(stderr, "error: grace time is too large\n");
5604                 return CMD_HELP;
5605         }
5606
5607         mnt = argv[optind];
5608         rc = llapi_quotactl(mnt, &qctl);
5609         if (rc) {
5610                 if (*obd_type)
5611                         fprintf(stderr, "%s %s ", obd_type,
5612                                 obd_uuid2str(&qctl.obd_uuid));
5613                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
5614                 return rc;
5615         }
5616
5617         return 0;
5618 }
5619
5620 #define BSLIMIT (1 << 0)
5621 #define BHLIMIT (1 << 1)
5622 #define ISLIMIT (1 << 2)
5623 #define IHLIMIT (1 << 3)
5624
5625 int lfs_setquota(int argc, char **argv)
5626 {
5627         int c, rc = 0;
5628         struct if_quotactl qctl;
5629         char *mnt, *obd_type = (char *)qctl.obd_type;
5630         struct obd_dqblk *dqb = &qctl.qc_dqblk;
5631         struct option long_opts[] = {
5632         { .val = 'b',   .name = "block-softlimit",
5633                                                 .has_arg = required_argument },
5634         { .val = 'B',   .name = "block-hardlimit",
5635                                                 .has_arg = required_argument },
5636         { .val = 'd',   .name = "default",      .has_arg = no_argument },
5637         { .val = 'g',   .name = "group",        .has_arg = required_argument },
5638         { .val = 'G',   .name = "default-grp",  .has_arg = no_argument },
5639         { .val = 'i',   .name = "inode-softlimit",
5640                                                 .has_arg = required_argument },
5641         { .val = 'I',   .name = "inode-hardlimit",
5642                                                 .has_arg = required_argument },
5643         { .val = 'p',   .name = "projid",       .has_arg = required_argument },
5644         { .val = 'P',   .name = "default-prj",  .has_arg = no_argument },
5645         { .val = 'u',   .name = "user",         .has_arg = required_argument },
5646         { .val = 'U',   .name = "default-usr",  .has_arg = no_argument },
5647         { .name = NULL } };
5648         unsigned limit_mask = 0;
5649         char *endptr;
5650         bool use_default = false;
5651         int qtype;
5652
5653         if (has_times_option(argc, argv))
5654                 return lfs_setquota_times(argc, argv);
5655
5656         memset(&qctl, 0, sizeof(qctl));
5657         qctl.qc_cmd  = LUSTRE_Q_SETQUOTA;
5658         qctl.qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
5659                                   * so it can be used as a marker that qc_type
5660                                   * isn't reinitialized from command line */
5661
5662         while ((c = getopt_long(argc, argv, "b:B:dg:Gi:I:p:Pu:U",
5663                 long_opts, NULL)) != -1) {
5664                 switch (c) {
5665                 case 'U':
5666                         qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5667                         qtype = USRQUOTA;
5668                         qctl.qc_id = 0;
5669                         goto quota_type_def;
5670                 case 'u':
5671                         qtype = USRQUOTA;
5672                         rc = name2uid(&qctl.qc_id, optarg);
5673                         goto quota_type;
5674                 case 'G':
5675                         qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5676                         qtype = GRPQUOTA;
5677                         qctl.qc_id = 0;
5678                         goto quota_type_def;
5679                 case 'g':
5680                         qtype = GRPQUOTA;
5681                         rc = name2gid(&qctl.qc_id, optarg);
5682                         goto quota_type;
5683                 case 'P':
5684                         qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5685                         qtype = PRJQUOTA;
5686                         qctl.qc_id = 0;
5687                         goto quota_type_def;
5688                 case 'p':
5689                         qtype = PRJQUOTA;
5690                         rc = name2projid(&qctl.qc_id, optarg);
5691 quota_type:
5692                         if (rc) {
5693                                 qctl.qc_id = strtoul(optarg, &endptr, 10);
5694                                 if (*endptr != '\0') {
5695                                         fprintf(stderr, "%s setquota: invalid"
5696                                                 " id '%s'\n", progname, optarg);
5697                                         return -1;
5698                                 }
5699                         }
5700
5701                         if (qctl.qc_id == 0) {
5702                                 fprintf(stderr, "%s setquota: can't set quota"
5703                                         " for root usr/group/project.\n",
5704                                         progname);
5705                                 return -1;
5706                         }
5707
5708 quota_type_def:
5709                         if (qctl.qc_type != ALLQUOTA) {
5710                                 fprintf(stderr,
5711                                         "%s setquota: only one of -u, -U, -g,"
5712                                         " -G, -p or -P may be specified\n",
5713                                         progname);
5714                                 return CMD_HELP;
5715                         }
5716                         qctl.qc_type = qtype;
5717                         break;
5718                 case 'd':
5719                         qctl.qc_cmd = LUSTRE_Q_SETDEFAULT;
5720                         use_default = true;
5721                         break;
5722                 case 'b':
5723                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
5724                         dqb->dqb_bsoftlimit >>= 10;
5725                         limit_mask |= BSLIMIT;
5726                         if (dqb->dqb_bsoftlimit &&
5727                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
5728                                 fprintf(stderr,
5729                                         "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\n"
5730                                         "See '%s help setquota' or Lustre manual for details\n",
5731                                         progname, dqb->dqb_bsoftlimit,
5732                                         progname);
5733                         break;
5734                 case 'B':
5735                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
5736                         dqb->dqb_bhardlimit >>= 10;
5737                         limit_mask |= BHLIMIT;
5738                         if (dqb->dqb_bhardlimit &&
5739                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
5740                                 fprintf(stderr,
5741                                         "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
5742                                         "See '%s help setquota' or Lustre manual for details\n",
5743                                         progname, dqb->dqb_bhardlimit,
5744                                         progname);
5745                         break;
5746                 case 'i':
5747                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
5748                         limit_mask |= ISLIMIT;
5749                         if (dqb->dqb_isoftlimit &&
5750                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
5751                                 fprintf(stderr,
5752                                         "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\n"
5753                                         "See '%s help setquota' or Lustre manual for details\n",
5754                                         progname, dqb->dqb_isoftlimit,
5755                                         progname);
5756                         break;
5757                 case 'I':
5758                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
5759                         limit_mask |= IHLIMIT;
5760                         if (dqb->dqb_ihardlimit &&
5761                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
5762                                 fprintf(stderr,
5763                                         "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\n"
5764                                         "See '%s help setquota' or Lustre manual for details\n",
5765                                         progname, dqb->dqb_ihardlimit,
5766                                         progname);
5767                         break;
5768                 default:
5769                         fprintf(stderr,
5770                                 "%s setquota: unrecognized option '%s'\n",
5771                                 progname, argv[optind - 1]);
5772                         return CMD_HELP;
5773                 }
5774         }
5775
5776         if (qctl.qc_type == ALLQUOTA) {
5777                 fprintf(stderr,
5778                         "%s setquota: either -u or -g must be specified\n",
5779                         progname);
5780                 return CMD_HELP;
5781         }
5782
5783         if (!use_default && limit_mask == 0) {
5784                 fprintf(stderr,
5785                         "%s setquota: at least one limit must be specified\n",
5786                         progname);
5787                 return CMD_HELP;
5788         }
5789
5790         if (use_default && limit_mask != 0) {
5791                 fprintf(stderr,
5792                         "%s setquota: limits should not be specified when"
5793                         " using default quota\n",
5794                         progname);
5795                 return CMD_HELP;
5796         }
5797
5798         if (use_default && qctl.qc_id == 0) {
5799                 fprintf(stderr,
5800                         "%s setquota: can not set default quota for root"
5801                         " user/group/project\n",
5802                         progname);
5803                 return CMD_HELP;
5804         }
5805
5806         if (optind != argc - 1) {
5807                 fprintf(stderr,
5808                         "%s setquota: filesystem not specified or unexpected argument '%s'\n",
5809                         progname, argv[optind]);
5810                 return CMD_HELP;
5811         }
5812
5813         mnt = argv[optind];
5814
5815         if (use_default) {
5816                 dqb->dqb_bhardlimit = 0;
5817                 dqb->dqb_bsoftlimit = 0;
5818                 dqb->dqb_ihardlimit = 0;
5819                 dqb->dqb_isoftlimit = 0;
5820                 dqb->dqb_itime = 0;
5821                 dqb->dqb_btime = 0;
5822                 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
5823         } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
5824                    (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
5825                 /* sigh, we can't just set blimits/ilimits */
5826                 struct if_quotactl tmp_qctl = {.qc_cmd  = LUSTRE_Q_GETQUOTA,
5827                                                .qc_type = qctl.qc_type,
5828                                                .qc_id   = qctl.qc_id};
5829
5830                 rc = llapi_quotactl(mnt, &tmp_qctl);
5831                 if (rc < 0)
5832                         return rc;
5833
5834                 if (!(limit_mask & BHLIMIT))
5835                         dqb->dqb_bhardlimit = tmp_qctl.qc_dqblk.dqb_bhardlimit;
5836                 if (!(limit_mask & BSLIMIT))
5837                         dqb->dqb_bsoftlimit = tmp_qctl.qc_dqblk.dqb_bsoftlimit;
5838                 if (!(limit_mask & IHLIMIT))
5839                         dqb->dqb_ihardlimit = tmp_qctl.qc_dqblk.dqb_ihardlimit;
5840                 if (!(limit_mask & ISLIMIT))
5841                         dqb->dqb_isoftlimit = tmp_qctl.qc_dqblk.dqb_isoftlimit;
5842
5843                 /* Keep grace times if we have got no softlimit arguments */
5844                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
5845                         dqb->dqb_valid |= QIF_BTIME;
5846                         dqb->dqb_btime = tmp_qctl.qc_dqblk.dqb_btime;
5847                 }
5848
5849                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
5850                         dqb->dqb_valid |= QIF_ITIME;
5851                         dqb->dqb_itime = tmp_qctl.qc_dqblk.dqb_itime;
5852                 }
5853         }
5854
5855         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
5856         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
5857
5858         rc = llapi_quotactl(mnt, &qctl);
5859         if (rc) {
5860                 if (*obd_type)
5861                         fprintf(stderr,
5862                                 "%s setquota: cannot quotactl '%s' '%s': %s",
5863                                 progname, obd_type,
5864                                 obd_uuid2str(&qctl.obd_uuid), strerror(-rc));
5865                 return rc;
5866         }
5867
5868         return 0;
5869 }
5870
5871 /* Converts seconds value into format string
5872  * result is returned in buf
5873  * Notes:
5874  *        1. result is in descenting order: 1w2d3h4m5s
5875  *        2. zero fields are not filled (except for p. 3): 5d1s
5876  *        3. zero seconds value is presented as "0s"
5877  */
5878 static char * __sec2str(time_t seconds, char *buf)
5879 {
5880         const char spec[] = "smhdw";
5881         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
5882         unsigned long c;
5883         char *tail = buf;
5884         int i;
5885
5886         for (i = sizeof(mult) / sizeof(mult[0]) - 1 ; i >= 0; i--) {
5887                 c = seconds / mult[i];
5888
5889                 if (c > 0 || (i == 0 && buf == tail))
5890                         tail += snprintf(tail, 40-(tail-buf), "%lu%c", c, spec[i]);
5891
5892                 seconds %= mult[i];
5893         }
5894
5895         return tail;
5896 }
5897
5898 static void sec2str(time_t seconds, char *buf, int rc)
5899 {
5900         char *tail = buf;
5901
5902         if (rc)
5903                 *tail++ = '[';
5904
5905         tail = __sec2str(seconds, tail);
5906
5907         if (rc && tail - buf < 39) {
5908                 *tail++ = ']';
5909                 *tail++ = 0;
5910         }
5911 }
5912
5913 static void diff2str(time_t seconds, char *buf, time_t now)
5914 {
5915
5916         buf[0] = 0;
5917         if (!seconds)
5918                 return;
5919         if (seconds <= now) {
5920                 strcpy(buf, "none");
5921                 return;
5922         }
5923         __sec2str(seconds - now, buf);
5924 }
5925
5926 static void print_quota_title(char *name, struct if_quotactl *qctl,
5927                               bool human_readable, bool show_default)
5928 {
5929         if (show_default) {
5930                 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
5931                 printf("%15s %8s%8s%8s %8s%8s%8s\n",
5932                        "Filesystem", "bquota", "blimit", "bgrace",
5933                        "iquota", "ilimit", "igrace");
5934         } else {
5935                 printf("Disk quotas for %s %s (%cid %u):\n",
5936                        qtype_name(qctl->qc_type), name,
5937                        *qtype_name(qctl->qc_type), qctl->qc_id);
5938                 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
5939                        "Filesystem", human_readable ? "used" : "kbytes",
5940                        "quota", "limit", "grace",
5941                        "files", "quota", "limit", "grace");
5942         }
5943 }
5944
5945 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
5946 {
5947         if (!h) {
5948                 snprintf(buf, buflen, "%ju", (uintmax_t)num);
5949         } else {
5950                 if (num >> 40)
5951                         snprintf(buf, buflen, "%5.4gP",
5952                                  (double)num / ((__u64)1 << 40));
5953                 else if (num >> 30)
5954                         snprintf(buf, buflen, "%5.4gT",
5955                                  (double)num / (1 << 30));
5956                 else if (num >> 20)
5957                         snprintf(buf, buflen, "%5.4gG",
5958                                  (double)num / (1 << 20));
5959                 else if (num >> 10)
5960                         snprintf(buf, buflen, "%5.4gM",
5961                                  (double)num / (1 << 10));
5962                 else
5963                         snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
5964         }
5965 }
5966
5967 #define STRBUF_LEN      32
5968 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
5969                         int rc, bool h, bool show_default)
5970 {
5971         time_t now;
5972
5973         time(&now);
5974
5975         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
5976             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) {
5977                 int bover = 0, iover = 0;
5978                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
5979                 char numbuf[3][STRBUF_LEN];
5980                 char timebuf[40];
5981                 char strbuf[STRBUF_LEN];
5982
5983                 if (dqb->dqb_bhardlimit &&
5984                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
5985                         bover = 1;
5986                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
5987                         if (dqb->dqb_btime > now) {
5988                                 bover = 2;
5989                         } else {
5990                                 bover = 3;
5991                         }
5992                 }
5993
5994                 if (dqb->dqb_ihardlimit &&
5995                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
5996                         iover = 1;
5997                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
5998                         if (dqb->dqb_itime > now) {
5999                                 iover = 2;
6000                         } else {
6001                                 iover = 3;
6002                         }
6003                 }
6004
6005
6006                 if (strlen(mnt) > 15)
6007                         printf("%s\n%15s", mnt, "");
6008                 else
6009                         printf("%15s", mnt);
6010
6011                 if (bover)
6012                         diff2str(dqb->dqb_btime, timebuf, now);
6013                 else if (show_default)
6014                         snprintf(timebuf, sizeof(timebuf), "%llu",
6015                                  dqb->dqb_btime);
6016
6017                 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
6018                            strbuf, sizeof(strbuf), h);
6019                 if (rc == -EREMOTEIO)
6020                         sprintf(numbuf[0], "%s*", strbuf);
6021                 else
6022                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
6023                                 "%s" : "[%s]", strbuf);
6024
6025                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
6026                 if (type == QC_GENERAL)
6027                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
6028                                 "%s" : "[%s]", strbuf);
6029                 else
6030                         sprintf(numbuf[1], "%s", "-");
6031
6032                 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
6033                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
6034                         "%s" : "[%s]", strbuf);
6035
6036                 if (show_default)
6037                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6038                 else
6039                         printf(" %7s%c %6s %7s %7s",
6040                                numbuf[0], bover ? '*' : ' ', numbuf[1],
6041                                numbuf[2], bover > 1 ? timebuf : "-");
6042
6043
6044                 if (iover)
6045                         diff2str(dqb->dqb_itime, timebuf, now);
6046                 else if (show_default)
6047                         snprintf(timebuf, sizeof(timebuf), "%llu",
6048                                  dqb->dqb_itime);
6049
6050                 snprintf(numbuf[0], sizeof(numbuf),
6051                          (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
6052                          (uintmax_t)dqb->dqb_curinodes);
6053
6054                 if (type == QC_GENERAL)
6055                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
6056                                 "%ju" : "[%ju]",
6057                                 (uintmax_t)dqb->dqb_isoftlimit);
6058                 else
6059                         sprintf(numbuf[1], "%s", "-");
6060
6061                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
6062                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
6063
6064                 if (show_default)
6065                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
6066                 else if (type != QC_OSTIDX)
6067                         printf(" %7s%c %6s %7s %7s",
6068                                numbuf[0], iover ? '*' : ' ', numbuf[1],
6069                                numbuf[2], iover > 1 ? timebuf : "-");
6070                 else
6071                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
6072                 printf("\n");
6073
6074         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO ||
6075                    qctl->qc_cmd == Q_GETOINFO) {
6076                 char bgtimebuf[40];
6077                 char igtimebuf[40];
6078
6079                 sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
6080                 sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
6081                 printf("Block grace time: %s; Inode grace time: %s\n",
6082                        bgtimebuf, igtimebuf);
6083         }
6084 }
6085
6086 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
6087                            bool h, __u64 *total)
6088 {
6089         int rc = 0, rc1 = 0, count = 0;
6090         __u32 valid = qctl->qc_valid;
6091
6092         rc = llapi_get_obd_count(mnt, &count, is_mdt);
6093         if (rc) {
6094                 fprintf(stderr, "can not get %s count: %s\n",
6095                         is_mdt ? "mdt": "ost", strerror(-rc));
6096                 return rc;
6097         }
6098
6099         for (qctl->qc_idx = 0; qctl->qc_idx < count; qctl->qc_idx++) {
6100                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
6101                 rc = llapi_quotactl(mnt, qctl);
6102                 if (rc) {
6103                         /* It is remote client case. */
6104                         if (rc == -EOPNOTSUPP) {
6105                                 rc = 0;
6106                                 goto out;
6107                         }
6108
6109                         if (!rc1)
6110                                 rc1 = rc;
6111                         fprintf(stderr, "quotactl %s%d failed.\n",
6112                                 is_mdt ? "mdt": "ost", qctl->qc_idx);
6113                         continue;
6114                 }
6115
6116                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
6117                             qctl->qc_valid, 0, h, false);
6118                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
6119                                    qctl->qc_dqblk.dqb_bhardlimit;
6120         }
6121 out:
6122         qctl->qc_valid = valid;
6123         return rc ? : rc1;
6124 }
6125
6126 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
6127                            int verbose, int quiet, bool human_readable,
6128                            bool show_default)
6129 {
6130         int rc1 = 0, rc2 = 0, rc3 = 0;
6131         char *obd_type = (char *)qctl->obd_type;
6132         char *obd_uuid = (char *)qctl->obd_uuid.uuid;
6133         __u64 total_ialloc = 0, total_balloc = 0;
6134         bool use_default_for_blk = false;
6135         bool use_default_for_file = false;
6136         int inacc;
6137
6138         rc1 = llapi_quotactl(mnt, qctl);
6139         if (rc1 < 0) {
6140                 switch (rc1) {
6141                 case -ESRCH:
6142                         fprintf(stderr, "%s quotas are not enabled.\n",
6143                                 qtype_name(qctl->qc_type));
6144                         goto out;
6145                 case -EPERM:
6146                         fprintf(stderr, "Permission denied.\n");
6147                 case -ENODEV:
6148                 case -ENOENT:
6149                         /* We already got error message. */
6150                         goto out;
6151                 default:
6152                         fprintf(stderr, "Unexpected quotactl error: %s\n",
6153                                 strerror(-rc1));
6154                 }
6155         }
6156
6157         if (!show_default && qctl->qc_id == 0) {
6158                 qctl->qc_dqblk.dqb_bhardlimit = 0;
6159                 qctl->qc_dqblk.dqb_bsoftlimit = 0;
6160                 qctl->qc_dqblk.dqb_ihardlimit = 0;
6161                 qctl->qc_dqblk.dqb_isoftlimit = 0;
6162                 qctl->qc_dqblk.dqb_btime = 0;
6163                 qctl->qc_dqblk.dqb_itime = 0;
6164                 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
6165         }
6166
6167         if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
6168             LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
6169                 use_default_for_blk = true;
6170                 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
6171         }
6172
6173         if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
6174             LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
6175                 use_default_for_file = true;
6176                 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
6177         }
6178
6179         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
6180              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
6181                 print_quota_title(name, qctl, human_readable, show_default);
6182
6183         if (rc1 && *obd_type)
6184                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
6185
6186         if (qctl->qc_valid != QC_GENERAL)
6187                 mnt = "";
6188
6189         inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA) &&
6190                 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
6191                  (QIF_LIMITS|QIF_USAGE));
6192
6193         print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
6194
6195         if (!show_default && verbose &&
6196             qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO) {
6197                 char strbuf[STRBUF_LEN];
6198
6199                 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
6200                                       &total_ialloc);
6201                 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
6202                                       &total_balloc);
6203                 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
6204                            human_readable);
6205                 printf("Total allocated inode limit: %ju, total "
6206                        "allocated block limit: %s\n", (uintmax_t)total_ialloc,
6207                        strbuf);
6208         }
6209
6210         if (use_default_for_blk)
6211                 printf("%cid %u is using default block quota setting\n",
6212                        *qtype_name(qctl->qc_type), qctl->qc_id);
6213
6214         if (use_default_for_file)
6215                 printf("%cid %u is using default file quota setting\n",
6216                        *qtype_name(qctl->qc_type), qctl->qc_id);
6217
6218         if (rc1 || rc2 || rc3 || inacc)
6219                 printf("Some errors happened when getting quota info. "
6220                        "Some devices may be not working or deactivated. "
6221                        "The data in \"[]\" is inaccurate.\n");
6222 out:
6223         return rc1;
6224
6225 }
6226
6227 static int lfs_project(int argc, char **argv)
6228 {
6229         int ret = 0, err = 0, c, i;
6230         struct project_handle_control phc = { 0 };
6231         enum lfs_project_ops_t op;
6232
6233         phc.newline = true;
6234         phc.assign_projid = false;
6235         /* default action */
6236         op = LFS_PROJECT_LIST;
6237
6238         while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
6239                 switch (c) {
6240                 case 'c':
6241                         if (op != LFS_PROJECT_LIST) {
6242                                 fprintf(stderr,
6243                                         "%s: cannot specify '-c' '-C' '-s' together\n",
6244                                         progname);
6245                                 return CMD_HELP;
6246                         }
6247
6248                         op = LFS_PROJECT_CHECK;
6249                         break;
6250                 case 'C':
6251                         if (op != LFS_PROJECT_LIST) {
6252                                 fprintf(stderr,
6253                                         "%s: cannot specify '-c' '-C' '-s' together\n",
6254                                         progname);
6255                                 return CMD_HELP;
6256                         }
6257
6258                         op = LFS_PROJECT_CLEAR;
6259                         break;
6260                 case 's':
6261                         if (op != LFS_PROJECT_LIST) {
6262                                 fprintf(stderr,
6263                                         "%s: cannot specify '-c' '-C' '-s' together\n",
6264                                         progname);
6265                                 return CMD_HELP;
6266                         }
6267
6268                         phc.set_inherit = true;
6269                         op = LFS_PROJECT_SET;
6270                         break;
6271                 case 'd':
6272                         phc.dironly = true;
6273                         break;
6274                 case 'k':
6275                         phc.keep_projid = true;
6276                         break;
6277                 case 'r':
6278                         phc.recursive = true;
6279                         break;
6280                 case 'p':
6281                         phc.projid = strtoul(optarg, NULL, 0);
6282                         phc.assign_projid = true;
6283
6284                         break;
6285                 case '0':
6286                         phc.newline = false;
6287                         break;
6288                 default:
6289                         fprintf(stderr, "%s: invalid option '%c'\n",
6290                                 progname, optopt);
6291                         return CMD_HELP;
6292                 }
6293         }
6294
6295         if (phc.assign_projid && op == LFS_PROJECT_LIST) {
6296                 op = LFS_PROJECT_SET;
6297                 phc.set_projid = true;
6298         } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
6299                 phc.set_projid = true;
6300         }
6301
6302         switch (op) {
6303         case LFS_PROJECT_CHECK:
6304                 if (phc.keep_projid) {
6305                         fprintf(stderr,
6306                                 "%s: '-k' is useless together with '-c'\n",
6307                                 progname);
6308                         return CMD_HELP;
6309                 }
6310                 break;
6311         case LFS_PROJECT_CLEAR:
6312                 if (!phc.newline) {
6313                         fprintf(stderr,
6314                                 "%s: '-0' is useless together with '-C'\n",
6315                                 progname);
6316                         return CMD_HELP;
6317                 }
6318                 if (phc.assign_projid) {
6319                         fprintf(stderr,
6320                                 "%s: '-p' is useless together with '-C'\n",
6321                                 progname);
6322                         return CMD_HELP;
6323                 }
6324                 break;
6325         case LFS_PROJECT_SET:
6326                 if (!phc.newline) {
6327                         fprintf(stderr,
6328                                 "%s: '-0' is useless together with '-s'\n",
6329                                 progname);
6330                         return CMD_HELP;
6331                 }
6332                 if (phc.keep_projid) {
6333                         fprintf(stderr,
6334                                 "%s: '-k' is useless together with '-s'\n",
6335                                 progname);
6336                         return CMD_HELP;
6337                 }
6338                 break;
6339         default:
6340                 if (!phc.newline) {
6341                         fprintf(stderr,
6342                                 "%s: '-0' is useless for list operations\n",
6343                                 progname);
6344                         return CMD_HELP;
6345                 }
6346                 break;
6347         }
6348
6349         argv += optind;
6350         argc -= optind;
6351         if (argc == 0) {
6352                 fprintf(stderr, "%s: missing file or directory target(s)\n",
6353                         progname);
6354                 return CMD_HELP;
6355         }
6356
6357         for (i = 0; i < argc; i++) {
6358                 switch (op) {
6359                 case LFS_PROJECT_CHECK:
6360                         err = lfs_project_check(argv[i], &phc);
6361                         break;
6362                 case LFS_PROJECT_LIST:
6363                         err = lfs_project_list(argv[i], &phc);
6364                         break;
6365                 case LFS_PROJECT_CLEAR:
6366                         err = lfs_project_clear(argv[i], &phc);
6367                         break;
6368                 case LFS_PROJECT_SET:
6369                         err = lfs_project_set(argv[i], &phc);
6370                         break;
6371                 default:
6372                         break;
6373                 }
6374                 if (err && !ret)
6375                         ret = err;
6376         }
6377
6378         return ret;
6379 }
6380
6381 static int lfs_quota(int argc, char **argv)
6382 {
6383         int c;
6384         char *mnt, *name = NULL;
6385         struct if_quotactl qctl = { .qc_cmd = LUSTRE_Q_GETQUOTA,
6386                                     .qc_type = ALLQUOTA };
6387         char *obd_uuid = (char *)qctl.obd_uuid.uuid;
6388         int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
6389         char *endptr;
6390         __u32 valid = QC_GENERAL, idx = 0;
6391         bool human_readable = false;
6392         bool show_default = false;
6393         int qtype;
6394
6395         while ((c = getopt(argc, argv, "gGi:I:o:pPqtuUvh")) != -1) {
6396                 switch (c) {
6397                 case 'U':
6398                         show_default = true;
6399                 case 'u':
6400                         qtype = USRQUOTA;
6401                         goto quota_type;
6402                 case 'G':
6403                         show_default = true;
6404                 case 'g':
6405                         qtype = GRPQUOTA;
6406                         goto quota_type;
6407                 case 'P':
6408                         show_default = true;
6409                 case 'p':
6410                         qtype = PRJQUOTA;
6411 quota_type:
6412                         if (qctl.qc_type != ALLQUOTA) {
6413                                 fprintf(stderr,
6414                                         "%s quota: only one of -u, -g, or -p may be specified\n",
6415                                         progname);
6416                                 return CMD_HELP;
6417                         }
6418                         qctl.qc_type = qtype;
6419                         break;
6420                 case 't':
6421                         qctl.qc_cmd = LUSTRE_Q_GETINFO;
6422                         break;
6423                 case 'o':
6424                         valid = qctl.qc_valid = QC_UUID;
6425                         snprintf(obd_uuid, sizeof(qctl.obd_uuid), "%s", optarg);
6426                         break;
6427                 case 'i':
6428                         valid = qctl.qc_valid = QC_MDTIDX;
6429                         idx = qctl.qc_idx = atoi(optarg);
6430                         if (idx == 0 && *optarg != '0') {
6431                                 fprintf(stderr,
6432                                         "%s quota: invalid MDT index '%s'\n",
6433                                         progname, optarg);
6434                                 return CMD_HELP;
6435                         }
6436                         break;
6437                 case 'I':
6438                         valid = qctl.qc_valid = QC_OSTIDX;
6439                         idx = qctl.qc_idx = atoi(optarg);
6440                         if (idx == 0 && *optarg != '0') {
6441                                 fprintf(stderr,
6442                                         "%s quota: invalid OST index '%s'\n",
6443                                         progname, optarg);
6444                                 return CMD_HELP;
6445                         }
6446                         break;
6447                 case 'v':
6448                         verbose = 1;
6449                         break;
6450                 case 'q':
6451                         quiet = 1;
6452                         break;
6453                 case 'h':
6454                         human_readable = true;
6455                         break;
6456                 default:
6457                         fprintf(stderr, "%s quota: unrecognized option '%s'\n",
6458                                 progname, argv[optind - 1]);
6459                         return CMD_HELP;
6460                 }
6461         }
6462
6463         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
6464         if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA && qctl.qc_type == ALLQUOTA &&
6465             optind == argc - 1 && !show_default) {
6466
6467                 qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
6468                 qctl.qc_valid = valid;
6469                 qctl.qc_idx = idx;
6470
6471                 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
6472                         qctl.qc_type = qtype;
6473                         if (qtype == USRQUOTA) {
6474                                 qctl.qc_id = geteuid();
6475                                 rc = uid2name(&name, qctl.qc_id);
6476                         } else {
6477                                 qctl.qc_id = getegid();
6478                                 rc = gid2name(&name, qctl.qc_id);
6479                         }
6480                         if (rc)
6481                                 name = "<unknown>";
6482                         mnt = argv[optind];
6483                         rc1 = get_print_quota(mnt, name, &qctl, verbose, quiet,
6484                                               human_readable, show_default);
6485                         if (rc1 && !rc)
6486                                 rc = rc1;
6487                 }
6488                 return rc;
6489         /* lfs quota -u username /path/to/lustre/mount */
6490         } else if (qctl.qc_cmd == LUSTRE_Q_GETQUOTA) {
6491                 /* options should be followed by u/g-name and mntpoint */
6492                 if ((!show_default && optind + 2 != argc) ||
6493                     (show_default && optind + 1 != argc) ||
6494                     qctl.qc_type == ALLQUOTA) {
6495                         fprintf(stderr,
6496                                 "%s quota: name and mount point must be specified\n",
6497                                 progname);
6498                         return CMD_HELP;
6499                 }
6500
6501                 if (!show_default) {
6502                         name = argv[optind++];
6503                         switch (qctl.qc_type) {
6504                         case USRQUOTA:
6505                                 rc = name2uid(&qctl.qc_id, name);
6506                                 break;
6507                         case GRPQUOTA:
6508                                 rc = name2gid(&qctl.qc_id, name);
6509                                 break;
6510                         case PRJQUOTA:
6511                                 rc = name2projid(&qctl.qc_id, name);
6512                                 break;
6513                         default:
6514                                 rc = -ENOTSUP;
6515                                 break;
6516                         }
6517                 } else {
6518                         qctl.qc_valid = QC_GENERAL;
6519                         qctl.qc_cmd = LUSTRE_Q_GETDEFAULT;
6520                         qctl.qc_id = 0;
6521                 }
6522
6523                 if (rc) {
6524                         qctl.qc_id = strtoul(name, &endptr, 10);
6525                         if (*endptr != '\0') {
6526                                 fprintf(stderr, "%s quota: invalid id '%s'\n",
6527                                         progname, name);
6528                                 return CMD_HELP;
6529                         }
6530                 }
6531         } else if (optind + 1 != argc || qctl.qc_type == ALLQUOTA) {
6532                 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
6533                         progname);
6534                 return CMD_HELP;
6535         }
6536
6537         mnt = argv[optind];
6538         rc = get_print_quota(mnt, name, &qctl, verbose, quiet,
6539                              human_readable, show_default);
6540         return rc;
6541 }
6542 #endif /* HAVE_SYS_QUOTA_H! */
6543
6544 static int flushctx_ioctl(char *mp)
6545 {
6546         int fd, rc;
6547
6548         fd = open(mp, O_RDONLY);
6549         if (fd == -1) {
6550                 fprintf(stderr, "flushctx: error open %s: %s\n",
6551                         mp, strerror(errno));
6552                 return -1;
6553         }
6554
6555         rc = ioctl(fd, LL_IOC_FLUSHCTX);
6556         if (rc == -1)
6557                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
6558                         mp, strerror(errno));
6559
6560         close(fd);
6561         return rc;
6562 }
6563
6564 static int lfs_flushctx(int argc, char **argv)
6565 {
6566         int     kdestroy = 0, c;
6567         char    mntdir[PATH_MAX] = {'\0'};
6568         int     index = 0;
6569         int     rc = 0;
6570
6571         while ((c = getopt(argc, argv, "k")) != -1) {
6572                 switch (c) {
6573                 case 'k':
6574                         kdestroy = 1;
6575                         break;
6576                 default:
6577                         fprintf(stderr, "error: %s: option '-%c' "
6578                                         "unrecognized\n", argv[0], c);
6579                         return CMD_HELP;
6580                 }
6581         }
6582
6583         if (kdestroy) {
6584             if ((rc = system("kdestroy > /dev/null")) != 0) {
6585                 rc = WEXITSTATUS(rc);
6586                 fprintf(stderr, "error destroying tickets: %d, continuing\n", rc);
6587             }
6588         }
6589
6590         if (optind >= argc) {
6591                 /* flush for all mounted lustre fs. */
6592                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
6593                         /* Check if we have a mount point */
6594                         if (mntdir[0] == '\0')
6595                                 continue;
6596
6597                         if (flushctx_ioctl(mntdir))
6598                                 rc = -1;
6599
6600                         mntdir[0] = '\0'; /* avoid matching in next loop */
6601                 }
6602         } else {
6603                 /* flush fs as specified */
6604                 while (optind < argc) {
6605                         if (flushctx_ioctl(argv[optind++]))
6606                                 rc = -1;
6607                 }
6608         }
6609         return rc;
6610 }
6611
6612 static int lfs_cp(int argc, char **argv)
6613 {
6614         fprintf(stderr, "remote client copy file(s).\n"
6615                 "obsolete, does not support it anymore.\n");
6616         return 0;
6617 }
6618
6619 static int lfs_ls(int argc, char **argv)
6620 {
6621         fprintf(stderr, "remote client lists directory contents.\n"
6622                 "obsolete, does not support it anymore.\n");
6623         return 0;
6624 }
6625
6626 static int lfs_changelog(int argc, char **argv)
6627 {
6628         void *changelog_priv;
6629         struct changelog_rec *rec;
6630         long long startrec = 0, endrec = 0;
6631         char *mdd;
6632         struct option long_opts[] = {
6633                 { .val = 'f', .name = "follow", .has_arg = no_argument },
6634                 { .name = NULL } };
6635         char short_opts[] = "f";
6636         int rc, follow = 0;
6637
6638         while ((rc = getopt_long(argc, argv, short_opts,
6639                 long_opts, NULL)) != -1) {
6640                 switch (rc) {
6641                 case 'f':
6642                         follow++;
6643                         break;
6644                 default:
6645                         fprintf(stderr,
6646                                 "%s changelog: unrecognized option '%s'\n",
6647                                 progname, argv[optind - 1]);
6648                         return CMD_HELP;
6649                 }
6650         }
6651         if (optind >= argc) {
6652                 fprintf(stderr, "%s changelog: mdtname must be specified\n",
6653                         progname);
6654                 return CMD_HELP;
6655         }
6656
6657         mdd = argv[optind++];
6658         if (argc > optind)
6659                 startrec = strtoll(argv[optind++], NULL, 10);
6660         if (argc > optind)
6661                 endrec = strtoll(argv[optind++], NULL, 10);
6662
6663         rc = llapi_changelog_start(&changelog_priv,
6664                                    CHANGELOG_FLAG_BLOCK |
6665                                    CHANGELOG_FLAG_JOBID |
6666                                    CHANGELOG_FLAG_EXTRA_FLAGS |
6667                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
6668                                    mdd, startrec);
6669         if (rc < 0) {
6670                 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
6671                         progname, strerror(errno = -rc));
6672                 return rc;
6673         }
6674
6675         rc = llapi_changelog_set_xflags(changelog_priv,
6676                                         CHANGELOG_EXTRA_FLAG_UIDGID |
6677                                         CHANGELOG_EXTRA_FLAG_NID |
6678                                         CHANGELOG_EXTRA_FLAG_OMODE |
6679                                         CHANGELOG_EXTRA_FLAG_XATTR);
6680         if (rc < 0) {
6681                 fprintf(stderr,
6682                         "%s changelog: cannot set xflags for changelog: %s\n",
6683                         progname, strerror(errno = -rc));
6684                 return rc;
6685         }
6686
6687         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
6688                 time_t secs;
6689                 struct tm ts;
6690
6691                 if (endrec && rec->cr_index > endrec) {
6692                         llapi_changelog_free(&rec);
6693                         break;
6694                 }
6695                 if (rec->cr_index < startrec) {
6696                         llapi_changelog_free(&rec);
6697                         continue;
6698                 }
6699
6700                 secs = rec->cr_time >> 30;
6701                 gmtime_r(&secs, &ts);
6702                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
6703                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
6704                        changelog_type2str(rec->cr_type),
6705                        ts.tm_hour, ts.tm_min, ts.tm_sec,
6706                        (int)(rec->cr_time & ((1 << 30) - 1)),
6707                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
6708                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
6709
6710                 if (rec->cr_flags & CLF_JOBID) {
6711                         struct changelog_ext_jobid *jid =
6712                                 changelog_rec_jobid(rec);
6713
6714                         if (jid->cr_jobid[0] != '\0')
6715                                 printf(" j=%s", jid->cr_jobid);
6716                 }
6717
6718                 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
6719                         struct changelog_ext_extra_flags *ef =
6720                                 changelog_rec_extra_flags(rec);
6721
6722                         printf(" ef=0x%llx", ef->cr_extra_flags);
6723
6724                         if (ef->cr_extra_flags & CLFE_UIDGID) {
6725                                 struct changelog_ext_uidgid *uidgid =
6726                                         changelog_rec_uidgid(rec);
6727
6728                                 printf(" u=%llu:%llu",
6729                                        uidgid->cr_uid, uidgid->cr_gid);
6730                         }
6731                         if (ef->cr_extra_flags & CLFE_NID) {
6732                                 struct changelog_ext_nid *nid =
6733                                         changelog_rec_nid(rec);
6734
6735                                 printf(" nid=%s",
6736                                        libcfs_nid2str(nid->cr_nid));
6737                         }
6738
6739                         if (ef->cr_extra_flags & CLFE_OPEN) {
6740                                 struct changelog_ext_openmode *omd =
6741                                         changelog_rec_openmode(rec);
6742                                 char mode[] = "---";
6743
6744                                 /* exec mode must be exclusive */
6745                                 if (omd->cr_openflags & MDS_FMODE_EXEC) {
6746                                         mode[2] = 'x';
6747                                 } else {
6748                                         if (omd->cr_openflags & MDS_FMODE_READ)
6749                                                 mode[0] = 'r';
6750                                         if (omd->cr_openflags &
6751                                             (MDS_FMODE_WRITE |
6752                                              MDS_OPEN_TRUNC |
6753                                              MDS_OPEN_APPEND))
6754                                                 mode[1] = 'w';
6755                                 }
6756
6757                                 if (strcmp(mode, "---") != 0)
6758                                         printf(" m=%s", mode);
6759
6760                         }
6761
6762                         if (ef->cr_extra_flags & CLFE_XATTR) {
6763                                 struct changelog_ext_xattr *xattr =
6764                                         changelog_rec_xattr(rec);
6765
6766                                 if (xattr->cr_xattr[0] != '\0')
6767                                         printf(" x=%s", xattr->cr_xattr);
6768                         }
6769                 }
6770
6771                 if (rec->cr_namelen)
6772                         printf(" p="DFID" %.*s", PFID(&rec->cr_pfid),
6773                                rec->cr_namelen, changelog_rec_name(rec));
6774
6775                 if (rec->cr_flags & CLF_RENAME) {
6776                         struct changelog_ext_rename *rnm =
6777                                 changelog_rec_rename(rec);
6778
6779                         if (!fid_is_zero(&rnm->cr_sfid))
6780                                 printf(" s="DFID" sp="DFID" %.*s",
6781                                        PFID(&rnm->cr_sfid),
6782                                        PFID(&rnm->cr_spfid),
6783                                        (int)changelog_rec_snamelen(rec),
6784                                        changelog_rec_sname(rec));
6785                 }
6786                 printf("\n");
6787
6788                 llapi_changelog_free(&rec);
6789         }
6790
6791         llapi_changelog_fini(&changelog_priv);
6792
6793         if (rc < 0)
6794                 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
6795                         progname, strerror(errno = -rc));
6796
6797         return (rc == 1 ? 0 : rc);
6798 }
6799
6800 static int lfs_changelog_clear(int argc, char **argv)
6801 {
6802         long long endrec;
6803         int rc;
6804
6805         if (argc != 4)
6806                 return CMD_HELP;
6807
6808         endrec = strtoll(argv[3], NULL, 10);
6809
6810         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
6811
6812         if (rc == -EINVAL)
6813                 fprintf(stderr, "%s: record out of range: %llu\n",
6814                         argv[0], endrec);
6815         else if (rc == -ENOENT)
6816                 fprintf(stderr, "%s: no changelog user: %s\n",
6817                         argv[0], argv[2]);
6818         else if (rc)
6819                 fprintf(stderr, "%s error: %s\n", argv[0],
6820                         strerror(-rc));
6821
6822         if (rc)
6823                 errno = -rc;
6824
6825         return rc;
6826 }
6827
6828 static int lfs_fid2path(int argc, char **argv)
6829 {
6830         struct option long_opts[] = {
6831                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
6832                 { .val = 'l',   .name = "link", .has_arg = required_argument },
6833                 { .val = 'r',   .name = "rec",  .has_arg = required_argument },
6834                 { .name = NULL } };
6835         char  short_opts[] = "cl:r:";
6836         char *device, *fid, *path;
6837         long long recno = -1;
6838         int linkno = -1;
6839         int lnktmp;
6840         int printcur = 0;
6841         int rc = 0;
6842         char *endptr = NULL;
6843
6844         while ((rc = getopt_long(argc, argv, short_opts,
6845                 long_opts, NULL)) != -1) {
6846                 switch (rc) {
6847                 case 'c':
6848                         printcur++;
6849                         break;
6850                 case 'l':
6851                         linkno = strtol(optarg, &endptr, 10);
6852                         if (*endptr != '\0') {
6853                                 fprintf(stderr,
6854                                         "%s fid2path: invalid linkno '%s'\n",
6855                                         progname, optarg);
6856                                 return CMD_HELP;
6857                         }
6858                         break;
6859                 case 'r':
6860                         recno = strtoll(optarg, &endptr, 10);
6861                         if (*endptr != '\0') {
6862                                 fprintf(stderr,
6863                                         "%s fid2path: invalid recno '%s'\n",
6864                                         progname, optarg);
6865                                 return CMD_HELP;
6866                         }
6867                         break;
6868                 default:
6869                         fprintf(stderr,
6870                                 "%s fid2path: unrecognized option '%s'\n",
6871                                 progname, argv[optind - 1]);
6872                         return CMD_HELP;
6873                 }
6874         }
6875
6876         if (argc < 3) {
6877                 fprintf(stderr,
6878                         "%s fid2path: <fsname|rootpath> and <fid>... must be specified\n",
6879                         progname);
6880                 return CMD_HELP;
6881         }
6882
6883         device = argv[optind++];
6884         path = calloc(1, PATH_MAX);
6885         if (path == NULL) {
6886                 rc = -errno;
6887                 fprintf(stderr,
6888                         "%s fid2path: cannot allocate memory for path: %s\n",
6889                         progname, strerror(-rc));
6890                 return rc;
6891         }
6892
6893         rc = 0;
6894         while (optind < argc) {
6895                 fid = argv[optind++];
6896
6897                 lnktmp = (linkno >= 0) ? linkno : 0;
6898                 while (1) {
6899                         int oldtmp = lnktmp;
6900                         long long rectmp = recno;
6901                         int rc2;
6902                         rc2 = llapi_fid2path(device, fid, path, PATH_MAX,
6903                                              &rectmp, &lnktmp);
6904                         if (rc2 < 0) {
6905                                 fprintf(stderr,
6906                                         "%s fid2path: cannot find '%s': %s\n",
6907                                         progname, fid, strerror(errno = -rc2));
6908                                 if (rc == 0)
6909                                         rc = rc2;
6910                                 break;
6911                         }
6912
6913                         if (printcur)
6914                                 fprintf(stdout, "%lld ", rectmp);
6915                         if (device[0] == '/') {
6916                                 fprintf(stdout, "%s", device);
6917                                 if (device[strlen(device) - 1] != '/')
6918                                         fprintf(stdout, "/");
6919                         } else if (path[0] == '\0') {
6920                                 fprintf(stdout, "/");
6921                         }
6922                         fprintf(stdout, "%s\n", path);
6923
6924                         if (linkno >= 0)
6925                                 /* specified linkno */
6926                                 break;
6927                         if (oldtmp == lnktmp)
6928                                 /* no more links */
6929                                 break;
6930                 }
6931         }
6932
6933         free(path);
6934         return rc;
6935 }
6936
6937 static int lfs_path2fid(int argc, char **argv)
6938 {
6939         struct option long_opts[] = {
6940                 { .val = 'p', .name = "parents", .has_arg = no_argument },
6941                 { .name = NULL } };
6942         char            **path;
6943         const char        short_opts[] = "p";
6944         const char       *sep = "";
6945         struct lu_fid     fid;
6946         int               rc = 0;
6947         bool              show_parents = false;
6948
6949         while ((rc = getopt_long(argc, argv, short_opts,
6950                                  long_opts, NULL)) != -1) {
6951                 switch (rc) {
6952                 case 'p':
6953                         show_parents = true;
6954                         break;
6955                 default:
6956                         fprintf(stderr,
6957                                 "%s path2fid: unrecognized option '%s'\n",
6958                                 progname, argv[optind - 1]);
6959                         return CMD_HELP;
6960                 }
6961         }
6962
6963         if (optind > argc - 1) {
6964                 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
6965                         progname);
6966                 return CMD_HELP;
6967         }
6968         else if (optind < argc - 1)
6969                 sep = ": ";
6970
6971         rc = 0;
6972         for (path = argv + optind; *path != NULL; path++) {
6973                 int err = 0;
6974                 if (!show_parents) {
6975                         err = llapi_path2fid(*path, &fid);
6976                         if (!err)
6977                                 printf("%s%s"DFID"\n",
6978                                        *sep != '\0' ? *path : "", sep,
6979                                        PFID(&fid));
6980                 } else {
6981                         char            name[NAME_MAX + 1];
6982                         unsigned int    linkno = 0;
6983
6984                         while ((err = llapi_path2parent(*path, linkno, &fid,
6985                                                 name, sizeof(name))) == 0) {
6986                                 if (*sep != '\0' && linkno == 0)
6987                                         printf("%s%s", *path, sep);
6988
6989                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
6990                                        PFID(&fid), name);
6991                                 linkno++;
6992                         }
6993
6994                         /* err == -ENODATA is end-of-loop */
6995                         if (linkno > 0 && err == -ENODATA) {
6996                                 printf("\n");
6997                                 err = 0;
6998                         }
6999                 }
7000
7001                 if (err) {
7002                         fprintf(stderr,
7003                                 "%s path2fid: cannot get %sfid for '%s': %s\n",
7004                                 progname, show_parents ? "parent " : "", *path,
7005                                 strerror(-err));
7006                         if (rc == 0) {
7007                                 rc = err;
7008                                 errno = -err;
7009                         }
7010                 }
7011         }
7012
7013         return rc;
7014 }
7015
7016 static int lfs_data_version(int argc, char **argv)
7017 {
7018         char *path;
7019         __u64 data_version;
7020         int fd;
7021         int rc;
7022         int c;
7023         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
7024
7025         if (argc < 2) {
7026                 fprintf(stderr, "%s data_version: FILE must be specified\n",
7027                         progname);
7028                 return CMD_HELP;
7029         }
7030
7031         while ((c = getopt(argc, argv, "nrw")) != -1) {
7032                 switch (c) {
7033                 case 'n':
7034                         data_version_flags = 0;
7035                         break;
7036                 case 'r':
7037                         data_version_flags |= LL_DV_RD_FLUSH;
7038                         break;
7039                 case 'w':
7040                         data_version_flags |= LL_DV_WR_FLUSH;
7041                         break;
7042                 default:
7043                         fprintf(stderr,
7044                                 "%s data_version: unrecognized option '%s'\n",
7045                                 progname, argv[optind - 1]);
7046                         return CMD_HELP;
7047                 }
7048         }
7049         if (optind == argc) {
7050                 fprintf(stderr, "%s data_version: FILE must be specified\n",
7051                         progname);
7052                 return CMD_HELP;
7053         }
7054
7055         path = argv[optind];
7056         fd = open(path, O_RDONLY);
7057         if (fd < 0) {
7058                 rc = -errno;
7059                 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
7060                         progname, path, strerror(-rc));
7061                 return rc;
7062         }
7063
7064         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
7065         if (rc < 0)
7066                 fprintf(stderr,
7067                         "%s data_version: cannot get version for '%s': %s\n",
7068                         progname, path, strerror(-rc));
7069         else
7070                 printf("%ju" "\n", (uintmax_t)data_version);
7071
7072         close(fd);
7073         return rc;
7074 }
7075
7076 static int lfs_hsm_state(int argc, char **argv)
7077 {
7078         int rc;
7079         int i = 1;
7080         char *path;
7081         struct hsm_user_state hus;
7082
7083         if (argc < 2)
7084                 return CMD_HELP;
7085
7086         do {
7087                 path = argv[i];
7088
7089                 rc = llapi_hsm_state_get(path, &hus);
7090                 if (rc) {
7091                         fprintf(stderr, "can't get hsm state for %s: %s\n",
7092                                 path, strerror(errno = -rc));
7093                         return rc;
7094                 }
7095
7096                 /* Display path name and status flags */
7097                 printf("%s: (0x%08x)", path, hus.hus_states);
7098
7099                 if (hus.hus_states & HS_RELEASED)
7100                         printf(" released");
7101                 if (hus.hus_states & HS_EXISTS)
7102                         printf(" exists");
7103                 if (hus.hus_states & HS_DIRTY)
7104                         printf(" dirty");
7105                 if (hus.hus_states & HS_ARCHIVED)
7106                         printf(" archived");
7107                 /* Display user-settable flags */
7108                 if (hus.hus_states & HS_NORELEASE)
7109                         printf(" never_release");
7110                 if (hus.hus_states & HS_NOARCHIVE)
7111                         printf(" never_archive");
7112                 if (hus.hus_states & HS_LOST)
7113                         printf(" lost_from_hsm");
7114
7115                 if (hus.hus_archive_id != 0)
7116                         printf(", archive_id:%d", hus.hus_archive_id);
7117                 printf("\n");
7118
7119         } while (++i < argc);
7120
7121         return 0;
7122 }
7123
7124 #define LFS_HSM_SET   0
7125 #define LFS_HSM_CLEAR 1
7126
7127 /**
7128  * Generic function to set or clear HSM flags.
7129  * Used by hsm_set and hsm_clear.
7130  *
7131  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
7132  */
7133 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
7134 {
7135         struct option long_opts[] = {
7136         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
7137         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
7138         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
7139         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
7140         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
7141         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
7142         { .val = 'i',   .name = "archive-id",   .has_arg = required_argument },
7143         { .name = NULL } };
7144         char short_opts[] = "lraAdei:";
7145         __u64 mask = 0;
7146         int c, rc;
7147         char *path;
7148         __u32 archive_id = 0;
7149         char *end = NULL;
7150
7151         if (argc < 3)
7152                 return CMD_HELP;
7153
7154         while ((c = getopt_long(argc, argv, short_opts,
7155                                 long_opts, NULL)) != -1) {
7156                 switch (c) {
7157                 case 'l':
7158                         mask |= HS_LOST;
7159                         break;
7160                 case 'a':
7161                         mask |= HS_NOARCHIVE;
7162                         break;
7163                 case 'A':
7164                         mask |= HS_ARCHIVED;
7165                         break;
7166                 case 'r':
7167                         mask |= HS_NORELEASE;
7168                         break;
7169                 case 'd':
7170                         mask |= HS_DIRTY;
7171                         break;
7172                 case 'e':
7173                         mask |= HS_EXISTS;
7174                         break;
7175                 case 'i':
7176                         archive_id = strtol(optarg, &end, 10);
7177                         if (*end != '\0') {
7178                                 fprintf(stderr, "invalid archive_id: '%s'\n",
7179                                         end);
7180                                 return CMD_HELP;
7181                         }
7182                         break;
7183                 case '?':
7184                         return CMD_HELP;
7185                 default:
7186                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7187                                 argv[0], argv[optind - 1]);
7188                         return CMD_HELP;
7189                 }
7190         }
7191
7192         /* User should have specified a flag */
7193         if (mask == 0)
7194                 return CMD_HELP;
7195
7196         while (optind < argc) {
7197
7198                 path = argv[optind];
7199
7200                 /* If mode == 0, this means we apply the mask. */
7201                 if (mode == LFS_HSM_SET)
7202                         rc = llapi_hsm_state_set(path, mask, 0, archive_id);
7203                 else
7204                         rc = llapi_hsm_state_set(path, 0, mask, 0);
7205
7206                 if (rc != 0) {
7207                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
7208                                 path, strerror(errno = -rc));
7209                         return rc;
7210                 }
7211                 optind++;
7212         }
7213
7214         return 0;
7215 }
7216
7217 static int lfs_hsm_action(int argc, char **argv)
7218 {
7219         int                              rc;
7220         int                              i = 1;
7221         char                            *path;
7222         struct hsm_current_action        hca;
7223         struct hsm_extent                he;
7224         enum hsm_user_action             hua;
7225         enum hsm_progress_states         hps;
7226
7227         if (argc < 2)
7228                 return CMD_HELP;
7229
7230         do {
7231                 path = argv[i];
7232
7233                 rc = llapi_hsm_current_action(path, &hca);
7234                 if (rc) {
7235                         fprintf(stderr, "can't get hsm action for %s: %s\n",
7236                                 path, strerror(errno = -rc));
7237                         return rc;
7238                 }
7239                 he = hca.hca_location;
7240                 hua = hca.hca_action;
7241                 hps = hca.hca_state;
7242
7243                 printf("%s: %s", path, hsm_user_action2name(hua));
7244
7245                 /* Skip file without action */
7246                 if (hca.hca_action == HUA_NONE) {
7247                         printf("\n");
7248                         continue;
7249                 }
7250
7251                 printf(" %s ", hsm_progress_state2name(hps));
7252
7253                 if ((hps == HPS_RUNNING) &&
7254                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
7255                         printf("(%llu bytes moved)\n",
7256                                (unsigned long long)he.length);
7257                 else if ((he.offset + he.length) == LUSTRE_EOF)
7258                         printf("(from %llu to EOF)\n",
7259                                (unsigned long long)he.offset);
7260                 else
7261                         printf("(from %llu to %llu)\n",
7262                                (unsigned long long)he.offset,
7263                                (unsigned long long)(he.offset + he.length));
7264
7265         } while (++i < argc);
7266
7267         return 0;
7268 }
7269
7270 static int lfs_hsm_set(int argc, char **argv)
7271 {
7272         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
7273 }
7274
7275 static int lfs_hsm_clear(int argc, char **argv)
7276 {
7277         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
7278 }
7279
7280 /**
7281  * Check file state and return its fid, to be used by lfs_hsm_request().
7282  *
7283  * \param[in]     file      Path to file to check
7284  * \param[in,out] fid       Pointer to allocated lu_fid struct.
7285  * \param[in,out] last_dev  Pointer to last device id used.
7286  *
7287  * \return 0 on success.
7288  */
7289 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
7290                                 dev_t *last_dev)
7291 {
7292         struct stat     st;
7293         int             rc;
7294
7295         rc = lstat(file, &st);
7296         if (rc) {
7297                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
7298                 return -errno;
7299         }
7300         /* Checking for regular file as archiving as posix copytool
7301          * rejects archiving files other than regular files
7302          */
7303         if (!S_ISREG(st.st_mode)) {
7304                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
7305                 return CMD_HELP;
7306         }
7307         /* A request should be ... */
7308         if (*last_dev != st.st_dev && *last_dev != 0) {
7309                 fprintf(stderr, "All files should be "
7310                         "on the same filesystem: %s\n", file);
7311                 return -EINVAL;
7312         }
7313         *last_dev = st.st_dev;
7314
7315         rc = llapi_path2fid(file, fid);
7316         if (rc) {
7317                 fprintf(stderr, "Cannot read FID of %s: %s\n",
7318                         file, strerror(-rc));
7319                 return rc;
7320         }
7321         return 0;
7322 }
7323
7324 /* Fill an HSM HUR item with a given file name.
7325  *
7326  * If mntpath is set, then the filename is actually a FID, and no
7327  * lookup on the filesystem will be performed.
7328  *
7329  * \param[in]  hur         the user request to fill
7330  * \param[in]  idx         index of the item inside the HUR to fill
7331  * \param[in]  mntpath     mountpoint of Lustre
7332  * \param[in]  fname       filename (if mtnpath is NULL)
7333  *                         or FID (if mntpath is set)
7334  * \param[in]  last_dev    pointer to last device id used
7335  *
7336  * \retval 0 on success
7337  * \retval CMD_HELP or a negative errno on error
7338  */
7339 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
7340                          const char *mntpath, const char *fname,
7341                          dev_t *last_dev)
7342 {
7343         struct hsm_user_item *hui = &hur->hur_user_item[idx];
7344         int rc;
7345
7346         hui->hui_extent.length = -1;
7347
7348         if (mntpath != NULL) {
7349                 if (*fname == '[')
7350                         fname++;
7351                 rc = sscanf(fname, SFID, RFID(&hui->hui_fid));
7352                 if (rc == 3) {
7353                         rc = 0;
7354                 } else {
7355                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
7356                                 fname);
7357                         rc = -EINVAL;
7358                 }
7359         } else {
7360                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
7361         }
7362
7363         if (rc == 0)
7364                 hur->hur_request.hr_itemcount++;
7365
7366         return rc;
7367 }
7368
7369 static int lfs_hsm_request(int argc, char **argv, int action)
7370 {
7371         struct option long_opts[] = {
7372         { .val = 'a',   .name = "archive",      .has_arg = required_argument },
7373         { .val = 'D',   .name = "data",         .has_arg = required_argument },
7374         { .val = 'l',   .name = "filelist",     .has_arg = required_argument },
7375         { .val = 'm',   .name = "mntpath",      .has_arg = required_argument },
7376         { .name = NULL } };
7377         dev_t                    last_dev = 0;
7378         char                     short_opts[] = "l:D:a:m:";
7379         struct hsm_user_request *hur, *oldhur;
7380         int                      c, i;
7381         size_t                   len;
7382         int                      nbfile;
7383         char                    *line = NULL;
7384         char                    *filelist = NULL;
7385         char                     fullpath[PATH_MAX];
7386         char                    *opaque = NULL;
7387         int                      opaque_len = 0;
7388         int                      archive_id = 0;
7389         FILE                    *fp;
7390         int                      nbfile_alloc = 0;
7391         char                    *some_file = NULL;
7392         char                    *mntpath = NULL;
7393         int                      rc;
7394
7395         if (argc < 2)
7396                 return CMD_HELP;
7397
7398         while ((c = getopt_long(argc, argv, short_opts,
7399                                 long_opts, NULL)) != -1) {
7400                 switch (c) {
7401                 case 'l':
7402                         filelist = optarg;
7403                         break;
7404                 case 'D':
7405                         opaque = optarg;
7406                         break;
7407                 case 'a':
7408                         if (action != HUA_ARCHIVE &&
7409                             action != HUA_REMOVE) {
7410                                 fprintf(stderr,
7411                                         "error: -a is supported only "
7412                                         "when archiving or removing\n");
7413                                 return CMD_HELP;
7414                         }
7415                         archive_id = atoi(optarg);
7416                         break;
7417                 case 'm':
7418                         if (some_file == NULL) {
7419                                 mntpath = optarg;
7420                                 some_file = strdup(optarg);
7421                         }
7422                         break;
7423                 case '?':
7424                         return CMD_HELP;
7425                 default:
7426                         fprintf(stderr, "error: %s: option '%s' unrecognized\n",
7427                                 argv[0], argv[optind - 1]);
7428                         return CMD_HELP;
7429                 }
7430         }
7431
7432         /* All remaining args are files, so we have at least nbfile */
7433         nbfile = argc - optind;
7434
7435         if ((nbfile == 0) && (filelist == NULL))
7436                 return CMD_HELP;
7437
7438         if (opaque != NULL)
7439                 opaque_len = strlen(opaque);
7440
7441         /* Alloc the request structure with enough place to store all files
7442          * from command line. */
7443         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
7444         if (hur == NULL) {
7445                 fprintf(stderr, "Cannot create the request: %s\n",
7446                         strerror(errno));
7447                 return errno;
7448         }
7449         nbfile_alloc = nbfile;
7450
7451         hur->hur_request.hr_action = action;
7452         hur->hur_request.hr_archive_id = archive_id;
7453         hur->hur_request.hr_flags = 0;
7454
7455         /* All remaining args are files, add them */
7456         if (nbfile != 0 && some_file == NULL)
7457                 some_file = strdup(argv[optind]);
7458
7459         for (i = 0; i < nbfile; i++) {
7460                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
7461                                    &last_dev);
7462                 if (rc)
7463                         goto out_free;
7464         }
7465
7466         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
7467
7468         /* If a filelist was specified, read the filelist from it. */
7469         if (filelist != NULL) {
7470                 fp = fopen(filelist, "r");
7471                 if (fp == NULL) {
7472                         fprintf(stderr, "Cannot read the file list %s: %s\n",
7473                                 filelist, strerror(errno));
7474                         rc = -errno;
7475                         goto out_free;
7476                 }
7477
7478                 while ((rc = getline(&line, &len, fp)) != -1) {
7479                         /* If allocated buffer was too small, get something
7480                          * larger */
7481                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
7482                                 ssize_t size;
7483
7484                                 nbfile_alloc = nbfile_alloc * 2 + 1;
7485                                 oldhur = hur;
7486                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
7487                                                                    opaque_len);
7488                                 if (hur == NULL) {
7489                                         fprintf(stderr, "hsm: cannot allocate "
7490                                                 "the request: %s\n",
7491                                                 strerror(errno));
7492                                         hur = oldhur;
7493                                         rc = -errno;
7494                                         fclose(fp);
7495                                         goto out_free;
7496                                 }
7497                                 size = hur_len(oldhur);
7498                                 if (size < 0) {
7499                                         fprintf(stderr, "hsm: cannot allocate "
7500                                                 "%u files + %u bytes data\n",
7501                                             oldhur->hur_request.hr_itemcount,
7502                                             oldhur->hur_request.hr_data_len);
7503                                         free(hur);
7504                                         hur = oldhur;
7505                                         rc = -E2BIG;
7506                                         fclose(fp);
7507                                         goto out_free;
7508                                 }
7509                                 memcpy(hur, oldhur, size);
7510                                 free(oldhur);
7511                         }
7512
7513                         /* Chop CR */
7514                         if (line[strlen(line) - 1] == '\n')
7515                                 line[strlen(line) - 1] = '\0';
7516
7517                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
7518                                            mntpath, line, &last_dev);
7519                         if (rc) {
7520                                 fclose(fp);
7521                                 goto out_free;
7522                         }
7523
7524                         if (some_file == NULL) {
7525                                 some_file = line;
7526                                 line = NULL;
7527                         }
7528                 }
7529
7530                 rc = fclose(fp);
7531                 free(line);
7532         }
7533
7534         /* If a --data was used, add it to the request */
7535         hur->hur_request.hr_data_len = opaque_len;
7536         if (opaque != NULL)
7537                 memcpy(hur_data(hur), opaque, opaque_len);
7538
7539         /* Send the HSM request */
7540         if (realpath(some_file, fullpath) == NULL) {
7541                 fprintf(stderr, "Could not find path '%s': %s\n",
7542                         some_file, strerror(errno));
7543         }
7544         rc = llapi_hsm_request(fullpath, hur);
7545         if (rc) {
7546                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
7547                         some_file, strerror(-rc));
7548                 goto out_free;
7549         }
7550
7551 out_free:
7552         free(some_file);
7553         free(hur);
7554         return rc;
7555 }
7556
7557 static int lfs_hsm_archive(int argc, char **argv)
7558 {
7559         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
7560 }
7561
7562 static int lfs_hsm_restore(int argc, char **argv)
7563 {
7564         return lfs_hsm_request(argc, argv, HUA_RESTORE);
7565 }
7566
7567 static int lfs_hsm_release(int argc, char **argv)
7568 {
7569         return lfs_hsm_request(argc, argv, HUA_RELEASE);
7570 }
7571
7572 static int lfs_hsm_remove(int argc, char **argv)
7573 {
7574         return lfs_hsm_request(argc, argv, HUA_REMOVE);
7575 }
7576
7577 static int lfs_hsm_cancel(int argc, char **argv)
7578 {
7579         return lfs_hsm_request(argc, argv, HUA_CANCEL);
7580 }
7581
7582 static int lfs_swap_layouts(int argc, char **argv)
7583 {
7584         if (argc != 3)
7585                 return CMD_HELP;
7586
7587         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
7588                                   SWAP_LAYOUTS_KEEP_MTIME |
7589                                   SWAP_LAYOUTS_KEEP_ATIME);
7590 }
7591
7592 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
7593
7594 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
7595
7596 int lfs_get_mode(const char *string)
7597 {
7598         enum lock_mode_user mode;
7599
7600         for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
7601                 if (lock_mode_names[mode] == NULL)
7602                         continue;
7603                 if (strcmp(string, lock_mode_names[mode]) == 0)
7604                         return mode;
7605         }
7606
7607         return -EINVAL;
7608 }
7609
7610 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
7611 {
7612         enum lu_ladvise_type advice;
7613
7614         for (advice = 0;
7615              advice < ARRAY_SIZE(ladvise_names); advice++) {
7616                 if (ladvise_names[advice] == NULL)
7617                         continue;
7618                 if (strcmp(string, ladvise_names[advice]) == 0)
7619                         return advice;
7620         }
7621
7622         return LU_LADVISE_INVALID;
7623 }
7624
7625 static int lfs_ladvise(int argc, char **argv)
7626 {
7627         struct option long_opts[] = {
7628         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
7629         { .val = 'b',   .name = "background",   .has_arg = no_argument },
7630         { .val = 'e',   .name = "end",          .has_arg = required_argument },
7631         { .val = 'l',   .name = "length",       .has_arg = required_argument },
7632         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
7633         { .val = 's',   .name = "start",        .has_arg = required_argument },
7634         { .val = 'u',   .name = "unset",        .has_arg = no_argument },
7635         { .name = NULL } };
7636         char                     short_opts[] = "a:be:l:m:s:u";
7637         int                      c;
7638         int                      rc = 0;
7639         const char              *path;
7640         int                      fd;
7641         struct llapi_lu_ladvise  advice;
7642         enum lu_ladvise_type     advice_type = LU_LADVISE_INVALID;
7643         unsigned long long       start = 0;
7644         unsigned long long       end = LUSTRE_EOF;
7645         unsigned long long       length = 0;
7646         unsigned long long       size_units;
7647         unsigned long long       flags = 0;
7648         int                      mode = 0;
7649
7650         optind = 0;
7651         while ((c = getopt_long(argc, argv, short_opts,
7652                                 long_opts, NULL)) != -1) {
7653                 switch (c) {
7654                 case 'a':
7655                         advice_type = lfs_get_ladvice(optarg);
7656                         if (advice_type == LU_LADVISE_INVALID) {
7657                                 fprintf(stderr, "%s: invalid advice type "
7658                                         "'%s'\n", argv[0], optarg);
7659                                 fprintf(stderr, "Valid types:");
7660
7661                                 for (advice_type = 0;
7662                                      advice_type < ARRAY_SIZE(ladvise_names);
7663                                      advice_type++) {
7664                                         if (ladvise_names[advice_type] == NULL)
7665                                                 continue;
7666                                         fprintf(stderr, " %s",
7667                                                 ladvise_names[advice_type]);
7668                                 }
7669                                 fprintf(stderr, "\n");
7670
7671                                 return CMD_HELP;
7672                         }
7673                         break;
7674                 case 'b':
7675                         flags |= LF_ASYNC;
7676                         break;
7677                 case 'u':
7678                         flags |= LF_UNSET;
7679                         break;
7680                 case 'e':
7681                         size_units = 1;
7682                         rc = llapi_parse_size(optarg, &end,
7683                                               &size_units, 0);
7684                         if (rc) {
7685                                 fprintf(stderr, "%s: bad end offset '%s'\n",
7686                                         argv[0], optarg);
7687                                 return CMD_HELP;
7688                         }
7689                         break;
7690                 case 's':
7691                         size_units = 1;
7692                         rc = llapi_parse_size(optarg, &start,
7693                                               &size_units, 0);
7694                         if (rc) {
7695                                 fprintf(stderr, "%s: bad start offset "
7696                                         "'%s'\n", argv[0], optarg);
7697                                 return CMD_HELP;
7698                         }
7699                         break;
7700                 case 'l':
7701                         size_units = 1;
7702                         rc = llapi_parse_size(optarg, &length,
7703                                               &size_units, 0);
7704                         if (rc) {
7705                                 fprintf(stderr, "%s: bad length '%s'\n",
7706                                         argv[0], optarg);
7707                                 return CMD_HELP;
7708                         }
7709                         break;
7710                 case 'm':
7711                         mode = lfs_get_mode(optarg);
7712                         if (mode < 0) {
7713                                 fprintf(stderr, "%s: bad mode '%s', valid "
7714                                                  "modes are READ or WRITE\n",
7715                                         argv[0], optarg);
7716                                 return CMD_HELP;
7717                         }
7718                         break;
7719                 case '?':
7720                         return CMD_HELP;
7721                 default:
7722                         fprintf(stderr, "%s: option '%s' unrecognized\n",
7723                                 argv[0], argv[optind - 1]);
7724                         return CMD_HELP;
7725                 }
7726         }
7727
7728         if (advice_type == LU_LADVISE_INVALID) {
7729                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
7730                 fprintf(stderr, "Valid types:");
7731                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
7732                      advice_type++) {
7733                         if (ladvise_names[advice_type] == NULL)
7734                                 continue;
7735                         fprintf(stderr, " %s", ladvise_names[advice_type]);
7736                 }
7737                 fprintf(stderr, "\n");
7738                 return CMD_HELP;
7739         }
7740
7741         if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
7742                 fprintf(stderr, "%s: Lock no expand advice is a per file "
7743                                  "descriptor advice, so when called from lfs, "
7744                                  "it does nothing.\n", argv[0]);
7745                 return CMD_HELP;
7746         }
7747
7748         if (argc <= optind) {
7749                 fprintf(stderr, "%s: please give one or more file names\n",
7750                         argv[0]);
7751                 return CMD_HELP;
7752         }
7753
7754         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
7755                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
7756                         argv[0]);
7757                 return CMD_HELP;
7758         }
7759
7760         if (end == LUSTRE_EOF && length != 0)
7761                 end = start + length;
7762
7763         if (end <= start) {
7764                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
7765                         argv[0], start, end);
7766                 return CMD_HELP;
7767         }
7768
7769         if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
7770                 fprintf(stderr, "%s: mode is only valid with lockahead\n",
7771                         argv[0]);
7772                 return CMD_HELP;
7773         }
7774
7775         if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
7776                 fprintf(stderr, "%s: mode is required with lockahead\n",
7777                         argv[0]);
7778                 return CMD_HELP;
7779         }
7780
7781         while (optind < argc) {
7782                 int rc2;
7783
7784                 path = argv[optind++];
7785
7786                 fd = open(path, O_RDONLY);
7787                 if (fd < 0) {
7788                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
7789                                 argv[0], path, strerror(errno));
7790                         rc2 = -errno;
7791                         goto next;
7792                 }
7793
7794                 advice.lla_start = start;
7795                 advice.lla_end = end;
7796                 advice.lla_advice = advice_type;
7797                 advice.lla_value1 = 0;
7798                 advice.lla_value2 = 0;
7799                 advice.lla_value3 = 0;
7800                 advice.lla_value4 = 0;
7801                 if (advice_type == LU_LADVISE_LOCKAHEAD) {
7802                         advice.lla_lockahead_mode = mode;
7803                         advice.lla_peradvice_flags = flags;
7804                 }
7805
7806                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
7807                 close(fd);
7808                 if (rc2 < 0) {
7809                         fprintf(stderr, "%s: cannot give advice '%s' to file "
7810                                 "'%s': %s\n", argv[0],
7811                                 ladvise_names[advice_type],
7812                                 path, strerror(errno));
7813
7814                         goto next;
7815                 }
7816
7817 next:
7818                 if (rc == 0 && rc2 < 0)
7819                         rc = rc2;
7820         }
7821         return rc;
7822 }
7823
7824 /** The input string contains a comma delimited list of component ids and
7825  * ranges, for example "1,2-4,7".
7826  */
7827 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
7828 {
7829         bool end_of_loop = false;
7830         char *ptr = NULL;
7831         int nr = 0;
7832         int rc;
7833
7834         if (arg == NULL)
7835                 return -EINVAL;
7836
7837         while (!end_of_loop) {
7838                 int start_index;
7839                 int end_index;
7840                 int i;
7841                 char *endptr = NULL;
7842
7843                 rc = -EINVAL;
7844                 ptr = strchrnul(arg, ',');
7845                 end_of_loop = *ptr == '\0';
7846                 *ptr = '\0';
7847
7848                 start_index = strtol(arg, &endptr, 0);
7849                 if (endptr == arg) /* no data at all */
7850                         break;
7851                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
7852                         break;
7853                 if (start_index < 0)
7854                         break;
7855
7856                 end_index = start_index;
7857                 if (*endptr == '-') {
7858                         end_index = strtol(endptr + 1, &endptr, 0);
7859                         if (*endptr != '\0')
7860                                 break;
7861                         if (end_index < start_index)
7862                                 break;
7863                 }
7864
7865                 for (i = start_index; i <= end_index && size > 0; i++) {
7866                         int j;
7867
7868                         /* remove duplicate */
7869                         for (j = 0; j < nr; j++) {
7870                                 if (ids[j] == i)
7871                                         break;
7872                         }
7873                         if (j == nr) { /* no duplicate */
7874                                 ids[nr++] = i;
7875                                 --size;
7876                         }
7877                 }
7878
7879                 if (size == 0 && i < end_index)
7880                         break;
7881
7882                 *ptr = ',';
7883                 arg = ++ptr;
7884                 rc = 0;
7885         }
7886         if (!end_of_loop && ptr != NULL)
7887                 *ptr = ',';
7888
7889         return rc < 0 ? rc : nr;
7890 }
7891
7892 /**
7893  * struct verify_mirror_id - Mirror id to be verified.
7894  * @mirror_id:   A specified mirror id.
7895  * @is_valid_id: @mirror_id is valid or not in the mirrored file.
7896  */
7897 struct verify_mirror_id {
7898         __u16 mirror_id;
7899         bool is_valid_id;
7900 };
7901
7902 /**
7903  * compare_mirror_ids() - Compare mirror ids.
7904  * @layout: Mirror component list.
7905  * @cbdata: Callback data in verify_mirror_id structure.
7906  *
7907  * This is a callback function called by llapi_layout_comp_iterate()
7908  * to compare the specified mirror id with the one in the current
7909  * component of @layout. If they are the same, then the specified
7910  * mirror id is valid.
7911  *
7912  * Return: a negative error code on failure or
7913  *         LLAPI_LAYOUT_ITER_CONT: Proceed iteration
7914  *         LLAPI_LAYOUT_ITER_STOP: Stop iteration
7915  */
7916 static inline
7917 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
7918 {
7919         struct verify_mirror_id *mirror_id_cbdata =
7920                                  (struct verify_mirror_id *)cbdata;
7921         uint32_t mirror_id;
7922         int rc = 0;
7923
7924         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
7925         if (rc < 0) {
7926                 rc = -errno;
7927                 fprintf(stderr,
7928                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
7929                         progname, strerror(errno));
7930                 return rc;
7931         }
7932
7933         if (mirror_id_cbdata->mirror_id == mirror_id) {
7934                 mirror_id_cbdata->is_valid_id = true;
7935                 return LLAPI_LAYOUT_ITER_STOP;
7936         }
7937
7938         return LLAPI_LAYOUT_ITER_CONT;
7939 }
7940
7941 /**
7942  * verify_mirror_ids() - Verify specified mirror ids.
7943  * @fname:      Mirrored file name.
7944  * @mirror_ids: Specified mirror ids to be verified.
7945  * @ids_nr:     Number of specified mirror ids.
7946  *
7947  * This function verifies that specified @mirror_ids are valid
7948  * in the mirrored file @fname.
7949  *
7950  * Return: 0 on success or a negative error code on failure.
7951  */
7952 static inline
7953 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
7954 {
7955         struct llapi_layout *layout = NULL;
7956         struct verify_mirror_id mirror_id_cbdata = { 0 };
7957         struct stat stbuf;
7958         uint32_t flr_state;
7959         int i;
7960         int fd;
7961         int rc = 0;
7962         int rc2 = 0;
7963
7964         if (ids_nr <= 0)
7965                 return -EINVAL;
7966
7967         if (stat(fname, &stbuf) < 0) {
7968                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
7969                         progname, fname, strerror(errno));
7970                 rc = -errno;
7971                 goto error;
7972         }
7973
7974         if (!S_ISREG(stbuf.st_mode)) {
7975                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
7976                         progname, fname);
7977                 rc = -EINVAL;
7978                 goto error;
7979         }
7980
7981         fd = open(fname, O_DIRECT | O_RDONLY);
7982         if (fd < 0) {
7983                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
7984                         progname, fname, strerror(errno));
7985                 rc = -errno;
7986                 goto error;
7987         }
7988
7989         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
7990         if (rc < 0) {
7991                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
7992                         progname, fname, strerror(errno));
7993                 goto close_fd;
7994         }
7995
7996         layout = llapi_layout_get_by_fd(fd, 0);
7997         if (layout == NULL) {
7998                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
7999                         progname, fname, strerror(errno));
8000                 rc = -errno;
8001                 llapi_lease_release(fd);
8002                 goto close_fd;
8003         }
8004
8005         rc = llapi_layout_flags_get(layout, &flr_state);
8006         if (rc < 0) {
8007                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8008                         progname, fname, strerror(errno));
8009                 rc = -errno;
8010                 goto free_layout;
8011         }
8012
8013         flr_state &= LCM_FL_FLR_MASK;
8014         switch (flr_state) {
8015         case LCM_FL_NONE:
8016                 rc = -EINVAL;
8017                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8018                         progname, fname, llapi_layout_flags_string(flr_state));
8019                 goto free_layout;
8020         default:
8021                 break;
8022         }
8023
8024         rc2 = 0;
8025         for (i = 0; i < ids_nr; i++) {
8026                 mirror_id_cbdata.mirror_id = mirror_ids[i];
8027                 mirror_id_cbdata.is_valid_id = false;
8028
8029                 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
8030                                                &mirror_id_cbdata);
8031                 if (rc < 0) {
8032                         rc = -errno;
8033                         fprintf(stderr,
8034                                 "%s: '%s' failed to verify mirror id: %u.\n",
8035                                 progname, fname, mirror_ids[i]);
8036                         goto free_layout;
8037                 }
8038
8039                 if (!mirror_id_cbdata.is_valid_id) {
8040                         rc2 = -EINVAL;
8041                         fprintf(stderr,
8042                                 "%s: '%s' invalid specified mirror id: %u.\n",
8043                                 progname, fname, mirror_ids[i]);
8044                 }
8045         }
8046         rc = rc2;
8047
8048 free_layout:
8049         llapi_layout_free(layout);
8050         llapi_lease_release(fd);
8051 close_fd:
8052         close(fd);
8053 error:
8054         return rc;
8055 }
8056
8057 static inline
8058 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
8059                            __u16 *mirror_ids, int ids_nr)
8060 {
8061         struct llapi_resync_comp comp_array[1024] = { { 0 } };
8062         struct llapi_layout *layout;
8063         struct stat stbuf;
8064         uint32_t flr_state;
8065         uint64_t start;
8066         uint64_t end;
8067         int comp_size = 0;
8068         int idx;
8069         int fd;
8070         int rc;
8071
8072         if (stat(fname, &stbuf) < 0) {
8073                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8074                         progname, fname, strerror(errno));
8075                 rc = -errno;
8076                 goto error;
8077         }
8078         if (!S_ISREG(stbuf.st_mode)) {
8079                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8080                         progname, fname);
8081                 rc = -EINVAL;
8082                 goto error;
8083         }
8084
8085         fd = open(fname, O_DIRECT | O_RDWR);
8086         if (fd < 0) {
8087                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8088                         progname, fname, strerror(errno));
8089                 rc = -errno;
8090                 goto error;
8091         }
8092
8093         layout = llapi_layout_get_by_fd(fd, 0);
8094         if (layout == NULL) {
8095                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8096                         progname, fname, strerror(errno));
8097                 rc = -errno;
8098                 goto close_fd;
8099         }
8100
8101         rc = llapi_layout_flags_get(layout, &flr_state);
8102         if (rc) {
8103                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8104                         progname, fname, strerror(errno));
8105                 rc = -errno;
8106                 goto free_layout;
8107         }
8108
8109         flr_state &= LCM_FL_FLR_MASK;
8110         if (flr_state == LCM_FL_NONE) {
8111                 rc = -EINVAL;
8112                 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
8113                         progname, fname);
8114                 goto free_layout;
8115         }
8116
8117         /* get stale component info */
8118         comp_size = llapi_mirror_find_stale(layout, comp_array,
8119                                             ARRAY_SIZE(comp_array),
8120                                             mirror_ids, ids_nr);
8121         if (comp_size <= 0) {
8122                 rc = comp_size;
8123                 goto free_layout;
8124         }
8125
8126         ioc->lil_mode = LL_LEASE_WRLCK;
8127         ioc->lil_flags = LL_LEASE_RESYNC;
8128         rc = llapi_lease_set(fd, ioc);
8129         if (rc < 0) {
8130                 if (rc == -EALREADY)
8131                         rc = 0;
8132                 else
8133                         fprintf(stderr,
8134                             "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
8135                                 progname, fname, strerror(errno));
8136                 goto free_layout;
8137         }
8138
8139         /* get the read range [start, end) */
8140         start = comp_array[0].lrc_start;
8141         end = comp_array[0].lrc_end;
8142         for (idx = 1; idx < comp_size; idx++) {
8143                 if (comp_array[idx].lrc_start < start)
8144                         start = comp_array[idx].lrc_start;
8145                 if (end < comp_array[idx].lrc_end)
8146                         end = comp_array[idx].lrc_end;
8147         }
8148
8149         rc = llapi_lease_check(fd);
8150         if (rc != LL_LEASE_WRLCK) {
8151                 fprintf(stderr, "%s: '%s' lost lease lock.\n",
8152                         progname, fname);
8153                 goto free_layout;
8154         }
8155
8156         rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
8157                                       start, end);
8158         if (rc < 0)
8159                 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %d.\n",
8160                         progname, fname, rc);
8161
8162         /* prepare ioc for lease put */
8163         ioc->lil_mode = LL_LEASE_UNLCK;
8164         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
8165         ioc->lil_count = 0;
8166         for (idx = 0; idx < comp_size; idx++) {
8167                 if (comp_array[idx].lrc_synced) {
8168                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
8169                         ioc->lil_count++;
8170                 }
8171         }
8172
8173         rc = llapi_lease_set(fd, ioc);
8174         if (rc <= 0) {
8175                 if (rc == 0) /* lost lease lock */
8176                         rc = -EBUSY;
8177                 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
8178                         progname, fname, strerror(errno));
8179                 goto free_layout;
8180         }
8181         /**
8182          * llapi_lease_set returns lease mode when it request to unlock
8183          * the lease lock
8184          */
8185         rc = 0;
8186
8187 free_layout:
8188         llapi_layout_free(layout);
8189 close_fd:
8190         close(fd);
8191 error:
8192         return rc;
8193 }
8194
8195 static inline int lfs_mirror_resync(int argc, char **argv)
8196 {
8197         struct ll_ioc_lease *ioc = NULL;
8198         __u16 mirror_ids[128] = { 0 };
8199         int ids_nr = 0;
8200         int c;
8201         int rc = 0;
8202
8203         struct option long_opts[] = {
8204         { .val = 'o',   .name = "only",         .has_arg = required_argument },
8205         { .name = NULL } };
8206
8207         while ((c = getopt_long(argc, argv, "o:", long_opts, NULL)) >= 0) {
8208                 switch (c) {
8209                 case 'o':
8210                         rc = parse_mirror_ids(mirror_ids,
8211                                         sizeof(mirror_ids) / sizeof(__u16),
8212                                         optarg);
8213                         if (rc < 0) {
8214                                 fprintf(stderr,
8215                                         "%s: bad mirror ids '%s'.\n",
8216                                         argv[0], optarg);
8217                                 goto error;
8218                         }
8219                         ids_nr = rc;
8220                         break;
8221                 default:
8222                         fprintf(stderr, "%s: options '%s' unrecognized.\n",
8223                                 argv[0], argv[optind - 1]);
8224                         rc = -EINVAL;
8225                         goto error;
8226                 }
8227         }
8228
8229         if (argc == optind) {
8230                 fprintf(stderr, "%s: no file name given.\n", argv[0]);
8231                 rc = CMD_HELP;
8232                 goto error;
8233         }
8234
8235         if (ids_nr > 0 && argc > optind + 1) {
8236                 fprintf(stderr,
8237                     "%s: option '--only' cannot be used upon multiple files.\n",
8238                         argv[0]);
8239                 rc = CMD_HELP;
8240                 goto error;
8241
8242         }
8243
8244         if (ids_nr > 0) {
8245                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
8246                 if (rc < 0)
8247                         goto error;
8248         }
8249
8250         /* set the lease on the file */
8251         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
8252         if (ioc == NULL) {
8253                 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
8254                         argv[0], strerror(errno));
8255                 rc = -errno;
8256                 goto error;
8257         }
8258
8259         for (; optind < argc; optind++) {
8260                 rc = lfs_mirror_resync_file(argv[optind], ioc,
8261                                             mirror_ids, ids_nr);
8262                 /* ignore previous file's error, continue with next file */
8263
8264                 /* reset ioc */
8265                 memset(ioc, 0, sizeof(__u32) * 4096);
8266         }
8267
8268         free(ioc);
8269 error:
8270         return rc;
8271 }
8272
8273 static inline int lfs_mirror_dump(int argc, char **argv)
8274 {
8275         int rc = CMD_HELP;
8276         __u16 mirror_id = 0;
8277         const char *outfile = NULL;
8278         char *fname;
8279         struct stat stbuf;
8280         int fd = 0;
8281         struct llapi_layout *layout;
8282         int outfd;
8283         int c;
8284         const size_t buflen = 4 << 20;
8285         void *buf;
8286         off_t pos;
8287
8288         struct option long_opts[] = {
8289         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
8290         { .val = 'o',   .name = "outfile",      .has_arg = required_argument },
8291         { .name = NULL } };
8292
8293         while ((c = getopt_long(argc, argv, "N:o:", long_opts, NULL)) >= 0) {
8294                 char *end;
8295
8296                 switch (c) {
8297                 case 'N':
8298                         mirror_id = strtoul(optarg, &end, 0);
8299                         if (*end != '\0' || mirror_id == 0) {
8300                                 fprintf(stderr,
8301                                         "%s %s: invalid mirror ID '%s'\n",
8302                                         progname, argv[0], optarg);
8303                                 return rc;
8304                         }
8305                         break;
8306                 case 'o':
8307                         outfile = optarg;
8308                         break;
8309                 }
8310         }
8311
8312         if (argc == optind) {
8313                 fprintf(stderr, "%s %s: no mirrored file provided\n",
8314                         progname, argv[0]);
8315                 return rc;
8316         } else if (argc > optind + 1) {
8317                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
8318                 return rc;
8319         }
8320
8321         if (mirror_id == 0) {
8322                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
8323                         progname, argv[0]);
8324                 return rc;
8325         }
8326
8327         /* open mirror file */
8328         fname = argv[optind];
8329
8330         if (stat(fname, &stbuf) < 0) {
8331                 fprintf(stderr, "%s %s: cannot stat file '%s': %s.\n",
8332                         progname, argv[0], fname, strerror(errno));
8333                 return rc;
8334         }
8335
8336         if (!S_ISREG(stbuf.st_mode)) {
8337                 fprintf(stderr, "%s %s: '%s' is not a regular file.\n",
8338                         progname, argv[0], fname);
8339                 return rc;
8340         }
8341
8342         fd = open(fname, O_DIRECT | O_RDONLY);
8343         if (fd < 0) {
8344                 fprintf(stderr, "%s %s: cannot open '%s': %s.\n",
8345                         progname, argv[0], fname, strerror(errno));
8346                 return rc;
8347         }
8348
8349         /* verify mirror id */
8350         layout = llapi_layout_get_by_fd(fd, 0);
8351         if (layout == NULL) {
8352                 fprintf(stderr, "%s %s: could not get layout of file '%s'.\n",
8353                         progname, argv[0], fname);
8354                 rc = -EINVAL;
8355                 goto close_fd;
8356         }
8357
8358         rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
8359         if (rc < 0) {
8360                 fprintf(stderr, "%s %s: failed to iterate layout of '%s'.\n",
8361                         progname, argv[0], fname);
8362                 llapi_layout_free(layout);
8363                 goto close_fd;
8364         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
8365                 fprintf(stderr,
8366                         "%s %s: file '%s' does not contain mirror with ID %u\n",
8367                         progname, argv[0], fname, mirror_id);
8368                 llapi_layout_free(layout);
8369
8370                 rc = -EINVAL;
8371                 goto close_fd;
8372         }
8373         llapi_layout_free(layout);
8374
8375         /* open output file */
8376         if (outfile) {
8377                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
8378                 if (outfd < 0) {
8379                         fprintf(stderr, "%s %s: cannot create file '%s':%s.\n",
8380                                 progname, argv[0], outfile, strerror(errno));
8381                         rc = -errno;
8382                         goto close_fd;
8383                 }
8384         } else {
8385                 outfd = STDOUT_FILENO;
8386         }
8387
8388         /* allocate buffer */
8389         rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
8390         if (rc) {
8391                 fprintf(stderr, "%s %s: posix_memalign returns %d.\n",
8392                                 progname, argv[0], rc);
8393                 goto close_outfd;
8394         }
8395
8396         pos = 0;
8397         while (1) {
8398                 ssize_t bytes_read;
8399                 ssize_t written = 0;
8400
8401                 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
8402                 if (bytes_read < 0) {
8403                         rc = bytes_read;
8404                         fprintf(stderr,
8405                                 "%s %s: fail to read data from mirror %u:%s.\n",
8406                                 progname, argv[0], mirror_id, strerror(-rc));
8407                         goto free_buf;
8408                 }
8409
8410                 /* EOF reached */
8411                 if (bytes_read == 0)
8412                         break;
8413
8414                 while (written < bytes_read) {
8415                         ssize_t written2;
8416
8417                         written2 = write(outfd, buf + written,
8418                                          bytes_read - written);
8419                         if (written2 < 0) {
8420                                 fprintf(stderr,
8421                                         "%s %s: fail to write %s:%s.\n",
8422                                         progname, argv[0], outfile ? : "STDOUT",
8423                                         strerror(errno));
8424                                 rc = -errno;
8425                                 goto free_buf;
8426                         }
8427                         written += written2;
8428                 }
8429
8430                 if (written != bytes_read) {
8431                         fprintf(stderr,
8432                 "%s %s: written %ld bytes does not match with %ld read.\n",
8433                                 progname, argv[0], written, bytes_read);
8434                         rc = -EIO;
8435                         goto free_buf;
8436                 }
8437
8438                 pos += bytes_read;
8439         }
8440
8441         fsync(outfd);
8442         rc = 0;
8443
8444 free_buf:
8445         free(buf);
8446 close_outfd:
8447         if (outfile)
8448                 close(outfd);
8449 close_fd:
8450         close(fd);
8451
8452         return rc;
8453 }
8454
8455 /**
8456  * struct verify_chunk - Mirror chunk to be verified.
8457  * @chunk:        [start, end) of the chunk.
8458  * @mirror_count: Number of mirror ids in @mirror_id array.
8459  * @mirror_id:    Array of valid mirror ids that cover the chunk.
8460  */
8461 struct verify_chunk {
8462         struct lu_extent chunk;
8463         unsigned int mirror_count;
8464         __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
8465 };
8466
8467 /**
8468  * print_chunks() - Print chunk information.
8469  * @fname:       Mirrored file name.
8470  * @chunks:      Array of chunks.
8471  * @chunk_count: Number of chunks in @chunks array.
8472  *
8473  * This function prints [start, end) of each chunk in @chunks
8474  * for mirrored file @fname, and also prints the valid mirror ids
8475  * that cover the chunk.
8476  *
8477  * Return: void.
8478  */
8479 static inline
8480 void print_chunks(const char *fname, struct verify_chunk *chunks,
8481                   int chunk_count)
8482 {
8483         int i;
8484         int j;
8485
8486         fprintf(stdout, "Chunks to be verified in %s:\n", fname);
8487         for (i = 0; i < chunk_count; i++) {
8488                 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
8489
8490                 if (chunks[i].mirror_count == 0)
8491                         fprintf(stdout, "\t[");
8492                 else {
8493                         fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
8494                         for (j = 1; j < chunks[i].mirror_count; j++)
8495                                 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
8496                 }
8497                 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
8498         }
8499         fprintf(stdout, "\n");
8500 }
8501
8502 /**
8503  * print_checksums() - Print CRC-32 checksum values.
8504  * @chunk: A chunk and its corresponding valid mirror ids.
8505  * @crc:   CRC-32 checksum values on the chunk for each valid mirror.
8506  *
8507  * This function prints CRC-32 checksum values on @chunk for
8508  * each valid mirror that covers it.
8509  *
8510  * Return: void.
8511  */
8512 static inline
8513 void print_checksums(struct verify_chunk *chunk, unsigned long *crc)
8514 {
8515         int i;
8516
8517         fprintf(stdout,
8518                 "CRC-32 checksum value for chunk "DEXT":\n",
8519                 PEXT(&chunk->chunk));
8520         for (i = 0; i < chunk->mirror_count; i++)
8521                 fprintf(stdout, "Mirror %u:\t%#lx\n",
8522                         chunk->mirror_id[i], crc[i]);
8523         fprintf(stdout, "\n");
8524 }
8525
8526 /**
8527  * filter_mirror_id() - Filter specified mirror ids.
8528  * @chunks:      Array of chunks.
8529  * @chunk_count: Number of chunks in @chunks array.
8530  * @mirror_ids:  Specified mirror ids to be verified.
8531  * @ids_nr:      Number of specified mirror ids.
8532  *
8533  * This function scans valid mirror ids that cover each chunk in @chunks
8534  * and filters specified mirror ids.
8535  *
8536  * Return: void.
8537  */
8538 static inline
8539 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
8540                       __u16 *mirror_ids, int ids_nr)
8541 {
8542         int i;
8543         int j;
8544         int k;
8545         __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8546         unsigned int valid_count = 0;
8547
8548         for (i = 0; i < chunk_count; i++) {
8549                 if (chunks[i].mirror_count == 0)
8550                         continue;
8551
8552                 valid_count = 0;
8553                 for (j = 0; j < ids_nr; j++) {
8554                         for (k = 0; k < chunks[i].mirror_count; k++) {
8555                                 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
8556                                         valid_id[valid_count] = mirror_ids[j];
8557                                         valid_count++;
8558                                         break;
8559                                 }
8560                         }
8561                 }
8562
8563                 memcpy(chunks[i].mirror_id, valid_id,
8564                        sizeof(__u16) * valid_count);
8565                 chunks[i].mirror_count = valid_count;
8566         }
8567 }
8568
8569 /**
8570  * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
8571  * @layout:      Mirror component list.
8572  * @chunks:      Array of chunks.
8573  * @chunks_size: Array size of @chunks.
8574  *
8575  * This function scans the components in @layout from offset 0 to LUSTRE_EOF
8576  * to find out chunk segments and store them in @chunks array.
8577  *
8578  * The @mirror_id array in each element of @chunks will store the valid
8579  * mirror ids that cover the chunk. If a mirror component covering the
8580  * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
8581  * will not be stored into the @mirror_id array, and the chunk for that
8582  * mirror will not be verified.
8583  *
8584  * The @mirror_count in each element of @chunks will store the number of
8585  * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
8586  * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
8587  * indicates the chunk is valid in only one mirror. In both cases, the
8588  * chunk will not be verified.
8589  *
8590  * Here is an example:
8591  *
8592  *  0      1M     2M     3M     4M           EOF
8593  *  +------+-------------+--------------------+
8594  *  |      |             |      S             |       mirror1
8595  *  +------+------+------+------+-------------+
8596  *  |             |   S  |   S  |             |       mirror2
8597  *  +-------------+------+------+-------------+
8598  *
8599  * prepared @chunks array will contain 5 elements:
8600  * (([0, 1M), [1, 2], 2),
8601  *  ([1M, 2M), [1, 2], 2),
8602  *  ([2M, 3M), [1], 1),
8603  *  ([3M, 4M], [], 0),
8604  *  ([4M, EOF), [2], 1))
8605  *
8606  * Return: the actual array size of @chunks on success
8607  *         or a negative error code on failure.
8608  */
8609 static inline
8610 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
8611                              struct verify_chunk *chunks,
8612                              size_t chunks_size)
8613 {
8614         uint64_t start;
8615         uint64_t end;
8616         uint32_t mirror_id;
8617         uint32_t flags;
8618         int idx = 0;
8619         int i = 0;
8620         int rc = 0;
8621
8622         memset(chunks, 0, sizeof(*chunks) * chunks_size);
8623
8624         while (1) {
8625                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
8626                 if (rc < 0) {
8627                         fprintf(stderr,
8628                                 "%s: move to the first layout component: %s.\n",
8629                                 progname, strerror(errno));
8630                         goto error;
8631                 }
8632
8633                 i = 0;
8634                 rc = 0;
8635                 chunks[idx].chunk.e_end = LUSTRE_EOF;
8636                 while (rc == 0) {
8637                         rc = llapi_layout_comp_extent_get(layout, &start, &end);
8638                         if (rc < 0) {
8639                                 fprintf(stderr,
8640                                         "%s: llapi_layout_comp_extent_get failed: %s.\n",
8641                                         progname, strerror(errno));
8642                                 goto error;
8643                         }
8644
8645                         if (start > chunks[idx].chunk.e_start ||
8646                             end <= chunks[idx].chunk.e_start)
8647                                 goto next;
8648
8649                         if (end < chunks[idx].chunk.e_end)
8650                                 chunks[idx].chunk.e_end = end;
8651
8652                         rc = llapi_layout_comp_flags_get(layout, &flags);
8653                         if (rc < 0) {
8654                                 fprintf(stderr,
8655                                         "%s: llapi_layout_comp_flags_get failed: %s.\n",
8656                                         progname, strerror(errno));
8657                                 goto error;
8658                         }
8659
8660                         if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
8661                                 goto next;
8662
8663                         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
8664                         if (rc < 0) {
8665                                 fprintf(stderr,
8666                                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
8667                                         progname, strerror(errno));
8668                                 goto error;
8669                         }
8670
8671                         chunks[idx].mirror_id[i] = mirror_id;
8672                         i++;
8673                         if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
8674                                 fprintf(stderr,
8675                                         "%s: mirror_id array is too small.\n",
8676                                         progname);
8677                                 rc = -EINVAL;
8678                                 goto error;
8679                         }
8680
8681                 next:
8682                         rc = llapi_layout_comp_use(layout,
8683                                                    LLAPI_LAYOUT_COMP_USE_NEXT);
8684                         if (rc < 0) {
8685                                 fprintf(stderr,
8686                                         "%s: move to the next layout component: %s.\n",
8687                                         progname, strerror(errno));
8688                                 goto error;
8689                         }
8690                 } /* loop through all components */
8691
8692                 chunks[idx].mirror_count = i;
8693
8694                 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
8695                         break;
8696
8697                 idx++;
8698                 if (idx >= chunks_size) {
8699                         fprintf(stderr, "%s: chunks array is too small.\n",
8700                                 progname);
8701                         rc = -EINVAL;
8702                         goto error;
8703                 }
8704
8705                 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
8706         }
8707
8708 error:
8709         return rc < 0 ? rc : idx + 1;
8710 }
8711
8712 /**
8713  * lfs_mirror_verify_chunk() - Verify a chunk.
8714  * @fd:        File descriptor of the mirrored file.
8715  * @file_size: Size of the mirrored file.
8716  * @chunk:     A chunk and its corresponding valid mirror ids.
8717  * @verbose:   Verbose mode.
8718  *
8719  * This function verifies a @chunk contains exactly the same data
8720  * ammong the mirrors that cover it.
8721  *
8722  * If @verbose is specified, then the function will print where the
8723  * differences are if the data do not match. Otherwise, it will
8724  * just return an error in that case.
8725  *
8726  * Return: 0 on success or a negative error code on failure.
8727  */
8728 static inline
8729 int lfs_mirror_verify_chunk(int fd, size_t file_size,
8730                             struct verify_chunk *chunk, int verbose)
8731 {
8732         const size_t buflen = 4 * 1024 * 1024; /* 4M */
8733         void *buf;
8734         size_t page_size = sysconf(_SC_PAGESIZE);
8735         ssize_t bytes_read;
8736         ssize_t bytes_done;
8737         size_t count;
8738         off_t pos;
8739         unsigned long crc;
8740         unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
8741         int i;
8742         int rc = 0;
8743
8744         if (file_size == 0)
8745                 return 0;
8746
8747         rc = posix_memalign(&buf, page_size, buflen);
8748         if (rc) /* error code is returned directly */
8749                 return -rc;
8750
8751         if (verbose > 1) {
8752                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8753                         PEXT(&chunk->chunk));
8754                 for (i = 0; i < chunk->mirror_count; i++)
8755                         fprintf(stdout, " %u", chunk->mirror_id[i]);
8756                 fprintf(stdout, "\n");
8757         }
8758
8759         bytes_done = 0;
8760         count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
8761         pos = chunk->chunk.e_start;
8762         while (bytes_done < count) {
8763                 /* compute initial CRC-32 checksum */
8764                 crc = crc32(0L, Z_NULL, 0);
8765                 memset(crc_array, 0, sizeof(crc_array));
8766
8767                 bytes_read = 0;
8768                 for (i = 0; i < chunk->mirror_count; i++) {
8769                         bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
8770                                                        buf, buflen, pos);
8771                         if (bytes_read < 0) {
8772                                 rc = bytes_read;
8773                                 fprintf(stderr,
8774                                         "%s: failed to read data from mirror %u: %s.\n",
8775                                         progname, chunk->mirror_id[i],
8776                                         strerror(-rc));
8777                                 goto error;
8778                         }
8779
8780                         /* compute new CRC-32 checksum */
8781                         crc_array[i] = crc32(crc, buf, bytes_read);
8782                 }
8783
8784                 if (verbose)
8785                         print_checksums(chunk, crc_array);
8786
8787                 /* compare CRC-32 checksum values */
8788                 for (i = 1; i < chunk->mirror_count; i++) {
8789                         if (crc_array[i] != crc_array[0]) {
8790                                 rc = -EINVAL;
8791                                 if (!verbose)
8792                                         goto error;
8793
8794                                 fprintf(stderr,
8795                                         "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
8796                                         progname, PEXT(&chunk->chunk),
8797                                         chunk->mirror_id[0],
8798                                         chunk->mirror_id[i]);
8799                         }
8800                 }
8801
8802                 pos += bytes_read;
8803                 bytes_done += bytes_read;
8804         }
8805
8806         if (verbose > 1 && rc == 0) {
8807                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
8808                         PEXT(&chunk->chunk));
8809                 for (i = 0; i < chunk->mirror_count; i++)
8810                         fprintf(stdout, " %u", chunk->mirror_id[i]);
8811                 fprintf(stdout, " PASS\n\n");
8812         }
8813
8814 error:
8815         free(buf);
8816         return rc;
8817 }
8818
8819 /**
8820  * lfs_mirror_verify_file() - Verify a mirrored file.
8821  * @fname:      Mirrored file name.
8822  * @mirror_ids: Specified mirror ids to be verified.
8823  * @ids_nr:     Number of specified mirror ids.
8824  * @verbose:    Verbose mode.
8825  *
8826  * This function verifies that each SYNC mirror of a mirrored file
8827  * specified by @fname contains exactly the same data.
8828  *
8829  * If @mirror_ids is specified, then the function will verify the
8830  * mirrors specified by @mirror_ids contain exactly the same data.
8831  *
8832  * If @verbose is specified, then the function will print where the
8833  * differences are if the data do not match. Otherwise, it will
8834  * just return an error in that case.
8835  *
8836  * Return: 0 on success or a negative error code on failure.
8837  */
8838 static inline
8839 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
8840                            int verbose)
8841 {
8842         struct verify_chunk chunks_array[1024] = { };
8843         struct llapi_layout *layout = NULL;
8844         struct stat stbuf;
8845         uint32_t flr_state;
8846         int fd;
8847         int chunk_count = 0;
8848         int idx = 0;
8849         int rc = 0;
8850         int rc1 = 0;
8851         int rc2 = 0;
8852
8853         if (stat(fname, &stbuf) < 0) {
8854                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
8855                         progname, fname, strerror(errno));
8856                 rc = -errno;
8857                 goto error;
8858         }
8859
8860         if (!S_ISREG(stbuf.st_mode)) {
8861                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
8862                         progname, fname);
8863                 rc = -EINVAL;
8864                 goto error;
8865         }
8866
8867         if (stbuf.st_size == 0) {
8868                 if (verbose)
8869                         fprintf(stdout, "%s: '%s' file size is 0.\n",
8870                                 progname, fname);
8871                 rc = 0;
8872                 goto error;
8873         }
8874
8875         fd = open(fname, O_DIRECT | O_RDONLY);
8876         if (fd < 0) {
8877                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
8878                         progname, fname, strerror(errno));
8879                 rc = -errno;
8880                 goto error;
8881         }
8882
8883         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
8884         if (rc < 0) {
8885                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
8886                         progname, fname, strerror(errno));
8887                 goto close_fd;
8888         }
8889
8890         layout = llapi_layout_get_by_fd(fd, 0);
8891         if (layout == NULL) {
8892                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
8893                         progname, fname, strerror(errno));
8894                 rc = -errno;
8895                 llapi_lease_release(fd);
8896                 goto close_fd;
8897         }
8898
8899         rc = llapi_layout_flags_get(layout, &flr_state);
8900         if (rc < 0) {
8901                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
8902                         progname, fname, strerror(errno));
8903                 rc = -errno;
8904                 goto free_layout;
8905         }
8906
8907         flr_state &= LCM_FL_FLR_MASK;
8908         switch (flr_state) {
8909         case LCM_FL_NONE:
8910                 rc = -EINVAL;
8911                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
8912                         progname, fname, llapi_layout_flags_string(flr_state));
8913                 goto free_layout;
8914         default:
8915                 break;
8916         }
8917
8918         /* find out mirror chunks to be verified */
8919         chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
8920                                                ARRAY_SIZE(chunks_array));
8921         if (chunk_count < 0) {
8922                 rc = chunk_count;
8923                 goto free_layout;
8924         }
8925
8926         if (ids_nr > 0)
8927                 /* filter specified mirror ids */
8928                 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
8929
8930         if (verbose > 2)
8931                 print_chunks(fname, chunks_array, chunk_count);
8932
8933         for (idx = 0; idx < chunk_count; idx++) {
8934                 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
8935                         if (verbose)
8936                                 fprintf(stdout,
8937                                         "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
8938                                         progname, fname,
8939                                         PEXT(&chunks_array[idx].chunk),
8940                                         (unsigned long long)stbuf.st_size);
8941                         break;
8942                 }
8943
8944                 if (chunks_array[idx].mirror_count == 0) {
8945                         fprintf(stderr,
8946                                 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
8947                                 progname, fname,
8948                                 PEXT(&chunks_array[idx].chunk));
8949                         if (verbose) {
8950                                 fprintf(stderr, "skipped\n");
8951                                 continue;
8952                         }
8953                         rc = -EINVAL;
8954                         fprintf(stderr, "failed\n");
8955                         goto free_layout;
8956                 }
8957
8958                 if (chunks_array[idx].mirror_count == 1) {
8959                         if (verbose)
8960                                 fprintf(stdout,
8961                                         "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
8962                                         progname, fname,
8963                                         PEXT(&chunks_array[idx].chunk),
8964                                         chunks_array[idx].mirror_id[0]);
8965                         continue;
8966                 }
8967
8968                 rc = llapi_lease_check(fd);
8969                 if (rc != LL_LEASE_RDLCK) {
8970                         fprintf(stderr, "%s: '%s' lost lease lock.\n",
8971                                 progname, fname);
8972                         goto free_layout;
8973                 }
8974
8975                 /* verify one chunk */
8976                 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
8977                                               &chunks_array[idx], verbose);
8978                 if (rc1 < 0) {
8979                         rc2 = rc1;
8980                         if (!verbose) {
8981                                 rc = rc1;
8982                                 goto free_layout;
8983                         }
8984                 }
8985         }
8986
8987         if (rc2 < 0)
8988                 rc = rc2;
8989
8990 free_layout:
8991         llapi_layout_free(layout);
8992         llapi_lease_release(fd);
8993 close_fd:
8994         close(fd);
8995 error:
8996         return rc;
8997 }
8998
8999 /**
9000  * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
9001  * @argc: The count of lfs mirror verify command line arguments.
9002  * @argv: Array of strings for lfs mirror verify command line arguments.
9003  *
9004  * This function parses lfs mirror verify command and verifies the
9005  * specified mirrored file(s).
9006  *
9007  * Return: 0 on success or a negative error code on failure.
9008  */
9009 static inline int lfs_mirror_verify(int argc, char **argv)
9010 {
9011         __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
9012         int ids_nr = 0;
9013         int c;
9014         int verbose = 0;
9015         int rc = 0;
9016         int rc1 = 0;
9017         char cmd[PATH_MAX];
9018
9019         struct option long_opts[] = {
9020         { .val = 'o',   .name = "only",         .has_arg = required_argument },
9021         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
9022         { .name = NULL } };
9023
9024         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9025         progname = cmd;
9026         while ((c = getopt_long(argc, argv, "o:v", long_opts, NULL)) >= 0) {
9027                 switch (c) {
9028                 case 'o':
9029                         rc = parse_mirror_ids(mirror_ids,
9030                                               ARRAY_SIZE(mirror_ids),
9031                                               optarg);
9032                         if (rc < 0) {
9033                                 fprintf(stderr,
9034                                         "%s: bad mirror ids '%s'.\n",
9035                                         progname, optarg);
9036                                 goto error;
9037                         }
9038                         ids_nr = rc;
9039                         if (ids_nr < 2) {
9040                                 fprintf(stderr,
9041                                         "%s: at least 2 mirror ids needed with '--only' option.\n",
9042                                         progname);
9043                                 rc = CMD_HELP;
9044                                 goto error;
9045                         }
9046                         break;
9047                 case 'v':
9048                         verbose++;
9049                         break;
9050                 default:
9051                         fprintf(stderr, "%s: options '%s' unrecognized.\n",
9052                                 progname, argv[optind - 1]);
9053                         rc = -EINVAL;
9054                         goto error;
9055                 }
9056         }
9057
9058         if (argc == optind) {
9059                 fprintf(stderr, "%s: no file name given.\n", progname);
9060                 rc = CMD_HELP;
9061                 goto error;
9062         }
9063
9064         if (ids_nr > 0 && argc > optind + 1) {
9065                 fprintf(stderr,
9066                         "%s: '--only' cannot be used upon multiple files.\n",
9067                         progname);
9068                 rc = CMD_HELP;
9069                 goto error;
9070
9071         }
9072
9073         if (ids_nr > 0) {
9074                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
9075                 if (rc < 0)
9076                         goto error;
9077         }
9078
9079         rc = 0;
9080         for (; optind < argc; optind++) {
9081                 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
9082                                              verbose);
9083                 if (rc1 < 0)
9084                         rc = rc1;
9085         }
9086 error:
9087         return rc;
9088 }
9089
9090 /**
9091  * lfs_mirror() - Parse and execute lfs mirror commands.
9092  * @argc: The count of lfs mirror command line arguments.
9093  * @argv: Array of strings for lfs mirror command line arguments.
9094  *
9095  * This function parses lfs mirror commands and performs the
9096  * corresponding functions specified in mirror_cmdlist[].
9097  *
9098  * Return: 0 on success or an error code on failure.
9099  */
9100 static int lfs_mirror(int argc, char **argv)
9101 {
9102         char cmd[PATH_MAX];
9103         int rc = 0;
9104
9105         setlinebuf(stdout);
9106
9107         Parser_init("lfs-mirror > ", mirror_cmdlist);
9108
9109         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
9110         progname = cmd;
9111         program_invocation_short_name = cmd;
9112         if (argc > 1)
9113                 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
9114         else
9115                 rc = Parser_commands();
9116
9117         return rc < 0 ? -rc : rc;
9118 }
9119
9120 static void lustre_som_swab(struct lustre_som_attrs *attrs)
9121 {
9122 #if __BYTE_ORDER == __BIG_ENDIAN
9123         __swab16s(&attrs->lsa_valid);
9124         __swab64s(&attrs->lsa_size);
9125         __swab64s(&attrs->lsa_blocks);
9126 #endif
9127 }
9128
9129 enum lfs_som_type {
9130         LFS_SOM_SIZE = 0x1,
9131         LFS_SOM_BLOCKS = 0x2,
9132         LFS_SOM_FLAGS = 0x4,
9133         LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
9134                            LFS_SOM_FLAGS,
9135 };
9136
9137 static int lfs_getsom(int argc, char **argv)
9138 {
9139         const char *path;
9140         struct lustre_som_attrs *attrs;
9141         char buf[sizeof(*attrs) + 64];
9142         enum lfs_som_type type = LFS_SOM_ATTR_ALL;
9143         int rc = 0, c;
9144
9145         while ((c = getopt(argc, argv, "sbf")) != -1) {
9146                 switch (c) {
9147                 case 's':
9148                         type = LFS_SOM_SIZE;
9149                         break;
9150                 case 'b':
9151                         type = LFS_SOM_BLOCKS;
9152                         break;
9153                 case 'f':
9154                         type = LFS_SOM_FLAGS;
9155                         break;
9156                 default:
9157                         fprintf(stderr, "%s: invalid option '%c'\n",
9158                                 progname, optopt);
9159                         return CMD_HELP;
9160                 }
9161         }
9162
9163         argc -= optind;
9164         argv += optind;
9165
9166         if (argc != 1) {
9167                 fprintf(stderr, "%s: %s\n",
9168                         progname, argc == 0 ? "miss file target" :
9169                         "input more than 2 files");
9170                 return CMD_HELP;
9171         }
9172
9173         path = argv[0];
9174         attrs = (void *)buf;
9175         rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
9176         if (rc < 0) {
9177                 fprintf(stderr, "%s failed to get som xattr: %s\n", argv[0],
9178                         strerror(-rc));
9179                 return rc;
9180         }
9181
9182         lustre_som_swab(attrs);
9183
9184         switch (type) {
9185         case LFS_SOM_ATTR_ALL:
9186                 printf("file: %s size: %llu blocks: %llu flags: %x\n",
9187                        path, attrs->lsa_size, attrs->lsa_blocks,
9188                        attrs->lsa_valid);
9189                 break;
9190         case LFS_SOM_SIZE:
9191                 printf("%llu\n", attrs->lsa_size);
9192                 break;
9193         case LFS_SOM_BLOCKS:
9194                 printf("%llu\n", attrs->lsa_blocks);
9195                 break;
9196         case LFS_SOM_FLAGS:
9197                 printf("%x\n", attrs->lsa_valid);
9198                 break;
9199         default:
9200                 fprintf(stderr, "%s: unknown option\n", progname);
9201                 return CMD_HELP;
9202         }
9203
9204         return rc;
9205 }
9206
9207 /**
9208  * lfs_mirror_list_commands() - List lfs mirror commands.
9209  * @argc: The count of command line arguments.
9210  * @argv: Array of strings for command line arguments.
9211  *
9212  * This function lists lfs mirror commands defined in mirror_cmdlist[].
9213  *
9214  * Return: 0 on success.
9215  */
9216 static int lfs_mirror_list_commands(int argc, char **argv)
9217 {
9218         char buffer[81] = "";
9219
9220         Parser_list_commands(mirror_cmdlist, buffer, sizeof(buffer),
9221                              NULL, 0, 4);
9222
9223         return 0;
9224 }
9225
9226 static int lfs_list_commands(int argc, char **argv)
9227 {
9228         char buffer[81] = ""; /* 80 printable chars + terminating NUL */
9229
9230         Parser_list_commands(cmdlist, buffer, sizeof(buffer), NULL, 0, 4);
9231
9232         return 0;
9233 }
9234
9235 int main(int argc, char **argv)
9236 {
9237         int rc;
9238
9239         /* Ensure that liblustreapi constructor has run */
9240         if (!llapi_liblustreapi_initialized())
9241                 fprintf(stderr, "liblustreapi was not properly initialized\n");
9242
9243         setlinebuf(stdout);
9244         opterr = 0;
9245
9246         Parser_init("lfs > ", cmdlist);
9247
9248         progname = program_invocation_short_name; /* Used in error messages */
9249         if (argc > 1) {
9250                 llapi_set_command_name(argv[1]);
9251                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
9252                 llapi_clear_command_name();
9253         } else {
9254                 rc = Parser_commands();
9255         }
9256
9257         return rc < 0 ? -rc : rc;
9258 }
9259
9260 #ifdef _LUSTRE_IDL_H_
9261 /* Everything we need here should be included by lustreapi.h. */
9262 # error "lfs should not depend on lustre_idl.h"
9263 #endif /* _LUSTRE_IDL_H_ */