Whamcloud - gitweb
LU-16553 utils: cleanup lfs options
[fs/lustre-release.git] / lustre / utils / lfs.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2011, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/utils/lfs.c
32  *
33  * Author: Peter J. Braam <braam@clusterfs.com>
34  * Author: Phil Schwan <phil@clusterfs.com>
35  * Author: Robert Read <rread@clusterfs.com>
36  */
37
38 /* for O_DIRECTORY */
39 #ifndef _GNU_SOURCE
40 #define _GNU_SOURCE
41 #endif
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <inttypes.h>
46 #include <getopt.h>
47 #include <string.h>
48 #include <mntent.h>
49 #include <unistd.h>
50 #include <errno.h>
51 #include <err.h>
52 #include <pwd.h>
53 #include <grp.h>
54 #include <sys/ioctl.h>
55 #include <sys/quota.h>
56 #include <sys/time.h>
57 #include <sys/types.h>
58 #include <sys/stat.h>
59 #include <sys/param.h>
60 #include <sys/xattr.h>
61 #include <fcntl.h>
62 #include <dirent.h>
63 #include <time.h>
64 #include <ctype.h>
65 #include <zlib.h>
66 #include <libgen.h>
67 #include <asm/byteorder.h>
68 #include "lfs_project.h"
69
70 #include <libcfs/util/string.h>
71 #include <libcfs/util/ioctl.h>
72 #include <libcfs/util/parser.h>
73 #include <libcfs/util/string.h>
74 #include <lustre/lustreapi.h>
75 #include <linux/lustre/lustre_ver.h>
76 #include <linux/lustre/lustre_param.h>
77 #include <linux/lnet/nidstr.h>
78 #include <lnetconfig/cyaml.h>
79 #include "lstddef.h"
80
81 #ifndef NSEC_PER_SEC
82 # define NSEC_PER_SEC 1000000000UL
83 #endif
84 #define ONE_MB 0x100000
85
86 /* all functions */
87 static int lfs_find(int argc, char **argv);
88 static int lfs_getstripe(int argc, char **argv);
89 static int lfs_getdirstripe(int argc, char **argv);
90 static int lfs_setdirstripe(int argc, char **argv);
91 static int lfs_rmentry(int argc, char **argv);
92 static int lfs_unlink_foreign(int argc, char **argv);
93 static int lfs_osts(int argc, char **argv);
94 static int lfs_mdts(int argc, char **argv);
95 static int lfs_df(int argc, char **argv);
96 static int lfs_getname(int argc, char **argv);
97 static int lfs_check(int argc, char **argv);
98 #ifdef HAVE_SYS_QUOTA_H
99 static int lfs_setquota(int argc, char **argv);
100 static int lfs_quota(int argc, char **argv);
101 static int lfs_project(int argc, char **argv);
102 #endif
103 static int lfs_flushctx(int argc, char **argv);
104 static int lfs_poollist(int argc, char **argv);
105 static int lfs_changelog(int argc, char **argv);
106 static int lfs_changelog_clear(int argc, char **argv);
107 static int lfs_fid2path(int argc, char **argv);
108 static int lfs_path2fid(int argc, char **argv);
109 static int lfs_rmfid(int argc, char **argv);
110 static int lfs_data_version(int argc, char **argv);
111 static int lfs_hsm_state(int argc, char **argv);
112 static int lfs_hsm_set(int argc, char **argv);
113 static int lfs_hsm_clear(int argc, char **argv);
114 static int lfs_hsm_action(int argc, char **argv);
115 static int lfs_hsm_archive(int argc, char **argv);
116 static int lfs_hsm_restore(int argc, char **argv);
117 static int lfs_hsm_release(int argc, char **argv);
118 static int lfs_hsm_remove(int argc, char **argv);
119 static int lfs_hsm_cancel(int argc, char **argv);
120 static int lfs_swap_layouts(int argc, char **argv);
121 static int lfs_mv(int argc, char **argv);
122 static int lfs_ladvise(int argc, char **argv);
123 static int lfs_getsom(int argc, char **argv);
124 static int lfs_heat_get(int argc, char **argv);
125 static int lfs_heat_set(int argc, char **argv);
126 static int lfs_mirror(int argc, char **argv);
127 static int lfs_mirror_list_commands(int argc, char **argv);
128 static int lfs_list_commands(int argc, char **argv);
129 static inline int lfs_mirror_resync(int argc, char **argv);
130 static inline int lfs_mirror_verify(int argc, char **argv);
131 static inline int lfs_mirror_read(int argc, char **argv);
132 static inline int lfs_mirror_write(int argc, char **argv);
133 static inline int lfs_mirror_copy(int argc, char **argv);
134 static int lfs_pcc_attach(int argc, char **argv);
135 static int lfs_pcc_attach_fid(int argc, char **argv);
136 static int lfs_pcc_detach(int argc, char **argv);
137 static int lfs_pcc_detach_fid(int argc, char **argv);
138 static int lfs_pcc_state(int argc, char **argv);
139 static int lfs_pcc(int argc, char **argv);
140 static int lfs_pcc_list_commands(int argc, char **argv);
141
142 enum stats_flag {
143         STATS_ON,
144         STATS_OFF,
145 };
146
147 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
148                               __u64 migration_flags,
149                               unsigned long long bandwidth_bytes_sec,
150                               enum stats_flag stats_flag,
151                               long stats_interval_sec);
152
153 struct pool_to_id_cbdata {
154         const char *pool;
155         __u32 id;
156 };
157
158 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata);
159 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata);
160
161 enum setstripe_origin {
162         SO_SETSTRIPE,
163         SO_MIGRATE,
164         SO_MIGRATE_MDT,
165         SO_MIRROR_CREATE,
166         SO_MIRROR_EXTEND,
167         SO_MIRROR_SPLIT,
168         SO_MIRROR_DELETE,
169 };
170
171 static int lfs_setstripe_internal(int argc, char **argv,
172                                   enum setstripe_origin opc);
173
174 static inline int lfs_setstripe(int argc, char **argv)
175 {
176         return lfs_setstripe_internal(argc, argv, SO_SETSTRIPE);
177 }
178
179 static inline int lfs_setstripe_migrate(int argc, char **argv)
180 {
181         return lfs_setstripe_internal(argc, argv, SO_MIGRATE);
182 }
183
184 static inline int lfs_mirror_create(int argc, char **argv)
185 {
186         return lfs_setstripe_internal(argc, argv, SO_MIRROR_CREATE);
187 }
188
189 static inline int lfs_mirror_extend(int argc, char **argv)
190 {
191         return lfs_setstripe_internal(argc, argv, SO_MIRROR_EXTEND);
192 }
193
194 static inline int lfs_mirror_split(int argc, char **argv)
195 {
196         return lfs_setstripe_internal(argc, argv, SO_MIRROR_SPLIT);
197 }
198
199 static inline int lfs_mirror_delete(int argc, char **argv)
200 {
201         return lfs_setstripe_internal(argc, argv, SO_MIRROR_DELETE);
202 }
203
204 /* Setstripe and migrate share mostly the same parameters */
205 #define SSM_CMD_COMMON(cmd) \
206         "usage: "cmd" [--component-end|-E COMP_END]\n"                  \
207         "                 [--copy=LUSTRE_SRC]\n"                        \
208         "                 [--extension-size|--ext-size|-z SIZE]\n"      \
209         "                 [--help|-h] [--layout|-L PATTERN]\n"          \
210         "                 [--layout|-L PATTERN]\n"                      \
211         "                 [--mirror-count|-N[MIRROR_COUNT]]\n"          \
212         "                 [--ost|-o OST_INDICES]\n"                     \
213         "                 [--overstripe-count|-C STRIPE_COUNT]\n"       \
214         "                 [--pool|-p POOL_NAME]\n"                      \
215         "                 [--stripe-count|-c STRIPE_COUNT]\n"           \
216         "                 [--stripe-index|-i START_OST_IDX]\n"          \
217         "                 [--stripe-size|-S STRIPE_SIZE]\n"             \
218         "                 [--yaml|-y YAML_TEMPLATE_FILE]\n"
219
220 #define MIRROR_EXTEND_USAGE                                             \
221         "                 {--mirror-count|-N[MIRROR_COUNT]}\n"          \
222         "                 [SETSTRIPE_OPTIONS|-f|--file VICTIM_FILE]\n"  \
223         "                 [--no-verify]\n"
224
225 #define SETSTRIPE_USAGE                                                 \
226         SSM_CMD_COMMON("setstripe")                                     \
227         MIRROR_EXTEND_USAGE                                             \
228         "                 DIRECTORY|FILENAME\n"
229
230 #define MIGRATE_USAGE                                                   \
231         SSM_CMD_COMMON("migrate  ")                                     \
232         "                 [--block|-b] [--non-block|-n]\n"              \
233         "                 [--non-direct|-D] [--verbose|-v]\n"           \
234         "                 FILENAME\n"
235
236 #define SETDIRSTRIPE_USAGE                                              \
237         "               [--mdt-count|-c stripe_count>\n"                \
238         "               [--help|-h] [--mdt-hash|-H mdt_hash]\n"         \
239         "               [--mdt-index|-i mdt_index[,mdt_index,...]\n"    \
240         "               [--default|-D] [--mode|-o mode]\n"              \
241         "               [--max-inherit|-X max_inherit]\n"               \
242         "               [--max-inherit-rr max_inherit_rr] <dir>\n"      \
243         "To create dir with a foreign (free format) layout :\n"         \
244         "setdirstripe|mkdir --foreign[=FOREIGN_TYPE] -x|-xattr STRING " \
245         "               [--mode|-o MODE] [--flags HEX] DIRECTORY\n"
246
247 /**
248  * command_t mirror_cmdlist - lfs mirror commands.
249  */
250 command_t mirror_cmdlist[] = {
251         { .pc_name = "create", .pc_func = lfs_mirror_create,
252           .pc_help = "Create a mirrored file.\n"
253                 "usage: lfs mirror create --mirror-count|-N[MIRROR_COUNT]\n"
254                 "           [SETSTRIPE_OPTIONS] ... FILENAME|DIRECTORY ...\n" },
255         { .pc_name = "delete", .pc_func = lfs_mirror_delete,
256           .pc_help = "Delete a mirror from a file.\n"
257         "usage: lfs mirror delete {--mirror-id <mirror_id> |\n"
258         "\t               --component-id|--comp-id|-I COMP_ID |\n"
259         "\t               -p <pool>} MIRRORED_FILE ...\n"
260         },
261         { .pc_name = "extend", .pc_func = lfs_mirror_extend,
262           .pc_help = "Extend a mirrored file.\n"
263                 "usage: lfs mirror extend "
264                 "{--mirror-count|-N[MIRROR_COUNT]} [--no-verify] "
265                 "[SETSTRIPE_OPTIONS|-f VICTIM_FILE] ... FILENAME ...\n" },
266         { .pc_name = "split", .pc_func = lfs_mirror_split,
267           .pc_help = "Split a mirrored file.\n"
268         "usage: lfs mirror split {--mirror-id MIRROR_ID |\n"
269         "\t             --component-id|-I COMP_ID|-p POOL} [--destroy|-d]\n"
270         "\t             [-f NEW_FILE] MIRRORED_FILE ...\n" },
271         { .pc_name = "read", .pc_func = lfs_mirror_read,
272           .pc_help = "Read the content of a specified mirror of a file.\n"
273                 "usage: lfs mirror read {--mirror-id|-N MIRROR_ID}\n"
274                 "\t\t[--outfile|-o <output_file>] <mirrored_file>\n" },
275         { .pc_name = "write", .pc_func = lfs_mirror_write,
276           .pc_help = "Write to a specified mirror of a file.\n"
277                 "usage: lfs mirror write {--mirror-id|-N MIRROR_ID}\n"
278                 "\t\t[--inputfile|-i <input_file>] <mirrored_file>\n" },
279         { .pc_name = "copy", .pc_func = lfs_mirror_copy,
280           .pc_help = "Copy a specified mirror to other mirror(s) of a file.\n"
281                 "usage: lfs mirror copy {--read-mirror|-i MIRROR_ID0}\n"
282                 "\t\t{--write-mirror|-o MIRROR_ID1[,...]} <mirrored_file>\n" },
283         { .pc_name = "resync", .pc_func = lfs_mirror_resync,
284           .pc_help = "Resynchronizes out-of-sync mirrored file(s).\n"
285                 "usage: lfs mirror resync [--only MIRROR_ID[,...]>]\n"
286                 "\t\t<mirrored_file> [<mirrored_file2>...]\n" },
287         { .pc_name = "verify", .pc_func = lfs_mirror_verify,
288           .pc_help = "Verify mirrored file(s).\n"
289                 "usage: lfs mirror verify [--only MIRROR_ID[,...]]\n"
290                 "\t\t[--verbose|-v] <mirrored_file> [<mirrored_file2> ...]\n" },
291         { .pc_name = "--list-commands", .pc_func = lfs_mirror_list_commands,
292           .pc_help = "list commands supported by lfs mirror"},
293         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
294         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
295         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
296         { .pc_help = NULL }
297 };
298
299 /**
300  * command_t pcc_cmdlist - lfs pcc commands.
301  */
302 command_t pcc_cmdlist[] = {
303         { .pc_name = "attach", .pc_func = lfs_pcc_attach,
304           .pc_help = "Attach given files to the Persistent Client Cache.\n"
305                 "usage: lfs pcc attach <--id|-i NUM> <file> ...\n"
306                 "\t-i: archive id for RW-PCC\n" },
307         { .pc_name = "attach_fid", .pc_func = lfs_pcc_attach_fid,
308           .pc_help = "Attach given files into PCC by FID(s).\n"
309                 "usage: lfs pcc attach_id {--id|-i NUM} {--mnt|-m MOUNTPOINT} FID ...\n"
310                 "\t-i: archive id for RW-PCC\n"
311                 "\t-m: Lustre mount point\n" },
312         { .pc_name = "state", .pc_func = lfs_pcc_state,
313           .pc_help = "Display the PCC state for given files.\n"
314                 "usage: lfs pcc state <file> ...\n" },
315         { .pc_name = "detach", .pc_func = lfs_pcc_detach,
316           .pc_help = "Detach given files from the Persistent Client Cache.\n"
317                 "usage: lfs pcc detach <file> ...\n" },
318         { .pc_name = "detach_fid", .pc_func = lfs_pcc_detach_fid,
319           .pc_help = "Detach given files from PCC by FID(s).\n"
320                 "usage: lfs pcc detach_fid <mntpath> <fid>...\n" },
321         { .pc_name = "--list-commands", .pc_func = lfs_pcc_list_commands,
322           .pc_help = "list commands supported by lfs pcc"},
323         { .pc_name = "help", .pc_func = Parser_help, .pc_help = "help" },
324         { .pc_name = "exit", .pc_func = Parser_quit, .pc_help = "quit" },
325         { .pc_name = "quit", .pc_func = Parser_quit, .pc_help = "quit" },
326         { .pc_help = NULL }
327 };
328
329 /* all available commands */
330 command_t cmdlist[] = {
331         {"setstripe", lfs_setstripe, 0,
332          "To create a file with specified striping/composite layout, or\n"
333          "create/replace the default layout on an existing directory:\n"
334          SSM_CMD_COMMON("setstripe")
335          "                 [--mode MODE]\n"
336          "                 <directory|filename>\n"
337          " or\n"
338          "To add component(s) to an existing composite file:\n"
339          SSM_CMD_COMMON("setstripe --component-add")
340          "To totally delete the default striping from an existing directory:\n"
341          "usage: setstripe [--delete|-d] <directory>\n"
342          " or\n"
343          "To create a mirrored file or set s default mirror layout on a directory:\n"
344          "usage: setstripe {--mirror-count|-N}[MIRROR_COUNT] [SETSTRIPE_OPTIONS] <directory|filename>\n"
345          " or\n"
346          "To delete the last component(s) from an existing composite file\n"
347          "(note that this will also delete any data in those components):\n"
348          "usage: setstripe --component-del [--component-id|-I COMP_ID]\n"
349          "                               [--component-flags|-F COMP_FLAGS]\n"
350          "                               <filename>\n"
351          "\tCOMP_ID:     Unique component ID to delete\n"
352          "\tCOMP_FLAGS:  'init' indicating all instantiated components\n"
353          "\t             '^init' indicating all uninstantiated components\n"
354          "\t-I and -F cannot be specified at the same time\n"
355          " or\n"
356          "To set or clear flags on a specific component\n"
357          "(note that this command can only be applied to mirrored files:\n"
358          "usage: setstripe --comp-set {-I COMP_ID|--comp-flags=COMP_FLAGS}\n"
359          "                            <filename>\n"
360          " or\n"
361          "To create a file with a foreign (free format) layout:\n"
362          "usage: setstripe --foreign[=FOREIGN_TYPE]\n"
363          "                 --xattr|-x LAYOUT_STRING [--flags HEX]\n"
364          "                 [--mode MODE] <filename>\n"},
365         {"getstripe", lfs_getstripe, 0,
366          "To list the layout pattern for a given file or files in a\n"
367          "directory or recursively for all files in a directory tree.\n"
368          "usage: getstripe [--ost|-O UUID] [--quiet|-q] [--verbose|-v]\n"
369          "                 [--stripe-count|-c] [--stripe-index|-i] [--fid|-F]\n"
370          "                 [--pool|-p] [--stripe-size|-S] [--directory|-d]\n"
371          "                 [--mdt-index|-m] [--recursive|-r] [--raw|-R]\n"
372          "                 [--layout|-L] [--generation|-g] [--yaml|-y]\n"
373          "                 [--help|-h] [--component-id|-I[=COMP_ID]]\n"
374          "                 [--component-flags[=COMP_FLAGS]]\n"
375          "                 [--component-count]\n"
376          "                 [--extension-size|--ext-size|-z]\n"
377          "                 [--component-start[=[+-]COMP_START]]\n"
378          "                 [--component-end[=[+-]COMP_END]|-E[[+-]comp_end]]\n"
379          "                 [[!] --mirror-index=[+-]INDEX |\n"
380          "                 [!] --mirror-id=[+-]MIRROR_ID] [--mirror-count|-N]\n"
381          "                 [--no-follow]\n"
382          "                 <directory|filename> ..."},
383         {"setdirstripe", lfs_setdirstripe, 0,
384          "Create striped directory on specified MDT, same as mkdir.\n"
385          "May be restricted to root or group users, depending on settings.\n"
386          "usage: setdirstripe [OPTION] <directory>\n"
387          SETDIRSTRIPE_USAGE},
388         {"getdirstripe", lfs_getdirstripe, 0,
389          "To list the layout pattern info for a given directory\n"
390          "or recursively for all directories in a directory tree.\n"
391          "usage: getdirstripe [--mdt-count|-c] [--mdt-index|-m|-i]\n"
392          "                    [--help|-h] [--mdt-hash|-H] [--obd|-O UUID]\n"
393          "                    [--recursive|-r] [--yaml|-y]\n"
394          "                    [--verbose|-v] [--default|-D]\n"
395          "                    [--max-inherit|-X]\n"
396          "                    [--max-inherit-rr] <dir> ..."},
397         {"mkdir", lfs_setdirstripe, 0,
398          "Create striped directory on specified MDT, same as setdirstripe.\n"
399          "usage: mkdir [OPTION] <directory>\n"
400          SETDIRSTRIPE_USAGE},
401         {"rm_entry", lfs_rmentry, 0,
402          "To remove the name entry of the remote directory. Note: This\n"
403          "command will only delete the name entry, i.e. the remote directory\n"
404          "will become inaccessable after this command. This can only be done\n"
405          "by the administrator\n"
406          "usage: rm_entry <dir>\n"},
407         {"rmentry", lfs_rmentry, 0, "remove a dir entry, same as 'rm_entry'\n"},
408         {"unlink_foreign", lfs_unlink_foreign, 0,
409          "To remove the foreign file/dir.\n"
410          "Note: This is for files/dirs prevented to be removed using\n"
411          "unlink/rmdir, but works also for regular ones\n"
412          "usage: unlink_foreign <foreign_dir/file> [<foreign_dir/file> ...]\n"},
413         {"pool_list", lfs_poollist, 0,
414          "List pools or pool OSTs\n"
415          "usage: pool_list <fsname>[.<pool>] | <pathname>\n"},
416         {"find", lfs_find, 0,
417          "find files matching given attributes recursively in directory tree.\n"
418          "usage: find <directory|filename> ...\n"
419          "     [[!] --atime|-A [+-]N[smhdwy]] [[!] --btime|-B [+-]N[smhdwy]]\n"
420          "     [[!] --ctime|-C [+-]N[smhdwy]] [[!] --mtime|-M [+-]N[smhdwy]]\n"
421          "     [[!] --blocks|-b N] [[!] --component-count [+-]<comp_cnt>]\n"
422          "     [[!] --component-start [+-]N[kMGTPE]]\n"
423          "     [[!] --component-end|-E [+-]N[kMGTPE]]\n"
424          "     [[!] --component-flags {init,stale,prefer,offline,nosync,extension}]\n"
425          "     [[!] --extension-size|--ext-size|-z [+-]N[kMGT]]\n"
426          "     [[!] --foreign[=<foreign_type>]]\n"
427          "     [[!] --gid|-g|--group|-G <gid>|<gname>] [--help|-h]\n"
428          "     [[!] --layout|-L released,raid0,mdt] [--lazy]\n"
429          "     [--maxdepth|-D N] [[!] --mdt-count|-T [+-]<stripes>]\n"
430          "     [[!] --mdt-hash|-H <[^][blm],[^]fnv_1a_64,all_char,crush,...>\n"
431          "     [[!] --mdt-index|--mdt|-m <uuid|index,...>]\n"
432          "     [[!] --mirror-count|-N [+-]<n>]\n"
433          "     [[!] --mirror-state <[^]state>]\n"
434          "     [[!] --name|-n <pattern>] [[!] --newer[XY] <reference>]\n"
435          "     [[!] --ost|-O <uuid|index,...>] [[!] --perm [/-]mode]\n"
436          "     [[!] --pool <pool>] [--print|-P] [--print0|-0] [--printf <format>]\n"
437          "     [[!] --projid <projid>] [[!] --size|-s [+-]N[bkMGTPE]]\n"
438          "     [[!] --stripe-count|-c [+-]<stripes>]\n"
439          "     [[!] --stripe-index|-i <index,...>]\n"
440          "     [[!] --stripe-size|-S [+-]N[kMGT]] [[!] --type|-t <filetype>]\n"
441          "     [[!] --uid|-u|--user|-U <uid>|<uname>]\n"
442          "\t !: used before an option indicates 'NOT' requested attribute\n"
443          "\t -: used before a value indicates less than requested value\n"
444          "\t +: used before a value indicates more than requested value\n"
445          "\t ^: used before a flag indicates to exclude it\n"},
446         {"check", lfs_check, 0,
447          "Display the status of MGTs, MDTs or OSTs (as specified in the command)\n"
448          "or all the servers (MGTs, MDTs and OSTs) [for specified path only].\n"
449          "usage: check {mgts|osts|mdts|all} [path]"},
450         {"osts", lfs_osts, 0, "list OSTs connected to client "
451          "[for specified path only]\n" "usage: osts [path]"},
452         {"mdts", lfs_mdts, 0, "list MDTs connected to client "
453          "[for specified path only]\n" "usage: mdts [path]"},
454         {"df", lfs_df, 0,
455          "report filesystem disk space usage or inodes usage "
456          "of each MDS and all OSDs or a batch belonging to a specific pool.\n"
457          "Usage: df [--inodes|-i] [--human-readable|-h] [--lazy|-l]\n"
458          "          [--pool|-p <fsname>[.<pool>]] [path]"},
459         {"getname", lfs_getname, 0,
460          "list instances and specified mount points [for specified path only]\n"
461          "Usage: getname [--help|-h] [--instance|-i] [--fsname|-n] [path ...]"},
462 #ifdef HAVE_SYS_QUOTA_H
463         {"setquota", lfs_setquota, 0, "Set filesystem quotas.\n"
464          "usage: setquota [-t][-D] {-u|-U|-g|-G|-p|-P} {-b|-B|-i|-I LIMIT} [--pool POOL] FILESYSTEM\n"
465          "       setquota {-u|-g|-p} --delete FILESYSTEM\n"},
466         {"quota", lfs_quota, 0, "Display disk usage and limits.\n"
467          "usage: quota [-q] [-v] [-h] [-o OBD_UUID|-i MDT_IDX|-I OST_IDX]\n"
468          "             [{-u|-g|-p} UNAME|UID|GNAME|GID|PROJID]\n"
469          "             [--pool <OST pool name>] <filesystem>\n"
470          "       quota -t <-u|-g|-p> [--pool <OST pool name>] <filesystem>\n"
471          "       quota [-q] [-v] [h] {-U|-G|-P} [--pool <OST pool name>] <filesystem>"},
472         {"project", lfs_project, 0,
473          "Change or list project attribute for specified file or directory.\n"
474          "usage: project [-d|-r] <file|directory...>\n"
475          "         list project ID and flags on file(s) or directories\n"
476          "       project [-p id] [-s] [-r] <file|directory...>\n"
477          "         set project ID and/or inherit flag for specified file(s) or directories\n"
478          "       project -c [-d|-r [-p id] [-0]] <file|directory...>\n"
479          "         check project ID and flags on file(s) or directories, print outliers\n"
480          "       project -C [-d|-r] [-k] <file|directory...>\n"
481          "         clear the project inherit flag and ID on the file or directory\n"
482         },
483 #endif
484         {"flushctx", lfs_flushctx, 0,
485          "Flush security context for current user.\n"
486          "usage: flushctx [-k] [-r] [mountpoint...]"},
487         {"changelog", lfs_changelog, 0,
488          "Show the metadata changes on an MDT."
489          "\nusage: changelog <mdtname> [startrec [endrec]]"},
490         {"changelog_clear", lfs_changelog_clear, 0,
491          "Indicate that old changelog records up to <endrec> are no longer of "
492          "interest to consumer <id>, allowing the system to free up space.\n"
493          "An <endrec> of 0 means all records.\n"
494          "usage: changelog_clear <mdtname> <id> <endrec>"},
495         {"fid2path", lfs_fid2path, 0,
496          "Resolve the full path(s) for given FID(s). For a specific hardlink "
497          "specify link number <linkno>.\n"
498          "usage: fid2path [--print-fid|-f] [--print-link|-c] [--link|-l <linkno>] "
499          "<fsname|root> <fid>..."},
500         {"path2fid", lfs_path2fid, 0, "Display the fid(s) for a given path(s).\n"
501          "usage: path2fid [--parents] <path> ..."},
502         {"rmfid", lfs_rmfid, 0, "Remove file(s) by FID(s)\n"
503          "usage: rmfid <fsname|rootpath> <fid> ..."},
504         {"data_version", lfs_data_version, 0, "Display file data version for "
505          "a given path.\n" "usage: data_version [-n|-r|-w] <path>"},
506         {"hsm_state", lfs_hsm_state, 0, "Display the HSM information (states, "
507          "undergoing actions) for given files.\n usage: hsm_state <file> ..."},
508         {"hsm_set", lfs_hsm_set, 0, "Set HSM user flag on specified files.\n"
509          "usage: hsm_set [--norelease] [--noarchive] [--dirty] [--exists] "
510          "[--archived] [--lost] [--archive-id NUM] <file> ..."},
511         {"hsm_clear", lfs_hsm_clear, 0, "Clear HSM user flag on specified "
512          "files.\n"
513          "usage: hsm_clear [--norelease] [--noarchive] [--dirty] [--exists] "
514          "[--archived] [--lost] <file> ..."},
515         {"hsm_action", lfs_hsm_action, 0, "Display current HSM request for "
516          "given files.\n" "usage: hsm_action <file> ..."},
517         {"hsm_archive", lfs_hsm_archive, 0,
518          "Archive file to external storage.\n"
519          "usage: hsm_archive [--filelist FILELIST] [--data DATA] [--archive NUM] "
520          "<file> ..."},
521         {"hsm_restore", lfs_hsm_restore, 0,
522          "Restore file from external storage.\n"
523          "usage: hsm_restore [--filelist FILELIST] [--data DATA] <file> ..."},
524         {"hsm_release", lfs_hsm_release, 0,
525          "Release files from Lustre.\n"
526          "usage: hsm_release [--filelist FILELIST] [--data DATA] <file> ..."},
527         {"hsm_remove", lfs_hsm_remove, 0,
528          "Remove file copy from external storage.\n"
529          "usage: hsm_remove [--filelist FILELIST] [--data DATA] "
530          "[--archive NUM]\n"
531          "                  (FILE [FILE ...] | "
532          "--mntpath MOUNTPATH FID [FID ...])\n"
533          "\n"
534          "Note: To remove an archived copy of a file already deleted from a "
535          "Lustre FS, the\n"
536          "--mntpath option and a list of FIDs must be specified"
537         },
538         {"hsm_cancel", lfs_hsm_cancel, 0,
539          "Cancel requests related to specified files.\n"
540          "usage: hsm_cancel [--filelist FILELIST] [--data DATA] <file> ..."},
541         {"swap_layouts", lfs_swap_layouts, 0, "Swap layouts between 2 files.\n"
542          "usage: swap_layouts <path1> <path2>"},
543         {"migrate", lfs_setstripe_migrate, 0,
544          "migrate directories and their inodes between MDTs.\n"
545          "usage: migrate [--mdt-count|-c STRIPE_COUNT] [--directory|-d]\n"
546          "               [--mdt-hash|-H HASH_TYPE]\n"
547          "               [--mdt-index|-m START_MDT_INDEX] [--verbose|-v]\n"
548          "               DIRECTORY\n"
549          "\n"
550          "migrate file objects from one OST layout to another\n"
551          "(may be not safe with concurent writes).\n"
552          MIGRATE_USAGE },
553         {"mv", lfs_mv, 0,
554          "To move directories between MDTs. This command is deprecated, "
555          "use \"migrate\" instead.\n"
556          "usage: mv <directory|filename> [--mdt-index|-m MDT_INDEX] "
557          "[--verbose|-v]\n"},
558         {"ladvise", lfs_ladvise, 0,
559          "Provide servers with advice about access patterns for a file.\n"
560          "usage: ladvise [--advice|-a ADVICE] [--start|-s START[kMGT]]\n"
561          "               [--background|-b] [--unset|-u]\n\n"
562          "               {--end|-e END[kMGT]|--length|-l LENGTH[kMGT]}\n"
563          "               {[--mode|-m [READ,WRITE]}\n"
564          "               <file> ...\n"},
565         {"mirror", lfs_mirror, mirror_cmdlist,
566          "lfs commands used to manage files with mirrored components:\n"
567          "lfs mirror create - create a mirrored file or directory\n"
568          "lfs mirror extend - add mirror(s) to an existing file\n"
569          "lfs mirror split  - split a mirror from an existing mirrored file\n"
570          "lfs mirror resync - resynchronize out-of-sync mirrored file(s)\n"
571          "lfs mirror read   - read a mirror content of a mirrored file\n"
572          "lfs mirror write  - write to a mirror of a mirrored file\n"
573          "lfs mirror copy   - copy a mirror to other mirror(s) of a file\n"
574          "lfs mirror verify - verify mirrored file(s)\n"},
575         {"getsom", lfs_getsom, 0, "To list the SOM info for a given file.\n"
576          "usage: getsom [-s] [-b] [-f] <path>\n"
577          "\t-s: Only show the size value of the SOM data for a given file\n"
578          "\t-b: Only show the blocks value of the SOM data for a given file\n"
579          "\t-f: Only show the flags value of the SOM data for a given file\n"},
580         {"heat_get", lfs_heat_get, 0,
581          "To get heat of files.\n"
582          "usage: heat_get <file> ...\n"},
583         {"heat_set", lfs_heat_set, 0,
584          "To set heat flags of files.\n"
585          "usage: heat_set [--clear|-c] [--off|-o] [--on|-O] <file> ...\n"
586          "\t--clear|-c: Clear file heat for given files\n"
587          "\t--off|-o:   Turn off file heat for given files\n"
588          "\t--on|-O:    Turn on file heat for given files\n"},
589         {"pcc", lfs_pcc, pcc_cmdlist,
590          "lfs commands used to interact with PCC features:\n"
591          "lfs pcc attach - attach given files to Persistent Client Cache\n"
592          "lfs pcc attach_fid - attach given files into PCC by FID(s)\n"
593          "lfs pcc state  - display the PCC state for given files\n"
594          "lfs pcc detach - detach given files from Persistent Client Cache\n"
595          "lfs pcc detach_fid - detach given files from PCC by FID(s)\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 static int check_hashtype(const char *hashtype)
607 {
608         int type_num = atoi(hashtype);
609         int i;
610
611         /* numeric hash type */
612         if (hashtype && lmv_is_known_hash_type(type_num))
613                 return type_num;
614         /* string hash type */
615         for (i = LMV_HASH_TYPE_ALL_CHARS; i < ARRAY_SIZE(mdt_hash_name); i++)
616                 if (strcmp(hashtype, mdt_hash_name[i]) == 0)
617                         return i;
618
619         return 0;
620 }
621
622 static uint32_t check_foreign_type_name(const char *foreign_type_name)
623 {
624         uint32_t i;
625
626         for (i = 0; i < LU_FOREIGN_TYPE_UNKNOWN; i++) {
627                 if (!lu_foreign_types[i].lft_name)
628                         break;
629                 if (strcmp(foreign_type_name,
630                            lu_foreign_types[i].lft_name) == 0)
631                         return lu_foreign_types[i].lft_type;
632         }
633
634         return LU_FOREIGN_TYPE_UNKNOWN;
635 }
636
637 static const char *error_loc = "syserror";
638
639 static int
640 migrate_open_files(const char *name, __u64 migration_flags,
641                    const struct llapi_stripe_param *param,
642                    struct llapi_layout *layout, int *fd_src, int *fd_tgt)
643 {
644         int                      fd = -1;
645         int                      fdv = -1;
646         int                      rflags;
647         int                      mdt_index;
648         int                      random_value;
649         char                     parent[PATH_MAX];
650         char                     volatile_file[PATH_MAX];
651         char                    *ptr;
652         int                      rc;
653         struct stat              st;
654         struct stat              stv;
655
656         if (!param && !layout) {
657                 error_loc = "layout information";
658                 return -EINVAL;
659         }
660
661         /* search for file directory pathname */
662         if (strlen(name) > sizeof(parent) - 1) {
663                 error_loc = "source file name";
664                 return -ERANGE;
665         }
666
667         strncpy(parent, name, sizeof(parent));
668         ptr = strrchr(parent, '/');
669         if (!ptr) {
670                 if (!getcwd(parent, sizeof(parent))) {
671                         error_loc = "getcwd";
672                         return -errno;
673                 }
674         } else {
675                 if (ptr == parent) /* leading '/' */
676                         ptr = parent + 1;
677                 *ptr = '\0';
678         }
679
680         /* even if the file is only read, WR mode is nedeed to allow
681          * layout swap on fd
682          */
683         /* Allow migrating even without the key on encrypted files */
684         rflags = O_RDWR | O_NOATIME | O_FILE_ENC;
685         if (!(migration_flags & LLAPI_MIGRATION_NONDIRECT))
686                 rflags |= O_DIRECT;
687 source_open:
688         fd = open(name, rflags);
689         if (fd < 0) {
690                 /* If encrypted file without the key,
691                  * retry mirror extend in O_DIRECT.
692                  */
693                 if (errno == ENOKEY && !(rflags & O_DIRECT) &&
694                     migration_flags & LLAPI_MIGRATION_MIRROR) {
695                         rflags |= O_DIRECT;
696                         goto source_open;
697                 }
698                 rc = -errno;
699                 error_loc = "cannot open source file";
700                 return rc;
701         }
702
703         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
704         if (rc < 0) {
705                 error_loc = "cannot get MDT index";
706                 goto out;
707         }
708
709         do {
710                 int open_flags = O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW |
711                         /* Allow migrating without the key on encrypted files */
712                         O_FILE_ENC;
713                 mode_t open_mode = S_IRUSR | S_IWUSR;
714
715                 if (rflags & O_DIRECT)
716                         open_flags |= O_DIRECT;
717                 random_value = random();
718                 rc = snprintf(volatile_file, sizeof(volatile_file),
719                               "%s/%s:%.4X:%.4X:fd=%.2d", parent,
720                               LUSTRE_VOLATILE_HDR, mdt_index,
721                               random_value, fd);
722                 if (rc >= sizeof(volatile_file)) {
723                         rc = -ENAMETOOLONG;
724                         break;
725                 }
726
727                 /* create, open a volatile file, use caching (ie no directio) */
728                 if (layout) {
729                         /* Returns -1 and sets errno on error: */
730                         fdv = llapi_layout_file_open(volatile_file, open_flags,
731                                                      open_mode, layout);
732                         if (fdv < 0)
733                                 fdv = -errno;
734                 } else {
735                         /* Does the right thing on error: */
736                         fdv = llapi_file_open_param(volatile_file, open_flags,
737                                                     open_mode, param);
738                 }
739         } while (fdv < 0 && (rc = fdv) == -EEXIST);
740
741         if (rc < 0) {
742                 error_loc = "cannot create volatile file";
743                 goto out;
744         }
745
746         /*
747          * In case the MDT does not support creation of volatile files
748          * we should try to unlink it.
749          */
750         (void)unlink(volatile_file);
751
752         /*
753          * Not-owner (root?) special case.
754          * Need to set owner/group of volatile file like original.
755          * This will allow to pass related check during layout_swap.
756          */
757         rc = fstat(fd, &st);
758         if (rc != 0) {
759                 rc = -errno;
760                 error_loc = "cannot stat source file";
761                 goto out;
762         }
763
764         rc = fstat(fdv, &stv);
765         if (rc != 0) {
766                 rc = -errno;
767                 error_loc = "cannot stat volatile";
768                 goto out;
769         }
770
771         if (st.st_uid != stv.st_uid || st.st_gid != stv.st_gid) {
772                 rc = fchown(fdv, st.st_uid, st.st_gid);
773                 if (rc != 0) {
774                         rc = -errno;
775                         error_loc = "cannot change ownwership of volatile";
776                         goto out;
777                 }
778         }
779
780 out:
781         if (rc < 0) {
782                 if (fd > 0)
783                         close(fd);
784                 if (fdv > 0)
785                         close(fdv);
786         } else {
787                 *fd_src = fd;
788                 *fd_tgt = fdv;
789                 error_loc = NULL;
790         }
791         return rc;
792 }
793
794 struct timespec timespec_sub(struct timespec *before, struct timespec *after)
795 {
796         struct timespec ret;
797
798         ret.tv_sec = after->tv_sec - before->tv_sec;
799         if (after->tv_nsec < before->tv_nsec) {
800                 ret.tv_sec--;
801                 ret.tv_nsec = NSEC_PER_SEC + after->tv_nsec - before->tv_nsec;
802         } else {
803                 ret.tv_nsec = after->tv_nsec - before->tv_nsec;
804         }
805
806         return ret;
807 }
808
809 static void stats_log(struct timespec *now, struct timespec *start_time,
810                       enum stats_flag stats_flag,
811                       ssize_t read_bytes, size_t write_bytes,
812                       off_t file_size_bytes)
813 {
814         struct timespec diff = timespec_sub(start_time, now);
815
816         if (stats_flag == STATS_ON && ((diff.tv_sec != 0) ||
817                 (diff.tv_nsec != 0)) && file_size_bytes != 0)
818                 printf("- { seconds: %li, rmbps: %5.2g, wmbps: %5.2g, copied: %lu, size: %lu, pct: %lu%% }\n",
819                         diff.tv_sec,
820                         (double) read_bytes/((ONE_MB * diff.tv_sec) +
821                                 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
822                         (double) write_bytes/((ONE_MB * diff.tv_sec) +
823                                 ((ONE_MB * diff.tv_nsec)/NSEC_PER_SEC)),
824                         write_bytes/ONE_MB,
825                         file_size_bytes/ONE_MB,
826                         ((write_bytes*100)/file_size_bytes));
827 }
828
829 static int migrate_copy_data(int fd_src, int fd_dst, int (*check_file)(int),
830                              unsigned long long bandwidth_bytes_sec,
831                              enum stats_flag stats_flag,
832                              long stats_interval_sec, off_t file_size_bytes)
833 {
834         struct llapi_layout *layout;
835         size_t buf_size = 4 * 1024 * 1024;
836         void *buf = NULL;
837         off_t pos = 0;
838         off_t data_end = 0;
839         size_t page_size = sysconf(_SC_PAGESIZE);
840         bool sparse;
841         int rc;
842         size_t write_bytes = 0;
843         ssize_t read_bytes = 0;
844         struct timespec start_time;
845         struct timespec now;
846         struct timespec last_bw_print;
847
848         layout = llapi_layout_get_by_fd(fd_src, 0);
849         if (layout) {
850                 uint64_t stripe_size;
851
852                 rc = llapi_layout_stripe_size_get(layout, &stripe_size);
853                 if (rc == 0)
854                         buf_size = stripe_size;
855
856                 llapi_layout_free(layout);
857         }
858
859         /* Use a page-aligned buffer for direct I/O */
860         rc = posix_memalign(&buf, page_size, buf_size);
861         if (rc != 0)
862                 return -rc;
863
864         sparse = llapi_file_is_sparse(fd_src);
865         if (sparse) {
866                 rc = ftruncate(fd_dst, pos);
867                 if (rc < 0) {
868                         rc = -errno;
869                         return rc;
870                 }
871         }
872
873         clock_gettime(CLOCK_REALTIME, &start_time);
874         now = last_bw_print = start_time;
875
876         while (1) {
877                 off_t data_off;
878                 size_t to_read, to_write;
879                 ssize_t rsize;
880
881                 if (sparse && pos >= data_end) {
882                         size_t data_size;
883
884                         data_off = llapi_data_seek(fd_src, pos, &data_size);
885                         if (data_off < 0) {
886                                 /* Non-fatal, switch to full copy */
887                                 sparse = false;
888                                 continue;
889                         }
890                         /* hole at the end of file, truncate up to it */
891                         if (!data_size) {
892                                 rc = ftruncate(fd_dst, data_off);
893                                 if (rc < 0)
894                                         goto out;
895                         }
896                         pos = data_off & ~(page_size - 1);
897                         data_end = data_off + data_size;
898                         to_read = ((data_end - pos - 1) | (page_size - 1)) + 1;
899                         to_read = MIN(to_read, buf_size);
900                 } else {
901                         to_read = buf_size;
902                 }
903
904                 if (check_file) {
905                         rc = check_file(fd_src);
906                         if (rc < 0)
907                                 goto out;
908                 }
909
910                 rsize = pread(fd_src, buf, to_read, pos);
911                 read_bytes += rsize;
912                 if (rsize < 0) {
913                         rc = -errno;
914                         goto out;
915                 }
916                 /* EOF */
917                 if (rsize == 0)
918                         break;
919
920                 to_write = rsize;
921                 while (to_write > 0) {
922                         unsigned long long write_target;
923                         ssize_t written;
924                         struct timespec diff;
925
926                         written = pwrite(fd_dst, buf, to_write, pos);
927                         if (written < 0) {
928                                 rc = -errno;
929                                 goto out;
930                         }
931                         pos += written;
932                         to_write -= written;
933                         write_bytes += written;
934
935                         if (bandwidth_bytes_sec == 0)
936                                 continue;
937
938                         clock_gettime(CLOCK_REALTIME, &now);
939                         diff = timespec_sub(&start_time, &now);
940                         write_target = ((bandwidth_bytes_sec * diff.tv_sec) +
941                                 ((bandwidth_bytes_sec *
942                                 diff.tv_nsec)/NSEC_PER_SEC));
943
944                         if (write_target < write_bytes) {
945                                 unsigned long long excess;
946                                 struct timespec delay = { 0, 0 };
947
948                                 excess = write_bytes - write_target;
949
950                                 if (excess == 0)
951                                         continue;
952
953                                 delay.tv_sec = excess / bandwidth_bytes_sec;
954                                 delay.tv_nsec = (excess % bandwidth_bytes_sec) *
955                                         NSEC_PER_SEC / bandwidth_bytes_sec;
956
957                                 do {
958                                         rc = clock_nanosleep(CLOCK_REALTIME, 0,
959                                                              &delay, &delay);
960                                 } while (rc < 0 && errno == EINTR);
961
962                                 if (rc < 0) {
963                                         if (stats_flag == STATS_OFF)
964                                                 fprintf(stderr,
965                                                         "error %s: delay for bandwidth control failed: %s\n",
966                                                         progname,
967                                                         strerror(-rc));
968                                         rc = 0;
969                                 }
970                         }
971                 }
972
973                 clock_gettime(CLOCK_REALTIME, &now);
974                 if ((write_bytes != file_size_bytes) &&
975                         (now.tv_sec >= last_bw_print.tv_sec +
976                         stats_interval_sec)) {
977                         stats_log(&now, &start_time, stats_flag,
978                                   read_bytes, write_bytes,
979                                   file_size_bytes);
980                         last_bw_print = now;
981                 }
982
983                 if (rc || rsize < to_read)
984                         break;
985         }
986
987         /* Output at least one log, regardless of stats_interval */
988         clock_gettime(CLOCK_REALTIME, &now);
989         stats_log(&now, &start_time, stats_flag,
990                   read_bytes, write_bytes,
991                   file_size_bytes);
992
993         rc = fsync(fd_dst);
994         if (rc < 0)
995                 rc = -errno;
996 out:
997         /* Try to avoid page cache pollution after migration. */
998         (void)posix_fadvise(fd_src, 0, 0, POSIX_FADV_DONTNEED);
999         (void)posix_fadvise(fd_dst, 0, 0, POSIX_FADV_DONTNEED);
1000
1001         free(buf);
1002         return rc;
1003 }
1004
1005 static int migrate_set_timestamps(int fd, const struct stat *st)
1006 {
1007         struct timeval tv[2] = {
1008                 {.tv_sec = st->st_atime},
1009                 {.tv_sec = st->st_mtime}
1010         };
1011
1012         return futimes(fd, tv);
1013 }
1014
1015 static int migrate_block(int fd, int fdv,
1016                          unsigned long long bandwidth_bytes_sec,
1017                          enum stats_flag stats_flag,
1018                          long stats_interval_sec)
1019 {
1020         struct stat st;
1021         __u64   dv1;
1022         int     gid;
1023         int     rc;
1024         int     rc2;
1025
1026         rc = fstat(fd, &st);
1027         if (rc < 0) {
1028                 error_loc = "cannot stat source file";
1029                 return -errno;
1030         }
1031
1032         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1033         if (rc < 0) {
1034                 error_loc = "cannot get dataversion";
1035                 return rc;
1036         }
1037
1038         do
1039                 gid = random();
1040         while (gid == 0);
1041
1042         /*
1043          * The grouplock blocks all concurrent accesses to the file.
1044          * It has to be taken after llapi_get_data_version as it would
1045          * block it too.
1046          */
1047         rc = llapi_group_lock(fd, gid);
1048         if (rc < 0) {
1049                 error_loc = "cannot get group lock";
1050                 return rc;
1051         }
1052
1053         rc = migrate_copy_data(fd, fdv, NULL, bandwidth_bytes_sec,
1054                                stats_flag, stats_interval_sec,
1055                                st.st_size);
1056         if (rc < 0) {
1057                 error_loc = "data copy failed";
1058                 goto out_unlock;
1059         }
1060
1061         /* Make sure we keep original atime/mtime values */
1062         rc = migrate_set_timestamps(fdv, &st);
1063         if (rc < 0) {
1064                 error_loc = "set target file timestamp failed";
1065                 goto out_unlock;
1066         }
1067
1068         /*
1069          * swap layouts
1070          * for a migration we need to check data version on file did
1071          * not change.
1072          *
1073          * Pass in gid=0 since we already own grouplock.
1074          */
1075         rc = llapi_fswap_layouts_grouplock(fd, fdv, dv1, 0, 0,
1076                                            SWAP_LAYOUTS_CHECK_DV1);
1077         if (rc == -EAGAIN) {
1078                 error_loc = "file changed";
1079                 goto out_unlock;
1080         } else if (rc < 0) {
1081                 error_loc = "cannot swap layout";
1082                 goto out_unlock;
1083         }
1084
1085 out_unlock:
1086         rc2 = llapi_group_unlock(fd, gid);
1087         if (rc2 < 0 && rc == 0) {
1088                 error_loc = "unlock group lock";
1089                 rc = rc2;
1090         }
1091
1092         return rc;
1093 }
1094
1095 /**
1096  * Internal helper for migrate_copy_data(). Check lease and report error if
1097  * need be.
1098  *
1099  * \param[in]  fd           File descriptor on which to check the lease.
1100  *
1101  * \retval 0       Migration can keep on going.
1102  * \retval -errno  Error occurred, abort migration.
1103  */
1104 static int check_lease(int fd)
1105 {
1106         int rc;
1107
1108         rc = llapi_lease_check(fd);
1109         if (rc > 0)
1110                 return 0; /* llapi_check_lease returns > 0 on success. */
1111
1112         return -EBUSY;
1113 }
1114
1115 static int migrate_nonblock(int fd, int fdv,
1116                             unsigned long long bandwidth_bytes_sec,
1117                             enum stats_flag stats_flag,
1118                             long stats_interval_sec)
1119 {
1120         struct stat st;
1121         __u64   dv1;
1122         __u64   dv2;
1123         int     rc;
1124
1125         rc = fstat(fd, &st);
1126         if (rc < 0) {
1127                 error_loc = "cannot stat source file";
1128                 return -errno;
1129         }
1130
1131         rc = llapi_get_data_version(fd, &dv1, LL_DV_RD_FLUSH);
1132         if (rc < 0) {
1133                 error_loc = "cannot get data version";
1134                 return rc;
1135         }
1136
1137         rc = migrate_copy_data(fd, fdv, check_lease, bandwidth_bytes_sec,
1138                                stats_flag, stats_interval_sec,
1139                                st.st_size);
1140         if (rc < 0) {
1141                 error_loc = "data copy failed";
1142                 return rc;
1143         }
1144
1145         rc = llapi_get_data_version(fd, &dv2, LL_DV_RD_FLUSH);
1146         if (rc != 0) {
1147                 error_loc = "cannot get data version";
1148                 return rc;
1149         }
1150
1151         if (dv1 != dv2) {
1152                 rc = -EAGAIN;
1153                 error_loc = "source file changed";
1154                 return rc;
1155         }
1156
1157         /* Make sure we keep original atime/mtime values */
1158         rc = migrate_set_timestamps(fdv, &st);
1159         if (rc < 0) {
1160                 error_loc = "set target file timestamp failed";
1161                 return -errno;
1162         }
1163         return 0;
1164 }
1165
1166 static
1167 int lfs_layout_compid_by_pool(char *fname, const char *pool, int *comp_id)
1168 {
1169         struct pool_to_id_cbdata data = { .pool = pool };
1170         struct llapi_layout *layout = NULL;
1171         int rc;
1172
1173         layout = llapi_layout_get_by_path(fname, 0);
1174         if (!layout) {
1175                 fprintf(stderr,
1176                         "error %s: file '%s' couldn't get layout: rc=%d\n",
1177                         progname, fname, errno);
1178                 rc = -errno;
1179                 goto free_layout;
1180         }
1181         rc = llapi_layout_sanity(layout, false, true);
1182         if (rc < 0) {
1183                 llapi_layout_sanity_perror(errno);
1184                 goto free_layout;
1185         }
1186         rc = llapi_layout_comp_iterate(layout, find_comp_id_by_pool, &data);
1187         if (rc < 0)
1188                 goto free_layout;
1189
1190         *comp_id = data.id;
1191         rc = 0;
1192
1193 free_layout:
1194         if (layout)
1195                 llapi_layout_free(layout);
1196         return rc;
1197 }
1198
1199 static int lfs_component_set(char *fname, int comp_id, const char *pool,
1200                              __u32 flags, __u32 neg_flags)
1201 {
1202         __u32 ids[2];
1203         __u32 flags_array[2];
1204         size_t count = 0;
1205         int rc;
1206
1207         if (!comp_id) {
1208                 if (pool == NULL) {
1209                         fprintf(stderr,
1210                                 "error %s: neither component id nor pool is specified\n",
1211                                 progname);
1212                         return -EINVAL;
1213                 }
1214                 rc = lfs_layout_compid_by_pool(fname, pool, &comp_id);
1215                 if (rc)
1216                         return rc;
1217         }
1218
1219         if (flags) {
1220                 ids[count] = comp_id;
1221                 flags_array[count] = flags;
1222                 ++count;
1223         }
1224
1225         if (neg_flags) {
1226                 if (neg_flags & LCME_FL_STALE) {
1227                         fprintf(stderr,
1228                                 "%s: cannot clear 'stale' flags from component. Please use lfs-mirror-resync(1) instead\n",
1229                                 progname);
1230                         return -EINVAL;
1231                 }
1232
1233                 ids[count] = comp_id;
1234                 flags_array[count] = neg_flags | LCME_FL_NEG;
1235                 ++count;
1236         }
1237
1238         rc = llapi_layout_file_comp_set(fname, ids, flags_array, count);
1239         if (rc) {
1240                 if (errno == EUCLEAN) {
1241                         rc = -errno;
1242                         fprintf(stderr,
1243                                 "%s: cannot set 'stale' flag on component '%#x' of the last non-stale mirror of '%s'\n",
1244                                 progname, comp_id, fname);
1245                 } else {
1246                         fprintf(stderr,
1247                                 "%s: cannot change the flags of component '%#x' of file '%s': %x / ^(%x)\n",
1248                                 progname, comp_id, fname, flags, neg_flags);
1249                 }
1250         }
1251
1252         return rc;
1253 }
1254
1255 static int lfs_component_del(char *fname, __u32 comp_id,
1256                              __u32 flags, __u32 neg_flags)
1257 {
1258         int     rc = 0;
1259
1260         if (flags && neg_flags) {
1261                 fprintf(stderr,
1262                         "%s: cannot specify both positive and negative flags\n",
1263                         progname);
1264                 return -EINVAL;
1265         }
1266
1267         if (!flags && neg_flags)
1268                 flags = neg_flags | LCME_FL_NEG;
1269
1270         if (flags && comp_id) {
1271                 fprintf(stderr,
1272                         "%s: cannot specify component ID and flags at the same time\n",
1273                         progname);
1274                 return -EINVAL;
1275         }
1276
1277         if (!flags && !comp_id) {
1278                 fprintf(stderr,
1279                         "%s: neither flags nor component ID is specified\n",
1280                         progname);
1281                 return -EINVAL;
1282         }
1283
1284         if (flags) {
1285                 if (flags & ~LCME_KNOWN_FLAGS) {
1286                         fprintf(stderr,
1287                                 "%s setstripe: unknown flags %#x\n",
1288                                 progname, flags);
1289                         return -EINVAL;
1290                 }
1291         } else if (comp_id > LCME_ID_MAX) {
1292                 fprintf(stderr, "%s setstripe: invalid component id %u\n",
1293                         progname, comp_id);
1294                 return -EINVAL;
1295         }
1296
1297         rc = llapi_layout_file_comp_del(fname, comp_id, flags);
1298         if (rc)
1299                 fprintf(stderr,
1300                         "%s setstripe: cannot delete component %#x from '%s': %s\n",
1301                         progname, comp_id, fname, strerror(errno));
1302         return rc;
1303 }
1304
1305 static int lfs_component_add(char *fname, struct llapi_layout *layout)
1306 {
1307         int     rc;
1308
1309         if (!layout)
1310                 return -EINVAL;
1311
1312         rc = llapi_layout_file_comp_add(fname, layout);
1313         if (rc)
1314                 fprintf(stderr, "Add layout component(s) to %s failed. %s\n",
1315                         fname, strerror(errno));
1316         return rc;
1317 }
1318
1319 static int lfs_component_create(char *fname, int open_flags, mode_t open_mode,
1320                                 struct llapi_layout *layout)
1321 {
1322         struct stat     st;
1323         int     fd;
1324
1325         if (!layout)
1326                 return -EINVAL;
1327
1328         fd = lstat(fname, &st);
1329         if (fd == 0 && S_ISDIR(st.st_mode))
1330                 open_flags = O_DIRECTORY | O_RDONLY;
1331
1332         fd = llapi_layout_file_open(fname, open_flags, open_mode, layout);
1333         if (fd < 0)
1334                 fprintf(stderr, "%s: cannot %s '%s': %s\n", progname,
1335                         S_ISDIR(st.st_mode) ?
1336                                 "set default composite layout for" :
1337                                 "create composite file",
1338                         fname, strerror(errno));
1339         return fd;
1340 }
1341
1342 static int lfs_migrate(char *name, __u64 migration_flags,
1343                         struct llapi_stripe_param *param,
1344                         struct llapi_layout *layout,
1345                         unsigned long long bandwidth_bytes_sec,
1346                         enum stats_flag stats_flag, long stats_interval_sec)
1347 {
1348         struct llapi_layout *existing;
1349         uint64_t dom_new, dom_cur;
1350         int fd = -1;
1351         int fdv = -1;
1352         int rc;
1353
1354         rc = migrate_open_files(name, migration_flags, param, layout,
1355                                 &fd, &fdv);
1356         if (rc < 0)
1357                 goto out;
1358
1359         rc = llapi_layout_dom_size(layout, &dom_new);
1360         if (rc) {
1361                 error_loc = "cannot get new layout DoM size";
1362                 goto out;
1363         }
1364         /* special case for migration to DOM layout*/
1365         existing = llapi_layout_get_by_fd(fd, 0);
1366         if (!existing) {
1367                 error_loc = "cannot get existing layout";
1368                 goto out;
1369         }
1370
1371         rc = llapi_layout_dom_size(existing, &dom_cur);
1372         if (rc) {
1373                 error_loc = "cannot get current layout DoM size";
1374                 goto out;
1375         }
1376
1377         /*
1378          * if file has DoM layout already then migration is possible to
1379          * the new layout with the same DoM component via swap layout,
1380          * if new layout used bigger DOM size, then mirroring is used
1381          */
1382         if (dom_new > dom_cur) {
1383                 rc = lfs_migrate_to_dom(fd, fdv, name, migration_flags,
1384                                         bandwidth_bytes_sec, stats_flag,
1385                                         stats_interval_sec);
1386                 if (rc)
1387                         error_loc = "cannot migrate to DOM layout";
1388                 goto out_closed;
1389         }
1390
1391         if (stats_flag == STATS_ON)
1392                 printf("%s:\n", name);
1393
1394         if (!(migration_flags & LLAPI_MIGRATION_NONBLOCK)) {
1395                 /*
1396                  * Blocking mode (forced if servers do not support file lease).
1397                  * It is also the default mode, since we cannot distinguish
1398                  * between a broken lease and a server that does not support
1399                  * atomic swap/close (LU-6785)
1400                  */
1401                 rc = migrate_block(fd, fdv, bandwidth_bytes_sec, stats_flag,
1402                                    stats_interval_sec);
1403                 goto out;
1404         }
1405
1406         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1407         if (rc < 0) {
1408                 error_loc = "cannot get lease";
1409                 goto out;
1410         }
1411
1412         rc = migrate_nonblock(fd, fdv, bandwidth_bytes_sec, stats_flag,
1413                               stats_interval_sec);
1414         if (rc < 0) {
1415                 llapi_lease_release(fd);
1416                 goto out;
1417         }
1418
1419         /*
1420          * Atomically put lease, swap layouts and close.
1421          * for a migration we need to check data version on file did
1422          * not change.
1423          */
1424         rc = llapi_fswap_layouts(fd, fdv, 0, 0, SWAP_LAYOUTS_CLOSE);
1425         if (rc < 0) {
1426                 error_loc = "cannot swap layout";
1427                 goto out;
1428         }
1429
1430 out:
1431         if (fd >= 0)
1432                 close(fd);
1433
1434         if (fdv >= 0)
1435                 close(fdv);
1436 out_closed:
1437         if (rc < 0)
1438                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1439                         progname, name, error_loc, strerror(-rc));
1440         else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
1441                 printf("%s\n", name);
1442
1443         return rc;
1444 }
1445
1446 static int comp_str2flags(char *string, __u32 *flags, __u32 *neg_flags)
1447 {
1448         char *name;
1449         char *dup_string = NULL;
1450         int rc = 0;
1451
1452         *flags = 0;
1453         *neg_flags = 0;
1454
1455         if (!string || !string[0])
1456                 return -EINVAL;
1457
1458         dup_string = strdup(string);
1459         if (!dup_string) {
1460                 llapi_printf(LLAPI_MSG_ERROR,
1461                              "%s: insufficient memory\n",
1462                              progname);
1463                 return -ENOMEM;
1464         }
1465
1466         for (name = strtok(dup_string, ","); name; name = strtok(NULL, ",")) {
1467                 bool found = false;
1468                 int i;
1469
1470                 for (i = 0; i < ARRAY_SIZE(comp_flags_table); i++) {
1471                         __u32 comp_flag = comp_flags_table[i].cfn_flag;
1472                         const char *comp_name = comp_flags_table[i].cfn_name;
1473
1474                         if (strcmp(name, comp_name) == 0) {
1475                                 *flags |= comp_flag;
1476                                 found = true;
1477                         } else if (strncmp(name, "^", 1) == 0 &&
1478                                    strcmp(name + 1, comp_name) == 0) {
1479                                 *neg_flags |= comp_flag;
1480                                 found = true;
1481                         }
1482                 }
1483                 if (!found) {
1484                         llapi_printf(LLAPI_MSG_ERROR,
1485                                      "%s: component flag '%s' not supported\n",
1486                                      progname, name);
1487                         rc = -EINVAL;
1488                         goto out_free;
1489                 }
1490         }
1491
1492         if (!*flags && !*neg_flags)
1493                 rc = -EINVAL;
1494
1495         /* don't allow to set and exclude the same flag */
1496         if (*flags & *neg_flags)
1497                 rc = -EINVAL;
1498
1499 out_free:
1500         free(dup_string);
1501         return rc;
1502 }
1503
1504 static int mdthash_input(char *string, __u32 *inflags,
1505                          __u32 *exflags, __u32 *type)
1506 {
1507         char *name;
1508         struct mhf_list {
1509                 char *name;
1510                 __u32 flag;
1511         } mhflist[] = {
1512                 {"migrating", LMV_HASH_FLAG_MIGRATION},
1513                 {"bad_type", LMV_HASH_FLAG_BAD_TYPE},
1514                 {"badtype", LMV_HASH_FLAG_BAD_TYPE},
1515                 {"lost_lmv", LMV_HASH_FLAG_LOST_LMV},
1516                 {"lostlmv", LMV_HASH_FLAG_LOST_LMV},
1517         };
1518
1519         if (string == NULL)
1520                 return -EINVAL;
1521
1522         *inflags = 0;
1523         *exflags = 0;
1524         *type = 0;
1525         for (name = strtok(string, ","); name; name = strtok(NULL, ",")) {
1526                 bool found = false;
1527                 int i;
1528
1529                 for (i = 0; i < ARRAY_SIZE(mhflist); i++) {
1530                         if (strcmp(name, mhflist[i].name) == 0 ||
1531                             name[0] == mhflist[i].name[0]) {
1532                                 *inflags |= mhflist[i].flag;
1533                                 found = true;
1534                         } else if (name[0] == '^' &&
1535                                    (strcmp(name + 1, mhflist[i].name) == 0 ||
1536                                     name[1] == mhflist[i].name[0])) {
1537                                 *exflags |= mhflist[i].flag;
1538                                 found = true;
1539                         }
1540                 }
1541                 if (!found) {
1542                         i = check_hashtype(name);
1543                         if (i > 0) {
1544                                 *type |= 1 << i;
1545                                 continue;
1546                         }
1547                         llapi_printf(LLAPI_MSG_ERROR,
1548                                      "%s: invalid mdt_hash value '%s'\n",
1549                                      progname, name);
1550                         return -EINVAL;
1551                 }
1552         }
1553
1554         /* don't allow to include and exclude the same flag */
1555         if (*inflags & *exflags) {
1556                 llapi_printf(LLAPI_MSG_ERROR,
1557                              "%s: include and exclude same flag '%s'\n",
1558                              progname, string);
1559                 return -EINVAL;
1560         }
1561
1562         return 0;
1563 }
1564
1565 static int mirror_str2state(char *string, __u16 *state, __u16 *neg_state)
1566 {
1567         if (!string)
1568                 return -EINVAL;
1569
1570         *state = 0;
1571         *neg_state = 0;
1572
1573         if (strncmp(string, "^", 1) == 0) {
1574                 *neg_state = llapi_layout_string_flags(string + 1);
1575                 if (*neg_state != 0)
1576                         return 0;
1577         } else {
1578                 *state = llapi_layout_string_flags(string);
1579                 if (*state != 0)
1580                         return 0;
1581         }
1582
1583         llapi_printf(LLAPI_MSG_ERROR,
1584                      "%s: mirrored file state '%s' not supported\n",
1585                      progname, string);
1586         return -EINVAL;
1587 }
1588
1589 /**
1590  * struct mirror_args - Command-line arguments for mirror(s).
1591  * @m_count:  Number of mirrors to be created with this layout.
1592  * @m_flags:  Mirror level flags, only 'prefer' is supported.
1593  * @m_layout: Mirror layout.
1594  * @m_file:   A victim file. Its layout will be split and used as a mirror.
1595  * @m_next:   Point to the next node of the list.
1596  *
1597  * Command-line arguments for mirror(s) will be parsed and stored in
1598  * a linked list that consists of this structure.
1599  */
1600 struct mirror_args {
1601         __u32                   m_count;
1602         __u32                   m_flags;
1603         struct llapi_layout     *m_layout;
1604         const char              *m_file;
1605         struct mirror_args      *m_next;
1606         bool                    m_inherit;
1607 };
1608
1609 /**
1610  * enum mirror_flags - Flags for extending a mirrored file.
1611  * @MF_NO_VERIFY: Indicates not to verify the mirror(s) from victim file(s)
1612  *             in case the victim file(s) contains the same data as the
1613  *             original mirrored file.
1614  * @MF_DESTROY: Indicates to delete the mirror from the mirrored file.
1615  * @MF_COMP_ID: specified component id instead of mirror id
1616  *
1617  * Flags for extending a mirrored file.
1618  */
1619 enum mirror_flags {
1620         MF_NO_VERIFY    = 0x1,
1621         MF_DESTROY      = 0x2,
1622         MF_COMP_ID      = 0x4,
1623         MF_COMP_POOL    = 0x8,
1624 };
1625
1626 /**
1627  * mirror_create_sanity_check() - Check mirror list.
1628  * @list:  A linked list that stores the mirror arguments.
1629  *
1630  * This function does a sanity check on @list for creating
1631  * a mirrored file.
1632  *
1633  * Return: 0 on success or a negative error code on failure.
1634  */
1635 static int mirror_create_sanity_check(const char *fname,
1636                                       struct mirror_args *list,
1637                                       bool check_fname)
1638 {
1639         int rc = 0;
1640         bool has_m_file = false;
1641         bool has_m_layout = false;
1642
1643         if (!list)
1644                 return -EINVAL;
1645
1646         if (fname && check_fname) {
1647                 struct llapi_layout *layout;
1648
1649                 layout = llapi_layout_get_by_path(fname, 0);
1650                 if (!layout) {
1651                         fprintf(stderr,
1652                                 "error: %s: file '%s' couldn't get layout\n",
1653                                 progname, fname);
1654                         return -ENODATA;
1655                 }
1656
1657                 rc = llapi_layout_sanity(layout, false, true);
1658
1659                 llapi_layout_free(layout);
1660
1661                 if (rc) {
1662                         llapi_layout_sanity_perror(rc);
1663                         return rc;
1664                 }
1665         }
1666
1667         while (list) {
1668                 if (list->m_file) {
1669                         has_m_file = true;
1670                         llapi_layout_free(list->m_layout);
1671
1672                         list->m_layout =
1673                                 llapi_layout_get_by_path(list->m_file, 0);
1674                         if (!list->m_layout) {
1675                                 fprintf(stderr,
1676                                         "error: %s: file '%s' has no layout\n",
1677                                         progname, list->m_file);
1678                                 return -ENODATA;
1679                         }
1680                 } else {
1681                         has_m_layout = true;
1682                         if (!list->m_layout) {
1683                                 fprintf(stderr, "error: %s: no mirror layout\n",
1684                                         progname);
1685                                 return -EINVAL;
1686                         }
1687                 }
1688
1689                 rc = llapi_layout_sanity(list->m_layout, false, true);
1690                 if (rc) {
1691                         llapi_layout_sanity_perror(rc);
1692                         return rc;
1693                 }
1694
1695                 list = list->m_next;
1696         }
1697
1698         if (has_m_file && has_m_layout) {
1699                 fprintf(stderr,
1700                         "error: %s: -f <victim_file> option should not be specified with setstripe options\n",
1701                         progname);
1702                 return -EINVAL;
1703         }
1704
1705         return 0;
1706 }
1707
1708 static int mirror_set_flags(struct llapi_layout *layout, void *cbdata)
1709 {
1710         __u32 mirror_flags = *(__u32 *)cbdata;
1711         uint32_t flags;
1712         int rc;
1713
1714         rc = llapi_layout_comp_flags_get(layout, &flags);
1715         if (rc < 0)
1716                 return rc;
1717
1718         if (!flags) {
1719                 rc = llapi_layout_comp_flags_set(layout, mirror_flags);
1720                 if (rc)
1721                         return rc;
1722         }
1723
1724         return LLAPI_LAYOUT_ITER_CONT;
1725 }
1726
1727 /**
1728  * mirror_create() - Create a mirrored file.
1729  * @fname:        The file to be created.
1730  * @mirror_list:  A linked list that stores the mirror arguments.
1731  *
1732  * This function creates a mirrored file @fname with the mirror(s)
1733  * from @mirror_list.
1734  *
1735  * Return: 0 on success or a negative error code on failure.
1736  */
1737 static int mirror_create(char *fname, struct mirror_args *mirror_list)
1738 {
1739         struct llapi_layout *layout = NULL;
1740         struct mirror_args *cur_mirror = NULL;
1741         uint16_t mirror_count = 0;
1742         int i = 0;
1743         int rc = 0;
1744
1745         rc = mirror_create_sanity_check(fname, mirror_list, false);
1746         if (rc)
1747                 return rc;
1748
1749         cur_mirror = mirror_list;
1750         while (cur_mirror) {
1751                 rc = llapi_layout_comp_iterate(cur_mirror->m_layout,
1752                                                mirror_set_flags,
1753                                                &cur_mirror->m_flags);
1754                 if (rc) {
1755                         rc = -errno;
1756                         fprintf(stderr, "%s: failed to set mirror flags\n",
1757                                 progname);
1758                         goto error;
1759                 }
1760
1761                 for (i = 0; i < cur_mirror->m_count; i++) {
1762                         rc = llapi_layout_merge(&layout, cur_mirror->m_layout);
1763                         if (rc) {
1764                                 rc = -errno;
1765                                 fprintf(stderr,
1766                                         "error: %s: merge layout failed: %s\n",
1767                                         progname, strerror(errno));
1768                                 goto error;
1769                         }
1770                 }
1771                 mirror_count += cur_mirror->m_count;
1772                 cur_mirror = cur_mirror->m_next;
1773         }
1774
1775         if (!layout) {
1776                 fprintf(stderr, "error: %s: layout is NULL\n", progname);
1777                 return -EINVAL;
1778         }
1779
1780         rc = llapi_layout_mirror_count_set(layout, mirror_count);
1781         if (rc) {
1782                 rc = -errno;
1783                 fprintf(stderr, "error: %s: set mirror count failed: %s\n",
1784                         progname, strerror(errno));
1785                 goto error;
1786         }
1787
1788         rc = lfs_component_create(fname, O_CREAT | O_WRONLY, 0666,
1789                                   layout);
1790         if (rc >= 0) {
1791                 close(rc);
1792                 rc = 0;
1793         }
1794
1795 error:
1796         llapi_layout_free(layout);
1797         return rc;
1798 }
1799
1800 /**
1801  * Compare files and check lease on @fd.
1802  *
1803  * \retval bytes number of bytes are the same
1804  */
1805 static ssize_t mirror_file_compare(int fd, int fdv)
1806 {
1807         const size_t buflen = 4 * 1024 * 1024; /* 4M */
1808         void *buf;
1809         ssize_t bytes_done = 0;
1810         ssize_t bytes_read = 0;
1811
1812         buf = malloc(buflen * 2);
1813         if (!buf)
1814                 return -ENOMEM;
1815
1816         while (1) {
1817                 if (!llapi_lease_check(fd)) {
1818                         bytes_done = -EBUSY;
1819                         break;
1820                 }
1821
1822                 bytes_read = read(fd, buf, buflen);
1823                 if (bytes_read <= 0)
1824                         break;
1825
1826                 if (bytes_read != read(fdv, buf + buflen, buflen))
1827                         break;
1828
1829                 /*
1830                  * XXX: should compute the checksum on each buffer and then
1831                  * compare checksum to avoid cache collision
1832                  */
1833                 if (memcmp(buf, buf + buflen, bytes_read))
1834                         break;
1835
1836                 bytes_done += bytes_read;
1837         }
1838
1839         free(buf);
1840
1841         return bytes_done;
1842 }
1843
1844 static int mirror_extend_file(const char *fname, const char *victim_file,
1845                               enum mirror_flags mirror_flags)
1846 {
1847         int fd = -1;
1848         int fdv = -1;
1849         struct stat stbuf;
1850         struct stat stbuf_v;
1851         struct ll_ioc_lease *data = NULL;
1852         int rc;
1853
1854         fd = open(fname, O_RDWR);
1855         if (fd < 0) {
1856                 error_loc = "open source file";
1857                 rc = -errno;
1858                 goto out;
1859         }
1860
1861         fdv = open(victim_file, O_RDWR);
1862         if (fdv < 0) {
1863                 error_loc = "open target file";
1864                 rc = -errno;
1865                 goto out;
1866         }
1867
1868         if (fstat(fd, &stbuf) || fstat(fdv, &stbuf_v)) {
1869                 error_loc = "stat source or target file";
1870                 rc = -errno;
1871                 goto out;
1872         }
1873
1874         if (stbuf.st_dev != stbuf_v.st_dev) {
1875                 error_loc = "stat source and target file";
1876                 rc = -EXDEV;
1877                 goto out;
1878         }
1879
1880         /* mirrors should be of the same size */
1881         if (stbuf.st_size != stbuf_v.st_size) {
1882                 error_loc = "file sizes don't match";
1883                 rc = -EINVAL;
1884                 goto out;
1885         }
1886
1887         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
1888         if (rc < 0) {
1889                 error_loc = "cannot get lease";
1890                 goto out;
1891         }
1892
1893         if (!(mirror_flags & MF_NO_VERIFY)) {
1894                 ssize_t ret;
1895                 /* mirrors should have the same contents */
1896                 ret = mirror_file_compare(fd, fdv);
1897                 if (ret != stbuf.st_size) {
1898                         error_loc = "file busy or contents don't match";
1899                         rc = ret < 0 ? ret : -EINVAL;
1900                         goto out;
1901                 }
1902         }
1903
1904         /* Get rid of caching pages from clients */
1905         rc = llapi_file_flush(fd);
1906         if (rc < 0) {
1907                 error_loc = "cannot get data version";
1908                 goto out;
1909         }
1910
1911         rc = llapi_file_flush(fdv);
1912         if (rc < 0) {
1913                 error_loc = "cannot get data version";
1914                 goto out;
1915         }
1916
1917         rc = migrate_set_timestamps(fd, &stbuf);
1918         if (rc < 0) {
1919                 error_loc = "cannot set source file timestamp";
1920                 goto out;
1921         }
1922
1923         /* Atomically put lease, merge layouts and close. */
1924         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
1925         if (!data) {
1926                 error_loc = "memory allocation";
1927                 goto out;
1928         }
1929         data->lil_mode = LL_LEASE_UNLCK;
1930         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
1931         data->lil_count = 1;
1932         data->lil_ids[0] = fdv;
1933         rc = llapi_lease_set(fd, data);
1934         if (rc < 0) {
1935                 error_loc = "cannot merge layout";
1936                 goto out;
1937         } else if (rc == 0) {
1938                 rc = -EBUSY;
1939                 error_loc = "lost lease lock";
1940                 goto out;
1941         }
1942         rc = 0;
1943
1944 out:
1945         if (data)
1946                 free(data);
1947         if (fd >= 0)
1948                 close(fd);
1949         if (fdv >= 0)
1950                 close(fdv);
1951         if (!rc)
1952                 (void) unlink(victim_file);
1953         if (rc < 0)
1954                 fprintf(stderr, "error: %s: %s: %s: %s\n",
1955                         progname, fname, error_loc, strerror(-rc));
1956         return rc;
1957 }
1958
1959 static int mirror_extend_layout(char *name, struct llapi_layout *m_layout,
1960                                 bool inherit, uint32_t flags,
1961                                 unsigned long long bandwidth_bytes_sec,
1962                                 enum stats_flag stats_flag,
1963                                 long stats_interval_sec)
1964 {
1965         struct llapi_layout *f_layout = NULL;
1966         struct ll_ioc_lease *data = NULL;
1967         struct stat st;
1968         int fd = -1;
1969         int fdv = -1;
1970         int rc = 0;
1971
1972         if (inherit) {
1973                 f_layout = llapi_layout_get_by_path(name, 0);
1974                 if (!f_layout) {
1975                         rc = -EINVAL;
1976                         fprintf(stderr, "%s: cannot get layout\n", progname);
1977                         goto out;
1978                 }
1979                 rc = llapi_layout_get_last_init_comp(f_layout);
1980                 if (rc) {
1981                         fprintf(stderr, "%s: cannot get the last init comp\n",
1982                                 progname);
1983                         goto out;
1984                 }
1985                 rc = llapi_layout_mirror_inherit(f_layout, m_layout);
1986                 if (rc) {
1987                         fprintf(stderr,
1988                                 "%s: cannot inherit from the last init comp\n",
1989                                 progname);
1990                         goto out;
1991                 }
1992         }
1993
1994         llapi_layout_comp_flags_set(m_layout, flags);
1995         rc = migrate_open_files(name,
1996                              LLAPI_MIGRATION_NONDIRECT | LLAPI_MIGRATION_MIRROR,
1997                              NULL, m_layout, &fd, &fdv);
1998         if (rc < 0)
1999                 goto out;
2000
2001         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2002         if (rc < 0) {
2003                 error_loc = "cannot get lease";
2004                 goto out;
2005         }
2006
2007         rc = fstat(fd, &st);
2008         if (rc < 0) {
2009                 error_loc = "cannot stat source file";
2010                 goto out;
2011         }
2012
2013         if (stats_flag)
2014                 printf("%s:\n", name);
2015
2016         rc = migrate_nonblock(fd, fdv, bandwidth_bytes_sec, stats_flag,
2017                               stats_interval_sec);
2018         if (rc < 0) {
2019                 llapi_lease_release(fd);
2020                 goto out;
2021         }
2022
2023         rc = migrate_set_timestamps(fd, &st);
2024         if (rc < 0) {
2025                 error_loc = "cannot set source file timestamp";
2026                 goto out;
2027         }
2028
2029         /* Atomically put lease, merge layouts and close. */
2030         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2031         if (!data) {
2032                 error_loc = "memory allocation";
2033                 goto out;
2034         }
2035         data->lil_mode = LL_LEASE_UNLCK;
2036         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2037         data->lil_count = 1;
2038         data->lil_ids[0] = fdv;
2039         rc = llapi_lease_set(fd, data);
2040         if (rc < 0) {
2041                 error_loc = "cannot merge layout";
2042                 goto out;
2043         } else if (rc == 0) {
2044                 rc = -EBUSY;
2045                 error_loc = "lost lease lock";
2046                 goto out;
2047         }
2048         rc = 0;
2049
2050 out:
2051         if (data)
2052                 free(data);
2053         if (fd >= 0)
2054                 close(fd);
2055         if (fdv >= 0)
2056                 close(fdv);
2057         if (rc < 0)
2058                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2059                         progname, name, error_loc, strerror(-rc));
2060         return rc;
2061 }
2062
2063 static int mirror_extend(char *fname, struct mirror_args *mirror_list,
2064                          enum mirror_flags mirror_flags,
2065                          unsigned long long bandwidth_bytes_sec,
2066                          enum stats_flag stats_flag, long stats_interval_sec)
2067 {
2068         int rc = 0;
2069
2070         while (mirror_list) {
2071                 if (mirror_list->m_file) {
2072                         rc = mirror_extend_file(fname, mirror_list->m_file,
2073                                                 mirror_flags);
2074                 } else {
2075                         __u32 mirror_count = mirror_list->m_count;
2076
2077                         while (mirror_count > 0) {
2078                                 rc = mirror_extend_layout(fname,
2079                                                         mirror_list->m_layout,
2080                                                         mirror_list->m_inherit,
2081                                                         mirror_list->m_flags,
2082                                                         bandwidth_bytes_sec,
2083                                                         stats_flag,
2084                                                         stats_interval_sec);
2085                                 if (rc)
2086                                         break;
2087
2088                                 --mirror_count;
2089                         }
2090                 }
2091                 if (rc)
2092                         break;
2093
2094                 mirror_list = mirror_list->m_next;
2095         }
2096
2097         return rc;
2098 }
2099
2100 static int find_mirror_id(struct llapi_layout *layout, void *cbdata)
2101 {
2102         uint32_t id;
2103         int rc;
2104
2105         rc = llapi_layout_mirror_id_get(layout, &id);
2106         if (rc < 0)
2107                 return rc;
2108
2109         if ((__u16)id == *(__u16 *)cbdata)
2110                 return LLAPI_LAYOUT_ITER_STOP;
2111
2112         return LLAPI_LAYOUT_ITER_CONT;
2113 }
2114
2115 static int find_comp_id(struct llapi_layout *layout, void *cbdata)
2116 {
2117         uint32_t id;
2118         int rc;
2119
2120         rc = llapi_layout_comp_id_get(layout, &id);
2121         if (rc < 0)
2122                 return rc;
2123
2124         if (id == *(__u32 *)cbdata)
2125                 return LLAPI_LAYOUT_ITER_STOP;
2126
2127         return LLAPI_LAYOUT_ITER_CONT;
2128 }
2129
2130 static int find_mirror_id_by_pool(struct llapi_layout *layout, void *cbdata)
2131 {
2132         char buf[LOV_MAXPOOLNAME + 1];
2133         struct pool_to_id_cbdata *d = (void *)cbdata;
2134         uint32_t id;
2135         int rc;
2136
2137         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2138         if (rc < 0)
2139                 return rc;
2140         if (strcmp(d->pool, buf))
2141                 return LLAPI_LAYOUT_ITER_CONT;
2142
2143         rc = llapi_layout_mirror_id_get(layout, &id);
2144         if (rc < 0)
2145                 return rc;
2146         d->id = id;
2147
2148         return LLAPI_LAYOUT_ITER_STOP;
2149 }
2150
2151 static int find_comp_id_by_pool(struct llapi_layout *layout, void *cbdata)
2152 {
2153         char buf[LOV_MAXPOOLNAME + 1];
2154         struct pool_to_id_cbdata *d = (void *)cbdata;
2155         uint32_t id;
2156         int rc;
2157
2158         rc = llapi_layout_pool_name_get(layout, buf, sizeof(buf));
2159         if (rc < 0)
2160                 return rc;
2161         if (strcmp(d->pool, buf))
2162                 return LLAPI_LAYOUT_ITER_CONT;
2163
2164         rc = llapi_layout_comp_id_get(layout, &id);
2165         if (rc < 0)
2166                 return rc;
2167         d->id = id;
2168
2169         return LLAPI_LAYOUT_ITER_STOP;
2170 }
2171
2172 struct collect_ids_data {
2173         __u16   *cid_ids;
2174         int     cid_count;
2175         __u16   cid_exclude;
2176 };
2177
2178 static int collect_mirror_id(struct llapi_layout *layout, void *cbdata)
2179 {
2180         struct collect_ids_data *cid = cbdata;
2181         uint32_t id;
2182         int rc;
2183
2184         rc = llapi_layout_mirror_id_get(layout, &id);
2185         if (rc < 0)
2186                 return rc;
2187
2188         if ((__u16)id != cid->cid_exclude) {
2189                 int i;
2190
2191                 for (i = 0; i < cid->cid_count; i++) {
2192                         /* already collected the mirror id */
2193                         if (id == cid->cid_ids[i])
2194                                 return LLAPI_LAYOUT_ITER_CONT;
2195                 }
2196                 cid->cid_ids[cid->cid_count] = id;
2197                 cid->cid_count++;
2198         }
2199
2200         return LLAPI_LAYOUT_ITER_CONT;
2201 }
2202
2203 /**
2204  * last_non_stale_mirror() - Check if a mirror is the last non-stale mirror.
2205  * @mirror_id: Mirror id to be checked.
2206  * @layout:    Mirror component list.
2207  *
2208  * This function checks if a mirror with specified @mirror_id is the last
2209  * non-stale mirror of a layout @layout.
2210  *
2211  * Return: true or false.
2212  */
2213 static inline
2214 bool last_non_stale_mirror(__u16 mirror_id, struct llapi_layout *layout)
2215 {
2216         __u16 mirror_ids[128] = { 0 };
2217         struct collect_ids_data cid = { .cid_ids = mirror_ids,
2218                                         .cid_count = 0,
2219                                         .cid_exclude = mirror_id, };
2220         int i;
2221
2222         llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
2223
2224         for (i = 0; i < cid.cid_count; i++) {
2225                 struct llapi_resync_comp comp_array[1024] = { { 0 } };
2226                 int comp_size = 0;
2227
2228                 comp_size = llapi_mirror_find_stale(layout, comp_array,
2229                                                     ARRAY_SIZE(comp_array),
2230                                                     &mirror_ids[i], 1);
2231                 if (comp_size == 0)
2232                         return false;
2233         }
2234
2235         return true;
2236 }
2237
2238 static int mirror_split(const char *fname, __u32 id, const char *pool,
2239                         enum mirror_flags mflags, const char *victim_file)
2240 {
2241         struct llapi_layout *layout;
2242         char parent[PATH_MAX];
2243         char victim[PATH_MAX];
2244         int flags = O_CREAT | O_EXCL | O_LOV_DELAY_CREATE | O_NOFOLLOW;
2245         char *ptr;
2246         struct ll_ioc_lease *data;
2247         uint16_t mirror_count;
2248         __u32 mirror_id;
2249         int mdt_index;
2250         int fd, fdv;
2251         bool purge = true; /* delete mirror by setting fdv=fd */
2252         bool is_encrypted;
2253         int rc;
2254
2255         if (victim_file && (strcmp(fname, victim_file) == 0)) {
2256                 fprintf(stderr,
2257                         "error %s: the source file '%s' and -f file are the same\n",
2258                         progname, fname);
2259                 return -EINVAL;
2260         }
2261
2262         /* check fname contains mirror with mirror_id/comp_id */
2263         layout = llapi_layout_get_by_path(fname, 0);
2264         if (!layout) {
2265                 fprintf(stderr,
2266                         "error %s: file '%s' couldn't get layout\n",
2267                         progname, fname);
2268                 return -EINVAL;
2269         }
2270
2271         rc = llapi_layout_sanity(layout, false, true);
2272         if (rc) {
2273                 llapi_layout_sanity_perror(rc);
2274                 goto free_layout;
2275         }
2276
2277         rc = llapi_layout_mirror_count_get(layout, &mirror_count);
2278         if (rc) {
2279                 fprintf(stderr,
2280                         "error %s: file '%s' couldn't get mirror count\n",
2281                         progname, fname);
2282                 goto free_layout;
2283         }
2284         if (mirror_count < 2) {
2285                 fprintf(stderr,
2286                         "error %s: file '%s' has %d component, cannot split\n",
2287                         progname, fname, mirror_count);
2288                 goto free_layout;
2289         }
2290
2291         if (mflags & MF_COMP_POOL) {
2292                 struct pool_to_id_cbdata data = { .pool = pool };
2293
2294                 rc = llapi_layout_comp_iterate(layout, find_mirror_id_by_pool,
2295                                                &data);
2296                 mirror_id = data.id;
2297         } else if (mflags & MF_COMP_ID) {
2298                 rc = llapi_layout_comp_iterate(layout, find_comp_id, &id);
2299                 mirror_id = mirror_id_of(id);
2300         } else {
2301                 rc = llapi_layout_comp_iterate(layout, find_mirror_id, &id);
2302                 mirror_id = id;
2303         }
2304         if (rc < 0) {
2305                 fprintf(stderr, "error %s: failed to iterate layout of '%s'\n",
2306                         progname, fname);
2307                 goto free_layout;
2308         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
2309                 if (mflags & MF_COMP_POOL) {
2310                         fprintf(stderr,
2311                                 "error %s: file '%s' does not contain mirror with pool '%s'\n",
2312                                 progname, fname, pool);
2313                         goto free_layout;
2314                 } else if (mflags & MF_COMP_ID) {
2315                         fprintf(stderr,
2316                                 "error %s: file '%s' does not contain mirror with comp-id %u\n",
2317                                 progname, fname, id);
2318                         goto free_layout;
2319                 } else {
2320                         fprintf(stderr,
2321                                 "error %s: file '%s' does not contain mirror with id %u\n",
2322                                 progname, fname, id);
2323                         goto free_layout;
2324                 }
2325         }
2326
2327         if (!victim_file && mflags & MF_DESTROY)
2328                 /* Allow mirror split even without the key on encrypted files,
2329                  * and in this case of a 'split -d', open file with O_DIRECT
2330                  * (no IOs will be done).
2331                  */
2332                 fd = open(fname, O_RDWR | O_DIRECT | O_FILE_ENC);
2333         else
2334                 fd = open(fname, O_RDWR);
2335
2336         if (fd < 0) {
2337                 fprintf(stderr,
2338                         "error %s: open file '%s' failed: %s\n",
2339                         progname, fname, strerror(errno));
2340                 goto free_layout;
2341         }
2342
2343         /* get victim file directory pathname */
2344         if (strlen(fname) > sizeof(parent) - 1) {
2345                 fprintf(stderr, "error %s: file name of '%s' too long\n",
2346                         progname, fname);
2347                 rc = -ERANGE;
2348                 goto close_fd;
2349         }
2350         strncpy(parent, fname, sizeof(parent));
2351         ptr = strrchr(parent, '/');
2352         if (!ptr) {
2353                 if (!getcwd(parent, sizeof(parent))) {
2354                         fprintf(stderr, "error %s: getcwd failed: %s\n",
2355                                 progname, strerror(errno));
2356                         rc = -errno;
2357                         goto close_fd;
2358                 }
2359         } else {
2360                 if (ptr == parent)
2361                         ptr = parent + 1;
2362                 *ptr = '\0';
2363         }
2364
2365         rc = llapi_file_fget_mdtidx(fd, &mdt_index);
2366         if (rc < 0) {
2367                 fprintf(stderr, "%s: cannot get MDT index of '%s'\n",
2368                         progname, fname);
2369                 goto close_fd;
2370         }
2371
2372         rc = llapi_file_is_encrypted(fd);
2373         if (rc < 0) {
2374                 fprintf(stderr, "%s: cannot get flags of '%s': %d\n",
2375                         progname, fname, rc);
2376                 goto close_fd;
2377         }
2378         is_encrypted = rc;
2379
2380 again:
2381         if (!victim_file) {
2382                 /* use a temp file to store the splitted layout */
2383                 if (mflags & MF_DESTROY) {
2384                         char file_path[PATH_MAX];
2385                         unsigned int rnumber;
2386                         int open_flags;
2387
2388                         if (last_non_stale_mirror(mirror_id, layout)) {
2389                                 rc = -EUCLEAN;
2390                                 fprintf(stderr,
2391                                         "%s: cannot destroy the last non-stale mirror of file '%s'\n",
2392                                         progname, fname);
2393                                 goto close_fd;
2394                         }
2395
2396                         if (purge) {
2397                                 /* don't use volatile file for mirror destroy */
2398                                 fdv = fd;
2399                         } else {
2400                                 /**
2401                                  * try the old way to delete mirror using
2402                                  * volatile file.
2403                                  */
2404                                 do {
2405                                         rnumber = random();
2406                                         rc = snprintf(file_path,
2407                                                       sizeof(file_path),
2408                                                       "%s/" LUSTRE_VOLATILE_HDR ":%.4X:%.4X:fd=%.2d",
2409                                                       parent, mdt_index,
2410                                                       rnumber, fd);
2411                                         if (rc < 0 ||
2412                                             rc >= sizeof(file_path)) {
2413                                                 fdv = -ENAMETOOLONG;
2414                                                 break;
2415                                         }
2416
2417                                         open_flags = O_RDWR |
2418                                              (O_LOV_DELAY_CREATE & ~O_ACCMODE) |
2419                                              O_CREAT | O_EXCL | O_NOFOLLOW |
2420                                              /* O_DIRECT for mirror split -d */
2421                                              O_DIRECT |
2422                                              /* Allow split without the key */
2423                                              O_FILE_ENC;
2424                                         fdv = open(file_path, open_flags,
2425                                                    S_IRUSR | S_IWUSR);
2426                                         if (fdv < 0)
2427                                                 rc = -errno;
2428                                 } while (fdv < 0 && rc == -EEXIST);
2429                         }
2430                 } else {
2431                         if (is_encrypted) {
2432                                 rc = -1;
2433                                 fprintf(stderr,
2434                                         "error %s: not permitted on encrypted file '%s': %d\n",
2435                                         progname, fname, rc);
2436                                 goto close_fd;
2437                         }
2438
2439                         snprintf(victim, sizeof(victim), "%s.mirror~%u",
2440                                  fname, mirror_id);
2441                         fdv = open(victim, flags, S_IRUSR | S_IWUSR);
2442                 }
2443         } else {
2444                 /* user specified victim file */
2445                 if (is_encrypted) {
2446                         rc = -1;
2447                         fprintf(stderr,
2448                                 "error %s: not permitted on encrypted file '%s': %d\n",
2449                                 progname, fname, rc);
2450                         goto close_fd;
2451                 }
2452                 fdv = open(victim_file, flags, S_IRUSR | S_IWUSR);
2453         }
2454
2455         if (fdv < 0) {
2456                 fprintf(stderr,
2457                         "error %s: create victim file failed: %s\n",
2458                         progname, strerror(errno));
2459                 goto close_fd;
2460         }
2461
2462         /* get lease lock of fname */
2463         rc = llapi_lease_acquire(fd, LL_LEASE_WRLCK);
2464         if (rc < 0) {
2465                 fprintf(stderr,
2466                         "error %s: cannot get lease of file '%s': %d\n",
2467                         progname, fname, rc);
2468                 goto close_victim;
2469         }
2470
2471         /* Atomatically put lease, split layouts and close. */
2472         data = malloc(offsetof(typeof(*data), lil_ids[2]));
2473         if (!data) {
2474                 rc = -ENOMEM;
2475                 goto close_victim;
2476         }
2477
2478         data->lil_mode = LL_LEASE_UNLCK;
2479         data->lil_flags = LL_LEASE_LAYOUT_SPLIT;
2480         data->lil_count = 2;
2481         data->lil_ids[0] = fdv;
2482         data->lil_ids[1] = mirror_id;
2483         rc = llapi_lease_set(fd, data);
2484         if (rc <= 0) {
2485                 if ((rc == -EINVAL || rc == -EBUSY) && purge) {
2486                         /* could be old MDS which prohibit fd==fdv */
2487                         purge = false;
2488                         goto again;
2489
2490                 }
2491                 if (rc == 0) /* lost lease lock */
2492                         rc = -EBUSY;
2493                 fprintf(stderr,
2494                         "error %s: cannot split '%s': %s\n",
2495                         progname, fname, strerror(-rc));
2496         } else {
2497                 rc = 0;
2498         }
2499         free(data);
2500
2501 close_victim:
2502         if (!purge)
2503                 close(fdv);
2504 close_fd:
2505         close(fd);
2506 free_layout:
2507         llapi_layout_free(layout);
2508         return rc;
2509 }
2510
2511 static inline
2512 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
2513                            __u16 *mirror_ids, int ids_nr);
2514
2515 static int lfs_migrate_to_dom(int fd, int fdv, char *name,
2516                               __u64 migration_flags,
2517                               unsigned long long bandwidth_bytes_sec,
2518                               enum stats_flag stats_flag,
2519                               long stats_interval_sec)
2520 {
2521         struct ll_ioc_lease *data = NULL;
2522         int rc;
2523
2524         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
2525         if (rc < 0) {
2526                 error_loc = "cannot get lease";
2527                 goto out_close;
2528         }
2529
2530         if (stats_flag)
2531                 printf("%s:\n", name);
2532
2533         rc = migrate_nonblock(fd, fdv, bandwidth_bytes_sec, stats_flag,
2534                               stats_interval_sec);
2535         if (rc < 0)
2536                 goto out_release;
2537
2538         /* Atomically put lease, merge layouts, resync and close. */
2539         data = calloc(1, offsetof(typeof(*data), lil_ids[1]));
2540         if (!data) {
2541                 error_loc = "memory allocation";
2542                 goto out_release;
2543         }
2544         data->lil_mode = LL_LEASE_UNLCK;
2545         data->lil_flags = LL_LEASE_LAYOUT_MERGE;
2546         data->lil_count = 1;
2547         data->lil_ids[0] = fdv;
2548         rc = llapi_lease_set(fd, data);
2549         if (rc < 0) {
2550                 error_loc = "cannot merge layout";
2551                 goto out_close;
2552         } else if (rc == 0) {
2553                 rc = -EBUSY;
2554                 error_loc = "lost lease lock";
2555                 goto out_close;
2556         }
2557         close(fd);
2558         close(fdv);
2559
2560         rc = lfs_mirror_resync_file(name, data, NULL, 0);
2561         if (rc) {
2562                 error_loc = "cannot resync file";
2563                 goto out;
2564         }
2565
2566         /* delete first mirror now */
2567         rc = mirror_split(name, 1, NULL, MF_DESTROY, NULL);
2568         if (rc < 0)
2569                 error_loc = "cannot delete old layout";
2570         goto out;
2571
2572 out_release:
2573         llapi_lease_release(fd);
2574 out_close:
2575         close(fd);
2576         close(fdv);
2577 out:
2578         if (rc < 0)
2579                 fprintf(stderr, "error: %s: %s: %s: %s\n",
2580                         progname, name, error_loc, strerror(-rc));
2581         else if (migration_flags & LLAPI_MIGRATION_VERBOSE)
2582                 printf("%s\n", name);
2583         if (data)
2584                 free(data);
2585         return rc;
2586 }
2587
2588 /**
2589  * Parse a string containing an target index list into an array of integers.
2590  *
2591  * The input string contains a comma delimited list of individual
2592  * indices and ranges, for example "1,2-4,7". Add the indices into the
2593  * \a tgts array and remove duplicates.
2594  *
2595  * \param[out] tgts             array to store indices in
2596  * \param[in] size              size of \a tgts array
2597  * \param[in] offset            starting index in \a tgts
2598  * \param[in] arg               string containing OST index list
2599  * \param[in/out] overstriping  index list may contain duplicates
2600  *
2601  * \retval positive    number of indices in \a tgts
2602  * \retval -EINVAL     unable to parse \a arg
2603  */
2604 static int parse_targets(__u32 *tgts, int size, int offset, char *arg,
2605                          unsigned long long *pattern)
2606 {
2607         int rc;
2608         int nr = offset;
2609         int slots = size - offset;
2610         char *ptr = NULL;
2611         bool overstriped = false;
2612         bool end_of_loop;
2613
2614         if (!arg)
2615                 return -EINVAL;
2616
2617         end_of_loop = false;
2618         while (!end_of_loop) {
2619                 int start_index = 0;
2620                 int end_index = 0;
2621                 int i;
2622                 char *endptr = NULL;
2623
2624                 rc = -EINVAL;
2625
2626                 ptr = strchrnul(arg, ',');
2627
2628                 end_of_loop = *ptr == '\0';
2629                 *ptr = '\0';
2630
2631                 errno = 0;
2632                 start_index = strtol(arg, &endptr, 0);
2633                 if (endptr == arg) /* no data at all */
2634                         break;
2635                 if (errno != 0 || start_index < -1 ||
2636                     (*endptr != '-' && *endptr != '\0'))
2637                         break;
2638
2639                 end_index = start_index;
2640                 if (*endptr == '-') {
2641                         errno = 0;
2642                         end_index = strtol(endptr + 1, &endptr, 0);
2643                         if (errno != 0 || *endptr != '\0' || end_index < -1)
2644                                 break;
2645                         if (end_index < start_index)
2646                                 break;
2647                 }
2648
2649                 for (i = start_index; i <= end_index && slots > 0; i++) {
2650                         int j;
2651
2652                         /* remove duplicate */
2653                         for (j = 0; j < offset; j++) {
2654                                 if (tgts[j] == i && pattern &&
2655                                     *pattern == LLAPI_LAYOUT_OVERSTRIPING)
2656                                         overstriped = true;
2657                                 else if (tgts[j] == i)
2658                                         return -EINVAL;
2659                         }
2660
2661                         j = offset;
2662
2663                         if (j == offset) { /* check complete */
2664                                 tgts[nr++] = i;
2665                                 --slots;
2666                         }
2667                 }
2668
2669                 if (slots == 0 && i < end_index)
2670                         break;
2671
2672                 *ptr = ',';
2673                 arg = ++ptr;
2674                 offset = nr;
2675                 rc = 0;
2676         }
2677         if (!end_of_loop && ptr)
2678                 *ptr = ',';
2679
2680         if (!overstriped && pattern)
2681                 *pattern = LLAPI_LAYOUT_DEFAULT;
2682
2683         return rc < 0 ? rc : nr;
2684 }
2685
2686 struct lfs_setstripe_args {
2687         unsigned long long       lsa_comp_end;
2688         unsigned long long       lsa_stripe_size;
2689         unsigned long long       lsa_extension_size;
2690         long long                lsa_stripe_count;
2691         long long                lsa_stripe_off;
2692         __u32                    lsa_comp_flags;
2693         __u32                    lsa_comp_neg_flags;
2694         unsigned long long       lsa_pattern;
2695         unsigned int             lsa_mirror_count;
2696         int                      lsa_nr_tgts;
2697         bool                     lsa_first_comp;
2698         bool                     lsa_extension_comp;
2699         __u32                   *lsa_tgts;
2700         char                    *lsa_pool_name;
2701 };
2702
2703 static inline void setstripe_args_init(struct lfs_setstripe_args *lsa)
2704 {
2705         unsigned int mirror_count = lsa->lsa_mirror_count;
2706         bool first_comp = lsa->lsa_first_comp;
2707
2708         memset(lsa, 0, sizeof(*lsa));
2709
2710         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2711         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2712         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2713         lsa->lsa_pattern = LLAPI_LAYOUT_RAID0;
2714         lsa->lsa_pool_name = NULL;
2715
2716         lsa->lsa_mirror_count = mirror_count;
2717         lsa->lsa_first_comp = first_comp;
2718 }
2719
2720 /**
2721  * setstripe_args_init_inherit() - Initialize and inherit stripe options.
2722  * @lsa: Stripe options to be initialized and inherited.
2723  *
2724  * This function initializes stripe options in @lsa and inherit
2725  * stripe_size, stripe_count and OST pool_name options.
2726  *
2727  * Return: void.
2728  */
2729 static inline void setstripe_args_init_inherit(struct lfs_setstripe_args *lsa)
2730 {
2731         unsigned long long stripe_size;
2732         long long stripe_count;
2733         char *pool_name = NULL;
2734
2735         stripe_size = lsa->lsa_stripe_size;
2736         stripe_count = lsa->lsa_stripe_count;
2737         pool_name = lsa->lsa_pool_name;
2738
2739         setstripe_args_init(lsa);
2740
2741         lsa->lsa_stripe_size = stripe_size;
2742         lsa->lsa_stripe_count = stripe_count;
2743         lsa->lsa_pool_name = pool_name;
2744 }
2745
2746 static inline bool setstripe_args_specified(struct lfs_setstripe_args *lsa)
2747 {
2748         return (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT ||
2749                 lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ||
2750                 lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
2751                 lsa->lsa_pattern != LLAPI_LAYOUT_RAID0 ||
2752                 lsa->lsa_comp_end != 0);
2753 }
2754
2755 static int lsa_args_stripe_count_check(struct lfs_setstripe_args *lsa)
2756 {
2757         if (lsa->lsa_nr_tgts) {
2758                 if (lsa->lsa_nr_tgts < 0 ||
2759                     lsa->lsa_nr_tgts >= LOV_MAX_STRIPE_COUNT) {
2760                         fprintf(stderr, "Invalid nr_tgts(%d)\n",
2761                                 lsa->lsa_nr_tgts);
2762                         errno = EINVAL;
2763                         return -1;
2764                 }
2765
2766                 if (lsa->lsa_stripe_count > 0 &&
2767                     lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
2768                     lsa->lsa_stripe_count != LLAPI_LAYOUT_WIDE &&
2769                     lsa->lsa_nr_tgts != lsa->lsa_stripe_count) {
2770                         fprintf(stderr, "stripe_count(%lld) != nr_tgts(%d)\n",
2771                                 lsa->lsa_stripe_count,
2772                                 lsa->lsa_nr_tgts);
2773                         errno = EINVAL;
2774                         return -1;
2775                 }
2776         }
2777
2778         return 0;
2779
2780 }
2781
2782 /**
2783  * comp_args_to_layout() - Create or extend a composite layout.
2784  * @composite:       Pointer to the composite layout.
2785  * @lsa:             Stripe options for the new component.
2786  *
2787  * This function creates or extends a composite layout by adding a new
2788  * component with stripe options from @lsa.
2789  *
2790  * When modified, adjust llapi_stripe_param_verify() if needed as well.
2791  *
2792  * Return: 0 on success or an error code on failure.
2793  */
2794 static int comp_args_to_layout(struct llapi_layout **composite,
2795                                struct lfs_setstripe_args *lsa,
2796                                bool set_extent)
2797 {
2798         struct llapi_layout *layout = *composite;
2799         uint64_t prev_end = 0;
2800         uint64_t size;
2801         int i = 0, rc;
2802
2803 new_comp:
2804         if (!layout) {
2805                 layout = llapi_layout_alloc();
2806                 if (!layout) {
2807                         fprintf(stderr, "Alloc llapi_layout failed. %s\n",
2808                                 strerror(errno));
2809                         errno = ENOMEM;
2810                         return -1;
2811                 }
2812                 *composite = layout;
2813                 lsa->lsa_first_comp = true;
2814         } else {
2815                 uint64_t start;
2816
2817                 /*
2818                  * Get current component extent, current component
2819                  * must be the tail component.
2820                  */
2821                 rc = llapi_layout_comp_extent_get(layout, &start, &prev_end);
2822                 if (rc) {
2823                         fprintf(stderr, "Get comp extent failed. %s\n",
2824                                 strerror(errno));
2825                         return rc;
2826                 }
2827
2828                 if (lsa->lsa_first_comp) {
2829                         prev_end = 0;
2830                         rc = llapi_layout_add_first_comp(layout);
2831                 } else {
2832                         rc = llapi_layout_comp_add(layout);
2833                 }
2834                 if (rc) {
2835                         fprintf(stderr, "Add component failed. %s\n",
2836                                 strerror(errno));
2837                         return rc;
2838                 }
2839         }
2840
2841         rc = llapi_layout_comp_flags_set(layout, lsa->lsa_comp_flags);
2842         if (rc) {
2843                 fprintf(stderr, "Set flags 0x%x failed: %s\n",
2844                         lsa->lsa_comp_flags, strerror(errno));
2845                 return rc;
2846         }
2847
2848         if (set_extent) {
2849                 uint64_t comp_end = lsa->lsa_comp_end;
2850
2851                 /*
2852                  * The extendable component is 0-length, so it can be removed
2853                  * if there is insufficient space to extend it.
2854                  */
2855                 if (lsa->lsa_extension_comp)
2856                         comp_end = prev_end;
2857
2858                 rc = llapi_layout_comp_extent_set(layout, prev_end,
2859                                                   comp_end);
2860                 if (rc) {
2861                         fprintf(stderr, "Set extent [%lu, %lu) failed. %s\n",
2862                                 prev_end, comp_end, strerror(errno));
2863                         return rc;
2864                 }
2865         }
2866         /* reset lsa_first_comp */
2867         lsa->lsa_first_comp = false;
2868
2869         /* Data-on-MDT component setting */
2870         if (lsa->lsa_pattern == LLAPI_LAYOUT_MDT) {
2871                 /* Yaml support */
2872                 if (lsa->lsa_stripe_count == 0)
2873                         lsa->lsa_stripe_count = LLAPI_LAYOUT_DEFAULT;
2874                 if (lsa->lsa_stripe_size == lsa->lsa_comp_end)
2875                         lsa->lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
2876                 if (lsa->lsa_stripe_off == -1 ||
2877                     lsa->lsa_stripe_off == 0)
2878                         lsa->lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
2879                 /*
2880                  * In case of Data-on-MDT patterns the only extra option
2881                  * applicable is stripe size option.
2882                  */
2883                 if (lsa->lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
2884                         fprintf(stderr,
2885                                 "Option 'stripe-count' can't be specified with Data-on-MDT component: %lld\n",
2886                                 lsa->lsa_stripe_count);
2887                         errno = EINVAL;
2888                         return -1;
2889                 }
2890                 if (lsa->lsa_stripe_size != LLAPI_LAYOUT_DEFAULT &&
2891                     lsa->lsa_stripe_size != lsa->lsa_comp_end - prev_end) {
2892                         fprintf(stderr,
2893                                 "Option 'stripe-size' can't be specified with Data-on-MDT component: %llu\n",
2894                                 lsa->lsa_stripe_size);
2895                         errno = EINVAL;
2896                         return -1;
2897                 }
2898                 if (lsa->lsa_nr_tgts != 0) {
2899                         fprintf(stderr,
2900                                 "Option 'ost-list' can't be specified with Data-on-MDT component: '%i'\n",
2901                                 lsa->lsa_nr_tgts);
2902                         errno = EINVAL;
2903                         return -1;
2904                 }
2905                 if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT) {
2906                         fprintf(stderr,
2907                                 "Option 'stripe-offset' can't be specified with Data-on-MDT component: %lld\n",
2908                                 lsa->lsa_stripe_off);
2909                         errno = EINVAL;
2910                         return -1;
2911                 }
2912                 if (lsa->lsa_pool_name != 0) {
2913                         fprintf(stderr,
2914                                 "Option 'pool' can't be specified with Data-on-MDT component: '%s'\n",
2915                                 lsa->lsa_pool_name);
2916                         errno = EINVAL;
2917                         return -1;
2918                 }
2919
2920                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2921                 if (rc) {
2922                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2923                                 lsa->lsa_pattern,
2924                                 strerror(errno));
2925                         return rc;
2926                 }
2927                 /* Data-on-MDT component has always single stripe up to end */
2928                 lsa->lsa_stripe_size = lsa->lsa_comp_end;
2929         } else if (lsa->lsa_pattern == LLAPI_LAYOUT_OVERSTRIPING) {
2930                 rc = llapi_layout_pattern_set(layout, lsa->lsa_pattern);
2931                 if (rc) {
2932                         fprintf(stderr, "Set stripe pattern %#llx failed. %s\n",
2933                                 lsa->lsa_pattern,
2934                                 strerror(errno));
2935                         return rc;
2936                 }
2937         }
2938
2939         size = lsa->lsa_comp_flags & LCME_FL_EXTENSION ?
2940                 lsa->lsa_extension_size : lsa->lsa_stripe_size;
2941
2942         if (lsa->lsa_comp_flags & LCME_FL_EXTENSION)
2943                 rc = llapi_layout_extension_size_set(layout, size);
2944         else
2945                 rc = llapi_layout_stripe_size_set(layout, size);
2946
2947         if (rc) {
2948                 fprintf(stderr, "Set stripe size %lu failed: %s\n",
2949                         size, strerror(errno));
2950                 return rc;
2951         }
2952
2953         rc = llapi_layout_stripe_count_set(layout, lsa->lsa_stripe_count);
2954         if (rc) {
2955                 fprintf(stderr, "Set stripe count %lld failed: %s\n",
2956                         lsa->lsa_stripe_count, strerror(errno));
2957                 return rc;
2958         }
2959
2960         if (lsa->lsa_pool_name) {
2961                 rc = llapi_layout_pool_name_set(layout, lsa->lsa_pool_name);
2962                 if (rc) {
2963                         fprintf(stderr, "Set pool name: %s failed. %s\n",
2964                                 lsa->lsa_pool_name, strerror(errno));
2965                         return rc;
2966                 }
2967         } else {
2968                 rc = llapi_layout_pool_name_set(layout, "");
2969                 if (rc) {
2970                         fprintf(stderr, "Clear pool name failed: %s\n",
2971                                 strerror(errno));
2972                         return rc;
2973                 }
2974         }
2975
2976         rc = lsa_args_stripe_count_check(lsa);
2977         if (rc)
2978                 return rc;
2979
2980         if (lsa->lsa_nr_tgts > 0) {
2981                 bool found = false;
2982
2983                 for (i = 0; i < lsa->lsa_nr_tgts; i++) {
2984                         rc = llapi_layout_ost_index_set(layout, i,
2985                                                         lsa->lsa_tgts[i]);
2986                         if (rc)
2987                                 break;
2988
2989                         /* Make sure stripe offset is in OST list. */
2990                         if (lsa->lsa_tgts[i] == lsa->lsa_stripe_off)
2991                                 found = true;
2992                 }
2993                 if (!found) {
2994                         fprintf(stderr, "Invalid stripe offset '%lld', not in the target list",
2995                                 lsa->lsa_stripe_off);
2996                         errno = EINVAL;
2997                         return -1;
2998                 }
2999         } else if (lsa->lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
3000                    lsa->lsa_stripe_off != -1) {
3001                 rc = llapi_layout_ost_index_set(layout, 0, lsa->lsa_stripe_off);
3002         }
3003         if (rc) {
3004                 fprintf(stderr, "Set ost index %d failed. %s\n",
3005                         i, strerror(errno));
3006                 return rc;
3007         }
3008
3009         /* Create the second, virtual component of extension space */
3010         if (lsa->lsa_extension_comp) {
3011                 lsa->lsa_comp_flags |= LCME_FL_EXTENSION;
3012                 lsa->lsa_extension_comp = false;
3013                 goto new_comp;
3014         }
3015
3016         return rc;
3017 }
3018
3019 static int build_component(struct llapi_layout **layout,
3020                            struct lfs_setstripe_args *lsa, bool set_extent)
3021 {
3022         int rc;
3023
3024         rc = comp_args_to_layout(layout, lsa, set_extent);
3025         if (rc)
3026                 return rc;
3027
3028         if (lsa->lsa_mirror_count > 0) {
3029                 rc = llapi_layout_mirror_count_set(*layout,
3030                                                    lsa->lsa_mirror_count);
3031                 if (rc)
3032                         return rc;
3033
3034                 rc = llapi_layout_flags_set(*layout, LCM_FL_RDONLY);
3035                 if (rc)
3036                         return rc;
3037                 lsa->lsa_mirror_count = 0;
3038         }
3039
3040         return rc;
3041 }
3042
3043 static int build_prev_component(struct llapi_layout **layout,
3044                                 struct lfs_setstripe_args *prev,
3045                                 struct lfs_setstripe_args *lsa,
3046                                 bool set_extent)
3047 {
3048         int extension = lsa->lsa_comp_flags & LCME_FL_EXTENSION;
3049         int rc;
3050
3051         if (prev->lsa_stripe_size) {
3052                 if (extension) {
3053                         prev->lsa_comp_end = lsa->lsa_comp_end;
3054                         prev->lsa_extension_size = lsa->lsa_extension_size;
3055                         prev->lsa_extension_comp = true;
3056                 }
3057
3058                 rc = build_component(layout, prev, true);
3059                 if (rc)
3060                         return rc;
3061         }
3062
3063         /*
3064          * Copy lsa to previous lsa;
3065          * if this is an extension component, make the previous invalid;
3066          */
3067         if (extension)
3068                 prev->lsa_stripe_size = 0;
3069         else
3070                 *prev = *lsa;
3071
3072         return 0;
3073 }
3074
3075 #ifndef LCME_TEMPLATE_FLAGS
3076 #define LCME_TEMPLATE_FLAGS     (LCME_FL_PREF_RW | LCME_FL_NOSYNC | \
3077                                  LCME_FL_EXTENSION)
3078 #endif
3079
3080 static int build_layout_from_yaml_node(struct cYAML *node,
3081                                        struct llapi_layout **layout,
3082                                        struct lfs_setstripe_args *lsa,
3083                                        struct lfs_setstripe_args *prevp)
3084 {
3085         struct lfs_setstripe_args prev = { 0 };
3086         __u32 *osts = lsa->lsa_tgts;
3087         char *string;
3088         int rc = 0;
3089
3090         if (!prevp)
3091                 prevp = &prev;
3092
3093         while (node) {
3094                 string = node->cy_string;
3095
3096                 if (node->cy_type == CYAML_TYPE_OBJECT) {
3097                         /* go deep to sub blocks */
3098                         if (string && !strncmp(string, "component", 9) &&
3099                             strncmp(string, "component0", 10) &&
3100                             strncmp(string, "components", 10)) {
3101                                 rc = build_prev_component(layout, prevp, lsa,
3102                                                           true);
3103                                 if (rc)
3104                                         return rc;
3105
3106                                 /* initialize lsa. */
3107                                 setstripe_args_init(lsa);
3108                                 lsa->lsa_first_comp = false;
3109                                 lsa->lsa_tgts = osts;
3110                         }
3111
3112                         rc = build_layout_from_yaml_node(node->cy_child, layout,
3113                                                          lsa, prevp);
3114                         if (rc)
3115                                 return rc;
3116                 } else {
3117                         if (!node->cy_string)
3118                                 return -EINVAL;
3119
3120                         /* skip leading lmm_ if present, to simplify parsing */
3121                         if (strncmp(string, "lmm_", 4) == 0)
3122                                 string += 4;
3123
3124                         if (node->cy_type == CYAML_TYPE_STRING) {
3125                                 if (!strcmp(string, "lcme_extent.e_end")) {
3126                                         if (!strcmp(node->cy_valuestring, "EOF") ||
3127                                             !strcmp(node->cy_valuestring, "eof"))
3128                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3129                                 } else if (!strcmp(string, "pool")) {
3130                                         lsa->lsa_pool_name = node->cy_valuestring;
3131                                 } else if (!strcmp(string, "pattern")) {
3132                                         if (!strcmp(node->cy_valuestring, "mdt"))
3133                                                 lsa->lsa_pattern = LLAPI_LAYOUT_MDT;
3134                                         if (!strcmp(node->cy_valuestring,
3135                                                     "raid0,overstriped"))
3136                                                 lsa->lsa_pattern =
3137                                                         LLAPI_LAYOUT_OVERSTRIPING;
3138                                 } else if (!strcmp(string, "lcme_flags")) {
3139                                         rc = comp_str2flags(node->cy_valuestring,
3140                                                             &lsa->lsa_comp_flags,
3141                                                             &lsa->lsa_comp_neg_flags);
3142                                         if (rc)
3143                                                 return rc;
3144                                         /*
3145                                          * Only template flags have meaning in
3146                                          * the layout for a new file
3147                                          */
3148                                         lsa->lsa_comp_flags &= LCME_TEMPLATE_FLAGS;
3149                                 }
3150                         } else if (node->cy_type == CYAML_TYPE_NUMBER) {
3151                                 if (!strcmp(string, "lcm_mirror_count")) {
3152                                         lsa->lsa_mirror_count = node->cy_valueint;
3153                                 } else if (!strcmp(string, "lcme_extent.e_start")) {
3154                                         if (node->cy_valueint == 0)
3155                                                 lsa->lsa_first_comp = true;
3156                                 } else if (!strcmp(string, "lcme_extent.e_end")) {
3157                                         if (node->cy_valueint == -1)
3158                                                 lsa->lsa_comp_end = LUSTRE_EOF;
3159                                         else
3160                                                 lsa->lsa_comp_end = node->cy_valueint;
3161                                 } else if (!strcmp(string, "stripe_count")) {
3162                                         lsa->lsa_stripe_count = node->cy_valueint;
3163                                 } else if (!strcmp(string, "stripe_size")) {
3164                                         lsa->lsa_stripe_size = node->cy_valueint;
3165                                 } else if (!strcmp(string, "extension_size")) {
3166                                         lsa->lsa_extension_size = node->cy_valueint;
3167                                         lsa->lsa_extension_comp = true;
3168                                 } else if (!strcmp(string, "stripe_offset")) {
3169                                         lsa->lsa_stripe_off = node->cy_valueint;
3170                                 } else if (!strcmp(string, "l_ost_idx")) {
3171                                         osts[lsa->lsa_nr_tgts] = node->cy_valueint;
3172                                         lsa->lsa_nr_tgts++;
3173                                 }
3174                         }
3175                 }
3176                 node = node->cy_next;
3177         }
3178
3179         if (prevp == &prev) {
3180                 rc = build_prev_component(layout, prevp, lsa, true);
3181                 if (rc)
3182                         return rc;
3183
3184                 if (!(lsa->lsa_comp_flags & LCME_FL_EXTENSION))
3185                         rc = build_component(layout, lsa, *layout != NULL);
3186         }
3187
3188         return rc;
3189 }
3190
3191 static int lfs_comp_create_from_yaml(char *template,
3192                                      struct llapi_layout **layout,
3193                                      struct lfs_setstripe_args *lsa,
3194                                      __u32 *osts)
3195 {
3196         struct cYAML *tree = NULL, *err_rc = NULL;
3197         int rc = 0;
3198
3199         tree = cYAML_build_tree(template, NULL, 0, &err_rc, false);
3200         if (!tree) {
3201                 fprintf(stderr, "%s: cannot parse YAML file %s\n",
3202                         progname, template);
3203                 cYAML_build_error(-EINVAL, -1, "yaml", "from comp yaml",
3204                                   "can't parse", &err_rc);
3205                 cYAML_print_tree2file(stderr, err_rc);
3206                 cYAML_free_tree(err_rc);
3207                 rc = -EINVAL;
3208                 goto err;
3209         }
3210
3211         /* initialize lsa for plain file */
3212         setstripe_args_init(lsa);
3213         lsa->lsa_tgts = osts;
3214
3215         rc = build_layout_from_yaml_node(tree, layout, lsa, NULL);
3216         if (rc) {
3217                 fprintf(stderr, "%s: cannot build layout from YAML file %s.\n",
3218                         progname, template);
3219                 goto err;
3220         }
3221         /* clean clean lsa */
3222         setstripe_args_init(lsa);
3223
3224 err:
3225         if (tree)
3226                 cYAML_free_tree(tree);
3227         return rc;
3228 }
3229
3230 /**
3231  * Get the extension size from the next (SEL) component and extend the
3232  * current component on it. The start of the next component is to be
3233  * adjusted as well.
3234  *
3235  * \param[in] layout    the current layout
3236  * \param[in] start     the start of the current component
3237  * \param[in,out] end   the end of the current component
3238  * \param[in] offset    the offset to adjust the end position to instead of
3239  *                      extension size
3240  *
3241  * \retval 0            - extended successfully
3242  * \retval < 0          - error
3243  */
3244 static int layout_extend_comp(struct llapi_layout *layout,
3245                               uint64_t start, uint64_t *end,
3246                               uint64_t offset)
3247 {
3248         uint64_t size, next_start, next_end;
3249         int rc;
3250
3251         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_NEXT);
3252         if (rc < 0) {
3253                 fprintf(stderr,
3254                         "%s setstripe: cannot move component cursor: %s\n",
3255                         progname, strerror(errno));
3256                 return rc;
3257         }
3258
3259         /*
3260          * Even if the @size will not be used below, this will fail if
3261          * this is not a SEL component - a good confirmation we are
3262          * working on right components.
3263          */
3264         rc = llapi_layout_extension_size_get(layout, &size);
3265         if (rc < 0) {
3266                 fprintf(stderr,
3267                         "%s setstripe: cannot get component ext size: %s\n",
3268                         progname, strerror(errno));
3269                 return rc;
3270         }
3271
3272         rc = llapi_layout_comp_extent_get(layout, &next_start, &next_end);
3273         if (rc) {
3274                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3275                         progname, strerror(errno));
3276                 return rc;
3277         }
3278
3279         next_start += offset ?: size;
3280         rc = llapi_layout_comp_extent_set(layout, next_start, next_end);
3281         if (rc) {
3282                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3283                         progname, strerror(errno));
3284                 return rc;
3285         }
3286
3287         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_PREV);
3288         if (rc < 0) {
3289                 fprintf(stderr,
3290                         "%s setstripe: cannot move component cursor: %s\n",
3291                         progname, strerror(errno));
3292                 return rc;
3293         }
3294
3295         *end += offset ?: size;
3296         rc = llapi_layout_comp_extent_set(layout, start, *end);
3297         if (rc) {
3298                 fprintf(stderr, "%s setstripe: cannot set extent: %s\n",
3299                         progname, strerror(errno));
3300                 return rc;
3301         }
3302
3303         return 0;
3304 }
3305
3306 /**
3307  * In 'lfs setstripe --component-add' mode, we need to fetch the extent
3308  * end of the last component in the existing file, and adjust the
3309  * first extent start of the components to be added accordingly.
3310  *
3311  * In the create mode, we need to check if the first component is an extendable
3312  * SEL component and extend its length to the extension size (first component
3313  * of the PFL file is initialised at the create time, cannot be 0-lenght.
3314  */
3315 static int layout_adjust_first_extent(char *fname, struct llapi_layout *layout,
3316                                       bool comp_add)
3317 {
3318         struct llapi_layout *head;
3319         uint64_t start = 0, prev_end = 0;
3320         uint64_t end;
3321         int rc, ret = 0;
3322
3323         if (!layout || !(comp_add || llapi_layout_is_composite(layout)))
3324                 return 0;
3325
3326         errno = 0;
3327         while (comp_add) {
3328                 head = llapi_layout_get_by_path(fname, 0);
3329                 if (!head) {
3330                         fprintf(stderr,
3331                                 "%s setstripe: cannot read layout from '%s': %s\n",
3332                                 progname, fname, strerror(errno));
3333                         return -EINVAL;
3334                 } else if (errno == ENODATA) {
3335                         /*
3336                          * file without LOVEA, this component-add will be turned
3337                          * into a component-create.
3338                          */
3339                         llapi_layout_free(head);
3340                         ret = -ENODATA;
3341
3342                         /*
3343                          * the new layout will be added to an empty one, it
3344                          * still needs to be adjusted below
3345                          */
3346                         comp_add = 0;
3347                         break;
3348                 } else if (!llapi_layout_is_composite(head)) {
3349                         fprintf(stderr,
3350                                 "%s setstripe: '%s' not a composite file\n",
3351                                 progname, fname);
3352                         llapi_layout_free(head);
3353                         return -EINVAL;
3354                 }
3355
3356                 rc = llapi_layout_comp_extent_get(head, &start, &prev_end);
3357                 if (rc) {
3358                         fprintf(stderr,
3359                                 "%s setstripe: cannot get prev extent: %s\n",
3360                                 progname, strerror(errno));
3361                         llapi_layout_free(head);
3362                         return rc;
3363                 }
3364
3365                 llapi_layout_free(head);
3366                 break;
3367         }
3368
3369         /* Make sure we use the first component of the layout to be added. */
3370         rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
3371         if (rc < 0) {
3372                 fprintf(stderr,
3373                         "%s setstripe: cannot move component cursor: %s\n",
3374                         progname, strerror(errno));
3375                 return rc;
3376         }
3377
3378         rc = llapi_layout_comp_extent_get(layout, &start, &end);
3379         if (rc) {
3380                 fprintf(stderr, "%s setstripe: cannot get extent: %s\n",
3381                         progname, strerror(errno));
3382                 return rc;
3383         }
3384
3385         if (start == 0 && end == 0) {
3386                 rc = layout_extend_comp(layout, start, &end,
3387                                         comp_add ? prev_end : 0);
3388                 if (rc)
3389                         return rc;
3390         }
3391
3392         if (start > prev_end || end < prev_end) {
3393                 fprintf(stderr,
3394                         "%s setstripe: first extent [%lu, %lu) not adjacent with extent end %lu\n",
3395                         progname, start, end, prev_end);
3396                 return -EINVAL;
3397         }
3398
3399         rc = llapi_layout_comp_extent_set(layout, prev_end, end);
3400         if (rc) {
3401                 fprintf(stderr,
3402                         "%s setstripe: cannot set component extent [%lu, %lu): %s\n",
3403                         progname, prev_end, end, strerror(errno));
3404                 return rc;
3405         }
3406
3407         return ret;
3408 }
3409
3410 static int mirror_adjust_first_extents(struct mirror_args *list)
3411 {
3412         int rc = 0;
3413
3414         if (!list)
3415                 return 0;
3416
3417         while (list) {
3418                 rc = layout_adjust_first_extent(NULL, list->m_layout, false);
3419                 if (rc)
3420                         break;
3421                 list = list->m_next;
3422         }
3423
3424         return rc;
3425 }
3426
3427 static inline bool arg_is_eof(char *arg)
3428 {
3429         return !strncmp(arg, "-1", strlen("-1")) ||
3430                !strncmp(arg, "EOF", strlen("EOF")) ||
3431                !strncmp(arg, "eof", strlen("eof"));
3432 }
3433
3434 /**
3435  * lfs_mirror_alloc() - Allocate a mirror argument structure.
3436  *
3437  * Return: Valid mirror_args pointer on success and
3438  *         NULL if memory allocation fails.
3439  */
3440 static struct mirror_args *lfs_mirror_alloc(void)
3441 {
3442         struct mirror_args *mirror = NULL;
3443
3444         while (1) {
3445                 mirror = calloc(1, sizeof(*mirror));
3446                 if (mirror) {
3447                         mirror->m_inherit = false;
3448                         break;
3449                 }
3450
3451                 sleep(1);
3452         }
3453
3454         return mirror;
3455 }
3456
3457 /**
3458  * lfs_mirror_free() - Free memory allocated for a mirror argument
3459  *                     structure.
3460  * @mirror: Previously allocated mirror argument structure by
3461  *          lfs_mirror_alloc().
3462  *
3463  * Free memory allocated for @mirror.
3464  *
3465  * Return: void.
3466  */
3467 static void lfs_mirror_free(struct mirror_args *mirror)
3468 {
3469         if (mirror->m_layout)
3470                 llapi_layout_free(mirror->m_layout);
3471         free(mirror);
3472 }
3473
3474 /**
3475  * lfs_mirror_list_free() - Free memory allocated for a mirror list.
3476  * @mirror_list: Previously allocated mirror list.
3477  *
3478  * Free memory allocated for @mirror_list.
3479  *
3480  * Return: void.
3481  */
3482 static void lfs_mirror_list_free(struct mirror_args *mirror_list)
3483 {
3484         struct mirror_args *next_mirror = NULL;
3485
3486         while (mirror_list) {
3487                 next_mirror = mirror_list->m_next;
3488                 lfs_mirror_free(mirror_list);
3489                 mirror_list = next_mirror;
3490         }
3491 }
3492
3493 enum {
3494         LFS_SETQUOTA_DELETE = (CHAR_MAX + 1),
3495         LFS_POOL_OPT,
3496         LFS_COMP_COUNT_OPT,
3497         LFS_COMP_START_OPT,
3498         LFS_COMP_FLAGS_OPT,
3499         LFS_COMP_DEL_OPT,
3500         LFS_COMP_SET_OPT,
3501         LFS_COMP_ADD_OPT,
3502         LFS_COMP_NO_VERIFY_OPT,
3503         LFS_PROJID_OPT,
3504         LFS_LAYOUT_FLAGS_OPT, /* used for mirror and foreign flags */
3505         LFS_MIRROR_ID_OPT,
3506         LFS_MIRROR_STATE_OPT,
3507         LFS_LAYOUT_COPY,
3508         LFS_MIRROR_INDEX_OPT,
3509         LFS_LAYOUT_FOREIGN_OPT,
3510         LFS_MODE_OPT,
3511         LFS_NEWERXY_OPT,
3512         LFS_INHERIT_RR_OPT,
3513         LFS_FIND_PERM,
3514         LFS_PRINTF_OPT,
3515         LFS_NO_FOLLOW_OPT,
3516         LFS_STATS_OPT,
3517         LFS_STATS_INTERVAL_OPT
3518 };
3519
3520 #ifndef LCME_USER_MIRROR_FLAGS
3521 /* The mirror flags can be set by users at creation time. */
3522 #define LCME_USER_MIRROR_FLAGS  (LCME_FL_PREF_RW)
3523 #endif
3524
3525 /* functions */
3526 static int lfs_setstripe_internal(int argc, char **argv,
3527                                   enum setstripe_origin opc)
3528 {
3529         struct lfs_setstripe_args        lsa = { 0 };
3530         struct llapi_stripe_param       *param = NULL;
3531         struct find_param                migrate_mdt_param = {
3532                 .fp_max_depth = -1,
3533                 .fp_mdt_index = -1,
3534         };
3535         char                            *fname;
3536         int                              result = 0;
3537         int                              result2 = 0;
3538         char                            *end;
3539         int                              c;
3540         int                              delete = 0;
3541         unsigned long long               size_units = 1;
3542         bool                             migrate_mode = false;
3543         bool                             migrate_mdt_mode = false;
3544         bool                             setstripe_mode = false;
3545         bool                             migration_block = false;
3546         __u64                            migration_flags = 0;
3547         __u32                            tgts[LOV_MAX_STRIPE_COUNT] = { 0 };
3548         int                              comp_del = 0, comp_set = 0;
3549         int                              comp_add = 0;
3550         __u32                            comp_id = 0;
3551         struct llapi_layout             *layout = NULL;
3552         struct llapi_layout             **lpp = &layout;
3553         bool                             mirror_mode = false;
3554         bool                             has_m_file = false;
3555         __u32                            mirror_count = 0;
3556         enum mirror_flags                mirror_flags = 0;
3557         struct mirror_args              *mirror_list = NULL;
3558         struct mirror_args              *new_mirror = NULL;
3559         struct mirror_args              *last_mirror = NULL;
3560         __u16                            mirror_id = 0;
3561         char                             cmd[PATH_MAX];
3562         bool from_yaml = false;
3563         bool from_copy = false;
3564         char *template = NULL;
3565         bool foreign_mode = false;
3566         char *xattr = NULL;
3567         uint32_t type = LU_FOREIGN_TYPE_NONE, flags = 0;
3568         char *mode_opt = NULL;
3569         mode_t previous_umask = 0;
3570         mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
3571         unsigned long long bandwidth_bytes_sec = 0;
3572         unsigned long long bandwidth_unit = ONE_MB;
3573         enum stats_flag stats_flag = STATS_OFF;
3574         long stats_interval_sec = 5;
3575
3576         struct option long_opts[] = {
3577 /* find { .val = '0',   .name = "null",         .has_arg = no_argument }, */
3578 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
3579         /* --block is only valid in migrate mode */
3580         { .val = 'b',   .name = "block",        .has_arg = no_argument },
3581 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
3582         { .val = LFS_COMP_ADD_OPT,
3583                         .name = "comp-add",     .has_arg = no_argument },
3584         { .val = LFS_COMP_ADD_OPT,
3585                         .name = "component-add", .has_arg = no_argument },
3586         { .val = LFS_COMP_DEL_OPT,
3587                         .name = "comp-del",     .has_arg = no_argument },
3588         { .val = LFS_COMP_DEL_OPT,
3589                         .name = "component-del", .has_arg = no_argument },
3590         { .val = LFS_COMP_FLAGS_OPT,
3591                         .name = "comp-flags",   .has_arg = required_argument },
3592         { .val = LFS_COMP_FLAGS_OPT,
3593                         .name = "component-flags",
3594                                                 .has_arg = required_argument },
3595         { .val = LFS_COMP_SET_OPT,
3596                         .name = "comp-set",     .has_arg = no_argument },
3597         { .val = LFS_COMP_SET_OPT,
3598                         .name = "component-set",
3599                                                 .has_arg = no_argument},
3600         { .val = LFS_COMP_NO_VERIFY_OPT,
3601                         .name = "no-verify",    .has_arg = no_argument},
3602         { .val = LFS_LAYOUT_FLAGS_OPT,
3603                         .name = "flags",        .has_arg = required_argument},
3604         { .val = LFS_LAYOUT_FOREIGN_OPT,
3605                         .name = "foreign",      .has_arg = optional_argument},
3606         { .val = LFS_MIRROR_ID_OPT,
3607                         .name = "mirror-id",    .has_arg = required_argument},
3608         { .val = LFS_MODE_OPT,
3609                         .name = "mode",         .has_arg = required_argument},
3610         { .val = LFS_LAYOUT_COPY,
3611                         .name = "copy",         .has_arg = required_argument},
3612         { .val = LFS_STATS_OPT,
3613                         .name = "stats",        .has_arg = no_argument},
3614         { .val = LFS_STATS_INTERVAL_OPT,
3615                         .name = "stats-interval",
3616                                                 .has_arg = required_argument},
3617         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument},
3618         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument},
3619         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument},
3620         { .val = 'C',   .name = "overstripe-count",
3621                                                 .has_arg = required_argument},
3622         { .val = 'd',   .name = "delete",       .has_arg = no_argument},
3623         { .val = 'd',   .name = "destroy",      .has_arg = no_argument},
3624         /* used with "lfs migrate -m" */
3625         { .val = 'd',   .name = "directory",    .has_arg = no_argument},
3626         /* --non-direct is only valid in migrate mode */
3627         { .val = 'D',   .name = "non-direct",   .has_arg = no_argument },
3628         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument},
3629         { .val = 'E',   .name = "component-end",
3630                                                 .has_arg = required_argument},
3631         { .val = 'f',   .name = "file",         .has_arg = required_argument },
3632 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
3633 /* find { .val = 'g',   .name = "gid",          .has_arg = no_argument }, */
3634 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
3635         { .val = 'h',   .name = "help",         .has_arg = no_argument },
3636         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument},
3637         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument},
3638         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument},
3639         { .val = 'I',   .name = "comp-id",      .has_arg = required_argument},
3640         { .val = 'I',   .name = "component-id", .has_arg = required_argument},
3641 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
3642         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
3643         { .val = 'm',   .name = "mdt",          .has_arg = required_argument},
3644         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument},
3645         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument},
3646         /* --non-block is only valid in migrate mode */
3647         { .val = 'n',   .name = "non-block",    .has_arg = no_argument },
3648         { .val = 'N',   .name = "mirror-count", .has_arg = optional_argument},
3649         { .val = 'o',   .name = "ost",          .has_arg = required_argument },
3650 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
3651         { .val = 'o',   .name = "ost-list",     .has_arg = required_argument },
3652         { .val = 'o',   .name = "ost_list",     .has_arg = required_argument },
3653 #endif
3654         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
3655 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
3656 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
3657 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
3658         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
3659         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
3660 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
3661 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
3662 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
3663 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
3664         /* --verbose is only valid in migrate mode */
3665         { .val = 'v',   .name = "verbose",      .has_arg = no_argument},
3666         { .val = 'W',   .name = "bandwidth",    .has_arg = required_argument },
3667         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
3668 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
3669         { .val = 'y',   .name = "yaml",         .has_arg = required_argument },
3670         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument},
3671         { .val = 'z',   .name = "extension-size", .has_arg = required_argument},
3672         { .name = NULL } };
3673
3674         setstripe_args_init(&lsa);
3675
3676         migrate_mode = (opc == SO_MIGRATE);
3677         mirror_mode = (opc == SO_MIRROR_CREATE || opc == SO_MIRROR_EXTEND);
3678         setstripe_mode = (opc == SO_SETSTRIPE);
3679         if (opc == SO_MIRROR_DELETE) {
3680                 delete = 1;
3681                 mirror_flags = MF_DESTROY;
3682         }
3683
3684         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
3685         progname = cmd;
3686         while ((c = getopt_long(argc, argv,
3687                                 "bc:C:dDE:f:hH:i:I:m:N::no:p:L:s:S:vx:W:y:z:",
3688                                 long_opts, NULL)) >= 0) {
3689                 size_units = 1;
3690                 switch (c) {
3691                 case 0:
3692                         /* Long options. */
3693                         break;
3694                 case LFS_COMP_ADD_OPT:
3695                         comp_add = 1;
3696                         break;
3697                 case LFS_COMP_DEL_OPT:
3698                         comp_del = 1;
3699                         break;
3700                 case LFS_COMP_FLAGS_OPT:
3701                         result = comp_str2flags(optarg, &lsa.lsa_comp_flags,
3702                                                 &lsa.lsa_comp_neg_flags);
3703                         if (result != 0)
3704                                 goto usage_error;
3705                         if (mirror_mode && lsa.lsa_comp_neg_flags) {
3706                                 fprintf(stderr,
3707                                         "%s: inverted flags are not supported\n",
3708                                         progname);
3709                                 goto usage_error;
3710                         }
3711                         break;
3712                 case LFS_COMP_SET_OPT:
3713                         comp_set = 1;
3714                         break;
3715                 case LFS_COMP_NO_VERIFY_OPT:
3716                         mirror_flags |= MF_NO_VERIFY;
3717                         break;
3718                 case LFS_MIRROR_ID_OPT: {
3719                         unsigned long int id;
3720
3721                         errno = 0;
3722                         id = strtoul(optarg, &end, 0);
3723                         if (errno != 0 || *end != '\0' || id == 0 ||
3724                             id > UINT16_MAX) {
3725                                 fprintf(stderr,
3726                                         "%s %s: invalid mirror ID '%s'\n",
3727                                         progname, argv[0], optarg);
3728                                 goto usage_error;
3729                         }
3730
3731                         mirror_id = (__u16)id;
3732                         break;
3733                 }
3734                 case LFS_LAYOUT_FLAGS_OPT: {
3735                         uint32_t neg_flags;
3736
3737                         /* check for numeric flags (foreign and mirror cases) */
3738                         if (setstripe_mode && !mirror_mode && !last_mirror) {
3739                                 errno = 0;
3740                                 flags = strtoul(optarg, &end, 16);
3741                                 if (errno != 0 || *end != '\0' ||
3742                                     flags >= UINT32_MAX) {
3743                                         fprintf(stderr,
3744                                                 "%s %s: invalid hex flags '%s'\n",
3745                                                 progname, argv[0], optarg);
3746                                         return CMD_HELP;
3747                                 }
3748                                 if (!foreign_mode) {
3749                                         fprintf(stderr,
3750                                                 "%s %s: hex flags must be specified with --foreign option\n",
3751                                                 progname, argv[0]);
3752                                         return CMD_HELP;
3753                                 }
3754                                 break;
3755                         }
3756
3757                         if (!mirror_mode || !last_mirror) {
3758                                 fprintf(stderr,
3759                                         "error: %s: --flags must be specified with --mirror-count|-N option\n",
3760                                         progname);
3761                                 goto usage_error;
3762                         }
3763
3764                         result = comp_str2flags(optarg, &last_mirror->m_flags,
3765                                                 &neg_flags);
3766                         if (result != 0)
3767                                 goto usage_error;
3768
3769                         if (neg_flags) {
3770                                 fprintf(stderr,
3771                                         "%s: inverted flags are not supported\n",
3772                                         progname);
3773                                 result = -EINVAL;
3774                                 goto usage_error;
3775                         }
3776                         if (last_mirror->m_flags & ~LCME_USER_MIRROR_FLAGS) {
3777                                 fprintf(stderr,
3778                                         "%s: unsupported mirror flags: %s\n",
3779                                         progname, optarg);
3780                                 result = -EINVAL;
3781                                 goto error;
3782                         }
3783                         break;
3784                 }
3785                 case LFS_LAYOUT_FOREIGN_OPT:
3786                         if (optarg) {
3787                                 /* check pure numeric */
3788                                 type = strtoul(optarg, &end, 0);
3789                                 if (*end) {
3790                                         /* check name */
3791                                         type = check_foreign_type_name(optarg);
3792                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
3793                                                 fprintf(stderr,
3794                                                         "%s %s: unrecognized foreign type '%s'\n",
3795                                                         progname, argv[0],
3796                                                         optarg);
3797                                                 return CMD_HELP;
3798                                         }
3799                                 } else if (type >= UINT32_MAX) {
3800                                         fprintf(stderr,
3801                                                 "%s %s: invalid foreign type '%s'\n",
3802                                                 progname, argv[0], optarg);
3803                                         return CMD_HELP;
3804                                 }
3805                         }
3806                         foreign_mode = true;
3807                         break;
3808                 case LFS_MODE_OPT:
3809                         mode_opt = optarg;
3810                         if (mode_opt) {
3811                                 mode = strtoul(mode_opt, &end, 8);
3812                                 if (*end != '\0') {
3813                                         fprintf(stderr,
3814                                                 "%s %s: bad mode '%s'\n",
3815                                                 progname, argv[0], mode_opt);
3816                                         return CMD_HELP;
3817                                 }
3818                                 previous_umask = umask(0);
3819                         }
3820                         break;
3821                 case LFS_LAYOUT_COPY:
3822                         from_copy = true;
3823                         template = optarg;
3824                         break;
3825                 case LFS_STATS_OPT:
3826                         stats_flag = STATS_ON;
3827                         break;
3828                 case LFS_STATS_INTERVAL_OPT:
3829                         stats_flag = STATS_ON;
3830                         stats_interval_sec = strtol(optarg, &end, 0);
3831                         if (stats_interval_sec == 0)
3832                                 stats_interval_sec = 5;
3833                         break;
3834                 case 'b':
3835                         if (!migrate_mode) {
3836                                 fprintf(stderr,
3837                                         "%s %s: -b|--block valid only for migrate command\n",
3838                                         progname, argv[0]);
3839                                 goto usage_error;
3840                         }
3841                         migration_block = true;
3842                         break;
3843                 case 'C':
3844                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
3845                                 fprintf(stderr,
3846                                         "%s %s: -C|--overstripe-count incompatible with DoM layout\n",
3847                                         progname, argv[0]);
3848                                 goto usage_error;
3849                         }
3850                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
3851                         fallthrough;
3852                 case 'c':
3853                         errno = 0;
3854                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
3855                         if (errno != 0 || *end != '\0'|| optarg == end ||
3856                             lsa.lsa_stripe_count < -1 ||
3857                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
3858                                 fprintf(stderr,
3859                                         "%s %s: invalid stripe count '%s'\n",
3860                                         progname, argv[0], optarg);
3861                                 goto usage_error;
3862                         }
3863
3864                         if (lsa.lsa_stripe_count == -1)
3865                                 lsa.lsa_stripe_count = LLAPI_LAYOUT_WIDE;
3866                         break;
3867                 case 'd':
3868                         if (migrate_mode) {
3869                                 migrate_mdt_param.fp_max_depth = 1;
3870                         } else {
3871                                 /* delete the default striping pattern */
3872                                 delete = 1;
3873                                 if (opc == SO_MIRROR_SPLIT) {
3874                                         if (has_m_file) {
3875                                                 fprintf(stderr,
3876                                                       "%s %s: -d cannot used with -f\n",
3877                                                         progname, argv[0]);
3878                                                 goto usage_error;
3879                                         }
3880                                         mirror_flags |= MF_DESTROY;
3881                                 }
3882                         }
3883                         break;
3884                 case 'D':
3885                         if (!migrate_mode) {
3886                                 fprintf(stderr,
3887                                         "%s %s: -D|--non-direct is valid only for migrate command\n",
3888                                         progname, argv[0]);
3889                                 goto usage_error;
3890                         }
3891                         migration_flags |= LLAPI_MIGRATION_NONDIRECT;
3892                         break;
3893                 case 'E':
3894                         if (lsa.lsa_comp_end != 0) {
3895                                 result = comp_args_to_layout(lpp, &lsa, true);
3896                                 if (result) {
3897                                         fprintf(stderr, "%s: invalid layout\n",
3898                                                 progname);
3899                                         goto usage_error;
3900                                 }
3901
3902                                 setstripe_args_init_inherit(&lsa);
3903                         }
3904
3905                         if (arg_is_eof(optarg)) {
3906                                 lsa.lsa_comp_end = LUSTRE_EOF;
3907                         } else {
3908                                 result = llapi_parse_size(optarg,
3909                                                           &lsa.lsa_comp_end,
3910                                                           &size_units, 0);
3911                                 /* assume units of KB if too small */
3912                                 if (lsa.lsa_comp_end < 4096)
3913                                         lsa.lsa_comp_end *= 1024;
3914                                 if (result ||
3915                                     lsa.lsa_comp_end & (LOV_MIN_STRIPE_SIZE - 1)) {
3916                                         fprintf(stderr,
3917                                                 "%s %s: invalid component end '%s'\n",
3918                                                 progname, argv[0], optarg);
3919                                         goto usage_error;
3920                                 }
3921                         }
3922                         break;
3923                 case 'H':
3924                         if (!migrate_mode) {
3925                                 fprintf(stderr,
3926                                         "--mdt-hash is valid only for migrate command\n");
3927                                 return CMD_HELP;
3928                         }
3929
3930                         lsa.lsa_pattern = check_hashtype(optarg);
3931                         if (lsa.lsa_pattern == 0) {
3932                                 fprintf(stderr,
3933                                         "%s %s: bad stripe hash type '%s'\n",
3934                                         progname, argv[0], optarg);
3935                                 return CMD_HELP;
3936                         }
3937                         break;
3938                 case 'i':
3939                         errno = 0;
3940                         lsa.lsa_stripe_off = strtol(optarg, &end, 0);
3941                         if (errno != 0 || *end != '\0' || optarg == end ||
3942                             lsa.lsa_stripe_off < -1 ||
3943                             lsa.lsa_stripe_off > LOV_V1_INSANE_STRIPE_COUNT) {
3944                                 fprintf(stderr,
3945                                         "%s %s: invalid stripe offset '%s'\n",
3946                                         progname, argv[0], optarg);
3947                                 goto usage_error;
3948                         }
3949                         if (lsa.lsa_stripe_off == -1)
3950                                 lsa.lsa_stripe_off = LLAPI_LAYOUT_DEFAULT;
3951                         break;
3952                 case 'I':
3953                         comp_id = strtoul(optarg, &end, 0);
3954                         if (*end != '\0' || comp_id == 0 ||
3955                             comp_id > LCME_ID_MAX) {
3956                                 fprintf(stderr,
3957                                         "%s %s: invalid component ID '%s'\n",
3958                                         progname, argv[0], optarg);
3959                                 goto usage_error;
3960                         }
3961                         break;
3962                 case 'f':
3963                         if (opc != SO_MIRROR_EXTEND && opc != SO_MIRROR_SPLIT) {
3964                                 fprintf(stderr,
3965                                         "error: %s: invalid option: %s\n",
3966                                         progname, argv[optopt + 1]);
3967                                 goto usage_error;
3968                         }
3969                         if (opc == SO_MIRROR_EXTEND) {
3970                                 if (!last_mirror) {
3971                                         fprintf(stderr,
3972                                 "error: %s: '-N' must exist in front of '%s'\n",
3973                                                 progname, argv[optopt + 1]);
3974                                         goto usage_error;
3975                                 }
3976                                 last_mirror->m_file = optarg;
3977                                 last_mirror->m_count = 1;
3978                         } else {
3979                                 /* mirror split */
3980                                 if (!mirror_list)
3981                                         mirror_list = lfs_mirror_alloc();
3982                                 mirror_list->m_file = optarg;
3983                         }
3984                         has_m_file = true;
3985                         break;
3986                 case 'L':
3987                         if (strcmp(argv[optind - 1], "mdt") == 0) {
3988                                 /* Can be only the first component */
3989                                 if (layout) {
3990                                         result = -EINVAL;
3991                                         fprintf(stderr,
3992                                                 "error: 'mdt' layout can be only the first one\n");
3993                                         goto error;
3994                                 }
3995                                 if (lsa.lsa_comp_end > (1ULL << 30)) { /* 1Gb */
3996                                         result = -EFBIG;
3997                                         fprintf(stderr,
3998                                                 "error: 'mdt' layout size is too big\n");
3999                                         goto error;
4000                                 }
4001                                 lsa.lsa_pattern = LLAPI_LAYOUT_MDT;
4002                                 lsa.lsa_stripe_size = LLAPI_LAYOUT_DEFAULT;
4003                         } else if (strcmp(argv[optind - 1], "raid0") != 0) {
4004                                 result = -EINVAL;
4005                                 fprintf(stderr,
4006                                         "error: layout '%s' is unknown, supported layouts are: 'mdt', 'raid0'\n",
4007                                         argv[optind]);
4008                                 goto error;
4009                         }
4010                         break;
4011                 case 'm':
4012                         if (!migrate_mode) {
4013                                 fprintf(stderr,
4014                                         "%s %s: -m|--mdt-index is valid only for migrate command\n",
4015                                         progname, argv[0]);
4016                                 goto usage_error;
4017                         }
4018                         migrate_mdt_mode = true;
4019                         lsa.lsa_nr_tgts = parse_targets(tgts,
4020                                                 sizeof(tgts) / sizeof(__u32),
4021                                                 lsa.lsa_nr_tgts, optarg, NULL);
4022                         if (lsa.lsa_nr_tgts < 0) {
4023                                 fprintf(stderr,
4024                                         "%s: invalid MDT target(s) '%s'\n",
4025                                         progname, optarg);
4026                                 goto usage_error;
4027                         }
4028
4029                         lsa.lsa_tgts = tgts;
4030                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4031                                 lsa.lsa_stripe_off = tgts[0];
4032                         break;
4033                 case 'n':
4034                         if (!migrate_mode) {
4035                                 fprintf(stderr,
4036                                         "%s %s: -n|--non-block valid only for migrate command\n",
4037                                         progname, argv[0]);
4038                                 goto usage_error;
4039                         }
4040                         migration_flags |= LLAPI_MIGRATION_NONBLOCK;
4041                         break;
4042                 case 'N':
4043                         if (opc == SO_SETSTRIPE) {
4044                                 opc = SO_MIRROR_CREATE;
4045                                 mirror_mode = true;
4046                         }
4047                         mirror_count = 1;
4048                         if (optarg) {
4049                                 errno = 0;
4050                                 mirror_count = strtoul(optarg, &end, 0);
4051                                 if (errno != 0 || *end != '\0' ||
4052                                     mirror_count == 0 ||
4053                                     mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
4054                                         fprintf(stderr,
4055                                                 "error: %s: bad mirror count: %s\n",
4056                                                 progname, optarg);
4057                                         result = -EINVAL;
4058                                         goto error;
4059                                 }
4060                         }
4061
4062                         new_mirror = lfs_mirror_alloc();
4063                         new_mirror->m_count = mirror_count;
4064
4065                         if (!mirror_list)
4066                                 mirror_list = new_mirror;
4067
4068                         if (last_mirror) {
4069                                 /* wrap up last mirror */
4070                                 if (!setstripe_args_specified(&lsa))
4071                                         last_mirror->m_inherit = true;
4072                                 if (lsa.lsa_comp_end == 0)
4073                                         lsa.lsa_comp_end = LUSTRE_EOF;
4074
4075                                 result = comp_args_to_layout(lpp, &lsa, true);
4076                                 if (result) {
4077                                         lfs_mirror_free(new_mirror);
4078                                         goto error;
4079                                 }
4080
4081                                 setstripe_args_init_inherit(&lsa);
4082
4083                                 last_mirror->m_next = new_mirror;
4084                         }
4085
4086                         last_mirror = new_mirror;
4087                         lpp = &last_mirror->m_layout;
4088                         break;
4089                 case 'o':
4090 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
4091                         if (strcmp(argv[optind - 1], "--ost-list") == 0)
4092                                 fprintf(stderr,
4093                                         "warning: '--ost-list' is deprecated, use '--ost' instead\n");
4094 #endif
4095                         if (lsa.lsa_pattern == LLAPI_LAYOUT_MDT) {
4096                                 fprintf(stderr,
4097                                         "%s %s: -o|--ost incompatible with DoM layout\n",
4098                                         progname, argv[0]);
4099                                 goto usage_error;
4100                         }
4101                         /*
4102                          * -o allows overstriping, and must note it because
4103                          * parse_targets is shared with MDT striping, which
4104                          * does not allow duplicates
4105                          */
4106                         lsa.lsa_pattern = LLAPI_LAYOUT_OVERSTRIPING;
4107                         lsa.lsa_nr_tgts = parse_targets(tgts,
4108                                                 sizeof(tgts) / sizeof(__u32),
4109                                                 lsa.lsa_nr_tgts, optarg,
4110                                                 &lsa.lsa_pattern);
4111                         if (lsa.lsa_nr_tgts < 0) {
4112                                 fprintf(stderr,
4113                                         "%s %s: invalid OST target(s) '%s'\n",
4114                                         progname, argv[0], optarg);
4115                                 goto usage_error;
4116                         }
4117
4118                         lsa.lsa_tgts = tgts;
4119                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4120                                 lsa.lsa_stripe_off = tgts[0];
4121                         break;
4122                 case 'p':
4123                         if (!optarg)
4124                                 goto usage_error;
4125
4126                         if (optarg[0] == '\0' || lov_pool_is_inherited(optarg))
4127                                 lsa.lsa_pool_name = NULL;
4128                         else
4129                                 lsa.lsa_pool_name = optarg;
4130                         break;
4131                 case 'S':
4132                         result = llapi_parse_size(optarg, &lsa.lsa_stripe_size,
4133                                                   &size_units, 0);
4134                         /* assume units of KB if too small to be valid */
4135                         if (lsa.lsa_stripe_size < 4096)
4136                                 lsa.lsa_stripe_size *= 1024;
4137                         if (result ||
4138                             lsa.lsa_stripe_size & (LOV_MIN_STRIPE_SIZE - 1)) {
4139                                 fprintf(stderr,
4140                                         "%s %s: invalid stripe size '%s'\n",
4141                                         progname, argv[0], optarg);
4142                                 goto usage_error;
4143                         }
4144                         break;
4145                 case 'v':
4146                         if (!migrate_mode) {
4147                                 fprintf(stderr,
4148                                         "%s %s: -v|--verbose valid only for migrate command\n",
4149                                         progname, argv[0]);
4150                                 goto usage_error;
4151                         }
4152                         migrate_mdt_param.fp_verbose = VERBOSE_DETAIL;
4153                         migration_flags = LLAPI_MIGRATION_VERBOSE;
4154                         break;
4155                 case 'x':
4156                         xattr = optarg;
4157                         break;
4158                 case 'W':
4159                         if (!migrate_mode) {
4160                                 fprintf(stderr,
4161                                         "--bandwidth is valid only for migrate and mirror mode\n");
4162                                 goto error;
4163                         }
4164                         if (llapi_parse_size(optarg, &bandwidth_bytes_sec,
4165                                              &bandwidth_unit, 0) < 0) {
4166                                 fprintf(stderr,
4167                                         "error: %s: bad value for bandwidth '%s'\n",
4168                                         argv[0], optarg);
4169                                 goto error;
4170                         }
4171                         break;
4172                 case 'y':
4173                         from_yaml = true;
4174                         template = optarg;
4175                         break;
4176                 case 'z':
4177                         result = llapi_parse_size(optarg,
4178                                                   &lsa.lsa_extension_size,
4179                                                   &size_units, 0);
4180                         if (result) {
4181                                 fprintf(stderr,
4182                                         "%s %s: invalid extension size '%s'\n",
4183                                         progname, argv[0], optarg);
4184                                 goto usage_error;
4185                         }
4186
4187                         lsa.lsa_extension_comp = true;
4188                         break;
4189                 default:
4190                         fprintf(stderr, "%s: unrecognized option '%s'\n",
4191                                 progname, argv[optind - 1]);
4192                 case 'h':
4193                         goto usage_error;
4194                 }
4195         }
4196
4197         fname = argv[optind];
4198
4199         if (optind == argc) {
4200                 fprintf(stderr, "%s %s: FILE must be specified\n",
4201                         progname, argv[0]);
4202                 goto usage_error;
4203         }
4204
4205         /* lfs migrate $filename should keep the file's layout by default */
4206         if (migrate_mode && !layout && !from_yaml &&
4207             !setstripe_args_specified(&lsa) && !lsa.lsa_pool_name)
4208                 from_copy = true;
4209
4210         if (xattr && !foreign_mode) {
4211                 /*
4212                  * only print a warning as this is harmless and will be ignored
4213                  */
4214                 fprintf(stderr,
4215                         "%s %s: xattr has been specified for non-foreign layout\n",
4216                         progname, argv[0]);
4217         } else if (foreign_mode && !xattr) {
4218                 fprintf(stderr,
4219                         "%s %s: xattr must be provided in foreign mode\n",
4220                         progname, argv[0]);
4221                 goto usage_error;
4222         }
4223
4224         if (foreign_mode && (!setstripe_mode || comp_add | comp_del ||
4225             comp_set || comp_id || delete || from_copy ||
4226             setstripe_args_specified(&lsa) || lsa.lsa_nr_tgts ||
4227             lsa.lsa_tgts)) {
4228                 fprintf(stderr,
4229                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
4230                         progname, argv[0]);
4231                 return CMD_HELP;
4232         }
4233
4234         if (mirror_mode && mirror_count == 0) {
4235                 fprintf(stderr,
4236                         "error: %s: --mirror-count|-N option is required\n",
4237                         progname);
4238                 result = -EINVAL;
4239                 goto error;
4240         }
4241
4242         if (mirror_mode) {
4243                 if (!setstripe_args_specified(&lsa))
4244                         last_mirror->m_inherit = true;
4245                 if (lsa.lsa_comp_end == 0)
4246                         lsa.lsa_comp_end = LUSTRE_EOF;
4247         }
4248
4249         if (lsa.lsa_comp_end != 0) {
4250                 result = comp_args_to_layout(lpp, &lsa, true);
4251                 if (result) {
4252                         fprintf(stderr, "error: %s: invalid layout\n",
4253                                 progname);
4254                         result = -EINVAL;
4255                         goto error;
4256                 }
4257         }
4258
4259         if (mirror_flags & MF_NO_VERIFY) {
4260                 if (opc != SO_MIRROR_EXTEND) {
4261                         fprintf(stderr,
4262                                 "error: %s: --no-verify is valid only for lfs mirror extend command\n",
4263                                 progname);
4264                         result = -EINVAL;
4265                         goto error;
4266                 } else if (!has_m_file) {
4267                         fprintf(stderr,
4268                                 "error: %s: --no-verify must be specified with -f <victim_file> option\n",
4269                                 progname);
4270                         result = -EINVAL;
4271                         goto error;
4272                 }
4273         }
4274
4275         if (comp_set && !comp_id && !lsa.lsa_pool_name) {
4276                 fprintf(stderr,
4277                         "%s %s: --component-set doesn't have component-id set\n",
4278                         progname, argv[0]);
4279                 goto usage_error;
4280         }
4281
4282         if ((delete + comp_set + comp_del + comp_add) > 1) {
4283                 fprintf(stderr,
4284                         "%s %s: options --component-set, --component-del, --component-add and -d are mutually exclusive\n",
4285                         progname, argv[0]);
4286                 goto usage_error;
4287         }
4288
4289         if (delete && (setstripe_args_specified(&lsa) || comp_id != 0 ||
4290                        lsa.lsa_comp_flags != 0 || layout != NULL)) {
4291                 fprintf(stderr,
4292                         "%s %s: option -d is mutually exclusive with -s, -c, -o, -p, -I, -F and -E options\n",
4293                         progname, argv[0]);
4294                 goto usage_error;
4295         }
4296
4297         if ((comp_set || comp_del) &&
4298             (setstripe_args_specified(&lsa) || layout != NULL)) {
4299                 fprintf(stderr,
4300                         "%s %s: options --component-del and --component-set are mutually exclusive when used with -c, -E, -o, -p, or -s\n",
4301                         progname, argv[0]);
4302                 goto usage_error;
4303         }
4304
4305         if (comp_del && comp_id != 0 && lsa.lsa_comp_flags != 0) {
4306                 fprintf(stderr,
4307                         "%s %s: options -I and -F are mutually exclusive when used with --component-del\n",
4308                         progname, argv[0]);
4309                 goto usage_error;
4310         }
4311
4312         if (comp_add || comp_del) {
4313                 struct stat st;
4314
4315                 result = lstat(fname, &st);
4316                 if (result == 0 && S_ISDIR(st.st_mode)) {
4317                         fprintf(stderr,
4318                                 "%s setstripe: cannot use --component-add or --component-del for directory\n",
4319                                 progname);
4320                         goto usage_error;
4321                 }
4322
4323                 if (mirror_mode) {
4324                         fprintf(stderr,
4325                                 "error: %s: can't use --component-add or --component-del for mirror operation\n",
4326                                 progname);
4327                         goto usage_error;
4328                 }
4329         }
4330
4331         if (comp_add) {
4332                 if (!layout) {
4333                         fprintf(stderr,
4334                                 "%s %s: option -E must be specified with --component-add\n",
4335                                 progname, argv[0]);
4336                         goto usage_error;
4337                 }
4338         }
4339
4340         if (from_yaml && from_copy) {
4341                 fprintf(stderr,
4342                         "%s: can't specify --yaml and --copy together\n",
4343                         progname);
4344                 goto error;
4345         }
4346
4347         if ((from_yaml || from_copy) &&
4348             (setstripe_args_specified(&lsa) || layout != NULL)) {
4349                 fprintf(stderr,
4350                         "error: %s: can't specify --yaml or --copy with -c, -S, -i, -o, -p or -E options.\n",
4351                         argv[0]);
4352                 goto error;
4353         }
4354
4355         if ((migration_flags & LLAPI_MIGRATION_NONBLOCK) && migration_block) {
4356                 fprintf(stderr,
4357                         "%s %s: options --non-block and --block are mutually exclusive\n",
4358                         progname, argv[0]);
4359                 goto usage_error;
4360         }
4361
4362         if (!comp_del && !comp_set && opc != SO_MIRROR_SPLIT &&
4363             opc != SO_MIRROR_DELETE && comp_id != 0) {
4364                 fprintf(stderr,
4365                         "%s: option -I can only be used with --component-del or --component-set or lfs mirror split\n",
4366                         progname);
4367                 goto usage_error;
4368         }
4369
4370         if (migrate_mdt_mode) {
4371                 struct lmv_user_md *lmu;
4372
4373                 /* initialize migrate mdt parameters */
4374                 lmu = calloc(1, lmv_user_md_size(lsa.lsa_nr_tgts,
4375                                                  LMV_USER_MAGIC_SPECIFIC));
4376                 if (!lmu) {
4377                         fprintf(stderr,
4378                                 "%s %s: cannot allocate memory for lmv_user_md: %s\n",
4379                                 progname, argv[0], strerror(ENOMEM));
4380                         result = -ENOMEM;
4381                         goto error;
4382                 }
4383                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
4384                         lmu->lum_stripe_count = lsa.lsa_stripe_count;
4385                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT) {
4386                         fprintf(stderr,
4387                                 "%s %s: migrate should specify MDT index\n",
4388                                 progname, argv[0]);
4389                         free(lmu);
4390                         goto usage_error;
4391                 }
4392                 lmu->lum_stripe_offset = lsa.lsa_stripe_off;
4393                 if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
4394                         lmu->lum_hash_type = lsa.lsa_pattern;
4395                 else
4396                         lmu->lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
4397                 if (lsa.lsa_pool_name) {
4398                         strncpy(lmu->lum_pool_name, lsa.lsa_pool_name,
4399                                 sizeof(lmu->lum_pool_name) - 1);
4400                         lmu->lum_pool_name[sizeof(lmu->lum_pool_name) - 1] = 0;
4401                 }
4402                 if (lsa.lsa_nr_tgts > 1) {
4403                         int i;
4404
4405                         if (lsa.lsa_stripe_count > 0 &&
4406                             lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
4407                             lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
4408                                 fprintf(stderr,
4409                                         "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
4410                                         progname, lsa.lsa_stripe_count,
4411                                         lsa.lsa_nr_tgts);
4412                                 free(lmu);
4413                                 goto usage_error;
4414                         }
4415
4416                         lmu->lum_magic = LMV_USER_MAGIC_SPECIFIC;
4417                         lmu->lum_stripe_count = lsa.lsa_nr_tgts;
4418                         for (i = 0; i < lsa.lsa_nr_tgts; i++)
4419                                 lmu->lum_objects[i].lum_mds = lsa.lsa_tgts[i];
4420                 } else {
4421                         lmu->lum_magic = LMV_USER_MAGIC;
4422                 }
4423
4424                 migrate_mdt_param.fp_lmv_md = lmu;
4425                 migrate_mdt_param.fp_migrate = 1;
4426         } else if (!layout) {
4427                 if (lsa_args_stripe_count_check(&lsa))
4428                         goto usage_error;
4429
4430                 /* initialize stripe parameters */
4431                 param = calloc(1, offsetof(typeof(*param),
4432                                lsp_osts[lsa.lsa_nr_tgts]));
4433                 if (!param) {
4434                         fprintf(stderr,
4435                                 "%s %s: cannot allocate memory for parameters: %s\n",
4436                                 progname, argv[0], strerror(ENOMEM));
4437                         result = -ENOMEM;
4438                         goto error;
4439                 }
4440
4441                 if (lsa.lsa_stripe_size != LLAPI_LAYOUT_DEFAULT)
4442                         param->lsp_stripe_size = lsa.lsa_stripe_size;
4443                 if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT) {
4444                         if (lsa.lsa_stripe_count == LLAPI_LAYOUT_WIDE)
4445                                 param->lsp_stripe_count = -1;
4446                         else
4447                                 param->lsp_stripe_count = lsa.lsa_stripe_count;
4448                 }
4449                 if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
4450                         param->lsp_stripe_offset = -1;
4451                 else
4452                         param->lsp_stripe_offset = lsa.lsa_stripe_off;
4453                 param->lsp_stripe_pattern =
4454                                 llapi_pattern_to_lov(lsa.lsa_pattern);
4455                 if (param->lsp_stripe_pattern == EINVAL) {
4456                         fprintf(stderr, "error: %s: invalid stripe pattern\n",
4457                                 argv[0]);
4458                         free(param);
4459                         goto usage_error;
4460                 }
4461                 param->lsp_pool = lsa.lsa_pool_name;
4462                 param->lsp_is_specific = false;
4463
4464                 if (lsa.lsa_nr_tgts > 0) {
4465                         param->lsp_is_specific = true;
4466                         param->lsp_stripe_count = lsa.lsa_nr_tgts;
4467                         memcpy(param->lsp_osts, tgts,
4468                                sizeof(*tgts) * lsa.lsa_nr_tgts);
4469                 }
4470         }
4471
4472         if (from_yaml) {
4473                 /* generate a layout from a YAML template */
4474                 result = lfs_comp_create_from_yaml(template, &layout,
4475                                                    &lsa, tgts);
4476                 if (result) {
4477                         fprintf(stderr,
4478                                 "error: %s: can't create composite layout from template file %s\n",
4479                                 argv[0], template);
4480                         goto error;
4481                 }
4482         }
4483
4484         if (layout != NULL || mirror_list != NULL) {
4485                 if (mirror_list)
4486                         result = mirror_adjust_first_extents(mirror_list);
4487                 else
4488                         result = layout_adjust_first_extent(fname, layout,
4489                                                             comp_add);
4490                 if (result == -ENODATA)
4491                         comp_add = 0;
4492                 else if (result != 0) {
4493                         fprintf(stderr, "error: %s: invalid layout\n",
4494                                 progname);
4495                         goto error;
4496                 }
4497         }
4498
4499         for (fname = argv[optind]; fname != NULL; fname = argv[++optind]) {
4500                 if (from_copy) {
4501                         layout = llapi_layout_get_by_path(template ?: fname, 0);
4502                         if (!layout) {
4503                                 fprintf(stderr,
4504                                         "%s: can't create composite layout from file %s: %s\n",
4505                                         progname, template ?: fname,
4506                                         strerror(errno));
4507                                 result = -errno;
4508                                 goto error;
4509                         }
4510                 }
4511
4512                 if (migrate_mdt_mode) {
4513                         result = llapi_migrate_mdt(fname, &migrate_mdt_param);
4514                 } else if (migrate_mode) {
4515                         if (from_copy) {
4516                                 /*
4517                                  * Strip the source layout of specific
4518                                  * OST object/index values.
4519                                  */
4520                                 result = llapi_layout_ost_index_set(layout, 0,
4521                                                 LLAPI_LAYOUT_DEFAULT);
4522                                 if (result) {
4523                                         fprintf(stderr,
4524                                                 "%s: set default ost index failed: %s\n",
4525                                                 progname, strerror(errno));
4526                                         result = -errno;
4527                                         goto error;
4528                                 }
4529                         }
4530
4531                         result = lfs_migrate(fname, migration_flags, param,
4532                                              layout, bandwidth_bytes_sec,
4533                                              stats_flag, stats_interval_sec);
4534                 } else if (comp_set != 0) {
4535                         result = lfs_component_set(fname, comp_id,
4536                                                    lsa.lsa_pool_name,
4537                                                    lsa.lsa_comp_flags,
4538                                                    lsa.lsa_comp_neg_flags);
4539                 } else if (comp_del != 0) {
4540                         result = lfs_component_del(fname, comp_id,
4541                                                    lsa.lsa_comp_flags,
4542                                                    lsa.lsa_comp_neg_flags);
4543                 } else if (comp_add != 0) {
4544                         result = lfs_component_add(fname, layout);
4545                 } else if (opc == SO_MIRROR_CREATE) {
4546                         result = mirror_create(fname, mirror_list);
4547                 } else if (opc == SO_MIRROR_EXTEND) {
4548                         result = mirror_extend(fname, mirror_list,
4549                                                mirror_flags,
4550                                                bandwidth_bytes_sec,
4551                                                stats_flag, stats_interval_sec);
4552                 } else if (opc == SO_MIRROR_SPLIT || opc == SO_MIRROR_DELETE) {
4553                         if (!mirror_id && !comp_id && !lsa.lsa_pool_name) {
4554                                 fprintf(stderr,
4555                                         "%s: no mirror id, component id, or pool name specified to delete from '%s'\n",
4556                                         progname, fname);
4557                                 goto usage_error;
4558                         }
4559                         if (lsa.lsa_pool_name)
4560                                 mirror_flags |= MF_COMP_POOL;
4561                         else if (mirror_id != 0)
4562                                 comp_id = mirror_id;
4563                         else
4564                                 mirror_flags |= MF_COMP_ID;
4565                         if (has_m_file && !strcmp(fname, mirror_list->m_file)) {
4566                                 fprintf(stderr,
4567                                         "%s: the file specified by -f cannot be same as the source file '%s'\n",
4568                                         progname, fname);
4569                                 goto usage_error;
4570                         }
4571                         result = mirror_split(fname, comp_id, lsa.lsa_pool_name,
4572                                               mirror_flags,
4573                                               has_m_file ? mirror_list->m_file :
4574                                               NULL);
4575                 } else if (layout) {
4576                         result = lfs_component_create(fname, O_CREAT | O_WRONLY,
4577                                                       mode, layout);
4578                         if (result >= 0) {
4579                                 close(result);
4580                                 result = 0;
4581                         }
4582                 } else if (foreign_mode) {
4583                         result = llapi_file_create_foreign(fname, mode, type,
4584                                                            flags, xattr);
4585                         if (result >= 0) {
4586                                 close(result);
4587                                 result = 0;
4588                         }
4589                 } else {
4590                         result = llapi_file_open_param(fname,
4591                                                        O_CREAT | O_WRONLY,
4592                                                        mode, param);
4593                         if (result >= 0) {
4594                                 close(result);
4595                                 result = 0;
4596                         }
4597                 }
4598                 if (result) {
4599                         /* Save the first error encountered. */
4600                         if (result2 == 0)
4601                                 result2 = result;
4602                         continue;
4603                 }
4604         }
4605
4606         if (mode_opt)
4607                 umask(previous_umask);
4608
4609         free(param);
4610         free(migrate_mdt_param.fp_lmv_md);
4611         llapi_layout_free(layout);
4612         lfs_mirror_list_free(mirror_list);
4613         return result2;
4614 usage_error:
4615         result = CMD_HELP;
4616 error:
4617         llapi_layout_free(layout);
4618         lfs_mirror_list_free(mirror_list);
4619         return result;
4620 }
4621
4622 static int lfs_poollist(int argc, char **argv)
4623 {
4624         if (argc != 2)
4625                 return CMD_HELP;
4626
4627         return llapi_poollist(argv[1]);
4628 }
4629
4630 #define FP_DEFAULT_TIME_MARGIN (24 * 60 * 60)
4631 static time_t set_time(struct find_param *param, time_t *time, time_t *set,
4632                        char *str)
4633 {
4634         long long t = 0;
4635         int sign = 0;
4636         char *endptr = "AD";
4637         char *timebuf;
4638
4639         if (str[0] == '+')
4640                 sign = 1;
4641         else if (str[0] == '-')
4642                 sign = -1;
4643
4644         if (sign)
4645                 str++;
4646
4647         for (timebuf = str; *endptr && *(endptr + 1); timebuf = endptr + 1) {
4648                 long long val = strtoll(timebuf, &endptr, 0);
4649                 int unit = 1;
4650
4651                 switch (*endptr) {
4652                 case  'y':
4653                         unit *= 52; /* 52 weeks + 1 day below */
4654                         fallthrough;
4655                 case  'w':
4656                         unit *= 7;
4657                         if (param->fp_time_margin == FP_DEFAULT_TIME_MARGIN)
4658                                 param->fp_time_margin *= (1 + unit / 52);
4659                         unit += (*endptr == 'y'); /* +1 day for 365 days/year */
4660                         fallthrough;
4661                 case '\0': /* days are default unit if none used */
4662                         fallthrough;
4663                 case  'd':
4664                         unit *= 24;
4665                         fallthrough;
4666                 case  'h':
4667                         unit *= 60;
4668                         fallthrough;
4669                 case  'm':
4670                         unit *= 60;
4671                         fallthrough;
4672                 case  's':
4673                         break;
4674                         /* don't need to multiply by 1 for seconds */
4675                 default:
4676                         fprintf(stderr,
4677                                 "%s find: bad time string '%s': %s\n",
4678                                 progname, timebuf, strerror(EINVAL));
4679                         return LONG_MAX;
4680                 }
4681
4682                 if (param->fp_time_margin == 0 ||
4683                     (*endptr && unit < param->fp_time_margin))
4684                         param->fp_time_margin = unit;
4685
4686                 t += val * unit;
4687         }
4688         if (*time < t) {
4689                 if (sign != 0)
4690                         str--;
4691                 fprintf(stderr, "%s find: bad time '%s': too large\n",
4692                         progname, str);
4693                 return LONG_MAX;
4694         }
4695
4696         *set = *time - t;
4697
4698         return sign;
4699 }
4700
4701 static int str2quotaid(__u32 *id, const char *arg)
4702 {
4703         unsigned long int projid_tmp = 0;
4704         char *endptr = NULL;
4705
4706         projid_tmp = strtoul(arg, &endptr, 10);
4707         if (*endptr != '\0')
4708                 return -EINVAL;
4709         /* UINT32_MAX is not allowed - see projid_valid()/INVALID_PROJID */
4710         if (projid_tmp >= UINT32_MAX)
4711                 return -ERANGE;
4712
4713         *id = projid_tmp;
4714         return 0;
4715 }
4716
4717 static int name2uid(unsigned int *id, const char *name)
4718 {
4719         struct passwd *passwd;
4720
4721         passwd = getpwnam(name);
4722         if (!passwd)
4723                 return -ENOENT;
4724         *id = passwd->pw_uid;
4725
4726         return 0;
4727 }
4728
4729 static int name2gid(unsigned int *id, const char *name)
4730 {
4731         struct group *group;
4732
4733         group = getgrnam(name);
4734         if (!group)
4735                 return -ENOENT;
4736         *id = group->gr_gid;
4737
4738         return 0;
4739 }
4740
4741 static inline int name2projid(unsigned int *id, const char *name)
4742 {
4743         return -ENOTSUP;
4744 }
4745
4746 static int uid2name(char **name, unsigned int id)
4747 {
4748         struct passwd *passwd;
4749
4750         passwd = getpwuid(id);
4751         if (!passwd)
4752                 return -ENOENT;
4753         *name = passwd->pw_name;
4754
4755         return 0;
4756 }
4757
4758 static inline int gid2name(char **name, unsigned int id)
4759 {
4760         struct group *group;
4761
4762         group = getgrgid(id);
4763         if (!group)
4764                 return -ENOENT;
4765         *name = group->gr_name;
4766
4767         return 0;
4768 }
4769
4770 static int name2layout(__u32 *layout, char *name)
4771 {
4772         char *ptr, *layout_name;
4773
4774         *layout = 0;
4775         for (ptr = name; ; ptr = NULL) {
4776                 layout_name = strtok(ptr, ",");
4777                 if (!layout_name)
4778                         break;
4779                 if (strcmp(layout_name, "released") == 0)
4780                         *layout |= LOV_PATTERN_F_RELEASED;
4781                 else if (strcmp(layout_name, "raid0") == 0)
4782                         *layout |= LOV_PATTERN_RAID0;
4783                 else if (strcmp(layout_name, "mdt") == 0)
4784                         *layout |= LOV_PATTERN_MDT;
4785                 else if (strcmp(layout_name, "overstriping") == 0)
4786                         *layout |= LOV_PATTERN_OVERSTRIPING;
4787                 else
4788                         return -1;
4789         }
4790         return 0;
4791 }
4792
4793 static int parse_symbolic(const char *input, mode_t *outmode, const char **end)
4794 {
4795         int loop;
4796         int user, group, other;
4797         int who, all;
4798         char c, op;
4799         mode_t perm;
4800         mode_t usermask;
4801         mode_t previous_flags;
4802
4803         user = group = other = 0;
4804         all = 0;
4805         loop = 1;
4806         perm = 0;
4807         previous_flags = 0;
4808         *end = input;
4809         usermask = 0;
4810
4811         while (loop) {
4812                 switch (*input) {
4813                 case 'u':
4814                         user = 1;
4815                         break;
4816                 case 'g':
4817                         group = 1;
4818                         break;
4819                 case 'o':
4820                         other = 1;
4821                         break;
4822                 case 'a':
4823                         user = group = other = 1;
4824                         all = 1;
4825                         break;
4826                 default:
4827                         loop = 0;
4828                 }
4829
4830                 if (loop)
4831                         input++;
4832         }
4833
4834         who = user || group || other;
4835         if (!who) {
4836                 /* get the umask */
4837                 usermask = umask(0022);
4838                 umask(usermask);
4839                 usermask &= 07777;
4840         }
4841
4842         if (*input == '-' || *input == '+' || *input == '=')
4843                 op = *input++;
4844         else
4845                 /* operation is required */
4846                 return -1;
4847
4848         /* get the flags in *outmode */
4849         switch (*input) {
4850         case 'u':
4851                 previous_flags = (*outmode & 0700);
4852                 perm |= user  ? previous_flags : 0;
4853                 perm |= group ? (previous_flags >> 3) : 0;
4854                 perm |= other ? (previous_flags >> 6) : 0;
4855                 input++;
4856                 goto write_perm;
4857         case 'g':
4858                 previous_flags = (*outmode & 0070);
4859                 perm |= user  ? (previous_flags << 3) : 0;
4860                 perm |= group ? previous_flags : 0;
4861                 perm |= other ? (previous_flags >> 3) : 0;
4862                 input++;
4863                 goto write_perm;
4864         case 'o':
4865                 previous_flags = (*outmode & 0007);
4866                 perm |= user  ? (previous_flags << 6) : 0;
4867                 perm |= group ? (previous_flags << 3) : 0;
4868                 perm |= other ? previous_flags : 0;
4869                 input++;
4870                 goto write_perm;
4871         default:
4872                 break;
4873         }
4874
4875         /* this part is optional,
4876          * if empty perm = 0 and *outmode is not modified
4877          */
4878         loop = 1;
4879         while (loop) {
4880                 c = *input;
4881                 switch (c) {
4882                 case 'r':
4883                         perm |= user  ? 0400 : 0;
4884                         perm |= group ? 0040 : 0;
4885                         perm |= other ? 0004 : 0;
4886                         /* set read permission for uog except for umask's
4887                          * permissions
4888                          */
4889                         perm |= who   ? 0 : (0444 & ~usermask);
4890                         break;
4891                 case 'w':
4892                         perm |= user  ? 0200 : 0;
4893                         perm |= group ? 0020 : 0;
4894                         perm |= other ? 0002 : 0;
4895                         /* set write permission for uog except for umask'
4896                          * permissions
4897                          */
4898                         perm |= who   ? 0 : (0222 & ~usermask);
4899                         break;
4900                 case 'x':
4901                         perm |= user  ? 0100 : 0;
4902                         perm |= group ? 0010 : 0;
4903                         perm |= other ? 0001 : 0;
4904                         /* set execute permission for uog except for umask'
4905                          * permissions
4906                          */
4907                         perm |= who   ? 0 : (0111 & ~usermask);
4908                         break;
4909                 case 'X':
4910                         /*
4911                          * Adds execute permission to 'u', 'g' and/or 'g' if
4912                          * specified and either 'u', 'g' or 'o' already has
4913                          * execute permissions.
4914                          */
4915                         if ((*outmode & 0111) != 0) {
4916                                 perm |= user  ? 0100 : 0;
4917                                 perm |= group ? 0010 : 0;
4918                                 perm |= other ? 0001 : 0;
4919                                 perm |= !who  ? 0111 : 0;
4920                         }
4921                         break;
4922                 case 's':
4923                         /* s is ignored if o is given, but it's not an error */
4924                         if (other && !group && !user)
4925                                 break;
4926                         perm |= user  ? S_ISUID : 0;
4927                         perm |= group ? S_ISGID : 0;
4928                         break;
4929                 case 't':
4930                         /* 't' should be used when 'a' is given
4931                          * or who is empty
4932                          */
4933                         perm |= (!who || all) ? S_ISVTX : 0;
4934                         /* using ugo with t is not an error */
4935                         break;
4936                 default:
4937                         loop = 0;
4938                         break;
4939                 }
4940                 if (loop)
4941                         input++;
4942         }
4943
4944 write_perm:
4945         /* uog flags should be only one character long */
4946         if (previous_flags && (*input != '\0' && *input != ','))
4947                 return -1;
4948
4949         switch (op) {
4950         case '-':
4951                 /* remove the flags from outmode */
4952                 *outmode &= ~perm;
4953                 break;
4954         case '+':
4955                 /* add the flags to outmode */
4956                 *outmode |= perm;
4957                 break;
4958         case '=':
4959                 /* set the flags of outmode to perm */
4960                 if (perm != 0)
4961                         *outmode = perm;
4962                 break;
4963         }
4964
4965         *end = input;
4966         return 0;
4967 }
4968
4969 static int str2mode_t(const char *input, mode_t *outmode)
4970 {
4971         int ret;
4972         const char *iter;
4973
4974         ret = 0;
4975
4976         if (*input >= '0' && *input <= '7') {
4977                 /* parse octal representation */
4978                 char *end;
4979
4980                 iter = input;
4981
4982                 /* look for invalid digits in octal representation */
4983                 while (isdigit(*iter))
4984                         if (*iter++ > '7')
4985                                 return -1;
4986
4987                 errno = 0;
4988                 *outmode = strtoul(input, &end, 8);
4989
4990                 if (errno != 0 || *outmode > 07777) {
4991                         *outmode = 0;
4992                         ret = -1;
4993                 }
4994
4995         } else if (*input == '8' || *input == '9') {
4996                 /* error: invalid octal number */
4997                 ret = -1;
4998         } else {
4999                 /* parse coma seperated list of symbolic representation */
5000                 int rc;
5001                 const char *end;
5002
5003                 *outmode = 0;
5004                 rc = 0;
5005                 end = NULL;
5006
5007                 do {
5008                         rc = parse_symbolic(input, outmode, &end);
5009                         if (rc)
5010                                 return -1;
5011
5012                         input = end+1;
5013                 } while (*end == ',');
5014
5015                 if (*end != '\0')
5016                         ret = -1;
5017         }
5018         return ret;
5019 }
5020
5021 static int lfs_find(int argc, char **argv)
5022 {
5023         int c, rc;
5024         int ret = 0;
5025         time_t t;
5026         struct find_param param = {
5027                 .fp_max_depth = -1,
5028                 .fp_quiet = 1,
5029                 .fp_time_margin = FP_DEFAULT_TIME_MARGIN,
5030         };
5031         struct option long_opts[] = {
5032         { .val = 'A',   .name = "atime",        .has_arg = required_argument },
5033         { .val = 'b',   .name = "blocks",       .has_arg = required_argument },
5034         { .val = 'B',   .name = "btime",        .has_arg = required_argument },
5035         { .val = 'B',   .name = "Btime",        .has_arg = required_argument },
5036         { .val = LFS_COMP_COUNT_OPT,
5037                         .name = "comp-count",   .has_arg = required_argument },
5038         { .val = LFS_COMP_COUNT_OPT,
5039                         .name = "component-count",
5040                                                 .has_arg = required_argument },
5041         { .val = LFS_COMP_FLAGS_OPT,
5042                         .name = "comp-flags",   .has_arg = required_argument },
5043         { .val = LFS_COMP_FLAGS_OPT,
5044                         .name = "component-flags",
5045                                                 .has_arg = required_argument },
5046         { .val = LFS_COMP_START_OPT,
5047                         .name = "comp-start",   .has_arg = required_argument },
5048         { .val = LFS_COMP_START_OPT,
5049                         .name = "component-start",
5050                                                 .has_arg = required_argument },
5051         { .val = LFS_MIRROR_STATE_OPT,
5052                         .name = "mirror-state", .has_arg = required_argument },
5053         { .val = LFS_NEWERXY_OPT,
5054                         .name = "newer",        .has_arg = required_argument},
5055         { .val = LFS_NEWERXY_OPT,
5056                         .name = "neweraa",      .has_arg = required_argument},
5057         { .val = LFS_NEWERXY_OPT,
5058                         .name = "neweram",      .has_arg = required_argument},
5059         { .val = LFS_NEWERXY_OPT,
5060                         .name = "newerac",      .has_arg = required_argument},
5061         { .val = LFS_NEWERXY_OPT,
5062                         .name = "newerab",      .has_arg = required_argument},
5063         { .val = LFS_NEWERXY_OPT,
5064                         .name = "newerma",      .has_arg = required_argument},
5065         { .val = LFS_NEWERXY_OPT,
5066                         .name = "newermm",      .has_arg = required_argument},
5067         { .val = LFS_NEWERXY_OPT,
5068                         .name = "newermc",      .has_arg = required_argument},
5069         { .val = LFS_NEWERXY_OPT,
5070                         .name = "newermb",      .has_arg = required_argument},
5071         { .val = LFS_NEWERXY_OPT,
5072                         .name = "newerca",      .has_arg = required_argument},
5073         { .val = LFS_NEWERXY_OPT,
5074                         .name = "newercm",      .has_arg = required_argument},
5075         { .val = LFS_NEWERXY_OPT,
5076                         .name = "newercc",      .has_arg = required_argument},
5077         { .val = LFS_NEWERXY_OPT,
5078                         .name = "newercb",      .has_arg = required_argument},
5079         { .val = LFS_NEWERXY_OPT,
5080                         .name = "newerba",      .has_arg = required_argument},
5081         { .val = LFS_NEWERXY_OPT,
5082                         .name = "newerbm",      .has_arg = required_argument},
5083         { .val = LFS_NEWERXY_OPT,
5084                         .name = "newerbc",      .has_arg = required_argument},
5085         { .val = LFS_NEWERXY_OPT,
5086                         .name = "newerbb",      .has_arg = required_argument},
5087         { .val = LFS_NEWERXY_OPT,
5088                         .name = "newerBa",      .has_arg = required_argument},
5089         { .val = LFS_NEWERXY_OPT,
5090                         .name = "newerBm",      .has_arg = required_argument},
5091         { .val = LFS_NEWERXY_OPT,
5092                         .name = "newerBc",      .has_arg = required_argument},
5093         { .val = LFS_NEWERXY_OPT,
5094                         .name = "newerBB",      .has_arg = required_argument},
5095         { .val = LFS_NEWERXY_OPT,
5096                         .name = "newerat",      .has_arg = required_argument},
5097         { .val = LFS_NEWERXY_OPT,
5098                         .name = "newermt",      .has_arg = required_argument},
5099         { .val = LFS_NEWERXY_OPT,
5100                         .name = "newerct",      .has_arg = required_argument},
5101         { .val = LFS_NEWERXY_OPT,
5102                         .name = "newerbt",      .has_arg = required_argument},
5103         { .val = LFS_NEWERXY_OPT,
5104                         .name = "newerBt",      .has_arg = required_argument},
5105         { .val = 'c',   .name = "stripe-count", .has_arg = required_argument },
5106         { .val = 'c',   .name = "stripe_count", .has_arg = required_argument },
5107         { .val = 'C',   .name = "ctime",        .has_arg = required_argument },
5108 /* getstripe { .val = 'd', .name = "directory", .has_arg = no_argument }, */
5109         { .val = 'D',   .name = "maxdepth",     .has_arg = required_argument },
5110         { .val = 'E',   .name = "comp-end",     .has_arg = required_argument },
5111         { .val = 'E',   .name = "component-end",
5112                                                 .has_arg = required_argument },
5113 /* find { .val = 'F',   .name = "fid",          .has_arg = no_argument }, */
5114         { .val = LFS_LAYOUT_FOREIGN_OPT,
5115                         .name = "foreign",      .has_arg = optional_argument},
5116         { .val = 'g',   .name = "gid",          .has_arg = required_argument },
5117         { .val = 'G',   .name = "group",        .has_arg = required_argument },
5118         { .val = 'h',   .name = "help",         .has_arg = no_argument },
5119         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
5120         { .val = 'i',   .name = "stripe-index", .has_arg = required_argument },
5121         { .val = 'i',   .name = "stripe_index", .has_arg = required_argument },
5122 /* getstripe { .val = 'I', .name = "comp-id",   .has_arg = required_argument }*/
5123         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
5124         { .val = 'L',   .name = "layout",       .has_arg = required_argument },
5125         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
5126         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
5127         { .val = 'm',   .name = "mdt_index",    .has_arg = required_argument },
5128         { .val = 'M',   .name = "mtime",        .has_arg = required_argument },
5129         { .val = 'n',   .name = "name",         .has_arg = required_argument },
5130         { .val = 'N',   .name = "mirror-count", .has_arg = required_argument },
5131 /* find { .val = 'o'    .name = "or", .has_arg = no_argument }, like find(1) */
5132         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
5133         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
5134         { .val = LFS_FIND_PERM,
5135                         .name = "perm",         .has_arg = required_argument },
5136         /* no short option for pool yet, can be 'p' after 2.18 */
5137         { .val = LFS_POOL_OPT,
5138                         .name = "pool",         .has_arg = required_argument },
5139         { .val = '0',   .name = "print0",       .has_arg = no_argument },
5140         { .val = 'P',   .name = "print",        .has_arg = no_argument },
5141         { .val = LFS_PRINTF_OPT,
5142                         .name = "printf",       .has_arg = required_argument },
5143         { .val = LFS_PROJID_OPT,
5144                         .name = "projid",       .has_arg = required_argument },
5145 /* getstripe { .val = 'q', .name = "quiet",     .has_arg = no_argument }, */
5146 /* getstripe { .val = 'r', .name = "recursive", .has_arg = no_argument }, */
5147 /* getstripe { .val = 'R', .name = "raw",       .has_arg = no_argument }, */
5148         { .val = 's',   .name = "size",         .has_arg = required_argument },
5149         { .val = 'S',   .name = "stripe-size",  .has_arg = required_argument },
5150         { .val = 'S',   .name = "stripe_size",  .has_arg = required_argument },
5151         { .val = 't',   .name = "type",         .has_arg = required_argument },
5152         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
5153         { .val = 'u',   .name = "uid",          .has_arg = required_argument },
5154         { .val = 'U',   .name = "user",         .has_arg = required_argument },
5155 /* getstripe { .val = 'v', .name = "verbose",   .has_arg = no_argument }, */
5156 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
5157         { .val = 'z',   .name = "extension-size",
5158                                                 .has_arg = required_argument },
5159         { .val = 'z',   .name = "ext-size",     .has_arg = required_argument },
5160         { .name = NULL } };
5161         int optidx = 0;
5162         int pathstart = -1;
5163         int pathend = -1;
5164         int pathbad = -1;
5165         int neg_opt = 0;
5166         time_t *xtime;
5167         int *xsign;
5168         int isoption;
5169         char *endptr;
5170
5171         time(&t);
5172
5173         /* when getopt_long_only() hits '!' it returns 1, puts "!" in optarg */
5174         while ((c = getopt_long_only(argc, argv,
5175                 "-0A:b:B:c:C:D:E:g:G:hH:i:L:m:M:n:N:O:Ppqrs:S:t:T:u:U:z:",
5176                 long_opts, &optidx)) >= 0) {
5177                 xtime = NULL;
5178                 xsign = NULL;
5179                 if (neg_opt)
5180                         --neg_opt;
5181                 /* '!' is part of option */
5182                 /*
5183                  * when getopt_long_only() finds a string which is not
5184                  * an option nor a known option argument it returns 1
5185                  * in that case if we already have found pathstart and pathend
5186                  * (i.e. we have the list of pathnames),
5187                  * the only supported value is "!"
5188                  */
5189                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
5190                 if (!isoption && pathend != -1) {
5191                         fprintf(stderr,
5192                                 "err: %s: filename|dirname must either precede options or follow options\n",
5193                                 argv[0]);
5194                         ret = CMD_HELP;
5195                         goto err;
5196                 }
5197                 if (!isoption && pathstart == -1)
5198                         pathstart = optind - 1;
5199                 if (isoption && pathstart != -1 && pathend == -1)
5200                         pathend = optind - 2;
5201                 switch (c) {
5202                 case 0:
5203                         /* Long options. */
5204                         break;
5205                 case 1:
5206                         /*
5207                          * unknown; opt is "!" or path component,
5208                          * checking done above.
5209                          */
5210                         if (strcmp(optarg, "!") == 0)
5211                                 neg_opt = 2;
5212                         break;
5213                 case 'A':
5214                         xtime = &param.fp_atime;
5215                         xsign = &param.fp_asign;
5216                         param.fp_exclude_atime = !!neg_opt;
5217                         /* no break, this falls through to 'B' for btime */
5218                         fallthrough;
5219                 case 'B':
5220                         if (c == 'B') {
5221                                 xtime = &param.fp_btime;
5222                                 xsign = &param.fp_bsign;
5223                                 param.fp_exclude_btime = !!neg_opt;
5224                         }
5225                         /* no break, this falls through to 'C' for ctime */
5226                         fallthrough;
5227                 case 'C':
5228                         if (c == 'C') {
5229                                 xtime = &param.fp_ctime;
5230                                 xsign = &param.fp_csign;
5231                                 param.fp_exclude_ctime = !!neg_opt;
5232                         }
5233                         /* no break, this falls through to 'M' for mtime */
5234                         fallthrough;
5235                 case 'M':
5236                         if (c == 'M') {
5237                                 xtime = &param.fp_mtime;
5238                                 xsign = &param.fp_msign;
5239                                 param.fp_exclude_mtime = !!neg_opt;
5240                         }
5241                         rc = set_time(&param, &t, xtime, optarg);
5242                         if (rc == LONG_MAX) {
5243                                 ret = -1;
5244                                 goto err;
5245                         }
5246                         if (rc)
5247                                 *xsign = rc;
5248                         break;
5249                 case 'b':
5250                         if (optarg[0] == '+') {
5251                                 param.fp_blocks_sign = -1;
5252                                 optarg++;
5253                         } else if (optarg[0] == '-') {
5254                                 param.fp_blocks_sign =  1;
5255                                 optarg++;
5256                         }
5257
5258                         param.fp_blocks_units = 1024;
5259                         ret = llapi_parse_size(optarg, &param.fp_blocks,
5260                                                &param.fp_blocks_units, 0);
5261                         if (ret) {
5262                                 fprintf(stderr, "error: bad blocks '%s'\n",
5263                                         optarg);
5264                                 goto err;
5265                         }
5266                         param.fp_check_blocks = 1;
5267                         param.fp_exclude_blocks = !!neg_opt;
5268                         break;
5269                 case LFS_COMP_COUNT_OPT:
5270                         if (optarg[0] == '+') {
5271                                 param.fp_comp_count_sign = -1;
5272                                 optarg++;
5273                         } else if (optarg[0] == '-') {
5274                                 param.fp_comp_count_sign =  1;
5275                                 optarg++;
5276                         }
5277
5278                         errno = 0;
5279                         param.fp_comp_count = strtoul(optarg, &endptr, 0);
5280                         if (errno != 0 || *endptr != '\0' ||
5281                             param.fp_comp_count > UINT32_MAX) {
5282                                 fprintf(stderr,
5283                                         "error: bad component count '%s'\n",
5284                                         optarg);
5285                                 goto err;
5286                         }
5287                         param.fp_check_comp_count = 1;
5288                         param.fp_exclude_comp_count = !!neg_opt;
5289                         break;
5290                 case LFS_COMP_FLAGS_OPT:
5291                         rc = comp_str2flags(optarg, &param.fp_comp_flags,
5292                                             &param.fp_comp_neg_flags);
5293                         if (rc) {
5294                                 fprintf(stderr,
5295                                         "error: bad component flags '%s'\n",
5296                                         optarg);
5297                                 goto err;
5298                         }
5299                         param.fp_check_comp_flags = 1;
5300                         if (neg_opt) {
5301                                 __u32 flags = param.fp_comp_neg_flags;
5302
5303                                 param.fp_comp_neg_flags = param.fp_comp_flags;
5304                                 param.fp_comp_flags = flags;
5305                         }
5306                         break;
5307                 case LFS_COMP_START_OPT:
5308                         if (optarg[0] == '+') {
5309                                 param.fp_comp_start_sign = -1;
5310                                 optarg++;
5311                         } else if (optarg[0] == '-') {
5312                                 param.fp_comp_start_sign =  1;
5313                                 optarg++;
5314                         }
5315
5316                         rc = llapi_parse_size(optarg, &param.fp_comp_start,
5317                                               &param.fp_comp_start_units, 0);
5318                         if (rc) {
5319                                 fprintf(stderr,
5320                                         "error: bad component start '%s'\n",
5321                                         optarg);
5322                                 goto err;
5323                         }
5324                         param.fp_check_comp_start = 1;
5325                         param.fp_exclude_comp_start = !!neg_opt;
5326                         break;
5327                 case LFS_MIRROR_STATE_OPT:
5328                         rc = mirror_str2state(optarg, &param.fp_mirror_state,
5329                                               &param.fp_mirror_neg_state);
5330                         if (rc) {
5331                                 fprintf(stderr,
5332                                         "error: bad mirrored file state '%s'\n",
5333                                         optarg);
5334                                 goto err;
5335                         }
5336                         param.fp_check_mirror_state = 1;
5337                         if (neg_opt) {
5338                                 __u16 state = param.fp_mirror_neg_state;
5339
5340                                 param.fp_mirror_neg_state =
5341                                         param.fp_mirror_state;
5342                                 param.fp_mirror_state = state;
5343                         }
5344                         break;
5345                 case 'c':
5346                         if (optarg[0] == '+') {
5347                                 param.fp_stripe_count_sign = -1;
5348                                 optarg++;
5349                         } else if (optarg[0] == '-') {
5350                                 param.fp_stripe_count_sign =  1;
5351                                 optarg++;
5352                         }
5353
5354                         errno = 0;
5355                         param.fp_stripe_count = strtoul(optarg, &endptr, 0);
5356                         if (errno != 0 || *endptr != '\0' ||
5357                             param.fp_stripe_count > LOV_MAX_STRIPE_COUNT) {
5358                                 fprintf(stderr,
5359                                         "error: bad stripe_count '%s'\n",
5360                                         optarg);
5361                                 ret = -1;
5362                                 goto err;
5363                         }
5364                         param.fp_check_stripe_count = 1;
5365                         param.fp_exclude_stripe_count = !!neg_opt;
5366                         break;
5367                 case 'D':
5368                         errno = 0;
5369                         param.fp_max_depth = strtol(optarg, 0, 0);
5370                         if (errno != 0 || param.fp_max_depth < 0) {
5371                                 fprintf(stderr,
5372                                         "error: bad maxdepth '%s'\n",
5373                                         optarg);
5374                                 ret = -1;
5375                                 goto err;
5376                         }
5377                         break;
5378                 case 'E':
5379                         if (optarg[0] == '+') {
5380                                 param.fp_comp_end_sign = -1;
5381                                 optarg++;
5382                         } else if (optarg[0] == '-') {
5383                                 param.fp_comp_end_sign =  1;
5384                                 optarg++;
5385                         }
5386
5387                         if (arg_is_eof(optarg)) {
5388                                 param.fp_comp_end = LUSTRE_EOF;
5389                                 param.fp_comp_end_units = 1;
5390                                 rc = 0;
5391                         } else {
5392                                 rc = llapi_parse_size(optarg,
5393                                                 &param.fp_comp_end,
5394                                                 &param.fp_comp_end_units, 0);
5395                                 /* assume units of KB if too small */
5396                                 if (param.fp_comp_end < 4096)
5397                                         param.fp_comp_end *= 1024;
5398                         }
5399                         if (rc) {
5400                                 fprintf(stderr,
5401                                         "error: bad component end '%s'\n",
5402                                         optarg);
5403                                 goto err;
5404                         }
5405                         param.fp_check_comp_end = 1;
5406                         param.fp_exclude_comp_end = !!neg_opt;
5407                         break;
5408                 case LFS_LAYOUT_FOREIGN_OPT: {
5409                         /* all types by default */
5410                         uint32_t type = LU_FOREIGN_TYPE_UNKNOWN;
5411
5412                         if (optarg) {
5413                                 /* check pure numeric */
5414                                 type = strtoul(optarg, &endptr, 0);
5415                                 if (*endptr) {
5416                                         /* check name */
5417                                         type = check_foreign_type_name(optarg);
5418                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
5419                                                 fprintf(stderr,
5420                                                         "%s %s: unknown foreign type '%s'\n",
5421                                                         progname, argv[0],
5422                                                         optarg);
5423                                                 return CMD_HELP;
5424                                         }
5425                                 } else if (type >= UINT32_MAX) {
5426                                         fprintf(stderr,
5427                                                 "%s %s: invalid foreign type '%s'\n",
5428                                                 progname, argv[0], optarg);
5429                                         return CMD_HELP;
5430                                 }
5431                         }
5432                         param.fp_foreign_type = type;
5433                         param.fp_check_foreign = 1;
5434                         param.fp_exclude_foreign = !!neg_opt;
5435                         break;
5436                 }
5437                 case LFS_NEWERXY_OPT: {
5438                         char x = 'm';
5439                         char y = 'm';
5440                         int xidx;
5441                         int negidx;
5442                         time_t *newery;
5443                         time_t ref = time(NULL);
5444
5445                         /* no need to check bad options, they won't get here */
5446                         if (strlen(long_opts[optidx].name) == 7) {
5447                                 x = long_opts[optidx].name[5];
5448                                 y = long_opts[optidx].name[6];
5449                         }
5450
5451                         if (y == 't') {
5452                                 static const char *const fmts[] = {
5453                                         "%Y-%m-%d %H:%M:%S",
5454                                         "%Y-%m-%d %H:%M",
5455                                         "%Y-%m-%d",
5456                                         "%H:%M:%S", /* sometime today */
5457                                         "%H:%M",
5458                                         "@%s",
5459                                         "%s",
5460                                         NULL };
5461                                 struct tm tm;
5462                                 bool found = false;
5463                                 int i;
5464
5465                                 for (i = 0; fmts[i] != NULL; i++) {
5466                                         char *ptr;
5467
5468                                         /* Init for times relative to today */
5469                                         if (strncmp(fmts[i], "%H", 2) == 0) {
5470                                                 localtime_r(&ref, &tm);
5471                                         } else {
5472                                                 memset(&tm, 0, sizeof(tm));
5473                                                 tm.tm_isdst = -1;
5474                                         }
5475                                         ptr = strptime(optarg, fmts[i], &tm);
5476                                         /* Skip spaces */
5477                                         while (ptr && isspace(*ptr))
5478                                                 ptr++;
5479                                         if (ptr == optarg + strlen(optarg)) {
5480                                                 found = true;
5481                                                 break;
5482                                         }
5483                                 }
5484
5485                                 if (!found) {
5486                                         fprintf(stderr,
5487                                                 "%s: invalid time '%s'\n",
5488                                                 progname, optarg);
5489                                         fprintf(stderr,
5490                                                 "supported formats are:\n  ");
5491                                         for (i = 0; fmts[i] != NULL; i++)
5492                                                 fprintf(stderr, "'%s', ",
5493                                                         fmts[i]);
5494                                         fprintf(stderr, "\n");
5495                                         ret = -EINVAL;
5496                                         goto err;
5497                                 }
5498
5499                                 ref = mktime(&tm);
5500                         } else if (y == 'b' || y == 'B') {
5501                                 lstatx_t stx;
5502
5503                                 rc = llapi_get_lum_file(optarg, NULL, &stx,
5504                                                         NULL, 0);
5505                                 if (rc || !(stx.stx_mask & STATX_BTIME)) {
5506                                         if (!(stx.stx_mask & STATX_BTIME))
5507                                                 ret = -EOPNOTSUPP;
5508                                         else
5509                                                 ret = -errno;
5510                                         fprintf(stderr,
5511                                                 "%s: get btime failed '%s': %s\n",
5512                                                 progname, optarg,
5513                                                 strerror(-ret));
5514                                         goto err;
5515                                 }
5516
5517                                 ref = stx.stx_btime.tv_sec;
5518                         } else {
5519                                 struct stat statbuf;
5520
5521                                 if (stat(optarg, &statbuf) < 0) {
5522                                         fprintf(stderr,
5523                                                 "%s: cannot stat file '%s': %s\n",
5524                                                 progname, optarg,
5525                                                 strerror(errno));
5526                                         ret = -errno;
5527                                         goto err;
5528                                 }
5529
5530                                 switch (y) {
5531                                 case 'a':
5532                                         ref = statbuf.st_atime;
5533                                         break;
5534                                 case 'm':
5535                                         ref = statbuf.st_mtime;
5536                                         break;
5537                                 case 'c':
5538                                         ref = statbuf.st_ctime;
5539                                         break;
5540                                 default:
5541                                         fprintf(stderr,
5542                                                 "%s: invalid Y argument: '%c'\n",
5543                                                 progname, x);
5544                                         ret = -EINVAL;
5545                                         goto err;
5546                                 }
5547                         }
5548
5549                         switch (x) {
5550                         case 'a':
5551                                 xidx = NEWERXY_ATIME;
5552                                 break;
5553                         case 'm':
5554                                 xidx = NEWERXY_MTIME;
5555                                 break;
5556                         case 'c':
5557                                 xidx = NEWERXY_CTIME;
5558                                 break;
5559                         case 'b':
5560                         case 'B':
5561                                 xidx = NEWERXY_BTIME;
5562                                 break;
5563                         default:
5564                                 fprintf(stderr,
5565                                         "%s: invalid X argument: '%c'\n",
5566                                         progname, x);
5567                                 ret = -EINVAL;
5568                                 goto err;
5569                         }
5570
5571                         negidx = !!neg_opt;
5572                         newery = &param.fp_newery[xidx][negidx];
5573
5574                         if (*newery == 0) {
5575                                 *newery = ref;
5576                         } else {
5577                                 if (negidx)
5578                                         *newery = *newery > ref ? ref : *newery;
5579                                 else
5580                                         *newery = *newery > ref ? *newery : ref;
5581                         }
5582                         param.fp_newerxy = 1;
5583                         break;
5584                 }
5585                 case 'g':
5586                 case 'G':
5587                         rc = name2gid(&param.fp_gid, optarg);
5588                         if (rc) {
5589                                 if (str2quotaid(&param.fp_gid, optarg)) {
5590                                         fprintf(stderr,
5591                                                 "Group/GID: %s cannot be found.\n",
5592                                                 optarg);
5593                                         ret = -1;
5594                                         goto err;
5595                                 }
5596                         }
5597                         param.fp_exclude_gid = !!neg_opt;
5598                         param.fp_check_gid = 1;
5599                         break;
5600                 case 'H':
5601                         rc = mdthash_input(optarg, &param.fp_hash_inflags,
5602                                            &param.fp_hash_exflags,
5603                                            &param.fp_hash_type);
5604                         if (rc) {
5605                                 ret = -1;
5606                                 goto err;
5607                         }
5608                         if (param.fp_hash_inflags || param.fp_hash_exflags)
5609                                 param.fp_check_hash_flag = 1;
5610                         param.fp_exclude_hash_type = !!neg_opt;
5611                         break;
5612                 case 'l':
5613                         param.fp_lazy = 1;
5614                         break;
5615                 case 'L':
5616                         ret = name2layout(&param.fp_layout, optarg);
5617                         if (ret)
5618                                 goto err;
5619                         param.fp_exclude_layout = !!neg_opt;
5620                         param.fp_check_layout = 1;
5621                         break;
5622                 case 'u':
5623                 case 'U':
5624                         rc = name2uid(&param.fp_uid, optarg);
5625                         if (rc) {
5626                                 if (str2quotaid(&param.fp_uid, optarg)) {
5627                                         fprintf(stderr,
5628                                                 "User/UID: %s cannot be found.\n",
5629                                                 optarg);
5630                                         ret = -1;
5631                                         goto err;
5632                                 }
5633                         }
5634                         param.fp_exclude_uid = !!neg_opt;
5635                         param.fp_check_uid = 1;
5636                         break;
5637                 case 'n':
5638                         param.fp_pattern = (char *)optarg;
5639                         param.fp_exclude_pattern = !!neg_opt;
5640                         break;
5641                 case 'N':
5642                         if (optarg[0] == '+') {
5643                                 param.fp_mirror_count_sign = -1;
5644                                 optarg++;
5645                         } else if (optarg[0] == '-') {
5646                                 param.fp_mirror_count_sign =  1;
5647                                 optarg++;
5648                         }
5649
5650                         errno = 0;
5651                         param.fp_mirror_count = strtoul(optarg, &endptr, 0);
5652                         if (errno != 0 || *endptr != '\0' ||
5653                             param.fp_mirror_count > LUSTRE_MIRROR_COUNT_MAX) {
5654                                 fprintf(stderr,
5655                                         "error: bad mirror count '%s'\n",
5656                                         optarg);
5657                                 goto err;
5658                         }
5659                         param.fp_check_mirror_count = 1;
5660                         param.fp_exclude_mirror_count = !!neg_opt;
5661                         break;
5662                 case 'm':
5663                 case 'i':
5664                 case 'O': {
5665                         char *buf, *token, *next, *p;
5666                         int len = 1;
5667                         void *tmp;
5668
5669                         buf = strdup(optarg);
5670                         if (!buf) {
5671                                 ret = -ENOMEM;
5672                                 goto err;
5673                         }
5674
5675                         param.fp_exclude_obd = !!neg_opt;
5676
5677                         token = buf;
5678                         while (token && *token) {
5679                                 token = strchr(token, ',');
5680                                 if (token) {
5681                                         len++;
5682                                         token++;
5683                                 }
5684                         }
5685                         if (c == 'm') {
5686                                 param.fp_exclude_mdt = !!neg_opt;
5687                                 param.fp_num_alloc_mdts += len;
5688                                 tmp = realloc(param.fp_mdt_uuid,
5689                                               param.fp_num_alloc_mdts *
5690                                               sizeof(*param.fp_mdt_uuid));
5691                                 if (!tmp) {
5692                                         ret = -ENOMEM;
5693                                         goto err_free;
5694                                 }
5695
5696                                 param.fp_mdt_uuid = tmp;
5697                         } else {
5698                                 param.fp_exclude_obd = !!neg_opt;
5699                                 param.fp_num_alloc_obds += len;
5700                                 tmp = realloc(param.fp_obd_uuid,
5701                                               param.fp_num_alloc_obds *
5702                                               sizeof(*param.fp_obd_uuid));
5703                                 if (!tmp) {
5704                                         ret = -ENOMEM;
5705                                         goto err_free;
5706                                 }
5707
5708                                 param.fp_obd_uuid = tmp;
5709                         }
5710                         for (token = buf; token && *token; token = next) {
5711                                 struct obd_uuid *puuid;
5712
5713                                 if (c == 'm') {
5714                                         puuid =
5715                                         &param.fp_mdt_uuid[param.fp_num_mdts++];
5716                                 } else {
5717                                         puuid =
5718                                         &param.fp_obd_uuid[param.fp_num_obds++];
5719                                 }
5720                                 p = strchr(token, ',');
5721                                 next = 0;
5722                                 if (p) {
5723                                         *p = 0;
5724                                         next = p+1;
5725                                 }
5726
5727                                 if (strlen(token) > sizeof(puuid->uuid) - 1) {
5728                                         ret = -E2BIG;
5729                                         goto err_free;
5730                                 }
5731
5732                                 strncpy(puuid->uuid, token,
5733                                         sizeof(puuid->uuid));
5734                         }
5735 err_free:
5736                         if (buf)
5737                                 free(buf);
5738                         break;
5739                 }
5740 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 18, 53, 0)
5741                 case 'p':
5742 #endif
5743                 case LFS_POOL_OPT:
5744                         if (strlen(optarg) > LOV_MAXPOOLNAME) {
5745                                 fprintf(stderr,
5746                                         "Pool name %s is too long (max %d)\n",
5747                                         optarg, LOV_MAXPOOLNAME);
5748                                 ret = -1;
5749                                 goto err;
5750                         }
5751                         /*
5752                          * We do check for empty pool because empty pool
5753                          * is used to find V1 LOV attributes
5754                          */
5755                         strncpy(param.fp_poolname, optarg, LOV_MAXPOOLNAME);
5756                         param.fp_poolname[LOV_MAXPOOLNAME] = '\0';
5757                         param.fp_exclude_pool = !!neg_opt;
5758                         param.fp_check_pool = 1;
5759                         break;
5760                 case '0':
5761                         param.fp_zero_end = 1;
5762                         break;
5763                 case 'P': /* we always print, this option is a no-op */
5764                         break;
5765                 case LFS_PRINTF_OPT:
5766                         param.fp_format_printf_str = optarg;
5767                         break;
5768                 case LFS_PROJID_OPT:
5769                         rc = name2projid(&param.fp_projid, optarg);
5770                         if (rc) {
5771                                 if (str2quotaid(&param.fp_projid, optarg)) {
5772                                         fprintf(stderr,
5773                                                 "Invalid project ID: %s\n",
5774                                                 optarg);
5775                                         ret = -1;
5776                                         goto err;
5777                                 }
5778                         }
5779                         param.fp_exclude_projid = !!neg_opt;
5780                         param.fp_check_projid = 1;
5781                         break;
5782                 case 's':
5783                         if (optarg[0] == '+') {
5784                                 param.fp_size_sign = -1;
5785                                 optarg++;
5786                         } else if (optarg[0] == '-') {
5787                                 param.fp_size_sign =  1;
5788                                 optarg++;
5789                         }
5790
5791                         ret = llapi_parse_size(optarg, &param.fp_size,
5792                                                &param.fp_size_units, 0);
5793                         if (ret) {
5794                                 fprintf(stderr, "error: bad file size '%s'\n",
5795                                         optarg);
5796                                 goto err;
5797                         }
5798                         param.fp_check_size = 1;
5799                         param.fp_exclude_size = !!neg_opt;
5800                         break;
5801                 case 'S':
5802                         if (optarg[0] == '+') {
5803                                 param.fp_stripe_size_sign = -1;
5804                                 optarg++;
5805                         } else if (optarg[0] == '-') {
5806                                 param.fp_stripe_size_sign =  1;
5807                                 optarg++;
5808                         }
5809
5810                         ret = llapi_parse_size(optarg, &param.fp_stripe_size,
5811                                                &param.fp_stripe_size_units, 0);
5812                         /* assume units of KB if too small to be valid */
5813                         if (param.fp_stripe_size < 4096)
5814                                 param.fp_stripe_size *= 1024;
5815                         if (ret) {
5816                                 fprintf(stderr, "error: bad stripe_size '%s'\n",
5817                                         optarg);
5818                                 goto err;
5819                         }
5820                         param.fp_check_stripe_size = 1;
5821                         param.fp_exclude_stripe_size = !!neg_opt;
5822                         break;
5823                 case 't':
5824                         param.fp_exclude_type = !!neg_opt;
5825                         switch (optarg[0]) {
5826                         case 'b':
5827                                 param.fp_type = S_IFBLK;
5828                                 break;
5829                         case 'c':
5830                                 param.fp_type = S_IFCHR;
5831                                 break;
5832                         case 'd':
5833                                 param.fp_type = S_IFDIR;
5834                                 break;
5835                         case 'f':
5836                                 param.fp_type = S_IFREG;
5837                                 break;
5838                         case 'l':
5839                                 param.fp_type = S_IFLNK;
5840                                 break;
5841                         case 'p':
5842                                 param.fp_type = S_IFIFO;
5843                                 break;
5844                         case 's':
5845                                 param.fp_type = S_IFSOCK;
5846                                 break;
5847                         default:
5848                                 fprintf(stderr, "%s: bad type '%s'\n",
5849                                         progname, optarg);
5850                                 ret = CMD_HELP;
5851                                 goto err;
5852                         }
5853                         break;
5854                 case LFS_FIND_PERM:
5855                         param.fp_exclude_perm = !!neg_opt;
5856                         param.fp_perm_sign = LFS_FIND_PERM_EXACT;
5857                         if (*optarg == '/') {
5858                                 param.fp_perm_sign = LFS_FIND_PERM_ANY;
5859                                 optarg++;
5860                         } else if (*optarg == '-') {
5861                                 param.fp_perm_sign = LFS_FIND_PERM_ALL;
5862                                 optarg++;
5863                         }
5864
5865                         if (str2mode_t(optarg, &param.fp_perm)) {
5866                                 fprintf(stderr, "error: invalid mode '%s'\n",
5867                                         optarg);
5868                                 ret = -1;
5869                                 goto err;
5870                         }
5871                         break;
5872                 case 'T':
5873                         if (optarg[0] == '+') {
5874                                 param.fp_mdt_count_sign = -1;
5875                                 optarg++;
5876                         } else if (optarg[0] == '-') {
5877                                 param.fp_mdt_count_sign =  1;
5878                                 optarg++;
5879                         }
5880
5881                         errno = 0;
5882                         param.fp_mdt_count = strtoul(optarg, &endptr, 0);
5883                         if (errno != 0 || *endptr != '\0' ||
5884                             param.fp_mdt_count >= UINT32_MAX) {
5885                                 fprintf(stderr, "error: bad mdt_count '%s'\n",
5886                                         optarg);
5887                                 ret = -1;
5888                                 goto err;
5889                         }
5890                         param.fp_check_mdt_count = 1;
5891                         param.fp_exclude_mdt_count = !!neg_opt;
5892                         break;
5893                 case 'z':
5894                         if (optarg[0] == '+') {
5895                                 param.fp_ext_size_sign = -1;
5896                                 optarg++;
5897                         } else if (optarg[0] == '-') {
5898                                 param.fp_ext_size_sign =  1;
5899                                 optarg++;
5900                         }
5901
5902                         ret = llapi_parse_size(optarg, &param.fp_ext_size,
5903                                                &param.fp_ext_size_units, 0);
5904                         if (ret) {
5905                                 fprintf(stderr, "error: bad ext-size '%s'\n",
5906                                         optarg);
5907                                 goto err;
5908                         }
5909                         param.fp_ext_size /= SEL_UNIT_SIZE;
5910                         param.fp_ext_size_units /= SEL_UNIT_SIZE;
5911                         param.fp_check_ext_size = 1;
5912                         param.fp_exclude_ext_size = !!neg_opt;
5913                         break;
5914                 default:
5915                         fprintf(stderr, "%s: unrecognized option '%s'\n",
5916                                 progname, argv[optind - 1]);
5917                 case 'h':
5918                         ret = CMD_HELP;
5919                         goto err;
5920                 }
5921         }
5922         if (!param.fp_verbose)
5923                 param.fp_verbose = VERBOSE_DEFAULT;
5924
5925         if (pathstart == -1) {
5926                 fprintf(stderr, "error: %s: no filename|pathname\n",
5927                         argv[0]);
5928                 ret = CMD_HELP;
5929                 goto err;
5930         } else if (pathend == -1) {
5931                 /* no options */
5932                 pathend = argc;
5933         }
5934
5935         do {
5936                 rc = llapi_find(argv[pathstart], &param);
5937                 if (rc && !ret) {
5938                         ret = rc;
5939                         pathbad = pathstart;
5940                 }
5941         } while (++pathstart < pathend);
5942
5943         if (ret)
5944                 fprintf(stderr, "%s: failed for '%s': %s\n",
5945                         progname, argv[pathbad], strerror(-rc));
5946
5947 err:
5948         if (param.fp_obd_uuid && param.fp_num_alloc_obds)
5949                 free(param.fp_obd_uuid);
5950
5951         if (param.fp_mdt_uuid && param.fp_num_alloc_mdts)
5952                 free(param.fp_mdt_uuid);
5953
5954         return ret;
5955 }
5956
5957 static int lfs_getstripe_internal(int argc, char **argv,
5958                                   struct find_param *param)
5959 {
5960         struct option long_opts[] = {
5961 /* find { .val = 'A',   .name = "atime",        .has_arg = required_argument }*/
5962 /* find { .val = 'b',   .name = "blocks",       .has_arg = required_argument }*/
5963 /* find { .val = 'B',   .name = "btime",        .has_arg = required_argument }*/
5964 /* find { .val = 'B',   .name = "Btime",        .has_arg = required_argument }*/
5965         { .val = LFS_COMP_COUNT_OPT,
5966                         .name = "comp-count",   .has_arg = no_argument },
5967         { .val = LFS_COMP_COUNT_OPT,
5968                 .name = "component-count",      .has_arg = no_argument },
5969         { .val = LFS_COMP_FLAGS_OPT,
5970                         .name = "comp-flags",   .has_arg = optional_argument },
5971         { .val = LFS_COMP_FLAGS_OPT,
5972                 .name = "component-flags",      .has_arg = optional_argument },
5973         { .val = LFS_COMP_START_OPT,
5974                         .name = "comp-start",   .has_arg = optional_argument },
5975         { .val = LFS_COMP_START_OPT,
5976                 .name = "component-start",      .has_arg = optional_argument },
5977         { .val = LFS_MIRROR_INDEX_OPT,
5978                 .name = "mirror-index",         .has_arg = required_argument },
5979         { .val = LFS_MIRROR_ID_OPT,
5980                 .name = "mirror-id",            .has_arg = required_argument },
5981         { .val = LFS_NO_FOLLOW_OPT,
5982                 .name = "no-follow",            .has_arg = no_argument },
5983         { .val = 'c',   .name = "stripe-count", .has_arg = no_argument },
5984         { .val = 'c',   .name = "stripe_count", .has_arg = no_argument },
5985 /* find { .val = 'C',   .name = "ctime",        .has_arg = required_argument }*/
5986         { .val = 'd',   .name = "directory",    .has_arg = no_argument },
5987         { .val = 'D',   .name = "default",      .has_arg = no_argument },
5988         { .val = 'E',   .name = "comp-end",     .has_arg = optional_argument },
5989         { .val = 'E',   .name = "component-end", .has_arg = optional_argument },
5990         { .val = 'F',   .name = "fid",          .has_arg = no_argument },
5991         { .val = 'g',   .name = "generation",   .has_arg = no_argument },
5992 /* find { .val = 'G',   .name = "group",        .has_arg = required_argument }*/
5993         { .val = 'h',   .name = "help",         .has_arg = no_argument },
5994 /* dirstripe { .val = 'H', .name = "mdt-hash",  .has_arg = required_argument }*/
5995         { .val = 'i',   .name = "stripe-index", .has_arg = no_argument },
5996         { .val = 'i',   .name = "stripe_index", .has_arg = no_argument },
5997         { .val = 'I',   .name = "comp-id",      .has_arg = optional_argument },
5998         { .val = 'I',   .name = "component-id", .has_arg = optional_argument },
5999 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
6000         { .val = 'L',   .name = "layout",       .has_arg = no_argument },
6001         { .val = 'm',   .name = "mdt",          .has_arg = no_argument },
6002         { .val = 'm',   .name = "mdt-index",    .has_arg = no_argument },
6003         { .val = 'm',   .name = "mdt_index",    .has_arg = no_argument },
6004 /* find { .val = 'M',   .name = "mtime",        .has_arg = required_argument }*/
6005 /* find { .val = 'n',   .name = "name",         .has_arg = required_argument }*/
6006         { .val = 'N',   .name = "mirror-count", .has_arg = no_argument },
6007         { .val = 'O',   .name = "obd",          .has_arg = required_argument },
6008         { .val = 'O',   .name = "ost",          .has_arg = required_argument },
6009         { .val = 'p',   .name = "pool",         .has_arg = no_argument },
6010 /* find { .val = 'P',   .name = "print",        .has_arg = no_argument }, */
6011         { .val = 'q',   .name = "quiet",        .has_arg = no_argument },
6012         { .val = 'r',   .name = "recursive",    .has_arg = no_argument },
6013         { .val = 'R',   .name = "raw",          .has_arg = no_argument },
6014         { .val = 'S',   .name = "stripe-size",  .has_arg = no_argument },
6015         { .val = 'S',   .name = "stripe_size",  .has_arg = no_argument },
6016 /* find { .val = 't',   .name = "type",         .has_arg = required_argument }*/
6017 /* dirstripe { .val = 'T', .name = "mdt-count", .has_arg = required_argument }*/
6018 /* find { .val = 'u',   .name = "uid",          .has_arg = required_argument }*/
6019 /* find { .val = 'U',   .name = "user",         .has_arg = required_argument }*/
6020         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
6021 /* dirstripe { .val = 'X',.name = "max-inherit",.has_arg = required_argument }*/
6022 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }*/
6023         { .val = 'y',   .name = "yaml",         .has_arg = no_argument },
6024         { .val = 'z',   .name = "extension-size", .has_arg = no_argument },
6025         { .val = 'z',   .name = "ext-size",     .has_arg = no_argument },
6026         { .name = NULL } };
6027         int c, rc;
6028         int neg_opt = 0;
6029         int pathstart = -1, pathend = -1;
6030         int isoption;
6031         char *end, *tmp;
6032
6033         while ((c = getopt_long(argc, argv,
6034                         "-cdDE::FghiI::LmMNoO:pqrRsSvyz",
6035                         long_opts, NULL)) != -1) {
6036                 if (neg_opt)
6037                         --neg_opt;
6038
6039                 /* '!' is part of option */
6040                 isoption = (c != 1) || (strcmp(optarg, "!") == 0);
6041                 if (!isoption && pathend != -1) {
6042                         fprintf(stderr,
6043                                 "error: %s: filename|dirname must either precede options or follow options\n",
6044                                 argv[0]);
6045                         return CMD_HELP;
6046                 }
6047                 if (!isoption && pathstart == -1)
6048                         pathstart = optind - 1;
6049                 if (isoption && pathstart != -1 && pathend == -1)
6050                         pathend = optind - 2;
6051
6052                 switch (c) {
6053                 case 1:
6054                         /* unknown: opt is "!" */
6055                         if (strcmp(optarg, "!") == 0)
6056                                 neg_opt = 2;
6057                         break;
6058                 case 'c':
6059                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6060                                 param->fp_verbose |= VERBOSE_COUNT;
6061                                 param->fp_max_depth = 0;
6062                         }
6063                         break;
6064                 case LFS_COMP_COUNT_OPT:
6065                         param->fp_verbose |= VERBOSE_COMP_COUNT;
6066                         param->fp_max_depth = 0;
6067                         break;
6068                 case LFS_COMP_FLAGS_OPT:
6069                         if (optarg) {
6070                                 rc = comp_str2flags(optarg,
6071                                                     &param->fp_comp_flags,
6072                                                     &param->fp_comp_neg_flags);
6073                                 if (rc != 0) {
6074                                         fprintf(stderr,
6075                                                 "error: %s bad component flags '%s'.\n",
6076                                                 argv[0], optarg);
6077                                         return CMD_HELP;
6078                                 }
6079                                 param->fp_check_comp_flags = 1;
6080                         } else {
6081                                 param->fp_verbose |= VERBOSE_COMP_FLAGS;
6082                                 param->fp_max_depth = 0;
6083                         }
6084                         break;
6085                 case LFS_COMP_START_OPT:
6086                         if (optarg) {
6087                                 tmp = optarg;
6088                                 if (tmp[0] == '+') {
6089                                         param->fp_comp_start_sign = -1;
6090                                         tmp++;
6091                                 } else if (tmp[0] == '-') {
6092                                         param->fp_comp_start_sign = 1;
6093                                         tmp++;
6094                                 }
6095                                 rc = llapi_parse_size(tmp,
6096                                                 &param->fp_comp_start,
6097                                                 &param->fp_comp_start_units, 0);
6098                                 if (rc != 0) {
6099                                         fprintf(stderr,
6100                                                 "error: %s bad component start '%s'.\n",
6101                                                 argv[0], tmp);
6102                                         return CMD_HELP;
6103                                 }
6104                                 param->fp_check_comp_start = 1;
6105                         } else {
6106                                 param->fp_verbose |= VERBOSE_COMP_START;
6107                                 param->fp_max_depth = 0;
6108                         }
6109                         break;
6110                 case LFS_MIRROR_INDEX_OPT: {
6111                         unsigned long int mirror_index;
6112
6113                         if (optarg[0] == '+') {
6114                                 param->fp_mirror_index_sign = -1;
6115                                 optarg++;
6116                         } else if (optarg[0] == '-') {
6117                                 param->fp_mirror_index_sign = 1;
6118                                 optarg++;
6119                         }
6120
6121                         errno = 0;
6122                         mirror_index = strtoul(optarg, &end, 0);
6123                         if (errno != 0 || *end != '\0' ||
6124                             mirror_index > UINT16_MAX || (mirror_index == 0 &&
6125                             param->fp_mirror_index_sign == 0 && neg_opt == 0)) {
6126                                 fprintf(stderr,
6127                                         "%s %s: invalid mirror index '%s'\n",
6128                                         progname, argv[0], optarg);
6129                                 return CMD_HELP;
6130                         }
6131
6132                         param->fp_mirror_index = (__u16)mirror_index;
6133
6134                         if (param->fp_mirror_id != 0) {
6135                                 fprintf(stderr,
6136                                         "%s %s: can't specify both mirror index and mirror ID\n",
6137                                         progname, argv[0]);
6138                                 return CMD_HELP;
6139                         }
6140                         param->fp_check_mirror_index = 1;
6141                         param->fp_exclude_mirror_index = !!neg_opt;
6142                         break;
6143                 }
6144                 case LFS_MIRROR_ID_OPT: {
6145                         unsigned long int mirror_id;
6146
6147                         if (optarg[0] == '+') {
6148                                 param->fp_mirror_id_sign = -1;
6149                                 optarg++;
6150                         } else if (optarg[0] == '-') {
6151                                 param->fp_mirror_id_sign = 1;
6152                                 optarg++;
6153                         }
6154
6155                         errno = 0;
6156                         mirror_id = strtoul(optarg, &end, 0);
6157                         if (errno != 0 || *end != '\0' ||
6158                             mirror_id > UINT16_MAX || (mirror_id == 0 &&
6159                             param->fp_mirror_id_sign == 0 && neg_opt == 0)) {
6160                                 fprintf(stderr,
6161                                         "%s %s: invalid mirror ID '%s'\n",
6162                                         progname, argv[0], optarg);
6163                                 return CMD_HELP;
6164                         }
6165
6166                         param->fp_mirror_id = (__u16)mirror_id;
6167
6168                         if (param->fp_mirror_index != 0) {
6169                                 fprintf(stderr,
6170                                         "%s %s: can't specify both mirror index and mirror ID\n",
6171                                         progname, argv[0]);
6172                                 return CMD_HELP;
6173                         }
6174                         param->fp_check_mirror_id = 1;
6175                         param->fp_exclude_mirror_id = !!neg_opt;
6176                         break;
6177                 }
6178                 case LFS_NO_FOLLOW_OPT:
6179                         param->fp_no_follow = true;
6180                         break;
6181                 case 'd':
6182                         param->fp_max_depth = 0;
6183                         break;
6184                 case 'D':
6185                         param->fp_get_default_lmv = 1;
6186                         break;
6187                 case 'E':
6188                         if (optarg) {
6189                                 tmp = optarg;
6190                                 if (tmp[0] == '+') {
6191                                         param->fp_comp_end_sign = -1;
6192                                         tmp++;
6193                                 } else if (tmp[0] == '-') {
6194                                         param->fp_comp_end_sign = 1;
6195                                         tmp++;
6196                                 }
6197
6198                                 if (arg_is_eof(tmp)) {
6199                                         param->fp_comp_end = LUSTRE_EOF;
6200                                         param->fp_comp_end_units = 1;
6201                                         rc = 0;
6202                                 } else {
6203                                         rc = llapi_parse_size(tmp,
6204                                                 &param->fp_comp_end,
6205                                                 &param->fp_comp_end_units, 0);
6206                                         /* assume units of KB if too small */
6207                                         if (param->fp_comp_end < 4096)
6208                                                 param->fp_comp_end *= 1024;
6209                                 }
6210                                 if (rc != 0) {
6211                                         fprintf(stderr,
6212                                                 "error: %s bad component end '%s'.\n",
6213                                                 argv[0], tmp);
6214                                         return CMD_HELP;
6215                                 }
6216                                 param->fp_check_comp_end = 1;
6217                         } else {
6218                                 param->fp_verbose |= VERBOSE_COMP_END;
6219                                 param->fp_max_depth = 0;
6220                         }
6221                         break;
6222                 case 'F':
6223                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6224                                 param->fp_verbose |= VERBOSE_DFID;
6225                                 param->fp_max_depth = 0;
6226                         }
6227                         break;
6228                 case 'g':
6229                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6230                                 param->fp_verbose |= VERBOSE_GENERATION;
6231                                 param->fp_max_depth = 0;
6232                         }
6233                         break;
6234                 case 'i':
6235                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6236                                 param->fp_verbose |= VERBOSE_STRIPE_OFFSET;
6237                                 param->fp_max_depth = 0;
6238                         }
6239                         break;
6240                 case 'I':
6241                         if (optarg) {
6242                                 param->fp_comp_id = strtoul(optarg, &end, 0);
6243                                 if (*end != '\0' || param->fp_comp_id == 0 ||
6244                                     param->fp_comp_id > LCME_ID_MAX) {
6245                                         fprintf(stderr,
6246                                                 "error: %s bad component id '%s'\n",
6247                                                 argv[0], optarg);
6248                                         return CMD_HELP;
6249                                 }
6250                                 param->fp_check_comp_id = 1;
6251                         } else {
6252                                 param->fp_max_depth = 0;
6253                                 param->fp_verbose |= VERBOSE_COMP_ID;
6254                         }
6255                         break;
6256                 case 'L':
6257                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6258                                 param->fp_verbose |= VERBOSE_PATTERN;
6259                                 param->fp_max_depth = 0;
6260                         }
6261                         break;
6262 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6263                 case 'M':
6264                         fprintf(stderr,
6265                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
6266 #endif
6267                 case 'm':
6268                         if (!(param->fp_verbose & VERBOSE_DETAIL))
6269                                 param->fp_max_depth = 0;
6270                         param->fp_verbose |= VERBOSE_MDTINDEX;
6271                         break;
6272                 case 'N':
6273                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6274                                 param->fp_verbose |= VERBOSE_MIRROR_COUNT;
6275                                 param->fp_max_depth = 0;
6276                         }
6277                         break;
6278                 case 'O':
6279                         if (param->fp_obd_uuid) {
6280                                 fprintf(stderr,
6281                                         "error: %s: only one obduuid allowed",
6282                                         argv[0]);
6283                                 return CMD_HELP;
6284                         }
6285                         param->fp_obd_uuid = (struct obd_uuid *)optarg;
6286                         break;
6287                 case 'p':
6288                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6289                                 param->fp_verbose |= VERBOSE_POOL;
6290                                 param->fp_max_depth = 0;
6291                         }
6292                         break;
6293                 case 'q':
6294                         param->fp_quiet++;
6295                         break;
6296                 case 'r':
6297                         param->fp_recursive = 1;
6298                         break;
6299                 case 'R':
6300                         param->fp_raw = 1;
6301                         break;
6302                 case 'S':
6303                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6304                                 param->fp_verbose |= VERBOSE_STRIPE_SIZE;
6305                                 param->fp_max_depth = 0;
6306                         }
6307                         break;
6308                 case 'v':
6309                         param->fp_verbose = VERBOSE_DEFAULT | VERBOSE_DETAIL;
6310                         break;
6311                 case 'y':
6312                         param->fp_yaml = 1;
6313                         break;
6314                 case 'z':
6315                         if (!(param->fp_verbose & VERBOSE_DETAIL)) {
6316                                 param->fp_verbose |= VERBOSE_EXT_SIZE;
6317                                 param->fp_max_depth = 0;
6318                         }
6319                         break;
6320                 default:
6321                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6322                                 progname, argv[optind - 1]);
6323                 case 'h':
6324                         return CMD_HELP;
6325                 }
6326         }
6327
6328         if (pathstart == -1) {
6329                 fprintf(stderr, "error: %s: no filename|pathname\n",
6330                                 argv[0]);
6331                 return CMD_HELP;
6332         } else if (pathend == -1) {
6333                 /* no options */
6334                 pathend = argc;
6335         }
6336
6337         if (pathend > argc)
6338                 return CMD_HELP;
6339
6340         if (param->fp_recursive)
6341                 param->fp_max_depth = -1;
6342         else if (param->fp_verbose & VERBOSE_DETAIL)
6343                 param->fp_max_depth = 1;
6344
6345         if (!param->fp_verbose)
6346                 param->fp_verbose = VERBOSE_DEFAULT;
6347         if (param->fp_quiet)
6348                 param->fp_verbose = VERBOSE_OBJID;
6349
6350         do {
6351                 rc = llapi_getstripe(argv[pathstart], param);
6352         } while (++pathstart < pathend && !rc);
6353
6354         if (rc)
6355                 fprintf(stderr, "error: %s failed for %s.\n",
6356                         argv[0], argv[optind - 1]);
6357         return rc;
6358 }
6359
6360 static int lfs_tgts(int argc, char **argv)
6361 {
6362         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
6363         struct find_param param;
6364         int index = 0, rc = 0;
6365
6366         if (argc > 2)
6367                 return CMD_HELP;
6368
6369         if (argc == 2 && !realpath(argv[1], path)) {
6370                 rc = -errno;
6371                 fprintf(stderr, "error: invalid path '%s': %s\n",
6372                         argv[1], strerror(-rc));
6373                 return rc;
6374         }
6375
6376         while (!llapi_search_mounts(path, index++, mntdir, NULL)) {
6377                 /* Check if we have a mount point */
6378                 if (mntdir[0] == '\0')
6379                         continue;
6380
6381                 memset(&param, 0, sizeof(param));
6382                 if (!strcmp(argv[0], "mdts"))
6383                         param.fp_get_lmv = 1;
6384
6385                 rc = llapi_ostlist(mntdir, &param);
6386                 if (rc) {
6387                         fprintf(stderr, "error: %s: failed on %s\n",
6388                                 argv[0], mntdir);
6389                 }
6390                 if (path[0] != '\0')
6391                         break;
6392                 memset(mntdir, 0, PATH_MAX);
6393         }
6394
6395         return rc;
6396 }
6397
6398 static int lfs_getstripe(int argc, char **argv)
6399 {
6400         struct find_param param = { 0 };
6401
6402         param.fp_max_depth = 1;
6403         return lfs_getstripe_internal(argc, argv, &param);
6404 }
6405
6406 /* functions */
6407 static int lfs_getdirstripe(int argc, char **argv)
6408 {
6409         struct find_param param = { 0 };
6410         struct option long_opts[] = {
6411         { .val = 'c',   .name = "mdt-count",     .has_arg = no_argument },
6412         { .val = 'D',   .name = "default",       .has_arg = no_argument },
6413         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6414         { .val = 'H',   .name = "mdt-hash",      .has_arg = no_argument },
6415         { .val = 'i',   .name = "mdt-index",     .has_arg = no_argument },
6416         { .val = 'm',   .name = "mdt-index",     .has_arg = no_argument },
6417         { .val = 'O',   .name = "obd",           .has_arg = required_argument },
6418         { .val = 'r',   .name = "recursive",     .has_arg = no_argument },
6419         { .val = 'T',   .name = "mdt-count",     .has_arg = no_argument },
6420         { .val = 'v',   .name = "verbose",       .has_arg = no_argument },
6421         { .val = 'X',   .name = "max-inherit",   .has_arg = no_argument },
6422         { .val = 'y',   .name = "yaml",          .has_arg = no_argument },
6423         { .val = LFS_INHERIT_RR_OPT,
6424                         .name = "max-inherit-rr", .has_arg = no_argument },
6425         { .name = NULL } };
6426         int c, rc;
6427
6428         param.fp_get_lmv = 1;
6429
6430         while ((c = getopt_long(argc, argv,
6431                                 "cDhHimO:rtTvXy", long_opts, NULL)) != -1) {
6432                 switch (c) {
6433                 case 'c':
6434                 case 'T':
6435                         param.fp_verbose |= VERBOSE_COUNT;
6436                         break;
6437                 case 'D':
6438                         param.fp_get_default_lmv = 1;
6439                         break;
6440 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6441                 case 't':
6442                         fprintf(stderr,
6443                                 "warning: '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6444                         fallthrough;
6445 #endif
6446                 case 'H':
6447                         param.fp_verbose |= VERBOSE_HASH_TYPE;
6448                         break;
6449                 case 'i':
6450                         fallthrough;
6451                 case 'm':
6452                         param.fp_verbose |= VERBOSE_STRIPE_OFFSET;
6453                         break;
6454                 case 'O':
6455                         if (param.fp_obd_uuid) {
6456                                 fprintf(stderr,
6457                                         "%s: only one obduuid allowed",
6458                                         progname);
6459                                 return CMD_HELP;
6460                         }
6461                         param.fp_obd_uuid = (struct obd_uuid *)optarg;
6462                         break;
6463                 case 'r':
6464                         param.fp_recursive = 1;
6465                         break;
6466                 case 'v':
6467                         param.fp_verbose |= VERBOSE_DETAIL;
6468                         break;
6469                 case 'X':
6470                         param.fp_verbose |= VERBOSE_INHERIT;
6471                         break;
6472                 case LFS_INHERIT_RR_OPT:
6473                         param.fp_verbose |= VERBOSE_INHERIT_RR;
6474                         break;
6475                 case 'y':
6476                         param.fp_yaml = 1;
6477                         break;
6478                 default:
6479                         fprintf(stderr, "%s: unrecognized option '%s'\n",
6480                                 progname, argv[optind - 1]);
6481                         fallthrough;
6482                 case 'h':
6483                         return CMD_HELP;
6484                 }
6485         }
6486
6487         if (optind >= argc)
6488                 return CMD_HELP;
6489
6490         if (param.fp_recursive)
6491                 param.fp_max_depth = -1;
6492
6493         if (!param.fp_verbose)
6494                 param.fp_verbose = VERBOSE_DEFAULT;
6495
6496         do {
6497                 rc = llapi_getstripe(argv[optind], &param);
6498         } while (++optind < argc && !rc);
6499
6500         if (rc)
6501                 fprintf(stderr, "error: %s failed for %s.\n",
6502                         argv[0], argv[optind - 1]);
6503         return rc;
6504 }
6505
6506 enum mntdf_flags {
6507         MNTDF_INODES    = 0x0001,
6508         MNTDF_COOKED    = 0x0002,
6509         MNTDF_LAZY      = 0x0004,
6510         MNTDF_VERBOSE   = 0x0008,
6511         MNTDF_SHOW      = 0x0010,
6512         MNTDF_DECIMAL   = 0x0020,
6513 };
6514
6515 #define COOK(value, base)                                       \
6516 ({                                                              \
6517         int radix = 0;                                          \
6518         while (value > base) {                                  \
6519                 value /= base;                                  \
6520                 radix++;                                        \
6521         }                                                       \
6522         radix;                                                  \
6523 })
6524 #define UUF     "%-20s"
6525 #define CSF     "%11s"
6526 #define CDF     "%11llu"
6527 #define HDF     "%8.1f%c"
6528 #define RSF     "%4s"
6529 #define RDF     "%3d%%"
6530
6531 static inline int obd_statfs_ratio(const struct obd_statfs *st, bool inodes)
6532 {
6533         double avail, used, ratio = 0;
6534
6535         if (inodes) {
6536                 avail = st->os_ffree;
6537                 used = st->os_files - st->os_ffree;
6538         } else {
6539                 avail = st->os_bavail;
6540                 used = st->os_blocks - st->os_bfree;
6541         }
6542         if (avail + used > 0)
6543                 ratio = used / (used + avail) * 100;
6544
6545         /* Round up to match df(1) usage percentage */
6546         return (ratio - (int)ratio) > 0 ? (int)(ratio + 1) : (int)ratio;
6547 }
6548
6549 /*
6550  * This is to identify various problem states for "lfs df" if .osn_err = true,
6551  * so only show flags reflecting those states by default. Informational states
6552  * are only shown with "-v" and use lower-case names to distinguish them.
6553  * UNUSED[12] were for "EROFS = 30" until 1.6 but are now available for use.
6554  */
6555 static struct obd_statfs_state_names {
6556         enum obd_statfs_state   osn_state;
6557         const char              osn_name;
6558         bool                    osn_err;
6559 } oss_names[] = {
6560         { .osn_state = OS_STATFS_DEGRADED,   .osn_name = 'D', .osn_err = true },
6561         { .osn_state = OS_STATFS_READONLY,   .osn_name = 'R', .osn_err = true },
6562         { .osn_state = OS_STATFS_NOPRECREATE,.osn_name = 'N', .osn_err = true },
6563         { .osn_state = OS_STATFS_UNUSED1,    .osn_name = '?', .osn_err = true },
6564         { .osn_state = OS_STATFS_UNUSED2,    .osn_name = '?', .osn_err = true },
6565         { .osn_state = OS_STATFS_ENOSPC,     .osn_name = 'S', .osn_err = true },
6566         { .osn_state = OS_STATFS_ENOINO,     .osn_name = 'I', .osn_err = true },
6567         { .osn_state = OS_STATFS_SUM,        .osn_name = 'a', /* aggregate */ },
6568         { .osn_state = OS_STATFS_NONROT,     .osn_name = 'f', /* flash */     },
6569 };
6570
6571 static int showdf(char *mntdir, struct obd_statfs *stat,
6572                   char *uuid, enum mntdf_flags flags,
6573                   char *type, int index, int rc)
6574 {
6575         long long avail, used, total;
6576         int ratio = 0;
6577         char *suffix = flags & MNTDF_DECIMAL ? "kMGTPEZY" : "KMGTPEZY";
6578         /* Note if we have >2^64 bytes/fs these buffers will need to be grown */
6579         char tbuf[3 * sizeof(__u64)];
6580         char ubuf[3 * sizeof(__u64)];
6581         char abuf[3 * sizeof(__u64)];
6582         char rbuf[3 * sizeof(__u64)];
6583
6584         if (!uuid || !stat)
6585                 return -EINVAL;
6586
6587         switch (rc) {
6588         case 0:
6589                 if (flags & MNTDF_INODES) {
6590                         avail = stat->os_ffree;
6591                         used = stat->os_files - stat->os_ffree;
6592                         total = stat->os_files;
6593                 } else {
6594                         int shift = flags & MNTDF_COOKED ? 0 : 10;
6595
6596                         avail = (stat->os_bavail * stat->os_bsize) >> shift;
6597                         used  = ((stat->os_blocks - stat->os_bfree) *
6598                                  stat->os_bsize) >> shift;
6599                         total = (stat->os_blocks * stat->os_bsize) >> shift;
6600                 }
6601
6602                 ratio = obd_statfs_ratio(stat, flags & MNTDF_INODES);
6603
6604                 if (flags & MNTDF_COOKED) {
6605                         int base = flags & MNTDF_DECIMAL ? 1000 : 1024;
6606                         double cook_val;
6607                         int i;
6608
6609                         cook_val = (double)total;
6610                         i = COOK(cook_val, base);
6611                         if (i > 0)
6612                                 snprintf(tbuf, sizeof(tbuf), HDF, cook_val,
6613                                          suffix[i - 1]);
6614                         else
6615                                 snprintf(tbuf, sizeof(tbuf), CDF, total);
6616
6617                         cook_val = (double)used;
6618                         i = COOK(cook_val, base);
6619                         if (i > 0)
6620                                 snprintf(ubuf, sizeof(ubuf), HDF, cook_val,
6621                                          suffix[i - 1]);
6622                         else
6623                                 snprintf(ubuf, sizeof(ubuf), CDF, used);
6624
6625                         cook_val = (double)avail;
6626                         i = COOK(cook_val, base);
6627                         if (i > 0)
6628                                 snprintf(abuf, sizeof(abuf), HDF, cook_val,
6629                                          suffix[i - 1]);
6630                         else
6631                                 snprintf(abuf, sizeof(abuf), CDF, avail);
6632                 } else {
6633                         snprintf(tbuf, sizeof(tbuf), CDF, total);
6634                         snprintf(ubuf, sizeof(tbuf), CDF, used);
6635                         snprintf(abuf, sizeof(tbuf), CDF, avail);
6636                 }
6637
6638                 sprintf(rbuf, RDF, ratio);
6639                 printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s",
6640                        uuid, tbuf, ubuf, abuf, rbuf, mntdir);
6641                 if (type)
6642                         printf("[%s:%d]", type, index);
6643
6644                 if (stat->os_state) {
6645                         uint32_t i;
6646
6647                         printf(" ");
6648                         for (i = 0; i < ARRAY_SIZE(oss_names); i++) {
6649                                 if (oss_names[i].osn_state & stat->os_state &&
6650                                     (oss_names[i].osn_err ||
6651                                      flags & MNTDF_VERBOSE))
6652                                         printf("%c", oss_names[i].osn_name);
6653                         }
6654                 }
6655
6656                 printf("\n");
6657                 break;
6658         case -ENODATA:
6659                 printf(UUF": inactive device\n", uuid);
6660                 break;
6661         default:
6662                 printf(UUF": %s\n", uuid, strerror(-rc));
6663                 break;
6664         }
6665
6666         return 0;
6667 }
6668
6669 struct ll_stat_type {
6670         int   st_op;
6671         char *st_name;
6672 };
6673
6674 #define LL_STATFS_MAX   LOV_MAX_STRIPE_COUNT
6675
6676 struct ll_statfs_data {
6677         int                     sd_index;
6678         struct obd_statfs       sd_st;
6679 };
6680
6681 struct ll_statfs_buf {
6682         int                     sb_count;
6683         struct ll_statfs_data   sb_buf[LL_STATFS_MAX];
6684 };
6685
6686 static int mntdf(char *mntdir, char *fsname, char *pool, enum mntdf_flags flags,
6687                  int ops, struct ll_statfs_buf *lsb)
6688 {
6689         struct obd_statfs stat_buf, sum = { .os_bsize = 1 };
6690         struct obd_uuid uuid_buf;
6691         char *poolname = NULL;
6692         struct ll_stat_type types[] = {
6693                 { .st_op = LL_STATFS_LMV,       .st_name = "MDT" },
6694                 { .st_op = LL_STATFS_LOV,       .st_name = "OST" },
6695                 { .st_name = NULL } };
6696         struct ll_stat_type *tp;
6697         __u64 ost_files = 0;
6698         __u64 ost_ffree = 0;
6699         __u32 index;
6700         __u32 type;
6701         int fd;
6702         int rc = 0;
6703         int rc2;
6704
6705         if (pool) {
6706                 poolname = strchr(pool, '.');
6707                 if (poolname) {
6708                         if (strncmp(fsname, pool, strlen(fsname))) {
6709                                 fprintf(stderr, "filesystem name incorrect\n");
6710                                 return -ENODEV;
6711                         }
6712                         poolname++;
6713                 } else
6714                         poolname = pool;
6715         }
6716
6717         fd = open(mntdir, O_RDONLY);
6718         if (fd < 0) {
6719                 rc = -errno;
6720                 fprintf(stderr, "%s: cannot open '%s': %s\n", progname, mntdir,
6721                         strerror(errno));
6722                 return rc;
6723         }
6724
6725         if (flags & MNTDF_SHOW) {
6726                 if (flags & MNTDF_INODES)
6727                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6728                                "UUID", "Inodes", "IUsed", "IFree",
6729                                "IUse%", "Mounted on");
6730                 else
6731                         printf(UUF" "CSF" "CSF" "CSF" "RSF" %-s\n",
6732                                "UUID",
6733                                flags & MNTDF_COOKED ? "bytes" : "1K-blocks",
6734                                "Used", "Available", "Use%", "Mounted on");
6735         }
6736
6737         for (tp = types; tp->st_name != NULL; tp++) {
6738                 bool have_ost = false;
6739
6740                 if (!(tp->st_op & ops))
6741                         continue;
6742
6743                 for (index = 0; index < LOV_ALL_STRIPES &&
6744                      (!lsb || lsb->sb_count < LL_STATFS_MAX); index++) {
6745                         memset(&stat_buf, 0, sizeof(struct obd_statfs));
6746                         memset(&uuid_buf, 0, sizeof(struct obd_uuid));
6747                         type = flags & MNTDF_LAZY ?
6748                                 tp->st_op | LL_STATFS_NODELAY : tp->st_op;
6749                         rc2 = llapi_obd_fstatfs(fd, type, index,
6750                                                 &stat_buf, &uuid_buf);
6751                         if (rc2 == -ENODEV)
6752                                 break;
6753                         if (rc2 == -EAGAIN)
6754                                 continue;
6755                         if (rc2 == -ENODATA) { /* Inactive device, OK. */
6756                                 if (!(flags & MNTDF_VERBOSE))
6757                                         continue;
6758                         } else if (rc2 < 0 && rc == 0) {
6759                                 rc = rc2;
6760                         }
6761
6762                         /*
6763                          * If we have OSTs then don't report MDT block counts.
6764                          * For MDT-only filesystems the expectation is that all
6765                          * layouts have a DoM component.  For filesystems with
6766                          * OSTs, files are not necessarily going to store data
6767                          * on MDTs, and MDT space is limited to a fraction of
6768                          * OST space, so don't include it in the summary.
6769                          */
6770                         if (tp->st_op == LL_STATFS_LOV && !have_ost) {
6771                                 have_ost = true;
6772                                 sum.os_blocks = 0;
6773                                 sum.os_bfree = 0;
6774                                 sum.os_bavail = 0;
6775                         }
6776
6777                         if (poolname && tp->st_op == LL_STATFS_LOV &&
6778                             llapi_search_ost(fsname, poolname,
6779                                              obd_uuid2str(&uuid_buf)) != 1)
6780                                 continue;
6781
6782                         /*
6783                          * the llapi_obd_fstatfs() call may have returned with
6784                          * an error, but if it filled in uuid_buf we will at
6785                          * lease use that to print out a message for that OBD.
6786                          * If we didn't get anything in the uuid_buf, then fill
6787                          * it in so that we can print an error message.
6788                          */
6789                         if (uuid_buf.uuid[0] == '\0')
6790                                 snprintf(uuid_buf.uuid, sizeof(uuid_buf.uuid),
6791                                          "%s%04x", tp->st_name, index);
6792                         if (!rc && lsb) {
6793                                 lsb->sb_buf[lsb->sb_count].sd_index = index;
6794                                 lsb->sb_buf[lsb->sb_count].sd_st = stat_buf;
6795                                 lsb->sb_count++;
6796                         }
6797                         if (flags & MNTDF_SHOW)
6798                                 showdf(mntdir, &stat_buf,
6799                                        obd_uuid2str(&uuid_buf), flags,
6800                                        tp->st_name, index, rc2);
6801
6802                         if (rc2)
6803                                 continue;
6804
6805                         if (tp->st_op == LL_STATFS_LMV) {
6806                                 sum.os_ffree += stat_buf.os_ffree;
6807                                 sum.os_files += stat_buf.os_files;
6808                         } else /* if (tp->st_op == LL_STATFS_LOV) */ {
6809                                 ost_files += stat_buf.os_files;
6810                                 ost_ffree += stat_buf.os_ffree;
6811                         }
6812                         sum.os_blocks += stat_buf.os_blocks *
6813                                          stat_buf.os_bsize;
6814                         sum.os_bfree  += stat_buf.os_bfree *
6815                                          stat_buf.os_bsize;
6816                         sum.os_bavail += stat_buf.os_bavail *
6817                                          stat_buf.os_bsize;
6818                 }
6819         }
6820
6821         close(fd);
6822
6823         /*
6824          * If we have _some_ OSTs, but don't have as many free objects on the
6825          * OST as inodes on the MDTs, reduce the reported number of inodes
6826          * to compensate, so that the "inodes in use" number is correct.
6827          * This should be kept in sync with ll_statfs_internal().
6828          */
6829         if (ost_files && ost_ffree < sum.os_ffree) {
6830                 sum.os_files = (sum.os_files - sum.os_ffree) + ost_ffree;
6831                 sum.os_ffree = ost_ffree;
6832         }
6833         if (flags & MNTDF_SHOW) {
6834                 printf("\n");
6835                 showdf(mntdir, &sum, "filesystem_summary:", flags, NULL, 0, 0);
6836                 printf("\n");
6837         }
6838
6839         return rc;
6840 }
6841
6842 enum {
6843         LAYOUT_INHERIT_UNSET    = -2,
6844 };
6845
6846 /* functions */
6847 static int lfs_setdirstripe(int argc, char **argv)
6848 {
6849         char *dname;
6850         struct lfs_setstripe_args lsa = { 0 };
6851         struct llapi_stripe_param *param = NULL;
6852         __u32 mdts[LMV_MAX_STRIPE_COUNT] = { 0 };
6853         char *end;
6854         int c;
6855         char *mode_opt = NULL;
6856         bool default_stripe = false;
6857         bool delete = false;
6858         bool foreign_mode = false;
6859         bool mdt_count_set = false;
6860         mode_t mode = S_IRWXU | S_IRWXG | S_IRWXO;
6861         mode_t previous_mode = 0;
6862         char *xattr = NULL;
6863         __u32 type = LU_FOREIGN_TYPE_SYMLINK, flags = 0;
6864         int max_inherit = LAYOUT_INHERIT_UNSET;
6865         int max_inherit_rr = LAYOUT_INHERIT_UNSET;
6866         struct option long_opts[] = {
6867         { .val = 'c',   .name = "count",        .has_arg = required_argument },
6868         { .val = 'c',   .name = "mdt-count",    .has_arg = required_argument },
6869         { .val = 'd',   .name = "delete",       .has_arg = no_argument },
6870         { .val = 'D',   .name = "default",      .has_arg = no_argument },
6871         { .val = 'D',   .name = "default_stripe", .has_arg = no_argument },
6872         { .val = LFS_LAYOUT_FLAGS_OPT,
6873                         .name = "flags",        .has_arg = required_argument },
6874         { .val = LFS_LAYOUT_FOREIGN_OPT,
6875                         .name = "foreign",      .has_arg = optional_argument},
6876         { .val = 'h',   .name = "help",         .has_arg = no_argument },
6877         { .val = 'H',   .name = "mdt-hash",     .has_arg = required_argument },
6878 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(2, 17, 53, 0)
6879         { .val = 'i',   .name = "mdt-index",    .has_arg = required_argument },
6880         { .val = 'i',   .name = "mdt",          .has_arg = required_argument },
6881 #else
6882 /* find { .val = 'l',   .name = "lazy",         .has_arg = no_argument }, */
6883         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
6884         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
6885 #endif
6886 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6887         { .val = 'i',   .name = "index",        .has_arg = required_argument },
6888 #endif
6889         { .val = 'o',   .name = "mode",         .has_arg = required_argument },
6890 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6891         { .val = 't',   .name = "hash-type",    .has_arg = required_argument },
6892 #endif
6893         { .val = 'T',   .name = "mdt-count",    .has_arg = required_argument },
6894         { .val = 'x',   .name = "xattr",        .has_arg = required_argument },
6895         { .val = 'X',   .name = "max-inherit",  .has_arg = required_argument },
6896         { .val = LFS_INHERIT_RR_OPT,
6897                         .name = "max-inherit-rr", .has_arg = required_argument},
6898 /* setstripe { .val = 'y', .name = "yaml",      .has_arg = no_argument }, */
6899 /* setstripe { .val = 'W', .name = "bandwidth", .has_arg = required_argument }, */
6900         { .name = NULL } };
6901         int result = 0;
6902
6903         setstripe_args_init(&lsa);
6904
6905         while ((c = getopt_long(argc, argv, "c:dDi:hH:m:o:t:T:x:X:",
6906                                 long_opts, NULL)) >= 0) {
6907                 switch (c) {
6908                 case 0:
6909                         /* Long options. */
6910                         break;
6911                 case 'c':
6912                 case 'T':
6913                         errno = 0;
6914                         lsa.lsa_stripe_count = strtoul(optarg, &end, 0);
6915                         if (errno != 0 || *end != '\0' ||
6916                             lsa.lsa_stripe_count < -1 ||
6917                             lsa.lsa_stripe_count > LOV_MAX_STRIPE_COUNT) {
6918                                 fprintf(stderr,
6919                                         "%s: invalid stripe count '%s'\n",
6920                                         progname, optarg);
6921                                 return CMD_HELP;
6922                         }
6923                         mdt_count_set = true;
6924                         break;
6925                 case 'd':
6926                         delete = true;
6927                         default_stripe = true;
6928                         break;
6929                 case 'D':
6930                         default_stripe = true;
6931                         break;
6932                 case LFS_LAYOUT_FOREIGN_OPT:
6933                         if (optarg) {
6934                                 /* check pure numeric */
6935                                 type = strtoul(optarg, &end, 0);
6936                                 if (*end) {
6937                                         /* check name */
6938                                         type = check_foreign_type_name(optarg);
6939                                         if (type == LU_FOREIGN_TYPE_UNKNOWN) {
6940                                                 fprintf(stderr,
6941                                                         "%s %s: unknown foreign type '%s'\n",
6942                                                         progname, argv[0],
6943                                                         optarg);
6944                                                 return CMD_HELP;
6945                                         }
6946                                 } else if (type >= UINT32_MAX) {
6947                                         fprintf(stderr,
6948                                                 "%s %s: invalid foreign type '%s'\n",
6949                                                 progname, argv[0], optarg);
6950                                         return CMD_HELP;
6951                                 }
6952                         }
6953                         foreign_mode = true;
6954                         break;
6955                 case LFS_LAYOUT_FLAGS_OPT:
6956                         errno = 0;
6957                         flags = strtoul(optarg, &end, 16);
6958                         if (errno != 0 || *end != '\0' ||
6959                             flags >= UINT32_MAX) {
6960                                 fprintf(stderr,
6961                                         "%s %s: invalid hex flags '%s'\n",
6962                                         progname, argv[0], optarg);
6963                                 return CMD_HELP;
6964                         }
6965                         if (!foreign_mode) {
6966                                 fprintf(stderr,
6967                                         "%s %s: hex flags must be specified with --foreign option\n",
6968                                         progname, argv[0]);
6969                                 return CMD_HELP;
6970                         }
6971                         break;
6972 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6973                 case 't':
6974                         fprintf(stderr,
6975                                 "warning: '--hash-type' and '-t' deprecated, use '--mdt-hash' or '-H' instead\n");
6976                         fallthrough;
6977 #endif
6978                 case 'H':
6979                         lsa.lsa_pattern = check_hashtype(optarg);
6980                         if (lsa.lsa_pattern == 0) {
6981                                 fprintf(stderr,
6982                                         "%s %s: bad directory hash type '%s'\n",
6983                                         progname, argv[0], optarg);
6984                                 return CMD_HELP;
6985                         }
6986                         break;
6987                 case 'i':
6988 #if LUSTRE_VERSION_CODE >= OBD_OCD_VERSION(2, 17, 53, 0)
6989                 case 'm':
6990 #endif
6991 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
6992                         if (strcmp(argv[optind - 1], "--index") == 0)
6993                                 fprintf(stderr,
6994                                         "%s %s: warning: '--index' deprecated, use '--mdt-index' instead\n",
6995                                         progname, argv[0]);
6996 #endif
6997                         lsa.lsa_nr_tgts = parse_targets(mdts,
6998                                                 sizeof(mdts) / sizeof(__u32),
6999                                                 lsa.lsa_nr_tgts, optarg, NULL);
7000                         if (lsa.lsa_nr_tgts < 0) {
7001                                 fprintf(stderr,
7002                                         "%s %s: invalid MDT target(s) '%s'\n",
7003                                         progname, argv[0], optarg);
7004                                 return CMD_HELP;
7005                         }
7006
7007                         lsa.lsa_tgts = mdts;
7008                         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7009                                 lsa.lsa_stripe_off = mdts[0];
7010                         break;
7011                 case 'o':
7012                         mode_opt = optarg;
7013                         break;
7014                 case 'x':
7015                         xattr = optarg;
7016                         break;
7017                 case 'X':
7018                         errno = 0;
7019                         max_inherit = strtol(optarg, &end, 10);
7020                         if (errno != 0 || *end != '\0' || max_inherit < -2) {
7021                                 fprintf(stderr,
7022                                         "%s %s: invalid max-inherit '%s'\n",
7023                                         progname, argv[0], optarg);
7024                                 return CMD_HELP;
7025                         }
7026                         if (max_inherit == 0) {
7027                                 max_inherit = LMV_INHERIT_NONE;
7028                         } else if (max_inherit == -1) {
7029                                 max_inherit = LMV_INHERIT_UNLIMITED;
7030                         } else if (max_inherit > LMV_INHERIT_MAX) {
7031                                 fprintf(stderr,
7032                                         "%s %s: max-inherit %d exceeds maximum %u\n",
7033                                         progname, argv[0], max_inherit,
7034                                         LMV_INHERIT_MAX);
7035                                 return CMD_HELP;
7036                         }
7037                         break;
7038                 case LFS_INHERIT_RR_OPT:
7039                         if (!default_stripe) {
7040                                 fprintf(stderr,
7041                                         "%s %s: '--max-inherit-rr' must be specified with '-D'\n",
7042                                         progname, argv[0]);
7043                                 return CMD_HELP;
7044                         }
7045                         errno = 0;
7046                         max_inherit_rr = strtol(optarg, &end, 10);
7047                         if (errno != 0 || *end != '\0' || max_inherit_rr < -2) {
7048                                 fprintf(stderr,
7049                                         "%s %s: invalid max-inherit-rr '%s'\n",
7050                                         progname, argv[0], optarg);
7051                                 return CMD_HELP;
7052                         }
7053                         if (max_inherit_rr == 0) {
7054                                 max_inherit_rr = LMV_INHERIT_RR_NONE;
7055                         } else if (max_inherit_rr == -1) {
7056                                 max_inherit_rr = LMV_INHERIT_RR_UNLIMITED;
7057                         } else if (max_inherit_rr > LMV_INHERIT_RR_MAX) {
7058                                 fprintf(stderr,
7059                                         "%s %s: max-inherit-rr %d exceeds maximum %u\n",
7060                                         progname, argv[0], max_inherit_rr,
7061                                         LMV_INHERIT_RR_MAX);
7062                                 return CMD_HELP;
7063                         }
7064                         break;
7065                 default:
7066                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7067                                 progname, argv[optind - 1]);
7068                         fallthrough;
7069                 case 'h':
7070                         return CMD_HELP;
7071                 }
7072         }
7073
7074         if (optind == argc) {
7075                 fprintf(stderr, "%s %s: DIR must be specified\n",
7076                         progname, argv[0]);
7077                 return CMD_HELP;
7078         }
7079
7080         if (xattr && !foreign_mode) {
7081                 /*
7082                  * only print a warning as this is armless and will be
7083                  * ignored
7084                  */
7085                 fprintf(stderr,
7086                         "%s %s: xattr has been specified for non-foreign layout\n",
7087                         progname, argv[0]);
7088         } else if (foreign_mode && !xattr) {
7089                 fprintf(stderr,
7090                         "%s %s: xattr must be provided in foreign mode\n",
7091                         progname, argv[0]);
7092                 return CMD_HELP;
7093         }
7094
7095         if (foreign_mode && (delete || default_stripe || lsa.lsa_nr_tgts ||
7096             lsa.lsa_tgts || setstripe_args_specified(&lsa))) {
7097                 fprintf(stderr,
7098                         "%s %s: only --xattr/--flags/--mode options are valid with --foreign\n",
7099                         progname, argv[0]);
7100                 return CMD_HELP;
7101         }
7102
7103         if (!delete && lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT &&
7104             lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT && !foreign_mode) {
7105                 /* if no parameters set, create directory on least-used MDTs */
7106                 lsa.lsa_stripe_off = LMV_OFFSET_DEFAULT;
7107                 lsa.lsa_stripe_count = 1;
7108         }
7109
7110         if (delete &&
7111             (lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT ||
7112              lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)) {
7113                 fprintf(stderr,
7114                         "%s %s: cannot specify -d with -c or -i options\n",
7115                         progname, argv[0]);
7116                 return CMD_HELP;
7117         }
7118
7119         if (mode_opt) {
7120                 mode = strtoul(mode_opt, &end, 8);
7121                 if (*end != '\0') {
7122                         fprintf(stderr,
7123                                 "%s %s: bad MODE '%s'\n",
7124                                 progname, argv[0], mode_opt);
7125                         return CMD_HELP;
7126                 }
7127                 previous_mode = umask(0);
7128         }
7129
7130         /* check max-inherit and warn user in some cases */
7131         if (default_stripe &&
7132             (lsa.lsa_stripe_count < 0 || lsa.lsa_stripe_count > 1)) {
7133                 if (max_inherit == LMV_INHERIT_UNLIMITED)
7134                         fprintf(stderr,
7135                         "%s %s: unrecommended max-inherit=-1 when default stripe-count=%lld\n",
7136                         progname, argv[0], lsa.lsa_stripe_count);
7137                 else if (max_inherit > LMV_INHERIT_DEFAULT_STRIPED + 2 &&
7138                          max_inherit != LMV_INHERIT_NONE)
7139                         fprintf(stderr,
7140                                 "%s %s: unrecommended max-inherit=%d when default stripe-count=%lld\n",
7141                                 progname, argv[0], max_inherit,
7142                                 lsa.lsa_stripe_count);
7143         }
7144
7145         if (default_stripe && lsa.lsa_nr_tgts > 1 && !mdt_count_set) {
7146                 fprintf(stderr,
7147                         "%s %s: trying to create unrecommended default striped directory layout,\n"
7148                         "       '-D -i x,y,z' will stripe every new directory across all MDTs,\n"
7149                         "       add -c with the number of MDTs to do this anyway\n",
7150                         progname, argv[0]);
7151                 return CMD_HELP;
7152         }
7153
7154         if (max_inherit_rr != LAYOUT_INHERIT_UNSET &&
7155             lsa.lsa_stripe_off != LLAPI_LAYOUT_DEFAULT &&
7156             lsa.lsa_stripe_off != LMV_OFFSET_DEFAULT) {
7157                 fprintf(stderr,
7158                         "%s %s: max-inherit-rr needs mdt-index=-1, not %lld\n",
7159                         progname, argv[0], lsa.lsa_stripe_off);
7160                 return CMD_HELP;
7161         }
7162
7163         /* foreign LMV/dir case */
7164         if (foreign_mode) {
7165                 if (argc > optind + 1) {
7166                         fprintf(stderr,
7167                                 "%s %s: cannot specify multiple foreign dirs\n",
7168                                 progname, argv[0]);
7169                         return CMD_HELP;
7170                 }
7171
7172                 dname = argv[optind];
7173                 result = llapi_dir_create_foreign(dname, mode, type, flags,
7174                                                   xattr);
7175                 if (result != 0)
7176                         fprintf(stderr,
7177                                 "%s mkdir: can't create foreign dir '%s': %s\n",
7178                                 progname, dname, strerror(-result));
7179                 return result;
7180         }
7181
7182         /*
7183          * initialize stripe parameters, in case param is converted to specific,
7184          * i.e, 'lfs mkdir -i -1 -c N', always allocate space for lsp_tgts.
7185          */
7186         param = calloc(1, offsetof(typeof(*param),
7187                        lsp_tgts[lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT ?
7188                                 lsa.lsa_stripe_count : lsa.lsa_nr_tgts]));
7189         if (!param) {
7190                 fprintf(stderr,
7191                         "%s %s: cannot allocate memory for parameters: %s\n",
7192                         progname, argv[0], strerror(ENOMEM));
7193                 return CMD_HELP;
7194         }
7195
7196         /* if "lfs setdirstripe -D -i -1" is used, assume 1-stripe directory */
7197         if (default_stripe && lsa.lsa_stripe_off == LMV_OFFSET_DEFAULT &&
7198             (lsa.lsa_stripe_count == LLAPI_LAYOUT_DEFAULT ||
7199              lsa.lsa_stripe_count == 0))
7200                 lsa.lsa_stripe_count = 1;
7201         if (lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT)
7202                 param->lsp_stripe_count = lsa.lsa_stripe_count;
7203         if (lsa.lsa_stripe_off == LLAPI_LAYOUT_DEFAULT)
7204                 param->lsp_stripe_offset = LMV_OFFSET_DEFAULT;
7205         else
7206                 param->lsp_stripe_offset = lsa.lsa_stripe_off;
7207         if (lsa.lsa_pattern != LLAPI_LAYOUT_RAID0)
7208                 param->lsp_stripe_pattern = lsa.lsa_pattern;
7209         else
7210                 param->lsp_stripe_pattern = LMV_HASH_TYPE_UNKNOWN;
7211         param->lsp_pool = lsa.lsa_pool_name;
7212         param->lsp_is_specific = false;
7213         if (max_inherit == LAYOUT_INHERIT_UNSET) {
7214                 if (lsa.lsa_stripe_count == 0 || lsa.lsa_stripe_count == 1)
7215                         max_inherit = LMV_INHERIT_DEFAULT_PLAIN;
7216                 else
7217                         max_inherit = LMV_INHERIT_DEFAULT_STRIPED;
7218         }
7219         param->lsp_max_inherit = max_inherit;
7220         if (default_stripe) {
7221
7222                 if (max_inherit_rr == LAYOUT_INHERIT_UNSET)
7223                         max_inherit_rr = LMV_INHERIT_RR_DEFAULT;
7224                 param->lsp_max_inherit_rr = max_inherit_rr;
7225         }
7226         if (strcmp(argv[0], "mkdir") == 0)
7227                 param->lsp_is_create = true;
7228         if (lsa.lsa_nr_tgts > 1) {
7229                 if (lsa.lsa_stripe_count > 0 &&
7230                     lsa.lsa_stripe_count != LLAPI_LAYOUT_DEFAULT &&
7231                     lsa.lsa_stripe_count != lsa.lsa_nr_tgts) {
7232                         fprintf(stderr,
7233                                 "error: %s: stripe count %lld doesn't match the number of MDTs: %d\n",
7234                                 argv[0], lsa.lsa_stripe_count,
7235                                 lsa.lsa_nr_tgts);
7236                         free(param);
7237                         return CMD_HELP;
7238                 }
7239
7240                 param->lsp_is_specific = true;
7241                 param->lsp_stripe_count = lsa.lsa_nr_tgts;
7242                 memcpy(param->lsp_tgts, mdts, sizeof(*mdts) * lsa.lsa_nr_tgts);
7243         }
7244
7245         dname = argv[optind];
7246         do {
7247                 if (default_stripe) {
7248                         result = llapi_dir_set_default_lmv(dname, param);
7249                         if (result)
7250                                 fprintf(stderr,
7251                                         "%s setdirstripe: cannot set default stripe on dir '%s': %s\n",
7252                                         progname, dname, strerror(-result));
7253                         continue;
7254                 }
7255
7256                 result = llapi_dir_create(dname, mode, param);
7257                 if (result)
7258                         fprintf(stderr,
7259                                 "%s setdirstripe: cannot create dir '%s': %s\n",
7260                                 progname, dname, strerror(-result));
7261         } while (!result && (dname = argv[++optind]));
7262
7263         if (mode_opt)
7264                 umask(previous_mode);
7265
7266         free(param);
7267         return result;
7268 }
7269
7270 /* functions */
7271 static int lfs_rmentry(int argc, char **argv)
7272 {
7273         char *dname;
7274         int   index;
7275         int   result = 0;
7276
7277         if (argc <= 1) {
7278                 fprintf(stderr, "error: %s: missing dirname\n",
7279                         argv[0]);
7280                 return CMD_HELP;
7281         }
7282
7283         index = 1;
7284         dname = argv[index];
7285         while (dname) {
7286                 result = llapi_direntry_remove(dname);
7287                 if (result) {
7288                         fprintf(stderr,
7289                                 "error: %s: remove dir entry '%s' failed\n",
7290                                 argv[0], dname);
7291                         break;
7292                 }
7293                 dname = argv[++index];
7294         }
7295         return result;
7296 }
7297
7298 static int lfs_unlink_foreign(int argc, char **argv)
7299 {
7300         char *name;
7301         int   index;
7302         int   result = 0;
7303
7304         if (argc <= 1) {
7305                 fprintf(stderr, "error: %s: missing pathname\n",
7306                         argv[0]);
7307                 return CMD_HELP;
7308         }
7309
7310         index = 1;
7311         name = argv[index];
7312         while (name != NULL) {
7313                 result = llapi_unlink_foreign(name);
7314                 if (result) {
7315                         fprintf(stderr,
7316                                 "error: %s: unlink foreign entry '%s' failed\n",
7317                                 argv[0], name);
7318                         break;
7319                 }
7320                 name = argv[++index];
7321         }
7322         return result;
7323 }
7324
7325 static int lfs_mv(int argc, char **argv)
7326 {
7327         struct lmv_user_md lmu = { LMV_USER_MAGIC };
7328         struct find_param param = {
7329                 .fp_max_depth = -1,
7330                 .fp_mdt_index = -1,
7331         };
7332         char *end;
7333         int c;
7334         int rc = 0;
7335         struct option long_opts[] = {
7336         { .val = 'm',   .name = "mdt",          .has_arg = required_argument },
7337         { .val = 'm',   .name = "mdt-index",    .has_arg = required_argument },
7338         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7339         { .name = NULL } };
7340
7341         while ((c = getopt_long(argc, argv, "m:M:v", long_opts, NULL)) != -1) {
7342                 switch (c) {
7343 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
7344                 case 'M':
7345                         fprintf(stderr,
7346                                 "warning: '-M' deprecated, use '--mdt-index' or '-m' instead\n");
7347 #endif
7348                 case 'm':
7349                         errno = 0;
7350                         lmu.lum_stripe_offset = strtoul(optarg, &end, 0);
7351                         if (errno != 0 || *end != '\0' ||
7352                             lmu.lum_stripe_offset >= UINT32_MAX) {
7353                                 fprintf(stderr, "%s mv: bad MDT index '%s'\n",
7354                                         progname, optarg);
7355                                 return CMD_HELP;
7356                         }
7357                         break;
7358                 case 'v':
7359                         param.fp_verbose = VERBOSE_DETAIL;
7360                         break;
7361                 default:
7362                         fprintf(stderr, "%s mv: unrecognized option '%s'\n",
7363                                 progname, argv[optind - 1]);
7364                         return CMD_HELP;
7365                 }
7366         }
7367
7368         if (lmu.lum_stripe_offset == LMV_OFFSET_DEFAULT) {
7369                 fprintf(stderr, "%s mv: MDT index must be specified\n",
7370                         progname);
7371                 return CMD_HELP;
7372         }
7373
7374         if (optind >= argc) {
7375                 fprintf(stderr, "%s mv: DIR must be specified\n", progname);
7376                 return CMD_HELP;
7377         }
7378
7379         lmu.lum_hash_type = LMV_HASH_TYPE_UNKNOWN;
7380
7381         /* initialize migrate mdt parameters */
7382         param.fp_lmv_md = &lmu;
7383         param.fp_migrate = 1;
7384         rc = llapi_migrate_mdt(argv[optind], &param);
7385         if (rc != 0)
7386                 fprintf(stderr, "%s mv: cannot migrate '%s' to MDT%04x: %s\n",
7387                         progname, argv[optind], lmu.lum_stripe_offset,
7388                         strerror(-rc));
7389         return rc;
7390 }
7391
7392 static int lfs_osts(int argc, char **argv)
7393 {
7394         return lfs_tgts(argc, argv);
7395 }
7396
7397 static int lfs_mdts(int argc, char **argv)
7398 {
7399         return lfs_tgts(argc, argv);
7400 }
7401
7402 static int lfs_df(int argc, char **argv)
7403 {
7404         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7405         enum mntdf_flags flags = MNTDF_SHOW;
7406         int ops = LL_STATFS_LMV | LL_STATFS_LOV;
7407         int c, rc = 0, rc1 = 0, index = 0, arg_idx = 0;
7408         char fsname[PATH_MAX] = "", *pool_name = NULL;
7409         struct option long_opts[] = {
7410         { .val = 'h',   .name = "human-readable", .has_arg = no_argument },
7411         { .val = 'H',   .name = "si",           .has_arg = no_argument },
7412         { .val = 'i',   .name = "inodes",       .has_arg = no_argument },
7413         { .val = 'l',   .name = "lazy",         .has_arg = no_argument },
7414         { .val = 'p',   .name = "pool",         .has_arg = required_argument },
7415         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
7416         { .name = NULL} };
7417
7418         while ((c = getopt_long(argc, argv, "hHilp:v", long_opts, NULL)) != -1) {
7419                 switch (c) {
7420                 case 'h':
7421                         flags = (flags & ~MNTDF_DECIMAL) | MNTDF_COOKED;
7422                         break;
7423                 case 'H':
7424                         flags |= MNTDF_COOKED | MNTDF_DECIMAL;
7425                         break;
7426                 case 'i':
7427                         flags |= MNTDF_INODES;
7428                         break;
7429                 case 'l':
7430                         flags |= MNTDF_LAZY;
7431                         break;
7432                 case 'p':
7433                         pool_name = optarg;
7434                         break;
7435                 case 'v':
7436                         flags |= MNTDF_VERBOSE;
7437                         break;
7438                 default:
7439                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7440                                 progname, argv[optind - 1]);
7441                         return CMD_HELP;
7442                 }
7443         }
7444
7445         /* Handle case where path is not specified */
7446         if (optind == argc) {
7447                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7448                         /* Check if we have a mount point */
7449                         if (mntdir[0] == '\0')
7450                                 continue;
7451
7452                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7453                         if (rc || path[0] != '\0')
7454                                 break;
7455
7456                         fsname[0] = '\0'; /* avoid matching in next loop */
7457                         mntdir[0] = '\0'; /* avoid matching in next loop */
7458                         path[0] = '\0'; /* clean for next loop */
7459                 }
7460                 return rc;
7461         }
7462
7463         /* Loop through all the remaining arguments. These are Lustre FS
7464          * paths.
7465          */
7466         for (arg_idx = optind; arg_idx <= argc - 1; arg_idx++) {
7467                 bool valid = false;
7468
7469                 fsname[0] = '\0'; /* start clean */
7470                 mntdir[0] = '\0'; /* start clean */
7471                 path[0] = '\0';   /* start clean */
7472
7473                 /* path does not exists at all */
7474                 if (!realpath(argv[arg_idx], path)) {
7475                         rc = -errno;
7476                         fprintf(stderr, "error: invalid path '%s': %s\n",
7477                                 argv[arg_idx], strerror(-rc));
7478                         /* save first seen error */
7479                         if (!rc1)
7480                                 rc1 = rc;
7481
7482                         continue;
7483                 }
7484
7485                 /* path exists but may not be a Lustre filesystem */
7486                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7487                         /* Check if we have a mount point */
7488                         if (mntdir[0] == '\0')
7489                                 continue;
7490
7491                         rc = mntdf(mntdir, fsname, pool_name, flags, ops, NULL);
7492                         if (rc || path[0] != '\0') {
7493                                 valid = true;
7494
7495                                 /* save first seen error */
7496                                 if (!rc1)
7497                                         rc1 = rc;
7498                                 break;
7499                         }
7500                 }
7501
7502                 if (!valid) {
7503                         llapi_printf(LLAPI_MSG_ERROR,
7504                                      "%s:%s Not a Lustre filesystem\n",
7505                                      argv[0], argv[arg_idx]);
7506                         /* save first seen error */
7507                         if (!rc1)
7508                                 rc1 = -EOPNOTSUPP;
7509                 }
7510         }
7511
7512         return rc1;
7513 }
7514
7515 static int print_instance(const char *mntdir, char *buf, size_t buflen,
7516                           bool opt_instance, bool opt_fsname, bool opt_mntdir)
7517 {
7518         int rc = 0;
7519
7520         if (opt_fsname == opt_instance) { /* both true or both false */
7521                 rc = llapi_getname(mntdir, buf, buflen);
7522         } else if (opt_fsname) {
7523                 /*
7524                  * llapi_search_mounts() fills @buf with fsname, but that is not
7525                  * called if explicit paths are specified on the command-line
7526                  */
7527                 if (buf[0] == '\0')
7528                         rc = llapi_get_fsname(mntdir, buf, buflen);
7529         } else /* if (opt_instance) */ {
7530                 rc = llapi_get_instance(mntdir, buf, buflen);
7531         }
7532
7533         if (rc < 0) {
7534                 fprintf(stderr, "cannot get instance for '%s': %s\n",
7535                         mntdir, strerror(-rc));
7536                 return rc;
7537         }
7538
7539         if (opt_mntdir)
7540                 printf("%s %s\n", buf, mntdir);
7541         else
7542                 printf("%s\n", buf);
7543
7544         return 0;
7545 }
7546
7547 static int lfs_getname(int argc, char **argv)
7548 {
7549         struct option long_opts[] = {
7550         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7551         { .val = 'i',   .name = "instance",     .has_arg = no_argument },
7552         { .val = 'n',   .name = "fsname",       .has_arg = no_argument },
7553         { .name = NULL} };
7554         bool opt_instance = false, opt_fsname = false;
7555         char fsname[PATH_MAX] = "";
7556         int rc = 0, rc2, c;
7557
7558         while ((c = getopt_long(argc, argv, "hin", long_opts, NULL)) != -1) {
7559                 switch (c) {
7560                 case 'i':
7561                         opt_instance = true;
7562                         break;
7563                 case 'n':
7564                         opt_fsname = true;
7565                         break;
7566                 default:
7567                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7568                                 progname, argv[optind - 1]);
7569                         fallthrough;
7570                 case 'h':
7571                         return CMD_HELP;
7572                 }
7573         }
7574
7575         if (optind == argc) { /* no paths specified, get all paths. */
7576                 char mntdir[PATH_MAX] = "", path[PATH_MAX] = "";
7577                 int index = 0;
7578
7579                 while (!llapi_search_mounts(path, index++, mntdir, fsname)) {
7580                         rc2 = print_instance(mntdir, fsname, sizeof(fsname),
7581                                              opt_instance, opt_fsname, true);
7582                         if (!rc)
7583                                 rc = rc2;
7584                         path[0] = fsname[0] = mntdir[0] = '\0';
7585                 }
7586         } else { /* paths specified, only attempt to search these. */
7587                 bool opt_mntdir;
7588
7589                 /* if only one path is given, print only requested info */
7590                 opt_mntdir = argc - optind > 1 || (opt_instance == opt_fsname);
7591
7592                 for (; optind < argc; optind++) {
7593                         rc2 = print_instance(argv[optind], fsname,
7594                                              sizeof(fsname), opt_instance,
7595                                              opt_fsname, opt_mntdir);
7596                         if (!rc)
7597                                 rc = rc2;
7598                         fsname[0] = '\0';
7599                 }
7600         }
7601
7602         return rc;
7603 }
7604
7605 static int lfs_check(int argc, char **argv)
7606 {
7607         char mntdir[PATH_MAX] = {'\0'}, path[PATH_MAX] = {'\0'};
7608         int num_types = 1;
7609         char *obd_types[3];
7610         char obd_type1[4];
7611         char obd_type2[4];
7612         char obd_type3[4];
7613         int rc;
7614
7615         if (argc < 2 || argc > 3) {
7616                 fprintf(stderr, "%s check: server type must be specified\n",
7617                         progname);
7618                 return CMD_HELP;
7619         }
7620
7621         obd_types[0] = obd_type1;
7622         obd_types[1] = obd_type2;
7623         obd_types[2] = obd_type3;
7624
7625         if (strcmp(argv[1], "osts") == 0) {
7626                 strcpy(obd_types[0], "osc");
7627         } else if (strcmp(argv[1], "mdts") == 0 ||
7628                    strcmp(argv[1], "mds") == 0) {
7629                 strcpy(obd_types[0], "mdc");
7630         } else if (strcmp(argv[1], "mgts") == 0) {
7631                 strcpy(obd_types[0], "mgc");
7632         } else if (strcmp(argv[1], "all") == 0 ||
7633                    strcmp(argv[1], "servers") == 0) {
7634                 num_types = 3;
7635                 strcpy(obd_types[0], "osc");
7636                 strcpy(obd_types[1], "mdc");
7637                 strcpy(obd_types[2], "mgc");
7638         } else {
7639                 fprintf(stderr, "%s check: unrecognized option '%s'\n",
7640                         progname, argv[1]);
7641                 return CMD_HELP;
7642         }
7643
7644         if (argc >= 3 && !realpath(argv[2], path)) {
7645                 rc = -errno;
7646                 fprintf(stderr, "error: invalid path '%s': %s\n",
7647                         argv[2], strerror(-rc));
7648                 return rc;
7649         }
7650
7651         rc = llapi_search_mounts(path, 0, mntdir, NULL);
7652         if (rc < 0 || mntdir[0] == '\0') {
7653                 fprintf(stderr,
7654                         "%s check: cannot find mounted Lustre filesystem: %s\n",
7655                         progname, (rc < 0) ? strerror(-rc) : strerror(ENODEV));
7656                 return rc;
7657         }
7658
7659         rc = llapi_target_check(num_types, obd_types, path);
7660         if (rc)
7661                 fprintf(stderr, "%s check: cannot check target '%s': %s\n",
7662                         progname, argv[1], strerror(-rc));
7663
7664         return rc;
7665 }
7666
7667 #ifdef HAVE_SYS_QUOTA_H
7668 #define ADD_OVERFLOW(a, b) \
7669                      ((((a) + (b)) < (a)) ? \
7670                       ((a) = ULONG_MAX) : ((a) = (a) + (b)))
7671
7672 /* Convert format time string "XXwXXdXXhXXmXXs" into seconds value
7673  * returns the value or ULONG_MAX on integer overflow or incorrect format
7674  * Notes:
7675  *        1. the order of specifiers is arbitrary (may be: 5w3s or 3s5w)
7676  *        2. specifiers may be encountered multiple times (2s3s is 5 seconds)
7677  *        3. empty integer value is interpreted as 0
7678  */
7679 static unsigned long str2sec(const char *timestr)
7680 {
7681         const char spec[] = "smhdw";
7682         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
7683         unsigned long val = 0;
7684         char *tail;
7685
7686         if (strpbrk(timestr, spec) == NULL) {
7687                 /*
7688                  * no specifiers inside the time string,
7689                  * should treat it as an integer value
7690                  */
7691                 val = strtoul(timestr, &tail, 10);
7692                 return *tail ? ULONG_MAX : val;
7693         }
7694
7695         /* format string is XXwXXdXXhXXmXXs */
7696         while (*timestr) {
7697                 unsigned long v;
7698                 int ind;
7699                 char *ptr;
7700
7701                 v = strtoul(timestr, &tail, 10);
7702                 if (v == ULONG_MAX || *tail == '\0')
7703                         /*
7704                          * value too large (ULONG_MAX or more)
7705                          * or missing specifier
7706                          */
7707                         goto error;
7708
7709                 ptr = strchr(spec, *tail);
7710                 if (!ptr)
7711                         /* unknown specifier */
7712                         goto error;
7713
7714                 ind = ptr - spec;
7715
7716                 /* check if product will overflow the type */
7717                 if (!(v < ULONG_MAX / mult[ind]))
7718                         goto error;
7719
7720                 ADD_OVERFLOW(val, mult[ind] * v);
7721                 if (val == ULONG_MAX)
7722                         goto error;
7723
7724                 timestr = tail + 1;
7725         }
7726
7727         return val;
7728
7729 error:
7730         return ULONG_MAX;
7731 }
7732
7733 #define ARG2ULL(nr, str, def_units)                                     \
7734 do {                                                                    \
7735         unsigned long long limit, units = def_units;                    \
7736         int rc;                                                         \
7737                                                                         \
7738         rc = llapi_parse_size(str, &limit, &units, 1);                  \
7739         if (rc < 0) {                                                   \
7740                 fprintf(stderr, "%s: invalid limit '%s'\n",             \
7741                         progname, str);                                 \
7742                 return CMD_HELP;                                        \
7743         }                                                               \
7744         nr = limit;                                                     \
7745 } while (0)
7746
7747 static inline int has_times_option(int argc, char **argv)
7748 {
7749         int i;
7750
7751         for (i = 1; i < argc; i++)
7752                 if (!strcmp(argv[i], "-t"))
7753                         return 1;
7754
7755         return 0;
7756 }
7757
7758 static inline int lfs_verify_poolarg(char *pool)
7759 {
7760         if (strnlen(optarg, LOV_MAXPOOLNAME + 1) > LOV_MAXPOOLNAME) {
7761                 fprintf(stderr,
7762                         "Pool name '%.*s' is longer than %d\n",
7763                         LOV_MAXPOOLNAME, pool, LOV_MAXPOOLNAME);
7764                 return 1;
7765         }
7766         return 0;
7767 }
7768
7769 /* special grace time, only notify the user when its quota is over soft limit
7770  * but doesn't block new writes until the hard limit is reached.
7771  */
7772 #define NOTIFY_GRACE            "notify"
7773 #define NOTIFY_GRACE_TIME       LQUOTA_GRACE_MASK
7774
7775 #ifndef toqb
7776 static inline __u64 lustre_stoqb(size_t space)
7777 {
7778         return (space + QIF_DQBLKSIZE - 1) >> QIF_DQBLKSIZE_BITS;
7779 }
7780 #else
7781 #define lustre_stoqb   toqb
7782 #endif
7783
7784 int lfs_setquota_times(int argc, char **argv, struct if_quotactl *qctl)
7785 {
7786         int c, rc;
7787         char *mnt, *obd_type = (char *)qctl->obd_type;
7788         struct obd_dqblk *dqb = &qctl->qc_dqblk;
7789         struct obd_dqinfo *dqi = &qctl->qc_dqinfo;
7790         struct option long_opts[] = {
7791         { .val = 'b',   .name = "block-grace",  .has_arg = required_argument },
7792         { .val = 'g',   .name = "group",        .has_arg = no_argument },
7793         { .val = 'h',   .name = "help",         .has_arg = no_argument },
7794         { .val = 'i',   .name = "inode-grace",  .has_arg = required_argument },
7795         { .val = 'p',   .name = "projid",       .has_arg = no_argument },
7796         { .val = 't',   .name = "times",        .has_arg = no_argument },
7797         { .val = 'u',   .name = "user",         .has_arg = no_argument },
7798         { .val = LFS_POOL_OPT,
7799                         .name = "pool",         .has_arg = required_argument },
7800         { .name = NULL } };
7801         int qtype;
7802
7803         qctl->qc_cmd  = LUSTRE_Q_SETINFO;
7804         qctl->qc_type = ALLQUOTA;
7805
7806         while ((c = getopt_long(argc, argv, "b:ghi:ptu",
7807                                 long_opts, NULL)) != -1) {
7808                 switch (c) {
7809                 case 'u':
7810                         qtype = USRQUOTA;
7811                         goto quota_type;
7812                 case 'g':
7813                         qtype = GRPQUOTA;
7814                         goto quota_type;
7815                 case 'p':
7816                         qtype = PRJQUOTA;
7817 quota_type:
7818                         if (qctl->qc_type != ALLQUOTA) {
7819                                 fprintf(stderr,
7820                                         "%s: -u/g/p cannot be used more than once\n",
7821                                         progname);
7822                                 return CMD_HELP;
7823                         }
7824                         qctl->qc_type = qtype;
7825                         break;
7826                 case 'b':
7827                         if (strncmp(optarg, NOTIFY_GRACE,
7828                                     strlen(NOTIFY_GRACE)) == 0) {
7829                                 dqi->dqi_bgrace = NOTIFY_GRACE_TIME;
7830                         } else {
7831                                 dqi->dqi_bgrace = str2sec(optarg);
7832                                 if (dqi->dqi_bgrace >= NOTIFY_GRACE_TIME) {
7833                                         fprintf(stderr,
7834                                                 "%s: bad block-grace: %s\n",
7835                                                 progname, optarg);
7836                                         return CMD_HELP;
7837                                 }
7838                         }
7839                         dqb->dqb_valid |= QIF_BTIME;
7840                         break;
7841                 case 'i':
7842                         if (strncmp(optarg, NOTIFY_GRACE,
7843                                     strlen(NOTIFY_GRACE)) == 0) {
7844                                 dqi->dqi_igrace = NOTIFY_GRACE_TIME;
7845                         } else {
7846                                 dqi->dqi_igrace = str2sec(optarg);
7847                                 if (dqi->dqi_igrace >= NOTIFY_GRACE_TIME) {
7848                                         fprintf(stderr,
7849                                                 "%s: bad inode-grace: %s\n",
7850                                                 progname, optarg);
7851                                         return CMD_HELP;
7852                                 }
7853                         }
7854                         dqb->dqb_valid |= QIF_ITIME;
7855                         break;
7856                 case 't': /* Yes, of course! */
7857                         break;
7858                 case LFS_POOL_OPT:
7859                         if (lfs_verify_poolarg(optarg))
7860                                 return -1;
7861                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
7862                         qctl->qc_cmd  = LUSTRE_Q_SETINFOPOOL;
7863                         break;
7864                 /* getopt prints error message for us when opterr != 0 */
7865                 default:
7866                         fprintf(stderr, "%s: unrecognized option '%s'\n",
7867                                 progname, argv[optind - 1]);
7868                         fallthrough;
7869                 case 'h':
7870                         return CMD_HELP;
7871                 }
7872         }
7873
7874         if (qctl->qc_type == ALLQUOTA) {
7875                 fprintf(stderr, "%s: neither -u, -g nor -p specified\n",
7876                         progname);
7877                 return CMD_HELP;
7878         }
7879
7880         if (optind != argc - 1) {
7881                 fprintf(stderr, "%s: unexpected parameter '%s'\n",
7882                         progname, argv[optind + 1]);
7883                 return CMD_HELP;
7884         }
7885
7886         mnt = argv[optind];
7887         rc = llapi_quotactl(mnt, qctl);
7888         if (rc) {
7889                 if (*obd_type)
7890                         fprintf(stderr, "%s %s ", obd_type,
7891                                 obd_uuid2str(&qctl->obd_uuid));
7892                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
7893                 return rc;
7894         }
7895
7896         return 0;
7897 }
7898
7899 static int lfs_reset_quota(char *mnt, struct if_quotactl *qctl)
7900 {
7901         struct if_quotactl tmp_qctl;
7902         int index, md_count, dt_count;
7903         int wait_phase = 0, wait_index = 0, wait_count = 0;
7904         int rc, rc2;
7905
7906         /* reset the quota ID, the existing quota setting will be returned */
7907         rc = llapi_quotactl(mnt, qctl);
7908         if (rc)
7909                 return rc;
7910
7911         /* sanity check */
7912         if ((qctl->qc_dqblk.dqb_valid & QIF_LIMITS) != QIF_LIMITS) {
7913                 fprintf(stderr,
7914                         "the existing quota settings are not returned!\n");
7915                 return -EINVAL;
7916         }
7917
7918         rc = llapi_get_obd_count(mnt, &md_count, 1);
7919         if (rc) {
7920                 fprintf(stderr, "can not get mdt count: %s\n", strerror(-rc));
7921                 return rc;
7922         }
7923
7924         rc = llapi_get_obd_count(mnt, &dt_count, 0);
7925         if (rc) {
7926                 fprintf(stderr, "can not get ost count: %s\n", strerror(-rc));
7927                 return rc;
7928         }
7929
7930         memset(&tmp_qctl, 0, sizeof(tmp_qctl));
7931         tmp_qctl.qc_type = qctl->qc_type;
7932         tmp_qctl.qc_id = qctl->qc_id;
7933         tmp_qctl.qc_cmd = LUSTRE_Q_GETQUOTA;
7934
7935 retry:
7936         if (wait_phase == 0) {
7937                 for (index = wait_index; index < md_count; index++) {
7938                         tmp_qctl.qc_idx = index;
7939                         tmp_qctl.qc_valid = QC_MDTIDX;
7940                         rc = llapi_quotactl(mnt, &tmp_qctl);
7941                         if (rc == -ENODEV || rc == -ENODATA)
7942                                 continue;
7943                         if (rc) {
7944                                 fprintf(stderr, "quotactl mdt%d failed: %s\n",
7945                                         index, strerror(-rc));
7946                                 break;
7947                         }
7948                         /* check whether the md quota grant is reset */
7949                         if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
7950                             tmp_qctl.qc_dqblk.dqb_ihardlimit != 0)
7951                                 break;
7952                 }
7953
7954                 if (index < md_count) {
7955                         wait_phase = 0;
7956                         wait_index = index;
7957                         goto wait;
7958                 }
7959         } else {
7960                 for (index = wait_index; index < dt_count; index++) {
7961                         tmp_qctl.qc_idx = index;
7962                         tmp_qctl.qc_valid = QC_OSTIDX;
7963                         rc = llapi_quotactl(mnt, &tmp_qctl);
7964                         if (rc == -ENODEV || rc == -ENODATA)
7965                                 continue;
7966                         if (rc) {
7967                                 fprintf(stderr, "quotactl mdt%d failed: %s\n",
7968                                         index, strerror(-rc));
7969                                 break;
7970                         }
7971                         /* check whether the dt quota grant is reset */
7972                         if (tmp_qctl.qc_dqblk.dqb_valid & QIF_LIMITS &&
7973                             tmp_qctl.qc_dqblk.dqb_bhardlimit != 0)
7974                                 break;
7975                 }
7976
7977                 if (index < dt_count) {
7978                         wait_phase = 1;
7979                         wait_index = index;
7980                         goto wait;
7981                 }
7982         }
7983
7984         if (wait_phase == 0) {
7985                 wait_phase = 1;
7986                 goto retry;
7987         }
7988
7989         goto out;
7990
7991 wait:
7992         if (rc || wait_count > 30) {
7993                 fprintf(stderr, "fail to reset the quota ID %d on OBDs\n",
7994                         qctl->qc_id);
7995                 goto out;
7996         }
7997
7998         wait_count++;
7999         sleep(1);
8000         fprintf(stdout, "wait %d seconds for OBDs to reset the quota ID %u\n",
8001                 wait_count, qctl->qc_id);
8002         goto retry;
8003
8004
8005 out:
8006         /* restore the quota setting */
8007         if (qctl->qc_dqblk.dqb_isoftlimit == 0 &&
8008             qctl->qc_dqblk.dqb_ihardlimit == 0 &&
8009             qctl->qc_dqblk.dqb_bsoftlimit == 0 &&
8010             qctl->qc_dqblk.dqb_bhardlimit == 0)
8011                 return rc;
8012
8013         memcpy(&tmp_qctl, qctl, sizeof(tmp_qctl));
8014         tmp_qctl.qc_cmd = LUSTRE_Q_SETQUOTA;
8015         rc2 = llapi_quotactl(mnt, &tmp_qctl);
8016         if (!rc2)
8017                 return rc;
8018
8019         fprintf(stderr,
8020                 "fail to restore the quota setting: %s, please restore it manually by\n  lfs setquota %s %d",
8021                 strerror(-rc2),
8022                 qctl->qc_type == USRQUOTA ? "-u" :
8023                                 (qctl->qc_type == GRPQUOTA ? "-g" : "-p"),
8024                 qctl->qc_id);
8025
8026         if (qctl->qc_dqblk.dqb_isoftlimit != 0)
8027                 fprintf(stderr, " -i %llu",
8028                         (unsigned long long)qctl->qc_dqblk.dqb_isoftlimit);
8029         if (qctl->qc_dqblk.dqb_ihardlimit != 0)
8030                 fprintf(stderr, " -I %llu",
8031                         (unsigned long long)qctl->qc_dqblk.dqb_ihardlimit);
8032         if (qctl->qc_dqblk.dqb_bsoftlimit != 0)
8033                 fprintf(stderr, " -b %llu",
8034                         (unsigned long long)qctl->qc_dqblk.dqb_bsoftlimit);
8035         if (qctl->qc_dqblk.dqb_bhardlimit != 0)
8036                 fprintf(stderr, " -B %llu",
8037                         (unsigned long long)qctl->qc_dqblk.dqb_bhardlimit);
8038
8039         fprintf(stderr, " %s\n", mnt);
8040         if (!rc)
8041                 rc = rc2;
8042
8043         return rc;
8044 }
8045
8046 #define BSLIMIT (1 << 0)
8047 #define BHLIMIT (1 << 1)
8048 #define ISLIMIT (1 << 2)
8049 #define IHLIMIT (1 << 3)
8050
8051 int lfs_setquota(int argc, char **argv)
8052 {
8053         int c, rc = 0;
8054         struct if_quotactl *qctl;
8055         char *mnt, *obd_type;
8056         struct obd_dqblk *dqb;
8057         struct option long_opts[] = {
8058         { .val = 'b',   .name = "block-softlimit",
8059                                                 .has_arg = required_argument },
8060         { .val = 'B',   .name = "block-hardlimit",
8061                                                 .has_arg = required_argument },
8062         { .val = 'd',   .name = "default",      .has_arg = no_argument },
8063         { .val = LFS_SETQUOTA_DELETE,
8064                         .name = "delete",       .has_arg = no_argument },
8065         { .val = 'g',   .name = "group",        .has_arg = required_argument },
8066         { .val = 'G',   .name = "default-grp",  .has_arg = no_argument },
8067         { .val = 'h',   .name = "help",         .has_arg = no_argument },
8068         { .val = 'i',   .name = "inode-softlimit",
8069                                                 .has_arg = required_argument },
8070         { .val = 'I',   .name = "inode-hardlimit",
8071                                                 .has_arg = required_argument },
8072         { .val = 'p',   .name = "projid",       .has_arg = required_argument },
8073         { .val = 'P',   .name = "default-prj",  .has_arg = no_argument },
8074         { .val = 'r',   .name = "reset",        .has_arg = no_argument },
8075         { .val = 'u',   .name = "user",         .has_arg = required_argument },
8076         { .val = 'U',   .name = "default-usr",  .has_arg = no_argument },
8077         { .val = LFS_POOL_OPT,
8078                         .name = "pool",         .has_arg = required_argument },
8079         { .name = NULL } };
8080         unsigned int limit_mask = 0;
8081         bool use_default = false;
8082         int qtype, qctl_len;
8083
8084         qctl_len = sizeof(*qctl) + LOV_MAXPOOLNAME + 1;
8085         qctl = malloc(qctl_len);
8086         if (!qctl)
8087                 return -ENOMEM;
8088
8089         memset(qctl, 0, qctl_len);
8090         obd_type = (char *)qctl->obd_type;
8091         dqb = &qctl->qc_dqblk;
8092
8093         if (has_times_option(argc, argv)) {
8094                 rc = lfs_setquota_times(argc, argv, qctl);
8095                 goto out;
8096         }
8097
8098         qctl->qc_cmd  = LUSTRE_Q_SETQUOTA;
8099         qctl->qc_type = ALLQUOTA; /* ALLQUOTA makes no sense for setquota,
8100                                    * so it can be used as a marker that qc_type
8101                                    * isn't reinitialized from command line
8102                                    */
8103         while ((c = getopt_long(argc, argv, "b:B:dDg:Ghi:I:p:Pru:U",
8104                 long_opts, NULL)) != -1) {
8105                 switch (c) {
8106                 case 'U':
8107                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8108                         qtype = USRQUOTA;
8109                         qctl->qc_id = 0;
8110                         goto quota_type_def;
8111                 case 'u':
8112                         qtype = USRQUOTA;
8113                         rc = name2uid(&qctl->qc_id, optarg);
8114                         goto quota_type;
8115                 case 'G':
8116                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8117                         qtype = GRPQUOTA;
8118                         qctl->qc_id = 0;
8119                         goto quota_type_def;
8120                 case 'g':
8121                         qtype = GRPQUOTA;
8122                         rc = name2gid(&qctl->qc_id, optarg);
8123                         goto quota_type;
8124                 case 'P':
8125                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8126                         qtype = PRJQUOTA;
8127                         qctl->qc_id = 0;
8128                         goto quota_type_def;
8129                 case 'p':
8130                         qtype = PRJQUOTA;
8131                         rc = name2projid(&qctl->qc_id, optarg);
8132 quota_type:
8133                         if (rc) {
8134                                 if (str2quotaid(&qctl->qc_id, optarg)) {
8135                                         fprintf(stderr,
8136                                                 "%s setquota: invalid id '%s'\n",
8137                                                 progname, optarg);
8138                                         rc = -1;
8139                                         goto out;
8140                                 }
8141                         }
8142
8143                         if (qctl->qc_id == 0) {
8144                                 fprintf(stderr,
8145                                         "%s setquota: can't set quota for root usr/group/project.\n",
8146                                         progname);
8147                                 rc = -1;
8148                                 goto out;
8149                         }
8150
8151 quota_type_def:
8152                         if (qctl->qc_type != ALLQUOTA) {
8153                                 fprintf(stderr,
8154                                         "%s setquota: only one of -u, -U, -g, -G, -p or -P may be specified\n",
8155                                         progname);
8156                                 rc = CMD_HELP;
8157                                 goto out;
8158                         }
8159                         qctl->qc_type = qtype;
8160                         break;
8161 #if LUSTRE_VERSION_CODE < OBD_OCD_VERSION(3, 0, 53, 0)
8162                 case 'd':
8163                         fprintf(stderr,
8164                                 "%s setquota: '-d' deprecated, use '-D' or '--default'\n",
8165                                 progname);
8166                         fallthrough;
8167 #endif
8168                 case 'D':
8169                         use_default = true;
8170                         qctl->qc_cmd = LUSTRE_Q_SETDEFAULT;
8171                         break;
8172                 case LFS_SETQUOTA_DELETE:
8173                         qctl->qc_cmd = LUSTRE_Q_DELETEQID;
8174                         break;
8175                 case 'b':
8176                         ARG2ULL(dqb->dqb_bsoftlimit, optarg, 1024);
8177                         dqb->dqb_bsoftlimit >>= 10;
8178                         limit_mask |= BSLIMIT;
8179                         if (dqb->dqb_bsoftlimit &&
8180                             dqb->dqb_bsoftlimit <= 1024) /* <= 1M? */
8181                                 fprintf(stderr,
8182                                         "%s setquota: warning: block softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8183                                         progname,
8184                                         (unsigned long long)dqb->dqb_bsoftlimit,
8185                                         progname);
8186                         break;
8187                 case 'B':
8188                         ARG2ULL(dqb->dqb_bhardlimit, optarg, 1024);
8189                         dqb->dqb_bhardlimit >>= 10;
8190                         limit_mask |= BHLIMIT;
8191                         if (dqb->dqb_bhardlimit &&
8192                             dqb->dqb_bhardlimit <= 1024) /* <= 1M? */
8193                                 fprintf(stderr,
8194                                         "%s setquota: warning: block hardlimit '%llu' smaller than minimum qunit size\n"
8195                                         "See '%s help setquota' or Lustre manual for details\n",
8196                                         progname,
8197                                         (unsigned long long)dqb->dqb_bhardlimit,
8198                                         progname);
8199                         break;
8200                 case 'i':
8201                         ARG2ULL(dqb->dqb_isoftlimit, optarg, 1);
8202                         limit_mask |= ISLIMIT;
8203                         if (dqb->dqb_isoftlimit &&
8204                             dqb->dqb_isoftlimit <= 1024) /* <= 1K inodes? */
8205                                 fprintf(stderr,
8206                                         "%s setquota: warning: inode softlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8207                                         progname,
8208                                         (unsigned long long)dqb->dqb_isoftlimit,
8209                                         progname);
8210                         break;
8211                 case 'I':
8212                         ARG2ULL(dqb->dqb_ihardlimit, optarg, 1);
8213                         limit_mask |= IHLIMIT;
8214                         if (dqb->dqb_ihardlimit &&
8215                             dqb->dqb_ihardlimit <= 1024) /* <= 1K inodes? */
8216                                 fprintf(stderr,
8217                                         "%s setquota: warning: inode hardlimit '%llu' smaller than minimum qunit size\nSee '%s help setquota' or Lustre manual for details\n",
8218                                         progname,
8219                                         (unsigned long long)dqb->dqb_ihardlimit,
8220                                         progname);
8221                         break;
8222                 case LFS_POOL_OPT:
8223                         if (lfs_verify_poolarg(optarg)) {
8224                                 rc = -1;
8225                                 goto out;
8226                         }
8227                         strncpy(qctl->qc_poolname, optarg, LOV_MAXPOOLNAME);
8228                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_SETDEFAULT ?
8229                                                 LUSTRE_Q_SETDEFAULT_POOL :
8230                                                 LUSTRE_Q_SETQUOTAPOOL;
8231                         break;
8232                 case 'r':
8233                         qctl->qc_cmd = LUSTRE_Q_RESETQID;
8234                         break;
8235                 default:
8236                         fprintf(stderr,
8237                                 "%s setquota: unrecognized option '%s'\n",
8238                                 progname, argv[optind - 1]);
8239                         fallthrough;
8240                 case 'h':
8241                         rc = CMD_HELP;
8242                         goto out;
8243                 }
8244         }
8245
8246         if (qctl->qc_type == ALLQUOTA) {
8247                 fprintf(stderr,
8248                         "%s setquota: either -u or -g must be specified\n",
8249                         progname);
8250                 rc = CMD_HELP;
8251                 goto out;
8252         }
8253
8254         if (!use_default && qctl->qc_cmd != LUSTRE_Q_DELETEQID &&
8255             qctl->qc_cmd != LUSTRE_Q_RESETQID && limit_mask == 0) {
8256                 fprintf(stderr,
8257                         "%s setquota: at least one limit must be specified\n",
8258                         progname);
8259                 rc = CMD_HELP;
8260                 goto out;
8261         }
8262
8263         if ((use_default || qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8264              qctl->qc_cmd == LUSTRE_Q_RESETQID) && limit_mask != 0) {
8265                 fprintf(stderr,
8266                         "%s setquota: limits should not be specified when using default quota, deleting or resetting quota ID\n",
8267                         progname);
8268                 rc = CMD_HELP;
8269                 goto out;
8270         }
8271
8272         if (use_default && qctl->qc_id == 0) {
8273                 fprintf(stderr,
8274                         "%s setquota: can not set default quota for root user/group/project\n",
8275                         progname);
8276                 rc = CMD_HELP;
8277                 goto out;
8278         }
8279
8280         if ((qctl->qc_cmd == LUSTRE_Q_DELETEQID ||
8281              qctl->qc_cmd == LUSTRE_Q_RESETQID)  && qctl->qc_id == 0) {
8282                 fprintf(stderr,
8283                         "%s setquota: can not delete or reset root user/group/project\n",
8284                         progname);
8285                 rc = CMD_HELP;
8286                 goto out;
8287         }
8288
8289         if (optind != argc - 1) {
8290                 fprintf(stderr,
8291                         "%s setquota: filesystem not specified or unexpected argument '%s'\n",
8292                         progname, argv[optind]);
8293                 rc = CMD_HELP;
8294                 goto out;
8295         }
8296
8297         mnt = argv[optind];
8298
8299         if (use_default) {
8300                 dqb->dqb_bhardlimit = 0;
8301                 dqb->dqb_bsoftlimit = 0;
8302                 dqb->dqb_ihardlimit = 0;
8303                 dqb->dqb_isoftlimit = 0;
8304                 dqb->dqb_itime = 0;
8305                 dqb->dqb_btime = 0;
8306                 dqb->dqb_valid |= QIF_LIMITS | QIF_TIMES;
8307                 /* do not set inode limits for Pool Quotas */
8308                 if (qctl->qc_cmd  == LUSTRE_Q_SETDEFAULT_POOL)
8309                         dqb->dqb_valid ^= QIF_ILIMITS | QIF_ITIME;
8310         } else if ((!(limit_mask & BHLIMIT) ^ !(limit_mask & BSLIMIT)) ||
8311                    (!(limit_mask & IHLIMIT) ^ !(limit_mask & ISLIMIT))) {
8312                 /* sigh, we can't just set blimits/ilimits */
8313                 struct if_quotactl *tmp_qctl;
8314
8315                 tmp_qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
8316                 if (!tmp_qctl)
8317                         goto out;
8318
8319                 if (qctl->qc_cmd == LUSTRE_Q_SETQUOTAPOOL) {
8320                         tmp_qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
8321                         strncpy(tmp_qctl->qc_poolname, qctl->qc_poolname,
8322                                 LOV_MAXPOOLNAME);
8323                 } else {
8324                         tmp_qctl->qc_cmd  = LUSTRE_Q_GETQUOTA;
8325                 }
8326                 tmp_qctl->qc_type = qctl->qc_type;
8327                 tmp_qctl->qc_id = qctl->qc_id;
8328
8329                 rc = llapi_quotactl(mnt, tmp_qctl);
8330                 if (rc < 0) {
8331                         free(tmp_qctl);
8332                         goto out;
8333                 }
8334
8335                 if (!(limit_mask & BHLIMIT))
8336                         dqb->dqb_bhardlimit = tmp_qctl->qc_dqblk.dqb_bhardlimit;
8337                 if (!(limit_mask & BSLIMIT))
8338                         dqb->dqb_bsoftlimit = tmp_qctl->qc_dqblk.dqb_bsoftlimit;
8339                 if (!(limit_mask & IHLIMIT))
8340                         dqb->dqb_ihardlimit = tmp_qctl->qc_dqblk.dqb_ihardlimit;
8341                 if (!(limit_mask & ISLIMIT))
8342                         dqb->dqb_isoftlimit = tmp_qctl->qc_dqblk.dqb_isoftlimit;
8343
8344                 /* Keep grace times if we have got no softlimit arguments */
8345                 if ((limit_mask & BHLIMIT) && !(limit_mask & BSLIMIT)) {
8346                         dqb->dqb_valid |= QIF_BTIME;
8347                         dqb->dqb_btime = tmp_qctl->qc_dqblk.dqb_btime;
8348                 }
8349
8350                 if ((limit_mask & IHLIMIT) && !(limit_mask & ISLIMIT)) {
8351                         dqb->dqb_valid |= QIF_ITIME;
8352                         dqb->dqb_itime = tmp_qctl->qc_dqblk.dqb_itime;
8353                 }
8354                 free(tmp_qctl);
8355         }
8356
8357         dqb->dqb_valid |= (limit_mask & (BHLIMIT | BSLIMIT)) ? QIF_BLIMITS : 0;
8358         dqb->dqb_valid |= (limit_mask & (IHLIMIT | ISLIMIT)) ? QIF_ILIMITS : 0;
8359
8360         if (qctl->qc_cmd == LUSTRE_Q_RESETQID)
8361                 rc = lfs_reset_quota(mnt, qctl);
8362         else
8363                 rc = llapi_quotactl(mnt, qctl);
8364
8365         if (rc) {
8366                 if (*obd_type)
8367                         fprintf(stderr,
8368                                 "%s setquota: cannot quotactl '%s' '%s': %s\n",
8369                                 progname, obd_type,
8370                                 obd_uuid2str(&qctl->obd_uuid), strerror(-rc));
8371                 else
8372                         fprintf(stderr,
8373                                 "%s setquota: quotactl failed: %s\n",
8374                                 progname, strerror(-rc));
8375         }
8376 out:
8377         if (rc)
8378                 fprintf(stderr, "setquota failed: %s\n", strerror(-rc));
8379
8380         free(qctl);
8381         return rc;
8382 }
8383
8384 /* Converts seconds value into format string
8385  * result is returned in buf
8386  * Notes:
8387  *        1. result is in descenting order: 1w2d3h4m5s
8388  *        2. zero fields are not filled (except for p. 3): 5d1s
8389  *        3. zero seconds value is presented as "0s"
8390  */
8391 static char *__sec2str(time_t seconds, char *buf)
8392 {
8393         const char spec[] = "smhdw";
8394         const unsigned long mult[] = {1, 60, 60*60, 24*60*60, 7*24*60*60};
8395         unsigned long c;
8396         char *tail = buf;
8397         int i;
8398
8399         for (i = ARRAY_SIZE(mult) - 1 ; i >= 0; i--) {
8400                 c = seconds / mult[i];
8401
8402                 if (c > 0 || (i == 0 && buf == tail))
8403                         tail += scnprintf(tail, 40-(tail-buf), "%lu%c", c,
8404                                           spec[i]);
8405
8406                 seconds %= mult[i];
8407         }
8408
8409         return tail;
8410 }
8411
8412 static void sec2str(time_t seconds, char *buf, int rc)
8413 {
8414         char *tail = buf;
8415
8416         if (rc)
8417                 *tail++ = '[';
8418
8419         tail = __sec2str(seconds, tail);
8420
8421         if (rc && tail - buf < 39) {
8422                 *tail++ = ']';
8423                 *tail++ = 0;
8424         }
8425 }
8426
8427 static void diff2str(time_t seconds, char *buf, time_t now)
8428 {
8429         buf[0] = 0;
8430         if (!seconds)
8431                 return;
8432         if (seconds <= now) {
8433                 strcpy(buf, "expired");
8434                 return;
8435         }
8436         __sec2str(seconds - now, buf);
8437 }
8438
8439 static void print_quota_title(char *name, struct if_quotactl *qctl,
8440                               bool human_readable, bool show_default)
8441 {
8442         if (show_default) {
8443                 printf("Disk default %s quota:\n", qtype_name(qctl->qc_type));
8444                 printf("%15s %8s%8s%8s %8s%8s%8s\n",
8445                        "Filesystem", "bquota", "blimit", "bgrace",
8446                        "iquota", "ilimit", "igrace");
8447         } else {
8448                 printf("Disk quotas for %s %s (%cid %u):\n",
8449                        qtype_name(qctl->qc_type), name,
8450                        *qtype_name(qctl->qc_type), qctl->qc_id);
8451                 printf("%15s%8s %7s%8s%8s%8s %7s%8s%8s\n",
8452                        "Filesystem", human_readable ? "used" : "kbytes",
8453                        "quota", "limit", "grace",
8454                        "files", "quota", "limit", "grace");
8455         }
8456 }
8457
8458 static void kbytes2str(__u64 num, char *buf, int buflen, bool h)
8459 {
8460         if (!h) {
8461                 snprintf(buf, buflen, "%ju", (uintmax_t)num);
8462         } else {
8463                 if (num >> 40)
8464                         snprintf(buf, buflen, "%5.4gP",
8465                                  (double)num / ((__u64)1 << 40));
8466                 else if (num >> 30)
8467                         snprintf(buf, buflen, "%5.4gT",
8468                                  (double)num / (1 << 30));
8469                 else if (num >> 20)
8470                         snprintf(buf, buflen, "%5.4gG",
8471                                  (double)num / (1 << 20));
8472                 else if (num >> 10)
8473                         snprintf(buf, buflen, "%5.4gM",
8474                                  (double)num / (1 << 10));
8475                 else
8476                         snprintf(buf, buflen, "%ju%s", (uintmax_t)num, "k");
8477         }
8478 }
8479
8480 #ifdef HAVE_NATIVE_CLIENT
8481 /* In the current Lustre implementation, the grace time is either the time
8482  * or the timestamp to be used after some quota ID exceeds the soft limt,
8483  * 48 bits should be enough, its high 16 bits can be used as quota flags.
8484  */
8485 #define LQUOTA_GRACE_BITS       48
8486 #define LQUOTA_GRACE_MASK       ((1ULL << LQUOTA_GRACE_BITS) - 1)
8487 #define LQUOTA_GRACE_MAX        LQUOTA_GRACE_MASK
8488 #define LQUOTA_GRACE(t)         (t & LQUOTA_GRACE_MASK)
8489 #define LQUOTA_FLAG(t)          (t >> LQUOTA_GRACE_BITS)
8490 #define LQUOTA_GRACE_FLAG(t, f) ((__u64)t | (__u64)f << LQUOTA_GRACE_BITS)
8491 #endif
8492
8493 #define STRBUF_LEN      24
8494 static void print_quota(char *mnt, struct if_quotactl *qctl, int type,
8495                         int rc, bool h, bool show_default)
8496 {
8497         time_t now;
8498
8499         time(&now);
8500
8501         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA || qctl->qc_cmd == Q_GETOQUOTA ||
8502             qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8503             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT ||
8504             qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL) {
8505                 int bover = 0, iover = 0;
8506                 struct obd_dqblk *dqb = &qctl->qc_dqblk;
8507                 char numbuf[3][STRBUF_LEN + 2]; /* 2 for brackets or wildcard */
8508                 char timebuf[40];
8509                 char strbuf[STRBUF_LEN];
8510
8511                 dqb->dqb_btime &= LQUOTA_GRACE_MASK;
8512                 dqb->dqb_itime &= LQUOTA_GRACE_MASK;
8513
8514                 if (dqb->dqb_bhardlimit &&
8515                     lustre_stoqb(dqb->dqb_curspace) >= dqb->dqb_bhardlimit) {
8516                         bover = 1;
8517                 } else if (dqb->dqb_bsoftlimit && dqb->dqb_btime) {
8518                         if (dqb->dqb_btime > now)
8519                                 bover = 2;
8520                         else
8521                                 bover = 3;
8522                 }
8523
8524                 if (dqb->dqb_ihardlimit &&
8525                     dqb->dqb_curinodes >= dqb->dqb_ihardlimit) {
8526                         iover = 1;
8527                 } else if (dqb->dqb_isoftlimit && dqb->dqb_itime) {
8528                         if (dqb->dqb_itime > now)
8529                                 iover = 2;
8530                         else
8531                                 iover = 3;
8532                 }
8533
8534                 if (strlen(mnt) > 15)
8535                         printf("%s\n%15s", mnt, "");
8536                 else
8537                         printf("%15s", mnt);
8538
8539                 if (show_default)
8540                         snprintf(timebuf, sizeof(timebuf), "%llu",
8541                                  (unsigned long long)dqb->dqb_btime);
8542                 else if (bover)
8543                         diff2str(dqb->dqb_btime, timebuf, now);
8544
8545                 kbytes2str(lustre_stoqb(dqb->dqb_curspace),
8546                            strbuf, sizeof(strbuf), h);
8547                 if (rc == -EREMOTEIO)
8548                         sprintf(numbuf[0], "%s*", strbuf);
8549                 else
8550                         sprintf(numbuf[0], (dqb->dqb_valid & QIF_SPACE) ?
8551                                 "%s" : "[%s]", strbuf);
8552
8553                 kbytes2str(dqb->dqb_bsoftlimit, strbuf, sizeof(strbuf), h);
8554                 if (type == QC_GENERAL)
8555                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_BLIMITS) ?
8556                                 "%s" : "[%s]", strbuf);
8557                 else
8558                         sprintf(numbuf[1], "%s", "-");
8559
8560                 kbytes2str(dqb->dqb_bhardlimit, strbuf, sizeof(strbuf), h);
8561                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_BLIMITS) ?
8562                         "%s" : "[%s]", strbuf);
8563
8564                 if (show_default)
8565                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8566                 else
8567                         printf(" %7s%c %6s %7s %7s",
8568                                numbuf[0], bover ? '*' : ' ', numbuf[1],
8569                                numbuf[2], bover > 1 ? timebuf : "-");
8570
8571                 if (show_default)
8572                         snprintf(timebuf, sizeof(timebuf), "%llu",
8573                                  (unsigned long long)dqb->dqb_itime);
8574                 else if (iover)
8575                         diff2str(dqb->dqb_itime, timebuf, now);
8576
8577                 snprintf(numbuf[0], sizeof(numbuf),
8578                          (dqb->dqb_valid & QIF_INODES) ? "%ju" : "[%ju]",
8579                          (uintmax_t)dqb->dqb_curinodes);
8580
8581                 if (type == QC_GENERAL)
8582                         sprintf(numbuf[1], (dqb->dqb_valid & QIF_ILIMITS) ?
8583                                 "%ju" : "[%ju]",
8584                                 (uintmax_t)dqb->dqb_isoftlimit);
8585                 else
8586                         sprintf(numbuf[1], "%s", "-");
8587
8588                 sprintf(numbuf[2], (dqb->dqb_valid & QIF_ILIMITS) ?
8589                         "%ju" : "[%ju]", (uintmax_t)dqb->dqb_ihardlimit);
8590
8591                 if (show_default)
8592                         printf(" %6s %7s %7s", numbuf[1], numbuf[2], timebuf);
8593                 else if (type != QC_OSTIDX)
8594                         printf(" %7s%c %6s %7s %7s",
8595                                numbuf[0], iover ? '*' : ' ', numbuf[1],
8596                                numbuf[2], iover > 1 ? timebuf : "-");
8597                 else
8598                         printf(" %7s %7s %7s %7s", "-", "-", "-", "-");
8599                 printf("\n");
8600         } else if (qctl->qc_cmd == LUSTRE_Q_GETINFO || LUSTRE_Q_GETINFOPOOL ||
8601                    qctl->qc_cmd == Q_GETOINFO) {
8602                 char bgtimebuf[40];
8603                 char igtimebuf[40];
8604
8605                 if (qctl->qc_dqinfo.dqi_bgrace == NOTIFY_GRACE_TIME)
8606                         strncpy(bgtimebuf, NOTIFY_GRACE, 40);
8607                 else
8608                         sec2str(qctl->qc_dqinfo.dqi_bgrace, bgtimebuf, rc);
8609                 if (qctl->qc_dqinfo.dqi_igrace == NOTIFY_GRACE_TIME)
8610                         strncpy(igtimebuf, NOTIFY_GRACE, 40);
8611                 else
8612                         sec2str(qctl->qc_dqinfo.dqi_igrace, igtimebuf, rc);
8613
8614                 printf("Block grace time: %s; Inode grace time: %s\n",
8615                        bgtimebuf, igtimebuf);
8616         }
8617 }
8618
8619 static int tgt_name2index(const char *tgtname, unsigned int *idx)
8620 {
8621         char *dash, *endp;
8622
8623         /* format is "lustre-OST0001" */
8624         dash = memchr(tgtname, '-', LUSTRE_MAXFSNAME + 1);
8625         if (!dash) {
8626                 fprintf(stderr, "wrong tgtname format '%s'\n", tgtname);
8627                 return -EINVAL;
8628         }
8629         dash += 4;
8630
8631         *idx = strtoul(dash, &endp, 16);
8632         if (*idx > 0xffff) {
8633                 fprintf(stderr, "wrong index %s\n", tgtname);
8634                 return -ERANGE;
8635         }
8636
8637         return 0;
8638 }
8639
8640 static int print_obd_quota(char *mnt, struct if_quotactl *qctl, int is_mdt,
8641                            bool h, __u64 *total)
8642 {
8643         int rc = 0, rc1 = 0, count = 0, i = 0;
8644         char **list = NULL, *buffer = NULL;
8645         __u32 valid = qctl->qc_valid;
8646
8647         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL && is_mdt)
8648                 return 0;
8649
8650         /* Is it correct for the case OST0000, OST0002, OST0003 -
8651          * we will ask OST0001 that is absent and won't ask OST0003? */
8652         rc = llapi_get_obd_count(mnt, &count, is_mdt);
8653         if (rc) {
8654                 fprintf(stderr, "can not get %s count: %s\n",
8655                         is_mdt ? "mdt" : "ost", strerror(-rc));
8656                 return rc;
8657         }
8658
8659         if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8660                 char fname[PATH_MAX];
8661                 char fsname[LUSTRE_MAXFSNAME + 1];
8662                 int bufsize = sizeof(struct obd_uuid) * count;
8663
8664                 rc = llapi_search_fsname(mnt, fsname);
8665                 if (rc) {
8666                         fprintf(stderr, "cannot get fsname for mountpoint %s\n",
8667                                 mnt);
8668                         goto out;
8669                 }
8670                 buffer = malloc(bufsize + sizeof(*list) * count);
8671                 if (!buffer)
8672                         return -ENOMEM;
8673                 list = (char **)(buffer + bufsize);
8674                 snprintf(fname, PATH_MAX, "%s.%s", fsname, qctl->qc_poolname);
8675                 count = llapi_get_poolmembers(fname, list, count,
8676                                               buffer, bufsize);
8677                 if (count <= 0)
8678                         goto out;
8679         }
8680
8681         for (i = 0; i < count; i++) {
8682                 if (qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
8683                         unsigned int index;
8684
8685                         if (tgt_name2index(list[i], &index))
8686                                 continue;
8687                         qctl->qc_idx = index;
8688                 } else {
8689                         qctl->qc_idx = i;
8690                 }
8691
8692                 qctl->qc_valid = is_mdt ? QC_MDTIDX : QC_OSTIDX;
8693                 rc = llapi_quotactl(mnt, qctl);
8694                 if (rc) {
8695                         /* It is remote client case. */
8696                         if (rc == -EOPNOTSUPP) {
8697                                 rc = 0;
8698                                 goto out;
8699                         }
8700
8701                         /* no target for this index yet */
8702                         if (rc == -ENODEV) {
8703                                 rc = 0;
8704                                 continue;
8705                         }
8706
8707                         /* inactive target */
8708                         if (rc == -ENODATA) {
8709                                 char name[UUID_MAX+8];
8710
8711                                 snprintf(name, sizeof(name), "%s[inact]",
8712                                         obd_uuid2str(&qctl->obd_uuid));
8713                                 memset(&qctl->qc_dqinfo, 0,
8714                                        sizeof(qctl->qc_dqinfo));
8715                                 memset(&qctl->qc_dqblk, 0,
8716                                        sizeof(qctl->qc_dqblk));
8717                                 print_quota(name, qctl, qctl->qc_valid, 0, h,
8718                                             false);
8719                                 rc = 0;
8720                                 continue;
8721                         }
8722
8723                         if (!rc1)
8724                                 rc1 = rc;
8725                         fprintf(stderr, "quotactl %s%d failed.\n",
8726                                 is_mdt ? "mdt" : "ost", qctl->qc_idx);
8727                         continue;
8728                 }
8729
8730                 print_quota(obd_uuid2str(&qctl->obd_uuid), qctl,
8731                             qctl->qc_valid, 0, h, false);
8732                 *total += is_mdt ? qctl->qc_dqblk.dqb_ihardlimit :
8733                                    qctl->qc_dqblk.dqb_bhardlimit;
8734         }
8735 out:
8736         if (buffer)
8737                 free(buffer);
8738         qctl->qc_valid = valid;
8739         return rc ? : rc1;
8740 }
8741
8742 static int get_print_quota(char *mnt, char *name, struct if_quotactl *qctl,
8743                            int verbose, int quiet, bool human_readable,
8744                            bool show_default)
8745 {
8746         int rc1 = 0, rc2 = 0, rc3 = 0;
8747         char *obd_type = (char *)qctl->obd_type;
8748         char *obd_uuid = (char *)qctl->obd_uuid.uuid;
8749         __u64 total_ialloc = 0, total_balloc = 0;
8750         bool use_default_for_blk = false;
8751         bool use_default_for_file = false;
8752         int inacc;
8753
8754         rc1 = llapi_quotactl(mnt, qctl);
8755         if (rc1 < 0) {
8756                 switch (rc1) {
8757                 case -ESRCH:
8758                         fprintf(stderr, "%s quotas are not enabled.\n",
8759                                 qtype_name(qctl->qc_type));
8760                         goto out;
8761                 case -EPERM:
8762                         fprintf(stderr, "Permission denied.\n");
8763                 case -ENODEV:
8764                 case -ENOENT:
8765                         /* We already got error message. */
8766                         goto out;
8767                 default:
8768                         fprintf(stderr, "Unexpected quotactl error: %s\n",
8769                                 strerror(-rc1));
8770                 }
8771         }
8772
8773         if (!show_default && qctl->qc_id == 0) {
8774                 qctl->qc_dqblk.dqb_bhardlimit = 0;
8775                 qctl->qc_dqblk.dqb_bsoftlimit = 0;
8776                 qctl->qc_dqblk.dqb_ihardlimit = 0;
8777                 qctl->qc_dqblk.dqb_isoftlimit = 0;
8778                 qctl->qc_dqblk.dqb_btime = 0;
8779                 qctl->qc_dqblk.dqb_itime = 0;
8780                 qctl->qc_dqblk.dqb_valid |= QIF_LIMITS | QIF_TIMES;
8781         }
8782
8783         if (qctl->qc_dqblk.dqb_valid & QIF_BTIME &&
8784             LQUOTA_FLAG(qctl->qc_dqblk.dqb_btime) & LQUOTA_FLAG_DEFAULT) {
8785                 use_default_for_blk = true;
8786                 qctl->qc_dqblk.dqb_btime &= LQUOTA_GRACE_MASK;
8787         }
8788
8789         if (qctl->qc_dqblk.dqb_valid & QIF_ITIME &&
8790             LQUOTA_FLAG(qctl->qc_dqblk.dqb_itime) & LQUOTA_FLAG_DEFAULT) {
8791                 use_default_for_file = true;
8792                 qctl->qc_dqblk.dqb_itime &= LQUOTA_GRACE_MASK;
8793         }
8794
8795         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8796              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ||
8797              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT_POOL ||
8798              qctl->qc_cmd == LUSTRE_Q_GETDEFAULT) && !quiet)
8799                 print_quota_title(name, qctl, human_readable, show_default);
8800
8801         if (rc1 && *obd_type)
8802                 fprintf(stderr, "%s %s ", obd_type, obd_uuid);
8803
8804         if (qctl->qc_valid != QC_GENERAL)
8805                 mnt = "";
8806
8807         inacc = (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
8808                  qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
8809                 ((qctl->qc_dqblk.dqb_valid & (QIF_LIMITS|QIF_USAGE)) !=
8810                  (QIF_LIMITS|QIF_USAGE));
8811
8812         print_quota(mnt, qctl, QC_GENERAL, rc1, human_readable, show_default);
8813
8814         if (!show_default && verbose &&
8815             qctl->qc_valid == QC_GENERAL && qctl->qc_cmd != LUSTRE_Q_GETINFO &&
8816             qctl->qc_cmd != LUSTRE_Q_GETINFOPOOL) {
8817                 char strbuf[STRBUF_LEN];
8818
8819                 rc2 = print_obd_quota(mnt, qctl, 1, human_readable,
8820                                       &total_ialloc);
8821                 rc3 = print_obd_quota(mnt, qctl, 0, human_readable,
8822                                       &total_balloc);
8823                 kbytes2str(total_balloc, strbuf, sizeof(strbuf),
8824                            human_readable);
8825                 printf("Total allocated inode limit: %ju, total allocated block limit: %s\n",
8826                        (uintmax_t)total_ialloc, strbuf);
8827         }
8828
8829         if (use_default_for_blk)
8830                 printf("%cid %u is using default block quota setting\n",
8831                        *qtype_name(qctl->qc_type), qctl->qc_id);
8832
8833         if (use_default_for_file)
8834                 printf("%cid %u is using default file quota setting\n",
8835                        *qtype_name(qctl->qc_type), qctl->qc_id);
8836
8837         if (rc1 || rc2 || rc3 || inacc)
8838                 printf("Some errors happened when getting quota info. Some devices may be not working or deactivated. The data in \"[]\" is inaccurate.\n");
8839 out:
8840         if (rc1)
8841                 return rc1;
8842         if (rc2)
8843                 return rc2;
8844         if (rc3)
8845                 return rc3;
8846         if (inacc)
8847                 return -EIO;
8848
8849         return 0;
8850 }
8851
8852 static int lfs_project(int argc, char **argv)
8853 {
8854         int ret = 0, err = 0, c, i;
8855         struct project_handle_control phc = { 0 };
8856         enum lfs_project_ops_t op;
8857
8858         phc.newline = true;
8859         phc.assign_projid = false;
8860         /* default action */
8861         op = LFS_PROJECT_LIST;
8862
8863         while ((c = getopt(argc, argv, "p:cCsdkr0")) != -1) {
8864                 switch (c) {
8865                 case 'c':
8866                         if (op != LFS_PROJECT_LIST) {
8867                                 fprintf(stderr,
8868                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8869                                         progname);
8870                                 return CMD_HELP;
8871                         }
8872
8873                         op = LFS_PROJECT_CHECK;
8874                         break;
8875                 case 'C':
8876                         if (op != LFS_PROJECT_LIST) {
8877                                 fprintf(stderr,
8878                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8879                                         progname);
8880                                 return CMD_HELP;
8881                         }
8882
8883                         op = LFS_PROJECT_CLEAR;
8884                         break;
8885                 case 's':
8886                         if (op != LFS_PROJECT_LIST) {
8887                                 fprintf(stderr,
8888                                         "%s: cannot specify '-c' '-C' '-s' together\n",
8889                                         progname);
8890                                 return CMD_HELP;
8891                         }
8892
8893                         phc.set_inherit = true;
8894                         op = LFS_PROJECT_SET;
8895                         break;
8896                 case 'd':
8897                         phc.dironly = true;
8898                         break;
8899                 case 'k':
8900                         phc.keep_projid = true;
8901                         break;
8902                 case 'r':
8903                         phc.recursive = true;
8904                         break;
8905                 case 'p':
8906                         if (str2quotaid(&phc.projid, optarg)) {
8907                                 fprintf(stderr,
8908                                         "Invalid project ID: %s\n",
8909                                         optarg);
8910                                 return CMD_HELP;
8911                         }
8912
8913                         phc.assign_projid = true;
8914
8915                         break;
8916                 case '0':
8917                         phc.newline = false;
8918                         break;
8919                 default:
8920                         fprintf(stderr, "%s: invalid option '%c'\n",
8921                                 progname, optopt);
8922                         return CMD_HELP;
8923                 }
8924         }
8925
8926         if (phc.assign_projid && op == LFS_PROJECT_LIST) {
8927                 op = LFS_PROJECT_SET;
8928                 phc.set_projid = true;
8929         } else if (phc.assign_projid && op == LFS_PROJECT_SET) {
8930                 phc.set_projid = true;
8931         }
8932
8933         switch (op) {
8934         case LFS_PROJECT_CHECK:
8935                 if (phc.keep_projid) {
8936                         fprintf(stderr,
8937                                 "%s: '-k' is useless together with '-c'\n",
8938                                 progname);
8939                         return CMD_HELP;
8940                 }
8941                 break;
8942         case LFS_PROJECT_CLEAR:
8943                 if (!phc.newline) {
8944                         fprintf(stderr,
8945                                 "%s: '-0' is useless together with '-C'\n",
8946                                 progname);
8947                         return CMD_HELP;
8948                 }
8949                 if (phc.assign_projid) {
8950                         fprintf(stderr,
8951                                 "%s: '-p' is useless together with '-C'\n",
8952                                 progname);
8953                         return CMD_HELP;
8954                 }
8955                 break;
8956         case LFS_PROJECT_SET:
8957                 if (!phc.newline) {
8958                         fprintf(stderr,
8959                                 "%s: '-0' is useless together with '-s'\n",
8960                                 progname);
8961                         return CMD_HELP;
8962                 }
8963                 if (phc.keep_projid) {
8964                         fprintf(stderr,
8965                                 "%s: '-k' is useless together with '-s'\n",
8966                                 progname);
8967                         return CMD_HELP;
8968                 }
8969                 break;
8970         default:
8971                 if (!phc.newline) {
8972                         fprintf(stderr,
8973                                 "%s: '-0' is useless for list operations\n",
8974                                 progname);
8975                         return CMD_HELP;
8976                 }
8977                 break;
8978         }
8979
8980         argv += optind;
8981         argc -= optind;
8982         if (argc == 0) {
8983                 fprintf(stderr, "%s: missing file or directory target(s)\n",
8984                         progname);
8985                 return CMD_HELP;
8986         }
8987
8988         for (i = 0; i < argc; i++) {
8989                 switch (op) {
8990                 case LFS_PROJECT_CHECK:
8991                         err = lfs_project_check(argv[i], &phc);
8992                         break;
8993                 case LFS_PROJECT_LIST:
8994                         err = lfs_project_list(argv[i], &phc);
8995                         break;
8996                 case LFS_PROJECT_CLEAR:
8997                         err = lfs_project_clear(argv[i], &phc);
8998                         break;
8999                 case LFS_PROJECT_SET:
9000                         err = lfs_project_set(argv[i], &phc);
9001                         break;
9002                 default:
9003                         break;
9004                 }
9005                 if (err && !ret)
9006                         ret = err;
9007         }
9008
9009         return ret;
9010 }
9011
9012 static int lfs_quota(int argc, char **argv)
9013 {
9014         int c;
9015         char *mnt, *name = NULL;
9016         struct if_quotactl *qctl;
9017         char *obd_uuid;
9018         int rc = 0, rc1 = 0, verbose = 0, quiet = 0;
9019         __u32 valid = QC_GENERAL, idx = 0;
9020         bool human_readable = false;
9021         bool show_default = false;
9022         int qtype;
9023         bool show_pools = false;
9024         struct option long_opts[] = {
9025         { .val = LFS_POOL_OPT, .name = "pool", .has_arg = optional_argument },
9026         { .name = NULL } };
9027         char **poollist = NULL;
9028         char *buf = NULL;
9029         int poolcount, i;
9030
9031         qctl = calloc(1, sizeof(*qctl) + LOV_MAXPOOLNAME + 1);
9032         if (!qctl)
9033                 return -ENOMEM;
9034
9035         qctl->qc_cmd = LUSTRE_Q_GETQUOTA;
9036         qctl->qc_type = ALLQUOTA;
9037         obd_uuid = (char *)qctl->obd_uuid.uuid;
9038
9039         while ((c = getopt_long(argc, argv, "gGi:I:o:pPqtuUvh",
9040                 long_opts, NULL)) != -1) {
9041                 switch (c) {
9042                 case 'U':
9043                         show_default = true;
9044                 case 'u':
9045                         qtype = USRQUOTA;
9046                         goto quota_type;
9047                 case 'G':
9048                         show_default = true;
9049                 case 'g':
9050                         qtype = GRPQUOTA;
9051                         goto quota_type;
9052                 case 'P':
9053                         show_default = true;
9054                 case 'p':
9055                         qtype = PRJQUOTA;
9056 quota_type:
9057                         if (qctl->qc_type != ALLQUOTA) {
9058                                 fprintf(stderr,
9059                                         "%s quota: only one of -u, -g, or -p may be specified\n",
9060                                         progname);
9061                                 rc = CMD_HELP;
9062                                 goto out;
9063                         }
9064                         qctl->qc_type = qtype;
9065                         break;
9066                 case 't':
9067                         qctl->qc_cmd = LUSTRE_Q_GETINFO;
9068                         break;
9069                 case 'o':
9070                         valid = qctl->qc_valid = QC_UUID;
9071                         snprintf(obd_uuid, sizeof(*obd_uuid), "%s", optarg);
9072                         break;
9073                 case 'i':
9074                         valid = qctl->qc_valid = QC_MDTIDX;
9075                         idx = qctl->qc_idx = atoi(optarg);
9076                         if (idx == 0 && *optarg != '0') {
9077                                 fprintf(stderr,
9078                                         "%s quota: invalid MDT index '%s'\n",
9079                                         progname, optarg);
9080                                 rc = CMD_HELP;
9081                                 goto out;
9082                         }
9083                         break;
9084                 case 'I':
9085                         valid = qctl->qc_valid = QC_OSTIDX;
9086                         idx = qctl->qc_idx = atoi(optarg);
9087                         if (idx == 0 && *optarg != '0') {
9088                                 fprintf(stderr,
9089                                         "%s quota: invalid OST index '%s'\n",
9090                                         progname, optarg);
9091                                 rc = CMD_HELP;
9092                                 goto out;
9093                         }
9094                         break;
9095                 case 'v':
9096                         verbose = 1;
9097                         break;
9098                 case 'q':
9099                         quiet = 1;
9100                         break;
9101                 case 'h':
9102                         human_readable = true;
9103                         break;
9104                 case LFS_POOL_OPT:
9105                         if ((!optarg) && (argv[optind] != NULL) &&
9106                                 (argv[optind][0] != '-') &&
9107                                 (argv[optind][0] != '/')) {
9108                                 optarg = argv[optind++];
9109                                 if (lfs_verify_poolarg(optarg)) {
9110                                         rc = -EINVAL;
9111                                         goto out;
9112                                 }
9113                                 strncpy(qctl->qc_poolname, optarg,
9114                                         LOV_MAXPOOLNAME);
9115                                 if (qctl->qc_cmd == LUSTRE_Q_GETINFO)
9116                                         qctl->qc_cmd = LUSTRE_Q_GETINFOPOOL;
9117                                 else
9118                                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9119                                 break;
9120                         }
9121
9122                         /* optarg is NULL */
9123                         show_pools = true;
9124                         qctl->qc_cmd = LUSTRE_Q_GETQUOTAPOOL;
9125                         break;
9126                 default:
9127                         fprintf(stderr, "%s quota: unrecognized option '%s'\n",
9128                                 progname, argv[optind - 1]);
9129                         rc = CMD_HELP;
9130                         goto out;
9131                 }
9132         }
9133
9134         /* current uid/gid info for "lfs quota /path/to/lustre/mount" */
9135         if ((qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9136              qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) &&
9137              qctl->qc_type == ALLQUOTA &&
9138              optind == argc - 1 && !show_default) {
9139                 qctl->qc_idx = idx;
9140
9141                 for (qtype = USRQUOTA; qtype <= GRPQUOTA; qtype++) {
9142                         qctl->qc_type = qtype;
9143                         qctl->qc_valid = valid;
9144                         if (qtype == USRQUOTA) {
9145                                 qctl->qc_id = geteuid();
9146                                 rc = uid2name(&name, qctl->qc_id);
9147                         } else {
9148                                 qctl->qc_id = getegid();
9149                                 rc = gid2name(&name, qctl->qc_id);
9150                                 memset(&qctl->qc_dqblk, 0,
9151                                        sizeof(qctl->qc_dqblk));
9152                         }
9153                         if (rc)
9154                                 name = "<unknown>";
9155                         mnt = argv[optind];
9156                         rc1 = get_print_quota(mnt, name, qctl, verbose, quiet,
9157                                               human_readable, show_default);
9158                         if (rc1 && !rc)
9159                                 rc = rc1;
9160                 }
9161                 goto out;
9162         /* lfs quota -u username /path/to/lustre/mount */
9163         } else if (qctl->qc_cmd == LUSTRE_Q_GETQUOTA ||
9164                    qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL) {
9165                 /* options should be followed by u/g-name and mntpoint */
9166                 if ((!show_default && optind + 2 != argc) ||
9167                     (show_default && optind + 1 != argc) ||
9168                     qctl->qc_type == ALLQUOTA) {
9169                         fprintf(stderr,
9170                                 "%s quota: name and mount point must be specified\n",
9171                                 progname);
9172                         rc = CMD_HELP;
9173                         goto out;
9174                 }
9175
9176                 if (!show_default) {
9177                         name = argv[optind++];
9178                         switch (qctl->qc_type) {
9179                         case USRQUOTA:
9180                                 rc = name2uid(&qctl->qc_id, name);
9181                                 break;
9182                         case GRPQUOTA:
9183                                 rc = name2gid(&qctl->qc_id, name);
9184                                 break;
9185                         case PRJQUOTA:
9186                                 rc = name2projid(&qctl->qc_id, name);
9187                                 break;
9188                         default:
9189                                 rc = -ENOTSUP;
9190                                 break;
9191                         }
9192                 } else {
9193                         qctl->qc_valid = QC_GENERAL;
9194                         qctl->qc_cmd = qctl->qc_cmd == LUSTRE_Q_GETQUOTAPOOL ?
9195                                         LUSTRE_Q_GETDEFAULT_POOL :
9196                                         LUSTRE_Q_GETDEFAULT;
9197                         qctl->qc_id = 0;
9198                 }
9199
9200                 if (rc) {
9201                         if (str2quotaid(&qctl->qc_id, name)) {
9202                                 fprintf(stderr, "%s quota: invalid id '%s'\n",
9203                                         progname, name);
9204                                 rc = CMD_HELP;
9205                                 goto out;
9206                         }
9207                 }
9208         } else if (optind + 1 != argc || qctl->qc_type == ALLQUOTA) {
9209                 fprintf(stderr, "%s quota: missing quota info argument(s)\n",
9210                         progname);
9211                 rc = CMD_HELP;
9212                 goto out;
9213         }
9214
9215         mnt = argv[optind];
9216         if (show_pools) {
9217                 char *p;
9218
9219                 i = 0;
9220                 rc = llapi_get_poolbuf(mnt, &buf, &poollist, &poolcount);
9221                 if (rc)
9222                         goto out;
9223
9224                 for (i = 0; i < poolcount; i++) {
9225                         p = memchr(poollist[i], '.', MAXNAMLEN);
9226                         if (!p) {
9227                                 fprintf(stderr, "bad string format %.*s\n",
9228                                         MAXNAMLEN, poollist[i]);
9229                                 rc = -EINVAL;
9230                                 goto out;
9231                         }
9232                         p++;
9233                         printf("Quotas for pool: %s\n", p);
9234                         strncpy(qctl->qc_poolname, p, LOV_MAXPOOLNAME);
9235                         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9236                                              human_readable, show_default);
9237                         if (rc)
9238                                 break;
9239                 }
9240                 goto out;
9241         }
9242
9243         rc = get_print_quota(mnt, name, qctl, verbose, quiet,
9244                              human_readable, show_default);
9245 out:
9246         free(buf);
9247         free(qctl);
9248         return rc;
9249 }
9250 #endif /* HAVE_SYS_QUOTA_H! */
9251
9252 static int flushctx_ioctl(char *mp)
9253 {
9254         int fd, rc;
9255
9256         fd = open(mp, O_RDONLY);
9257         if (fd == -1) {
9258                 fprintf(stderr, "flushctx: error open %s: %s\n",
9259                         mp, strerror(errno));
9260                 return -1;
9261         }
9262
9263         rc = ioctl(fd, LL_IOC_FLUSHCTX);
9264         if (rc == -1)
9265                 fprintf(stderr, "flushctx: error ioctl %s: %s\n",
9266                         mp, strerror(errno));
9267
9268         close(fd);
9269         return rc;
9270 }
9271
9272 static int lfs_flushctx(int argc, char **argv)
9273 {
9274         int     kdestroy = 0, reap = 0, c;
9275         char    mntdir[PATH_MAX] = {'\0'};
9276         int     index = 0;
9277         int     rc = 0;
9278
9279         while ((c = getopt(argc, argv, "kr")) != -1) {
9280                 switch (c) {
9281                 case 'k':
9282                         kdestroy = 1;
9283                         break;
9284                 case 'r':
9285                         reap = 1;
9286                         break;
9287                 default:
9288                         fprintf(stderr,
9289                                 "error: %s: option '-%c' unrecognized\n",
9290                                 argv[0], c);
9291                         return CMD_HELP;
9292                 }
9293         }
9294
9295         if (kdestroy) {
9296                 rc = system("kdestroy > /dev/null");
9297                 if (rc) {
9298                         rc = WEXITSTATUS(rc);
9299                         fprintf(stderr,
9300                                 "error destroying tickets: %d, continuing\n",
9301                                 rc);
9302                 }
9303         }
9304
9305         if (optind >= argc) {
9306                 /* flush for all mounted lustre fs. */
9307                 while (!llapi_search_mounts(NULL, index++, mntdir, NULL)) {
9308                         /* Check if we have a mount point */
9309                         if (mntdir[0] == '\0')
9310                                 continue;
9311
9312                         if (flushctx_ioctl(mntdir))
9313                                 rc = -1;
9314
9315                         mntdir[0] = '\0'; /* avoid matching in next loop */
9316                 }
9317         } else {
9318                 /* flush fs as specified */
9319                 while (optind < argc) {
9320                         if (flushctx_ioctl(argv[optind++]))
9321                                 rc = -1;
9322                 }
9323         }
9324
9325         if (reap) {
9326                 rc = system("keyctl reap > /dev/null");
9327                 if (rc != 0) {
9328                         rc = WEXITSTATUS(rc);
9329                         fprintf(stderr, "error reaping keyring: %d\n", rc);
9330                 }
9331         }
9332
9333         return rc;
9334 }
9335
9336 static int lfs_changelog(int argc, char **argv)
9337 {
9338         void *changelog_priv;
9339         struct changelog_rec *rec;
9340         long long startrec = 0, endrec = 0;
9341         char *mdd;
9342         struct option long_opts[] = {
9343                 { .val = 'f', .name = "follow", .has_arg = no_argument },
9344                 { .name = NULL } };
9345         char short_opts[] = "f";
9346         int rc, follow = 0;
9347
9348         while ((rc = getopt_long(argc, argv, short_opts,
9349                 long_opts, NULL)) != -1) {
9350                 switch (rc) {
9351                 case 'f':
9352                         follow++;
9353                         break;
9354                 default:
9355                         fprintf(stderr,
9356                                 "%s changelog: unrecognized option '%s'\n",
9357                                 progname, argv[optind - 1]);
9358                         return CMD_HELP;
9359                 }
9360         }
9361         if (optind >= argc) {
9362                 fprintf(stderr, "%s changelog: mdtname must be specified\n",
9363                         progname);
9364                 return CMD_HELP;
9365         }
9366
9367         mdd = argv[optind++];
9368         if (argc > optind) {
9369                 errno = 0;
9370                 startrec = strtoll(argv[optind++], NULL, 10);
9371                 if (errno != 0 || startrec < 0) {
9372                         fprintf(stderr,
9373                                 "%s changelog: bad startrec\n",
9374                                 progname);
9375                         return CMD_HELP;
9376                 }
9377         }
9378
9379         if (argc > optind) {
9380                 errno = 0;
9381                 endrec = strtoll(argv[optind++], NULL, 10);
9382                 if (errno != 0 || endrec < 0) {
9383                         fprintf(stderr,
9384                                 "%s changelog: bad endrec\n",
9385                                 progname);
9386                         return CMD_HELP;
9387                 }
9388         }
9389
9390         rc = llapi_changelog_start(&changelog_priv,
9391                                    CHANGELOG_FLAG_BLOCK |
9392                                    CHANGELOG_FLAG_JOBID |
9393                                    CHANGELOG_FLAG_EXTRA_FLAGS |
9394                                    (follow ? CHANGELOG_FLAG_FOLLOW : 0),
9395                                    mdd, startrec);
9396         if (rc < 0) {
9397                 fprintf(stderr, "%s changelog: cannot start changelog: %s\n",
9398                         progname, strerror(errno = -rc));
9399                 return rc;
9400         }
9401
9402         rc = llapi_changelog_set_xflags(changelog_priv,
9403                                         CHANGELOG_EXTRA_FLAG_UIDGID |
9404                                         CHANGELOG_EXTRA_FLAG_NID |
9405                                         CHANGELOG_EXTRA_FLAG_OMODE |
9406                                         CHANGELOG_EXTRA_FLAG_XATTR);
9407         if (rc < 0) {
9408                 fprintf(stderr,
9409                         "%s changelog: cannot set xflags for changelog: %s\n",
9410                         progname, strerror(errno = -rc));
9411                 return rc;
9412         }
9413
9414         while ((rc = llapi_changelog_recv(changelog_priv, &rec)) == 0) {
9415                 time_t secs;
9416                 struct tm ts;
9417
9418                 if (endrec && rec->cr_index > endrec) {
9419                         llapi_changelog_free(&rec);
9420                         break;
9421                 }
9422                 if (rec->cr_index < startrec) {
9423                         llapi_changelog_free(&rec);
9424                         continue;
9425                 }
9426
9427                 secs = rec->cr_time >> 30;
9428                 gmtime_r(&secs, &ts);
9429                 printf("%ju %02d%-5s %02d:%02d:%02d.%09d %04d.%02d.%02d "
9430                        "0x%x t="DFID, (uintmax_t)rec->cr_index, rec->cr_type,
9431                        changelog_type2str(rec->cr_type),
9432                        ts.tm_hour, ts.tm_min, ts.tm_sec,
9433                        (int)(rec->cr_time & ((1 << 30) - 1)),
9434                        ts.tm_year + 1900, ts.tm_mon + 1, ts.tm_mday,
9435                        rec->cr_flags & CLF_FLAGMASK, PFID(&rec->cr_tfid));
9436
9437                 if (rec->cr_flags & CLF_JOBID) {
9438                         struct changelog_ext_jobid *jid =
9439                                 changelog_rec_jobid(rec);
9440
9441                         if (jid->cr_jobid[0] != '\0')
9442                                 printf(" j=%s", jid->cr_jobid);
9443                 }
9444
9445                 if (rec->cr_flags & CLF_EXTRA_FLAGS) {
9446                         struct changelog_ext_extra_flags *ef =
9447                                 changelog_rec_extra_flags(rec);
9448
9449                         printf(" ef=0x%llx",
9450                                (unsigned long long)ef->cr_extra_flags);
9451
9452                         if (ef->cr_extra_flags & CLFE_UIDGID) {
9453                                 struct changelog_ext_uidgid *uidgid =
9454                                         changelog_rec_uidgid(rec);
9455
9456                                 printf(" u=%llu:%llu",
9457                                        (unsigned long long)uidgid->cr_uid,
9458                                        (unsigned long long)uidgid->cr_gid);
9459                         }
9460                         if (ef->cr_extra_flags & CLFE_NID) {
9461                                 struct changelog_ext_nid *nid =
9462                                         changelog_rec_nid(rec);
9463
9464                                 printf(" nid=%s",
9465                                        libcfs_nid2str(nid->cr_nid));
9466                         }
9467
9468                         if (ef->cr_extra_flags & CLFE_OPEN) {
9469                                 struct changelog_ext_openmode *omd =
9470                                         changelog_rec_openmode(rec);
9471                                 char mode[] = "---";
9472
9473                                 /* exec mode must be exclusive */
9474                                 if (omd->cr_openflags & MDS_FMODE_EXEC) {
9475                                         mode[2] = 'x';
9476                                 } else {
9477                                         if (omd->cr_openflags & MDS_FMODE_READ)
9478                                                 mode[0] = 'r';
9479                                         if (omd->cr_openflags &
9480                                             (MDS_FMODE_WRITE |
9481                                              MDS_OPEN_TRUNC |
9482                                              MDS_OPEN_APPEND))
9483                                                 mode[1] = 'w';
9484                                 }
9485
9486                                 if (strcmp(mode, "---") != 0)
9487                                         printf(" m=%s", mode);
9488                         }
9489
9490                         if (ef->cr_extra_flags & CLFE_XATTR) {
9491                                 struct changelog_ext_xattr *xattr =
9492                                         changelog_rec_xattr(rec);
9493
9494                                 if (xattr->cr_xattr[0] != '\0')
9495                                         printf(" x=%s", xattr->cr_xattr);
9496                         }
9497                 }
9498
9499                 if (!fid_is_zero(&rec->cr_pfid))
9500                         printf(" p="DFID, PFID(&rec->cr_pfid));
9501                 if (rec->cr_namelen)
9502                         printf(" %.*s", rec->cr_namelen,
9503                                changelog_rec_name(rec));
9504
9505                 if (rec->cr_flags & CLF_RENAME) {
9506                         struct changelog_ext_rename *rnm =
9507                                 changelog_rec_rename(rec);
9508
9509                         if (!fid_is_zero(&rnm->cr_sfid))
9510                                 printf(" s="DFID" sp="DFID" %.*s",
9511                                        PFID(&rnm->cr_sfid),
9512                                        PFID(&rnm->cr_spfid),
9513                                        (int)changelog_rec_snamelen(rec),
9514                                        changelog_rec_sname(rec));
9515                 }
9516                 printf("\n");
9517
9518                 llapi_changelog_free(&rec);
9519         }
9520
9521         llapi_changelog_fini(&changelog_priv);
9522
9523         if (rc < 0)
9524                 fprintf(stderr, "%s changelog: cannot access changelog: %s\n",
9525                         progname, strerror(errno = -rc));
9526
9527         return (rc == 1 ? 0 : rc);
9528 }
9529
9530 static int lfs_changelog_clear(int argc, char **argv)
9531 {
9532         long long endrec;
9533         int rc;
9534
9535         if (argc != 4)
9536                 return CMD_HELP;
9537
9538         errno = 0;
9539         endrec = strtoll(argv[3], NULL, 10);
9540         if (errno != 0 || endrec < 0) {
9541                 fprintf(stderr,
9542                         "%s: bad endrec '%s'\n",
9543                         argv[0], argv[3]);
9544                 return CMD_HELP;
9545         }
9546
9547         rc = llapi_changelog_clear(argv[1], argv[2], endrec);
9548
9549         if (rc == -EINVAL)
9550                 fprintf(stderr, "%s: record out of range: %llu\n",
9551                         argv[0], endrec);
9552         else if (rc == -ENOENT)
9553                 fprintf(stderr, "%s: no changelog user: %s\n",
9554                         argv[0], argv[2]);
9555         else if (rc)
9556                 fprintf(stderr, "%s error: %s\n", argv[0],
9557                         strerror(-rc));
9558
9559         if (rc)
9560                 errno = -rc;
9561
9562         return rc;
9563 }
9564
9565 static void rstripc(char *str, int c)
9566 {
9567         char *end = str + strlen(str);
9568
9569         for (; str < end && end[-1] == c; --end)
9570                 end[-1] = '\0';
9571 }
9572
9573 static int lfs_fid2path(int argc, char **argv)
9574 {
9575         struct option long_opts[] = {
9576                 { .val = 'c',   .name = "cur",  .has_arg = no_argument },
9577                 { .val = 'c',   .name = "current",      .has_arg = no_argument },
9578                 { .val = 'c',   .name = "print-link",   .has_arg = no_argument },
9579                 { .val = 'f',   .name = "print-fid",    .has_arg = no_argument },
9580                 { .val = 'l',   .name = "link", .has_arg = required_argument },
9581                 { .name = NULL } };
9582         char short_opts[] = "cfl:pr:";
9583         bool print_link = false;
9584         bool print_fid = false;
9585         bool print_mnt_dir;
9586         char mnt_dir[PATH_MAX] = "";
9587         int mnt_fd = -1;
9588         char *path_or_fsname;
9589         long long recno = -1;
9590         int linkno = -1;
9591         char *endptr = NULL;
9592         int rc = 0;
9593         int c;
9594         int i;
9595
9596         while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
9597                 switch (c) {
9598                 case 'c':
9599                         print_link = true;
9600                         break;
9601                 case 'f':
9602                         print_fid = true;
9603                         break;
9604                 case 'l':
9605                         errno = 0;
9606                         linkno = strtol(optarg, &endptr, 10);
9607                         if (errno != 0 || *endptr != '\0' || linkno < 0) {
9608                                 fprintf(stderr,
9609                                         "%s fid2path: invalid linkno '%s'\n",
9610                                         progname, optarg);
9611                                 return CMD_HELP;
9612                         }
9613                         break;
9614                 case 'r':
9615                         /* recno is something to do with changelogs
9616                          * that was never implemented. We just pass it
9617                          * through for the MDT to ignore.
9618                          */
9619                         errno = 0;
9620                         recno = strtoll(optarg, &endptr, 10);
9621                         if (errno != 0 || *endptr != '\0' || recno < 0) {
9622                                 fprintf(stderr,
9623                                         "%s fid2path: invalid recno '%s'\n",
9624                                         progname, optarg);
9625                                 return CMD_HELP;
9626                         }
9627                         break;
9628                 default:
9629                         fprintf(stderr,
9630                                 "%s fid2path: unrecognized option '%s'\n",
9631                                 progname, argv[optind - 1]);
9632                         return CMD_HELP;
9633                 }
9634         }
9635
9636         if (argc - optind < 2) {
9637                 fprintf(stderr,
9638                         "Usage: %s fid2path FSNAME|ROOT FID...\n",
9639                         progname);
9640                 return CMD_HELP;
9641         }
9642
9643         path_or_fsname = argv[optind];
9644
9645         if (*path_or_fsname == '/') {
9646                 print_mnt_dir = true;
9647                 rc = llapi_search_mounts(path_or_fsname, 0, mnt_dir, NULL);
9648         } else {
9649                 print_mnt_dir = false;
9650                 rc = llapi_search_rootpath(mnt_dir, path_or_fsname);
9651         }
9652
9653         if (rc < 0) {
9654                 fprintf(stderr,
9655                         "%s fid2path: cannot resolve mount point for '%s': %s\n",
9656                         progname, path_or_fsname, strerror(-rc));
9657                 goto out;
9658         }
9659
9660         mnt_fd = open(mnt_dir, O_RDONLY | O_DIRECTORY);
9661         if (mnt_fd < 0) {
9662                 fprintf(stderr,
9663                         "%s fid2path: cannot open mount point for '%s': %s\n",
9664                         progname, path_or_fsname, strerror(-rc));
9665                 goto out;
9666         }
9667
9668         /* Strip trailing slashes from mnt_dir. */
9669         rstripc(mnt_dir + 1, '/');
9670
9671         for (i = optind + 1; i < argc; i++) {
9672                 const char *fid_str = argv[i];
9673                 struct lu_fid fid;
9674                 int rc2;
9675
9676                 rc2 = llapi_fid_parse(fid_str, &fid, NULL);
9677                 if (rc2 < 0) {
9678                         fprintf(stderr,
9679                                 "%s fid2path: invalid FID '%s'\n",
9680                                 progname, fid_str);
9681                         if (rc == 0)
9682                                 rc = rc2;
9683
9684                         continue;
9685                 }
9686
9687                 int linktmp = (linkno >= 0) ? linkno : 0;
9688                 while (1) {
9689                         int oldtmp = linktmp;
9690                         long long rectmp = recno;
9691                         char path_buf[PATH_MAX];
9692
9693                         rc2 = llapi_fid2path_at(mnt_fd, &fid,
9694                                 path_buf, sizeof(path_buf), &rectmp, &linktmp);
9695                         if (rc2 < 0) {
9696                                 fprintf(stderr,
9697                                         "%s fid2path: cannot find %s %s: %s\n",
9698                                         progname, path_or_fsname, fid_str,
9699                                         strerror(-rc2));
9700                                 if (rc == 0)
9701                                         rc = rc2;
9702                                 break;
9703                         }
9704
9705                         if (print_fid)
9706                                 printf("%s ", fid_str);
9707
9708                         if (print_link)
9709                                 printf("%d ", linktmp);
9710
9711                         /* You may think this looks wrong or weird (and it is!)
9712                          * but we are actually trying to preserve the old quirky
9713                          * behaviors (enforced by our old quirky tests!) that
9714                          * make lfs so much fun to work on:
9715                          *
9716                          *   lustre 0x200000007:0x1:0x0 => "/"
9717                          *   /mnt/lustre 0x200000007:0x1:0x0 => "/mnt/lustre//"
9718                          *
9719                          * Note that llapi_fid2path() returns "" for the root
9720                          * FID. */
9721
9722                         printf("%s%s%s\n",
9723                                print_mnt_dir ? mnt_dir : "",
9724                                (print_mnt_dir || *path_buf == '\0') ? "/" : "",
9725                                path_buf);
9726
9727                         if (linkno >= 0)
9728                                 /* specified linkno */
9729                                 break;
9730
9731                         if (oldtmp == linktmp)
9732                                 /* no more links */
9733                                 break;
9734                 }
9735         }
9736 out:
9737         if (!(mnt_fd < 0))
9738                 close(mnt_fd);
9739
9740         return rc;
9741 }
9742
9743 static int lfs_path2fid(int argc, char **argv)
9744 {
9745         struct option long_opts[] = {
9746                 { .val = 'p', .name = "parents", .has_arg = no_argument },
9747                 { .name = NULL } };
9748         char            **path;
9749         const char        short_opts[] = "p";
9750         const char       *sep = "";
9751         struct lu_fid     fid;
9752         int               rc = 0;
9753         bool              show_parents = false;
9754
9755         while ((rc = getopt_long(argc, argv, short_opts,
9756                                  long_opts, NULL)) != -1) {
9757                 switch (rc) {
9758                 case 'p':
9759                         show_parents = true;
9760                         break;
9761                 default:
9762                         fprintf(stderr,
9763                                 "%s path2fid: unrecognized option '%s'\n",
9764                                 progname, argv[optind - 1]);
9765                         return CMD_HELP;
9766                 }
9767         }
9768
9769         if (optind > argc - 1) {
9770                 fprintf(stderr, "%s path2fid: FILE... must be specified\n",
9771                         progname);
9772                 return CMD_HELP;
9773         } else if (optind < argc - 1) {
9774                 sep = ": ";
9775         }
9776
9777         rc = 0;
9778         for (path = argv + optind; *path != NULL; path++) {
9779                 int err = 0;
9780
9781                 if (!show_parents) {
9782                         err = llapi_path2fid(*path, &fid);
9783                         if (!err)
9784                                 printf("%s%s"DFID"\n",
9785                                        *sep != '\0' ? *path : "", sep,
9786                                        PFID(&fid));
9787                 } else {
9788                         char            name[NAME_MAX + 1];
9789                         unsigned int    linkno = 0;
9790
9791                         while ((err = llapi_path2parent(*path, linkno, &fid,
9792                                                 name, sizeof(name))) == 0) {
9793                                 if (*sep != '\0' && linkno == 0)
9794                                         printf("%s%s", *path, sep);
9795
9796                                 printf("%s"DFID"/%s", linkno != 0 ? "\t" : "",
9797                                        PFID(&fid), name);
9798                                 linkno++;
9799                         }
9800
9801                         /* err == -ENODATA is end-of-loop */
9802                         if (linkno > 0 && err == -ENODATA) {
9803                                 printf("\n");
9804                                 err = 0;
9805                         }
9806                 }
9807
9808                 if (err) {
9809                         fprintf(stderr,
9810                                 "%s path2fid: cannot get %sfid for '%s': %s\n",
9811                                 progname, show_parents ? "parent " : "", *path,
9812                                 strerror(-err));
9813                         if (rc == 0) {
9814                                 rc = err;
9815                                 errno = -err;
9816                         }
9817                 }
9818         }
9819
9820         return rc;
9821 }
9822
9823 #define MAX_ERRNO       4095
9824 #define IS_ERR_VALUE(x) ((unsigned long)(x) >= (unsigned long)-MAX_ERRNO)
9825
9826 static int lfs_rmfid_and_show_errors(const char *device, struct fid_array *fa)
9827 {
9828         int rc, rc2, k;
9829
9830         rc = llapi_rmfid(device, fa);
9831         if (rc < 0) {
9832                 fprintf(stderr, "%s rmfid: cannot remove FIDs: %s\n",
9833                         progname, strerror(-rc));
9834                 return rc;
9835         }
9836
9837         for (k = 0; k < fa->fa_nr; k++) {
9838                 rc2 = (__s32)fa->fa_fids[k].f_ver;
9839                 if (!IS_ERR_VALUE(rc2))
9840                         continue;
9841
9842                 if (rc == 0)
9843                         rc = rc2;
9844
9845                 fa->fa_fids[k].f_ver = 0;
9846                 fprintf(stderr, "%s rmfid: cannot remove "DFID": %s\n",
9847                         progname, PFID(&fa->fa_fids[k]), strerror(-rc2));
9848         }
9849
9850         return rc;
9851 }
9852
9853 static int lfs_rmfid(int argc, char **argv)
9854 {
9855         char *fidstr, *device;
9856         int rc = 0, rc2, nr;
9857         struct fid_array *fa;
9858
9859         if (optind > argc - 1) {
9860                 fprintf(stderr, "%s rmfid: missing dirname\n", progname);
9861                 return CMD_HELP;
9862         }
9863
9864         device = argv[optind++];
9865
9866         nr = argc - optind;
9867         fa = malloc(offsetof(struct fid_array, fa_fids[nr + 1]));
9868         if (!fa)
9869                 return -ENOMEM;
9870
9871         fa->fa_nr = 0;
9872         rc = 0;
9873         while (optind < argc) {
9874                 int found;
9875
9876                 fidstr = argv[optind++];
9877                 while (*fidstr == '[')
9878                         fidstr++;
9879                 found = sscanf(fidstr, SFID, RFID(&fa->fa_fids[fa->fa_nr]));
9880                 if (found != 3) {
9881                         fprintf(stderr, "unrecognized FID: %s\n",
9882                                 argv[optind - 1]);
9883                         exit(1);
9884                 }
9885                 fa->fa_nr++;
9886                 if (fa->fa_nr == OBD_MAX_FIDS_IN_ARRAY) {
9887                         /* start another batch */
9888                         rc2 = lfs_rmfid_and_show_errors(device, fa);
9889                         if (rc2 && !rc)
9890                                 rc = rc2;
9891                         fa->fa_nr = 0;
9892                 }
9893         }
9894         if (fa->fa_nr) {
9895                 rc2 = lfs_rmfid_and_show_errors(device, fa);
9896                 if (rc2 && !rc)
9897                         rc = rc2;
9898         }
9899
9900         return rc;
9901 }
9902
9903 static int lfs_data_version(int argc, char **argv)
9904 {
9905         int data_version_flags = LL_DV_RD_FLUSH; /* Read by default */
9906         __u64 data_version;
9907         char *path;
9908         int fd;
9909         int rc;
9910         int c;
9911
9912         if (argc < 2) {
9913                 fprintf(stderr, "%s: FILE must be specified\n",
9914                         progname);
9915                 return CMD_HELP;
9916         }
9917
9918         while ((c = getopt(argc, argv, "hnrw")) != -1) {
9919                 switch (c) {
9920                 case 'n':
9921                         data_version_flags = 0;
9922                         break;
9923                 case 'r':
9924                         data_version_flags |= LL_DV_RD_FLUSH;
9925                         break;
9926                 case 'w':
9927                         data_version_flags |= LL_DV_WR_FLUSH;
9928                         break;
9929                 default:
9930                         fprintf(stderr,
9931                                 "%s data_version: unrecognized option '%s'\n",
9932                                 progname, argv[optind - 1]);
9933                         fallthrough;
9934                 case 'h':
9935                         return CMD_HELP;
9936                 }
9937         }
9938         if (optind == argc) {
9939                 fprintf(stderr, "%s data_version: FILE must be specified\n",
9940                         progname);
9941                 return CMD_HELP;
9942         }
9943
9944         path = argv[optind];
9945         fd = open(path, O_RDONLY);
9946         if (fd < 0) {
9947                 rc = -errno;
9948                 fprintf(stderr, "%s data_version: cannot open file '%s': %s\n",
9949                         progname, path, strerror(-rc));
9950                 return rc;
9951         }
9952
9953         rc = llapi_get_data_version(fd, &data_version, data_version_flags);
9954         if (rc < 0)
9955                 fprintf(stderr,
9956                         "%s data_version: cannot get version for '%s': %s\n",
9957                         progname, path, strerror(-rc));
9958         else
9959                 printf("%ju" "\n", (uintmax_t)data_version);
9960
9961         close(fd);
9962         return rc;
9963 }
9964
9965 static int lfs_hsm_state(int argc, char **argv)
9966 {
9967         int rc;
9968         int i = 1;
9969         char *path;
9970         struct hsm_user_state hus;
9971
9972         if (argc < 2)
9973                 return CMD_HELP;
9974
9975         do {
9976                 path = argv[i];
9977
9978                 rc = llapi_hsm_state_get(path, &hus);
9979                 if (rc) {
9980                         fprintf(stderr, "can't get hsm state for %s: %s\n",
9981                                 path, strerror(errno = -rc));
9982                         return rc;
9983                 }
9984
9985                 /* Display path name and status flags */
9986                 printf("%s: (0x%08x)", path, hus.hus_states);
9987
9988                 if (hus.hus_states & HS_RELEASED)
9989                         printf(" released");
9990                 if (hus.hus_states & HS_EXISTS)
9991                         printf(" exists");
9992                 if (hus.hus_states & HS_DIRTY)
9993                         printf(" dirty");
9994                 if (hus.hus_states & HS_ARCHIVED)
9995                         printf(" archived");
9996                 /* Display user-settable flags */
9997                 if (hus.hus_states & HS_NORELEASE)
9998                         printf(" never_release");
9999                 if (hus.hus_states & HS_NOARCHIVE)
10000                         printf(" never_archive");
10001                 if (hus.hus_states & HS_LOST)
10002                         printf(" lost_from_hsm");
10003
10004                 if (hus.hus_archive_id != 0)
10005                         printf(", archive_id:%d", hus.hus_archive_id);
10006                 printf("\n");
10007
10008         } while (++i < argc);
10009
10010         return 0;
10011 }
10012
10013 #define LFS_HSM_SET   0
10014 #define LFS_HSM_CLEAR 1
10015
10016 /**
10017  * Generic function to set or clear HSM flags.
10018  * Used by hsm_set and hsm_clear.
10019  *
10020  * @mode  if LFS_HSM_SET, set the flags, if LFS_HSM_CLEAR, clear the flags.
10021  */
10022 static int lfs_hsm_change_flags(int argc, char **argv, int mode)
10023 {
10024         struct option long_opts[] = {
10025         { .val = 'A',   .name = "archived",     .has_arg = no_argument },
10026         { .val = 'a',   .name = "noarchive",    .has_arg = no_argument },
10027         { .val = 'd',   .name = "dirty",        .has_arg = no_argument },
10028         { .val = 'e',   .name = "exists",       .has_arg = no_argument },
10029         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10030         { .val = 'i',   .name = "archive-id",   .has_arg = required_argument },
10031         { .val = 'l',   .name = "lost",         .has_arg = no_argument },
10032         { .val = 'r',   .name = "norelease",    .has_arg = no_argument },
10033         { .name = NULL } };
10034         __u64 mask = 0;
10035         int c, rc;
10036         char *path;
10037         __u32 archive_id = 0;
10038         char *end = NULL;
10039
10040         if (argc < 3)
10041                 return CMD_HELP;
10042
10043         while ((c = getopt_long(argc, argv, "aAdehi:lr",
10044                                 long_opts, NULL)) != -1) {
10045                 switch (c) {
10046                 case 'l':
10047                         mask |= HS_LOST;
10048                         break;
10049                 case 'a':
10050                         mask |= HS_NOARCHIVE;
10051                         break;
10052                 case 'A':
10053                         mask |= HS_ARCHIVED;
10054                         break;
10055                 case 'r':
10056                         mask |= HS_NORELEASE;
10057                         break;
10058                 case 'd':
10059                         mask |= HS_DIRTY;
10060                         break;
10061                 case 'e':
10062                         mask |= HS_EXISTS;
10063                         break;
10064                 case 'i':
10065                         errno = 0;
10066                         archive_id = strtol(optarg, &end, 10);
10067                         if (errno != 0 || *end != '\0' || archive_id < 0) {
10068                                 fprintf(stderr,
10069                                         "%s: invalid archive_id: '%s'\n",
10070                                         progname, end);
10071                                 return CMD_HELP;
10072                         }
10073                         break;
10074                 default:
10075                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10076                                 progname, argv[optind - 1]);
10077                         fallthrough;
10078                 case 'h':
10079                         return CMD_HELP;
10080                 }
10081         }
10082
10083         /* User should have specified a flag */
10084         if (mask == 0)
10085                 return CMD_HELP;
10086
10087         while (optind < argc) {
10088                 path = argv[optind];
10089
10090                 /* If mode == 0, this means we apply the mask. */
10091                 if (mode == LFS_HSM_SET)
10092                         rc = llapi_hsm_state_set(path, mask, 0, archive_id);
10093                 else
10094                         rc = llapi_hsm_state_set(path, 0, mask, 0);
10095
10096                 if (rc != 0) {
10097                         fprintf(stderr, "Can't change hsm flags for %s: %s\n",
10098                                 path, strerror(errno = -rc));
10099                         return rc;
10100                 }
10101                 optind++;
10102         }
10103
10104         return 0;
10105 }
10106
10107 static int lfs_hsm_action(int argc, char **argv)
10108 {
10109         int                              rc;
10110         int                              i = 1;
10111         char                            *path;
10112         struct hsm_current_action        hca;
10113         struct hsm_extent                he;
10114         enum hsm_user_action             hua;
10115         enum hsm_progress_states         hps;
10116
10117         if (argc < 2)
10118                 return CMD_HELP;
10119
10120         do {
10121                 path = argv[i];
10122
10123                 rc = llapi_hsm_current_action(path, &hca);
10124                 if (rc) {
10125                         fprintf(stderr, "can't get hsm action for %s: %s\n",
10126                                 path, strerror(errno = -rc));
10127                         return rc;
10128                 }
10129                 he = hca.hca_location;
10130                 hua = hca.hca_action;
10131                 hps = hca.hca_state;
10132
10133                 printf("%s: %s", path, hsm_user_action2name(hua));
10134
10135                 /* Skip file without action */
10136                 if (hca.hca_action == HUA_NONE) {
10137                         printf("\n");
10138                         continue;
10139                 }
10140
10141                 printf(" %s ", hsm_progress_state2name(hps));
10142
10143                 if ((hps == HPS_RUNNING) &&
10144                     (hua == HUA_ARCHIVE || hua == HUA_RESTORE))
10145                         printf("(%llu bytes moved)\n",
10146                                (unsigned long long)he.length);
10147                 else if ((he.offset + he.length) == LUSTRE_EOF)
10148                         printf("(from %llu to EOF)\n",
10149                                (unsigned long long)he.offset);
10150                 else
10151                         printf("(from %llu to %llu)\n",
10152                                (unsigned long long)he.offset,
10153                                (unsigned long long)(he.offset + he.length));
10154
10155         } while (++i < argc);
10156
10157         return 0;
10158 }
10159
10160 static int lfs_hsm_set(int argc, char **argv)
10161 {
10162         return lfs_hsm_change_flags(argc, argv, LFS_HSM_SET);
10163 }
10164
10165 static int lfs_hsm_clear(int argc, char **argv)
10166 {
10167         return lfs_hsm_change_flags(argc, argv, LFS_HSM_CLEAR);
10168 }
10169
10170 /**
10171  * Check file state and return its fid, to be used by lfs_hsm_request().
10172  *
10173  * \param[in]     file      Path to file to check
10174  * \param[in,out] fid       Pointer to allocated lu_fid struct.
10175  * \param[in,out] last_dev  Pointer to last device id used.
10176  *
10177  * \return 0 on success.
10178  */
10179 static int lfs_hsm_prepare_file(const char *file, struct lu_fid *fid,
10180                                 dev_t *last_dev)
10181 {
10182         struct stat     st;
10183         int             rc;
10184
10185         rc = lstat(file, &st);
10186         if (rc) {
10187                 fprintf(stderr, "Cannot stat %s: %s\n", file, strerror(errno));
10188                 return -errno;
10189         }
10190         /*
10191          * Checking for regular file as archiving as posix copytool
10192          * rejects archiving files other than regular files
10193          */
10194         if (!S_ISREG(st.st_mode)) {
10195                 fprintf(stderr, "error: \"%s\" is not a regular file\n", file);
10196                 return CMD_HELP;
10197         }
10198         /* A request should be ... */
10199         if (*last_dev != st.st_dev && *last_dev != 0) {
10200                 fprintf(stderr,
10201                         "All files should be on the same filesystem: %s\n",
10202                         file);
10203                 return -EINVAL;
10204         }
10205         *last_dev = st.st_dev;
10206
10207         rc = llapi_path2fid(file, fid);
10208         if (rc) {
10209                 fprintf(stderr, "Cannot read FID of %s: %s\n",
10210                         file, strerror(-rc));
10211                 return rc;
10212         }
10213         return 0;
10214 }
10215
10216 /* Fill an HSM HUR item with a given file name.
10217  *
10218  * If mntpath is set, then the filename is actually a FID, and no
10219  * lookup on the filesystem will be performed.
10220  *
10221  * \param[in]  hur         the user request to fill
10222  * \param[in]  idx         index of the item inside the HUR to fill
10223  * \param[in]  mntpath     mountpoint of Lustre
10224  * \param[in]  fname       filename (if mtnpath is NULL)
10225  *                         or FID (if mntpath is set)
10226  * \param[in]  last_dev    pointer to last device id used
10227  *
10228  * \retval 0 on success
10229  * \retval CMD_HELP or a negative errno on error
10230  */
10231 static int fill_hur_item(struct hsm_user_request *hur, unsigned int idx,
10232                          const char *mntpath, const char *fname,
10233                          dev_t *last_dev)
10234 {
10235         struct hsm_user_item *hui = &hur->hur_user_item[idx];
10236         int rc;
10237
10238         hui->hui_extent.length = -1;
10239
10240         if (mntpath) {
10241                 rc = llapi_fid_parse(fname, &hui->hui_fid, NULL);
10242                 if (rc)
10243                         fprintf(stderr, "hsm: '%s' is not a valid FID\n",
10244                                 fname);
10245         } else {
10246                 rc = lfs_hsm_prepare_file(fname, &hui->hui_fid, last_dev);
10247         }
10248
10249         if (rc == 0)
10250                 hur->hur_request.hr_itemcount++;
10251
10252         return rc;
10253 }
10254
10255 static int lfs_hsm_request(int argc, char **argv, int action)
10256 {
10257         struct option long_opts[] = {
10258         { .val = 'a',   .name = "archive",      .has_arg = required_argument },
10259         { .val = 'D',   .name = "data",         .has_arg = required_argument },
10260         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10261         { .val = 'l',   .name = "filelist",     .has_arg = required_argument },
10262         { .val = 'm',   .name = "mntpath",      .has_arg = required_argument },
10263         { .name = NULL } };
10264         dev_t last_dev = 0;
10265         struct hsm_user_request *hur, *oldhur;
10266         int c, i;
10267         size_t len;
10268         int nbfile;
10269         char *line = NULL;
10270         char *filelist = NULL;
10271         char fullpath[PATH_MAX];
10272         char *opaque = NULL;
10273         int opaque_len = 0;
10274         int archive_id = 0;
10275         FILE *fp;
10276         int nbfile_alloc = 0;
10277         char *some_file = NULL;
10278         char *mntpath = NULL;
10279         int rc;
10280
10281         if (argc < 2)
10282                 return CMD_HELP;
10283
10284         while ((c = getopt_long(argc, argv, "a:D:hl:m:",
10285                                 long_opts, NULL)) != -1) {
10286                 switch (c) {
10287                 case 'l':
10288                         filelist = optarg;
10289                         break;
10290                 case 'D':
10291                         opaque = optarg;
10292                         break;
10293                 case 'a':
10294                         if (action != HUA_ARCHIVE &&
10295                             action != HUA_REMOVE) {
10296                                 fprintf(stderr,
10297                                         "error: -a is supported only when archiving or removing\n");
10298                                 return CMD_HELP;
10299                         }
10300                         archive_id = atoi(optarg);
10301                         break;
10302                 case 'm':
10303                         if (!some_file) {
10304                                 mntpath = optarg;
10305                                 some_file = strdup(optarg);
10306                         }
10307                         break;
10308                 default:
10309                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10310                                 progname, argv[optind - 1]);
10311                         fallthrough;
10312                 case 'h':
10313                         return CMD_HELP;
10314                 }
10315         }
10316
10317         /* All remaining args are files, so we have at least nbfile */
10318         nbfile = argc - optind;
10319
10320         if ((nbfile == 0) && (!filelist))
10321                 return CMD_HELP;
10322
10323         if (opaque)
10324                 opaque_len = strlen(opaque);
10325
10326         /*
10327          * Alloc the request structure with enough place to store all files
10328          * from command line.
10329          */
10330         hur = llapi_hsm_user_request_alloc(nbfile, opaque_len);
10331         if (!hur) {
10332                 fprintf(stderr, "Cannot create the request: %s\n",
10333                         strerror(errno));
10334                 return errno;
10335         }
10336         nbfile_alloc = nbfile;
10337
10338         hur->hur_request.hr_action = action;
10339         hur->hur_request.hr_archive_id = archive_id;
10340         hur->hur_request.hr_flags = 0;
10341
10342         /* All remaining args are files, add them */
10343         if (nbfile != 0 && some_file == NULL)
10344                 some_file = strdup(argv[optind]);
10345
10346         for (i = 0; i < nbfile; i++) {
10347                 rc = fill_hur_item(hur, i, mntpath, argv[optind + i],
10348                                    &last_dev);
10349                 if (rc)
10350                         goto out_free;
10351         }
10352
10353         /* from here stop using nb_file, use hur->hur_request.hr_itemcount */
10354
10355         /* If a filelist was specified, read the filelist from it. */
10356         if (filelist) {
10357                 fp = fopen(filelist, "r");
10358                 if (!fp) {
10359                         fprintf(stderr, "Cannot read the file list %s: %s\n",
10360                                 filelist, strerror(errno));
10361                         rc = -errno;
10362                         goto out_free;
10363                 }
10364
10365                 while ((rc = getline(&line, &len, fp)) != -1) {
10366                         /*
10367                          * If allocated buffer was too small, get something
10368                          * larger
10369                          */
10370                         if (nbfile_alloc <= hur->hur_request.hr_itemcount) {
10371                                 ssize_t size;
10372
10373                                 nbfile_alloc = nbfile_alloc * 2 + 1;
10374                                 oldhur = hur;
10375                                 hur = llapi_hsm_user_request_alloc(nbfile_alloc,
10376                                                                    opaque_len);
10377                                 if (!hur) {
10378                                         fprintf(stderr,
10379                                                 "hsm: cannot allocate the request: %s\n",
10380                                                 strerror(errno));
10381                                         hur = oldhur;
10382                                         rc = -errno;
10383                                         fclose(fp);
10384                                         goto out_free;
10385                                 }
10386                                 size = hur_len(oldhur);
10387                                 if (size < 0) {
10388                                         fprintf(stderr,
10389                                                 "hsm: cannot allocate %u files + %u bytes data\n",
10390                                                 oldhur->hur_request.hr_itemcount,
10391                                                 oldhur->hur_request.hr_data_len);
10392                                         free(hur);
10393                                         hur = oldhur;
10394                                         rc = -E2BIG;
10395                                         fclose(fp);
10396                                         goto out_free;
10397                                 }
10398                                 memcpy(hur, oldhur, size);
10399                                 free(oldhur);
10400                         }
10401
10402                         /* Chop CR */
10403                         if (line[strlen(line) - 1] == '\n')
10404                                 line[strlen(line) - 1] = '\0';
10405
10406                         rc = fill_hur_item(hur, hur->hur_request.hr_itemcount,
10407                                            mntpath, line, &last_dev);
10408                         if (rc) {
10409                                 fclose(fp);
10410                                 goto out_free;
10411                         }
10412
10413                         if (!some_file) {
10414                                 some_file = line;
10415                                 line = NULL;
10416                         }
10417                 }
10418
10419                 rc = fclose(fp);
10420                 free(line);
10421         }
10422
10423         /* If a --data was used, add it to the request */
10424         hur->hur_request.hr_data_len = opaque_len;
10425         if (opaque)
10426                 memcpy(hur_data(hur), opaque, opaque_len);
10427
10428         /* Send the HSM request */
10429         if (realpath(some_file, fullpath) == NULL) {
10430                 fprintf(stderr, "Could not find path '%s': %s\n",
10431                         some_file, strerror(errno));
10432         }
10433         rc = llapi_hsm_request(fullpath, hur);
10434         if (rc) {
10435                 fprintf(stderr, "Cannot send HSM request (use of %s): %s\n",
10436                         some_file, strerror(-rc));
10437                 goto out_free;
10438         }
10439
10440 out_free:
10441         free(some_file);
10442         free(hur);
10443         return rc;
10444 }
10445
10446 static int lfs_hsm_archive(int argc, char **argv)
10447 {
10448         return lfs_hsm_request(argc, argv, HUA_ARCHIVE);
10449 }
10450
10451 static int lfs_hsm_restore(int argc, char **argv)
10452 {
10453         return lfs_hsm_request(argc, argv, HUA_RESTORE);
10454 }
10455
10456 static int lfs_hsm_release(int argc, char **argv)
10457 {
10458         return lfs_hsm_request(argc, argv, HUA_RELEASE);
10459 }
10460
10461 static int lfs_hsm_remove(int argc, char **argv)
10462 {
10463         return lfs_hsm_request(argc, argv, HUA_REMOVE);
10464 }
10465
10466 static int lfs_hsm_cancel(int argc, char **argv)
10467 {
10468         return lfs_hsm_request(argc, argv, HUA_CANCEL);
10469 }
10470
10471 static int lfs_swap_layouts(int argc, char **argv)
10472 {
10473         if (argc != 3)
10474                 return CMD_HELP;
10475
10476         return llapi_swap_layouts(argv[1], argv[2], 0, 0,
10477                                   SWAP_LAYOUTS_KEEP_MTIME |
10478                                   SWAP_LAYOUTS_KEEP_ATIME);
10479 }
10480
10481 static const char *const ladvise_names[] = LU_LADVISE_NAMES;
10482
10483 static const char *const lock_mode_names[] = LOCK_MODE_NAMES;
10484
10485 int lfs_get_mode(const char *string)
10486 {
10487         enum lock_mode_user mode;
10488
10489         for (mode = 0; mode < ARRAY_SIZE(lock_mode_names); mode++) {
10490                 if (lock_mode_names[mode] == NULL)
10491                         continue;
10492                 if (strcasecmp(string, lock_mode_names[mode]) == 0)
10493                         return mode;
10494         }
10495
10496         return -EINVAL;
10497 }
10498
10499 static enum lu_ladvise_type lfs_get_ladvice(const char *string)
10500 {
10501         enum lu_ladvise_type advice;
10502
10503         for (advice = 0;
10504              advice < ARRAY_SIZE(ladvise_names); advice++) {
10505                 if (ladvise_names[advice] == NULL)
10506                         continue;
10507                 if (strcmp(string, ladvise_names[advice]) == 0)
10508                         return advice;
10509         }
10510
10511         return LU_LADVISE_INVALID;
10512 }
10513
10514 static int lfs_ladvise(int argc, char **argv)
10515 {
10516         struct option long_opts[] = {
10517         { .val = 'a',   .name = "advice",       .has_arg = required_argument },
10518         { .val = 'b',   .name = "background",   .has_arg = no_argument },
10519         { .val = 'e',   .name = "end",          .has_arg = required_argument },
10520         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10521         { .val = 'l',   .name = "length",       .has_arg = required_argument },
10522         { .val = 'm',   .name = "mode",         .has_arg = required_argument },
10523         { .val = 's',   .name = "start",        .has_arg = required_argument },
10524         { .val = 'u',   .name = "unset",        .has_arg = no_argument },
10525         { .name = NULL } };
10526         struct llapi_lu_ladvise advice;
10527         enum lu_ladvise_type advice_type = LU_LADVISE_INVALID;
10528         unsigned long long start = 0;
10529         unsigned long long end = LUSTRE_EOF;
10530         unsigned long long length = 0;
10531         unsigned long long size_units;
10532         unsigned long long flags = 0;
10533         int c, fd, rc = 0;
10534         const char *path;
10535         int mode = 0;
10536
10537         optind = 0;
10538         while ((c = getopt_long(argc, argv, "a:be:hl:m:s:u",
10539                                 long_opts, NULL)) != -1) {
10540                 switch (c) {
10541                 case 'a':
10542                         advice_type = lfs_get_ladvice(optarg);
10543                         if (advice_type == LU_LADVISE_INVALID) {
10544                                 fprintf(stderr,
10545                                         "%s: invalid advice type '%s'\n",
10546                                         progname, optarg);
10547                                 fprintf(stderr, "Valid types:");
10548
10549                                 for (advice_type = 0;
10550                                      advice_type < ARRAY_SIZE(ladvise_names);
10551                                      advice_type++) {
10552                                         if (ladvise_names[advice_type] == NULL)
10553                                                 continue;
10554                                         fprintf(stderr, " %s",
10555                                                 ladvise_names[advice_type]);
10556                                 }
10557                                 fprintf(stderr, "\n");
10558
10559                                 return CMD_HELP;
10560                         }
10561                         break;
10562                 case 'b':
10563                         flags |= LF_ASYNC;
10564                         break;
10565                 case 'u':
10566                         flags |= LF_UNSET;
10567                         break;
10568                 case 'e':
10569                         size_units = 1;
10570                         rc = llapi_parse_size(optarg, &end,
10571                                               &size_units, 0);
10572                         if (rc) {
10573                                 fprintf(stderr, "%s: bad end offset '%s'\n",
10574                                         argv[0], optarg);
10575                                 return CMD_HELP;
10576                         }
10577                         break;
10578                 case 's':
10579                         size_units = 1;
10580                         rc = llapi_parse_size(optarg, &start,
10581                                               &size_units, 0);
10582                         if (rc) {
10583                                 fprintf(stderr,
10584                                         "%s: bad start offset '%s'\n",
10585                                         argv[0], optarg);
10586                                 return CMD_HELP;
10587                         }
10588                         break;
10589                 case 'l':
10590                         size_units = 1;
10591                         rc = llapi_parse_size(optarg, &length,
10592                                               &size_units, 0);
10593                         if (rc) {
10594                                 fprintf(stderr, "%s: bad length '%s'\n",
10595                                         argv[0], optarg);
10596                                 return CMD_HELP;
10597                         }
10598                         break;
10599                 case 'm':
10600                         mode = lfs_get_mode(optarg);
10601                         if (mode < 0) {
10602                                 fprintf(stderr,
10603                                         "%s: bad mode '%s', valid modes are READ or WRITE\n",
10604                                         argv[0], optarg);
10605                                 return CMD_HELP;
10606                         }
10607                         break;
10608                 default:
10609                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10610                                 progname, argv[optind - 1]);
10611                         fallthrough;
10612                 case 'h':
10613                         return CMD_HELP;
10614                 }
10615         }
10616
10617         if (advice_type == LU_LADVISE_INVALID) {
10618                 fprintf(stderr, "%s: please give an advice type\n", argv[0]);
10619                 fprintf(stderr, "Valid types:");
10620                 for (advice_type = 0; advice_type < ARRAY_SIZE(ladvise_names);
10621                      advice_type++) {
10622                         if (ladvise_names[advice_type] == NULL)
10623                                 continue;
10624                         fprintf(stderr, " %s", ladvise_names[advice_type]);
10625                 }
10626                 fprintf(stderr, "\n");
10627                 return CMD_HELP;
10628         }
10629
10630         if (advice_type == LU_LADVISE_LOCKNOEXPAND) {
10631                 fprintf(stderr,
10632                         "%s: Lock no expand advice is a per file descriptor advice, so when called from lfs, it does nothing.\n",
10633                         argv[0]);
10634                 return CMD_HELP;
10635         }
10636
10637         if (argc <= optind) {
10638                 fprintf(stderr, "%s: please give one or more file names\n",
10639                         argv[0]);
10640                 return CMD_HELP;
10641         }
10642
10643         if (end != LUSTRE_EOF && length != 0 && end != start + length) {
10644                 fprintf(stderr, "%s: conflicting arguments of -l and -e\n",
10645                         argv[0]);
10646                 return CMD_HELP;
10647         }
10648
10649         if (end == LUSTRE_EOF && length != 0)
10650                 end = start + length;
10651
10652         if (end <= start) {
10653                 fprintf(stderr, "%s: range [%llu, %llu] is invalid\n",
10654                         argv[0], start, end);
10655                 return CMD_HELP;
10656         }
10657
10658         if (advice_type != LU_LADVISE_LOCKAHEAD && mode != 0) {
10659                 fprintf(stderr, "%s: mode is only valid with lockahead\n",
10660                         argv[0]);
10661                 return CMD_HELP;
10662         }
10663
10664         if (advice_type == LU_LADVISE_LOCKAHEAD && mode == 0) {
10665                 fprintf(stderr, "%s: mode is required with lockahead\n",
10666                         argv[0]);
10667                 return CMD_HELP;
10668         }
10669
10670         while (optind < argc) {
10671                 int rc2;
10672
10673                 path = argv[optind++];
10674
10675                 fd = open(path, O_RDONLY);
10676                 if (fd < 0) {
10677                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10678                                 argv[0], path, strerror(errno));
10679                         rc2 = -errno;
10680                         goto next;
10681                 }
10682
10683                 advice.lla_start = start;
10684                 advice.lla_end = end;
10685                 advice.lla_advice = advice_type;
10686                 advice.lla_value1 = 0;
10687                 advice.lla_value2 = 0;
10688                 advice.lla_value3 = 0;
10689                 advice.lla_value4 = 0;
10690                 if (advice_type == LU_LADVISE_LOCKAHEAD) {
10691                         advice.lla_lockahead_mode = mode;
10692                         advice.lla_peradvice_flags = flags;
10693                 }
10694
10695                 rc2 = llapi_ladvise(fd, flags, 1, &advice);
10696                 close(fd);
10697                 if (rc2 < 0) {
10698                         fprintf(stderr,
10699                                 "%s: cannot give advice '%s' to file '%s': %s\n",
10700                                 argv[0], ladvise_names[advice_type],
10701                                 path, strerror(errno));
10702
10703                         goto next;
10704                 }
10705
10706 next:
10707                 if (rc == 0 && rc2 < 0)
10708                         rc = rc2;
10709         }
10710         return rc;
10711 }
10712
10713 static const char *const heat_names[] = LU_HEAT_NAMES;
10714
10715 static int lfs_heat_get(int argc, char **argv)
10716 {
10717         struct lu_heat *heat;
10718         int rc = 0, rc2;
10719         char *path;
10720         int fd;
10721         int i;
10722
10723         if (argc <= 1)
10724                 return CMD_HELP;
10725
10726         heat = calloc(sizeof(*heat) + sizeof(__u64) * OBD_HEAT_COUNT, 1);
10727         if (!heat) {
10728                 fprintf(stderr, "%s: memory allocation failed\n", argv[0]);
10729                 return -ENOMEM;
10730         }
10731
10732         optind = 1;
10733         while (optind < argc) {
10734                 path = argv[optind++];
10735
10736                 fd = open(path, O_RDONLY);
10737                 if (fd < 0) {
10738                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10739                                 argv[0], path, strerror(errno));
10740                         rc2 = -errno;
10741                         goto next;
10742                 }
10743
10744                 heat->lh_count = OBD_HEAT_COUNT;
10745                 rc2 = llapi_heat_get(fd, heat);
10746                 close(fd);
10747                 if (rc2 < 0) {
10748                         fprintf(stderr,
10749                                 "%s: cannot get heat of file '%s': %s\n",
10750                                 argv[0], path, strerror(errno));
10751                         goto next;
10752                 }
10753
10754                 printf("flags: %x\n", heat->lh_flags);
10755                 for (i = 0; i < heat->lh_count; i++)
10756                         printf("%s: %llu\n", heat_names[i],
10757                                (unsigned long long)heat->lh_heat[i]);
10758 next:
10759                 if (rc == 0 && rc2 < 0)
10760                         rc = rc2;
10761         }
10762
10763         free(heat);
10764         return rc;
10765 }
10766
10767 static int lfs_heat_set(int argc, char **argv)
10768 {
10769         struct option long_opts[] = {
10770         { .val = 'c',   .name = "clear",        .has_arg = no_argument },
10771         { .val = 'h',   .name = "help",         .has_arg = no_argument },
10772         { .val = 'o',   .name = "off",          .has_arg = no_argument },
10773         { .val = 'O',   .name = "on",           .has_arg = no_argument },
10774         { .name = NULL } };
10775         enum lu_heat_flag flags = 0;
10776         int rc = 0, rc2;
10777         char *path;
10778         int fd;
10779         int c;
10780
10781         if (argc <= 1)
10782                 return CMD_HELP;
10783
10784         optind = 0;
10785         while ((c = getopt_long(argc, argv, "choO", long_opts, NULL)) != -1) {
10786                 switch (c) {
10787                 case 'c':
10788                         flags |= LU_HEAT_FLAG_CLEAR;
10789                         break;
10790                 case 'o':
10791                         flags |= LU_HEAT_FLAG_CLEAR;
10792                         flags |= LU_HEAT_FLAG_OFF;
10793                         break;
10794                 case 'O':
10795                         flags &= ~LU_HEAT_FLAG_OFF;
10796                         break;
10797                 default:
10798                         fprintf(stderr, "%s: unrecognized option '%s'\n",
10799                                 progname, argv[optind - 1]);
10800                         fallthrough;
10801                 case 'h':
10802                         return CMD_HELP;
10803                 }
10804         }
10805
10806         if (argc <= optind) {
10807                 fprintf(stderr, "%s: please give one or more file names\n",
10808                         argv[0]);
10809                 return CMD_HELP;
10810         }
10811
10812         while (optind < argc) {
10813                 path = argv[optind++];
10814
10815                 fd = open(path, O_RDONLY);
10816                 if (fd < 0) {
10817                         fprintf(stderr, "%s: cannot open file '%s': %s\n",
10818                                 argv[0], path, strerror(errno));
10819                         rc2 = -errno;
10820                         goto next;
10821                 }
10822
10823                 rc2 = llapi_heat_set(fd, flags);
10824                 close(fd);
10825                 if (rc2 < 0) {
10826                         fprintf(stderr,
10827                                 "%s: cannot setflags heat of file '%s': %s\n",
10828                                 argv[0], path, strerror(errno));
10829                         goto next;
10830                 }
10831 next:
10832                 if (rc == 0 && rc2 < 0)
10833                         rc = rc2;
10834         }
10835         return rc;
10836 }
10837
10838 /**
10839  * The input string contains a comma delimited list of component ids and
10840  * ranges, for example "1,2-4,7".
10841  */
10842 static int parse_mirror_ids(__u16 *ids, int size, char *arg)
10843 {
10844         bool end_of_loop = false;
10845         char *ptr = NULL;
10846         int nr = 0;
10847         int rc;
10848
10849         if (!arg)
10850                 return -EINVAL;
10851
10852         while (!end_of_loop) {
10853                 int start_index;
10854                 int end_index;
10855                 int i;
10856                 char *endptr = NULL;
10857
10858                 rc = -EINVAL;
10859                 ptr = strchrnul(arg, ',');
10860                 end_of_loop = *ptr == '\0';
10861                 *ptr = '\0';
10862
10863                 start_index = strtol(arg, &endptr, 0);
10864                 if (endptr == arg) /* no data at all */
10865                         break;
10866                 if (*endptr != '-' && *endptr != '\0') /* has invalid data */
10867                         break;
10868                 if (start_index < 0)
10869                         break;
10870
10871                 end_index = start_index;
10872                 if (*endptr == '-') {
10873                         end_index = strtol(endptr + 1, &endptr, 0);
10874                         if (*endptr != '\0')
10875                                 break;
10876                         if (end_index < start_index)
10877                                 break;
10878                 }
10879
10880                 for (i = start_index; i <= end_index && size > 0; i++) {
10881                         int j;
10882
10883                         /* remove duplicate */
10884                         for (j = 0; j < nr; j++) {
10885                                 if (ids[j] == i)
10886                                         break;
10887                         }
10888                         if (j == nr) { /* no duplicate */
10889                                 ids[nr++] = i;
10890                                 --size;
10891                         }
10892                 }
10893
10894                 if (size == 0 && i < end_index)
10895                         break;
10896
10897                 *ptr = ',';
10898                 arg = ++ptr;
10899                 rc = 0;
10900         }
10901         if (!end_of_loop && ptr)
10902                 *ptr = ',';
10903
10904         return rc < 0 ? rc : nr;
10905 }
10906
10907 /**
10908  * struct verify_mirror_id - Mirror id to be verified.
10909  * @mirror_id:   A specified mirror id.
10910  * @is_valid_id: @mirror_id is valid or not in the mirrored file.
10911  */
10912 struct verify_mirror_id {
10913         __u16 mirror_id;
10914         bool is_valid_id;
10915 };
10916
10917 /**
10918  * compare_mirror_ids() - Compare mirror ids.
10919  * @layout: Mirror component list.
10920  * @cbdata: Callback data in verify_mirror_id structure.
10921  *
10922  * This is a callback function called by llapi_layout_comp_iterate()
10923  * to compare the specified mirror id with the one in the current
10924  * component of @layout. If they are the same, then the specified
10925  * mirror id is valid.
10926  *
10927  * Return: a negative error code on failure or
10928  *         LLAPI_LAYOUT_ITER_CONT: Proceed iteration
10929  *         LLAPI_LAYOUT_ITER_STOP: Stop iteration
10930  */
10931 static inline
10932 int compare_mirror_ids(struct llapi_layout *layout, void *cbdata)
10933 {
10934         struct verify_mirror_id *mirror_id_cbdata =
10935                                  (struct verify_mirror_id *)cbdata;
10936         uint32_t mirror_id;
10937         int rc = 0;
10938
10939         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
10940         if (rc < 0) {
10941                 rc = -errno;
10942                 fprintf(stderr,
10943                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
10944                         progname, strerror(errno));
10945                 return rc;
10946         }
10947
10948         if (mirror_id_cbdata->mirror_id == mirror_id) {
10949                 mirror_id_cbdata->is_valid_id = true;
10950                 return LLAPI_LAYOUT_ITER_STOP;
10951         }
10952
10953         return LLAPI_LAYOUT_ITER_CONT;
10954 }
10955
10956 /**
10957  * verify_mirror_ids() - Verify specified mirror ids.
10958  * @fname:      Mirrored file name.
10959  * @mirror_ids: Specified mirror ids to be verified.
10960  * @ids_nr:     Number of specified mirror ids.
10961  *
10962  * This function verifies that specified @mirror_ids are valid
10963  * in the mirrored file @fname.
10964  *
10965  * Return: 0 on success or a negative error code on failure.
10966  */
10967 static inline
10968 int verify_mirror_ids(const char *fname, __u16 *mirror_ids, int ids_nr)
10969 {
10970         struct llapi_layout *layout = NULL;
10971         struct verify_mirror_id mirror_id_cbdata = { 0 };
10972         struct stat stbuf;
10973         uint32_t flr_state;
10974         int i;
10975         int fd;
10976         int rc = 0;
10977         int rc2 = 0;
10978
10979         if (ids_nr <= 0)
10980                 return -EINVAL;
10981
10982         if (stat(fname, &stbuf) < 0) {
10983                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
10984                         progname, fname, strerror(errno));
10985                 rc = -errno;
10986                 goto error;
10987         }
10988
10989         if (!S_ISREG(stbuf.st_mode)) {
10990                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
10991                         progname, fname);
10992                 rc = -EINVAL;
10993                 goto error;
10994         }
10995
10996         fd = open(fname, O_DIRECT | O_RDONLY);
10997         if (fd < 0) {
10998                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
10999                         progname, fname, strerror(errno));
11000                 rc = -errno;
11001                 goto error;
11002         }
11003
11004         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
11005         if (rc < 0) {
11006                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
11007                         progname, fname, strerror(errno));
11008                 goto close_fd;
11009         }
11010
11011         layout = llapi_layout_get_by_fd(fd, 0);
11012         if (!layout) {
11013                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11014                         progname, fname, strerror(errno));
11015                 rc = -errno;
11016                 llapi_lease_release(fd);
11017                 goto close_fd;
11018         }
11019
11020         rc = llapi_layout_flags_get(layout, &flr_state);
11021         if (rc < 0) {
11022                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11023                         progname, fname, strerror(errno));
11024                 rc = -errno;
11025                 goto free_layout;
11026         }
11027
11028         flr_state &= LCM_FL_FLR_MASK;
11029         switch (flr_state) {
11030         case LCM_FL_NONE:
11031                 rc = -EINVAL;
11032                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
11033                         progname, fname, llapi_layout_flags_string(flr_state));
11034                 goto free_layout;
11035         default:
11036                 break;
11037         }
11038
11039         rc2 = 0;
11040         for (i = 0; i < ids_nr; i++) {
11041                 mirror_id_cbdata.mirror_id = mirror_ids[i];
11042                 mirror_id_cbdata.is_valid_id = false;
11043
11044                 rc = llapi_layout_comp_iterate(layout, compare_mirror_ids,
11045                                                &mirror_id_cbdata);
11046                 if (rc < 0) {
11047                         rc = -errno;
11048                         fprintf(stderr,
11049                                 "%s: '%s' failed to verify mirror id: %u.\n",
11050                                 progname, fname, mirror_ids[i]);
11051                         goto free_layout;
11052                 }
11053
11054                 if (!mirror_id_cbdata.is_valid_id) {
11055                         rc2 = -EINVAL;
11056                         fprintf(stderr,
11057                                 "%s: '%s' invalid specified mirror id: %u.\n",
11058                                 progname, fname, mirror_ids[i]);
11059                 }
11060         }
11061         rc = rc2;
11062
11063 free_layout:
11064         llapi_layout_free(layout);
11065         llapi_lease_release(fd);
11066 close_fd:
11067         close(fd);
11068 error:
11069         return rc;
11070 }
11071
11072 static inline
11073 int lfs_mirror_resync_file(const char *fname, struct ll_ioc_lease *ioc,
11074                            __u16 *mirror_ids, int ids_nr)
11075 {
11076         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11077         struct llapi_layout *layout;
11078         struct stat stbuf;
11079         uint32_t flr_state;
11080         uint64_t start;
11081         uint64_t end;
11082         int comp_size = 0;
11083         int idx;
11084         int fd;
11085         int rc;
11086         int rc2;
11087
11088         if (stat(fname, &stbuf) < 0) {
11089                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
11090                         progname, fname, strerror(errno));
11091                 rc = -errno;
11092                 goto error;
11093         }
11094         if (!S_ISREG(stbuf.st_mode)) {
11095                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
11096                         progname, fname);
11097                 rc = -EINVAL;
11098                 goto error;
11099         }
11100
11101         /* Allow mirror resync even without the key on encrypted files */
11102         fd = open(fname, O_DIRECT | O_RDWR | O_FILE_ENC);
11103         if (fd < 0) {
11104                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
11105                         progname, fname, strerror(errno));
11106                 rc = -errno;
11107                 goto error;
11108         }
11109
11110         layout = llapi_layout_get_by_fd(fd, 0);
11111         if (!layout) {
11112                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
11113                         progname, fname, strerror(errno));
11114                 rc = -errno;
11115                 goto close_fd;
11116         }
11117
11118         rc = llapi_layout_flags_get(layout, &flr_state);
11119         if (rc) {
11120                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
11121                         progname, fname, strerror(errno));
11122                 rc = -errno;
11123                 goto free_layout;
11124         }
11125
11126         flr_state &= LCM_FL_FLR_MASK;
11127         if (flr_state == LCM_FL_NONE) {
11128                 rc = -EINVAL;
11129                 fprintf(stderr, "%s: '%s' is not a FLR file.\n",
11130                         progname, fname);
11131                 goto free_layout;
11132         }
11133
11134         /* get stale component info */
11135         comp_size = llapi_mirror_find_stale(layout, comp_array,
11136                                             ARRAY_SIZE(comp_array),
11137                                             mirror_ids, ids_nr);
11138         if (comp_size <= 0) {
11139                 rc = comp_size;
11140                 goto free_layout;
11141         }
11142
11143         ioc->lil_mode = LL_LEASE_WRLCK;
11144         ioc->lil_flags = LL_LEASE_RESYNC;
11145         rc = llapi_lease_set(fd, ioc);
11146         if (rc < 0) {
11147                 if (rc == -EALREADY)
11148                         rc = 0;
11149                 else
11150                         fprintf(stderr,
11151                             "%s: '%s' llapi_lease_get_ext resync failed: %s.\n",
11152                                 progname, fname, strerror(-rc));
11153                 goto free_layout;
11154         }
11155
11156         /* get the read range [start, end) */
11157         start = comp_array[0].lrc_start;
11158         end = comp_array[0].lrc_end;
11159         for (idx = 1; idx < comp_size; idx++) {
11160                 if (comp_array[idx].lrc_start < start)
11161                         start = comp_array[idx].lrc_start;
11162                 if (end < comp_array[idx].lrc_end)
11163                         end = comp_array[idx].lrc_end;
11164         }
11165
11166         rc = llapi_lease_check(fd);
11167         if (rc != LL_LEASE_WRLCK) {
11168                 fprintf(stderr, "%s: '%s' lost lease lock.\n",
11169                         progname, fname);
11170                 goto free_layout;
11171         }
11172
11173         rc = llapi_mirror_resync_many(fd, layout, comp_array, comp_size,
11174                                       start, end);
11175         if (rc < 0)
11176                 fprintf(stderr, "%s: '%s' llapi_mirror_resync_many: %s.\n",
11177                         progname, fname, strerror(-rc));
11178
11179         rc = migrate_set_timestamps(fd, &stbuf);
11180         if (rc < 0) {
11181                 fprintf(stderr, "%s: '%s' cannot set timestamps: %s\n",
11182                         progname, fname, strerror(-rc));
11183                 goto free_layout;
11184         }
11185
11186         /* need to do the lease unlock even resync fails */
11187         ioc->lil_mode = LL_LEASE_UNLCK;
11188         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11189         ioc->lil_count = 0;
11190         for (idx = 0; idx < comp_size; idx++) {
11191                 if (comp_array[idx].lrc_synced) {
11192                         ioc->lil_ids[ioc->lil_count] = comp_array[idx].lrc_id;
11193                         ioc->lil_count++;
11194                 }
11195         }
11196
11197         rc2 = llapi_lease_set(fd, ioc);
11198         /**
11199          * llapi_lease_set returns lease mode when it request to unlock
11200          * the lease lock.
11201          */
11202         if (rc2 <= 0) {
11203                 /* rc2 == 0 means lost lease lock */
11204                 if (rc2 == 0 && rc == 0)
11205                         rc = -EBUSY;
11206                 else
11207                         rc = rc2;
11208                 fprintf(stderr, "%s: resync file '%s' failed: %s.\n",
11209                         progname, fname,
11210                         rc2 == 0 ? "lost lease lock" : strerror(-rc2));
11211
11212                 llapi_lease_release(fd);
11213                 goto free_layout;
11214         }
11215
11216 free_layout:
11217         llapi_layout_free(layout);
11218 close_fd:
11219         close(fd);
11220 error:
11221         return rc;
11222 }
11223
11224 static inline int lfs_mirror_resync(int argc, char **argv)
11225 {
11226         struct option long_opts[] = {
11227         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11228         { .val = 'o',   .name = "only",         .has_arg = required_argument },
11229         { .name = NULL } };
11230         struct ll_ioc_lease *ioc = NULL;
11231         __u16 mirror_ids[128] = { 0 };
11232         int ids_nr = 0;
11233         int c;
11234         int rc = 0;
11235
11236         while ((c = getopt_long(argc, argv, "ho:", long_opts, NULL)) >= 0) {
11237                 switch (c) {
11238                 case 'o':
11239                         rc = parse_mirror_ids(mirror_ids,
11240                                         sizeof(mirror_ids) / sizeof(__u16),
11241                                         optarg);
11242                         if (rc < 0) {
11243                                 fprintf(stderr,
11244                                         "%s: bad mirror ids '%s'.\n",
11245                                         argv[0], optarg);
11246                                 goto error;
11247                         }
11248                         ids_nr = rc;
11249                         break;
11250                 default:
11251                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11252                                 progname, argv[optind - 1]);
11253                         fallthrough;
11254                 case 'h':
11255                         rc = CMD_HELP;
11256                         goto error;
11257                 }
11258         }
11259
11260         if (argc == optind) {
11261                 fprintf(stderr, "%s: no file name given.\n", argv[0]);
11262                 rc = CMD_HELP;
11263                 goto error;
11264         }
11265
11266         if (ids_nr > 0 && argc > optind + 1) {
11267                 fprintf(stderr,
11268                     "%s: option '--only' cannot be used upon multiple files.\n",
11269                         argv[0]);
11270                 rc = CMD_HELP;
11271                 goto error;
11272         }
11273
11274         if (ids_nr > 0) {
11275                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
11276                 if (rc < 0)
11277                         goto error;
11278         }
11279
11280         /* set the lease on the file */
11281         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11282         if (!ioc) {
11283                 fprintf(stderr, "%s: cannot alloc id array for ioc: %s.\n",
11284                         argv[0], strerror(errno));
11285                 rc = -errno;
11286                 goto error;
11287         }
11288
11289         for (; optind < argc; optind++) {
11290                 rc = lfs_mirror_resync_file(argv[optind], ioc,
11291                                             mirror_ids, ids_nr);
11292                 /* ignore previous file's error, continue with next file */
11293
11294                 /* reset ioc */
11295                 memset(ioc, 0, sizeof(*ioc) + sizeof(__u32) * 4096);
11296         }
11297
11298         free(ioc);
11299 error:
11300         return rc;
11301 }
11302
11303 static inline int verify_mirror_id_by_fd(int fd, __u16 mirror_id)
11304 {
11305         struct llapi_layout *layout;
11306         int rc;
11307
11308         layout = llapi_layout_get_by_fd(fd, 0);
11309         if (!layout) {
11310                 fprintf(stderr, "could not get layout.\n");
11311                 return  -EINVAL;
11312         }
11313
11314         rc = llapi_layout_comp_iterate(layout, find_mirror_id, &mirror_id);
11315         if (rc < 0) {
11316                 fprintf(stderr, "failed to iterate layout\n");
11317                 llapi_layout_free(layout);
11318
11319                 return rc;
11320         } else if (rc == LLAPI_LAYOUT_ITER_CONT) {
11321                 fprintf(stderr, "does not find mirror with ID %u\n", mirror_id);
11322                 llapi_layout_free(layout);
11323
11324                 return -EINVAL;
11325         }
11326         llapi_layout_free(layout);
11327
11328         return 0;
11329 }
11330
11331 /**
11332  * Check whether two files are the same file
11333  * \retval      0  same file
11334  * \retval      1  not the same file
11335  * \retval      <0 error code
11336  */
11337 static inline int check_same_file(int fd, const char *f2)
11338 {
11339         struct stat stbuf1;
11340         struct stat stbuf2;
11341
11342         if (fstat(fd, &stbuf1) < 0)
11343                 return -errno;
11344
11345         if (stat(f2, &stbuf2) < 0)
11346                 return 1;
11347
11348         if (stbuf1.st_rdev == stbuf2.st_rdev &&
11349             stbuf1.st_ino == stbuf2.st_ino)
11350                 return 0;
11351
11352         return 1;
11353 }
11354
11355 static inline int lfs_mirror_read(int argc, char **argv)
11356 {
11357         int rc = CMD_HELP;
11358         __u16 mirror_id = 0;
11359         const char *outfile = NULL;
11360         char *fname;
11361         int fd = 0;
11362         int outfd;
11363         int c;
11364         void *buf;
11365         const size_t buflen = 4 << 20;
11366         off_t pos;
11367         struct option long_opts[] = {
11368         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11369         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
11370         { .val = 'o',   .name = "outfile",      .has_arg = required_argument },
11371         { .name = NULL } };
11372
11373         while ((c = getopt_long(argc, argv, "hN:o:", long_opts, NULL)) >= 0) {
11374                 char *end;
11375
11376                 switch (c) {
11377                 case 'N': {
11378                         unsigned long int id;
11379
11380                         errno = 0;
11381                         id = strtoul(optarg, &end, 0);
11382                         if (errno != 0 || *end != '\0' || id == 0 ||
11383                             id > UINT16_MAX) {
11384                                 fprintf(stderr,
11385                                         "%s %s: invalid mirror ID '%s'\n",
11386                                         progname, argv[0], optarg);
11387                                 return rc;
11388                         }
11389
11390                         mirror_id = (__u16)id;
11391                         break;
11392                 }
11393                 case 'o':
11394                         outfile = optarg;
11395                         break;
11396                 default:
11397                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11398                                 progname, argv[optind - 1]);
11399                         fallthrough;
11400                 case 'h':
11401                         return CMD_HELP;
11402                 }
11403         }
11404
11405         if (argc == optind) {
11406                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11407                         progname, argv[0]);
11408                 return rc;
11409         } else if (argc > optind + 1) {
11410                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11411                 return rc;
11412         }
11413
11414         if (mirror_id == 0) {
11415                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11416                         progname, argv[0]);
11417                 return rc;
11418         }
11419
11420         /* open mirror file */
11421         fname = argv[optind];
11422         fd = open(fname, O_DIRECT | O_RDONLY);
11423         if (fd < 0) {
11424                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11425                         progname, argv[0], fname, strerror(errno));
11426                 return rc;
11427         }
11428
11429         /* verify mirror id */
11430         rc = verify_mirror_id_by_fd(fd, mirror_id);
11431         if (rc) {
11432                 fprintf(stderr,
11433                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11434                         progname, argv[0], mirror_id, fname);
11435                 goto close_fd;
11436         }
11437
11438         /* open output file - O_EXCL ensures output is not the same as input */
11439         if (outfile) {
11440                 outfd = open(outfile, O_EXCL | O_WRONLY | O_CREAT, 0644);
11441                 if (outfd < 0) {
11442                         fprintf(stderr, "%s %s: cannot create file '%s': %s\n",
11443                                 progname, argv[0], outfile, strerror(errno));
11444                         rc = -errno;
11445                         goto close_fd;
11446                 }
11447         } else {
11448                 outfd = STDOUT_FILENO;
11449         }
11450
11451         /* allocate buffer */
11452         rc = posix_memalign(&buf, sysconf(_SC_PAGESIZE), buflen);
11453         if (rc) {
11454                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11455                                 progname, argv[0], rc);
11456                 goto close_outfd;
11457         }
11458
11459         pos = 0;
11460         while (1) {
11461                 ssize_t bytes_read;
11462                 ssize_t written = 0;
11463
11464                 bytes_read = llapi_mirror_read(fd, mirror_id, buf, buflen, pos);
11465                 if (bytes_read < 0) {
11466                         rc = bytes_read;
11467                         fprintf(stderr,
11468                                 "%s %s: fail to read data from mirror %u: %s\n",
11469                                 progname, argv[0], mirror_id, strerror(-rc));
11470                         goto free_buf;
11471                 }
11472
11473                 /* EOF reached */
11474                 if (bytes_read == 0)
11475                         break;
11476
11477                 while (written < bytes_read) {
11478                         ssize_t written2;
11479
11480                         written2 = write(outfd, buf + written,
11481                                          bytes_read - written);
11482                         if (written2 < 0) {
11483                                 fprintf(stderr,
11484                                         "%s %s: fail to write %s: %s\n",
11485                                         progname, argv[0], outfile ? : "STDOUT",
11486                                         strerror(errno));
11487                                 rc = -errno;
11488                                 goto free_buf;
11489                         }
11490                         written += written2;
11491                 }
11492
11493                 if (written != bytes_read) {
11494                         fprintf(stderr,
11495                 "%s %s: written %ld bytes does not match with %ld read.\n",
11496                                 progname, argv[0], written, bytes_read);
11497                         rc = -EIO;
11498                         goto free_buf;
11499                 }
11500
11501                 pos += bytes_read;
11502         }
11503
11504         fsync(outfd);
11505         rc = 0;
11506
11507 free_buf:
11508         free(buf);
11509 close_outfd:
11510         if (outfile)
11511                 close(outfd);
11512 close_fd:
11513         close(fd);
11514
11515         return rc;
11516 }
11517
11518 static inline int lfs_mirror_write(int argc, char **argv)
11519 {
11520         int rc = CMD_HELP;
11521         __u16 mirror_id = 0;
11522         const char *inputfile = NULL;
11523         char *fname;
11524         int fd = 0;
11525         int inputfd;
11526         int c;
11527         void *buf;
11528         const size_t buflen = 4 << 20;
11529         off_t pos;
11530         size_t page_size = sysconf(_SC_PAGESIZE);
11531         struct ll_ioc_lease_id ioc;
11532         struct option long_opts[] = {
11533         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11534         { .val = 'i',   .name = "inputfile",    .has_arg = required_argument },
11535         { .val = 'N',   .name = "mirror-id",    .has_arg = required_argument },
11536         { .name = NULL } };
11537
11538         while ((c = getopt_long(argc, argv, "hi:N:", long_opts, NULL)) >= 0) {
11539                 char *end;
11540
11541                 switch (c) {
11542                 case 'N': {
11543                         unsigned long int id;
11544
11545                         errno = 0;
11546                         id = strtoul(optarg, &end, 0);
11547                         if (errno != 0 || *end != '\0' || id == 0 ||
11548                             id > UINT16_MAX) {
11549                                 fprintf(stderr,
11550                                         "%s %s: invalid mirror ID '%s'\n",
11551                                         progname, argv[0], optarg);
11552                                 return rc;
11553                         }
11554
11555                         mirror_id = (__u16)id;
11556                         break;
11557                 }
11558                 case 'i':
11559                         inputfile = optarg;
11560                         break;
11561                 default:
11562                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11563                                 progname, argv[optind - 1]);
11564                         fallthrough;
11565                 case 'h':
11566                         return CMD_HELP;
11567                 }
11568         }
11569
11570         if (argc == optind) {
11571                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11572                         progname, argv[0]);
11573                 return rc;
11574         } else if (argc > optind + 1) {
11575                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11576                 return rc;
11577         }
11578
11579         if (mirror_id == 0) {
11580                 fprintf(stderr, "%s %s: no valid mirror ID is provided\n",
11581                         progname, argv[0]);
11582                 return rc;
11583         }
11584
11585         /* open mirror file */
11586         fname = argv[optind];
11587         fd = open(fname, O_DIRECT | O_WRONLY);
11588         if (fd < 0) {
11589                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11590                         progname, argv[0], fname, strerror(errno));
11591                 return rc;
11592         }
11593
11594         /* verify mirror id */
11595         rc = verify_mirror_id_by_fd(fd, mirror_id);
11596         if (rc) {
11597                 fprintf(stderr,
11598                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11599                         progname, argv[0], mirror_id, fname);
11600                 goto close_fd;
11601         }
11602
11603         /* open input file */
11604         if (inputfile) {
11605                 rc = check_same_file(fd, inputfile);
11606                 if (rc == 0) {
11607                         fprintf(stderr,
11608                         "%s %s: input file cannot be the mirrored file\n",
11609                                 progname, argv[0]);
11610                         goto close_fd;
11611                 }
11612                 if (rc < 0)
11613                         goto close_fd;
11614
11615                 inputfd = open(inputfile, O_RDONLY, 0644);
11616                 if (inputfd < 0) {
11617                         fprintf(stderr, "%s %s: cannot open file '%s': %s\n",
11618                                 progname, argv[0], inputfile, strerror(errno));
11619                         rc = -errno;
11620                         goto close_fd;
11621                 }
11622         } else {
11623                 inputfd = STDIN_FILENO;
11624         }
11625
11626         /* allocate buffer */
11627         rc = posix_memalign(&buf, page_size, buflen);
11628         if (rc) {
11629                 fprintf(stderr, "%s %s: posix_memalign returns %d\n",
11630                         progname, argv[0], rc);
11631                 goto close_inputfd;
11632         }
11633
11634         /* prepare target mirror components instantiation */
11635         ioc.lil_mode = LL_LEASE_WRLCK;
11636         ioc.lil_flags = LL_LEASE_RESYNC;
11637         ioc.lil_mirror_id = mirror_id;
11638         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11639         if (rc < 0) {
11640                 fprintf(stderr,
11641                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11642                         progname, argv[0], fname, strerror(errno));
11643                 goto free_buf;
11644         }
11645
11646         pos = 0;
11647         while (1) {
11648                 ssize_t bytes_read;
11649                 ssize_t written;
11650                 size_t to_write;
11651
11652                 rc = llapi_lease_check(fd);
11653                 if (rc != LL_LEASE_WRLCK) {
11654                         fprintf(stderr, "%s %s: '%s' lost lease lock\n",
11655                                 progname, argv[0], fname);
11656                         goto free_buf;
11657                 }
11658
11659                 bytes_read = read(inputfd, buf, buflen);
11660                 if (bytes_read < 0) {
11661                         rc = bytes_read;
11662                         fprintf(stderr,
11663                                 "%s %s: fail to read data from '%s': %s\n",
11664                                 progname, argv[0], inputfile ? : "STDIN",
11665                                 strerror(errno));
11666                         rc = -errno;
11667                         goto free_buf;
11668                 }
11669
11670                 /* EOF reached */
11671                 if (bytes_read == 0)
11672                         break;
11673
11674                 /* round up to page align to make direct IO happy. */
11675                 to_write = (bytes_read + page_size - 1) & ~(page_size - 1);
11676
11677                 written = llapi_mirror_write(fd, mirror_id, buf, to_write,
11678                                              pos);
11679                 if (written < 0) {
11680                         rc = written;
11681                         fprintf(stderr,
11682                               "%s %s: fail to write to mirror %u: %s\n",
11683                                 progname, argv[0], mirror_id,
11684                                 strerror(-rc));
11685                         goto free_buf;
11686                 }
11687
11688                 pos += bytes_read;
11689         }
11690
11691         if (pos & (page_size - 1)) {
11692                 rc = llapi_mirror_truncate(fd, mirror_id, pos);
11693                 if (rc < 0)
11694                         goto free_buf;
11695         }
11696
11697         ioc.lil_mode = LL_LEASE_UNLCK;
11698         ioc.lil_flags = LL_LEASE_RESYNC_DONE;
11699         ioc.lil_count = 0;
11700         rc = llapi_lease_set(fd, (struct ll_ioc_lease *)&ioc);
11701         if (rc <= 0) {
11702                 if (rc == 0)
11703                         rc = -EBUSY;
11704                 fprintf(stderr,
11705                         "%s %s: release lease lock of '%s' failed: %s\n",
11706                         progname, argv[0], fname, strerror(errno));
11707                 goto free_buf;
11708         }
11709
11710         rc = 0;
11711
11712 free_buf:
11713         free(buf);
11714 close_inputfd:
11715         if (inputfile)
11716                 close(inputfd);
11717 close_fd:
11718         close(fd);
11719
11720         return rc;
11721 }
11722
11723 static inline int get_other_mirror_ids(int fd, __u16 *ids, __u16 exclude_id)
11724 {
11725         struct llapi_layout *layout;
11726         struct collect_ids_data cid = { .cid_ids = ids,
11727                                         .cid_count = 0,
11728                                         .cid_exclude = exclude_id, };
11729         int rc;
11730
11731         layout = llapi_layout_get_by_fd(fd, 0);
11732         if (!layout) {
11733                 fprintf(stderr, "could not get layout\n");
11734                 return -EINVAL;
11735         }
11736
11737         rc = llapi_layout_comp_iterate(layout, collect_mirror_id, &cid);
11738         if (rc < 0) {
11739                 fprintf(stderr, "failed to iterate layout\n");
11740                 llapi_layout_free(layout);
11741
11742                 return rc;
11743         }
11744         llapi_layout_free(layout);
11745
11746         return cid.cid_count;
11747 }
11748
11749 #ifndef MIRROR_ID_NEG
11750 #define MIRROR_ID_NEG         0x8000
11751 #endif
11752
11753 static inline int lfs_mirror_copy(int argc, char **argv)
11754 {
11755         int rc = CMD_HELP;
11756         __u16 read_mirror_id = 0;
11757         __u16 ids[128] = { 0 };
11758         int count = 0;
11759         struct llapi_layout *layout = NULL;
11760         struct llapi_resync_comp comp_array[1024] = { { 0 } };
11761         int comp_size = 0;
11762         char *fname;
11763         int fd = 0;
11764         int c;
11765         int i;
11766         ssize_t copied;
11767         struct ll_ioc_lease *ioc = NULL;
11768         struct ll_ioc_lease_id *resync_ioc;
11769         struct option long_opts[] = {
11770         { .val = 'h',   .name = "help",         .has_arg = no_argument },
11771         { .val = 'i',   .name = "read-mirror",  .has_arg = required_argument },
11772         { .val = 'o',   .name = "write-mirror", .has_arg = required_argument },
11773         { .name = NULL } };
11774         char cmd[PATH_MAX];
11775
11776         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
11777         progname = cmd;
11778         while ((c = getopt_long(argc, argv, "hi:o:", long_opts, NULL)) >= 0) {
11779                 char *end;
11780
11781                 switch (c) {
11782                 case 'i': {
11783                         unsigned long int id;
11784
11785                         errno = 0;
11786                         id = strtoul(optarg, &end, 0);
11787                         if (errno != 0 || *end != '\0' || id == 0 ||
11788                             id > UINT16_MAX) {
11789                                 fprintf(stderr,
11790                                         "%s: invalid read mirror ID '%s'\n",
11791                                         progname, optarg);
11792                                 return rc;
11793                         }
11794
11795                         read_mirror_id = (__u16)id;
11796                         break;
11797                 }
11798                 case 'o':
11799                         if (!strcmp(optarg, "-1")) {
11800                                 /* specify all other mirrors */
11801                                 ids[0] = (__u16)-1;
11802                                 count = 1;
11803                         } else {
11804                                 count = parse_mirror_ids((__u16 *)ids,
11805                                                          ARRAY_SIZE(ids),
11806                                                          optarg);
11807                                 if (count < 0)
11808                                         return rc;
11809                         }
11810                         break;
11811                 default:
11812                         fprintf(stderr, "%s: unrecognized option '%s'\n",
11813                                 progname, argv[optind - 1]);
11814                         fallthrough;
11815                 case 'h':
11816                         return CMD_HELP;
11817                 }
11818         }
11819
11820         if (argc == optind) {
11821                 fprintf(stderr, "%s %s: no mirrored file provided\n",
11822                         progname, argv[0]);
11823                 return rc;
11824         } else if (argc > optind + 1) {
11825                 fprintf(stderr, "%s %s: too many files\n", progname, argv[0]);
11826                 return rc;
11827         }
11828
11829         if (read_mirror_id == 0) {
11830                 fprintf(stderr,
11831                         "%s %s: no valid read mirror ID %d is provided\n",
11832                         progname, argv[0], read_mirror_id);
11833                 return rc;
11834         }
11835
11836         if (count == 0) {
11837                 fprintf(stderr,
11838                         "%s %s: no write mirror ID is provided\n",
11839                         progname, argv[0]);
11840                 return rc;
11841         }
11842
11843         for (i = 0; i < count; i++) {
11844                 if (read_mirror_id == ids[i]) {
11845                         fprintf(stderr,
11846                         "%s %s: read and write mirror ID cannot be the same\n",
11847                                 progname, argv[0]);
11848                         return rc;
11849                 }
11850         }
11851
11852         /* open mirror file */
11853         fname = argv[optind];
11854
11855         fd = open(fname, O_DIRECT | O_RDWR);
11856         if (fd < 0) {
11857                 fprintf(stderr, "%s %s: cannot open '%s': %s\n",
11858                         progname, argv[0], fname, strerror(errno));
11859                 return rc;
11860         }
11861
11862         /* write to all other mirrors */
11863         if (ids[0] == (__u16)-1) {
11864                 count = get_other_mirror_ids(fd, ids, read_mirror_id);
11865                 if (count <= 0) {
11866                         rc = count;
11867                         fprintf(stderr,
11868                         "%s %s: failed to get other mirror ids in '%s': %d\n",
11869                                 progname, argv[0], fname, rc);
11870                         goto close_fd;
11871                 }
11872         }
11873
11874         /* verify mirror id */
11875         rc = verify_mirror_id_by_fd(fd, read_mirror_id);
11876         if (rc) {
11877                 fprintf(stderr,
11878                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11879                         progname, argv[0], read_mirror_id, fname);
11880                 goto close_fd;
11881         }
11882
11883         for (i = 0; i < count; i++) {
11884                 rc = verify_mirror_id_by_fd(fd, ids[i]);
11885                 if (rc) {
11886                         fprintf(stderr,
11887                         "%s %s: cannot find mirror with ID %u in '%s'\n",
11888                                 progname, argv[0], ids[i], fname);
11889                         goto close_fd;
11890                 }
11891         }
11892
11893         ioc = calloc(sizeof(*ioc) + sizeof(__u32) * 4096, 1);
11894         if (!ioc) {
11895                 fprintf(stderr,
11896                         "%s %s: cannot alloc comp id array for ioc: %s\n",
11897                         progname, argv[0], strerror(errno));
11898                 rc = -errno;
11899                 goto close_fd;
11900         }
11901
11902         /* get stale component info */
11903         layout = llapi_layout_get_by_fd(fd, 0);
11904         if (!layout) {
11905                 fprintf(stderr, "%s %s: failed to get layout of '%s': %s\n",
11906                         progname, argv[0], fname, strerror(errno));
11907                 rc = -errno;
11908                 goto free_ioc;
11909         }
11910         comp_size = llapi_mirror_find_stale(layout, comp_array,
11911                                             ARRAY_SIZE(comp_array),
11912                                             ids, count);
11913         llapi_layout_free(layout);
11914         if (comp_size < 0) {
11915                 rc = comp_size;
11916                 goto free_ioc;
11917         }
11918
11919         /* prepare target mirror components instantiation */
11920         resync_ioc = (struct ll_ioc_lease_id *)ioc;
11921         resync_ioc->lil_mode = LL_LEASE_WRLCK;
11922         resync_ioc->lil_flags = LL_LEASE_RESYNC;
11923         if (count == 1)
11924                 resync_ioc->lil_mirror_id = ids[0];
11925         else
11926                 resync_ioc->lil_mirror_id = read_mirror_id | MIRROR_ID_NEG;
11927         rc = llapi_lease_set(fd, ioc);
11928         if (rc < 0) {
11929                 fprintf(stderr,
11930                         "%s %s: '%s' llapi_lease_get_ext failed: %s\n",
11931                         progname, argv[0], fname, strerror(errno));
11932                 goto free_ioc;
11933         }
11934
11935         copied = llapi_mirror_copy_many(fd, read_mirror_id, ids, count);
11936         if (copied < 0) {
11937                 rc = copied;
11938                 fprintf(stderr, "%s %s: copy error: %d\n",
11939                         progname, argv[0], rc);
11940                 goto free_ioc;
11941         }
11942
11943         fprintf(stdout, "mirror copied successfully: ");
11944         for (i = 0; i < copied; i++)
11945                 fprintf(stdout, "%d ", ids[i]);
11946         fprintf(stdout, "\n");
11947
11948         ioc->lil_mode = LL_LEASE_UNLCK;
11949         ioc->lil_flags = LL_LEASE_RESYNC_DONE;
11950         ioc->lil_count = 0;
11951         for (i = 0; i < comp_size; i++) {
11952                 int j;
11953
11954                 for (j = 0; j < copied; j++) {
11955                         if (comp_array[i].lrc_mirror_id != ids[j])
11956                                 continue;
11957
11958                         ioc->lil_ids[ioc->lil_count] = comp_array[i].lrc_id;
11959                         ioc->lil_count++;
11960                 }
11961         }
11962         rc = llapi_lease_set(fd, ioc);
11963         if (rc <= 0) {
11964                 if (rc == 0)
11965                         rc = -EBUSY;
11966                 fprintf(stderr,
11967                         "%s %s: release lease lock of '%s' failed: %s\n",
11968                         progname, argv[0], fname, strerror(errno));
11969                 goto free_ioc;
11970         }
11971
11972         rc = 0;
11973
11974 free_ioc:
11975         free(ioc);
11976 close_fd:
11977         close(fd);
11978
11979         return rc;
11980 }
11981
11982 /**
11983  * struct verify_chunk - Mirror chunk to be verified.
11984  * @chunk:        [start, end) of the chunk.
11985  * @mirror_count: Number of mirror ids in @mirror_id array.
11986  * @mirror_id:    Array of valid mirror ids that cover the chunk.
11987  */
11988 struct verify_chunk {
11989         struct lu_extent chunk;
11990         unsigned int mirror_count;
11991         __u16 mirror_id[LUSTRE_MIRROR_COUNT_MAX];
11992 };
11993
11994 /**
11995  * print_chunks() - Print chunk information.
11996  * @fname:       Mirrored file name.
11997  * @chunks:      Array of chunks.
11998  * @chunk_count: Number of chunks in @chunks array.
11999  *
12000  * This function prints [start, end) of each chunk in @chunks
12001  * for mirrored file @fname, and also prints the valid mirror ids
12002  * that cover the chunk.
12003  *
12004  * Return: void.
12005  */
12006 static inline
12007 void print_chunks(const char *fname, struct verify_chunk *chunks,
12008                   int chunk_count)
12009 {
12010         int i;
12011         int j;
12012
12013         fprintf(stdout, "Chunks to be verified in %s:\n", fname);
12014         for (i = 0; i < chunk_count; i++) {
12015                 fprintf(stdout, DEXT, PEXT(&chunks[i].chunk));
12016
12017                 if (chunks[i].mirror_count == 0)
12018                         fprintf(stdout, "\t[");
12019                 else {
12020                         fprintf(stdout, "\t[%u", chunks[i].mirror_id[0]);
12021                         for (j = 1; j < chunks[i].mirror_count; j++)
12022                                 fprintf(stdout, ", %u", chunks[i].mirror_id[j]);
12023                 }
12024                 fprintf(stdout, "]\t%u\n", chunks[i].mirror_count);
12025         }
12026         fprintf(stdout, "\n");
12027 }
12028
12029 /**
12030  * print_checksums() - Print CRC-32 checksum values.
12031  * @chunk: A chunk and its corresponding valid mirror ids.
12032  * @crc:   CRC-32 checksum values on the chunk for each valid mirror.
12033  *
12034  * This function prints CRC-32 checksum values on @chunk for
12035  * each valid mirror that covers it.
12036  *
12037  * Return: void.
12038  */
12039 static inline
12040 void print_checksums(struct verify_chunk *chunk, unsigned long *crc,
12041                      unsigned long long pos, unsigned long long len)
12042 {
12043         int i;
12044
12045         fprintf(stdout,
12046                 "CRC-32 checksum value for chunk "DEXT":\n", pos, pos + len);
12047         for (i = 0; i < chunk->mirror_count; i++)
12048                 fprintf(stdout, "Mirror %u:\t%#lx\n",
12049                         chunk->mirror_id[i], crc[i]);
12050         fprintf(stdout, "\n");
12051 }
12052
12053 /**
12054  * filter_mirror_id() - Filter specified mirror ids.
12055  * @chunks:      Array of chunks.
12056  * @chunk_count: Number of chunks in @chunks array.
12057  * @mirror_ids:  Specified mirror ids to be verified.
12058  * @ids_nr:      Number of specified mirror ids.
12059  *
12060  * This function scans valid mirror ids that cover each chunk in @chunks
12061  * and filters specified mirror ids.
12062  *
12063  * Return: void.
12064  */
12065 static inline
12066 void filter_mirror_id(struct verify_chunk *chunks, int chunk_count,
12067                       __u16 *mirror_ids, int ids_nr)
12068 {
12069         int i;
12070         int j;
12071         int k;
12072         __u16 valid_id[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12073         unsigned int valid_count = 0;
12074
12075         for (i = 0; i < chunk_count; i++) {
12076                 if (chunks[i].mirror_count == 0)
12077                         continue;
12078
12079                 valid_count = 0;
12080                 for (j = 0; j < ids_nr; j++) {
12081                         for (k = 0; k < chunks[i].mirror_count; k++) {
12082                                 if (chunks[i].mirror_id[k] == mirror_ids[j]) {
12083                                         valid_id[valid_count] = mirror_ids[j];
12084                                         valid_count++;
12085                                         break;
12086                                 }
12087                         }
12088                 }
12089
12090                 memcpy(chunks[i].mirror_id, valid_id,
12091                        sizeof(__u16) * valid_count);
12092                 chunks[i].mirror_count = valid_count;
12093         }
12094 }
12095
12096 /**
12097  * lfs_mirror_prepare_chunk() - Find mirror chunks to be verified.
12098  * @layout:      Mirror component list.
12099  * @chunks:      Array of chunks.
12100  * @chunks_size: Array size of @chunks.
12101  *
12102  * This function scans the components in @layout from offset 0 to LUSTRE_EOF
12103  * to find out chunk segments and store them in @chunks array.
12104  *
12105  * The @mirror_id array in each element of @chunks will store the valid
12106  * mirror ids that cover the chunk. If a mirror component covering the
12107  * chunk has LCME_FL_STALE or LCME_FL_OFFLINE flag, then the mirror id
12108  * will not be stored into the @mirror_id array, and the chunk for that
12109  * mirror will not be verified.
12110  *
12111  * The @mirror_count in each element of @chunks will store the number of
12112  * mirror ids in @mirror_id array. If @mirror_count is 0, it indicates the
12113  * chunk is invalid in all of the mirrors. And if @mirror_count is 1, it
12114  * indicates the chunk is valid in only one mirror. In both cases, the
12115  * chunk will not be verified.
12116  *
12117  * Here is an example:
12118  *
12119  *  0      1M     2M     3M     4M           EOF
12120  *  +------+-------------+--------------------+
12121  *  |      |             |      S             |       mirror1
12122  *  +------+------+------+------+-------------+
12123  *  |             |   S  |   S  |             |       mirror2
12124  *  +-------------+------+------+-------------+
12125  *
12126  * prepared @chunks array will contain 5 elements:
12127  * (([0, 1M), [1, 2], 2),
12128  *  ([1M, 2M), [1, 2], 2),
12129  *  ([2M, 3M), [1], 1),
12130  *  ([3M, 4M], [], 0),
12131  *  ([4M, EOF), [2], 1))
12132  *
12133  * Return: the actual array size of @chunks on success
12134  *         or a negative error code on failure.
12135  */
12136 static inline
12137 int lfs_mirror_prepare_chunk(struct llapi_layout *layout,
12138                              struct verify_chunk *chunks,
12139                              size_t chunks_size)
12140 {
12141         uint64_t start;
12142         uint64_t end;
12143         uint32_t mirror_id;
12144         uint32_t flags;
12145         int idx = 0;
12146         int i = 0;
12147         int rc = 0;
12148
12149         memset(chunks, 0, sizeof(*chunks) * chunks_size);
12150
12151         while (1) {
12152                 rc = llapi_layout_comp_use(layout, LLAPI_LAYOUT_COMP_USE_FIRST);
12153                 if (rc < 0) {
12154                         fprintf(stderr,
12155                                 "%s: move to the first layout component: %s.\n",
12156                                 progname, strerror(errno));
12157                         goto error;
12158                 }
12159
12160                 i = 0;
12161                 rc = 0;
12162                 chunks[idx].chunk.e_end = LUSTRE_EOF;
12163                 while (rc == 0) {
12164                         rc = llapi_layout_comp_extent_get(layout, &start, &end);
12165                         if (rc < 0) {
12166                                 fprintf(stderr,
12167                                         "%s: llapi_layout_comp_extent_get failed: %s.\n",
12168                                         progname, strerror(errno));
12169                                 goto error;
12170                         }
12171
12172                         if (start > chunks[idx].chunk.e_start ||
12173                             end <= chunks[idx].chunk.e_start)
12174                                 goto next;
12175
12176                         if (end < chunks[idx].chunk.e_end)
12177                                 chunks[idx].chunk.e_end = end;
12178
12179                         rc = llapi_layout_comp_flags_get(layout, &flags);
12180                         if (rc < 0) {
12181                                 fprintf(stderr,
12182                                         "%s: llapi_layout_comp_flags_get failed: %s.\n",
12183                                         progname, strerror(errno));
12184                                 goto error;
12185                         }
12186
12187                         if (flags & LCME_FL_STALE || flags & LCME_FL_OFFLINE)
12188                                 goto next;
12189
12190                         rc = llapi_layout_mirror_id_get(layout, &mirror_id);
12191                         if (rc < 0) {
12192                                 fprintf(stderr,
12193                                         "%s: llapi_layout_mirror_id_get failed: %s.\n",
12194                                         progname, strerror(errno));
12195                                 goto error;
12196                         }
12197
12198                         if (i >= ARRAY_SIZE(chunks[idx].mirror_id)) {
12199                                 fprintf(stderr,
12200                                         "%s: mirror_id array is too small.\n",
12201                                         progname);
12202                                 rc = -EINVAL;
12203                                 goto error;
12204                         }
12205                         chunks[idx].mirror_id[i] = mirror_id;
12206                         i++;
12207
12208 next:
12209                         rc = llapi_layout_comp_use(layout,
12210                                                    LLAPI_LAYOUT_COMP_USE_NEXT);
12211                         if (rc < 0) {
12212                                 fprintf(stderr,
12213                                         "%s: move to the next layout component: %s.\n",
12214                                         progname, strerror(errno));
12215                                 goto error;
12216                         }
12217                 } /* loop through all components */
12218
12219                 chunks[idx].mirror_count = i;
12220
12221                 if (chunks[idx].chunk.e_end == LUSTRE_EOF)
12222                         break;
12223
12224                 idx++;
12225                 if (idx >= chunks_size) {
12226                         fprintf(stderr, "%s: chunks array is too small.\n",
12227                                 progname);
12228                         rc = -EINVAL;
12229                         goto error;
12230                 }
12231
12232                 chunks[idx].chunk.e_start = chunks[idx - 1].chunk.e_end;
12233         }
12234
12235 error:
12236         return rc < 0 ? rc : idx + 1;
12237 }
12238
12239 /**
12240  * lfs_mirror_verify_chunk() - Verify a chunk.
12241  * @fd:        File descriptor of the mirrored file.
12242  * @file_size: Size of the mirrored file.
12243  * @chunk:     A chunk and its corresponding valid mirror ids.
12244  * @verbose:   Verbose mode.
12245  *
12246  * This function verifies a @chunk contains exactly the same data
12247  * ammong the mirrors that cover it.
12248  *
12249  * If @verbose is specified, then the function will print where the
12250  * differences are if the data do not match. Otherwise, it will
12251  * just return an error in that case.
12252  *
12253  * Return: 0 on success or a negative error code on failure.
12254  */
12255 static inline
12256 int lfs_mirror_verify_chunk(int fd, size_t file_size,
12257                             struct verify_chunk *chunk, int verbose)
12258 {
12259         const size_t buflen = 4 * 1024 * 1024; /* 4M */
12260         void *buf;
12261         size_t page_size = sysconf(_SC_PAGESIZE);
12262         ssize_t bytes_read;
12263         ssize_t bytes_done;
12264         size_t count;
12265         off_t pos;
12266         unsigned long crc;
12267         unsigned long crc_array[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12268         int i;
12269         int rc = 0;
12270
12271         if (file_size == 0)
12272                 return 0;
12273
12274         rc = posix_memalign(&buf, page_size, buflen);
12275         if (rc) /* error code is returned directly */
12276                 return -rc;
12277
12278         if (verbose > 1) {
12279                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12280                         PEXT(&chunk->chunk));
12281                 for (i = 0; i < chunk->mirror_count; i++)
12282                         fprintf(stdout, " %u", chunk->mirror_id[i]);
12283                 fprintf(stdout, "\n");
12284         }
12285
12286         bytes_done = 0;
12287         count = MIN(chunk->chunk.e_end, file_size) - chunk->chunk.e_start;
12288         pos = chunk->chunk.e_start;
12289         while (bytes_done < count) {
12290                 /* compute initial CRC-32 checksum */
12291                 crc = crc32(0L, Z_NULL, 0);
12292                 memset(crc_array, 0, sizeof(crc_array));
12293
12294                 bytes_read = 0;
12295                 for (i = 0; i < chunk->mirror_count; i++) {
12296                         bytes_read = llapi_mirror_read(fd, chunk->mirror_id[i],
12297                                                        buf, buflen, pos);
12298                         if (bytes_read < 0) {
12299                                 rc = bytes_read;
12300                                 fprintf(stderr,
12301                                         "%s: failed to read data from mirror %u: %s.\n",
12302                                         progname, chunk->mirror_id[i],
12303                                         strerror(-rc));
12304                                 goto error;
12305                         }
12306
12307                         /* compute new CRC-32 checksum */
12308                         crc_array[i] = crc32(crc, buf, bytes_read);
12309                 }
12310
12311                 if (verbose)
12312                         print_checksums(chunk, crc_array, pos, buflen);
12313
12314                 /* compare CRC-32 checksum values */
12315                 for (i = 1; i < chunk->mirror_count; i++) {
12316                         if (crc_array[i] != crc_array[0]) {
12317                                 rc = -EINVAL;
12318
12319                                 fprintf(stderr,
12320                                         "%s: chunk "DEXT" has different checksum value on mirror %u and mirror %u.\n",
12321                                         progname, PEXT(&chunk->chunk),
12322                                         chunk->mirror_id[0],
12323                                         chunk->mirror_id[i]);
12324                         }
12325                 }
12326
12327                 pos += bytes_read;
12328                 bytes_done += bytes_read;
12329         }
12330
12331         if (verbose > 1 && rc == 0) {
12332                 fprintf(stdout, "Verifying chunk "DEXT" on mirror:",
12333                         PEXT(&chunk->chunk));
12334                 for (i = 0; i < chunk->mirror_count; i++)
12335                         fprintf(stdout, " %u", chunk->mirror_id[i]);
12336                 fprintf(stdout, " PASS\n\n");
12337         }
12338
12339 error:
12340         free(buf);
12341         return rc;
12342 }
12343
12344 /**
12345  * lfs_mirror_verify_file() - Verify a mirrored file.
12346  * @fname:      Mirrored file name.
12347  * @mirror_ids: Specified mirror ids to be verified.
12348  * @ids_nr:     Number of specified mirror ids.
12349  * @verbose:    Verbose mode.
12350  *
12351  * This function verifies that each SYNC mirror of a mirrored file
12352  * specified by @fname contains exactly the same data.
12353  *
12354  * If @mirror_ids is specified, then the function will verify the
12355  * mirrors specified by @mirror_ids contain exactly the same data.
12356  *
12357  * If @verbose is specified, then the function will print where the
12358  * differences are if the data do not match. Otherwise, it will
12359  * just return an error in that case.
12360  *
12361  * Return: 0 on success or a negative error code on failure.
12362  */
12363 static inline
12364 int lfs_mirror_verify_file(const char *fname, __u16 *mirror_ids, int ids_nr,
12365                            int verbose)
12366 {
12367         struct verify_chunk chunks_array[1024] = { };
12368         struct llapi_layout *layout = NULL;
12369         struct stat stbuf;
12370         uint32_t flr_state;
12371         int fd;
12372         int chunk_count = 0;
12373         int idx = 0;
12374         int rc = 0;
12375         int rc1 = 0;
12376         int rc2 = 0;
12377
12378         if (stat(fname, &stbuf) < 0) {
12379                 fprintf(stderr, "%s: cannot stat file '%s': %s.\n",
12380                         progname, fname, strerror(errno));
12381                 rc = -errno;
12382                 goto error;
12383         }
12384
12385         if (!S_ISREG(stbuf.st_mode)) {
12386                 fprintf(stderr, "%s: '%s' is not a regular file.\n",
12387                         progname, fname);
12388                 rc = -EINVAL;
12389                 goto error;
12390         }
12391
12392         if (stbuf.st_size == 0) {
12393                 if (verbose)
12394                         fprintf(stdout, "%s: '%s' file size is 0.\n",
12395                                 progname, fname);
12396                 rc = 0;
12397                 goto error;
12398         }
12399
12400         /* Allow mirror verify even without the key on encrypted files */
12401         fd = open(fname, O_DIRECT | O_RDONLY | O_FILE_ENC);
12402         if (fd < 0) {
12403                 fprintf(stderr, "%s: cannot open '%s': %s.\n",
12404                         progname, fname, strerror(errno));
12405                 rc = -errno;
12406                 goto error;
12407         }
12408
12409         rc = llapi_lease_acquire(fd, LL_LEASE_RDLCK);
12410         if (rc < 0) {
12411                 fprintf(stderr, "%s: '%s' llapi_lease_acquire failed: %s.\n",
12412                         progname, fname, strerror(errno));
12413                 goto close_fd;
12414         }
12415
12416         layout = llapi_layout_get_by_fd(fd, 0);
12417         if (!layout) {
12418                 fprintf(stderr, "%s: '%s' llapi_layout_get_by_fd failed: %s.\n",
12419                         progname, fname, strerror(errno));
12420                 rc = -errno;
12421                 llapi_lease_release(fd);
12422                 goto close_fd;
12423         }
12424
12425         rc = llapi_layout_flags_get(layout, &flr_state);
12426         if (rc < 0) {
12427                 fprintf(stderr, "%s: '%s' llapi_layout_flags_get failed: %s.\n",
12428                         progname, fname, strerror(errno));
12429                 rc = -errno;
12430                 goto free_layout;
12431         }
12432
12433         flr_state &= LCM_FL_FLR_MASK;
12434         switch (flr_state) {
12435         case LCM_FL_NONE:
12436                 rc = -EINVAL;
12437                 fprintf(stderr, "%s: '%s' file state error: %s.\n",
12438                         progname, fname, llapi_layout_flags_string(flr_state));
12439                 goto free_layout;
12440         default:
12441                 break;
12442         }
12443
12444         /* find out mirror chunks to be verified */
12445         chunk_count = lfs_mirror_prepare_chunk(layout, chunks_array,
12446                                                ARRAY_SIZE(chunks_array));
12447         if (chunk_count < 0) {
12448                 rc = chunk_count;
12449                 goto free_layout;
12450         }
12451
12452         if (ids_nr > 0)
12453                 /* filter specified mirror ids */
12454                 filter_mirror_id(chunks_array, chunk_count, mirror_ids, ids_nr);
12455
12456         if (verbose > 2)
12457                 print_chunks(fname, chunks_array, chunk_count);
12458
12459         for (idx = 0; idx < chunk_count; idx++) {
12460                 if (chunks_array[idx].chunk.e_start >= stbuf.st_size) {
12461                         if (verbose)
12462                                 fprintf(stdout,
12463                                         "%s: '%s' chunk "DEXT" exceeds file size %#llx: skipped\n",
12464                                         progname, fname,
12465                                         PEXT(&chunks_array[idx].chunk),
12466                                         (unsigned long long)stbuf.st_size);
12467                         break;
12468                 }
12469
12470                 if (chunks_array[idx].mirror_count == 0) {
12471                         fprintf(stderr,
12472                                 "%s: '%s' chunk "DEXT" is invalid in all of the mirrors: ",
12473                                 progname, fname,
12474                                 PEXT(&chunks_array[idx].chunk));
12475                         if (verbose) {
12476                                 fprintf(stderr, "skipped\n");
12477                                 continue;
12478                         }
12479                         rc = -EINVAL;
12480                         fprintf(stderr, "failed\n");
12481                         goto free_layout;
12482                 }
12483
12484                 if (chunks_array[idx].mirror_count == 1) {
12485                         if (verbose)
12486                                 fprintf(stdout,
12487                                         "%s: '%s' chunk "DEXT" is only valid in mirror %u: skipped\n",
12488                                         progname, fname,
12489                                         PEXT(&chunks_array[idx].chunk),
12490                                         chunks_array[idx].mirror_id[0]);
12491                         continue;
12492                 }
12493
12494                 rc = llapi_lease_check(fd);
12495                 if (rc != LL_LEASE_RDLCK) {
12496                         fprintf(stderr, "%s: '%s' lost lease lock.\n",
12497                                 progname, fname);
12498                         goto free_layout;
12499                 }
12500
12501                 /* verify one chunk */
12502                 rc1 = lfs_mirror_verify_chunk(fd, stbuf.st_size,
12503                                               &chunks_array[idx], verbose);
12504                 if (rc1 < 0) {
12505                         rc2 = rc1;
12506                         if (!verbose) {
12507                                 rc = rc1;
12508                                 goto free_layout;
12509                         }
12510                 }
12511         }
12512
12513         if (rc2 < 0)
12514                 rc = rc2;
12515
12516 free_layout:
12517         llapi_layout_free(layout);
12518         llapi_lease_release(fd);
12519 close_fd:
12520         close(fd);
12521 error:
12522         return rc;
12523 }
12524
12525 /**
12526  * lfs_mirror_verify() - Parse and execute lfs mirror verify command.
12527  * @argc: The count of lfs mirror verify command line arguments.
12528  * @argv: Array of strings for lfs mirror verify command line arguments.
12529  *
12530  * This function parses lfs mirror verify command and verifies the
12531  * specified mirrored file(s).
12532  *
12533  * Return: 0 on success or a negative error code on failure.
12534  */
12535 static inline int lfs_mirror_verify(int argc, char **argv)
12536 {
12537         __u16 mirror_ids[LUSTRE_MIRROR_COUNT_MAX] = { 0 };
12538         int ids_nr = 0;
12539         int c;
12540         int verbose = 0;
12541         int rc = 0;
12542         int rc1 = 0;
12543         char cmd[PATH_MAX];
12544
12545         struct option long_opts[] = {
12546         { .val = 'h',   .name = "help",         .has_arg = no_argument },
12547         { .val = 'o',   .name = "only",         .has_arg = required_argument },
12548         { .val = 'v',   .name = "verbose",      .has_arg = no_argument },
12549         { .name = NULL } };
12550
12551         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12552         progname = cmd;
12553         while ((c = getopt_long(argc, argv, "ho:v", long_opts, NULL)) >= 0) {
12554                 switch (c) {
12555                 case 'o':
12556                         rc = parse_mirror_ids(mirror_ids,
12557                                               ARRAY_SIZE(mirror_ids),
12558                                               optarg);
12559                         if (rc < 0) {
12560                                 fprintf(stderr,
12561                                         "%s: bad mirror ids '%s'.\n",
12562                                         progname, optarg);
12563                                 goto error;
12564                         }
12565                         ids_nr = rc;
12566                         if (ids_nr < 2) {
12567                                 fprintf(stderr,
12568                                         "%s: at least 2 mirror ids needed with '--only' option.\n",
12569                                         progname);
12570                                 rc = CMD_HELP;
12571                                 goto error;
12572                         }
12573                         break;
12574                 case 'v':
12575                         verbose++;
12576                         break;
12577                 default:
12578                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12579                                 progname, argv[optind - 1]);
12580                         fallthrough;
12581                 case 'h':
12582                         rc = CMD_HELP;
12583                         goto error;
12584                 }
12585         }
12586
12587         if (argc == optind) {
12588                 fprintf(stderr, "%s: no file name given.\n", progname);
12589                 rc = CMD_HELP;
12590                 goto error;
12591         }
12592
12593         if (ids_nr > 0 && argc > optind + 1) {
12594                 fprintf(stderr,
12595                         "%s: '--only' cannot be used upon multiple files.\n",
12596                         progname);
12597                 rc = CMD_HELP;
12598                 goto error;
12599         }
12600
12601         if (ids_nr > 0) {
12602                 rc = verify_mirror_ids(argv[optind], mirror_ids, ids_nr);
12603                 if (rc < 0)
12604                         goto error;
12605         }
12606
12607         rc = 0;
12608         for (; optind < argc; optind++) {
12609                 rc1 = lfs_mirror_verify_file(argv[optind], mirror_ids, ids_nr,
12610                                              verbose);
12611                 if (rc1 < 0)
12612                         rc = rc1;
12613         }
12614 error:
12615         return rc;
12616 }
12617
12618 /**
12619  * lfs_mirror() - Parse and execute lfs mirror commands.
12620  * @argc: The count of lfs mirror command line arguments.
12621  * @argv: Array of strings for lfs mirror command line arguments.
12622  *
12623  * This function parses lfs mirror commands and performs the
12624  * corresponding functions specified in mirror_cmdlist[].
12625  *
12626  * Return: 0 on success or an error code on failure.
12627  */
12628 static int lfs_mirror(int argc, char **argv)
12629 {
12630         char cmd[PATH_MAX];
12631         int rc = 0;
12632
12633         setlinebuf(stdout);
12634
12635         Parser_init("lfs-mirror > ", mirror_cmdlist);
12636
12637         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
12638         progname = cmd;
12639         program_invocation_short_name = cmd;
12640         if (argc > 1)
12641                 rc = Parser_execarg(argc - 1, argv + 1, mirror_cmdlist);
12642         else
12643                 rc = Parser_commands();
12644
12645         return rc < 0 ? -rc : rc;
12646 }
12647
12648 static void lustre_som_swab(struct lustre_som_attrs *attrs)
12649 {
12650 #if __BYTE_ORDER == __BIG_ENDIAN
12651         __swab16s(&attrs->lsa_valid);
12652         __swab64s(&attrs->lsa_size);
12653         __swab64s(&attrs->lsa_blocks);
12654 #endif
12655 }
12656
12657 enum lfs_som_type {
12658         LFS_SOM_SIZE = 0x1,
12659         LFS_SOM_BLOCKS = 0x2,
12660         LFS_SOM_FLAGS = 0x4,
12661         LFS_SOM_ATTR_ALL = LFS_SOM_SIZE | LFS_SOM_BLOCKS |
12662                            LFS_SOM_FLAGS,
12663 };
12664
12665 static int lfs_getsom(int argc, char **argv)
12666 {
12667         const char *path;
12668         struct lustre_som_attrs *attrs;
12669         char buf[sizeof(*attrs) + 64];
12670         enum lfs_som_type type = LFS_SOM_ATTR_ALL;
12671         int rc = 0, c;
12672
12673         while ((c = getopt(argc, argv, "bfhs")) != -1) {
12674                 switch (c) {
12675                 case 'b':
12676                         type = LFS_SOM_BLOCKS;
12677                         break;
12678                 case 'f':
12679                         type = LFS_SOM_FLAGS;
12680                         break;
12681                 case 's':
12682                         type = LFS_SOM_SIZE;
12683                         break;
12684                 default:
12685                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12686                                 progname, argv[optind - 1]);
12687                         fallthrough;
12688                 case 'h':
12689                         return CMD_HELP;
12690                 }
12691         }
12692
12693         argc -= optind;
12694         argv += optind;
12695
12696         if (argc != 1) {
12697                 fprintf(stderr, "%s: %s\n",
12698                         progname, argc == 0 ? "miss file target" :
12699                         "input more than 2 files");
12700                 return CMD_HELP;
12701         }
12702
12703         path = argv[0];
12704         attrs = (void *)buf;
12705         rc = lgetxattr(path, "trusted.som", attrs, sizeof(buf));
12706         if (rc < 0) {
12707                 rc = -errno;
12708                 fprintf(stderr, "%s failed to get som xattr: %s (%d)\n",
12709                         argv[0], strerror(errno), errno);
12710                 return rc;
12711         }
12712
12713         lustre_som_swab(attrs);
12714
12715         switch (type) {
12716         case LFS_SOM_ATTR_ALL:
12717                 printf("file: %s size: %llu blocks: %llu flags: %x\n",
12718                        path, (unsigned long long)attrs->lsa_size,
12719                        (unsigned long long)attrs->lsa_blocks,
12720                        attrs->lsa_valid);
12721                 break;
12722         case LFS_SOM_SIZE:
12723                 printf("%llu\n", (unsigned long long)attrs->lsa_size);
12724                 break;
12725         case LFS_SOM_BLOCKS:
12726                 printf("%llu\n", (unsigned long long)attrs->lsa_blocks);
12727                 break;
12728         case LFS_SOM_FLAGS:
12729                 printf("%x\n", attrs->lsa_valid);
12730                 break;
12731         default:
12732                 fprintf(stderr, "%s: unknown option\n", progname);
12733                 return CMD_HELP;
12734         }
12735
12736         return 0;
12737 }
12738
12739 /**
12740  * lfs_mirror_list_commands() - List lfs mirror commands.
12741  * @argc: The count of command line arguments.
12742  * @argv: Array of strings for command line arguments.
12743  *
12744  * This function lists lfs mirror commands defined in mirror_cmdlist[].
12745  *
12746  * Return: 0 on success.
12747  */
12748 static int lfs_mirror_list_commands(int argc, char **argv)
12749 {
12750         Parser_list_commands(mirror_cmdlist, 80, 4);
12751
12752         return 0;
12753 }
12754
12755 static int lfs_pcc_attach(int argc, char **argv)
12756 {
12757         struct option long_opts[] = {
12758         { .val = 'h',   .name = "help", .has_arg = no_argument },
12759         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12760         { .name = NULL } };
12761         int c;
12762         int rc = 0;
12763         __u32 archive_id = 0;
12764         const char *path;
12765         char *end;
12766         char fullpath[PATH_MAX];
12767         enum lu_pcc_type type = LU_PCC_READWRITE;
12768
12769         optind = 0;
12770         while ((c = getopt_long(argc, argv, "hi:",
12771                                 long_opts, NULL)) != -1) {
12772                 switch (c) {
12773                 case 'i':
12774                         errno = 0;
12775                         archive_id = strtoul(optarg, &end, 0);
12776                         if (errno != 0 || *end != '\0' ||
12777                             archive_id == 0 || archive_id > UINT32_MAX) {
12778                                 fprintf(stderr,
12779                                         "error: %s: bad archive ID '%s'\n",
12780                                         progname, optarg);
12781                                 return CMD_HELP;
12782                         }
12783                         break;
12784                 default:
12785                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12786                                 progname, argv[optind - 1]);
12787                         fallthrough;
12788                 case 'h':
12789                         return CMD_HELP;
12790                 }
12791         }
12792
12793         if (archive_id == 0) {
12794                 fprintf(stderr, "%s: must specify attach ID\n", argv[0]);
12795                 return CMD_HELP;
12796         }
12797
12798         if (argc <= optind) {
12799                 fprintf(stderr, "%s: must specify one or more file names\n",
12800                         argv[0]);
12801                 return CMD_HELP;
12802         }
12803
12804         while (optind < argc) {
12805                 int rc2;
12806
12807                 path = argv[optind++];
12808                 if (!realpath(path, fullpath)) {
12809                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12810                                 argv[0], path, strerror(errno));
12811                         if (rc == 0)
12812                                 rc = -EINVAL;
12813                         continue;
12814                 }
12815
12816                 rc2 = llapi_pcc_attach(fullpath, archive_id, type);
12817                 if (rc2 < 0) {
12818                         fprintf(stderr,
12819                                 "%s: cannot attach '%s' to PCC with archive ID '%u': %s\n",
12820                                 argv[0], path, archive_id, strerror(-rc2));
12821                         if (rc == 0)
12822                                 rc = rc2;
12823                 }
12824         }
12825         return rc;
12826 }
12827
12828 static int lfs_pcc_attach_fid(int argc, char **argv)
12829 {
12830         struct option long_opts[] = {
12831         { .val = 'h',   .name = "help", .has_arg = no_argument },
12832         { .val = 'i',   .name = "id",   .has_arg = required_argument },
12833         { .val = 'm',   .name = "mnt",  .has_arg = required_argument },
12834         { .name = NULL } };
12835         int c;
12836         int rc = 0;
12837         __u32 archive_id = 0;
12838         char *end;
12839         const char *mntpath = NULL;
12840         const char *fidstr;
12841         enum lu_pcc_type type = LU_PCC_READWRITE;
12842
12843         optind = 0;
12844         while ((c = getopt_long(argc, argv, "hi:m:",
12845                                 long_opts, NULL)) != -1) {
12846                 switch (c) {
12847                 case 'i':
12848                         errno = 0;
12849                         archive_id = strtoul(optarg, &end, 0);
12850                         if (errno != 0 || *end != '\0' ||
12851                             archive_id > UINT32_MAX) {
12852                                 fprintf(stderr,
12853                                         "error: %s: bad archive ID '%s'\n",
12854                                         argv[0], optarg);
12855                                 return CMD_HELP;
12856                         }
12857                         break;
12858                 case 'm':
12859                         mntpath = optarg;
12860                         break;
12861                 default:
12862                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12863                                 progname, argv[optind - 1]);
12864                         fallthrough;
12865                 case 'h':
12866                         return CMD_HELP;
12867                 }
12868         }
12869
12870         if (archive_id == 0) {
12871                 fprintf(stderr, "%s: must specify an archive ID\n", argv[0]);
12872                 return CMD_HELP;
12873         }
12874
12875         if (!mntpath) {
12876                 fprintf(stderr, "%s: must specify Lustre mount point\n",
12877                         argv[0]);
12878                 return CMD_HELP;
12879         }
12880
12881         if (argc <= optind) {
12882                 fprintf(stderr, "%s: must specify one or more fids\n", argv[0]);
12883                 return CMD_HELP;
12884         }
12885
12886         while (optind < argc) {
12887                 int rc2;
12888
12889                 fidstr = argv[optind++];
12890
12891                 rc2 = llapi_pcc_attach_fid_str(mntpath, fidstr,
12892                                                archive_id, type);
12893                 if (rc2 < 0) {
12894                         fprintf(stderr,
12895                                 "%s: cannot attach '%s' on '%s' to PCC with archive ID '%u': %s\n",
12896                                 argv[0], fidstr, mntpath, archive_id,
12897                                 strerror(rc2));
12898                 }
12899                 if (rc == 0 && rc2 < 0)
12900                         rc = rc2;
12901         }
12902         return rc;
12903 }
12904
12905 static int lfs_pcc_detach(int argc, char **argv)
12906 {
12907         struct option long_opts[] = {
12908         { .val = 'h',   .name = "help", .has_arg = no_argument },
12909         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12910         { .name = NULL } };
12911         int c;
12912         int rc = 0;
12913         const char *path;
12914         char fullpath[PATH_MAX];
12915         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12916
12917         optind = 0;
12918         while ((c = getopt_long(argc, argv, "hk",
12919                                 long_opts, NULL)) != -1) {
12920                 switch (c) {
12921                 case 'k':
12922                         detach_opt = PCC_DETACH_OPT_NONE;
12923                         break;
12924                 default:
12925                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12926                                 progname, argv[optind - 1]);
12927                         fallthrough;
12928                 case 'h':
12929                         return CMD_HELP;
12930                 }
12931         }
12932
12933         while (optind < argc) {
12934                 int rc2;
12935
12936                 path = argv[optind++];
12937                 if (!realpath(path, fullpath)) {
12938                         fprintf(stderr, "%s: could not find path '%s': %s\n",
12939                                 argv[0], path, strerror(errno));
12940                         if (rc == 0)
12941                                 rc = -EINVAL;
12942                         continue;
12943                 }
12944
12945                 rc2 = llapi_pcc_detach_file(fullpath, detach_opt);
12946                 if (rc2 < 0) {
12947                         rc2 = -errno;
12948                         fprintf(stderr,
12949                                 "%s: cannot detach '%s' from PCC: %s\n",
12950                                 argv[0], path, strerror(errno));
12951                         if (rc == 0)
12952                                 rc = rc2;
12953                 }
12954         }
12955         return rc;
12956 }
12957
12958 static int lfs_pcc_detach_fid(int argc, char **argv)
12959 {
12960         struct option long_opts[] = {
12961         { .val = 'h',   .name = "help", .has_arg = no_argument },
12962         { .val = 'k',   .name = "keep", .has_arg = no_argument },
12963         { .name = NULL } };
12964         int c;
12965         int rc = 0;
12966         const char *fid;
12967         const char *mntpath;
12968         __u32 detach_opt = PCC_DETACH_OPT_UNCACHE;
12969
12970         optind = 0;
12971         while ((c = getopt_long(argc, argv, "hk",
12972                                 long_opts, NULL)) != -1) {
12973                 switch (c) {
12974                 case 'k':
12975                         detach_opt = PCC_DETACH_OPT_NONE;
12976                         break;
12977                 default:
12978                         fprintf(stderr, "%s: unrecognized option '%s'\n",
12979                                 progname, argv[optind - 1]);
12980                         fallthrough;
12981                 case 'h':
12982                         return CMD_HELP;
12983                 }
12984         }
12985
12986         mntpath = argv[optind++];
12987
12988         while (optind < argc) {
12989                 int rc2;
12990
12991                 fid = argv[optind++];
12992
12993                 rc2 = llapi_pcc_detach_fid_str(mntpath, fid, detach_opt);
12994                 if (rc2 < 0) {
12995                         fprintf(stderr,
12996                                 "%s: cannot detach '%s' on '%s' from PCC: %s\n",
12997                                 argv[0], fid, mntpath, strerror(-rc2));
12998                         if (rc == 0)
12999                                 rc = rc2;
13000                 }
13001         }
13002         return rc;
13003 }
13004
13005 static int lfs_pcc_state(int argc, char **argv)
13006 {
13007         int rc = 0;
13008         const char *path;
13009         char fullpath[PATH_MAX];
13010         struct lu_pcc_state state;
13011
13012         optind = 1;
13013
13014         if (argc <= 1) {
13015                 fprintf(stderr, "%s: must specify one or more file names\n",
13016                         progname);
13017                 return CMD_HELP;
13018         }
13019
13020         while (optind < argc) {
13021                 int rc2;
13022
13023                 path = argv[optind++];
13024                 if (!realpath(path, fullpath)) {
13025                         fprintf(stderr, "%s: could not find path '%s': %s\n",
13026                                 argv[0], path, strerror(errno));
13027                         if (rc == 0)
13028                                 rc = -EINVAL;
13029                         continue;
13030                 }
13031
13032                 rc2 = llapi_pcc_state_get(fullpath, &state);
13033                 if (rc2 < 0) {
13034                         if (rc == 0)
13035                                 rc = rc2;
13036                         fprintf(stderr,
13037                                 "%s: cannot get PCC state of '%s': %s\n",
13038                                 argv[0], path, strerror(-rc2));
13039                         continue;
13040                 }
13041
13042                 printf("file: %s", path);
13043                 printf(", type: %s", pcc_type2string(state.pccs_type));
13044                 if (state.pccs_type == LU_PCC_NONE &&
13045                     state.pccs_open_count == 0) {
13046                         printf("\n");
13047                         continue;
13048                 }
13049
13050                 printf(", PCC file: %s", state.pccs_path);
13051                 printf(", user number: %u", state.pccs_open_count);
13052                 printf(", flags: %x", state.pccs_flags);
13053                 printf("\n");
13054         }
13055         return rc;
13056 }
13057
13058 /**
13059  * lfs_pcc_list_commands() - List lfs pcc commands.
13060  * @argc: The count of command line arguments.
13061  * @argv: Array of strings for command line arguments.
13062  *
13063  * This function lists lfs pcc commands defined in pcc_cmdlist[].
13064  *
13065  * Return: 0 on success.
13066  */
13067 static int lfs_pcc_list_commands(int argc, char **argv)
13068 {
13069         Parser_list_commands(pcc_cmdlist, 80, 4);
13070
13071         return 0;
13072 }
13073
13074 /**
13075  * lfs_pcc() - Parse and execute lfs pcc commands.
13076  * @argc: The count of lfs pcc command line arguments.
13077  * @argv: Array of strings for lfs pcc command line arguments.
13078  *
13079  * This function parses lfs pcc commands and performs the
13080  * corresponding functions specified in pcc_cmdlist[].
13081  *
13082  * Return: 0 on success or an error code on failure.
13083  */
13084 static int lfs_pcc(int argc, char **argv)
13085 {
13086         char cmd[PATH_MAX];
13087         int rc = 0;
13088
13089         setlinebuf(stdout);
13090
13091         Parser_init("lfs-pcc > ", pcc_cmdlist);
13092
13093         snprintf(cmd, sizeof(cmd), "%s %s", progname, argv[0]);
13094         progname = cmd;
13095         program_invocation_short_name = cmd;
13096         if (argc > 1)
13097                 rc = Parser_execarg(argc - 1, argv + 1, pcc_cmdlist);
13098         else
13099                 rc = Parser_commands();
13100
13101         return rc < 0 ? -rc : rc;
13102 }
13103
13104 static int lfs_list_commands(int argc, char **argv)
13105 {
13106         Parser_list_commands(cmdlist, 80, 4);
13107
13108         return 0;
13109 }
13110
13111 int main(int argc, char **argv)
13112 {
13113         int rc;
13114
13115         /* Ensure that liblustreapi constructor has run */
13116         if (!llapi_liblustreapi_initialized())
13117                 fprintf(stderr, "liblustreapi was not properly initialized\n");
13118
13119         setlinebuf(stdout);
13120         opterr = 0;
13121
13122         Parser_init("lfs > ", cmdlist);
13123
13124         progname = program_invocation_short_name; /* Used in error messages */
13125         if (argc > 1) {
13126                 llapi_set_command_name(argv[1]);
13127                 rc = Parser_execarg(argc - 1, argv + 1, cmdlist);
13128                 llapi_clear_command_name();
13129         } else {
13130                 rc = Parser_commands();
13131         }
13132
13133         return rc < 0 ? -rc : rc;
13134 }
13135
13136 #ifdef _LUSTRE_IDL_H_
13137 /* Everything we need here should be included by lustreapi.h. */
13138 # error "lfs should not depend on lustre_idl.h"
13139 #endif /* _LUSTRE_IDL_H_ */